57 #define DEBUG_TYPE "loop-unroll-and-jam" 61 cl::desc(
"Allows loops to be unroll-and-jammed."));
65 cl::desc(
"Use this unroll count for all loops including those with " 66 "unroll_and_jam_count pragma values, for testing purposes"));
70 cl::desc(
"Threshold to use for inner loop when doing unroll and jam."));
74 cl::desc(
"Unrolled size limit for loops with an unroll_and_jam(full) or " 75 "unroll_count pragma."));
91 assert(LoopID->getNumOperands() > 0 &&
"requires at least one operand");
92 assert(LoopID->getOperand(0) == LoopID &&
"invalid loop id");
94 for (
unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
126 "Unroll count hint metadata should have two operands.");
128 mdconst::extract<ConstantInt>(MD->
getOperand(1))->getZExtValue();
129 assert(Count >= 1 &&
"Unroll count must be positive.");
139 assert(LoopSize >= UP.
BEInsns &&
"LoopSize should not be less than BEInsns!");
150 unsigned OuterTripMultiple,
unsigned OuterLoopSize,
unsigned InnerTripCount,
154 if (UserUnrollCount) {
166 if (PragmaCount > 0) {
167 UP.
Count = PragmaCount;
170 if ((UP.
AllowRemainder || (OuterTripMultiple % PragmaCount == 0)) &&
181 unsigned MaxTripCount = 0;
182 bool UseUpperBound =
false;
184 L, TTI, DT, LI, SE, EphValues, ORE, OuterTripCount, MaxTripCount,
185 OuterTripMultiple, OuterLoopSize, UP, UseUpperBound);
186 if (ExplicitUnroll || UseUpperBound) {
194 ExplicitUnroll = PragmaCount > 0 || PragmaEnableUnroll || UserUnrollCount;
198 if (ExplicitUnroll && OuterTripCount != 0)
209 if (!ExplicitUnroll && InnerTripCount &&
210 InnerLoopSize * InnerTripCount < UP.
Threshold) {
222 if (!ExplicitUnroll) {
233 unsigned NumInvariant = 0;
236 if (
auto *Ld = dyn_cast<LoadInst>(&
I)) {
237 Value *V = Ld->getPointerOperand();
244 if (NumInvariant == 0) {
250 return ExplicitUnroll;
270 if (Latch != Exit || SubLoopLatch != SubLoopExit)
304 unsigned NumInlineCandidates;
305 bool NotDuplicatable;
309 unsigned InnerLoopSize =
311 Convergent, TTI, EphValues, UP.
BEInsns);
312 unsigned OuterLoopSize =
315 LLVM_DEBUG(
dbgs() <<
" Outer Loop Size: " << OuterLoopSize <<
"\n");
316 LLVM_DEBUG(
dbgs() <<
" Inner Loop Size: " << InnerLoopSize <<
"\n");
317 if (NotDuplicatable) {
318 LLVM_DEBUG(
dbgs() <<
" Not unrolling loop which contains non-duplicatable " 322 if (NumInlineCandidates != 0) {
323 LLVM_DEBUG(
dbgs() <<
" Not unrolling loop with inlinable calls.\n");
328 dbgs() <<
" Not unrolling loop with convergent instructions.\n");
339 L, SubLoop, TTI, DT, LI, SE, EphValues, &ORE, OuterTripCount,
340 OuterTripMultiple, OuterLoopSize, InnerTripCount, InnerLoopSize, UP);
344 if (OuterTripCount && UP.
Count > OuterTripCount)
345 UP.
Count = OuterTripCount;
361 class LoopUnrollAndJam :
public LoopPass {
366 LoopUnrollAndJam(
int OptLevel = 2) :
LoopPass(ID), OptLevel(OptLevel) {
376 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
377 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
378 ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
380 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
381 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
382 auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI();
412 "Unroll and Jam loops",
false,
false)
421 return new LoopUnrollAndJam(OptLevel);
429 Function *
F = L.getHeader()->getParent();
435 "LoopUnrollAndJamPass: OptimizationRemarkEmitterAnalysis not cached at " 441 &L, AR.
DT, &AR.
LI, AR.
SE, AR.
TTI, AR.
AC, DI, *ORE, 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...
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.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
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.
The main scalar evolution driver.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
const MDOperand & getOperand(unsigned I) const
bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE, const SmallPtrSetImpl< const Value *> &EphValues, OptimizationRemarkEmitter *ORE, unsigned &TripCount, unsigned MaxTripCount, unsigned &TripMultiple, unsigned LoopSize, TargetTransformInfo::UnrollingPreferences &UP, bool &UseUpperBound)
DependenceInfo - This class is the main dependence-analysis driver.
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)
BlockT * getHeader() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
static bool HasUnrollAndJamDisablePragma(const Loop *L)
LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple, bool UnrollRemainder, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, OptimizationRemarkEmitter *ORE)
bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT, DependenceInfo &DI)
This header provides classes for managing per-loop analyses.
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.
initializer< Ty > init(const Ty &Val)
TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, int OptLevel, Optional< unsigned > UserThreshold, Optional< unsigned > UserCount, Optional< bool > UserAllowPartial, Optional< bool > UserRuntime, Optional< bool > UserUpperBound, Optional< bool > UserAllowPeeling)
Gather the various unrolling parameters based on the defaults, compiler flags, TTI overrides and user...
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)
static MDNode * GetUnrollMetadataForLoop(const Loop *L, StringRef Name)
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)
TargetTransformInfo & TTI
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.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
void markLoopAsDeleted(Loop &L)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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...
const std::vector< LoopT * > & getSubLoops() const
Return the loops contained entirely within this loop.
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 getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
The loop was not modified.
void initializeLoopUnrollAndJamPass(PassRegistry &)
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
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.