#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
-#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/GraphTraits.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/Analysis/Dominators.h"
+#include "llvm/IR/CFG.h"
+#include "llvm/IR/Instruction.h"
#include "llvm/Pass.h"
-#include "llvm/Support/CFG.h"
-#include "llvm/Support/raw_ostream.h"
#include <algorithm>
namespace llvm {
class DominatorTree;
class LoopInfo;
class Loop;
+class MDNode;
class PHINode;
+class raw_ostream;
+template<class N> class DominatorTreeBase;
template<class N, class M> class LoopInfoBase;
template<class N, class M> class LoopBase;
// Blocks - The list of blocks in this loop. First entry is the header node.
std::vector<BlockT*> Blocks;
+ SmallPtrSet<const BlockT*, 8> DenseBlockSet;
+
LoopBase(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
const LoopBase<BlockT, LoopT>&
operator=(const LoopBase<BlockT, LoopT> &) LLVM_DELETED_FUNCTION;
public:
/// Loop ctor - This creates an empty loop.
- LoopBase() : ParentLoop(0) {}
+ LoopBase() : ParentLoop(nullptr) {}
~LoopBase() {
for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
delete SubLoops[i];
///
bool contains(const LoopT *L) const {
if (L == this) return true;
- if (L == 0) return false;
+ if (!L) return false;
return contains(L->getParentLoop());
}
/// contains - Return true if the specified basic block is in this loop.
///
bool contains(const BlockT *BB) const {
- return std::find(block_begin(), block_end(), BB) != block_end();
+ return DenseBlockSet.count(BB);
}
/// contains - Return true if the specified instruction is in this loop.
/// getBlocks - Get a list of the basic blocks which make up this loop.
///
const std::vector<BlockT*> &getBlocks() const { return Blocks; }
- std::vector<BlockT*> &getBlocksVector() { 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(); }
/// block that is outside of the current loop.
///
bool isLoopExiting(const BlockT *BB) const {
- typedef GraphTraits<BlockT*> BlockTraits;
+ typedef GraphTraits<const BlockT*> BlockTraits;
for (typename BlockTraits::ChildIteratorType SI =
- BlockTraits::child_begin(const_cast<BlockT*>(BB)),
- SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
+ BlockTraits::child_begin(BB),
+ SE = BlockTraits::child_end(BB); SI != SE; ++SI) {
if (!contains(*SI))
return true;
}
typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
for (typename InvBlockTraits::ChildIteratorType I =
- InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
- E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
+ InvBlockTraits::child_begin(H),
+ E = InvBlockTraits::child_end(H); I != E; ++I)
if (contains(*I))
++NumBackEdges;
/// A latch block is a block that contains a branch back to the header.
BlockT *getLoopLatch() const;
+ /// getLoopLatches - Return all loop latch blocks of this loop. A latch block
+ /// is a block that contains a branch back to the header.
+ void getLoopLatches(SmallVectorImpl<BlockT *> &LoopLatches) const {
+ BlockT *H = getHeader();
+ typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
+ for (typename InvBlockTraits::ChildIteratorType I =
+ InvBlockTraits::child_begin(H),
+ E = InvBlockTraits::child_end(H); I != E; ++I)
+ if (contains(*I))
+ LoopLatches.push_back(*I);
+ }
+
//===--------------------------------------------------------------------===//
// APIs for updating loop information after changing the CFG
//
/// updates the loop depth of the new child.
///
void addChildLoop(LoopT *NewChild) {
- assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
+ assert(!NewChild->ParentLoop && "NewChild already has a parent!");
NewChild->ParentLoop = static_cast<LoopT *>(this);
SubLoops.push_back(NewChild);
}
LoopT *Child = *I;
assert(Child->ParentLoop == this && "Child is not a child of this loop!");
SubLoops.erase(SubLoops.begin()+(I-begin()));
- Child->ParentLoop = 0;
+ Child->ParentLoop = nullptr;
return Child;
}
/// transformations should use addBasicBlockToLoop.
void addBlockEntry(BlockT *BB) {
Blocks.push_back(BB);
+ DenseBlockSet.insert(BB);
+ }
+
+ /// reverseBlocks - interface to reverse Blocks[from, end of loop] in this loop
+ void reverseBlock(unsigned from) {
+ std::reverse(Blocks.begin() + from, Blocks.end());
+ }
+
+ /// reserveBlocks- interface to do reserve() for Blocks
+ void reserveBlocks(unsigned size) {
+ Blocks.reserve(size);
}
/// moveToHeader - This method is used to move BB (which must be part of this
/// the mapping in the LoopInfo class.
void removeBlockFromLoop(BlockT *BB) {
RemoveFromVector(Blocks, BB);
+ DenseBlockSet.erase(BB);
}
/// verifyLoop - Verify loop structure
protected:
friend class LoopInfoBase<BlockT, LoopT>;
- explicit LoopBase(BlockT *BB) : ParentLoop(0) {
+ explicit LoopBase(BlockT *BB) : ParentLoop(nullptr) {
Blocks.push_back(BB);
+ DenseBlockSet.insert(BB);
}
};
/// If null, the terminator of the loop preheader is used.
///
bool makeLoopInvariant(Value *V, bool &Changed,
- Instruction *InsertPt = 0) const;
+ Instruction *InsertPt = nullptr) const;
/// makeLoopInvariant - If the given instruction is inside of the
/// loop and it can be hoisted, do so to make it trivially loop-invariant.
/// If null, the terminator of the loop preheader is used.
///
bool makeLoopInvariant(Instruction *I, bool &Changed,
- Instruction *InsertPt = 0) const;
+ Instruction *InsertPt = nullptr) const;
/// getCanonicalInductionVariable - Check to see if the loop has a canonical
/// induction variable: an integer recurrence that starts at 0 and increments
/// isSafeToClone - Return true if the loop body is safe to clone in practice.
bool isSafeToClone() const;
+ /// Returns true if the loop is annotated parallel.
+ ///
+ /// A parallel loop can be assumed to not contain any dependencies between
+ /// iterations by the compiler. That is, any loop-carried dependency checking
+ /// can be skipped completely when parallelizing the loop on the target
+ /// machine. Thus, if the parallel loop information originates from the
+ /// programmer, e.g. via the OpenMP parallel for pragma, it is the
+ /// programmer's responsibility to ensure there are no loop-carried
+ /// dependencies. The final execution order of the instructions across
+ /// iterations is not guaranteed, thus, the end result might or might not
+ /// implement actual concurrent execution of instructions across multiple
+ /// iterations.
+ bool isAnnotatedParallel() const;
+
+ /// Return the llvm.loop loop id metadata node for this loop if it is present.
+ ///
+ /// If this loop contains the same llvm.loop metadata on each branch to the
+ /// header then the node is returned. If any latch instruction does not
+ /// contain llvm.loop or or if multiple latches contain different nodes then
+ /// 0 is returned.
+ MDNode *getLoopID() const;
+ /// Set the llvm.loop loop id metadata for this loop.
+ ///
+ /// The LoopID metadata node will be added to each terminator instruction in
+ /// the loop that branches to the loop header.
+ ///
+ /// The LoopID metadata node should have one or more operands and the first
+ /// operand should should be the node itself.
+ void setLoopID(MDNode *LoopID) const;
+
/// hasDedicatedExits - Return true if no exit block for the loop
/// has a predecessor that is outside the loop.
bool hasDedicatedExits() const;
LoopT *removeLoop(iterator I) {
assert(I != end() && "Cannot remove end iterator!");
LoopT *L = *I;
- assert(L->getParentLoop() == 0 && "Not a top-level loop!");
+ assert(!L->getParentLoop() && "Not a top-level loop!");
TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
return L;
}
std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
assert(I != TopLevelLoops.end() && "Old loop not at top level!");
*I = NewLoop;
- assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
+ assert(!NewLoop->ParentLoop && !OldLoop->ParentLoop &&
"Loops already embedded into a subloop!");
}
/// addTopLevelLoop - This adds the specified loop to the collection of
/// top-level loops.
void addTopLevelLoop(LoopT *New) {
- assert(New->getParentLoop() == 0 && "Loop already in subloop!");
+ assert(!New->getParentLoop() && "Loop already in subloop!");
TopLevelLoops.push_back(New);
}
static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
const LoopT *ParentLoop) {
- if (SubLoop == 0) return true;
+ if (!SubLoop) return true;
if (SubLoop == ParentLoop) return false;
return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
}
/// runOnFunction - Calculate the natural loop information.
///
- virtual bool runOnFunction(Function &F);
+ bool runOnFunction(Function &F) override;
- virtual void verifyAnalysis() const;
+ void verifyAnalysis() const override;
- virtual void releaseMemory() { LI.releaseMemory(); }
+ void releaseMemory() override { LI.releaseMemory(); }
- virtual void print(raw_ostream &O, const Module* M = 0) const;
+ void print(raw_ostream &O, const Module* M = nullptr) const override;
- virtual void getAnalysisUsage(AnalysisUsage &AU) const;
+ void getAnalysisUsage(AnalysisUsage &AU) const override;
/// removeLoop - This removes the specified top-level loop from this loop info
/// object. The loop is not deleted, as it will presumably be inserted into