Once we're down to "setcc type constant1, constant2", at least come up
authorNick Lewycky <nicholas@mxc.ca>
Wed, 20 Sep 2006 23:02:24 +0000 (23:02 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Wed, 20 Sep 2006 23:02:24 +0000 (23:02 +0000)
with the right answer.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30550 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/PredicateSimplifier.cpp
test/Transforms/PredicateSimplifier/2006-09-20-SetCC.ll [new file with mode: 0644]

index 8dc1a4d7d090bd28f448e240ef9ee712032e7ad8..f57c0d94d204d5f905aa905c092674588e61b799 100644 (file)
@@ -524,6 +524,9 @@ Value *PredicateSimplifier::resolve(SetCondInst *SCI,
                                     const PropertySet &KP) {
   // Attempt to resolve the SetCondInst to a boolean.
 
+  static ConstantBool *True  = ConstantBool::True,
+                      *False = ConstantBool::False;
+
   Value *SCI0 = resolve(SCI->getOperand(0), KP),
         *SCI1 = resolve(SCI->getOperand(1), KP);
 
@@ -536,10 +539,8 @@ Value *PredicateSimplifier::resolve(SetCondInst *SCI,
 
     if (NE != KP.Properties.end()) {
       switch (SCI->getOpcode()) {
-        case Instruction::SetEQ:
-          return ConstantBool::False;
-        case Instruction::SetNE:
-          return ConstantBool::True;
+        case Instruction::SetEQ: return False;
+        case Instruction::SetNE: return True;
         case Instruction::SetLE:
         case Instruction::SetGE:
         case Instruction::SetLT:
@@ -553,25 +554,20 @@ Value *PredicateSimplifier::resolve(SetCondInst *SCI,
     return SCI;
   }
 
+  uint64_t I1 = CI1->getRawValue(), I2 = CI2->getRawValue();
   switch(SCI->getOpcode()) {
-    case Instruction::SetLE:
-    case Instruction::SetGE:
-    case Instruction::SetEQ:
-      if (CI1->getRawValue() == CI2->getRawValue())
-        return ConstantBool::True;
-      else
-        return ConstantBool::False;
-    case Instruction::SetLT:
-    case Instruction::SetGT:
-    case Instruction::SetNE:
-      if (CI1->getRawValue() == CI2->getRawValue())
-        return ConstantBool::False;
-      else
-        return ConstantBool::True;
+    case Instruction::SetLE: if (I1 <= I2) return True; else return False;
+    case Instruction::SetGE: if (I1 >= I2) return True; else return False;
+    case Instruction::SetEQ: if (I1 == I2) return True; else return False;
+    case Instruction::SetLT: if (I1 <  I2) return True; else return False;
+    case Instruction::SetGT: if (I1 >  I2) return True; else return False;
+    case Instruction::SetNE: if (I1 != I2) return True; else return False;
     default:
       assert(0 && "Unknown opcode in SetContInst.");
       break;
   }
+
+  return SCI;
 }
 
 Value *PredicateSimplifier::resolve(BinaryOperator *BO,
diff --git a/test/Transforms/PredicateSimplifier/2006-09-20-SetCC.ll b/test/Transforms/PredicateSimplifier/2006-09-20-SetCC.ll
new file mode 100644 (file)
index 0000000..b2462f9
--- /dev/null
@@ -0,0 +1,54 @@
+; RUN: llvm-as < %s | opt -predsimplify | llvm-dis | grep br | grep bb8 | grep cond_true$ | grep false
+
+%str = external global [4 x sbyte]             ; <[4 x sbyte]*> [#uses=1]
+
+implementation   ; Functions:
+
+declare int %sprintf(sbyte*, sbyte*, ...)
+
+int %main() {
+entry:
+       br label %cond_true.outer
+
+cond_true.outer:               ; preds = %cond_true.i, %entry
+       %i.0.0.ph = phi int [ 0, %entry ], [ %tmp5, %cond_true.i ]              ; <int> [#uses=1]
+       %j.0.0.ph = phi int [ 0, %entry ], [ %tmp312, %cond_true.i ]            ; <int> [#uses=2]
+       br label %cond_true
+
+cond_true:             ; preds = %return.i, %cond_true.outer
+       %indvar = phi uint [ 0, %cond_true.outer ], [ %indvar.next, %return.i ]         ; <uint> [#uses=2]
+       %indvar = cast uint %indvar to int              ; <int> [#uses=1]
+       %i.0.0 = add int %indvar, %i.0.0.ph             ; <int> [#uses=3]
+       %savedstack = call sbyte* %llvm.stacksave( )            ; <sbyte*> [#uses=2]
+       %tmp.i = seteq int %i.0.0, 0            ; <bool> [#uses=1]
+       %tmp5 = add int %i.0.0, 1               ; <int> [#uses=3]
+       br bool %tmp.i, label %return.i, label %cond_true.i
+
+cond_true.i:           ; preds = %cond_true
+       %tmp.i = alloca [1000 x sbyte]          ; <[1000 x sbyte]*> [#uses=1]
+       %tmp.sub.i = getelementptr [1000 x sbyte]* %tmp.i, int 0, int 0         ; <sbyte*> [#uses=2]
+       %tmp4.i = call int (sbyte*, sbyte*, ...)* %sprintf( sbyte* %tmp.sub.i, sbyte* getelementptr ([4 x sbyte]* %str, int 0, uint 0), int %i.0.0 )            ; <int> [#uses=0]
+       %tmp.i = load sbyte* %tmp.sub.i         ; <sbyte> [#uses=1]
+       %tmp7.i = cast sbyte %tmp.i to int              ; <int> [#uses=1]
+       call void %llvm.stackrestore( sbyte* %savedstack )
+       %tmp312 = add int %tmp7.i, %j.0.0.ph            ; <int> [#uses=2]
+       %tmp19 = setgt int %tmp5, 9999          ; <bool> [#uses=1]
+       br bool %tmp19, label %bb8, label %cond_true.outer
+
+return.i:              ; preds = %cond_true
+       call void %llvm.stackrestore( sbyte* %savedstack )
+       %tmp21 = setgt int %tmp5, 9999          ; <bool> [#uses=1]
+       %indvar.next = add uint %indvar, 1              ; <uint> [#uses=1]
+       br bool %tmp21, label %bb8, label %cond_true
+
+bb8:           ; preds = %return.i, %cond_true.i
+       %j.0.1 = phi int [ %j.0.0.ph, %return.i ], [ %tmp312, %cond_true.i ]            ; <int> [#uses=1]
+       %tmp10 = call int (sbyte*, ...)* %printf( sbyte* getelementptr ([4 x sbyte]* %str, int 0, uint 0), int %j.0.1 )         ; <int> [#uses=0]
+       ret int undef
+}
+
+declare int %printf(sbyte*, ...)
+
+declare sbyte* %llvm.stacksave()
+
+declare void %llvm.stackrestore(sbyte*)