diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/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) { diff --git a/llvm/test/Transforms/InstCombine/dont-distribute-phi.ll b/llvm/test/Transforms/InstCombine/dont-distribute-phi.ll new file mode 100644 --- /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()