[llvm] cfdc967 - [Instcombine] Fix uses of undef (PR46940)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 11 14:25:10 PDT 2020
Author: Kazu Hirata
Date: 2020-08-11T14:13:32-07:00
New Revision: cfdc96714bdfeabea31b1f4d2e0c65e424933994
URL: https://github.com/llvm/llvm-project/commit/cfdc96714bdfeabea31b1f4d2e0c65e424933994
DIFF: https://github.com/llvm/llvm-project/commit/cfdc96714bdfeabea31b1f4d2e0c65e424933994.diff
LOG: [Instcombine] Fix uses of undef (PR46940)
Without this patch, we attempt to distribute And over Xor even in
unsafe circumstances like so:
undef & (true ^ true) ==> (undef & true) ^ (undef & true)
and evaluate it to undef instead of false. Note that "true ^ true"
may show up implicitly with one true being part of a PHI node.
This patch fixes the problem by teaching SimplifyUsingDistributiveLaws
to not use undef as part of simplifications.
Reviewers: spatel, aqjune, nikic, lebedev.ri, fhahn, jdoerfert
Differential Revision: https://reviews.llvm.org/D85687
Added:
llvm/test/Transforms/InstCombine/dont-distribute-phi.ll
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index be86c1f111a3..e43dfaa34ccb 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -740,8 +740,10 @@ Value *InstCombinerImpl::SimplifyUsingDistributiveLaws(BinaryOperator &I) {
Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
- Value *L = SimplifyBinOp(TopLevelOpcode, A, C, SQ.getWithInstruction(&I));
- Value *R = SimplifyBinOp(TopLevelOpcode, B, C, SQ.getWithInstruction(&I));
+ // Disable the use of undef because it's not safe to distribute undef.
+ auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
+ Value *L = SimplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
+ Value *R = SimplifyBinOp(TopLevelOpcode, B, C, SQDistributive);
// Do "A op C" and "B op C" both simplify?
if (L && R) {
@@ -777,8 +779,10 @@ Value *InstCombinerImpl::SimplifyUsingDistributiveLaws(BinaryOperator &I) {
Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
- Value *L = SimplifyBinOp(TopLevelOpcode, A, B, SQ.getWithInstruction(&I));
- Value *R = SimplifyBinOp(TopLevelOpcode, A, C, SQ.getWithInstruction(&I));
+ // Disable the use of undef because it's not safe to distribute undef.
+ auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
+ Value *L = SimplifyBinOp(TopLevelOpcode, A, B, SQDistributive);
+ Value *R = SimplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
// Do "A op B" and "A op C" both simplify?
if (L && R) {
diff --git a/llvm/test/Transforms/InstCombine/dont-distribute-phi.ll b/llvm/test/Transforms/InstCombine/dont-distribute-phi.ll
new file mode 100644
index 000000000000..bc7ddacbed16
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/dont-distribute-phi.ll
@@ -0,0 +1,33 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+;
+; This test ensures that InstCombine does not distribute And over Xor
+; using simplifications involving undef.
+
+define zeroext i1 @foo(i32 %arg) {
+; CHECK-LABEL: @foo(
+
+entry:
+ %cmp1 = icmp eq i32 %arg, 37
+ br i1 %cmp1, label %bb_then, label %bb_else
+
+bb_then:
+ call void @bar()
+ br label %bb_exit
+
+bb_else:
+ %cmp2 = icmp slt i32 %arg, 17
+ br label %bb_exit
+
+; CHECK: bb_exit:
+; CHECK-NEXT: [[PHI1:%.*]] = phi i1 [ [[CMP2:%.*]], [[BB_ELSE:%.*]] ], [ undef, [[BB_THEN:%.*]] ]
+; CHECK-NEXT: [[XOR1:%.*]] = xor i1 [[CMP1:%.*]], true
+; CHECK-NEXT: [[AND1:%.*]] = and i1 [[PHI1]], [[XOR1]]
+; CHECK-NEXT: ret i1 [[AND1]]
+bb_exit:
+ %phi1 = phi i1 [ %cmp2, %bb_else ], [ undef, %bb_then ]
+ %xor1 = xor i1 %cmp1, true
+ %and1 = and i1 %phi1, %xor1
+ ret i1 %and1
+}
+
+declare void @bar()
More information about the llvm-commits
mailing list