22#define GEN_PASS_DEF_ARITHUNSIGNEDWHENEQUIVALENTPASS
23#include "mlir/Dialect/Arith/Transforms/Passes.h.inc"
35 CmpIPredicate pred = op.getPredicate();
37 case CmpIPredicate::sle:
38 case CmpIPredicate::slt:
39 case CmpIPredicate::sge:
40 case CmpIPredicate::sgt:
41 return success(llvm::all_of(op.getOperands(), [&solver](
Value v) ->
bool {
42 return succeeded(staticallyNonNegative(solver, v));
53 case CmpIPredicate::sle:
54 return CmpIPredicate::ule;
55 case CmpIPredicate::slt:
56 return CmpIPredicate::ult;
57 case CmpIPredicate::sge:
58 return CmpIPredicate::uge;
59 case CmpIPredicate::sgt:
60 return CmpIPredicate::ugt;
69 DataFlowListener(DataFlowSolver &s) : s(s) {}
72 void notifyOperationErased(Operation *op)
override {
73 s.eraseState(s.getProgramPointAfter(op));
85template <
typename Signed,
typename Un
signed>
87 ConvertOpToUnsigned(MLIRContext *context, DataFlowSolver &s)
88 : OpRewritePattern<
Signed>(context), solver(s) {}
90 LogicalResult matchAndRewrite(Signed op, PatternRewriter &rw)
const override {
95 typename Unsigned::Properties properties{};
96 Unsigned::populateDefaultProperties(
97 OperationName(Unsigned::getOperationName(), rw.
getContext()),
100 op, op->getResultTypes(), op->getOperands(), properties,
101 op->getDiscardableAttrDictionary().getValue());
106 DataFlowSolver &solver;
110 ConvertDivSIToUnsigned(MLIRContext *context, DataFlowSolver &s)
111 : OpRewritePattern<DivSIOp>(context), solver(s) {}
113 LogicalResult matchAndRewrite(DivSIOp op,
114 PatternRewriter &rw)
const override {
118 auto newOp = DivUIOp::create(rw, op.getLoc(), op.getType(), op.getLhs(),
119 op.getRhs(), op.getIsExactAttr());
120 newOp->setDiscardableAttrs(op->getDiscardableAttrDictionary());
126 DataFlowSolver &solver;
130 ConvertCmpIToUnsigned(MLIRContext *context, DataFlowSolver &s)
131 : OpRewritePattern<CmpIOp>(context), solver(s) {}
133 LogicalResult matchAndRewrite(CmpIOp op, PatternRewriter &rw)
const override {
138 op.getLhs(), op.getRhs());
143 DataFlowSolver &solver;
146struct ArithUnsignedWhenEquivalentPass
147 :
public arith::impl::ArithUnsignedWhenEquivalentPassBase<
148 ArithUnsignedWhenEquivalentPass> {
150 void runOnOperation()
override {
151 Operation *op = getOperation();
153 DataFlowSolver solver;
154 solver.
load<SparseConstantPropagation>();
155 solver.
load<DeadCodeAnalysis>();
156 solver.
load<IntegerRangeAnalysis>();
158 return signalPassFailure();
160 DataFlowListener listener(solver);
162 RewritePatternSet patterns(ctx);
172 patterns.
add<ConvertDivSIToUnsigned,
173 ConvertOpToUnsigned<CeilDivSIOp, CeilDivUIOp>,
174 ConvertOpToUnsigned<FloorDivSIOp, DivUIOp>,
175 ConvertOpToUnsigned<RemSIOp, RemUIOp>,
176 ConvertOpToUnsigned<MinSIOp, MinUIOp>,
177 ConvertOpToUnsigned<MaxSIOp, MaxUIOp>,
178 ConvertOpToUnsigned<ExtSIOp, ExtUIOp>, ConvertCmpIToUnsigned>(
static CmpIPredicate toUnsignedPred(CmpIPredicate pred)
Return the unsigned equivalent of a signed comparison predicate, or the predicate itself if there is ...
static LogicalResult isCmpIConvertable(DataFlowSolver &solver, CmpIOp op)
Succeeds when the comparison predicate is a signed operation and all the operands are non-negative,...
MLIRContext * getContext() const
The general data-flow analysis solver.
LogicalResult initializeAndRun(Operation *top, llvm::function_ref< bool(DataFlowAnalysis &)> analysisFilter=nullptr)
Initialize analyses starting from the provided top-level operation and run the analysis until fixpoin...
AnalysisT * load(Args &&...args)
Load an analysis into the solver. Return the analysis instance.
result_range getResults()
MLIRContext * getContext()
Return the context this operation is associated with.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void populateUnsignedWhenEquivalentPatterns(RewritePatternSet &patterns, DataFlowSolver &solver)
Replace signed ops with unsigned ones where they are proven equivalent.
LogicalResult staticallyNonNegative(DataFlowSolver &solver, Operation *op)
Succeeds if an op can be converted to its unsigned equivalent without changing its semantics.
Include the generated interface declarations.
void walkAndApplyPatterns(Operation *op, const FrozenRewritePatternSet &patterns, RewriterBase::Listener *listener=nullptr)
A fast walk-based pattern rewrite driver.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...