27#include "llvm/Support/DebugLog.h"
30#define DEBUG_TYPE "cf-sink"
42 : shouldMoveIntoRegion(shouldMoveIntoRegion),
43 moveIntoRegion(moveIntoRegion), domInfo(domInfo) {}
61 std::vector<Operation *> &stack);
65 void sinkRegion(
Region *region);
80 "expected op to be defined outside the region");
81 return llvm::all_of(op->
getUsers(), [&](Operation *user) {
84 return domInfo.dominates(®ion->front(), user->getBlock());
88void Sinker::tryToSinkPredecessors(Operation *user, Region *region,
89 std::vector<Operation *> &stack) {
90 LDBG() <<
"Contained op: "
91 << OpWithFlags(user, OpPrintingFlags().skipRegions());
93 Operation *op = value.getDefiningOp();
97 LDBG() <<
"Try to sink:\n"
98 << OpWithFlags(op, OpPrintingFlags().skipRegions());
101 if (allUsersDominatedBy(op, region) && shouldMoveIntoRegion(op, region)) {
102 moveIntoRegion(op, region);
110void Sinker::sinkRegion(Region *region) {
112 std::vector<Operation *> stack;
113 for (Operation &op : region->
getOps())
114 stack.push_back(&op);
118 while (!stack.empty()) {
119 Operation *op = stack.back();
121 tryToSinkPredecessors(op, region, stack);
125size_t Sinker::sinkRegions(RegionRange regions) {
126 for (Region *region : regions)
127 if (!region->
empty())
136 return Sinker(shouldMoveIntoRegion, moveIntoRegion, domInfo)
137 .sinkRegions(regions);
144 for (
auto [idx, operand] : llvm::enumerate(branch->getOperands()))
149 branch.getRegionInvocationBounds(operands, bounds);
153 for (
auto it : llvm::zip(branch->getRegions(), bounds)) {
156 regions.push_back(&std::get<0>(it));
Attributes are known-constant values of operations.
A class for computing basic dominance information.
This class represents upper and lower bounds on the number of times a region of a RegionBranchOpInter...
std::optional< unsigned > getUpperBound() const
Return the upper bound.
Operation is the basic unit of execution within MLIR.
operand_range getOperands()
Returns an iterator on the underlying Value's.
user_range getUsers()
Returns a range of all users.
Region * getParentRegion()
Returns the region to which the instruction belongs.
This class provides an abstraction over the different types of ranges over Regions.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Operation * findAncestorOpInRegion(Operation &op)
Returns 'op' if 'op' lies in this region, or otherwise finds the ancestor of 'op' that lies in this r...
iterator_range< OpIterator > getOps()
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
void getSinglyExecutedRegionsToSink(RegionBranchOpInterface branch, SmallVectorImpl< Region * > ®ions)
Populates regions with regions of the provided region branch op that are executed at most once at tha...
size_t controlFlowSink(RegionRange regions, DominanceInfo &domInfo, function_ref< bool(Operation *, Region *)> shouldMoveIntoRegion, function_ref< void(Operation *, Region *)> moveIntoRegion)
Given a list of regions, perform control flow sinking on them.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
llvm::function_ref< Fn > function_ref