1
//===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines the LoopInfo class that is used to identify natural loops
11
// and determine the loop depth of various nodes of the CFG. Note that the
12
// loops identified may actually be several natural loops that share the same
13
// header node... not just a single natural loop.
15
//===----------------------------------------------------------------------===//
17
#include "llvm/Analysis/LoopInfo.h"
18
#include "llvm/Constants.h"
19
#include "llvm/Instructions.h"
20
#include "llvm/Analysis/Dominators.h"
21
#include "llvm/Assembly/Writer.h"
22
#include "llvm/Support/CFG.h"
23
#include "llvm/Support/CommandLine.h"
24
#include "llvm/Support/Debug.h"
25
#include "llvm/ADT/DepthFirstIterator.h"
26
#include "llvm/ADT/SmallPtrSet.h"
30
// Always verify loopinfo if expensive checking is enabled.
32
bool VerifyLoopInfo = true;
34
bool VerifyLoopInfo = false;
36
static cl::opt<bool,true>
37
VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
38
cl::desc("Verify loop info (time consuming)"));
40
char LoopInfo::ID = 0;
41
static RegisterPass<LoopInfo>
42
X("loops", "Natural Loop Information", true, true);
44
//===----------------------------------------------------------------------===//
45
// Loop implementation
48
/// isLoopInvariant - Return true if the specified value is loop invariant
50
bool Loop::isLoopInvariant(Value *V) const {
51
if (Instruction *I = dyn_cast<Instruction>(V))
52
return isLoopInvariant(I);
53
return true; // All non-instructions are loop invariant
56
/// isLoopInvariant - Return true if the specified instruction is
59
bool Loop::isLoopInvariant(Instruction *I) const {
63
/// makeLoopInvariant - If the given value is an instruciton inside of the
64
/// loop and it can be hoisted, do so to make it trivially loop-invariant.
65
/// Return true if the value after any hoisting is loop invariant. This
66
/// function can be used as a slightly more aggressive replacement for
69
/// If InsertPt is specified, it is the point to hoist instructions to.
70
/// If null, the terminator of the loop preheader is used.
72
bool Loop::makeLoopInvariant(Value *V, bool &Changed,
73
Instruction *InsertPt) const {
74
if (Instruction *I = dyn_cast<Instruction>(V))
75
return makeLoopInvariant(I, Changed, InsertPt);
76
return true; // All non-instructions are loop-invariant.
79
/// makeLoopInvariant - If the given instruction is inside of the
80
/// loop and it can be hoisted, do so to make it trivially loop-invariant.
81
/// Return true if the instruction after any hoisting is loop invariant. This
82
/// function can be used as a slightly more aggressive replacement for
85
/// If InsertPt is specified, it is the point to hoist instructions to.
86
/// If null, the terminator of the loop preheader is used.
88
bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
89
Instruction *InsertPt) const {
90
// Test if the value is already loop-invariant.
91
if (isLoopInvariant(I))
93
if (!I->isSafeToSpeculativelyExecute())
95
if (I->mayReadFromMemory())
97
// Determine the insertion point, unless one was given.
99
BasicBlock *Preheader = getLoopPreheader();
100
// Without a preheader, hoisting is not feasible.
103
InsertPt = Preheader->getTerminator();
105
// Don't hoist instructions with loop-variant operands.
106
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
107
if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt))
110
I->moveBefore(InsertPt);
115
/// getCanonicalInductionVariable - Check to see if the loop has a canonical
116
/// induction variable: an integer recurrence that starts at 0 and increments
117
/// by one each time through the loop. If so, return the phi node that
118
/// corresponds to it.
120
/// The IndVarSimplify pass transforms loops to have a canonical induction
123
PHINode *Loop::getCanonicalInductionVariable() const {
124
BasicBlock *H = getHeader();
126
BasicBlock *Incoming = 0, *Backedge = 0;
127
typedef GraphTraits<Inverse<BasicBlock*> > InvBlockTraits;
128
InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(H);
129
assert(PI != InvBlockTraits::child_end(H) &&
130
"Loop must have at least one backedge!");
132
if (PI == InvBlockTraits::child_end(H)) return 0; // dead loop
134
if (PI != InvBlockTraits::child_end(H)) return 0; // multiple backedges?
136
if (contains(Incoming)) {
137
if (contains(Backedge))
139
std::swap(Incoming, Backedge);
140
} else if (!contains(Backedge))
143
// Loop over all of the PHI nodes, looking for a canonical indvar.
144
for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
145
PHINode *PN = cast<PHINode>(I);
146
if (ConstantInt *CI =
147
dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
148
if (CI->isNullValue())
149
if (Instruction *Inc =
150
dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
151
if (Inc->getOpcode() == Instruction::Add &&
152
Inc->getOperand(0) == PN)
153
if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
154
if (CI->equalsInt(1))
160
/// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
161
/// the canonical induction variable value for the "next" iteration of the
162
/// loop. This always succeeds if getCanonicalInductionVariable succeeds.
164
Instruction *Loop::getCanonicalInductionVariableIncrement() const {
165
if (PHINode *PN = getCanonicalInductionVariable()) {
166
bool P1InLoop = contains(PN->getIncomingBlock(1));
167
return cast<Instruction>(PN->getIncomingValue(P1InLoop));
172
/// getTripCount - Return a loop-invariant LLVM value indicating the number of
173
/// times the loop will be executed. Note that this means that the backedge
174
/// of the loop executes N-1 times. If the trip-count cannot be determined,
175
/// this returns null.
177
/// The IndVarSimplify pass transforms loops to have a form that this
178
/// function easily understands.
180
Value *Loop::getTripCount() const {
181
// Canonical loops will end with a 'cmp ne I, V', where I is the incremented
182
// canonical induction variable and V is the trip count of the loop.
183
Instruction *Inc = getCanonicalInductionVariableIncrement();
184
if (Inc == 0) return 0;
185
PHINode *IV = cast<PHINode>(Inc->getOperand(0));
187
BasicBlock *BackedgeBlock =
188
IV->getIncomingBlock(contains(IV->getIncomingBlock(1)));
190
if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
191
if (BI->isConditional()) {
192
if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
193
if (ICI->getOperand(0) == Inc) {
194
if (BI->getSuccessor(0) == getHeader()) {
195
if (ICI->getPredicate() == ICmpInst::ICMP_NE)
196
return ICI->getOperand(1);
197
} else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
198
return ICI->getOperand(1);
207
/// getSmallConstantTripCount - Returns the trip count of this loop as a
208
/// normal unsigned value, if possible. Returns 0 if the trip count is unknown
209
/// of not constant. Will also return 0 if the trip count is very large
211
unsigned Loop::getSmallConstantTripCount() const {
212
Value* TripCount = this->getTripCount();
214
if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) {
215
// Guard against huge trip counts.
216
if (TripCountC->getValue().getActiveBits() <= 32) {
217
return (unsigned)TripCountC->getZExtValue();
224
/// getSmallConstantTripMultiple - Returns the largest constant divisor of the
225
/// trip count of this loop as a normal unsigned value, if possible. This
226
/// means that the actual trip count is always a multiple of the returned
227
/// value (don't forget the trip count could very well be zero as well!).
229
/// Returns 1 if the trip count is unknown or not guaranteed to be the
230
/// multiple of a constant (which is also the case if the trip count is simply
231
/// constant, use getSmallConstantTripCount for that case), Will also return 1
232
/// if the trip count is very large (>= 2^32).
233
unsigned Loop::getSmallConstantTripMultiple() const {
234
Value* TripCount = this->getTripCount();
235
// This will hold the ConstantInt result, if any
236
ConstantInt *Result = NULL;
238
// See if the trip count is constant itself
239
Result = dyn_cast<ConstantInt>(TripCount);
240
// if not, see if it is a multiplication
242
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) {
243
switch (BO->getOpcode()) {
244
case BinaryOperator::Mul:
245
Result = dyn_cast<ConstantInt>(BO->getOperand(1));
247
case BinaryOperator::Shl:
248
if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
249
if (CI->getValue().getActiveBits() <= 5)
250
return 1u << CI->getZExtValue();
257
// Guard against huge trip counts.
258
if (Result && Result->getValue().getActiveBits() <= 32) {
259
return (unsigned)Result->getZExtValue();
265
/// isLCSSAForm - Return true if the Loop is in LCSSA form
266
bool Loop::isLCSSAForm() const {
267
// Sort the blocks vector so that we can use binary search to do quick
269
SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end());
271
for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
272
BasicBlock *BB = *BI;
273
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;++I)
274
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
276
BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
277
if (PHINode *P = dyn_cast<PHINode>(*UI))
278
UserBB = P->getIncomingBlock(UI);
280
// Check the current block, as a fast-path. Most values are used in
281
// the same block they are defined in.
282
if (UserBB != BB && !LoopBBs.count(UserBB))
290
/// isLoopSimplifyForm - Return true if the Loop is in the form that
291
/// the LoopSimplify form transforms loops to, which is sometimes called
293
bool Loop::isLoopSimplifyForm() const {
294
// Normal-form loops have a preheader, a single backedge, and all of their
295
// exits have all their predecessors inside the loop.
296
return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
299
/// hasDedicatedExits - Return true if no exit block for the loop
300
/// has a predecessor that is outside the loop.
301
bool Loop::hasDedicatedExits() const {
302
// Sort the blocks vector so that we can use binary search to do quick
304
SmallPtrSet<BasicBlock *, 16> LoopBBs(block_begin(), block_end());
305
// Each predecessor of each exit block of a normal loop is contained
307
SmallVector<BasicBlock *, 4> ExitBlocks;
308
getExitBlocks(ExitBlocks);
309
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
310
for (pred_iterator PI = pred_begin(ExitBlocks[i]),
311
PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
312
if (!LoopBBs.count(*PI))
314
// All the requirements are met.
318
/// getUniqueExitBlocks - Return all unique successor blocks of this loop.
319
/// These are the blocks _outside of the current loop_ which are branched to.
320
/// This assumes that loop exits are in canonical form.
323
Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
324
assert(hasDedicatedExits() &&
325
"getUniqueExitBlocks assumes the loop has canonical form exits!");
327
// Sort the blocks vector so that we can use binary search to do quick
329
SmallVector<BasicBlock *, 128> LoopBBs(block_begin(), block_end());
330
std::sort(LoopBBs.begin(), LoopBBs.end());
332
SmallVector<BasicBlock *, 32> switchExitBlocks;
334
for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
336
BasicBlock *current = *BI;
337
switchExitBlocks.clear();
339
typedef GraphTraits<BasicBlock *> BlockTraits;
340
typedef GraphTraits<Inverse<BasicBlock *> > InvBlockTraits;
341
for (BlockTraits::ChildIteratorType I =
342
BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
344
// If block is inside the loop then it is not a exit block.
345
if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
348
InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(*I);
349
BasicBlock *firstPred = *PI;
351
// If current basic block is this exit block's first predecessor
352
// then only insert exit block in to the output ExitBlocks vector.
353
// This ensures that same exit block is not inserted twice into
354
// ExitBlocks vector.
355
if (current != firstPred)
358
// If a terminator has more then two successors, for example SwitchInst,
359
// then it is possible that there are multiple edges from current block
360
// to one exit block.
361
if (std::distance(BlockTraits::child_begin(current),
362
BlockTraits::child_end(current)) <= 2) {
363
ExitBlocks.push_back(*I);
367
// In case of multiple edges from current block to exit block, collect
368
// only one edge in ExitBlocks. Use switchExitBlocks to keep track of
370
if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
371
== switchExitBlocks.end()) {
372
switchExitBlocks.push_back(*I);
373
ExitBlocks.push_back(*I);
379
/// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
380
/// block, return that block. Otherwise return null.
381
BasicBlock *Loop::getUniqueExitBlock() const {
382
SmallVector<BasicBlock *, 8> UniqueExitBlocks;
383
getUniqueExitBlocks(UniqueExitBlocks);
384
if (UniqueExitBlocks.size() == 1)
385
return UniqueExitBlocks[0];
389
void Loop::dump() const {
393
//===----------------------------------------------------------------------===//
394
// LoopInfo implementation
396
bool LoopInfo::runOnFunction(Function &) {
398
LI.Calculate(getAnalysis<DominatorTree>().getBase()); // Update
402
void LoopInfo::verifyAnalysis() const {
403
// LoopInfo is a FunctionPass, but verifying every loop in the function
404
// each time verifyAnalysis is called is very expensive. The
405
// -verify-loop-info option can enable this. In order to perform some
406
// checking by default, LoopPass has been taught to call verifyLoop
407
// manually during loop pass sequences.
409
if (!VerifyLoopInfo) return;
411
for (iterator I = begin(), E = end(); I != E; ++I) {
412
assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
413
(*I)->verifyLoopNest();
416
// TODO: check BBMap consistency.
419
void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
420
AU.setPreservesAll();
421
AU.addRequired<DominatorTree>();
424
void LoopInfo::print(raw_ostream &OS, const Module*) const {