LoopInfo.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. //===- LoopInfo.cpp - Natural Loop Calculator -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the LoopInfo class that is used to identify natural loops
  10. // and determine the loop depth of various nodes of the CFG. Note that the
  11. // loops identified may actually be several natural loops that share the same
  12. // header node... not just a single natural loop.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/LoopInfo.h"
  16. #include "llvm/ADT/DepthFirstIterator.h"
  17. #include "llvm/ADT/ScopeExit.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/Analysis/IVDescriptors.h"
  20. #include "llvm/Analysis/LoopInfoImpl.h"
  21. #include "llvm/Analysis/LoopIterator.h"
  22. #include "llvm/Analysis/MemorySSA.h"
  23. #include "llvm/Analysis/MemorySSAUpdater.h"
  24. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  25. #include "llvm/Analysis/ValueTracking.h"
  26. #include "llvm/Config/llvm-config.h"
  27. #include "llvm/IR/CFG.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/IR/DebugLoc.h"
  30. #include "llvm/IR/Dominators.h"
  31. #include "llvm/IR/IRPrintingPasses.h"
  32. #include "llvm/IR/Instructions.h"
  33. #include "llvm/IR/LLVMContext.h"
  34. #include "llvm/IR/Metadata.h"
  35. #include "llvm/IR/PassManager.h"
  36. #include "llvm/Support/CommandLine.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <algorithm>
  40. using namespace llvm;
  41. // Explicitly instantiate methods in LoopInfoImpl.h for IR-level Loops.
  42. template class llvm::LoopBase<BasicBlock, Loop>;
  43. template class llvm::LoopInfoBase<BasicBlock, Loop>;
  44. // Always verify loopinfo if expensive checking is enabled.
  45. #ifdef EXPENSIVE_CHECKS
  46. bool llvm::VerifyLoopInfo = true;
  47. #else
  48. bool llvm::VerifyLoopInfo = false;
  49. #endif
  50. static cl::opt<bool, true>
  51. VerifyLoopInfoX("verify-loop-info", cl::location(VerifyLoopInfo),
  52. cl::Hidden, cl::desc("Verify loop info (time consuming)"));
  53. //===----------------------------------------------------------------------===//
  54. // Loop implementation
  55. //
  56. bool Loop::isLoopInvariant(const Value *V) const {
  57. if (const Instruction *I = dyn_cast<Instruction>(V))
  58. return !contains(I);
  59. return true; // All non-instructions are loop invariant
  60. }
  61. bool Loop::hasLoopInvariantOperands(const Instruction *I) const {
  62. return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });
  63. }
  64. bool Loop::makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt,
  65. MemorySSAUpdater *MSSAU) const {
  66. if (Instruction *I = dyn_cast<Instruction>(V))
  67. return makeLoopInvariant(I, Changed, InsertPt, MSSAU);
  68. return true; // All non-instructions are loop-invariant.
  69. }
  70. bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
  71. Instruction *InsertPt,
  72. MemorySSAUpdater *MSSAU) const {
  73. // Test if the value is already loop-invariant.
  74. if (isLoopInvariant(I))
  75. return true;
  76. if (!isSafeToSpeculativelyExecute(I))
  77. return false;
  78. if (I->mayReadFromMemory())
  79. return false;
  80. // EH block instructions are immobile.
  81. if (I->isEHPad())
  82. return false;
  83. // Determine the insertion point, unless one was given.
  84. if (!InsertPt) {
  85. BasicBlock *Preheader = getLoopPreheader();
  86. // Without a preheader, hoisting is not feasible.
  87. if (!Preheader)
  88. return false;
  89. InsertPt = Preheader->getTerminator();
  90. }
  91. // Don't hoist instructions with loop-variant operands.
  92. for (Value *Operand : I->operands())
  93. if (!makeLoopInvariant(Operand, Changed, InsertPt, MSSAU))
  94. return false;
  95. // Hoist.
  96. I->moveBefore(InsertPt);
  97. if (MSSAU)
  98. if (auto *MUD = MSSAU->getMemorySSA()->getMemoryAccess(I))
  99. MSSAU->moveToPlace(MUD, InsertPt->getParent(), MemorySSA::End);
  100. // There is possibility of hoisting this instruction above some arbitrary
  101. // condition. Any metadata defined on it can be control dependent on this
  102. // condition. Conservatively strip it here so that we don't give any wrong
  103. // information to the optimizer.
  104. I->dropUnknownNonDebugMetadata();
  105. Changed = true;
  106. return true;
  107. }
  108. bool Loop::getIncomingAndBackEdge(BasicBlock *&Incoming,
  109. BasicBlock *&Backedge) const {
  110. BasicBlock *H = getHeader();
  111. Incoming = nullptr;
  112. Backedge = nullptr;
  113. pred_iterator PI = pred_begin(H);
  114. assert(PI != pred_end(H) && "Loop must have at least one backedge!");
  115. Backedge = *PI++;
  116. if (PI == pred_end(H))
  117. return false; // dead loop
  118. Incoming = *PI++;
  119. if (PI != pred_end(H))
  120. return false; // multiple backedges?
  121. if (contains(Incoming)) {
  122. if (contains(Backedge))
  123. return false;
  124. std::swap(Incoming, Backedge);
  125. } else if (!contains(Backedge))
  126. return false;
  127. assert(Incoming && Backedge && "expected non-null incoming and backedges");
  128. return true;
  129. }
  130. PHINode *Loop::getCanonicalInductionVariable() const {
  131. BasicBlock *H = getHeader();
  132. BasicBlock *Incoming = nullptr, *Backedge = nullptr;
  133. if (!getIncomingAndBackEdge(Incoming, Backedge))
  134. return nullptr;
  135. // Loop over all of the PHI nodes, looking for a canonical indvar.
  136. for (BasicBlock::iterator I = H->begin(); isa<PHINode>(I); ++I) {
  137. PHINode *PN = cast<PHINode>(I);
  138. if (ConstantInt *CI =
  139. dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
  140. if (CI->isZero())
  141. if (Instruction *Inc =
  142. dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
  143. if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
  144. if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
  145. if (CI->isOne())
  146. return PN;
  147. }
  148. return nullptr;
  149. }
  150. /// Get the latch condition instruction.
  151. static ICmpInst *getLatchCmpInst(const Loop &L) {
  152. if (BasicBlock *Latch = L.getLoopLatch())
  153. if (BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator()))
  154. if (BI->isConditional())
  155. return dyn_cast<ICmpInst>(BI->getCondition());
  156. return nullptr;
  157. }
  158. /// Return the final value of the loop induction variable if found.
  159. static Value *findFinalIVValue(const Loop &L, const PHINode &IndVar,
  160. const Instruction &StepInst) {
  161. ICmpInst *LatchCmpInst = getLatchCmpInst(L);
  162. if (!LatchCmpInst)
  163. return nullptr;
  164. Value *Op0 = LatchCmpInst->getOperand(0);
  165. Value *Op1 = LatchCmpInst->getOperand(1);
  166. if (Op0 == &IndVar || Op0 == &StepInst)
  167. return Op1;
  168. if (Op1 == &IndVar || Op1 == &StepInst)
  169. return Op0;
  170. return nullptr;
  171. }
  172. Optional<Loop::LoopBounds> Loop::LoopBounds::getBounds(const Loop &L,
  173. PHINode &IndVar,
  174. ScalarEvolution &SE) {
  175. InductionDescriptor IndDesc;
  176. if (!InductionDescriptor::isInductionPHI(&IndVar, &L, &SE, IndDesc))
  177. return None;
  178. Value *InitialIVValue = IndDesc.getStartValue();
  179. Instruction *StepInst = IndDesc.getInductionBinOp();
  180. if (!InitialIVValue || !StepInst)
  181. return None;
  182. const SCEV *Step = IndDesc.getStep();
  183. Value *StepInstOp1 = StepInst->getOperand(1);
  184. Value *StepInstOp0 = StepInst->getOperand(0);
  185. Value *StepValue = nullptr;
  186. if (SE.getSCEV(StepInstOp1) == Step)
  187. StepValue = StepInstOp1;
  188. else if (SE.getSCEV(StepInstOp0) == Step)
  189. StepValue = StepInstOp0;
  190. Value *FinalIVValue = findFinalIVValue(L, IndVar, *StepInst);
  191. if (!FinalIVValue)
  192. return None;
  193. return LoopBounds(L, *InitialIVValue, *StepInst, StepValue, *FinalIVValue,
  194. SE);
  195. }
  196. using Direction = Loop::LoopBounds::Direction;
  197. ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate() const {
  198. BasicBlock *Latch = L.getLoopLatch();
  199. assert(Latch && "Expecting valid latch");
  200. BranchInst *BI = dyn_cast_or_null<BranchInst>(Latch->getTerminator());
  201. assert(BI && BI->isConditional() && "Expecting conditional latch branch");
  202. ICmpInst *LatchCmpInst = dyn_cast<ICmpInst>(BI->getCondition());
  203. assert(LatchCmpInst &&
  204. "Expecting the latch compare instruction to be a CmpInst");
  205. // Need to inverse the predicate when first successor is not the loop
  206. // header
  207. ICmpInst::Predicate Pred = (BI->getSuccessor(0) == L.getHeader())
  208. ? LatchCmpInst->getPredicate()
  209. : LatchCmpInst->getInversePredicate();
  210. if (LatchCmpInst->getOperand(0) == &getFinalIVValue())
  211. Pred = ICmpInst::getSwappedPredicate(Pred);
  212. // Need to flip strictness of the predicate when the latch compare instruction
  213. // is not using StepInst
  214. if (LatchCmpInst->getOperand(0) == &getStepInst() ||
  215. LatchCmpInst->getOperand(1) == &getStepInst())
  216. return Pred;
  217. // Cannot flip strictness of NE and EQ
  218. if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
  219. return ICmpInst::getFlippedStrictnessPredicate(Pred);
  220. Direction D = getDirection();
  221. if (D == Direction::Increasing)
  222. return ICmpInst::ICMP_SLT;
  223. if (D == Direction::Decreasing)
  224. return ICmpInst::ICMP_SGT;
  225. // If cannot determine the direction, then unable to find the canonical
  226. // predicate
  227. return ICmpInst::BAD_ICMP_PREDICATE;
  228. }
  229. Direction Loop::LoopBounds::getDirection() const {
  230. if (const SCEVAddRecExpr *StepAddRecExpr =
  231. dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&getStepInst())))
  232. if (const SCEV *StepRecur = StepAddRecExpr->getStepRecurrence(SE)) {
  233. if (SE.isKnownPositive(StepRecur))
  234. return Direction::Increasing;
  235. if (SE.isKnownNegative(StepRecur))
  236. return Direction::Decreasing;
  237. }
  238. return Direction::Unknown;
  239. }
  240. Optional<Loop::LoopBounds> Loop::getBounds(ScalarEvolution &SE) const {
  241. if (PHINode *IndVar = getInductionVariable(SE))
  242. return LoopBounds::getBounds(*this, *IndVar, SE);
  243. return None;
  244. }
  245. PHINode *Loop::getInductionVariable(ScalarEvolution &SE) const {
  246. if (!isLoopSimplifyForm())
  247. return nullptr;
  248. BasicBlock *Header = getHeader();
  249. assert(Header && "Expected a valid loop header");
  250. ICmpInst *CmpInst = getLatchCmpInst(*this);
  251. if (!CmpInst)
  252. return nullptr;
  253. Instruction *LatchCmpOp0 = dyn_cast<Instruction>(CmpInst->getOperand(0));
  254. Instruction *LatchCmpOp1 = dyn_cast<Instruction>(CmpInst->getOperand(1));
  255. for (PHINode &IndVar : Header->phis()) {
  256. InductionDescriptor IndDesc;
  257. if (!InductionDescriptor::isInductionPHI(&IndVar, this, &SE, IndDesc))
  258. continue;
  259. Instruction *StepInst = IndDesc.getInductionBinOp();
  260. // case 1:
  261. // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
  262. // StepInst = IndVar + step
  263. // cmp = StepInst < FinalValue
  264. if (StepInst == LatchCmpOp0 || StepInst == LatchCmpOp1)
  265. return &IndVar;
  266. // case 2:
  267. // IndVar = phi[{InitialValue, preheader}, {StepInst, latch}]
  268. // StepInst = IndVar + step
  269. // cmp = IndVar < FinalValue
  270. if (&IndVar == LatchCmpOp0 || &IndVar == LatchCmpOp1)
  271. return &IndVar;
  272. }
  273. return nullptr;
  274. }
  275. bool Loop::getInductionDescriptor(ScalarEvolution &SE,
  276. InductionDescriptor &IndDesc) const {
  277. if (PHINode *IndVar = getInductionVariable(SE))
  278. return InductionDescriptor::isInductionPHI(IndVar, this, &SE, IndDesc);
  279. return false;
  280. }
  281. bool Loop::isAuxiliaryInductionVariable(PHINode &AuxIndVar,
  282. ScalarEvolution &SE) const {
  283. // Located in the loop header
  284. BasicBlock *Header = getHeader();
  285. if (AuxIndVar.getParent() != Header)
  286. return false;
  287. // No uses outside of the loop
  288. for (User *U : AuxIndVar.users())
  289. if (const Instruction *I = dyn_cast<Instruction>(U))
  290. if (!contains(I))
  291. return false;
  292. InductionDescriptor IndDesc;
  293. if (!InductionDescriptor::isInductionPHI(&AuxIndVar, this, &SE, IndDesc))
  294. return false;
  295. // The step instruction opcode should be add or sub.
  296. if (IndDesc.getInductionOpcode() != Instruction::Add &&
  297. IndDesc.getInductionOpcode() != Instruction::Sub)
  298. return false;
  299. // Incremented by a loop invariant step for each loop iteration
  300. return SE.isLoopInvariant(IndDesc.getStep(), this);
  301. }
  302. BranchInst *Loop::getLoopGuardBranch() const {
  303. assert(isLoopSimplifyForm() && "Only valid for loop in simplify form");
  304. BasicBlock *Preheader = getLoopPreheader();
  305. BasicBlock *Latch = getLoopLatch();
  306. assert(Preheader && Latch &&
  307. "Expecting a loop with valid preheader and latch");
  308. assert(isLoopExiting(Latch) && "Only valid for rotated loop");
  309. Instruction *LatchTI = Latch->getTerminator();
  310. if (!LatchTI || LatchTI->getNumSuccessors() != 2)
  311. return nullptr;
  312. BasicBlock *ExitFromLatch = (LatchTI->getSuccessor(0) == getHeader())
  313. ? LatchTI->getSuccessor(1)
  314. : LatchTI->getSuccessor(0);
  315. BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor();
  316. if (!ExitFromLatchSucc)
  317. return nullptr;
  318. BasicBlock *GuardBB = Preheader->getUniquePredecessor();
  319. if (!GuardBB)
  320. return nullptr;
  321. assert(GuardBB->getTerminator() && "Expecting valid guard terminator");
  322. BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator());
  323. if (!GuardBI || GuardBI->isUnconditional())
  324. return nullptr;
  325. BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader)
  326. ? GuardBI->getSuccessor(1)
  327. : GuardBI->getSuccessor(0);
  328. return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr;
  329. }
  330. bool Loop::isCanonical(ScalarEvolution &SE) const {
  331. InductionDescriptor IndDesc;
  332. if (!getInductionDescriptor(SE, IndDesc))
  333. return false;
  334. ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue());
  335. if (!Init || !Init->isZero())
  336. return false;
  337. if (IndDesc.getInductionOpcode() != Instruction::Add)
  338. return false;
  339. ConstantInt *Step = IndDesc.getConstIntStepValue();
  340. if (!Step || !Step->isOne())
  341. return false;
  342. return true;
  343. }
  344. // Check that 'BB' doesn't have any uses outside of the 'L'
  345. static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
  346. DominatorTree &DT) {
  347. for (const Instruction &I : BB) {
  348. // Tokens can't be used in PHI nodes and live-out tokens prevent loop
  349. // optimizations, so for the purposes of considered LCSSA form, we
  350. // can ignore them.
  351. if (I.getType()->isTokenTy())
  352. continue;
  353. for (const Use &U : I.uses()) {
  354. const Instruction *UI = cast<Instruction>(U.getUser());
  355. const BasicBlock *UserBB = UI->getParent();
  356. if (const PHINode *P = dyn_cast<PHINode>(UI))
  357. UserBB = P->getIncomingBlock(U);
  358. // Check the current block, as a fast-path, before checking whether
  359. // the use is anywhere in the loop. Most values are used in the same
  360. // block they are defined in. Also, blocks not reachable from the
  361. // entry are special; uses in them don't need to go through PHIs.
  362. if (UserBB != &BB && !L.contains(UserBB) &&
  363. DT.isReachableFromEntry(UserBB))
  364. return false;
  365. }
  366. }
  367. return true;
  368. }
  369. bool Loop::isLCSSAForm(DominatorTree &DT) const {
  370. // For each block we check that it doesn't have any uses outside of this loop.
  371. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  372. return isBlockInLCSSAForm(*this, *BB, DT);
  373. });
  374. }
  375. bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
  376. // For each block we check that it doesn't have any uses outside of its
  377. // innermost loop. This process will transitively guarantee that the current
  378. // loop and all of the nested loops are in LCSSA form.
  379. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  380. return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
  381. });
  382. }
  383. bool Loop::isLoopSimplifyForm() const {
  384. // Normal-form loops have a preheader, a single backedge, and all of their
  385. // exits have all their predecessors inside the loop.
  386. return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
  387. }
  388. // Routines that reform the loop CFG and split edges often fail on indirectbr.
  389. bool Loop::isSafeToClone() const {
  390. // Return false if any loop blocks contain indirectbrs, or there are any calls
  391. // to noduplicate functions.
  392. // FIXME: it should be ok to clone CallBrInst's if we correctly update the
  393. // operand list to reflect the newly cloned labels.
  394. for (BasicBlock *BB : this->blocks()) {
  395. if (isa<IndirectBrInst>(BB->getTerminator()) ||
  396. isa<CallBrInst>(BB->getTerminator()))
  397. return false;
  398. for (Instruction &I : *BB)
  399. if (auto CS = CallSite(&I))
  400. if (CS.cannotDuplicate())
  401. return false;
  402. }
  403. return true;
  404. }
  405. MDNode *Loop::getLoopID() const {
  406. MDNode *LoopID = nullptr;
  407. // Go through the latch blocks and check the terminator for the metadata.
  408. SmallVector<BasicBlock *, 4> LatchesBlocks;
  409. getLoopLatches(LatchesBlocks);
  410. for (BasicBlock *BB : LatchesBlocks) {
  411. Instruction *TI = BB->getTerminator();
  412. MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
  413. if (!MD)
  414. return nullptr;
  415. if (!LoopID)
  416. LoopID = MD;
  417. else if (MD != LoopID)
  418. return nullptr;
  419. }
  420. if (!LoopID || LoopID->getNumOperands() == 0 ||
  421. LoopID->getOperand(0) != LoopID)
  422. return nullptr;
  423. return LoopID;
  424. }
  425. void Loop::setLoopID(MDNode *LoopID) const {
  426. assert((!LoopID || LoopID->getNumOperands() > 0) &&
  427. "Loop ID needs at least one operand");
  428. assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
  429. "Loop ID should refer to itself");
  430. SmallVector<BasicBlock *, 4> LoopLatches;
  431. getLoopLatches(LoopLatches);
  432. for (BasicBlock *BB : LoopLatches)
  433. BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
  434. }
  435. void Loop::setLoopAlreadyUnrolled() {
  436. LLVMContext &Context = getHeader()->getContext();
  437. MDNode *DisableUnrollMD =
  438. MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
  439. MDNode *LoopID = getLoopID();
  440. MDNode *NewLoopID = makePostTransformationMetadata(
  441. Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
  442. setLoopID(NewLoopID);
  443. }
  444. bool Loop::isAnnotatedParallel() const {
  445. MDNode *DesiredLoopIdMetadata = getLoopID();
  446. if (!DesiredLoopIdMetadata)
  447. return false;
  448. MDNode *ParallelAccesses =
  449. findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
  450. SmallPtrSet<MDNode *, 4>
  451. ParallelAccessGroups; // For scalable 'contains' check.
  452. if (ParallelAccesses) {
  453. for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
  454. MDNode *AccGroup = cast<MDNode>(MD.get());
  455. assert(isValidAsAccessGroup(AccGroup) &&
  456. "List item must be an access group");
  457. ParallelAccessGroups.insert(AccGroup);
  458. }
  459. }
  460. // The loop branch contains the parallel loop metadata. In order to ensure
  461. // that any parallel-loop-unaware optimization pass hasn't added loop-carried
  462. // dependencies (thus converted the loop back to a sequential loop), check
  463. // that all the memory instructions in the loop belong to an access group that
  464. // is parallel to this loop.
  465. for (BasicBlock *BB : this->blocks()) {
  466. for (Instruction &I : *BB) {
  467. if (!I.mayReadOrWriteMemory())
  468. continue;
  469. if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
  470. auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
  471. if (AG->getNumOperands() == 0) {
  472. assert(isValidAsAccessGroup(AG) && "Item must be an access group");
  473. return ParallelAccessGroups.count(AG);
  474. }
  475. for (const MDOperand &AccessListItem : AG->operands()) {
  476. MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
  477. assert(isValidAsAccessGroup(AccGroup) &&
  478. "List item must be an access group");
  479. if (ParallelAccessGroups.count(AccGroup))
  480. return true;
  481. }
  482. return false;
  483. };
  484. if (ContainsAccessGroup(AccessGroup))
  485. continue;
  486. }
  487. // The memory instruction can refer to the loop identifier metadata
  488. // directly or indirectly through another list metadata (in case of
  489. // nested parallel loops). The loop identifier metadata refers to
  490. // itself so we can check both cases with the same routine.
  491. MDNode *LoopIdMD =
  492. I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
  493. if (!LoopIdMD)
  494. return false;
  495. bool LoopIdMDFound = false;
  496. for (const MDOperand &MDOp : LoopIdMD->operands()) {
  497. if (MDOp == DesiredLoopIdMetadata) {
  498. LoopIdMDFound = true;
  499. break;
  500. }
  501. }
  502. if (!LoopIdMDFound)
  503. return false;
  504. }
  505. }
  506. return true;
  507. }
  508. DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
  509. Loop::LocRange Loop::getLocRange() const {
  510. // If we have a debug location in the loop ID, then use it.
  511. if (MDNode *LoopID = getLoopID()) {
  512. DebugLoc Start;
  513. // We use the first DebugLoc in the header as the start location of the loop
  514. // and if there is a second DebugLoc in the header we use it as end location
  515. // of the loop.
  516. for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
  517. if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
  518. if (!Start)
  519. Start = DebugLoc(L);
  520. else
  521. return LocRange(Start, DebugLoc(L));
  522. }
  523. }
  524. if (Start)
  525. return LocRange(Start);
  526. }
  527. // Try the pre-header first.
  528. if (BasicBlock *PHeadBB = getLoopPreheader())
  529. if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
  530. return LocRange(DL);
  531. // If we have no pre-header or there are no instructions with debug
  532. // info in it, try the header.
  533. if (BasicBlock *HeadBB = getHeader())
  534. return LocRange(HeadBB->getTerminator()->getDebugLoc());
  535. return LocRange();
  536. }
  537. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  538. LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
  539. LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
  540. print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
  541. }
  542. #endif
  543. //===----------------------------------------------------------------------===//
  544. // UnloopUpdater implementation
  545. //
  546. namespace {
  547. /// Find the new parent loop for all blocks within the "unloop" whose last
  548. /// backedges has just been removed.
  549. class UnloopUpdater {
  550. Loop &Unloop;
  551. LoopInfo *LI;
  552. LoopBlocksDFS DFS;
  553. // Map unloop's immediate subloops to their nearest reachable parents. Nested
  554. // loops within these subloops will not change parents. However, an immediate
  555. // subloop's new parent will be the nearest loop reachable from either its own
  556. // exits *or* any of its nested loop's exits.
  557. DenseMap<Loop *, Loop *> SubloopParents;
  558. // Flag the presence of an irreducible backedge whose destination is a block
  559. // directly contained by the original unloop.
  560. bool FoundIB;
  561. public:
  562. UnloopUpdater(Loop *UL, LoopInfo *LInfo)
  563. : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
  564. void updateBlockParents();
  565. void removeBlocksFromAncestors();
  566. void updateSubloopParents();
  567. protected:
  568. Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
  569. };
  570. } // end anonymous namespace
  571. /// Update the parent loop for all blocks that are directly contained within the
  572. /// original "unloop".
  573. void UnloopUpdater::updateBlockParents() {
  574. if (Unloop.getNumBlocks()) {
  575. // Perform a post order CFG traversal of all blocks within this loop,
  576. // propagating the nearest loop from successors to predecessors.
  577. LoopBlocksTraversal Traversal(DFS, LI);
  578. for (BasicBlock *POI : Traversal) {
  579. Loop *L = LI->getLoopFor(POI);
  580. Loop *NL = getNearestLoop(POI, L);
  581. if (NL != L) {
  582. // For reducible loops, NL is now an ancestor of Unloop.
  583. assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
  584. "uninitialized successor");
  585. LI->changeLoopFor(POI, NL);
  586. } else {
  587. // Or the current block is part of a subloop, in which case its parent
  588. // is unchanged.
  589. assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
  590. }
  591. }
  592. }
  593. // Each irreducible loop within the unloop induces a round of iteration using
  594. // the DFS result cached by Traversal.
  595. bool Changed = FoundIB;
  596. for (unsigned NIters = 0; Changed; ++NIters) {
  597. assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
  598. // Iterate over the postorder list of blocks, propagating the nearest loop
  599. // from successors to predecessors as before.
  600. Changed = false;
  601. for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
  602. POE = DFS.endPostorder();
  603. POI != POE; ++POI) {
  604. Loop *L = LI->getLoopFor(*POI);
  605. Loop *NL = getNearestLoop(*POI, L);
  606. if (NL != L) {
  607. assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
  608. "uninitialized successor");
  609. LI->changeLoopFor(*POI, NL);
  610. Changed = true;
  611. }
  612. }
  613. }
  614. }
  615. /// Remove unloop's blocks from all ancestors below their new parents.
  616. void UnloopUpdater::removeBlocksFromAncestors() {
  617. // Remove all unloop's blocks (including those in nested subloops) from
  618. // ancestors below the new parent loop.
  619. for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
  620. BI != BE; ++BI) {
  621. Loop *OuterParent = LI->getLoopFor(*BI);
  622. if (Unloop.contains(OuterParent)) {
  623. while (OuterParent->getParentLoop() != &Unloop)
  624. OuterParent = OuterParent->getParentLoop();
  625. OuterParent = SubloopParents[OuterParent];
  626. }
  627. // Remove blocks from former Ancestors except Unloop itself which will be
  628. // deleted.
  629. for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
  630. OldParent = OldParent->getParentLoop()) {
  631. assert(OldParent && "new loop is not an ancestor of the original");
  632. OldParent->removeBlockFromLoop(*BI);
  633. }
  634. }
  635. }
  636. /// Update the parent loop for all subloops directly nested within unloop.
  637. void UnloopUpdater::updateSubloopParents() {
  638. while (!Unloop.empty()) {
  639. Loop *Subloop = *std::prev(Unloop.end());
  640. Unloop.removeChildLoop(std::prev(Unloop.end()));
  641. assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
  642. if (Loop *Parent = SubloopParents[Subloop])
  643. Parent->addChildLoop(Subloop);
  644. else
  645. LI->addTopLevelLoop(Subloop);
  646. }
  647. }
  648. /// Return the nearest parent loop among this block's successors. If a successor
  649. /// is a subloop header, consider its parent to be the nearest parent of the
  650. /// subloop's exits.
  651. ///
  652. /// For subloop blocks, simply update SubloopParents and return NULL.
  653. Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
  654. // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
  655. // is considered uninitialized.
  656. Loop *NearLoop = BBLoop;
  657. Loop *Subloop = nullptr;
  658. if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
  659. Subloop = NearLoop;
  660. // Find the subloop ancestor that is directly contained within Unloop.
  661. while (Subloop->getParentLoop() != &Unloop) {
  662. Subloop = Subloop->getParentLoop();
  663. assert(Subloop && "subloop is not an ancestor of the original loop");
  664. }
  665. // Get the current nearest parent of the Subloop exits, initially Unloop.
  666. NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
  667. }
  668. succ_iterator I = succ_begin(BB), E = succ_end(BB);
  669. if (I == E) {
  670. assert(!Subloop && "subloop blocks must have a successor");
  671. NearLoop = nullptr; // unloop blocks may now exit the function.
  672. }
  673. for (; I != E; ++I) {
  674. if (*I == BB)
  675. continue; // self loops are uninteresting
  676. Loop *L = LI->getLoopFor(*I);
  677. if (L == &Unloop) {
  678. // This successor has not been processed. This path must lead to an
  679. // irreducible backedge.
  680. assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
  681. FoundIB = true;
  682. }
  683. if (L != &Unloop && Unloop.contains(L)) {
  684. // Successor is in a subloop.
  685. if (Subloop)
  686. continue; // Branching within subloops. Ignore it.
  687. // BB branches from the original into a subloop header.
  688. assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
  689. // Get the current nearest parent of the Subloop's exits.
  690. L = SubloopParents[L];
  691. // L could be Unloop if the only exit was an irreducible backedge.
  692. }
  693. if (L == &Unloop) {
  694. continue;
  695. }
  696. // Handle critical edges from Unloop into a sibling loop.
  697. if (L && !L->contains(&Unloop)) {
  698. L = L->getParentLoop();
  699. }
  700. // Remember the nearest parent loop among successors or subloop exits.
  701. if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
  702. NearLoop = L;
  703. }
  704. if (Subloop) {
  705. SubloopParents[Subloop] = NearLoop;
  706. return BBLoop;
  707. }
  708. return NearLoop;
  709. }
  710. LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
  711. bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  712. FunctionAnalysisManager::Invalidator &) {
  713. // Check whether the analysis, all analyses on functions, or the function's
  714. // CFG have been preserved.
  715. auto PAC = PA.getChecker<LoopAnalysis>();
  716. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  717. PAC.preservedSet<CFGAnalyses>());
  718. }
  719. void LoopInfo::erase(Loop *Unloop) {
  720. assert(!Unloop->isInvalid() && "Loop has already been erased!");
  721. auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
  722. // First handle the special case of no parent loop to simplify the algorithm.
  723. if (!Unloop->getParentLoop()) {
  724. // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
  725. for (Loop::block_iterator I = Unloop->block_begin(),
  726. E = Unloop->block_end();
  727. I != E; ++I) {
  728. // Don't reparent blocks in subloops.
  729. if (getLoopFor(*I) != Unloop)
  730. continue;
  731. // Blocks no longer have a parent but are still referenced by Unloop until
  732. // the Unloop object is deleted.
  733. changeLoopFor(*I, nullptr);
  734. }
  735. // Remove the loop from the top-level LoopInfo object.
  736. for (iterator I = begin();; ++I) {
  737. assert(I != end() && "Couldn't find loop");
  738. if (*I == Unloop) {
  739. removeLoop(I);
  740. break;
  741. }
  742. }
  743. // Move all of the subloops to the top-level.
  744. while (!Unloop->empty())
  745. addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
  746. return;
  747. }
  748. // Update the parent loop for all blocks within the loop. Blocks within
  749. // subloops will not change parents.
  750. UnloopUpdater Updater(Unloop, this);
  751. Updater.updateBlockParents();
  752. // Remove blocks from former ancestor loops.
  753. Updater.removeBlocksFromAncestors();
  754. // Add direct subloops as children in their new parent loop.
  755. Updater.updateSubloopParents();
  756. // Remove unloop from its parent loop.
  757. Loop *ParentLoop = Unloop->getParentLoop();
  758. for (Loop::iterator I = ParentLoop->begin();; ++I) {
  759. assert(I != ParentLoop->end() && "Couldn't find loop");
  760. if (*I == Unloop) {
  761. ParentLoop->removeChildLoop(I);
  762. break;
  763. }
  764. }
  765. }
  766. AnalysisKey LoopAnalysis::Key;
  767. LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
  768. // FIXME: Currently we create a LoopInfo from scratch for every function.
  769. // This may prove to be too wasteful due to deallocating and re-allocating
  770. // memory each time for the underlying map and vector datastructures. At some
  771. // point it may prove worthwhile to use a freelist and recycle LoopInfo
  772. // objects. I don't want to add that kind of complexity until the scope of
  773. // the problem is better understood.
  774. LoopInfo LI;
  775. LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
  776. return LI;
  777. }
  778. PreservedAnalyses LoopPrinterPass::run(Function &F,
  779. FunctionAnalysisManager &AM) {
  780. AM.getResult<LoopAnalysis>(F).print(OS);
  781. return PreservedAnalyses::all();
  782. }
  783. void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
  784. if (forcePrintModuleIR()) {
  785. // handling -print-module-scope
  786. OS << Banner << " (loop: ";
  787. L.getHeader()->printAsOperand(OS, false);
  788. OS << ")\n";
  789. // printing whole module
  790. OS << *L.getHeader()->getModule();
  791. return;
  792. }
  793. OS << Banner;
  794. auto *PreHeader = L.getLoopPreheader();
  795. if (PreHeader) {
  796. OS << "\n; Preheader:";
  797. PreHeader->print(OS);
  798. OS << "\n; Loop:";
  799. }
  800. for (auto *Block : L.blocks())
  801. if (Block)
  802. Block->print(OS);
  803. else
  804. OS << "Printing <null> block";
  805. SmallVector<BasicBlock *, 8> ExitBlocks;
  806. L.getExitBlocks(ExitBlocks);
  807. if (!ExitBlocks.empty()) {
  808. OS << "\n; Exit blocks";
  809. for (auto *Block : ExitBlocks)
  810. if (Block)
  811. Block->print(OS);
  812. else
  813. OS << "Printing <null> block";
  814. }
  815. }
  816. MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
  817. // No loop metadata node, no loop properties.
  818. if (!LoopID)
  819. return nullptr;
  820. // First operand should refer to the metadata node itself, for legacy reasons.
  821. assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
  822. assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
  823. // Iterate over the metdata node operands and look for MDString metadata.
  824. for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
  825. MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
  826. if (!MD || MD->getNumOperands() < 1)
  827. continue;
  828. MDString *S = dyn_cast<MDString>(MD->getOperand(0));
  829. if (!S)
  830. continue;
  831. // Return the operand node if MDString holds expected metadata.
  832. if (Name.equals(S->getString()))
  833. return MD;
  834. }
  835. // Loop property not found.
  836. return nullptr;
  837. }
  838. MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
  839. return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
  840. }
  841. bool llvm::isValidAsAccessGroup(MDNode *Node) {
  842. return Node->getNumOperands() == 0 && Node->isDistinct();
  843. }
  844. MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context,
  845. MDNode *OrigLoopID,
  846. ArrayRef<StringRef> RemovePrefixes,
  847. ArrayRef<MDNode *> AddAttrs) {
  848. // First remove any existing loop metadata related to this transformation.
  849. SmallVector<Metadata *, 4> MDs;
  850. // Reserve first location for self reference to the LoopID metadata node.
  851. TempMDTuple TempNode = MDNode::getTemporary(Context, None);
  852. MDs.push_back(TempNode.get());
  853. // Remove metadata for the transformation that has been applied or that became
  854. // outdated.
  855. if (OrigLoopID) {
  856. for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
  857. bool IsVectorMetadata = false;
  858. Metadata *Op = OrigLoopID->getOperand(i);
  859. if (MDNode *MD = dyn_cast<MDNode>(Op)) {
  860. const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
  861. if (S)
  862. IsVectorMetadata =
  863. llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
  864. return S->getString().startswith(Prefix);
  865. });
  866. }
  867. if (!IsVectorMetadata)
  868. MDs.push_back(Op);
  869. }
  870. }
  871. // Add metadata to avoid reapplying a transformation, such as
  872. // llvm.loop.unroll.disable and llvm.loop.isvectorized.
  873. MDs.append(AddAttrs.begin(), AddAttrs.end());
  874. MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
  875. // Replace the temporary node with a self-reference.
  876. NewLoopID->replaceOperandWith(0, NewLoopID);
  877. return NewLoopID;
  878. }
  879. //===----------------------------------------------------------------------===//
  880. // LoopInfo implementation
  881. //
  882. char LoopInfoWrapperPass::ID = 0;
  883. INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  884. true, true)
  885. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  886. INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  887. true, true)
  888. bool LoopInfoWrapperPass::runOnFunction(Function &) {
  889. releaseMemory();
  890. LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
  891. return false;
  892. }
  893. void LoopInfoWrapperPass::verifyAnalysis() const {
  894. // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
  895. // function each time verifyAnalysis is called is very expensive. The
  896. // -verify-loop-info option can enable this. In order to perform some
  897. // checking by default, LoopPass has been taught to call verifyLoop manually
  898. // during loop pass sequences.
  899. if (VerifyLoopInfo) {
  900. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  901. LI.verify(DT);
  902. }
  903. }
  904. void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  905. AU.setPreservesAll();
  906. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  907. }
  908. void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
  909. LI.print(OS);
  910. }
  911. PreservedAnalyses LoopVerifierPass::run(Function &F,
  912. FunctionAnalysisManager &AM) {
  913. LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
  914. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  915. LI.verify(DT);
  916. return PreservedAnalyses::all();
  917. }
  918. //===----------------------------------------------------------------------===//
  919. // LoopBlocksDFS implementation
  920. //
  921. /// Traverse the loop blocks and store the DFS result.
  922. /// Useful for clients that just want the final DFS result and don't need to
  923. /// visit blocks during the initial traversal.
  924. void LoopBlocksDFS::perform(LoopInfo *LI) {
  925. LoopBlocksTraversal Traversal(*this, LI);
  926. for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
  927. POE = Traversal.end();
  928. POI != POE; ++POI)
  929. ;
  930. }