15 #define DEBUG_TYPE "loop-unroll"
33 cl::desc(
"The cut-off point for automatic loop unrolling"));
37 cl::desc(
"Use this unroll count for all loops, for testing purposes"));
41 cl::desc(
"Allows loops to be partially unrolled until "
42 "-unroll-threshold loop size is reached."));
46 cl::desc(
"Unroll loops with run-time trip counts"));
52 LoopUnroll(
int T = -1,
int C = -1,
int P = -1,
int R = -1) :
LoopPass(
ID) {
59 UserAllowPartial = (
P != -1) ||
61 UserRuntime = (R != -1) || (
UnrollRuntime.getNumOccurrences() > 0);
62 UserCount = (
C != -1) || (
UnrollCount.getNumOccurrences() > 0);
70 static const unsigned NoThreshold = UINT_MAX;
74 static const unsigned OptSizeUnrollThreshold = 50;
78 static const unsigned UnrollRuntimeCount = 8;
80 unsigned CurrentCount;
81 unsigned CurrentThreshold;
82 bool CurrentAllowPartial;
86 bool UserAllowPartial;
124 return new LoopUnroll(Threshold, Count, AllowPartial, Runtime);
129 bool &NotDuplicatable,
138 unsigned LoopSize = Metrics.
NumInsts;
143 if (LoopSize == 0) LoopSize = 1;
154 DEBUG(
dbgs() <<
"Loop Unroll: F[" << Header->getParent()->getName()
155 <<
"] Loop %" << Header->getName() <<
"\n");
160 UP.OptSizeThreshold = OptSizeUnrollThreshold;
161 UP.Count = CurrentCount;
162 UP.Partial = CurrentAllowPartial;
163 UP.Runtime = CurrentRuntime;
164 TTI.getUnrollingPreferences(L, UP);
170 unsigned Threshold = UserThreshold ? CurrentThreshold : UP.Threshold;
171 if (!UserThreshold &&
172 Header->getParent()->getAttributes().
173 hasAttribute(AttributeSet::FunctionIndex,
175 Threshold = UP.OptSizeThreshold;
178 unsigned TripCount = 0;
179 unsigned TripMultiple = 1;
185 TripCount = SE->getSmallConstantTripCount(L, LatchBlock);
186 TripMultiple = SE->getSmallConstantTripMultiple(L, LatchBlock);
189 bool Runtime = UserRuntime ? CurrentRuntime : UP.Runtime;
194 unsigned Count = UserCount ? CurrentCount : UP.Count;
195 if (Runtime && Count == 0 && TripCount == 0)
196 Count = UnrollRuntimeCount;
209 if (Threshold != NoThreshold) {
210 unsigned NumInlineCandidates;
211 bool notDuplicatable;
213 notDuplicatable, TTI);
214 DEBUG(
dbgs() <<
" Loop Size = " << LoopSize <<
"\n");
215 if (notDuplicatable) {
216 DEBUG(
dbgs() <<
" Not unrolling loop which contains non duplicatable"
217 <<
" instructions.\n");
220 if (NumInlineCandidates != 0) {
221 DEBUG(
dbgs() <<
" Not unrolling loop with inlinable calls.\n");
224 uint64_t Size = (uint64_t)LoopSize*Count;
225 if (TripCount != 1 && Size > Threshold) {
226 DEBUG(
dbgs() <<
" Too large to fully unroll with count: " << Count
227 <<
" because size: " << Size <<
">" << Threshold <<
"\n");
228 bool AllowPartial = UserAllowPartial ? CurrentAllowPartial : UP.Partial;
229 if (!AllowPartial && !(Runtime && TripCount == 0)) {
230 DEBUG(
dbgs() <<
" will not try to unroll partially because "
231 <<
"-unroll-allow-partial not given\n");
236 Count = Threshold / LoopSize;
237 while (Count != 0 && TripCount%Count != 0)
242 while (Count != 0 && Size > Threshold) {
244 Size = LoopSize*Count;
248 DEBUG(
dbgs() <<
" could not unroll partially\n");
251 DEBUG(
dbgs() <<
" partially unrolling with count: " << Count <<
"\n");
256 if (!
UnrollLoop(L, Count, TripCount, Runtime, TripMultiple, LI, &LPM))
AnalysisUsage & addPreserved()
static PassRegistry * getPassRegistry()
unsigned NumInlineCandidates
The number of calls to internal functions with a single caller.
bool notDuplicatable
True if this function cannot be duplicated.
static cl::opt< unsigned > UnrollCount("unroll-count", cl::init(0), cl::Hidden, cl::desc("Use this unroll count for all loops, for testing purposes"))
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
BlockT * getLoopLatch() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI)
Add information about a block to the current state.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
ID
LLVM Calling Convention Representation.
bool UnrollLoop(Loop *L, unsigned Count, unsigned TripCount, bool AllowRuntime, unsigned TripMultiple, LoopInfo *LI, LPPassManager *LPM)
AnalysisUsage & addPreservedID(const void *ID)
Pass * createLoopUnrollPass(int Threshold=-1, int Count=-1, int AllowPartial=-1, int Runtime=-1)
void initializeLoopUnrollPass(PassRegistry &)
initializer< Ty > init(const Ty &Val)
static cl::opt< bool > UnrollAllowPartial("unroll-allow-partial", cl::init(false), cl::Hidden, cl::desc("Allows loops to be partially unrolled until ""-unroll-threshold loop size is reached."))
LLVM Basic Block Representation.
static cl::opt< bool > UnrollRuntime("unroll-runtime", cl::ZeroOrMore, cl::init(false), cl::Hidden, cl::desc("Unroll loops with run-time trip counts"))
#define INITIALIZE_AG_DEPENDENCY(depName)
static cl::opt< unsigned > UnrollThreshold("unroll-threshold", cl::init(150), cl::Hidden, cl::desc("The cut-off point for automatic loop unrolling"))
machine trace Machine Trace Metrics
AnalysisUsage & addRequiredID(const void *ID)
Utility to calculate the size and a few similar metrics for a set of basic blocks.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
std::vector< BlockT * >::const_iterator block_iterator
block_iterator block_end() const
static int const Threshold
static unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls, bool &NotDuplicatable, const TargetTransformInfo &TTI)
ApproximateLoopSize - Approximate the size of the loop.
block_iterator block_begin() const
unsigned NumInsts
Number of instructions in the analyzed blocks.