diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -2091,9 +2091,17 @@ return V; // And distributes over Xor. Try some generic simplifications based on this. - if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1, - Instruction::Xor, Q, MaxRecurse)) - return V; + { + // Disable the use of undef. Note that the following equivalence does not + // hold: + // + // undef & (true ^ true) <===> (undef & true) ^ (undef & true) + auto QNoUndef = Q; + QNoUndef.CanUseUndef = false; + if (Value *V = expandCommutativeBinOp( + Instruction::And, Op0, Op1, Instruction::Xor, QNoUndef, MaxRecurse)) + return V; + } // If the operation is with the result of a select instruction, check whether // operating on either branch of the select always yields the same value. 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,16 @@ 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)); + auto SQDistributive = SQ.getWithInstruction(&I); + if (TopLevelOpcode == Instruction::And && InnerOpcode == Instruction::Xor) { + // Disable the use of undef if we are distributing And over Xor. Note + // that the following equivalence does not hold: + // + // undef & (true ^ true) <===> (undef & true) ^ (undef & true) + SQDistributive.CanUseUndef = false; + } + 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 +785,16 @@ 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)); + auto SQDistributive = SQ.getWithInstruction(&I); + if (TopLevelOpcode == Instruction::And && InnerOpcode == Instruction::Xor) { + // Disable the use of undef if we are distributing And over Xor. Note + // that the following equivalence does not hold: + // + // undef & (true ^ true) <===> (undef & true) ^ (undef & true) + SQDistributive.CanUseUndef = false; + } + 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()