MLIR  17.0.0git
ConstantPropagationAnalysis.h
Go to the documentation of this file.
1 //===- ConstantPropagationAnalysis.h - Constant propagation analysis ------===//
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 implements constant propagation analysis. In this file are defined
10 // the lattice value class that represents constant values in the program and
11 // a sparse constant propagation analysis that uses operation folders to
12 // speculate about constant values in the program.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef MLIR_ANALYSIS_DATAFLOW_CONSTANTPROPAGATIONANALYSIS_H
17 #define MLIR_ANALYSIS_DATAFLOW_CONSTANTPROPAGATIONANALYSIS_H
18 
20 #include <optional>
21 
22 namespace mlir {
23 namespace dataflow {
24 
25 //===----------------------------------------------------------------------===//
26 // ConstantValue
27 //===----------------------------------------------------------------------===//
28 
29 /// This lattice value represents a known constant value of a lattice.
31 public:
32  /// Construct a constant value as uninitialized.
33  explicit ConstantValue() = default;
34 
35  /// Construct a constant value with a known constant.
36  explicit ConstantValue(Attribute constant, Dialect *dialect)
37  : constant(constant), dialect(dialect) {}
38 
39  /// Get the constant value. Returns null if no value was determined.
41  assert(!isUninitialized());
42  return *constant;
43  }
44 
45  /// Get the dialect instance that can be used to materialize the constant.
47  assert(!isUninitialized());
48  return dialect;
49  }
50 
51  /// Compare the constant values.
52  bool operator==(const ConstantValue &rhs) const {
53  return constant == rhs.constant;
54  }
55 
56  /// Print the constant value.
57  void print(raw_ostream &os) const;
58 
59  /// The state where the constant value is uninitialized. This happens when the
60  /// state hasn't been set during the analysis.
62 
63  /// Whether the state is uninitialized.
64  bool isUninitialized() const { return !constant.has_value(); }
65 
66  /// The state where the constant value is unknown.
68  return ConstantValue{/*constant=*/nullptr, /*dialect=*/nullptr};
69  }
70 
71  /// The union with another constant value is null if they are different, and
72  /// the same if they are the same.
73  static ConstantValue join(const ConstantValue &lhs,
74  const ConstantValue &rhs) {
75  if (lhs.isUninitialized())
76  return rhs;
77  if (rhs.isUninitialized())
78  return lhs;
79  if (lhs == rhs)
80  return lhs;
81  return getUnknownConstant();
82  }
83 
84 private:
85  /// The constant value.
86  std::optional<Attribute> constant;
87  /// A dialect instance that can be used to materialize the constant.
88  Dialect *dialect = nullptr;
89 };
90 
91 //===----------------------------------------------------------------------===//
92 // SparseConstantPropagation
93 //===----------------------------------------------------------------------===//
94 
95 /// This analysis implements sparse constant propagation, which attempts to
96 /// determine constant-valued results for operations using constant-valued
97 /// operands, by speculatively folding operations. When combined with dead-code
98 /// analysis, this becomes sparse conditional constant propagation (SCCP).
100  : public SparseDataFlowAnalysis<Lattice<ConstantValue>> {
101 public:
103 
104  void visitOperation(Operation *op,
105  ArrayRef<const Lattice<ConstantValue> *> operands,
106  ArrayRef<Lattice<ConstantValue> *> results) override;
107 
108  void setToEntryState(Lattice<ConstantValue> *lattice) override;
109 };
110 
111 } // end namespace dataflow
112 } // end namespace mlir
113 
114 #endif // MLIR_ANALYSIS_DATAFLOW_CONSTANTPROPAGATIONANALYSIS_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:41
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
This lattice value represents a known constant value of a lattice.
bool operator==(const ConstantValue &rhs) const
Compare the constant values.
ConstantValue()=default
Construct a constant value as uninitialized.
ConstantValue(Attribute constant, Dialect *dialect)
Construct a constant value with a known constant.
Attribute getConstantValue() const
Get the constant value. Returns null if no value was determined.
Dialect * getConstantDialect() const
Get the dialect instance that can be used to materialize the constant.
static ConstantValue join(const ConstantValue &lhs, const ConstantValue &rhs)
The union with another constant value is null if they are different, and the same if they are the sam...
bool isUninitialized() const
Whether the state is uninitialized.
void print(raw_ostream &os) const
Print the constant value.
static ConstantValue getUninitialized()
The state where the constant value is uninitialized.
static ConstantValue getUnknownConstant()
The state where the constant value is unknown.
This class represents a lattice holding a specific value of type ValueT.
This analysis implements sparse constant propagation, which attempts to determine constant-valued res...
void setToEntryState(Lattice< ConstantValue > *lattice) override
Set the given lattice element(s) at control flow entry point(s).
void visitOperation(Operation *op, ArrayRef< const Lattice< ConstantValue > * > operands, ArrayRef< Lattice< ConstantValue > * > results) override
Visit an operation with the lattices of its operands.
A sparse (forward) data-flow analysis for propagating SSA value lattices across the IR by implementin...
SparseDataFlowAnalysis(DataFlowSolver &solver)
Include the generated interface declarations.