[llvm] [InstCombine] Canoncalize complex boolean expressions into ~((y | z) ^ x) (PR #149530)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 09:17:11 PDT 2025


yafet-a wrote:

## Pull Request: Response to Changes Requested and Discussion in #97044

### Design Change: Pattern Matching → Truth Table Approach

**Before:**
- Manual pattern matching with hardcoded transformations
- Difficult to extend and maintain

**After:**
- Generalized truth table canonicalization using 3 input truth tables (similar to createLogicFromTruthTable)
- Separates: extract variables → build truth table → canonicalize
- Extensible for other 3 input examples (can be extended similarly to  X86's `simplifyTernaryLogic()`

## Key Changes

- Added functions:
  - `extractThreeVariables()` 
  - `extractThreeBitTruthTable()` - evaluates all 8 input combinations
  - `createLogicFromTable3Var()` - maps to canonical forms
  - `foldThreeVarBoolExpr()` - orchestrates the process

- Currently implements cases needed for issue #97044:
  - 0xE1: ~((Op1 | Op2) ^ Op0)
  - 0x60: `Op0 & (Op1 ^ Op2)` 
  - 0xD2: ((Op1 | Op2) ^ Op0) ^ Op1

## Testing & Validation

- Passes the new regression test pr97044.ll  which cover the canonicalization cases from issue #97044 4-way OR
- All existing InstCombine tests pass

## Future Extensibility
Framework can support all 256 possible 3-variable boolean functions. For now may be better to see where this goes and incrementally enable addition of new cases with real-world justification.


https://github.com/llvm/llvm-project/pull/149530


More information about the llvm-commits mailing list