[InstCombine] Matchers work with both ConstExpr and Instructions.
authorDavide Italiano <davide@freebsd.org>
Mon, 17 Apr 2017 20:49:50 +0000 (20:49 +0000)
committerDavide Italiano <davide@freebsd.org>
Mon, 17 Apr 2017 20:49:50 +0000 (20:49 +0000)
So, `cast<Instruction>` is not guaranteed to succeed. Change the
code so that we create a new constant and use it in the newly
created instruction, as it's done in other places in InstCombine.

OK'ed by Sanjay/Craig. Fixes PR32686.

llvm-svn: 300495

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/pr32686.ll [new file with mode: 0644]

index b2a41c6..2906d34 100644 (file)
@@ -2078,7 +2078,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
       Value *NOr = Builder->CreateOr(A, Op1);
       NOr->takeName(Op0);
       return BinaryOperator::CreateXor(NOr,
-                                       cast<Instruction>(Op0)->getOperand(1));
+                                       ConstantInt::get(NOr->getType(), *C));
     }
 
     // Y|(X^C) -> (X|Y)^C iff Y&C == 0
@@ -2087,7 +2087,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
       Value *NOr = Builder->CreateOr(A, Op0);
       NOr->takeName(Op0);
       return BinaryOperator::CreateXor(NOr,
-                                       cast<Instruction>(Op1)->getOperand(1));
+                                       ConstantInt::get(NOr->getType(), *C));
     }
   }
 
diff --git a/llvm/test/Transforms/InstCombine/pr32686.ll b/llvm/test/Transforms/InstCombine/pr32686.ll
new file mode 100644 (file)
index 0000000..7abda23
--- /dev/null
@@ -0,0 +1,22 @@
+; RUN: opt -S -instcombine %s | FileCheck %s
+
+; CHECK-LABEL: define void @tinkywinky
+; CHECK-NEXT:   %patatino = load i8, i8* @a, align 1
+; CHECK-NEXT:   %tobool = icmp eq i8 %patatino, 0
+; CHECK-NEXT:   %1 = zext i1 %tobool to i32
+; CHECK-NEXT:   %or1 = or i32 %1, or (i32 zext (i1 icmp ne (i32* bitcast (i8* @a to i32*), i32* @b) to i32), i32 2)
+; CHECK-NEXT:   store i32 %or1, i32* @b, align 4
+; CHECK-NEXT:   ret void
+
+@a = external global i8
+@b = external global i32
+
+define void @tinkywinky() {
+  %patatino = load i8, i8* @a
+  %tobool = icmp ne i8 %patatino, 0
+  %lnot = xor i1 %tobool, true
+  %lnot.ext = zext i1 %lnot to i32
+  %or = or i32 xor (i32 zext (i1 icmp ne (i32* bitcast (i8* @a to i32*), i32* @b) to i32), i32 2), %lnot.ext
+  store i32 %or, i32* @b, align 4
+  ret void
+}