[llvm] [InstCombine] Fold comparison of adding two z/sext booleans (PR #67895)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 2 02:33:17 PDT 2023
================
@@ -2895,19 +2896,86 @@ Instruction *InstCombinerImpl::foldICmpSubConstant(ICmpInst &Cmp,
return new ICmpInst(SwappedPred, Add, ConstantInt::get(Ty, ~C));
}
+static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
+ Value *Op1, IRBuilderBase &Builder) {
+ switch (Table.to_ulong()) {
+ case 0: // 0 0 0 0
+ return Builder.getFalse();
+ case 1: // 0 0 0 1
+ return Builder.CreateNot(Builder.CreateOr(Op0, Op1));
+ case 2: // 0 0 1 0
+ return Builder.CreateAnd(Builder.CreateNot(Op0), Op1);
+ case 3: // 0 0 1 1
+ return Op1;
+ case 4: // 0 1 0 0
+ return Builder.CreateAnd(Op0, Builder.CreateNot(Op1));
+ case 5: // 0 1 0 1
+ return Op0;
+ case 6: // 0 1 1 0
+ return Builder.CreateXor(Op0, Op1);
+ case 7: // 0 1 1 1
+ return Builder.CreateNot(Builder.CreateAnd(Op0, Op1));
+ case 8: // 1 0 0 0
+ return Builder.CreateAnd(Op0, Op1);
+ case 9: // 1 0 0 1
+ return Builder.CreateNot(Builder.CreateXor(Op0, Op1));
+ case 10: // 1 0 1 0
+ return Builder.CreateNot(Op1);
+ case 11: // 1 0 1 1
+ return Builder.CreateOr(Builder.CreateNot(Op0), Op1);
+ case 12: // 1 1 0 0
+ return Builder.CreateNot(Op1);
+ case 13: // 1 1 0 1
+ return Builder.CreateOr(Op0, Builder.CreateNot(Op1));
+ case 14: // 1 1 1 0
+ return Builder.CreateOr(Op0, Op1);
+ case 15: // 1 1 1 1
+ return Builder.getTrue();
+ }
+ return nullptr;
+}
+
/// Fold icmp (add X, Y), C.
Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
BinaryOperator *Add,
const APInt &C) {
Value *Y = Add->getOperand(1);
+ Value *X = Add->getOperand(0);
+
+ Value *Op0, *Op1;
+ Instruction *Ext0, *Ext1;
+ const CmpInst::Predicate Pred = Cmp.getPredicate();
+ if (match(Add, m_c_Add(m_CombineAnd(m_Instruction(Ext0),
----------------
nikic wrote:
m_c_Add -> m_Add
Also, we are missing a one-use check on the add itself. The one-use checks on the extensions are pointless if the add itself is multi-use.
https://github.com/llvm/llvm-project/pull/67895
More information about the llvm-commits
mailing list