diff options
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 18 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/icmp-logical.ll | 14 |
2 files changed, 21 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 60d1cde971d..98cdd3c9d81 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1814,9 +1814,21 @@ Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType())); Value *CmpQ = Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType())); - auto LogicOpc = Pred == ICmpInst::Predicate::ICMP_EQ ? Instruction::And - : Instruction::Or; - return BinaryOperator::Create(LogicOpc, CmpP, CmpQ); + auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; + return BinaryOperator::Create(BOpc, CmpP, CmpQ); + } + + // Are we using xors to bitwise check for a pair of (in)equalities? Convert to + // a shorter form that has more potential to be folded even further. + Value *X1, *X2, *X3, *X4; + if (match(Or->getOperand(0), m_OneUse(m_Xor(m_Value(X1), m_Value(X2)))) && + match(Or->getOperand(1), m_OneUse(m_Xor(m_Value(X3), m_Value(X4))))) { + // ((X1 ^ X2) || (X3 ^ X4)) == 0 --> (X1 == X2) && (X3 == X4) + // ((X1 ^ X2) || (X3 ^ X4)) != 0 --> (X1 != X2) || (X3 != X4) + Value *Cmp12 = Builder.CreateICmp(Pred, X1, X2); + Value *Cmp34 = Builder.CreateICmp(Pred, X3, X4); + auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; + return BinaryOperator::Create(BOpc, Cmp12, Cmp34); } return nullptr; diff --git a/llvm/test/Transforms/InstCombine/icmp-logical.ll b/llvm/test/Transforms/InstCombine/icmp-logical.ll index 248d89d934e..aa95cc5a131 100644 --- a/llvm/test/Transforms/InstCombine/icmp-logical.ll +++ b/llvm/test/Transforms/InstCombine/icmp-logical.ll @@ -157,10 +157,9 @@ define i1 @fold_mask_cmps_to_true(i32 %x) { define i1 @cmpeq_bitwise(i8 %a, i8 %b, i8 %c, i8 %d) { ; CHECK-LABEL: @cmpeq_bitwise( -; CHECK-NEXT: [[XOR1:%.*]] = xor i8 %a, %b -; CHECK-NEXT: [[XOR2:%.*]] = xor i8 %c, %d -; CHECK-NEXT: [[OR:%.*]] = or i8 [[XOR1]], [[XOR2]] -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[OR]], 0 +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 %a, %b +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 %c, %d +; CHECK-NEXT: [[CMP:%.*]] = and i1 [[TMP1]], [[TMP2]] ; CHECK-NEXT: ret i1 [[CMP]] ; %xor1 = xor i8 %a, %b @@ -172,10 +171,9 @@ define i1 @cmpeq_bitwise(i8 %a, i8 %b, i8 %c, i8 %d) { define <2 x i1> @cmpne_bitwise(<2 x i64> %a, <2 x i64> %b, <2 x i64> %c, <2 x i64> %d) { ; CHECK-LABEL: @cmpne_bitwise( -; CHECK-NEXT: [[XOR1:%.*]] = xor <2 x i64> %a, %b -; CHECK-NEXT: [[XOR2:%.*]] = xor <2 x i64> %c, %d -; CHECK-NEXT: [[OR:%.*]] = or <2 x i64> [[XOR1]], [[XOR2]] -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i64> [[OR]], zeroinitializer +; CHECK-NEXT: [[TMP1:%.*]] = icmp ne <2 x i64> %a, %b +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <2 x i64> %c, %d +; CHECK-NEXT: [[CMP:%.*]] = or <2 x i1> [[TMP1]], [[TMP2]] ; CHECK-NEXT: ret <2 x i1> [[CMP]] ; %xor1 = xor <2 x i64> %a, %b |

