From: Davide Italiano Date: Mon, 17 Apr 2017 20:49:50 +0000 (+0000) Subject: [InstCombine] Matchers work with both ConstExpr and Instructions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=61710dfa3ab1cec87b34ad912d50e0cba6e9a39c;p=llvm [InstCombine] Matchers work with both ConstExpr and Instructions. So, `cast` 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300495 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index b2a41c69920..2906d34ee3d 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2078,7 +2078,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { Value *NOr = Builder->CreateOr(A, Op1); NOr->takeName(Op0); return BinaryOperator::CreateXor(NOr, - cast(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(Op1)->getOperand(1)); + ConstantInt::get(NOr->getType(), *C)); } } diff --git a/test/Transforms/InstCombine/pr32686.ll b/test/Transforms/InstCombine/pr32686.ll new file mode 100644 index 00000000000..7abda234920 --- /dev/null +++ b/test/Transforms/InstCombine/pr32686.ll @@ -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 +}