2003-10-13 11:32:08 +08:00
|
|
|
//===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-27 02:41:20 +08:00
|
|
|
//
|
|
|
|
// This file defines the LoopInfo class that is used to identify natural loops
|
|
|
|
// and determine the loop depth of various nodes of the CFG. Note that the
|
|
|
|
// loops identified may actually be several natural loops that share the same
|
|
|
|
// header node... not just a single natural loop.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-01-31 01:26:24 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2017-09-20 10:31:57 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-06-20 11:42:09 +08:00
|
|
|
#include "llvm/Analysis/LoopInfoImpl.h"
|
2011-08-10 09:59:05 +08:00
|
|
|
#include "llvm/Analysis/LoopIterator.h"
|
2011-12-15 07:49:11 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-10-21 08:13:20 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-02-14 02:08:57 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
2015-01-20 18:58:50 +08:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2009-09-28 08:27:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-01-06 05:08:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2001-11-27 02:41:20 +08:00
|
|
|
#include <algorithm>
|
2004-04-13 04:26:17 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2012-06-20 11:42:09 +08:00
|
|
|
// Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
|
|
|
|
template class llvm::LoopBase<BasicBlock, Loop>;
|
|
|
|
template class llvm::LoopInfoBase<BasicBlock, Loop>;
|
|
|
|
|
2009-09-28 08:27:48 +08:00
|
|
|
// Always verify loopinfo if expensive checking is enabled.
|
2016-04-29 23:22:48 +08:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
2017-01-24 13:52:07 +08:00
|
|
|
bool llvm::VerifyLoopInfo = true;
|
2009-09-28 08:27:48 +08:00
|
|
|
#else
|
2017-01-24 13:52:07 +08:00
|
|
|
bool llvm::VerifyLoopInfo = false;
|
2009-09-28 08:27:48 +08:00
|
|
|
#endif
|
2017-09-20 09:12:09 +08:00
|
|
|
static cl::opt<bool, true>
|
|
|
|
VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
|
|
|
|
cl::desc("Verify loop info (time consuming)"));
|
2009-09-28 08:27:48 +08:00
|
|
|
|
2002-01-31 08:42:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-29 00:21:30 +08:00
|
|
|
// Loop implementation
|
2002-01-31 08:42:27 +08:00
|
|
|
//
|
2002-10-11 13:31:10 +08:00
|
|
|
|
2015-05-13 09:12:06 +08:00
|
|
|
bool Loop::isLoopInvariant(const Value *V) const {
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V))
|
2010-09-06 09:05:37 +08:00
|
|
|
return !contains(I);
|
2017-09-20 09:12:09 +08:00
|
|
|
return true; // All non-instructions are loop invariant
|
2009-07-14 06:02:44 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 09:12:06 +08:00
|
|
|
bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
|
2015-05-14 06:19:13 +08:00
|
|
|
return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
|
2009-07-14 09:06:29 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 09:25:43 +08:00
|
|
|
bool Loop::makeLoopInvariant(Value *V, bool &Changed,
|
|
|
|
Instruction *InsertPt) const {
|
2009-07-14 09:06:29 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
2009-07-15 09:25:43 +08:00
|
|
|
return makeLoopInvariant(I, Changed, InsertPt);
|
2017-09-20 09:12:09 +08:00
|
|
|
return true; // All non-instructions are loop-invariant.
|
2009-07-14 09:06:29 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 09:25:43 +08:00
|
|
|
bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
|
|
|
|
Instruction *InsertPt) const {
|
2009-07-14 09:06:29 +08:00
|
|
|
// Test if the value is already loop-invariant.
|
|
|
|
if (isLoopInvariant(I))
|
|
|
|
return true;
|
2011-12-15 07:49:11 +08:00
|
|
|
if (!isSafeToSpeculativelyExecute(I))
|
2009-07-14 09:06:29 +08:00
|
|
|
return false;
|
2009-07-17 12:28:42 +08:00
|
|
|
if (I->mayReadFromMemory())
|
2009-07-14 09:06:29 +08:00
|
|
|
return false;
|
2015-08-01 01:58:14 +08:00
|
|
|
// EH block instructions are immobile.
|
|
|
|
if (I->isEHPad())
|
2011-08-18 04:36:44 +08:00
|
|
|
return false;
|
2009-07-14 09:06:29 +08:00
|
|
|
// Determine the insertion point, unless one was given.
|
|
|
|
if (!InsertPt) {
|
|
|
|
BasicBlock *Preheader = getLoopPreheader();
|
|
|
|
// Without a preheader, hoisting is not feasible.
|
|
|
|
if (!Preheader)
|
|
|
|
return false;
|
|
|
|
InsertPt = Preheader->getTerminator();
|
|
|
|
}
|
|
|
|
// Don't hoist instructions with loop-variant operands.
|
2016-01-15 08:08:10 +08:00
|
|
|
for (Value *Operand : I->operands())
|
|
|
|
if (!makeLoopInvariant(Operand, Changed, InsertPt))
|
2009-07-14 09:06:29 +08:00
|
|
|
return false;
|
2011-08-04 07:45:50 +08:00
|
|
|
|
2009-07-14 09:06:29 +08:00
|
|
|
// Hoist.
|
|
|
|
I->moveBefore(InsertPt);
|
2015-11-18 22:50:18 +08:00
|
|
|
|
|
|
|
// There is possibility of hoisting this instruction above some arbitrary
|
|
|
|
// condition. Any metadata defined on it can be control dependent on this
|
|
|
|
// condition. Conservatively strip it here so that we don't give any wrong
|
|
|
|
// information to the optimizer.
|
|
|
|
I->dropUnknownNonDebugMetadata();
|
|
|
|
|
2009-07-15 09:25:43 +08:00
|
|
|
Changed = true;
|
2009-07-14 09:06:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-14 06:02:44 +08:00
|
|
|
PHINode *Loop::getCanonicalInductionVariable() const {
|
|
|
|
BasicBlock *H = getHeader();
|
|
|
|
|
2014-04-15 12:59:12 +08:00
|
|
|
BasicBlock *Incoming = nullptr, *Backedge = nullptr;
|
2010-07-24 05:25:16 +08:00
|
|
|
pred_iterator PI = pred_begin(H);
|
2017-09-20 09:12:09 +08:00
|
|
|
assert(PI != pred_end(H) && "Loop must have at least one backedge!");
|
2009-07-14 06:02:44 +08:00
|
|
|
Backedge = *PI++;
|
2017-09-20 09:12:09 +08:00
|
|
|
if (PI == pred_end(H))
|
|
|
|
return nullptr; // dead loop
|
2009-07-14 06:02:44 +08:00
|
|
|
Incoming = *PI++;
|
2017-09-20 09:12:09 +08:00
|
|
|
if (PI != pred_end(H))
|
|
|
|
return nullptr; // multiple backedges?
|
2009-07-14 06:02:44 +08:00
|
|
|
|
|
|
|
if (contains(Incoming)) {
|
|
|
|
if (contains(Backedge))
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-07-14 06:02:44 +08:00
|
|
|
std::swap(Incoming, Backedge);
|
|
|
|
} else if (!contains(Backedge))
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-07-14 06:02:44 +08:00
|
|
|
|
|
|
|
// Loop over all of the PHI nodes, looking for a canonical indvar.
|
|
|
|
for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
if (ConstantInt *CI =
|
2017-09-20 09:12:09 +08:00
|
|
|
dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
|
2017-07-07 02:39:47 +08:00
|
|
|
if (CI->isZero())
|
2009-07-14 06:02:44 +08:00
|
|
|
if (Instruction *Inc =
|
2017-09-20 09:12:09 +08:00
|
|
|
dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
|
|
|
|
if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
|
2009-07-14 06:02:44 +08:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
|
2017-07-07 02:39:49 +08:00
|
|
|
if (CI->isOne())
|
2009-07-14 06:02:44 +08:00
|
|
|
return PN;
|
|
|
|
}
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-07-14 06:02:44 +08:00
|
|
|
}
|
|
|
|
|
2016-10-11 21:37:22 +08:00
|
|
|
// Check that 'BB' doesn't have any uses outside of the 'L'
|
|
|
|
static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
|
|
|
|
DominatorTree &DT) {
|
|
|
|
for (const Instruction &I : BB) {
|
|
|
|
// Tokens can't be used in PHI nodes and live-out tokens prevent loop
|
|
|
|
// optimizations, so for the purposes of considered LCSSA form, we
|
|
|
|
// can ignore them.
|
|
|
|
if (I.getType()->isTokenTy())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const Use &U : I.uses()) {
|
|
|
|
const Instruction *UI = cast<Instruction>(U.getUser());
|
|
|
|
const BasicBlock *UserBB = UI->getParent();
|
|
|
|
if (const PHINode *P = dyn_cast<PHINode>(UI))
|
|
|
|
UserBB = P->getIncomingBlock(U);
|
|
|
|
|
|
|
|
// Check the current block, as a fast-path, before checking whether
|
|
|
|
// the use is anywhere in the loop. Most values are used in the same
|
|
|
|
// block they are defined in. Also, blocks not reachable from the
|
|
|
|
// entry are special; uses in them don't need to go through PHIs.
|
|
|
|
if (UserBB != &BB && !L.contains(UserBB) &&
|
|
|
|
DT.isReachableFromEntry(UserBB))
|
|
|
|
return false;
|
2015-12-19 02:12:35 +08:00
|
|
|
}
|
2009-07-14 06:02:44 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-17 00:16:23 +08:00
|
|
|
|
2016-10-11 21:37:22 +08:00
|
|
|
bool Loop::isLCSSAForm(DominatorTree &DT) const {
|
|
|
|
// For each block we check that it doesn't have any uses outside of this loop.
|
|
|
|
return all_of(this->blocks(), [&](const BasicBlock *BB) {
|
|
|
|
return isBlockInLCSSAForm(*this, *BB, DT);
|
|
|
|
});
|
|
|
|
}
|
2015-12-08 08:13:21 +08:00
|
|
|
|
2016-10-11 21:37:22 +08:00
|
|
|
bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
|
2017-01-09 06:22:09 +08:00
|
|
|
// For each block we check that it doesn't have any uses outside of its
|
|
|
|
// innermost loop. This process will transitively guarantee that the current
|
|
|
|
// loop and all of the nested loops are in LCSSA form.
|
2016-10-11 21:37:22 +08:00
|
|
|
return all_of(this->blocks(), [&](const BasicBlock *BB) {
|
|
|
|
return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
|
|
|
|
});
|
2015-12-08 08:13:21 +08:00
|
|
|
}
|
|
|
|
|
2009-07-17 00:16:23 +08:00
|
|
|
bool Loop::isLoopSimplifyForm() const {
|
2009-11-06 03:21:41 +08:00
|
|
|
// Normal-form loops have a preheader, a single backedge, and all of their
|
|
|
|
// exits have all their predecessors inside the loop.
|
|
|
|
return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
|
|
|
|
}
|
|
|
|
|
2016-01-15 07:23:04 +08:00
|
|
|
// Routines that reform the loop CFG and split edges often fail on indirectbr.
|
2012-04-10 13:14:42 +08:00
|
|
|
bool Loop::isSafeToClone() const {
|
2012-12-21 00:04:27 +08:00
|
|
|
// Return false if any loop blocks contain indirectbrs, or there are any calls
|
|
|
|
// to noduplicate functions.
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : this->blocks()) {
|
|
|
|
if (isa<IndirectBrInst>(BB->getTerminator()))
|
2012-04-10 13:14:42 +08:00
|
|
|
return false;
|
2013-11-14 04:18:38 +08:00
|
|
|
|
2016-05-03 11:57:40 +08:00
|
|
|
for (Instruction &I : *BB)
|
|
|
|
if (auto CS = CallSite(&I))
|
|
|
|
if (CS.cannotDuplicate())
|
2012-12-21 00:04:27 +08:00
|
|
|
return false;
|
2012-04-10 13:14:42 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-29 04:00:34 +08:00
|
|
|
MDNode *Loop::getLoopID() const {
|
2014-04-15 12:59:12 +08:00
|
|
|
MDNode *LoopID = nullptr;
|
2017-01-16 05:17:52 +08:00
|
|
|
if (BasicBlock *Latch = getLoopLatch()) {
|
|
|
|
LoopID = Latch->getTerminator()->getMetadata(LLVMContext::MD_loop);
|
2013-05-29 04:00:34 +08:00
|
|
|
} else {
|
2017-01-16 05:17:52 +08:00
|
|
|
assert(!getLoopLatch() &&
|
|
|
|
"The loop should have no single latch at this point");
|
2013-05-29 04:00:34 +08:00
|
|
|
// Go through each predecessor of the loop header and check the
|
|
|
|
// terminator for the metadata.
|
|
|
|
BasicBlock *H = getHeader();
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : this->blocks()) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
2014-04-15 12:59:12 +08:00
|
|
|
MDNode *MD = nullptr;
|
2013-02-14 02:08:57 +08:00
|
|
|
|
2013-05-29 04:00:34 +08:00
|
|
|
// Check if this terminator branches to the loop header.
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *Successor : TI->successors()) {
|
|
|
|
if (Successor == H) {
|
2016-03-25 08:35:38 +08:00
|
|
|
MD = TI->getMetadata(LLVMContext::MD_loop);
|
2013-05-29 04:00:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!MD)
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2013-05-29 04:00:34 +08:00
|
|
|
|
|
|
|
if (!LoopID)
|
|
|
|
LoopID = MD;
|
|
|
|
else if (MD != LoopID)
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2013-05-29 04:00:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!LoopID || LoopID->getNumOperands() == 0 ||
|
|
|
|
LoopID->getOperand(0) != LoopID)
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2013-05-29 04:00:34 +08:00
|
|
|
return LoopID;
|
|
|
|
}
|
2013-02-14 02:08:57 +08:00
|
|
|
|
2013-05-29 04:00:34 +08:00
|
|
|
void Loop::setLoopID(MDNode *LoopID) const {
|
|
|
|
assert(LoopID && "Loop ID should not be null");
|
|
|
|
assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
|
|
|
|
assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
|
|
|
|
|
2017-01-16 05:17:52 +08:00
|
|
|
if (BasicBlock *Latch = getLoopLatch()) {
|
|
|
|
Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
|
2013-05-29 04:00:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-20 09:12:09 +08:00
|
|
|
assert(!getLoopLatch() &&
|
|
|
|
"The loop should have no single latch at this point");
|
2013-05-29 04:00:34 +08:00
|
|
|
BasicBlock *H = getHeader();
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : this->blocks()) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
for (BasicBlock *Successor : TI->successors()) {
|
|
|
|
if (Successor == H)
|
2016-03-25 08:35:38 +08:00
|
|
|
TI->setMetadata(LLVMContext::MD_loop, LoopID);
|
2013-05-29 04:00:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 15:31:02 +08:00
|
|
|
void Loop::setLoopAlreadyUnrolled() {
|
|
|
|
MDNode *LoopID = getLoopID();
|
|
|
|
// First remove any existing loop unrolling metadata.
|
|
|
|
SmallVector<Metadata *, 4> MDs;
|
|
|
|
// Reserve first location for self reference to the LoopID metadata node.
|
|
|
|
MDs.push_back(nullptr);
|
|
|
|
|
|
|
|
if (LoopID) {
|
|
|
|
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
|
|
|
|
bool IsUnrollMetadata = false;
|
|
|
|
MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
|
|
|
|
if (MD) {
|
|
|
|
const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
|
|
|
|
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
|
|
|
|
}
|
|
|
|
if (!IsUnrollMetadata)
|
|
|
|
MDs.push_back(LoopID->getOperand(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add unroll(disable) metadata to disable future unrolling.
|
|
|
|
LLVMContext &Context = getHeader()->getContext();
|
|
|
|
SmallVector<Metadata *, 1> DisableOperands;
|
|
|
|
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
|
|
|
|
MDNode *DisableNode = MDNode::get(Context, DisableOperands);
|
|
|
|
MDs.push_back(DisableNode);
|
|
|
|
|
|
|
|
MDNode *NewLoopID = MDNode::get(Context, MDs);
|
|
|
|
// Set operand 0 to refer to the loop id itself.
|
|
|
|
NewLoopID->replaceOperandWith(0, NewLoopID);
|
|
|
|
setLoopID(NewLoopID);
|
|
|
|
}
|
|
|
|
|
2013-05-29 04:00:34 +08:00
|
|
|
bool Loop::isAnnotatedParallel() const {
|
2016-01-18 07:18:05 +08:00
|
|
|
MDNode *DesiredLoopIdMetadata = getLoopID();
|
2013-02-14 02:08:57 +08:00
|
|
|
|
2016-01-18 07:18:05 +08:00
|
|
|
if (!DesiredLoopIdMetadata)
|
2017-09-20 09:12:09 +08:00
|
|
|
return false;
|
2013-02-14 02:08:57 +08:00
|
|
|
|
|
|
|
// The loop branch contains the parallel loop metadata. In order to ensure
|
|
|
|
// that any parallel-loop-unaware optimization pass hasn't added loop-carried
|
|
|
|
// dependencies (thus converted the loop back to a sequential loop), check
|
|
|
|
// that all the memory instructions in the loop contain parallelism metadata
|
|
|
|
// that point to the same unique "loop id metadata" the loop branch does.
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : this->blocks()) {
|
|
|
|
for (Instruction &I : *BB) {
|
|
|
|
if (!I.mayReadOrWriteMemory())
|
2013-02-14 02:08:57 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// The memory instruction can refer to the loop identifier metadata
|
|
|
|
// directly or indirectly through another list metadata (in case of
|
|
|
|
// nested parallel loops). The loop identifier metadata refers to
|
|
|
|
// itself so we can check both cases with the same routine.
|
2016-01-18 07:18:05 +08:00
|
|
|
MDNode *LoopIdMD =
|
2016-01-15 08:08:10 +08:00
|
|
|
I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
|
2013-11-14 04:18:38 +08:00
|
|
|
|
2016-01-18 07:18:05 +08:00
|
|
|
if (!LoopIdMD)
|
2013-11-14 04:18:38 +08:00
|
|
|
return false;
|
|
|
|
|
2016-01-18 07:18:05 +08:00
|
|
|
bool LoopIdMDFound = false;
|
|
|
|
for (const MDOperand &MDOp : LoopIdMD->operands()) {
|
|
|
|
if (MDOp == DesiredLoopIdMetadata) {
|
|
|
|
LoopIdMDFound = true;
|
2013-02-14 02:08:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 07:18:05 +08:00
|
|
|
if (!LoopIdMDFound)
|
2013-02-14 02:08:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-20 09:12:09 +08:00
|
|
|
DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
|
2016-11-08 19:18:59 +08:00
|
|
|
|
|
|
|
Loop::LocRange Loop::getLocRange() const {
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
// If we have a debug location in the loop ID, then use it.
|
2016-11-08 19:18:59 +08:00
|
|
|
if (MDNode *LoopID = getLoopID()) {
|
|
|
|
DebugLoc Start;
|
|
|
|
// We use the first DebugLoc in the header as the start location of the loop
|
|
|
|
// and if there is a second DebugLoc in the header we use it as end location
|
|
|
|
// of the loop.
|
|
|
|
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
|
|
|
|
if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
|
|
|
|
if (!Start)
|
|
|
|
Start = DebugLoc(L);
|
|
|
|
else
|
|
|
|
return LocRange(Start, DebugLoc(L));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Start)
|
|
|
|
return LocRange(Start);
|
|
|
|
}
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
|
|
|
|
// Try the pre-header first.
|
|
|
|
if (BasicBlock *PHeadBB = getLoopPreheader())
|
|
|
|
if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
|
2016-11-08 19:18:59 +08:00
|
|
|
return LocRange(DL);
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
|
|
|
|
// If we have no pre-header or there are no instructions with debug
|
|
|
|
// info in it, try the header.
|
|
|
|
if (BasicBlock *HeadBB = getHeader())
|
2016-11-08 19:18:59 +08:00
|
|
|
return LocRange(HeadBB->getTerminator()->getDebugLoc());
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
|
2016-11-08 19:18:59 +08:00
|
|
|
return LocRange();
|
Look for a loop's starting location in the llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. A companion Clang patch will cause Clang to insert
llvm.loop metadata with appropriate locations when generating debugging
information. With these changes, our loop remarks have much more accurate
locations.
Differential Revision: http://reviews.llvm.org/D19738
llvm-svn: 270771
2016-05-26 05:42:37 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 03:21:41 +08:00
|
|
|
bool Loop::hasDedicatedExits() const {
|
2009-07-17 00:16:23 +08:00
|
|
|
// Each predecessor of each exit block of a normal loop is contained
|
|
|
|
// within the loop.
|
|
|
|
SmallVector<BasicBlock *, 4> ExitBlocks;
|
|
|
|
getExitBlocks(ExitBlocks);
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : ExitBlocks)
|
|
|
|
for (BasicBlock *Predecessor : predecessors(BB))
|
|
|
|
if (!contains(Predecessor))
|
2009-07-17 00:16:23 +08:00
|
|
|
return false;
|
|
|
|
// All the requirements are met.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-20 09:12:09 +08:00
|
|
|
void Loop::getUniqueExitBlocks(
|
|
|
|
SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
|
2009-12-12 04:05:23 +08:00
|
|
|
assert(hasDedicatedExits() &&
|
|
|
|
"getUniqueExitBlocks assumes the loop has canonical form exits!");
|
2009-09-08 23:45:00 +08:00
|
|
|
|
2016-01-18 07:18:05 +08:00
|
|
|
SmallVector<BasicBlock *, 32> SwitchExitBlocks;
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *BB : this->blocks()) {
|
2016-01-18 07:18:05 +08:00
|
|
|
SwitchExitBlocks.clear();
|
2016-01-15 08:08:10 +08:00
|
|
|
for (BasicBlock *Successor : successors(BB)) {
|
|
|
|
// If block is inside the loop then it is not an exit block.
|
|
|
|
if (contains(Successor))
|
2009-09-04 00:10:48 +08:00
|
|
|
continue;
|
|
|
|
|
2016-01-15 08:08:10 +08:00
|
|
|
pred_iterator PI = pred_begin(Successor);
|
2016-01-18 07:18:05 +08:00
|
|
|
BasicBlock *FirstPred = *PI;
|
2009-09-04 00:10:48 +08:00
|
|
|
|
|
|
|
// If current basic block is this exit block's first predecessor
|
|
|
|
// then only insert exit block in to the output ExitBlocks vector.
|
|
|
|
// This ensures that same exit block is not inserted twice into
|
|
|
|
// ExitBlocks vector.
|
2016-01-18 07:18:05 +08:00
|
|
|
if (BB != FirstPred)
|
2009-09-04 00:10:48 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If a terminator has more then two successors, for example SwitchInst,
|
|
|
|
// then it is possible that there are multiple edges from current block
|
|
|
|
// to one exit block.
|
2016-01-15 08:08:10 +08:00
|
|
|
if (std::distance(succ_begin(BB), succ_end(BB)) <= 2) {
|
|
|
|
ExitBlocks.push_back(Successor);
|
2009-09-04 00:10:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case of multiple edges from current block to exit block, collect
|
|
|
|
// only one edge in ExitBlocks. Use switchExitBlocks to keep track of
|
|
|
|
// duplicate edges.
|
2016-08-12 05:15:00 +08:00
|
|
|
if (!is_contained(SwitchExitBlocks, Successor)) {
|
2016-01-18 07:18:05 +08:00
|
|
|
SwitchExitBlocks.push_back(Successor);
|
2016-01-15 08:08:10 +08:00
|
|
|
ExitBlocks.push_back(Successor);
|
2009-09-04 00:10:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *Loop::getUniqueExitBlock() const {
|
|
|
|
SmallVector<BasicBlock *, 8> UniqueExitBlocks;
|
|
|
|
getUniqueExitBlocks(UniqueExitBlocks);
|
|
|
|
if (UniqueExitBlocks.size() == 1)
|
|
|
|
return UniqueExitBlocks[0];
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-09-04 00:10:48 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:16:06 +08:00
|
|
|
#ifdef LLVM_ENABLE_DUMP
|
2017-09-20 09:12:09 +08:00
|
|
|
LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
|
2016-07-27 13:02:17 +08:00
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
|
2017-09-20 09:12:09 +08:00
|
|
|
print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
|
2016-07-27 13:02:17 +08:00
|
|
|
}
|
2012-09-07 03:55:56 +08:00
|
|
|
#endif
|
2010-01-06 05:08:02 +08:00
|
|
|
|
2011-08-11 07:22:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UnloopUpdater implementation
|
|
|
|
//
|
|
|
|
|
2011-08-19 09:42:18 +08:00
|
|
|
namespace {
|
2011-08-11 07:22:57 +08:00
|
|
|
/// Find the new parent loop for all blocks within the "unloop" whose last
|
|
|
|
/// backedges has just been removed.
|
|
|
|
class UnloopUpdater {
|
2016-05-13 22:54:50 +08:00
|
|
|
Loop &Unloop;
|
2011-08-11 07:22:57 +08:00
|
|
|
LoopInfo *LI;
|
|
|
|
|
|
|
|
LoopBlocksDFS DFS;
|
|
|
|
|
|
|
|
// Map unloop's immediate subloops to their nearest reachable parents. Nested
|
|
|
|
// loops within these subloops will not change parents. However, an immediate
|
|
|
|
// subloop's new parent will be the nearest loop reachable from either its own
|
|
|
|
// exits *or* any of its nested loop's exits.
|
2017-09-20 09:12:09 +08:00
|
|
|
DenseMap<Loop *, Loop *> SubloopParents;
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
// Flag the presence of an irreducible backedge whose destination is a block
|
|
|
|
// directly contained by the original unloop.
|
|
|
|
bool FoundIB;
|
|
|
|
|
|
|
|
public:
|
2017-09-20 09:12:09 +08:00
|
|
|
UnloopUpdater(Loop *UL, LoopInfo *LInfo)
|
|
|
|
: Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
void updateBlockParents();
|
|
|
|
|
2011-08-12 04:27:32 +08:00
|
|
|
void removeBlocksFromAncestors();
|
|
|
|
|
2011-08-11 07:22:57 +08:00
|
|
|
void updateSubloopParents();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
|
|
|
|
};
|
2011-08-19 09:42:18 +08:00
|
|
|
} // end anonymous namespace
|
2011-08-11 07:22:57 +08:00
|
|
|
|
2016-01-15 07:23:04 +08:00
|
|
|
/// Update the parent loop for all blocks that are directly contained within the
|
|
|
|
/// original "unloop".
|
2011-08-11 07:22:57 +08:00
|
|
|
void UnloopUpdater::updateBlockParents() {
|
2016-05-13 22:54:50 +08:00
|
|
|
if (Unloop.getNumBlocks()) {
|
2011-08-11 07:22:57 +08:00
|
|
|
// Perform a post order CFG traversal of all blocks within this loop,
|
2017-07-09 13:54:44 +08:00
|
|
|
// propagating the nearest loop from successors to predecessors.
|
2011-08-11 07:22:57 +08:00
|
|
|
LoopBlocksTraversal Traversal(DFS, LI);
|
2016-06-27 01:27:42 +08:00
|
|
|
for (BasicBlock *POI : Traversal) {
|
2011-08-11 07:22:57 +08:00
|
|
|
|
2016-06-27 01:27:42 +08:00
|
|
|
Loop *L = LI->getLoopFor(POI);
|
|
|
|
Loop *NL = getNearestLoop(POI, L);
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
if (NL != L) {
|
|
|
|
// For reducible loops, NL is now an ancestor of Unloop.
|
2016-05-13 22:54:50 +08:00
|
|
|
assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
|
2011-08-11 07:22:57 +08:00
|
|
|
"uninitialized successor");
|
2016-06-27 01:27:42 +08:00
|
|
|
LI->changeLoopFor(POI, NL);
|
2017-09-20 09:12:09 +08:00
|
|
|
} else {
|
2011-08-11 07:22:57 +08:00
|
|
|
// Or the current block is part of a subloop, in which case its parent
|
|
|
|
// is unchanged.
|
2016-05-13 22:54:50 +08:00
|
|
|
assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
|
2011-08-11 07:22:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Each irreducible loop within the unloop induces a round of iteration using
|
|
|
|
// the DFS result cached by Traversal.
|
|
|
|
bool Changed = FoundIB;
|
|
|
|
for (unsigned NIters = 0; Changed; ++NIters) {
|
2016-05-13 22:54:50 +08:00
|
|
|
assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
// Iterate over the postorder list of blocks, propagating the nearest loop
|
|
|
|
// from successors to predecessors as before.
|
|
|
|
Changed = false;
|
|
|
|
for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
|
2017-09-20 09:12:09 +08:00
|
|
|
POE = DFS.endPostorder();
|
|
|
|
POI != POE; ++POI) {
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
Loop *L = LI->getLoopFor(*POI);
|
|
|
|
Loop *NL = getNearestLoop(*POI, L);
|
|
|
|
if (NL != L) {
|
2016-05-13 22:54:50 +08:00
|
|
|
assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
|
2011-08-11 07:22:57 +08:00
|
|
|
"uninitialized successor");
|
|
|
|
LI->changeLoopFor(*POI, NL);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 07:23:04 +08:00
|
|
|
/// Remove unloop's blocks from all ancestors below their new parents.
|
2011-08-12 04:27:32 +08:00
|
|
|
void UnloopUpdater::removeBlocksFromAncestors() {
|
2011-11-18 11:42:41 +08:00
|
|
|
// Remove all unloop's blocks (including those in nested subloops) from
|
|
|
|
// ancestors below the new parent loop.
|
2017-09-20 09:12:09 +08:00
|
|
|
for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
|
|
|
|
BI != BE; ++BI) {
|
2011-11-18 11:42:41 +08:00
|
|
|
Loop *OuterParent = LI->getLoopFor(*BI);
|
2016-05-13 22:54:50 +08:00
|
|
|
if (Unloop.contains(OuterParent)) {
|
|
|
|
while (OuterParent->getParentLoop() != &Unloop)
|
2011-11-18 11:42:41 +08:00
|
|
|
OuterParent = OuterParent->getParentLoop();
|
|
|
|
OuterParent = SubloopParents[OuterParent];
|
|
|
|
}
|
2011-08-12 04:27:32 +08:00
|
|
|
// Remove blocks from former Ancestors except Unloop itself which will be
|
|
|
|
// deleted.
|
2016-05-13 22:54:50 +08:00
|
|
|
for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
|
2011-08-12 04:27:32 +08:00
|
|
|
OldParent = OldParent->getParentLoop()) {
|
|
|
|
assert(OldParent && "new loop is not an ancestor of the original");
|
|
|
|
OldParent->removeBlockFromLoop(*BI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 07:23:04 +08:00
|
|
|
/// Update the parent loop for all subloops directly nested within unloop.
|
2011-08-11 07:22:57 +08:00
|
|
|
void UnloopUpdater::updateSubloopParents() {
|
2016-05-13 22:54:50 +08:00
|
|
|
while (!Unloop.empty()) {
|
|
|
|
Loop *Subloop = *std::prev(Unloop.end());
|
|
|
|
Unloop.removeChildLoop(std::prev(Unloop.end()));
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
|
2012-08-22 23:37:57 +08:00
|
|
|
if (Loop *Parent = SubloopParents[Subloop])
|
|
|
|
Parent->addChildLoop(Subloop);
|
2011-08-26 11:06:34 +08:00
|
|
|
else
|
|
|
|
LI->addTopLevelLoop(Subloop);
|
2011-08-11 07:22:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 07:23:04 +08:00
|
|
|
/// Return the nearest parent loop among this block's successors. If a successor
|
|
|
|
/// is a subloop header, consider its parent to be the nearest parent of the
|
|
|
|
/// subloop's exits.
|
2011-08-11 07:22:57 +08:00
|
|
|
///
|
|
|
|
/// For subloop blocks, simply update SubloopParents and return NULL.
|
|
|
|
Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
|
|
|
|
|
2011-08-12 01:54:58 +08:00
|
|
|
// Initially for blocks directly contained by Unloop, NearLoop == Unloop and
|
|
|
|
// is considered uninitialized.
|
2011-08-11 07:22:57 +08:00
|
|
|
Loop *NearLoop = BBLoop;
|
|
|
|
|
2014-04-15 12:59:12 +08:00
|
|
|
Loop *Subloop = nullptr;
|
2016-05-13 22:54:50 +08:00
|
|
|
if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
|
2011-08-11 07:22:57 +08:00
|
|
|
Subloop = NearLoop;
|
|
|
|
// Find the subloop ancestor that is directly contained within Unloop.
|
2016-05-13 22:54:50 +08:00
|
|
|
while (Subloop->getParentLoop() != &Unloop) {
|
2011-08-11 07:22:57 +08:00
|
|
|
Subloop = Subloop->getParentLoop();
|
|
|
|
assert(Subloop && "subloop is not an ancestor of the original loop");
|
|
|
|
}
|
|
|
|
// Get the current nearest parent of the Subloop exits, initially Unloop.
|
2016-08-12 05:15:00 +08:00
|
|
|
NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
|
2011-08-11 07:22:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
succ_iterator I = succ_begin(BB), E = succ_end(BB);
|
|
|
|
if (I == E) {
|
|
|
|
assert(!Subloop && "subloop blocks must have a successor");
|
2014-04-15 12:59:12 +08:00
|
|
|
NearLoop = nullptr; // unloop blocks may now exit the function.
|
2011-08-11 07:22:57 +08:00
|
|
|
}
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
if (*I == BB)
|
|
|
|
continue; // self loops are uninteresting
|
|
|
|
|
|
|
|
Loop *L = LI->getLoopFor(*I);
|
2016-05-13 22:54:50 +08:00
|
|
|
if (L == &Unloop) {
|
2011-08-11 07:22:57 +08:00
|
|
|
// This successor has not been processed. This path must lead to an
|
|
|
|
// irreducible backedge.
|
|
|
|
assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
|
|
|
|
FoundIB = true;
|
|
|
|
}
|
2016-05-13 22:54:50 +08:00
|
|
|
if (L != &Unloop && Unloop.contains(L)) {
|
2011-08-11 07:22:57 +08:00
|
|
|
// Successor is in a subloop.
|
|
|
|
if (Subloop)
|
|
|
|
continue; // Branching within subloops. Ignore it.
|
|
|
|
|
|
|
|
// BB branches from the original into a subloop header.
|
2016-05-13 22:54:50 +08:00
|
|
|
assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
// Get the current nearest parent of the Subloop's exits.
|
|
|
|
L = SubloopParents[L];
|
|
|
|
// L could be Unloop if the only exit was an irreducible backedge.
|
|
|
|
}
|
2016-05-13 22:54:50 +08:00
|
|
|
if (L == &Unloop) {
|
2011-08-11 07:22:57 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Handle critical edges from Unloop into a sibling loop.
|
2016-05-13 22:54:50 +08:00
|
|
|
if (L && !L->contains(&Unloop)) {
|
2011-08-11 07:22:57 +08:00
|
|
|
L = L->getParentLoop();
|
|
|
|
}
|
|
|
|
// Remember the nearest parent loop among successors or subloop exits.
|
2016-05-13 22:54:50 +08:00
|
|
|
if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
|
2011-08-11 07:22:57 +08:00
|
|
|
NearLoop = L;
|
|
|
|
}
|
|
|
|
if (Subloop) {
|
|
|
|
SubloopParents[Subloop] = NearLoop;
|
|
|
|
return BBLoop;
|
|
|
|
}
|
|
|
|
return NearLoop;
|
|
|
|
}
|
|
|
|
|
2017-09-20 09:12:09 +08:00
|
|
|
LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
|
2015-07-17 07:23:35 +08:00
|
|
|
|
2017-01-15 14:32:49 +08:00
|
|
|
bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
|
|
|
|
FunctionAnalysisManager::Invalidator &) {
|
|
|
|
// Check whether the analysis, all analyses on functions, or the function's
|
|
|
|
// CFG have been preserved.
|
|
|
|
auto PAC = PA.getChecker<LoopAnalysis>();
|
|
|
|
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
|
|
|
|
PAC.preservedSet<CFGAnalyses>());
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:47:41 +08:00
|
|
|
void LoopInfo::erase(Loop *Unloop) {
|
2017-09-20 07:19:00 +08:00
|
|
|
assert(!Unloop->isInvalid() && "Loop has already been erased!");
|
2011-08-11 07:22:57 +08:00
|
|
|
|
2017-09-28 10:45:42 +08:00
|
|
|
auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
|
2017-09-20 10:31:57 +08:00
|
|
|
|
2011-08-11 07:22:57 +08:00
|
|
|
// First handle the special case of no parent loop to simplify the algorithm.
|
|
|
|
if (!Unloop->getParentLoop()) {
|
|
|
|
// Since BBLoop had no parent, Unloop blocks are no longer in a loop.
|
|
|
|
for (Loop::block_iterator I = Unloop->block_begin(),
|
2015-01-18 09:25:51 +08:00
|
|
|
E = Unloop->block_end();
|
|
|
|
I != E; ++I) {
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
// Don't reparent blocks in subloops.
|
|
|
|
if (getLoopFor(*I) != Unloop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Blocks no longer have a parent but are still referenced by Unloop until
|
|
|
|
// the Unloop object is deleted.
|
2015-01-18 09:25:51 +08:00
|
|
|
changeLoopFor(*I, nullptr);
|
2011-08-11 07:22:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the loop from the top-level LoopInfo object.
|
2015-01-18 09:25:51 +08:00
|
|
|
for (iterator I = begin();; ++I) {
|
|
|
|
assert(I != end() && "Couldn't find loop");
|
2011-08-11 07:22:57 +08:00
|
|
|
if (*I == Unloop) {
|
2015-01-18 09:25:51 +08:00
|
|
|
removeLoop(I);
|
2011-08-11 07:22:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move all of the subloops to the top-level.
|
|
|
|
while (!Unloop->empty())
|
2015-01-18 09:25:51 +08:00
|
|
|
addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the parent loop for all blocks within the loop. Blocks within
|
|
|
|
// subloops will not change parents.
|
|
|
|
UnloopUpdater Updater(Unloop, this);
|
|
|
|
Updater.updateBlockParents();
|
|
|
|
|
2011-08-12 04:27:32 +08:00
|
|
|
// Remove blocks from former ancestor loops.
|
|
|
|
Updater.removeBlocksFromAncestors();
|
2011-08-11 07:22:57 +08:00
|
|
|
|
|
|
|
// Add direct subloops as children in their new parent loop.
|
|
|
|
Updater.updateSubloopParents();
|
|
|
|
|
|
|
|
// Remove unloop from its parent loop.
|
|
|
|
Loop *ParentLoop = Unloop->getParentLoop();
|
2011-08-12 22:54:45 +08:00
|
|
|
for (Loop::iterator I = ParentLoop->begin();; ++I) {
|
|
|
|
assert(I != ParentLoop->end() && "Couldn't find loop");
|
2011-08-11 07:22:57 +08:00
|
|
|
if (*I == Unloop) {
|
|
|
|
ParentLoop->removeChildLoop(I);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:53:26 +08:00
|
|
|
AnalysisKey LoopAnalysis::Key;
|
2016-02-29 01:17:00 +08:00
|
|
|
|
2016-08-09 08:28:15 +08:00
|
|
|
LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
|
2015-01-20 18:58:50 +08:00
|
|
|
// FIXME: Currently we create a LoopInfo from scratch for every function.
|
|
|
|
// This may prove to be too wasteful due to deallocating and re-allocating
|
|
|
|
// memory each time for the underlying map and vector datastructures. At some
|
|
|
|
// point it may prove worthwhile to use a freelist and recycle LoopInfo
|
|
|
|
// objects. I don't want to add that kind of complexity until the scope of
|
|
|
|
// the problem is better understood.
|
|
|
|
LoopInfo LI;
|
2016-03-11 19:05:24 +08:00
|
|
|
LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
|
2015-05-01 07:07:00 +08:00
|
|
|
return LI;
|
2015-01-20 18:58:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses LoopPrinterPass::run(Function &F,
|
2016-08-09 08:28:15 +08:00
|
|
|
FunctionAnalysisManager &AM) {
|
2016-03-11 19:05:24 +08:00
|
|
|
AM.getResult<LoopAnalysis>(F).print(OS);
|
2015-01-20 18:58:50 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:23:21 +08:00
|
|
|
void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
|
2015-11-05 06:24:08 +08:00
|
|
|
OS << Banner;
|
|
|
|
for (auto *Block : L.blocks())
|
|
|
|
if (Block)
|
|
|
|
Block->print(OS);
|
|
|
|
else
|
|
|
|
OS << "Printing <null> block";
|
|
|
|
}
|
|
|
|
|
2015-01-17 22:16:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LoopInfo implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
char LoopInfoWrapperPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
|
|
|
|
true, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
|
|
|
|
true, true)
|
|
|
|
|
|
|
|
bool LoopInfoWrapperPass::runOnFunction(Function &) {
|
|
|
|
releaseMemory();
|
2015-07-17 02:23:57 +08:00
|
|
|
LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
|
2015-01-17 22:16:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfoWrapperPass::verifyAnalysis() const {
|
|
|
|
// LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
|
|
|
|
// function each time verifyAnalysis is called is very expensive. The
|
2009-09-28 08:27:48 +08:00
|
|
|
// -verify-loop-info option can enable this. In order to perform some
|
2015-01-17 22:16:18 +08:00
|
|
|
// checking by default, LoopPass has been taught to call verifyLoop manually
|
|
|
|
// during loop pass sequences.
|
2016-09-01 03:26:19 +08:00
|
|
|
if (VerifyLoopInfo) {
|
2017-01-15 18:23:18 +08:00
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
LI.verify(DT);
|
2016-09-01 03:26:19 +08:00
|
|
|
}
|
2009-09-08 23:45:00 +08:00
|
|
|
}
|
|
|
|
|
2015-01-17 22:16:18 +08:00
|
|
|
void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-04-27 14:56:12 +08:00
|
|
|
AU.setPreservesAll();
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2002-01-31 08:42:27 +08:00
|
|
|
}
|
2009-08-23 13:17:37 +08:00
|
|
|
|
2015-01-17 22:16:18 +08:00
|
|
|
void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
|
2015-01-18 09:25:51 +08:00
|
|
|
LI.print(OS);
|
2009-08-23 13:17:37 +08:00
|
|
|
}
|
|
|
|
|
2016-07-20 07:54:23 +08:00
|
|
|
PreservedAnalyses LoopVerifierPass::run(Function &F,
|
2016-08-09 08:28:15 +08:00
|
|
|
FunctionAnalysisManager &AM) {
|
2016-07-20 07:54:23 +08:00
|
|
|
LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
|
2016-09-01 03:26:19 +08:00
|
|
|
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
LI.verify(DT);
|
2016-07-20 07:54:23 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2011-08-10 09:59:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LoopBlocksDFS implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Traverse the loop blocks and store the DFS result.
|
|
|
|
/// Useful for clients that just want the final DFS result and don't need to
|
|
|
|
/// visit blocks during the initial traversal.
|
|
|
|
void LoopBlocksDFS::perform(LoopInfo *LI) {
|
|
|
|
LoopBlocksTraversal Traversal(*this, LI);
|
|
|
|
for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
|
2017-09-20 09:12:09 +08:00
|
|
|
POE = Traversal.end();
|
|
|
|
POI != POE; ++POI)
|
|
|
|
;
|
2011-08-10 09:59:05 +08:00
|
|
|
}
|