[Mlir-commits] [mlir] [MLIR][Arith] add and(a, or(a, b)) folder (PR #138998)

Mehdi Amini llvmlistbot at llvm.org
Tue Jun 3 17:03:53 PDT 2025


================
@@ -896,6 +896,18 @@ OpFoldResult arith::AndIOp::fold(FoldAdaptor adaptor) {
   if (Value result = foldAndIofAndI(*this))
     return result;
 
+  /// and(a, or(a, b)) -> a
+  for (int i = 0; i < 2; i++) {
----------------
joker-eph wrote:

Oh I missed the `1-i` in the code, this looks like all code obfuscation to me.

I would write this with a lambda instead, something like:

```
auto matchAndOr = [&] (Value lhs, Value rhs) {
  auto orOp = lhs.getDefiningOp<arith::OrIOp>();
   if (!orOp) return false;
   for (Value orOperand : orOp->getOperands())
     if (orOperand == rhs) return true;
   return false;
};
/// and(or(a, b), a) -> a
Value lhs = getOperand(0);
Value rhs = getOperand(1);
if (matchAndOr(lhs, rhs)) return rhs;
/// And is commutative: and(a, or(a, b)) -> a
if (matchAndOr(rhs, lhs)) return lhs;
```

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


More information about the Mlir-commits mailing list