[llvm] [InstCombine] Canonicalize complex boolean expressions into ~((y | z) ^ x) via 3-input truth table (PR #149530)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 18 02:10:12 PDT 2025
================
@@ -50,6 +51,242 @@ static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
return Builder.CreateFCmpFMF(NewPred, LHS, RHS, FMF);
}
+/// This is to create optimal 3-variable boolean logic from truth tables.
+/// currently it supports the cases pertaining to the issue 97044. More cases
+/// can be added based on real-world justification for specific 3 input cases
+/// or with reviewer approval all 256 cases can be added (choose the
+/// canonicalizations found
+/// in x86InstCombine.cpp?)
+static Value *createLogicFromTable3Var(const std::bitset<8> &Table, Value *Op0,
+ Value *Op1, Value *Op2, Value *Root,
+ IRBuilderBase &Builder, bool HasOneUse) {
+ uint8_t TruthValue = Table.to_ulong();
+ auto FoldConstant = [&](bool Val) {
+ Type *Ty = Op0->getType();
+ return Val ? ConstantInt::getTrue(Ty) : ConstantInt::getFalse(Ty);
+ };
+
+ Value *Result = nullptr;
+ switch (TruthValue) {
+ default:
+ return nullptr;
+
+ case 0x00: // Always FALSE
+ Result = FoldConstant(false);
+ break;
+
+ case 0xFF: // Always TRUE
+ Result = FoldConstant(true);
+ break;
+
+ case 0xE1: // ~((Op1 | Op2) ^ Op0)
+ if (!HasOneUse)
----------------
yafet-a wrote:
Thanks. I have added them in the latest commits
https://github.com/llvm/llvm-project/pull/149530
More information about the llvm-commits
mailing list