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 {
101 DataFlowSolver &solver;
105 ConvertCmpIToUnsigned(MLIRContext *context, DataFlowSolver &s)
106 : OpRewritePattern<CmpIOp>(context), solver(s) {}
108 LogicalResult matchAndRewrite(CmpIOp op, PatternRewriter &rw)
const override {
113 op.getLhs(), op.getRhs());
118 DataFlowSolver &solver;
121struct ArithUnsignedWhenEquivalentPass
122 :
public arith::impl::ArithUnsignedWhenEquivalentPassBase<
123 ArithUnsignedWhenEquivalentPass> {
125 void runOnOperation()
override {
126 Operation *op = getOperation();
128 DataFlowSolver solver;
129 solver.
load<SparseConstantPropagation>();
130 solver.
load<DeadCodeAnalysis>();
131 solver.
load<IntegerRangeAnalysis>();
133 return signalPassFailure();
135 DataFlowListener listener(solver);
137 RewritePatternSet patterns(ctx);
147 patterns.
add<ConvertOpToUnsigned<DivSIOp, DivUIOp>,
148 ConvertOpToUnsigned<CeilDivSIOp, CeilDivUIOp>,
149 ConvertOpToUnsigned<FloorDivSIOp, DivUIOp>,
150 ConvertOpToUnsigned<RemSIOp, RemUIOp>,
151 ConvertOpToUnsigned<MinSIOp, MinUIOp>,
152 ConvertOpToUnsigned<MaxSIOp, MaxUIOp>,
153 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,...
The general data-flow analysis solver.
AnalysisT * load(Args &&...args)
Load an analysis into the solver. Return the analysis instance.
LogicalResult initializeAndRun(Operation *top)
Initialize the children analyses starting from the provided top-level operation and run the analysis ...
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.
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...