Clang-format few files to make later diffs leaner; NFC

llvm-svn: 313705
This commit is contained in:
Sanjoy Das 2017-09-20 01:12:09 +00:00
parent 4ce010e775
commit 66a004ac0c
3 changed files with 185 additions and 190 deletions

View File

@ -29,7 +29,7 @@
// in the CFG. There can be strongly connected components in the CFG which
// this analysis will not recognize and that will not be represented by a Loop
// instance. In particular, a Loop might be inside such a non-loop SCC, or a
// non-loop SCC might contain a sub-SCC which is a Loop.
// non-loop SCC might contain a sub-SCC which is a Loop.
//
//===----------------------------------------------------------------------===//
@ -56,32 +56,31 @@ class Loop;
class MDNode;
class PHINode;
class raw_ostream;
template <class N, bool IsPostDom>
class DominatorTreeBase;
template<class N, class M> class LoopInfoBase;
template<class N, class M> class LoopBase;
template <class N, bool IsPostDom> class DominatorTreeBase;
template <class N, class M> class LoopInfoBase;
template <class N, class M> class LoopBase;
//===----------------------------------------------------------------------===//
/// Instances of this class are used to represent loops that are detected in the
/// flow graph.
///
template<class BlockT, class LoopT>
class LoopBase {
template <class BlockT, class LoopT> class LoopBase {
LoopT *ParentLoop;
// Loops contained entirely within this one.
std::vector<LoopT *> SubLoops;
// The list of blocks in this loop. First entry is the header node.
std::vector<BlockT*> Blocks;
std::vector<BlockT *> Blocks;
SmallPtrSet<const BlockT*, 8> DenseBlockSet;
SmallPtrSet<const BlockT *, 8> DenseBlockSet;
/// Indicator that this loop is no longer a valid loop.
bool IsInvalid = false;
LoopBase(const LoopBase<BlockT, LoopT> &) = delete;
const LoopBase<BlockT, LoopT>&
operator=(const LoopBase<BlockT, LoopT> &) = delete;
const LoopBase<BlockT, LoopT> &
operator=(const LoopBase<BlockT, LoopT> &) = delete;
public:
/// This creates an empty loop.
LoopBase() : ParentLoop(nullptr) {}
@ -104,19 +103,18 @@ public:
/// Return true if the specified loop is contained within in this loop.
bool contains(const LoopT *L) const {
if (L == this) return true;
if (!L) return false;
if (L == this)
return true;
if (!L)
return false;
return contains(L->getParentLoop());
}
/// Return true if the specified basic block is in this loop.
bool contains(const BlockT *BB) const {
return DenseBlockSet.count(BB);
}
bool contains(const BlockT *BB) const { return DenseBlockSet.count(BB); }
/// Return true if the specified instruction is in this loop.
template<class InstT>
bool contains(const InstT *Inst) const {
template <class InstT> bool contains(const InstT *Inst) const {
return contains(Inst->getParent());
}
@ -124,8 +122,8 @@ public:
const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
std::vector<LoopT *> &getSubLoopsVector() { return SubLoops; }
typedef typename std::vector<LoopT *>::const_iterator iterator;
typedef typename std::vector<LoopT *>::const_reverse_iterator
reverse_iterator;
typedef
typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
iterator begin() const { return SubLoops.begin(); }
iterator end() const { return SubLoops.end(); }
reverse_iterator rbegin() const { return SubLoops.rbegin(); }
@ -133,8 +131,8 @@ public:
bool empty() const { return SubLoops.empty(); }
/// Get a list of the basic blocks which make up this loop.
const std::vector<BlockT*> &getBlocks() const { return Blocks; }
typedef typename std::vector<BlockT*>::const_iterator block_iterator;
const std::vector<BlockT *> &getBlocks() const { return Blocks; }
typedef typename std::vector<BlockT *>::const_iterator block_iterator;
block_iterator block_begin() const { return Blocks.begin(); }
block_iterator block_end() const { return Blocks.end(); }
inline iterator_range<block_iterator> blocks() const {
@ -142,9 +140,7 @@ public:
}
/// Get the number of blocks in this loop in constant time.
unsigned getNumBlocks() const {
return Blocks.size();
}
unsigned getNumBlocks() const { return Blocks.size(); }
/// Invalidate the loop, indicating that it is no longer a loop.
void invalidate() { IsInvalid = true; }
@ -155,7 +151,7 @@ public:
/// True if terminator in the block can branch to another block that is
/// outside of the current loop.
bool isLoopExiting(const BlockT *BB) const {
for (const auto &Succ : children<const BlockT*>(BB)) {
for (const auto &Succ : children<const BlockT *>(BB)) {
if (!contains(Succ))
return true;
}
@ -170,8 +166,8 @@ public:
assert(contains(BB) && "block does not belong to the loop");
BlockT *Header = getHeader();
auto PredBegin = GraphTraits<Inverse<BlockT*> >::child_begin(Header);
auto PredEnd = GraphTraits<Inverse<BlockT*> >::child_end(Header);
auto PredBegin = GraphTraits<Inverse<BlockT *>>::child_begin(Header);
auto PredEnd = GraphTraits<Inverse<BlockT *>>::child_end(Header);
return std::find(PredBegin, PredEnd, BB) != PredEnd;
}
@ -180,7 +176,7 @@ public:
unsigned NumBackEdges = 0;
BlockT *H = getHeader();
for (const auto Pred : children<Inverse<BlockT*> >(H))
for (const auto Pred : children<Inverse<BlockT *>>(H))
if (contains(Pred))
++NumBackEdges;
@ -206,14 +202,14 @@ public:
/// Return all of the successor blocks of this loop. These are the blocks
/// _outside of the current loop_ which are branched to.
void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const;
void getExitBlocks(SmallVectorImpl<BlockT *> &ExitBlocks) const;
/// If getExitBlocks would return exactly one block, return that block.
/// Otherwise return null.
BlockT *getExitBlock() const;
/// Edge type.
typedef std::pair<const BlockT*, const BlockT*> Edge;
typedef std::pair<const BlockT *, const BlockT *> Edge;
/// Return all pairs of (_inside_block_,_outside_block_).
void getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const;
@ -240,7 +236,7 @@ public:
/// contains a branch back to the header.
void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
BlockT *H = getHeader();
for (const auto Pred : children<Inverse<BlockT*>>(H))
for (const auto Pred : children<Inverse<BlockT *>>(H))
if (contains(Pred))
LoopLatches.push_back(Pred);
}
@ -276,7 +272,7 @@ public:
assert(I != SubLoops.end() && "Cannot remove end iterator!");
LoopT *Child = *I;
assert(Child->ParentLoop == this && "Child is not a child of this loop!");
SubLoops.erase(SubLoops.begin()+(I-begin()));
SubLoops.erase(SubLoops.begin() + (I - begin()));
Child->ParentLoop = nullptr;
return Child;
}
@ -295,15 +291,14 @@ public:
}
/// interface to do reserve() for Blocks
void reserveBlocks(unsigned size) {
Blocks.reserve(size);
}
void reserveBlocks(unsigned size) { Blocks.reserve(size); }
/// This method is used to move BB (which must be part of this loop) to be the
/// loop header of the loop (the block that dominates all others).
void moveToHeader(BlockT *BB) {
if (Blocks[0] == BB) return;
for (unsigned i = 0; ; ++i) {
if (Blocks[0] == BB)
return;
for (unsigned i = 0;; ++i) {
assert(i != Blocks.size() && "Loop does not contain BB!");
if (Blocks[i] == BB) {
Blocks[i] = Blocks[0];
@ -328,7 +323,7 @@ public:
void verifyLoop() const;
/// Verify loop structure of this loop and all nested loops.
void verifyLoopNest(DenseSet<const LoopT*> *Loops) const;
void verifyLoopNest(DenseSet<const LoopT *> *Loops) const;
/// Print loop with all the BBs inside it.
void print(raw_ostream &OS, unsigned Depth = 0, bool Verbose = false) const;
@ -355,8 +350,8 @@ protected:
}
};
template<class BlockT, class LoopT>
raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
template <class BlockT, class LoopT>
raw_ostream &operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
Loop.print(OS);
return OS;
}
@ -364,7 +359,6 @@ raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
// Implementation in LoopInfoImpl.h
extern template class LoopBase<BasicBlock, Loop>;
/// Represents a single loop in the control flow graph. Note that not all SCCs
/// in the CFG are necessarily loops.
class Loop : public LoopBase<BasicBlock, Loop> {
@ -377,17 +371,15 @@ public:
public:
LocRange() {}
LocRange(DebugLoc Start) : Start(std::move(Start)), End(std::move(Start)) {}
LocRange(DebugLoc Start, DebugLoc End) : Start(std::move(Start)),
End(std::move(End)) {}
LocRange(DebugLoc Start, DebugLoc End)
: Start(std::move(Start)), End(std::move(End)) {}
const DebugLoc &getStart() const { return Start; }
const DebugLoc &getEnd() const { return End; }
/// \brief Check for null.
///
explicit operator bool() const {
return Start && End;
}
explicit operator bool() const { return Start && End; }
};
Loop() {}
@ -520,8 +512,7 @@ private:
/// structures in the specified function.
///
template<class BlockT, class LoopT>
class LoopInfoBase {
template <class BlockT, class LoopT> class LoopInfoBase {
// BBMap - Mapping of basic blocks to the inner most loop they occur in
DenseMap<const BlockT *, LoopT *> BBMap;
std::vector<LoopT *> TopLevelLoops;
@ -532,8 +523,9 @@ class LoopInfoBase {
void operator=(const LoopInfoBase &) = delete;
LoopInfoBase(const LoopInfoBase &) = delete;
public:
LoopInfoBase() { }
LoopInfoBase() {}
~LoopInfoBase() { releaseMemory(); }
LoopInfoBase(LoopInfoBase &&Arg)
@ -567,8 +559,8 @@ public:
/// function.
///
typedef typename std::vector<LoopT *>::const_iterator iterator;
typedef typename std::vector<LoopT *>::const_reverse_iterator
reverse_iterator;
typedef
typename std::vector<LoopT *>::const_reverse_iterator reverse_iterator;
iterator begin() const { return TopLevelLoops.begin(); }
iterator end() const { return TopLevelLoops.end(); }
reverse_iterator rbegin() const { return TopLevelLoops.rbegin(); }
@ -597,9 +589,7 @@ public:
LoopT *getLoopFor(const BlockT *BB) const { return BBMap.lookup(BB); }
/// Same as getLoopFor.
const LoopT *operator[](const BlockT *BB) const {
return getLoopFor(BB);
}
const LoopT *operator[](const BlockT *BB) const { return getLoopFor(BB); }
/// Return the loop nesting level of the specified block. A depth of 0 means
/// the block is not inside any loop.
@ -621,7 +611,7 @@ public:
assert(I != end() && "Cannot remove end iterator!");
LoopT *L = *I;
assert(!L->getParentLoop() && "Not a top-level loop!");
TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
TopLevelLoops.erase(TopLevelLoops.begin() + (I - begin()));
return L;
}
@ -638,8 +628,7 @@ public:
/// Replace the specified loop in the top-level loops list with the indicated
/// loop.
void changeTopLevelLoop(LoopT *OldLoop,
LoopT *NewLoop) {
void changeTopLevelLoop(LoopT *OldLoop, LoopT *NewLoop) {
auto I = find(TopLevelLoops, OldLoop);
assert(I != TopLevelLoops.end() && "Old loop not at top level!");
*I = NewLoop;
@ -670,8 +659,10 @@ public:
static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
const LoopT *ParentLoop) {
if (!SubLoop) return true;
if (SubLoop == ParentLoop) return false;
if (!SubLoop)
return true;
if (SubLoop == ParentLoop)
return false;
return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
}
@ -694,6 +685,7 @@ class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
void operator=(const LoopInfo &) = delete;
LoopInfo(const LoopInfo &) = delete;
public:
LoopInfo() {}
explicit LoopInfo(const DominatorTreeBase<BasicBlock, false> &DomTree);
@ -722,7 +714,8 @@ public:
// Preserving LCSSA form is only problematic if the replacing value is an
// instruction.
Instruction *I = dyn_cast<Instruction>(To);
if (!I) return true;
if (!I)
return true;
// If both instructions are defined in the same basic block then replacement
// cannot break LCSSA form.
if (I->getParent() == From->getParent())
@ -730,7 +723,8 @@ public:
// If the instruction is not defined in a loop then it can safely replace
// anything.
Loop *ToLoop = getLoopFor(I->getParent());
if (!ToLoop) return true;
if (!ToLoop)
return true;
// If the replacing instruction is defined in the same loop as the original
// instruction, or in a loop that contains it as an inner loop, then using
// it as a replacement will not break LCSSA form.
@ -810,7 +804,7 @@ public:
};
// Allow clients to walk the list of nested loops...
template <> struct GraphTraits<const Loop*> {
template <> struct GraphTraits<const Loop *> {
typedef const Loop *NodeRef;
typedef LoopInfo::iterator ChildIteratorType;
@ -819,7 +813,7 @@ template <> struct GraphTraits<const Loop*> {
static ChildIteratorType child_end(NodeRef N) { return N->end(); }
};
template <> struct GraphTraits<Loop*> {
template <> struct GraphTraits<Loop *> {
typedef Loop *NodeRef;
typedef LoopInfo::iterator ChildIteratorType;

View File

@ -31,11 +31,11 @@ namespace llvm {
/// outside of the loop. These are the blocks _inside of the current loop_
/// which branch out. The returned list is always unique.
///
template<class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::
getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::getExitingBlocks(
SmallVectorImpl<BlockT *> &ExitingBlocks) const {
for (const auto BB : blocks())
for (const auto &Succ : children<BlockT*>(BB))
for (const auto &Succ : children<BlockT *>(BB))
if (!contains(Succ)) {
// Not in current loop? It must be an exit block.
ExitingBlocks.push_back(BB);
@ -45,9 +45,9 @@ getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
/// getExitingBlock - If getExitingBlocks would return exactly one block,
/// return that block. Otherwise return null.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
SmallVector<BlockT*, 8> ExitingBlocks;
SmallVector<BlockT *, 8> ExitingBlocks;
getExitingBlocks(ExitingBlocks);
if (ExitingBlocks.size() == 1)
return ExitingBlocks[0];
@ -57,11 +57,11 @@ BlockT *LoopBase<BlockT, LoopT>::getExitingBlock() const {
/// getExitBlocks - Return all of the successor blocks of this loop. These
/// are the blocks _outside of the current loop_ which are branched to.
///
template<class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::
getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::getExitBlocks(
SmallVectorImpl<BlockT *> &ExitBlocks) const {
for (const auto BB : blocks())
for (const auto &Succ : children<BlockT*>(BB))
for (const auto &Succ : children<BlockT *>(BB))
if (!contains(Succ))
// Not in current loop? It must be an exit block.
ExitBlocks.push_back(Succ);
@ -69,9 +69,9 @@ getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
/// getExitBlock - If getExitBlocks would return exactly one block,
/// return that block. Otherwise return null.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
SmallVector<BlockT*, 8> ExitBlocks;
SmallVector<BlockT *, 8> ExitBlocks;
getExitBlocks(ExitBlocks);
if (ExitBlocks.size() == 1)
return ExitBlocks[0];
@ -79,11 +79,11 @@ BlockT *LoopBase<BlockT, LoopT>::getExitBlock() const {
}
/// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
template<class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::
getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::getExitEdges(
SmallVectorImpl<Edge> &ExitEdges) const {
for (const auto BB : blocks())
for (const auto &Succ : children<BlockT*>(BB))
for (const auto &Succ : children<BlockT *>(BB))
if (!contains(Succ))
// Not in current loop? It must be an exit block.
ExitEdges.emplace_back(BB, Succ);
@ -97,22 +97,23 @@ getExitEdges(SmallVectorImpl<Edge> &ExitEdges) const {
///
/// This method returns null if there is no preheader for the loop.
///
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
// Keep track of nodes outside the loop branching to the header...
BlockT *Out = getLoopPredecessor();
if (!Out) return nullptr;
if (!Out)
return nullptr;
// Make sure we are allowed to hoist instructions into the predecessor.
if (!Out->isLegalToHoistInto())
return nullptr;
// Make sure there is only one exit out of the preheader.
typedef GraphTraits<BlockT*> BlockTraits;
typedef GraphTraits<BlockT *> BlockTraits;
typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
++SI;
if (SI != BlockTraits::child_end(Out))
return nullptr; // Multiple exits from the block, must not be a preheader.
return nullptr; // Multiple exits from the block, must not be a preheader.
// The predecessor has exactly one successor, so it is a preheader.
return Out;
@ -123,17 +124,17 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
/// This is less strict that the loop "preheader" concept, which requires
/// the predecessor to have exactly one successor.
///
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
// Keep track of nodes outside the loop branching to the header...
BlockT *Out = nullptr;
// Loop over the predecessors of the header node...
BlockT *Header = getHeader();
for (const auto Pred : children<Inverse<BlockT*>>(Header)) {
if (!contains(Pred)) { // If the block is not in the loop...
for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
if (!contains(Pred)) { // If the block is not in the loop...
if (Out && Out != Pred)
return nullptr; // Multiple predecessors outside the loop
return nullptr; // Multiple predecessors outside the loop
Out = Pred;
}
}
@ -145,13 +146,14 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopPredecessor() const {
/// getLoopLatch - If there is a single latch block for this loop, return it.
/// A latch block is a block that contains a branch back to the header.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
BlockT *Header = getHeader();
BlockT *Latch = nullptr;
for (const auto Pred : children<Inverse<BlockT*>>(Header)) {
for (const auto Pred : children<Inverse<BlockT *>>(Header)) {
if (contains(Pred)) {
if (Latch) return nullptr;
if (Latch)
return nullptr;
Latch = Pred;
}
}
@ -169,14 +171,14 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopLatch() const {
/// to the specified LoopInfo object as being in the current basic block. It
/// is not valid to replace the loop header with this method.
///
template<class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::
addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::addBasicBlockToLoop(
BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
#ifndef NDEBUG
if (!Blocks.empty()) {
auto SameHeader = LIB[getHeader()];
assert(contains(SameHeader) && getHeader() == SameHeader->getHeader()
&& "Incorrect LI specified for this loop!");
assert(contains(SameHeader) && getHeader() == SameHeader->getHeader() &&
"Incorrect LI specified for this loop!");
}
#endif
assert(NewBB && "Cannot add a null basic block to the loop!");
@ -198,9 +200,9 @@ addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LIB) {
/// the OldChild entry in our children list with NewChild, and updates the
/// parent pointer of OldChild to be null and the NewChild to be this loop.
/// This updates the loop depth of the new child.
template<class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::
replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) {
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::replaceChildLoopWith(LoopT *OldChild,
LoopT *NewChild) {
assert(OldChild->ParentLoop == this && "This loop is already broken!");
assert(!NewChild->ParentLoop && "NewChild already has a parent!");
typename std::vector<LoopT *>::iterator I = find(SubLoops, OldChild);
@ -211,46 +213,47 @@ replaceChildLoopWith(LoopT *OldChild, LoopT *NewChild) {
}
/// verifyLoop - Verify loop structure
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::verifyLoop() const {
#ifndef NDEBUG
assert(!Blocks.empty() && "Loop header is missing");
// Setup for using a depth-first iterator to visit every block in the loop.
SmallVector<BlockT*, 8> ExitBBs;
SmallVector<BlockT *, 8> ExitBBs;
getExitBlocks(ExitBBs);
df_iterator_default_set<BlockT*> VisitSet;
df_iterator_default_set<BlockT *> VisitSet;
VisitSet.insert(ExitBBs.begin(), ExitBBs.end());
df_ext_iterator<BlockT*, df_iterator_default_set<BlockT*>>
BI = df_ext_begin(getHeader(), VisitSet),
BE = df_ext_end(getHeader(), VisitSet);
df_ext_iterator<BlockT *, df_iterator_default_set<BlockT *>>
BI = df_ext_begin(getHeader(), VisitSet),
BE = df_ext_end(getHeader(), VisitSet);
// Keep track of the BBs visited.
SmallPtrSet<BlockT*, 8> VisitedBBs;
SmallPtrSet<BlockT *, 8> VisitedBBs;
// Check the individual blocks.
for ( ; BI != BE; ++BI) {
for (; BI != BE; ++BI) {
BlockT *BB = *BI;
assert(std::any_of(GraphTraits<BlockT*>::child_begin(BB),
GraphTraits<BlockT*>::child_end(BB),
[&](BlockT *B){return contains(B);}) &&
assert(std::any_of(GraphTraits<BlockT *>::child_begin(BB),
GraphTraits<BlockT *>::child_end(BB),
[&](BlockT *B) { return contains(B); }) &&
"Loop block has no in-loop successors!");
assert(std::any_of(GraphTraits<Inverse<BlockT*> >::child_begin(BB),
GraphTraits<Inverse<BlockT*> >::child_end(BB),
[&](BlockT *B){return contains(B);}) &&
assert(std::any_of(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
GraphTraits<Inverse<BlockT *>>::child_end(BB),
[&](BlockT *B) { return contains(B); }) &&
"Loop block has no in-loop predecessors!");
SmallVector<BlockT *, 2> OutsideLoopPreds;
std::for_each(GraphTraits<Inverse<BlockT*> >::child_begin(BB),
GraphTraits<Inverse<BlockT*> >::child_end(BB),
[&](BlockT *B){if (!contains(B))
std::for_each(GraphTraits<Inverse<BlockT *>>::child_begin(BB),
GraphTraits<Inverse<BlockT *>>::child_end(BB),
[&](BlockT *B) {
if (!contains(B))
OutsideLoopPreds.push_back(B);
});
if (BB == getHeader()) {
assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
} else if (!OutsideLoopPreds.empty()) {
// A non-header loop shouldn't be reachable from outside the loop,
// though it is permitted if the predecessor is not itself actually
@ -282,8 +285,8 @@ void LoopBase<BlockT, LoopT>::verifyLoop() const {
// Each block in each subloop should be contained within this loop.
for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
BI != BE; ++BI) {
assert(contains(*BI) &&
"Loop does not contain all the blocks of a subloop!");
assert(contains(*BI) &&
"Loop does not contain all the blocks of a subloop!");
}
// Check the parent loop pointer.
@ -295,9 +298,9 @@ void LoopBase<BlockT, LoopT>::verifyLoop() const {
}
/// verifyLoop - Verify loop structure of this loop and all nested loops.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::verifyLoopNest(
DenseSet<const LoopT*> *Loops) const {
DenseSet<const LoopT *> *Loops) const {
Loops->insert(static_cast<const LoopT *>(this));
// Verify this loop.
verifyLoop();
@ -306,30 +309,34 @@ void LoopBase<BlockT, LoopT>::verifyLoopNest(
(*I)->verifyLoopNest(Loops);
}
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth,
bool Verbose) const {
OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
<< " containing: ";
OS.indent(Depth * 2) << "Loop at depth " << getLoopDepth() << " containing: ";
BlockT *H = getHeader();
for (unsigned i = 0; i < getBlocks().size(); ++i) {
BlockT *BB = getBlocks()[i];
if (!Verbose) {
if (i) OS << ",";
if (i)
OS << ",";
BB->printAsOperand(OS, false);
} else OS << "\n";
} else
OS << "\n";
if (BB == H) OS << "<header>";
if (isLoopLatch(BB)) OS << "<latch>";
if (isLoopExiting(BB)) OS << "<exiting>";
if (BB == H)
OS << "<header>";
if (isLoopLatch(BB))
OS << "<latch>";
if (isLoopExiting(BB))
OS << "<exiting>";
if (Verbose)
BB->print(OS);
}
OS << "\n";
for (iterator I = begin(), E = end(); I != E; ++I)
(*I)->print(OS, Depth+2);
(*I)->print(OS, Depth + 2);
}
//===----------------------------------------------------------------------===//
@ -341,10 +348,10 @@ void LoopBase<BlockT, LoopT>::print(raw_ostream &OS, unsigned Depth,
/// this loop are mapped to this loop or a subloop. And all subloops within this
/// loop have their parent loop set to this loop or a subloop.
template <class BlockT, class LoopT>
static void discoverAndMapSubloop(
LoopT *L, ArrayRef<BlockT *> Backedges, LoopInfoBase<BlockT, LoopT> *LI,
const DomTreeBase<BlockT> &DomTree) {
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
static void discoverAndMapSubloop(LoopT *L, ArrayRef<BlockT *> Backedges,
LoopInfoBase<BlockT, LoopT> *LI,
const DomTreeBase<BlockT> &DomTree) {
typedef GraphTraits<Inverse<BlockT *>> InvBlockTraits;
unsigned NumBlocks = 0;
unsigned NumSubloops = 0;
@ -364,13 +371,12 @@ static void discoverAndMapSubloop(
LI->changeLoopFor(PredBB, L);
++NumBlocks;
if (PredBB == L->getHeader())
continue;
continue;
// Push all block predecessors on the worklist.
ReverseCFGWorklist.insert(ReverseCFGWorklist.end(),
InvBlockTraits::child_begin(PredBB),
InvBlockTraits::child_end(PredBB));
}
else {
} else {
// This is a discovered block. Find its outermost discovered loop.
while (LoopT *Parent = Subloop->getParentLoop())
Subloop = Parent;
@ -388,7 +394,7 @@ static void discoverAndMapSubloop(
// within this subloop tree itself. Note that a predecessor may directly
// reach another subloop that is not yet discovered to be a subloop of
// this loop, which we must traverse.
for (const auto Pred : children<Inverse<BlockT*>>(PredBB)) {
for (const auto Pred : children<Inverse<BlockT *>>(PredBB)) {
if (LI->getLoopFor(Pred) != Subloop)
ReverseCFGWorklist.push_back(Pred);
}
@ -399,15 +405,14 @@ static void discoverAndMapSubloop(
}
/// Populate all loop data in a stable order during a single forward DFS.
template<class BlockT, class LoopT>
class PopulateLoopsDFS {
typedef GraphTraits<BlockT*> BlockTraits;
template <class BlockT, class LoopT> class PopulateLoopsDFS {
typedef GraphTraits<BlockT *> BlockTraits;
typedef typename BlockTraits::ChildIteratorType SuccIterTy;
LoopInfoBase<BlockT, LoopT> *LI;
public:
PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li):
LI(li) {}
PopulateLoopsDFS(LoopInfoBase<BlockT, LoopT> *li) : LI(li) {}
void traverse(BlockT *EntryBlock);
@ -416,7 +421,7 @@ protected:
};
/// Top-level driver for the forward DFS within the loop.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
for (BlockT *BB : post_order(EntryBlock))
insertIntoLoop(BB);
@ -425,7 +430,7 @@ void PopulateLoopsDFS<BlockT, LoopT>::traverse(BlockT *EntryBlock) {
/// Add a single Block to its ancestor loops in PostOrder. If the block is a
/// subloop header, add the subloop to its parent in PostOrder, then reverse the
/// Block and Subloop vectors of the now complete subloop to achieve RPO.
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
LoopT *Subloop = LI->getLoopFor(Block);
if (Subloop && Block == Subloop->getHeader()) {
@ -463,8 +468,7 @@ void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
/// The Block vectors are inclusive, so step 3 requires loop-depth number of
/// insertions per block.
template <class BlockT, class LoopT>
void LoopInfoBase<BlockT, LoopT>::analyze(
const DomTreeBase<BlockT> &DomTree) {
void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
// Postorder traversal of the dominator tree.
const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
for (auto DomNode : post_order(DomRoot)) {
@ -473,17 +477,17 @@ void LoopInfoBase<BlockT, LoopT>::analyze(
SmallVector<BlockT *, 4> Backedges;
// Check each predecessor of the potential loop header.
for (const auto Backedge : children<Inverse<BlockT*>>(Header)) {
for (const auto Backedge : children<Inverse<BlockT *>>(Header)) {
// If Header dominates predBB, this is a new loop. Collect the backedges.
if (DomTree.dominates(Header, Backedge)
&& DomTree.isReachableFromEntry(Backedge)) {
if (DomTree.dominates(Header, Backedge) &&
DomTree.isReachableFromEntry(Backedge)) {
Backedges.push_back(Backedge);
}
}
// Perform a backward CFG traversal to discover and map blocks in this loop.
if (!Backedges.empty()) {
LoopT *L = new LoopT(Header);
discoverAndMapSubloop(L, ArrayRef<BlockT*>(Backedges), this, DomTree);
discoverAndMapSubloop(L, ArrayRef<BlockT *>(Backedges), this, DomTree);
}
}
// Perform a single forward CFG traversal to populate block and subloop
@ -542,7 +546,7 @@ LoopInfoBase<BlockT, LoopT>::getLoopsInReverseSiblingPreorder() {
}
// Debugging
template<class BlockT, class LoopT>
template <class BlockT, class LoopT>
void LoopInfoBase<BlockT, LoopT>::print(raw_ostream &OS) const {
for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
TopLevelLoops[i]->print(OS);
@ -607,13 +611,13 @@ static void compareLoops(const LoopT *L, const LoopT *OtherL,
template <class BlockT, class LoopT>
void LoopInfoBase<BlockT, LoopT>::verify(
const DomTreeBase<BlockT> &DomTree) const {
DenseSet<const LoopT*> Loops;
DenseSet<const LoopT *> Loops;
for (iterator I = begin(), E = end(); I != E; ++I) {
assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
(*I)->verifyLoopNest(&Loops);
}
// Verify that blocks are mapped to valid loops.
// Verify that blocks are mapped to valid loops.
#ifndef NDEBUG
for (auto &Entry : BBMap) {
const BlockT *BB = Entry.first;

View File

@ -44,9 +44,9 @@ bool llvm::VerifyLoopInfo = true;
#else
bool llvm::VerifyLoopInfo = false;
#endif
static cl::opt<bool,true>
VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
cl::desc("Verify loop info (time consuming)"));
static cl::opt<bool, true>
VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
cl::desc("Verify loop info (time consuming)"));
//===----------------------------------------------------------------------===//
// Loop implementation
@ -55,7 +55,7 @@ VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
bool Loop::isLoopInvariant(const Value *V) const {
if (const Instruction *I = dyn_cast<Instruction>(V))
return !contains(I);
return true; // All non-instructions are loop invariant
return true; // All non-instructions are loop invariant
}
bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
@ -66,7 +66,7 @@ bool Loop::makeLoopInvariant(Value *V, bool &Changed,
Instruction *InsertPt) const {
if (Instruction *I = dyn_cast<Instruction>(V))
return makeLoopInvariant(I, Changed, InsertPt);
return true; // All non-instructions are loop-invariant.
return true; // All non-instructions are loop-invariant.
}
bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
@ -112,12 +112,13 @@ PHINode *Loop::getCanonicalInductionVariable() const {
BasicBlock *Incoming = nullptr, *Backedge = nullptr;
pred_iterator PI = pred_begin(H);
assert(PI != pred_end(H) &&
"Loop must have at least one backedge!");
assert(PI != pred_end(H) && "Loop must have at least one backedge!");
Backedge = *PI++;
if (PI == pred_end(H)) return nullptr; // dead loop
if (PI == pred_end(H))
return nullptr; // dead loop
Incoming = *PI++;
if (PI != pred_end(H)) return nullptr; // multiple backedges?
if (PI != pred_end(H))
return nullptr; // multiple backedges?
if (contains(Incoming)) {
if (contains(Backedge))
@ -130,12 +131,11 @@ PHINode *Loop::getCanonicalInductionVariable() const {
for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I);
if (ConstantInt *CI =
dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
if (CI->isZero())
if (Instruction *Inc =
dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
if (Inc->getOpcode() == Instruction::Add &&
Inc->getOperand(0) == PN)
dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
if (CI->isOne())
return PN;
@ -255,7 +255,8 @@ void Loop::setLoopID(MDNode *LoopID) const {
return;
}
assert(!getLoopLatch() && "The loop should have no single latch at this point");
assert(!getLoopLatch() &&
"The loop should have no single latch at this point");
BasicBlock *H = getHeader();
for (BasicBlock *BB : this->blocks()) {
TerminatorInst *TI = BB->getTerminator();
@ -270,7 +271,7 @@ bool Loop::isAnnotatedParallel() const {
MDNode *DesiredLoopIdMetadata = getLoopID();
if (!DesiredLoopIdMetadata)
return false;
return false;
// The loop branch contains the parallel loop metadata. In order to ensure
// that any parallel-loop-unaware optimization pass hasn't added loop-carried
@ -307,9 +308,7 @@ bool Loop::isAnnotatedParallel() const {
return true;
}
DebugLoc Loop::getStartLoc() const {
return getLocRange().getStart();
}
DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
Loop::LocRange Loop::getLocRange() const {
// If we have a debug location in the loop ID, then use it.
@ -357,8 +356,8 @@ bool Loop::hasDedicatedExits() const {
return true;
}
void
Loop::getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
void Loop::getUniqueExitBlocks(
SmallVectorImpl<BasicBlock *> &ExitBlocks) const {
assert(hasDedicatedExits() &&
"getUniqueExitBlocks assumes the loop has canonical form exits!");
@ -408,12 +407,10 @@ BasicBlock *Loop::getUniqueExitBlock() const {
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void Loop::dump() const {
print(dbgs());
}
LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
print(dbgs(), /*Depth=*/ 0, /*Verbose=*/ true);
print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
}
#endif
@ -434,15 +431,15 @@ class UnloopUpdater {
// 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.
DenseMap<Loop*, Loop*> SubloopParents;
DenseMap<Loop *, Loop *> SubloopParents;
// Flag the presence of an irreducible backedge whose destination is a block
// directly contained by the original unloop.
bool FoundIB;
public:
UnloopUpdater(Loop *UL, LoopInfo *LInfo) :
Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
UnloopUpdater(Loop *UL, LoopInfo *LInfo)
: Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
void updateBlockParents();
@ -472,8 +469,7 @@ void UnloopUpdater::updateBlockParents() {
assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
"uninitialized successor");
LI->changeLoopFor(POI, NL);
}
else {
} else {
// Or the current block is part of a subloop, in which case its parent
// is unchanged.
assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
@ -490,7 +486,8 @@ void UnloopUpdater::updateBlockParents() {
// from successors to predecessors as before.
Changed = false;
for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
POE = DFS.endPostorder(); POI != POE; ++POI) {
POE = DFS.endPostorder();
POI != POE; ++POI) {
Loop *L = LI->getLoopFor(*POI);
Loop *NL = getNearestLoop(*POI, L);
@ -508,8 +505,8 @@ void UnloopUpdater::updateBlockParents() {
void UnloopUpdater::removeBlocksFromAncestors() {
// Remove all unloop's blocks (including those in nested subloops) from
// ancestors below the new parent loop.
for (Loop::block_iterator BI = Unloop.block_begin(),
BE = Unloop.block_end(); BI != BE; ++BI) {
for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
BI != BE; ++BI) {
Loop *OuterParent = LI->getLoopFor(*BI);
if (Unloop.contains(OuterParent)) {
while (OuterParent->getParentLoop() != &Unloop)
@ -609,9 +606,7 @@ Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
return NearLoop;
}
LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) {
analyze(DomTree);
}
LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
@ -766,5 +761,7 @@ PreservedAnalyses LoopVerifierPass::run(Function &F,
void LoopBlocksDFS::perform(LoopInfo *LI) {
LoopBlocksTraversal Traversal(*this, LI);
for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
POE = Traversal.end(); POI != POE; ++POI) ;
POE = Traversal.end();
POI != POE; ++POI)
;
}