aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam S. Moses <gh@wsmoses.com>2025-05-07 19:32:51 -0500
committerWilliam S. Moses <gh@wsmoses.com>2025-05-07 19:35:05 -0500
commit6775523e4f0380e4de662a2fc9f3bcc6da926d85 (patch)
tree8c9bfe4b2cba52cc2c264695d60aced14dcba2b7
parentc1f0e68cec4218c9d51a4ad0a6f6d878ed573dfe (diff)
downloadllvm-users/wmoses/arith.zip
llvm-users/wmoses/arith.tar.gz
llvm-users/wmoses/arith.tar.bz2
[MLIR][Arith] add and(a, or(a,b)) folderusers/wmoses/arith
-rw-r--r--mlir/lib/Dialect/Arith/IR/ArithOps.cpp12
-rw-r--r--mlir/test/Dialect/Arith/canonicalize.mlir9
2 files changed, 21 insertions, 0 deletions
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 3b30871..c7bc944 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -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++) {
+ auto a = getOperand(1 - i);
+ if (auto orOp = getOperand(i).getDefiningOp<arith::OrIOp>()) {
+ for (int j = 0; j < 2; j++) {
+ if (orOp->getOperand(j) == a) {
+ return a;
+ }
+ }
+ }
+ }
+
return constFoldBinaryOp<IntegerAttr>(
adaptor.getOperands(),
[](APInt a, const APInt &b) { return std::move(a) & b; });
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index d62c5b1..fb29e72 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -2901,6 +2901,15 @@ func.func @andand3(%a : i32, %b : i32) -> i32 {
return %res : i32
}
+// CHECK-LABEL: @andor
+// CHECK-SAME: (%[[A:.*]]: i32, %[[B:.*]]: i32)
+// CHECK: return %[[A]]
+func.func @andor(%a : i32, %b : i32) -> i32 {
+ %c = arith.ori %a, %b : i32
+ %res = arith.andi %a, %b : i32
+ return %res : i32
+}
+
// -----
// CHECK-LABEL: @truncIShrSIToTrunciShrUI