diff options
author | Dan Gohman <gohman@apple.com> | 2009-08-12 01:16:29 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2009-08-12 01:16:29 +0000 |
commit | bf933a0cb628670490c15367b3f5ccb3193354a7 (patch) | |
tree | 292c085394a4dce127b28cef2a428b30e421f611 /lib/CodeGen/CGExprScalar.cpp | |
parent | 6cc670e2eb6d5bae0e41a8ab8be4f02c4a0c72cf (diff) |
Use the new nsw form of add for signed integer addition.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78765 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGExprScalar.cpp')
-rw-r--r-- | lib/CodeGen/CGExprScalar.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index c0f6335d88..564322b4c8 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -741,7 +741,12 @@ Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, NextVal = llvm::ConstantInt::getTrue(VMContext); } else if (isa<llvm::IntegerType>(InVal->getType())) { NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); - NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); + + // Signed integer overflow is undefined behavior. + if (ValTy->isSignedIntegerType()) + NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); + else + NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); } else { // Add the inc/dec to the real part. if (InVal->getType() == llvm::Type::FloatTy) @@ -1033,7 +1038,11 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { if (Ops.LHS->getType()->isFPOrFPVector()) return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); - + + // Signed integer overflow is undefined behavior. + if (Ops.Ty->isSignedIntegerType()) + return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); + return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); } |