Mercurial > hg > CbC > CbC_llvm
diff lib/Analysis/LoopInfo.cpp @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children | 3a76565eade5 |
line wrap: on
line diff
--- a/lib/Analysis/LoopInfo.cpp Fri Nov 25 19:14:25 2016 +0900 +++ b/lib/Analysis/LoopInfo.cpp Fri Oct 27 17:07:41 2017 +0900 @@ -16,6 +16,7 @@ #include "llvm/Analysis/LoopInfo.h" #include "llvm/ADT/DepthFirstIterator.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/Analysis/LoopInfoImpl.h" #include "llvm/Analysis/LoopIterator.h" @@ -40,13 +41,13 @@ // Always verify loopinfo if expensive checking is enabled. #ifdef EXPENSIVE_CHECKS -static bool VerifyLoopInfo = true; +bool llvm::VerifyLoopInfo = true; #else -static bool VerifyLoopInfo = false; +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 +56,7 @@ 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 +67,7 @@ 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 +113,13 @@ 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,14 +132,13 @@ for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) { PHINode *PN = cast<PHINode>(I); if (ConstantInt *CI = - dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming))) - if (CI->isNullValue()) + 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->equalsInt(1)) + if (CI->isOne()) return PN; } return nullptr; @@ -179,9 +180,9 @@ } bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const { - // For each block we check that it doesn't have any uses outside of it's - // innermost loop. This process will transitivelly guarntee that current loop - // and all of the nested loops are in the LCSSA form. + // 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. return all_of(this->blocks(), [&](const BasicBlock *BB) { return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT); }); @@ -211,9 +212,11 @@ MDNode *Loop::getLoopID() const { MDNode *LoopID = nullptr; - if (isLoopSimplifyForm()) { - LoopID = getLoopLatch()->getTerminator()->getMetadata(LLVMContext::MD_loop); + if (BasicBlock *Latch = getLoopLatch()) { + LoopID = Latch->getTerminator()->getMetadata(LLVMContext::MD_loop); } else { + assert(!getLoopLatch() && + "The loop should have no single latch at this point"); // Go through each predecessor of the loop header and check the // terminator for the metadata. BasicBlock *H = getHeader(); @@ -248,11 +251,13 @@ assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand"); assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself"); - if (isLoopSimplifyForm()) { - getLoopLatch()->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); + if (BasicBlock *Latch = getLoopLatch()) { + Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); return; } + assert(!getLoopLatch() && + "The loop should have no single latch at this point"); BasicBlock *H = getHeader(); for (BasicBlock *BB : this->blocks()) { TerminatorInst *TI = BB->getTerminator(); @@ -263,11 +268,44 @@ } } +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); +} + 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 @@ -304,9 +342,7 @@ 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. @@ -354,8 +390,8 @@ 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!"); @@ -405,12 +441,10 @@ } #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 @@ -431,15 +465,15 @@ // 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(); @@ -457,7 +491,7 @@ void UnloopUpdater::updateBlockParents() { if (Unloop.getNumBlocks()) { // Perform a post order CFG traversal of all blocks within this loop, - // propagating the nearest loop from sucessors to predecessors. + // propagating the nearest loop from successors to predecessors. LoopBlocksTraversal Traversal(DFS, LI); for (BasicBlock *POI : Traversal) { @@ -469,8 +503,7 @@ 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"); @@ -487,7 +520,8 @@ // 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); @@ -505,8 +539,8 @@ 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) @@ -606,14 +640,21 @@ return NearLoop; } -LoopInfo::LoopInfo(const DominatorTreeBase<BasicBlock> &DomTree) { - analyze(DomTree); +LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); } + +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>()); } -void LoopInfo::markAsRemoved(Loop *Unloop) { - assert(!Unloop->isInvalid() && "Loop has already been removed"); - Unloop->invalidate(); - RemovedLoops.push_back(Unloop); +void LoopInfo::erase(Loop *Unloop) { + assert(!Unloop->isInvalid() && "Loop has already been erased!"); + + auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); }); // First handle the special case of no parent loop to simplify the algorithm. if (!Unloop->getParentLoop()) { @@ -689,18 +730,13 @@ return PreservedAnalyses::all(); } -PrintLoopPass::PrintLoopPass() : OS(dbgs()) {} -PrintLoopPass::PrintLoopPass(raw_ostream &OS, const std::string &Banner) - : OS(OS), Banner(Banner) {} - -PreservedAnalyses PrintLoopPass::run(Loop &L, AnalysisManager<Loop> &) { +void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) { OS << Banner; for (auto *Block : L.blocks()) if (Block) Block->print(OS); else OS << "Printing <null> block"; - return PreservedAnalyses::all(); } //===----------------------------------------------------------------------===// @@ -759,5 +795,7 @@ 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) + ; }