[cfe-commits] r126816 - in /cfe/trunk: lib/CodeGen/CGExprScalar.cpp test/CodeGen/integer-overflow.c

Eli Friedman eli.friedman at gmail.com
Tue Mar 1 17:49:12 PST 2011


Author: efriedma
Date: Tue Mar  1 19:49:12 2011
New Revision: 126816

URL: http://llvm.org/viewvc/llvm-project?rev=126816&view=rev
Log:
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.


Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/test/CodeGen/integer-overflow.c

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=126816&r1=126815&r2=126816&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Tue Mar  1 19:49:12 2011
@@ -1278,10 +1278,12 @@
 
     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");
   

Modified: cfe/trunk/test/CodeGen/integer-overflow.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/integer-overflow.c?rev=126816&r1=126815&r2=126816&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/integer-overflow.c (original)
+++ cfe/trunk/test/CodeGen/integer-overflow.c Tue Mar  1 19:49:12 2011
@@ -50,11 +50,17 @@
   // 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;
 }





More information about the cfe-commits mailing list