MLIR  20.0.0git
IntegerRangeAnalysis.h
Go to the documentation of this file.
1 //===-IntegerRangeAnalysis.h - Integer range analysis -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the dataflow analysis class for integer range inference
10 // so that it can be used in transformations over the `arith` dialect such as
11 // branch elimination or signed->unsigned rewriting.
12 //
13 // One can also implement InferIntRangeInterface on ops in custom dialects,
14 // and then use this analysis to propagate ranges with custom semantics.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H
19 #define MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H
20 
23 
24 namespace mlir {
25 namespace dataflow {
26 
27 /// This lattice element represents the integer value range of an SSA value.
28 /// When this lattice is updated, it automatically updates the constant value
29 /// of the SSA value (if the range can be narrowed to one).
30 class IntegerValueRangeLattice : public Lattice<IntegerValueRange> {
31 public:
32  using Lattice::Lattice;
33 
34  /// If the range can be narrowed to an integer constant, update the constant
35  /// value of the SSA value.
36  void onUpdate(DataFlowSolver *solver) const override;
37 };
38 
39 /// Integer range analysis determines the integer value range of SSA values
40 /// using operations that define `InferIntRangeInterface` and also sets the
41 /// range of iteration indices of loops with known bounds.
42 ///
43 /// This analysis depends on DeadCodeAnalysis, and will be a silent no-op
44 /// if DeadCodeAnalysis is not loaded in the same solver context.
46  : public SparseForwardDataFlowAnalysis<IntegerValueRangeLattice> {
47 public:
49 
50  /// At an entry point, we cannot reason about interger value ranges.
51  void setToEntryState(IntegerValueRangeLattice *lattice) override {
53  lattice->getPoint())));
54  }
55 
56  /// Visit an operation. Invoke the transfer function on each operation that
57  /// implements `InferIntRangeInterface`.
58  void visitOperation(Operation *op,
60  ArrayRef<IntegerValueRangeLattice *> results) override;
61 
62  /// Visit block arguments or operation results of an operation with region
63  /// control-flow for which values are not defined by region control-flow. This
64  /// function calls `InferIntRangeInterface` to provide values for block
65  /// arguments or tries to reduce the range on loop induction variables with
66  /// known bounds.
67  void
70  unsigned firstIndex) override;
71 };
72 
73 } // end namespace dataflow
74 } // end namespace mlir
75 
76 #endif // MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H
void propagateIfChanged(AnalysisState *state, ChangeResult changed)
Propagate an update to a state if it changed.
The general data-flow analysis solver.
static IntegerValueRange getMaxRange(Value value)
Create a maximal range ([0, uint_max(t)] / [int_min(t), int_max(t)]) range that is used to mark the v...
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class represents a successor of a region.
Integer range analysis determines the integer value range of SSA values using operations that define ...
void visitOperation(Operation *op, ArrayRef< const IntegerValueRangeLattice * > operands, ArrayRef< IntegerValueRangeLattice * > results) override
Visit an operation.
void setToEntryState(IntegerValueRangeLattice *lattice) override
At an entry point, we cannot reason about interger value ranges.
void visitNonControlFlowArguments(Operation *op, const RegionSuccessor &successor, ArrayRef< IntegerValueRangeLattice * > argLattices, unsigned firstIndex) override
Visit block arguments or operation results of an operation with region control-flow for which values ...
This lattice element represents the integer value range of an SSA value.
void onUpdate(DataFlowSolver *solver) const override
If the range can be narrowed to an integer constant, update the constant value of the SSA value.
This class represents a lattice holding a specific value of type ValueT.
ChangeResult join(const AbstractSparseLattice &rhs) override
Join the information contained in the 'rhs' lattice into this lattice.
Value getPoint() const
Return the program point this lattice is located at.
A sparse forward data-flow analysis for propagating SSA value lattices across the IR by implementing ...
SparseForwardDataFlowAnalysis(DataFlowSolver &solver)
Include the generated interface declarations.