57 #define DEBUG_TYPE "loop-unroll-and-jam" 62 "llvm.loop.unroll_and_jam.followup_all";
64 "llvm.loop.unroll_and_jam.followup_inner";
66 "llvm.loop.unroll_and_jam.followup_outer";
68 "llvm.loop.unroll_and_jam.followup_remainder_inner";
70 "llvm.loop.unroll_and_jam.followup_remainder_outer";
75 cl::desc(
"Allows loops to be unroll-and-jammed."));
79 cl::desc(
"Use this unroll count for all loops including those with " 80 "unroll_and_jam_count pragma values, for testing purposes"));
84 cl::desc(
"Threshold to use for inner loop when doing unroll and jam."));
88 cl::desc(
"Unrolled size limit for loops with an unroll_and_jam(full) or " 89 "unroll_count pragma."));
105 assert(LoopID->getNumOperands() > 0 &&
"requires at least one operand");
106 assert(LoopID->getOperand(0) == LoopID &&
"invalid loop id");
108 for (
unsigned i = 1,
e = LoopID->getNumOperands(); i <
e; ++i) {
135 "Unroll count hint metadata should have two operands.");
137 mdconst::extract<ConstantInt>(MD->
getOperand(1))->getZExtValue();
138 assert(Count >= 1 &&
"Unroll count must be positive.");
148 assert(LoopSize >= UP.
BEInsns &&
"LoopSize should not be less than BEInsns!");
159 unsigned OuterTripMultiple,
unsigned OuterLoopSize,
unsigned InnerTripCount,
166 unsigned MaxTripCount = 0;
167 bool UseUpperBound =
false;
169 L, TTI, DT, LI, SE, EphValues, ORE, OuterTripCount, MaxTripCount,
170 false, OuterTripMultiple, OuterLoopSize, UP, UseUpperBound);
171 if (ExplicitUnroll || UseUpperBound) {
174 LLVM_DEBUG(
dbgs() <<
"Won't unroll-and-jam; explicit count set by " 175 "computeUnrollCount\n");
182 if (UserUnrollCount) {
194 if (PragmaCount > 0) {
195 UP.
Count = PragmaCount;
198 if ((UP.
AllowRemainder || (OuterTripMultiple % PragmaCount == 0)) &&
206 bool ExplicitUnrollAndJamCount = PragmaCount > 0 || UserUnrollCount;
207 bool ExplicitUnrollAndJam = PragmaEnableUnroll || ExplicitUnrollAndJamCount;
211 if (ExplicitUnrollAndJam)
216 LLVM_DEBUG(
dbgs() <<
"Won't unroll-and-jam; can't create remainder and " 217 "inner loop too large\n");
233 if (ExplicitUnrollAndJam)
238 if (InnerTripCount && InnerLoopSize * InnerTripCount < UP.
Threshold) {
239 LLVM_DEBUG(
dbgs() <<
"Won't unroll-and-jam; small inner loop count is " 240 "being left for the unroller\n");
249 dbgs() <<
"Won't unroll-and-jam; More than one inner loop block\n");
257 unsigned NumInvariant = 0;
260 if (
auto *Ld = dyn_cast<LoadInst>(&
I)) {
261 Value *V = Ld->getPointerOperand();
268 if (NumInvariant == 0) {
269 LLVM_DEBUG(
dbgs() <<
"Won't unroll-and-jam; No loop invariant loads\n");
294 if (Latch != Exit || SubLoopLatch != SubLoopExit)
332 unsigned NumInlineCandidates;
333 bool NotDuplicatable;
337 unsigned InnerLoopSize =
339 Convergent, TTI, EphValues, UP.
BEInsns);
340 unsigned OuterLoopSize =
343 LLVM_DEBUG(
dbgs() <<
" Outer Loop Size: " << OuterLoopSize <<
"\n");
344 LLVM_DEBUG(
dbgs() <<
" Inner Loop Size: " << InnerLoopSize <<
"\n");
345 if (NotDuplicatable) {
346 LLVM_DEBUG(
dbgs() <<
" Not unrolling loop which contains non-duplicatable " 350 if (NumInlineCandidates != 0) {
351 LLVM_DEBUG(
dbgs() <<
" Not unrolling loop with inlinable calls.\n");
356 dbgs() <<
" Not unrolling loop with convergent instructions.\n");
369 LLVMLoopUnrollAndJamFollowupRemainderInner});
370 if (NewInnerEpilogueLoopID.
hasValue())
380 L, SubLoop, TTI, DT, LI, SE, EphValues, &ORE, OuterTripCount,
381 OuterTripMultiple, OuterLoopSize, InnerTripCount, InnerLoopSize, UP);
385 if (OuterTripCount && UP.
Count > OuterTripCount)
386 UP.
Count = OuterTripCount;
388 Loop *EpilogueOuterLoop =
nullptr;
391 &SE, &DT, &AC, &ORE, &EpilogueOuterLoop);
394 if (EpilogueOuterLoop) {
397 LLVMLoopUnrollAndJamFollowupRemainderOuter});
398 if (NewOuterEpilogueLoopID.
hasValue())
404 LLVMLoopUnrollAndJamFollowupInner});
436 bool DidSomething =
false;
451 while (!Worklist.
empty()) {
470 LoopUnrollAndJam(
int OptLevel = 2) :
FunctionPass(ID), OptLevel(OptLevel) {
478 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
479 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
480 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
482 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
483 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
484 auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI();
485 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
508 "Unroll and Jam loops",
false,
false)
520 return new LoopUnrollAndJam(OptLevel);
Pass interface - Implemented by all 'passes'.
static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value *> &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop)...
unsigned getSmallConstantTripCount(const Loop *L)
Returns the maximum trip count of the loop if it is a single-exit loop and we can compute a small max...
bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
unsigned getSmallConstantTripMultiple(const Loop *L)
Returns the largest constant divisor of the trip count of the loop if it is a single-exit loop and we...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisPass to compute dependence information in a function.
This class represents lattice values for constants.
LLVM_NODISCARD T pop_back_val()
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static bool HasUnrollAndJamEnablePragma(const Loop *L)
INITIALIZE_PASS_BEGIN(LoopUnrollAndJam, "loop-unroll-and-jam", "Unroll and Jam loops", false, false) INITIALIZE_PASS_END(LoopUnrollAndJam
static cl::opt< unsigned > UnrollAndJamCount("unroll-and-jam-count", cl::Hidden, cl::desc("Use this unroll count for all loops including those with " "unroll_and_jam_count pragma values, for testing purposes"))
Legacy pass manager pass to access dependence information.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
The main scalar evolution driver.
An immutable pass that tracks lazily created AssumptionCache objects.
static const char *const LLVMLoopUnrollAndJamFollowupRemainderInner
A cache of @llvm.assume calls within a function.
Analysis pass providing the TargetTransformInfo.
static const char *const LLVMLoopUnrollAndJamFollowupOuter
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool empty() const
Determine if the PriorityWorklist is empty or not.
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Analysis pass which computes a DominatorTree.
const MDOperand & getOperand(unsigned I) const
DependenceInfo - This class is the main dependence-analysis driver.
bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static cl::opt< bool > AllowUnrollAndJam("allow-unroll-and-jam", cl::Hidden, cl::desc("Allows loops to be unroll-and-jammed."))
static LoopUnrollResult tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, const TargetTransformInfo &TTI, AssumptionCache &AC, DependenceInfo &DI, OptimizationRemarkEmitter &ORE, int OptLevel)
TransformationMode hasUnrollAndJamTransformation(Loop *L)
TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, int OptLevel, Optional< unsigned > UserThreshold, Optional< unsigned > UserCount, Optional< bool > UserAllowPartial, Optional< bool > UserRuntime, Optional< bool > UserUpperBound, Optional< bool > UserAllowPeeling, Optional< bool > UserAllowProfileBasedPeeling, Optional< unsigned > UserFullUnrollMaxCount)
Gather the various unrolling parameters based on the defaults, compiler flags, TTI overrides and user...
Analysis pass that exposes the LoopInfo for a function.
static const char *const LLVMLoopUnrollAndJamFollowupInner
BlockT * getHeader() const
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
const T & getValue() const LLVM_LVALUE_FUNCTION
bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT, DependenceInfo &DI)
This header provides classes for managing per-loop analyses.
bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, const SmallPtrSetImpl< const Value *> &EphValues, OptimizationRemarkEmitter *ORE, unsigned &TripCount, unsigned MaxTripCount, bool MaxOrZero, unsigned &TripMultiple, unsigned LoopSize, TargetTransformInfo::UnrollingPreferences &UP, bool &UseUpperBound)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Pass * createLoopUnrollAndJamPass(int OptLevel=2)
StringRef getString() const
loop unroll and Unroll and Jam loops
The loop was fully unrolled into straight-line code.
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
A set of analyses that are preserved following a run of a transformation pass.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
LLVM Basic Block Representation.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void setLoopAlreadyUnrolled()
Add llvm.loop.unroll.disable to this loop's loop id metadata.
static bool computeUnrollAndJamCount(Loop *L, Loop *SubLoop, const TargetTransformInfo &TTI, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, const SmallPtrSetImpl< const Value *> &EphValues, OptimizationRemarkEmitter *ORE, unsigned OuterTripCount, unsigned OuterTripMultiple, unsigned OuterLoopSize, unsigned InnerTripCount, unsigned InnerLoopSize, TargetTransformInfo::UnrollingPreferences &UP)
const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
Represent the analysis usage information of a pass.
static unsigned UnrollAndJamCountPragmaValue(const Loop *L)
FunctionPass class - This class is used to implement most global optimizations.
static MDNode * GetUnrollMetadataForLoop(const Loop *L, StringRef Name)
A version of PriorityWorklist that selects small size optimized data structures for the vector and ma...
The loop was partially unrolled – we still have a loop, but with a smaller trip count.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static uint64_t getUnrollAndJammedLoopSize(unsigned LoopSize, TargetTransformInfo::UnrollingPreferences &UP)
The transformation should not be applied.
bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution *SE)
Put loop into LCSSA form.
A function analysis which provides an AssumptionCache.
unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, bool &NotDuplicatable, bool &Convergent, const TargetTransformInfo &TTI, const SmallPtrSetImpl< const Value *> &EphValues, unsigned BEInsns)
ApproximateLoopSize - Approximate the size of the loop.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
MDNode * GetUnrollMetadata(MDNode *LoopID, StringRef Name)
Given an llvm.loop loop id metadata node, returns the loop hint metadata node with the given name (fo...
Analysis pass that exposes the ScalarEvolution for a function.
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
static const char *const LLVMLoopUnrollAndJamFollowupAll
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
This class represents an analyzed expression in the program.
static cl::opt< unsigned > UnrollAndJamThreshold("unroll-and-jam-threshold", cl::init(60), cl::Hidden, cl::desc("Threshold to use for inner loop when doing unroll and jam."))
Represents a single loop in the control flow graph.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
static cl::opt< unsigned > PragmaUnrollAndJamThreshold("pragma-unroll-and-jam-threshold", cl::init(1024), cl::Hidden, cl::desc("Unrolled size limit for loops with an unroll_and_jam(full) or " "unroll_count pragma."))
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
void appendLoopsToWorklist(RangeT &&Loops, SmallPriorityWorklist< Loop *, 4 > &Worklist)
Helper to implement appending of loops onto a worklist.
static const char *const LLVMLoopUnrollAndJamFollowupRemainderOuter
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
TransformationMode
The mode sets how eager a transformation should be applied.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
The loop was not modified.
void initializeLoopUnrollAndJamPass(PassRegistry &)
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
static bool HasAnyUnrollPragma(const Loop *L, StringRef Prefix)
This header defines various interfaces for pass management in LLVM.
unsigned getNumOperands() const
Return number of MDNode operands.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
LoopUnrollResult
Represents the result of a UnrollLoop invocation.
LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple, bool UnrollRemainder, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, OptimizationRemarkEmitter *ORE, Loop **EpilogueLoop=nullptr)