17 #include "PassDetail.h" 26 #include "llvm/Support/Debug.h" 28 #define DEBUG_TYPE "sccp" 37 struct SCCPLatticeValue {
39 : constant(constant), constantDialect(dialect) {}
42 static SCCPLatticeValue getPessimisticValueState(
MLIRContext *context) {
43 return SCCPLatticeValue();
45 static SCCPLatticeValue getPessimisticValueState(
Value value) {
46 return SCCPLatticeValue();
51 bool operator==(
const SCCPLatticeValue &rhs)
const {
52 return constant == rhs.constant;
56 static SCCPLatticeValue join(
const SCCPLatticeValue &lhs,
57 const SCCPLatticeValue &rhs) {
58 return lhs == rhs ? lhs : SCCPLatticeValue();
71 ~SCCPAnalysis()
override =
default;
77 LLVM_DEBUG(llvm::dbgs() <<
"SCCP: Visiting operation: " << *op <<
"\n");
84 return markAllPessimisticFixpoint(op->
getResults());
101 if (
failed(op->
fold(constantOperands, foldResults)))
102 return markAllPessimisticFixpoint(op->
getResults());
107 if (foldResults.empty()) {
110 return markAllPessimisticFixpoint(op->
getResults());
114 assert(foldResults.size() == op->
getNumResults() &&
"invalid result size");
117 for (
unsigned i = 0, e = foldResults.size(); i != e; ++i) {
124 result |= lattice.
join(SCCPLatticeValue(attr, dialect));
126 result |= lattice.
join(getLatticeElement(foldResult.get<
Value>()));
134 BranchOpInterface branch,
141 if (
Block *singleSucc = branch.getSuccessorForOperands(constantOperands)) {
142 successors.push_back(singleSucc);
150 void getSuccessorsForOperands(
158 branch.getSuccessorRegions(sourceIndex, constantOperands, successors);
174 analysis.lookupLatticeElement(value);
177 SCCPLatticeValue &latticeValue = lattice->
getValue();
178 if (!latticeValue.constant)
182 Dialect *dialect = latticeValue.constantDialect;
184 builder, dialect, latticeValue.constant, value.
getType(), value.
getLoc());
199 for (
Region ®ion : regions)
200 for (
Block &block : llvm::reverse(region))
201 worklist.push_back(&block);
208 addToWorklist(initialRegions);
209 while (!worklist.empty()) {
210 Block *block = worklist.pop_back_val();
212 for (
Operation &op : llvm::make_early_inc_range(*block)) {
216 bool replacedAll = op.getNumResults() != 0;
217 for (
Value res : op.getResults())
224 assert(op.use_empty() &&
"expected all uses to be replaced");
230 addToWorklist(op.getRegions());
245 struct SCCP :
public SCCPBase<SCCP> {
246 void runOnOperation()
override;
250 void SCCP::runOnOperation() {
259 return std::make_unique<SCCP>();
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
This class contains a list of basic blocks and a link to the parent operation it is attached to...
bool wouldOpBeTriviallyDead(Operation *op)
Return true if the given operation would be dead if unused, and has no side effects on memory that wo...
Operation is a basic unit of execution within MLIR.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
operand_range getOperands()
Returns an iterator on the underlying Value's.
unsigned getNumRegions()
Returns the number of regions held by this operation.
void setOperands(ValueRange operands)
Replace the current operands of this operation with the ones provided in 'operands'.
Block represents an ordered list of Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
This class represents a single result from folding an operation.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
This class represents a lattice holding a specific value of type ValueT.
static LogicalResult replaceWithConstant(SCCPAnalysis &analysis, OpBuilder &builder, OperationFolder &folder, Value value)
Replace the given value with a constant if the corresponding lattice represents a constant...
void replaceAllUsesWith(Value newValue) const
Replace all uses of 'this' value with the new value, updating anything in the IR that uses 'this' to ...
static constexpr const bool value
MLIRContext * getContext()
Return the context this operation is associated with.
std::unique_ptr< Pass > createSCCPPass()
Creates a pass which performs sparse conditional constant propagation over nested operations...
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
This class represents an efficient way to signal success or failure.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Value getOrCreateConstant(OpBuilder &builder, Dialect *dialect, Attribute value, Type type, Location loc)
Get or create a constant using the given builder.
Attributes are known-constant values of operations.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
static void rewrite(SCCPAnalysis &analysis, MLIRContext *context, MutableArrayRef< Region > initialRegions)
Rewrite the given regions using the computing analysis.
BlockArgListType getArguments()
This class represents an argument of a Block.
Location getLoc() const
Return the location of this value.
DictionaryAttr getAttrDictionary()
Return all of the attributes on this operation as a DictionaryAttr.
ChangeResult join(const detail::AbstractLatticeElement &rhs) final
Join the information contained in the 'rhs' lattice into this lattice.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
A utility class for folding operations, and unifying duplicated constants generated along the way...
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Type getType() const
Return the type of this value.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
MLIRContext is the top-level object for a collection of MLIR operations.
LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)
Attempt to fold this operation with the specified constant operand values.
unsigned getNumResults()
Return the number of results held by this operation.
ValueT & getValue()
Return the value held by this lattice.
ChangeResult
A result type used to indicate if a change happened.
void setAttrs(DictionaryAttr newAttrs)
Set the attribute dictionary on this operation.
result_range getResults()
This class helps build Operations.
This class provides a general forward dataflow analysis driver utilizing the lattice classes defined ...
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...