diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/CodeGen/CGExprScalar.cpp | 24 | ||||
-rw-r--r-- | clang/test/CodeGen/integer-overflow.c | 22 |
2 files changed, 37 insertions, 9 deletions
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 63d14f0e53b..208e1a6c896 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -1179,11 +1179,27 @@ EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, } else if (isa<llvm::IntegerType>(InVal->getType())) { NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); - // Signed integer overflow is undefined behavior. - if (ValTy->isSignedIntegerType()) - NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); - else + if (!ValTy->isSignedIntegerType()) + // Unsigned integer inc is always two's complement. NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); + else { + switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { + case LangOptions::SOB_Undefined: + NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); + break; + case LangOptions::SOB_Defined: + NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); + break; + case LangOptions::SOB_Trapping: + BinOpInfo BinOp; + BinOp.LHS = InVal; + BinOp.RHS = NextVal; + BinOp.Ty = E->getType(); + BinOp.Opcode = BinaryOperator::Add; + BinOp.E = E; + return EmitOverflowCheckedBinOp(BinOp); + } + } } else { // Add the inc/dec to the real part. if (InVal->getType()->isFloatTy()) diff --git a/clang/test/CodeGen/integer-overflow.c b/clang/test/CodeGen/integer-overflow.c index dea3d0a1a3e..9bed741b323 100644 --- a/clang/test/CodeGen/integer-overflow.c +++ b/clang/test/CodeGen/integer-overflow.c @@ -21,13 +21,25 @@ void test1() { // TRAPV: llvm.ssub.with.overflow.i32 f11G = a - b; - // DEFAULT: sub nsw i32 0, - // WRAPV: sub i32 0, - // TRAPV: llvm.ssub.with.overflow.i32 - f11G = -a; - // DEFAULT: mul nsw i32 // WRAPV: mul i32 // TRAPV: llvm.smul.with.overflow.i32 f11G = a * b; + + // DEFAULT: sub nsw i32 0, + // WRAPV: sub i32 0, + // TRAPV: llvm.ssub.with.overflow.i32(i32 0 + f11G = -a; + + // PR7426 - Overflow checking for increments. + + // DEFAULT: add nsw i32 {{.*}}, 1 + // WRAPV: add i32 {{.*}}, 1 + // TRAPV: llvm.sadd.with.overflow.i32({{.*}}, i32 1) + ++a; + + // DEFAULT: add nsw i32 {{.*}}, -1 + // WRAPV: add i32 {{.*}}, -1 + // TRAPV: llvm.sadd.with.overflow.i32({{.*}}, i32 -1) + --a; } |