56 #define DEBUG_TYPE "loopsink"
58 STATISTIC(NumLoopSunk,
"Number of instructions sunk into loop");
59 STATISTIC(NumLoopSunkCloned,
"Number of cloned instructions sunk into loop");
63 cl::desc(
"Do not sink instructions that require cloning unless they "
64 "execute less than this percent of the time."));
68 cl::desc(
"Do not sink instructions that have too many uses."));
125 if (UseBBs.
size() == 0)
126 return BBsToSinkInto;
140 BBsDominatedByColdestBB.
clear();
143 BBsDominatedByColdestBB.
insert(SinkedBB);
144 if (BBsDominatedByColdestBB.
size() == 0)
148 for (
BasicBlock *DominatedBB : BBsDominatedByColdestBB) {
149 BBsToSinkInto.erase(DominatedBB);
151 BBsToSinkInto.insert(ColdestBB);
159 BBsToSinkInto.
clear();
160 return BBsToSinkInto;
174 for (
auto &U : I.
uses()) {
177 if (dyn_cast<PHINode>(UI))
194 if (BBsToSinkInto.
empty())
201 SortedBBsToSinkInto.
insert(SortedBBsToSinkInto.
begin(), BBsToSinkInto.
begin(),
202 BBsToSinkInto.
end());
205 return *LoopBlockNumber.
find(A) < *LoopBlockNumber.
find(
B);
221 auto *I = cast<Instruction>(U.
getUser());
227 DEBUG(
dbgs() <<
"Sinking a clone of " << I <<
" To: " <<
N->getName()
233 I.
moveBefore(&*MoveBB->getFirstInsertionPt());
262 bool Changed =
false;
276 LoopBlockNumber[
B] = ++i;
278 std::stable_sort(ColdLoopBBs.
begin(), ColdLoopBBs.
end(),
286 for (
auto II = Preheader->
rbegin(),
E = Preheader->
rend(); II !=
E;) {
290 "Insts in a loop's preheader should have loop invariant operands!");
319 bool Changed =
false;
321 Loop &
L = *PreorderLoops.pop_back_val();
328 }
while (!PreorderLoops.empty());
339 struct LegacyLoopSinkPass :
public LoopPass {
349 auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
351 *L, getAnalysis<AAResultsWrapperPass>().getAAResults(),
352 getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
353 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
354 getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI(),
355 SE ? &SE->getSE() :
nullptr);
Pass interface - Implemented by all 'passes'.
void push_back(const T &Elt)
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
static cl::opt< unsigned > SinkFrequencyPercentThreshold("sink-freq-percent-threshold", cl::Hidden, cl::init(90), cl::desc("Do not sink instructions that require cloning unless they ""execute less than this percent of the time."))
The main scalar evolution driver.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
const Function * getParent() const
Return the enclosing method, or null if none.
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Analysis pass which computes a DominatorTree.
reverse_iterator rbegin()
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge)
Replace each use of 'From' with 'To' if that use is dominated by the given edge.
StringRef getName() const
Return a constant reference to the value's name.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
This is the interface for a SCEV-based alias analysis.
A Use represents the edge between a Value definition and its users.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Legacy analysis pass which computes BlockFrequencyInfo.
void setName(const Twine &Name)
Change the name of the value.
Analysis pass that exposes the LoopInfo for a function.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Optional< uint64_t > getEntryCount() const
Get the entry count for this function.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void forgetLoopDispositions(const Loop *L)
Called when the client has changed the disposition of values in this loop.
iterator_range< block_iterator > blocks() const
use_iterator_impl< Use > use_iterator
iterator find(const_arg_type_t< KeyT > Val)
Function Alias Analysis false
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
initializer< Ty > init(const Ty &Val)
A set of analyses that are preserved following a run of a transformation pass.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
LLVM Basic Block Representation.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
A manager for alias analyses.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
static bool sinkLoopInvariantInstructions(Loop &L, AAResults &AA, LoopInfo &LI, DominatorTree &DT, BlockFrequencyInfo &BFI, ScalarEvolution *SE)
Sinks instructions from loop's preheader to the loop body if the sum frequency of inserted copy is sm...
Pass * createLoopSinkPass()
LLVM_NODISCARD bool empty() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Analysis pass which computes BlockFrequencyInfo.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static SmallPtrSet< BasicBlock *, 2 > findBBsToSinkInto(const Loop &L, const SmallPtrSetImpl< BasicBlock * > &UseBBs, const SmallVectorImpl< BasicBlock * > &ColdLoopBBs, DominatorTree &DT, BlockFrequencyInfo &BFI)
Return a set of basic blocks to insert sinked instructions.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
void add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo)
These methods are used to add different types of instructions to the alias sets.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Represents analyses that only rely on functions' control flow.
iterator insert(iterator I, T &&Elt)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Represents a single loop in the control flow graph.
void preserveSet()
Mark an analysis set as preserved.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
SmallVector< LoopT *, 4 > getLoopsInPreorder()
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
static cl::opt< unsigned > MaxNumberOfUseBBsForSinking("max-uses-for-sinking", cl::Hidden, cl::init(30), cl::desc("Do not sink instructions that have too many uses."))
INITIALIZE_PASS_BEGIN(LegacyLoopSinkPass,"loop-sink","Loop Sink", false, false) Pass *llvm
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void initializeLegacyLoopSinkPassPass(PassRegistry &)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
This is the interface for LLVM's primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
bool canSinkOrHoistInst(Instruction &I, AAResults *AA, DominatorTree *DT, Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo, OptimizationRemarkEmitter *ORE=nullptr)
Returns true if the hoister and sinker can handle this instruction.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
static BlockFrequency adjustedSumFreq(SmallPtrSetImpl< BasicBlock * > &BBs, BlockFrequencyInfo &BFI)
Return adjusted total frequency of BBs.
const BasicBlock * getParent() const
static bool sinkInstruction(Loop &L, Instruction &I, const SmallVectorImpl< BasicBlock * > &ColdLoopBBs, const SmallDenseMap< BasicBlock *, int, 16 > &LoopBlockNumber, LoopInfo &LI, DominatorTree &DT, BlockFrequencyInfo &BFI)
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.