aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Friedman <eli.friedman@gmail.com>2011-03-02 01:49:12 +0000
committerEli Friedman <eli.friedman@gmail.com>2011-03-02 01:49:12 +0000
commitfa0b409ef81f1f70edfa72857c4e211ff50998d5 (patch)
tree23ee7de26fdfc8a32bbd7f2f83240624d69ec8b1
parentdb04e2e26dd991ecddb6badf8c91ee46de80c31c (diff)
PR9350: increment/decrement of char (and anything else narrower than int)
can't overflow due to promotion rules; emit a wrapping add for those cases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126816 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGExprScalar.cpp8
-rw-r--r--test/CodeGen/integer-overflow.c8
2 files changed, 12 insertions, 4 deletions
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index c4ff8df381..b11236a97f 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -1278,10 +1278,12 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
- if (type->isSignedIntegerType())
+ // Note that signed integer inc/dec with width less than int can't
+ // overflow because of promotion rules; we're just eliding a few steps here.
+ if (type->isSignedIntegerType() &&
+ value->getType()->getPrimitiveSizeInBits() >=
+ CGF.CGM.IntTy->getBitWidth())
value = EmitAddConsiderOverflowBehavior(E, value, amt, isInc);
-
- // Unsigned integer inc is always two's complement.
else
value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
diff --git a/test/CodeGen/integer-overflow.c b/test/CodeGen/integer-overflow.c
index 066ef89574..1d460656a1 100644
--- a/test/CodeGen/integer-overflow.c
+++ b/test/CodeGen/integer-overflow.c
@@ -50,11 +50,17 @@ void test1() {
// TRAPV_HANDLER: foo(
--a;
-
// -fwrapv should turn off inbounds for GEP's, PR9256
extern int* P;
++P;
// DEFAULT: getelementptr inbounds i32*
// WRAPV: getelementptr i32*
// TRAPV: getelementptr inbounds i32*
+
+ // PR9350: char increment never overflows.
+ extern volatile signed char PR9350;
+ // DEFAULT: add i8 {{.*}}, 1
+ // WRAPV: add i8 {{.*}}, 1
+ // TRAPV: add i8 {{.*}}, 1
+ ++PR9350;
}