Handle "if ((x|y) != 0)" for ints like we do for bools. Fixes missed
authorNick Lewycky <nicholas@mxc.ca>
Sun, 22 Oct 2006 21:36:41 +0000 (21:36 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 22 Oct 2006 21:36:41 +0000 (21:36 +0000)
optimization opportunity pointed out by Chris Lattner.

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

lib/Transforms/Scalar/PredicateSimplifier.cpp
test/Transforms/PredicateSimplifier/2006-10-22-IntOr.ll [new file with mode: 0644]

index 4d86f6aa46ce1c7b26cf91e2dbe16bd15ec534d5..ca0d056e58cb83921cb87c704669d3b20a21a677 100644 (file)
@@ -334,32 +334,35 @@ namespace {
           if (V1 == ConstantBool::getFalse())
             add(Opcode, BO->getOperand(0), BO->getOperand(1), true);
           break;
-        case Instruction::And:
-          if (V1 == ConstantBool::getTrue()) {
+        case Instruction::And: {
+          ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
+          if (CI && CI->isAllOnesValue()) {
             add(Opcode, V1, BO->getOperand(0), false);
             add(Opcode, V1, BO->getOperand(1), false);
           }
-          break;
-        case Instruction::Or:
-          if (V1 == ConstantBool::getFalse()) {
+        } break;
+        case Instruction::Or: {
+          ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
+          if (CI && CI->isNullValue()) {
             add(Opcode, V1, BO->getOperand(0), false);
             add(Opcode, V1, BO->getOperand(1), false);
           }
-          break;
-        case Instruction::Xor:
-          if (V1 == ConstantBool::getTrue()) {
+        } break;
+        case Instruction::Xor: {
+          ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V1);
+          if (CI->isAllOnesValue()) {
             if (BO->getOperand(0) == V1)
               add(Opcode, ConstantBool::getFalse(), BO->getOperand(1), false);
             if (BO->getOperand(1) == V1)
               add(Opcode, ConstantBool::getFalse(), BO->getOperand(0), false);
           }
-          if (V1 == ConstantBool::getFalse()) {
+          if (CI->isNullValue()) {
             if (BO->getOperand(0) == ConstantBool::getTrue())
               add(Opcode, ConstantBool::getTrue(), BO->getOperand(1), false);
             if (BO->getOperand(1) == ConstantBool::getTrue())
               add(Opcode, ConstantBool::getTrue(), BO->getOperand(0), false);
           }
-          break;
+        } break;
         default:
           break;
         }
diff --git a/test/Transforms/PredicateSimplifier/2006-10-22-IntOr.ll b/test/Transforms/PredicateSimplifier/2006-10-22-IntOr.ll
new file mode 100644 (file)
index 0000000..181952b
--- /dev/null
@@ -0,0 +1,21 @@
+; RUN: llvm-as < %s | opt -predsimplify -instcombine -simplifycfg | llvm-dis | grep -v declare | not grep fail
+
+int %f(int %x, int %y) {
+entry:
+       %tmp2 = or int %x, %y           ; <int> [#uses=1]
+       %tmp = setne int %tmp2, 0               ; <bool> [#uses=1]
+       br bool %tmp, label %cond_true, label %return
+
+cond_true:             ; preds = %entry
+       %tmp4 = seteq int %x, 0         ; <bool> [#uses=1]
+       br bool %tmp4, label %cond_true5, label %return
+
+cond_true5:            ; preds = %cond_true
+       %tmp6 = call int %fail( )               ; <int> [#uses=0]
+       ret int %tmp6
+
+return:                ; preds = %cond_next7
+       ret int 0
+}
+
+declare int %fail()