From: Dan Gohman Date: Wed, 12 Aug 2009 01:16:29 +0000 (+0000) Subject: Use the new nsw form of add for signed integer addition. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf933a0cb628670490c15367b3f5ccb3193354a7;p=clang 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 --- 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(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"); }