[PATCH] D85687: [Instcombine] Fix uses of undef (PR46940)

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 14:25:21 PDT 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcfdc96714bdf: [Instcombine] Fix uses of undef (PR46940) (authored by kazu).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85687/new/

https://reviews.llvm.org/D85687

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/dont-distribute-phi.ll


Index: llvm/test/Transforms/InstCombine/dont-distribute-phi.ll
===================================================================
--- /dev/null
+++ 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()
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -740,8 +740,10 @@
     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 *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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85687.284897.patch
Type: text/x-patch
Size: 2713 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200811/c5bcdc6c/attachment.bin>


More information about the llvm-commits mailing list