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

Chris Lattner sabre at nondot.org
Sat Jun 26 17:18:28 CDT 2010


Author: lattner
Date: Sat Jun 26 17:18:28 2010
New Revision: 106962

URL: http://llvm.org/viewvc/llvm-project?rev=106962&view=rev
Log:
fix inc/dec to honor -fwrapv and -ftrapv, implementing PR7426.

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=106962&r1=106961&r2=106962&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sat Jun 26 17:18:28 2010
@@ -1179,11 +1179,27 @@
   } 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())

Modified: cfe/trunk/test/CodeGen/integer-overflow.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/integer-overflow.c?rev=106962&r1=106961&r2=106962&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/integer-overflow.c (original)
+++ cfe/trunk/test/CodeGen/integer-overflow.c Sat Jun 26 17:18:28 2010
@@ -21,13 +21,25 @@
   // 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;
 }




More information about the cfe-commits mailing list