Teach the constant folder how to not a cmpinst.
authorNick Lewycky <nicholas@mxc.ca>
Sun, 20 Sep 2009 06:24:51 +0000 (06:24 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 20 Sep 2009 06:24:51 +0000 (06:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82378 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantFold.cpp
test/Transforms/ConstProp/2009-09-19-ConstFold-i1-ConstExpr.ll

index 3f5c7efbb9afa1bfd45c20c78e7a525e417a2e8c..1cf7b290c1e07d3b80e9e459678e162abd2c40be 100644 (file)
@@ -704,6 +704,20 @@ Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
       break;
     case Instruction::Xor:
       if (CI2->equalsInt(0)) return C1;    // X ^ 0 == X
+
+      if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
+        switch (CE1->getOpcode()) {
+        default: break;
+        case Instruction::ICmp:
+        case Instruction::FCmp:
+          // icmp pred ^ true -> icmp !pred
+          assert(CI2->equalsInt(1));
+         CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
+          pred = CmpInst::getInversePredicate(pred);
+          return ConstantExpr::getCompare(pred, CE1->getOperand(0),
+                                          CE1->getOperand(1));
+        }
+      }
       break;
     case Instruction::AShr:
       // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
index 0a40c0ce2c4034d3c5b083d8d35b34b1e477fc56..66a3a85b3ce5b6214e20d6d07ae5a308a22d3d0f 100644 (file)
@@ -28,3 +28,9 @@ global i1 icmp ule (i32* bitcast (i8* @X to i32*), i32* bitcast (i8* @Y to i32*)
 ; CHECK-NOT: bitcast
 ; CHECK: icmp
 
+global i1 icmp eq (i1 icmp ult (i8* @X, i8* @Y), i1 false)
+; CHECK-NOT: false
+; CHECK: icmp
+global i1 icmp eq (i1 icmp ult (i8* @X, i8* @Y), i1 true)
+; CHECK-NOT: true
+; CHECK: icmp