From 640d3267228415328c0feb2bcc35757230ab4db7 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 26 Jun 2010 22:18:28 +0000 Subject: [PATCH] fix inc/dec to honor -fwrapv and -ftrapv, implementing PR7426. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@106962 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprScalar.cpp | 24 ++++++++++++++++++++---- test/CodeGen/integer-overflow.c | 22 +++++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 63d14f0e53..208e1a6c89 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -1179,11 +1179,27 @@ EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, } else if (isa(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/test/CodeGen/integer-overflow.c b/test/CodeGen/integer-overflow.c index dea3d0a1a3..9bed741b32 100644 --- a/test/CodeGen/integer-overflow.c +++ b/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; } -- 2.40.0