Skip to content

Commit 6775523

Browse files
committed
[MLIR][Arith] add and(a, or(a,b)) folder
1 parent c1f0e68 commit 6775523

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,18 @@ OpFoldResult arith::AndIOp::fold(FoldAdaptor adaptor) {
896896
if (Value result = foldAndIofAndI(*this))
897897
return result;
898898

899+
/// and(a, or(a, b)) -> a
900+
for (int i = 0; i < 2; i++) {
901+
auto a = getOperand(1 - i);
902+
if (auto orOp = getOperand(i).getDefiningOp<arith::OrIOp>()) {
903+
for (int j = 0; j < 2; j++) {
904+
if (orOp->getOperand(j) == a) {
905+
return a;
906+
}
907+
}
908+
}
909+
}
910+
899911
return constFoldBinaryOp<IntegerAttr>(
900912
adaptor.getOperands(),
901913
[](APInt a, const APInt &b) { return std::move(a) & b; });

mlir/test/Dialect/Arith/canonicalize.mlir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,6 +2901,15 @@ func.func @andand3(%a : i32, %b : i32) -> i32 {
29012901
return %res : i32
29022902
}
29032903

2904+
// CHECK-LABEL: @andor
2905+
// CHECK-SAME: (%[[A:.*]]: i32, %[[B:.*]]: i32)
2906+
// CHECK: return %[[A]]
2907+
func.func @andor(%a : i32, %b : i32) -> i32 {
2908+
%c = arith.ori %a, %b : i32
2909+
%res = arith.andi %a, %b : i32
2910+
return %res : i32
2911+
}
2912+
29042913
// -----
29052914

29062915
// CHECK-LABEL: @truncIShrSIToTrunciShrUI

0 commit comments

Comments
 (0)