LoopInfo.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. if (!isLoopSimplifyForm())
  304. return nullptr;
  305. BasicBlock *Preheader = getLoopPreheader();
  306. BasicBlock *Latch = getLoopLatch();
  307. assert(Preheader && Latch &&
  308. "Expecting a loop with valid preheader and latch");
  309. // Loop should be in rotate form.
  310. if (!isLoopExiting(Latch))
  311. return nullptr;
  312. // Disallow loops with more than one unique exit block, as we do not verify
  313. // that GuardOtherSucc post dominates all exit blocks.
  314. BasicBlock *ExitFromLatch = getUniqueExitBlock();
  315. if (!ExitFromLatch)
  316. return nullptr;
  317. BasicBlock *ExitFromLatchSucc = ExitFromLatch->getUniqueSuccessor();
  318. if (!ExitFromLatchSucc)
  319. return nullptr;
  320. BasicBlock *GuardBB = Preheader->getUniquePredecessor();
  321. if (!GuardBB)
  322. return nullptr;
  323. assert(GuardBB->getTerminator() && "Expecting valid guard terminator");
  324. BranchInst *GuardBI = dyn_cast<BranchInst>(GuardBB->getTerminator());
  325. if (!GuardBI || GuardBI->isUnconditional())
  326. return nullptr;
  327. BasicBlock *GuardOtherSucc = (GuardBI->getSuccessor(0) == Preheader)
  328. ? GuardBI->getSuccessor(1)
  329. : GuardBI->getSuccessor(0);
  330. return (GuardOtherSucc == ExitFromLatchSucc) ? GuardBI : nullptr;
  331. }
  332. bool Loop::isCanonical(ScalarEvolution &SE) const {
  333. InductionDescriptor IndDesc;
  334. if (!getInductionDescriptor(SE, IndDesc))
  335. return false;
  336. ConstantInt *Init = dyn_cast_or_null<ConstantInt>(IndDesc.getStartValue());
  337. if (!Init || !Init->isZero())
  338. return false;
  339. if (IndDesc.getInductionOpcode() != Instruction::Add)
  340. return false;
  341. ConstantInt *Step = IndDesc.getConstIntStepValue();
  342. if (!Step || !Step->isOne())
  343. return false;
  344. return true;
  345. }
  346. // Check that 'BB' doesn't have any uses outside of the 'L'
  347. static bool isBlockInLCSSAForm(const Loop &L, const BasicBlock &BB,
  348. DominatorTree &DT) {
  349. for (const Instruction &I : BB) {
  350. // Tokens can't be used in PHI nodes and live-out tokens prevent loop
  351. // optimizations, so for the purposes of considered LCSSA form, we
  352. // can ignore them.
  353. if (I.getType()->isTokenTy())
  354. continue;
  355. for (const Use &U : I.uses()) {
  356. const Instruction *UI = cast<Instruction>(U.getUser());
  357. const BasicBlock *UserBB = UI->getParent();
  358. if (const PHINode *P = dyn_cast<PHINode>(UI))
  359. UserBB = P->getIncomingBlock(U);
  360. // Check the current block, as a fast-path, before checking whether
  361. // the use is anywhere in the loop. Most values are used in the same
  362. // block they are defined in. Also, blocks not reachable from the
  363. // entry are special; uses in them don't need to go through PHIs.
  364. if (UserBB != &BB && !L.contains(UserBB) &&
  365. DT.isReachableFromEntry(UserBB))
  366. return false;
  367. }
  368. }
  369. return true;
  370. }
  371. bool Loop::isLCSSAForm(DominatorTree &DT) const {
  372. // For each block we check that it doesn't have any uses outside of this loop.
  373. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  374. return isBlockInLCSSAForm(*this, *BB, DT);
  375. });
  376. }
  377. bool Loop::isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const {
  378. // For each block we check that it doesn't have any uses outside of its
  379. // innermost loop. This process will transitively guarantee that the current
  380. // loop and all of the nested loops are in LCSSA form.
  381. return all_of(this->blocks(), [&](const BasicBlock *BB) {
  382. return isBlockInLCSSAForm(*LI.getLoopFor(BB), *BB, DT);
  383. });
  384. }
  385. bool Loop::isLoopSimplifyForm() const {
  386. // Normal-form loops have a preheader, a single backedge, and all of their
  387. // exits have all their predecessors inside the loop.
  388. return getLoopPreheader() && getLoopLatch() && hasDedicatedExits();
  389. }
  390. // Routines that reform the loop CFG and split edges often fail on indirectbr.
  391. bool Loop::isSafeToClone() const {
  392. // Return false if any loop blocks contain indirectbrs, or there are any calls
  393. // to noduplicate functions.
  394. // FIXME: it should be ok to clone CallBrInst's if we correctly update the
  395. // operand list to reflect the newly cloned labels.
  396. for (BasicBlock *BB : this->blocks()) {
  397. if (isa<IndirectBrInst>(BB->getTerminator()) ||
  398. isa<CallBrInst>(BB->getTerminator()))
  399. return false;
  400. for (Instruction &I : *BB)
  401. if (auto CS = CallSite(&I))
  402. if (CS.cannotDuplicate())
  403. return false;
  404. }
  405. return true;
  406. }
  407. MDNode *Loop::getLoopID() const {
  408. MDNode *LoopID = nullptr;
  409. // Go through the latch blocks and check the terminator for the metadata.
  410. SmallVector<BasicBlock *, 4> LatchesBlocks;
  411. getLoopLatches(LatchesBlocks);
  412. for (BasicBlock *BB : LatchesBlocks) {
  413. Instruction *TI = BB->getTerminator();
  414. MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
  415. if (!MD)
  416. return nullptr;
  417. if (!LoopID)
  418. LoopID = MD;
  419. else if (MD != LoopID)
  420. return nullptr;
  421. }
  422. if (!LoopID || LoopID->getNumOperands() == 0 ||
  423. LoopID->getOperand(0) != LoopID)
  424. return nullptr;
  425. return LoopID;
  426. }
  427. void Loop::setLoopID(MDNode *LoopID) const {
  428. assert((!LoopID || LoopID->getNumOperands() > 0) &&
  429. "Loop ID needs at least one operand");
  430. assert((!LoopID || LoopID->getOperand(0) == LoopID) &&
  431. "Loop ID should refer to itself");
  432. SmallVector<BasicBlock *, 4> LoopLatches;
  433. getLoopLatches(LoopLatches);
  434. for (BasicBlock *BB : LoopLatches)
  435. BB->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
  436. }
  437. void Loop::setLoopAlreadyUnrolled() {
  438. LLVMContext &Context = getHeader()->getContext();
  439. MDNode *DisableUnrollMD =
  440. MDNode::get(Context, MDString::get(Context, "llvm.loop.unroll.disable"));
  441. MDNode *LoopID = getLoopID();
  442. MDNode *NewLoopID = makePostTransformationMetadata(
  443. Context, LoopID, {"llvm.loop.unroll."}, {DisableUnrollMD});
  444. setLoopID(NewLoopID);
  445. }
  446. bool Loop::isAnnotatedParallel() const {
  447. MDNode *DesiredLoopIdMetadata = getLoopID();
  448. if (!DesiredLoopIdMetadata)
  449. return false;
  450. MDNode *ParallelAccesses =
  451. findOptionMDForLoop(this, "llvm.loop.parallel_accesses");
  452. SmallPtrSet<MDNode *, 4>
  453. ParallelAccessGroups; // For scalable 'contains' check.
  454. if (ParallelAccesses) {
  455. for (const MDOperand &MD : drop_begin(ParallelAccesses->operands(), 1)) {
  456. MDNode *AccGroup = cast<MDNode>(MD.get());
  457. assert(isValidAsAccessGroup(AccGroup) &&
  458. "List item must be an access group");
  459. ParallelAccessGroups.insert(AccGroup);
  460. }
  461. }
  462. // The loop branch contains the parallel loop metadata. In order to ensure
  463. // that any parallel-loop-unaware optimization pass hasn't added loop-carried
  464. // dependencies (thus converted the loop back to a sequential loop), check
  465. // that all the memory instructions in the loop belong to an access group that
  466. // is parallel to this loop.
  467. for (BasicBlock *BB : this->blocks()) {
  468. for (Instruction &I : *BB) {
  469. if (!I.mayReadOrWriteMemory())
  470. continue;
  471. if (MDNode *AccessGroup = I.getMetadata(LLVMContext::MD_access_group)) {
  472. auto ContainsAccessGroup = [&ParallelAccessGroups](MDNode *AG) -> bool {
  473. if (AG->getNumOperands() == 0) {
  474. assert(isValidAsAccessGroup(AG) && "Item must be an access group");
  475. return ParallelAccessGroups.count(AG);
  476. }
  477. for (const MDOperand &AccessListItem : AG->operands()) {
  478. MDNode *AccGroup = cast<MDNode>(AccessListItem.get());
  479. assert(isValidAsAccessGroup(AccGroup) &&
  480. "List item must be an access group");
  481. if (ParallelAccessGroups.count(AccGroup))
  482. return true;
  483. }
  484. return false;
  485. };
  486. if (ContainsAccessGroup(AccessGroup))
  487. continue;
  488. }
  489. // The memory instruction can refer to the loop identifier metadata
  490. // directly or indirectly through another list metadata (in case of
  491. // nested parallel loops). The loop identifier metadata refers to
  492. // itself so we can check both cases with the same routine.
  493. MDNode *LoopIdMD =
  494. I.getMetadata(LLVMContext::MD_mem_parallel_loop_access);
  495. if (!LoopIdMD)
  496. return false;
  497. bool LoopIdMDFound = false;
  498. for (const MDOperand &MDOp : LoopIdMD->operands()) {
  499. if (MDOp == DesiredLoopIdMetadata) {
  500. LoopIdMDFound = true;
  501. break;
  502. }
  503. }
  504. if (!LoopIdMDFound)
  505. return false;
  506. }
  507. }
  508. return true;
  509. }
  510. DebugLoc Loop::getStartLoc() const { return getLocRange().getStart(); }
  511. Loop::LocRange Loop::getLocRange() const {
  512. // If we have a debug location in the loop ID, then use it.
  513. if (MDNode *LoopID = getLoopID()) {
  514. DebugLoc Start;
  515. // We use the first DebugLoc in the header as the start location of the loop
  516. // and if there is a second DebugLoc in the header we use it as end location
  517. // of the loop.
  518. for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
  519. if (DILocation *L = dyn_cast<DILocation>(LoopID->getOperand(i))) {
  520. if (!Start)
  521. Start = DebugLoc(L);
  522. else
  523. return LocRange(Start, DebugLoc(L));
  524. }
  525. }
  526. if (Start)
  527. return LocRange(Start);
  528. }
  529. // Try the pre-header first.
  530. if (BasicBlock *PHeadBB = getLoopPreheader())
  531. if (DebugLoc DL = PHeadBB->getTerminator()->getDebugLoc())
  532. return LocRange(DL);
  533. // If we have no pre-header or there are no instructions with debug
  534. // info in it, try the header.
  535. if (BasicBlock *HeadBB = getHeader())
  536. return LocRange(HeadBB->getTerminator()->getDebugLoc());
  537. return LocRange();
  538. }
  539. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  540. LLVM_DUMP_METHOD void Loop::dump() const { print(dbgs()); }
  541. LLVM_DUMP_METHOD void Loop::dumpVerbose() const {
  542. print(dbgs(), /*Depth=*/0, /*Verbose=*/true);
  543. }
  544. #endif
  545. //===----------------------------------------------------------------------===//
  546. // UnloopUpdater implementation
  547. //
  548. namespace {
  549. /// Find the new parent loop for all blocks within the "unloop" whose last
  550. /// backedges has just been removed.
  551. class UnloopUpdater {
  552. Loop &Unloop;
  553. LoopInfo *LI;
  554. LoopBlocksDFS DFS;
  555. // Map unloop's immediate subloops to their nearest reachable parents. Nested
  556. // loops within these subloops will not change parents. However, an immediate
  557. // subloop's new parent will be the nearest loop reachable from either its own
  558. // exits *or* any of its nested loop's exits.
  559. DenseMap<Loop *, Loop *> SubloopParents;
  560. // Flag the presence of an irreducible backedge whose destination is a block
  561. // directly contained by the original unloop.
  562. bool FoundIB;
  563. public:
  564. UnloopUpdater(Loop *UL, LoopInfo *LInfo)
  565. : Unloop(*UL), LI(LInfo), DFS(UL), FoundIB(false) {}
  566. void updateBlockParents();
  567. void removeBlocksFromAncestors();
  568. void updateSubloopParents();
  569. protected:
  570. Loop *getNearestLoop(BasicBlock *BB, Loop *BBLoop);
  571. };
  572. } // end anonymous namespace
  573. /// Update the parent loop for all blocks that are directly contained within the
  574. /// original "unloop".
  575. void UnloopUpdater::updateBlockParents() {
  576. if (Unloop.getNumBlocks()) {
  577. // Perform a post order CFG traversal of all blocks within this loop,
  578. // propagating the nearest loop from successors to predecessors.
  579. LoopBlocksTraversal Traversal(DFS, LI);
  580. for (BasicBlock *POI : Traversal) {
  581. Loop *L = LI->getLoopFor(POI);
  582. Loop *NL = getNearestLoop(POI, L);
  583. if (NL != L) {
  584. // For reducible loops, NL is now an ancestor of Unloop.
  585. assert((NL != &Unloop && (!NL || NL->contains(&Unloop))) &&
  586. "uninitialized successor");
  587. LI->changeLoopFor(POI, NL);
  588. } else {
  589. // Or the current block is part of a subloop, in which case its parent
  590. // is unchanged.
  591. assert((FoundIB || Unloop.contains(L)) && "uninitialized successor");
  592. }
  593. }
  594. }
  595. // Each irreducible loop within the unloop induces a round of iteration using
  596. // the DFS result cached by Traversal.
  597. bool Changed = FoundIB;
  598. for (unsigned NIters = 0; Changed; ++NIters) {
  599. assert(NIters < Unloop.getNumBlocks() && "runaway iterative algorithm");
  600. // Iterate over the postorder list of blocks, propagating the nearest loop
  601. // from successors to predecessors as before.
  602. Changed = false;
  603. for (LoopBlocksDFS::POIterator POI = DFS.beginPostorder(),
  604. POE = DFS.endPostorder();
  605. POI != POE; ++POI) {
  606. Loop *L = LI->getLoopFor(*POI);
  607. Loop *NL = getNearestLoop(*POI, L);
  608. if (NL != L) {
  609. assert(NL != &Unloop && (!NL || NL->contains(&Unloop)) &&
  610. "uninitialized successor");
  611. LI->changeLoopFor(*POI, NL);
  612. Changed = true;
  613. }
  614. }
  615. }
  616. }
  617. /// Remove unloop's blocks from all ancestors below their new parents.
  618. void UnloopUpdater::removeBlocksFromAncestors() {
  619. // Remove all unloop's blocks (including those in nested subloops) from
  620. // ancestors below the new parent loop.
  621. for (Loop::block_iterator BI = Unloop.block_begin(), BE = Unloop.block_end();
  622. BI != BE; ++BI) {
  623. Loop *OuterParent = LI->getLoopFor(*BI);
  624. if (Unloop.contains(OuterParent)) {
  625. while (OuterParent->getParentLoop() != &Unloop)
  626. OuterParent = OuterParent->getParentLoop();
  627. OuterParent = SubloopParents[OuterParent];
  628. }
  629. // Remove blocks from former Ancestors except Unloop itself which will be
  630. // deleted.
  631. for (Loop *OldParent = Unloop.getParentLoop(); OldParent != OuterParent;
  632. OldParent = OldParent->getParentLoop()) {
  633. assert(OldParent && "new loop is not an ancestor of the original");
  634. OldParent->removeBlockFromLoop(*BI);
  635. }
  636. }
  637. }
  638. /// Update the parent loop for all subloops directly nested within unloop.
  639. void UnloopUpdater::updateSubloopParents() {
  640. while (!Unloop.empty()) {
  641. Loop *Subloop = *std::prev(Unloop.end());
  642. Unloop.removeChildLoop(std::prev(Unloop.end()));
  643. assert(SubloopParents.count(Subloop) && "DFS failed to visit subloop");
  644. if (Loop *Parent = SubloopParents[Subloop])
  645. Parent->addChildLoop(Subloop);
  646. else
  647. LI->addTopLevelLoop(Subloop);
  648. }
  649. }
  650. /// Return the nearest parent loop among this block's successors. If a successor
  651. /// is a subloop header, consider its parent to be the nearest parent of the
  652. /// subloop's exits.
  653. ///
  654. /// For subloop blocks, simply update SubloopParents and return NULL.
  655. Loop *UnloopUpdater::getNearestLoop(BasicBlock *BB, Loop *BBLoop) {
  656. // Initially for blocks directly contained by Unloop, NearLoop == Unloop and
  657. // is considered uninitialized.
  658. Loop *NearLoop = BBLoop;
  659. Loop *Subloop = nullptr;
  660. if (NearLoop != &Unloop && Unloop.contains(NearLoop)) {
  661. Subloop = NearLoop;
  662. // Find the subloop ancestor that is directly contained within Unloop.
  663. while (Subloop->getParentLoop() != &Unloop) {
  664. Subloop = Subloop->getParentLoop();
  665. assert(Subloop && "subloop is not an ancestor of the original loop");
  666. }
  667. // Get the current nearest parent of the Subloop exits, initially Unloop.
  668. NearLoop = SubloopParents.insert({Subloop, &Unloop}).first->second;
  669. }
  670. succ_iterator I = succ_begin(BB), E = succ_end(BB);
  671. if (I == E) {
  672. assert(!Subloop && "subloop blocks must have a successor");
  673. NearLoop = nullptr; // unloop blocks may now exit the function.
  674. }
  675. for (; I != E; ++I) {
  676. if (*I == BB)
  677. continue; // self loops are uninteresting
  678. Loop *L = LI->getLoopFor(*I);
  679. if (L == &Unloop) {
  680. // This successor has not been processed. This path must lead to an
  681. // irreducible backedge.
  682. assert((FoundIB || !DFS.hasPostorder(*I)) && "should have seen IB");
  683. FoundIB = true;
  684. }
  685. if (L != &Unloop && Unloop.contains(L)) {
  686. // Successor is in a subloop.
  687. if (Subloop)
  688. continue; // Branching within subloops. Ignore it.
  689. // BB branches from the original into a subloop header.
  690. assert(L->getParentLoop() == &Unloop && "cannot skip into nested loops");
  691. // Get the current nearest parent of the Subloop's exits.
  692. L = SubloopParents[L];
  693. // L could be Unloop if the only exit was an irreducible backedge.
  694. }
  695. if (L == &Unloop) {
  696. continue;
  697. }
  698. // Handle critical edges from Unloop into a sibling loop.
  699. if (L && !L->contains(&Unloop)) {
  700. L = L->getParentLoop();
  701. }
  702. // Remember the nearest parent loop among successors or subloop exits.
  703. if (NearLoop == &Unloop || !NearLoop || NearLoop->contains(L))
  704. NearLoop = L;
  705. }
  706. if (Subloop) {
  707. SubloopParents[Subloop] = NearLoop;
  708. return BBLoop;
  709. }
  710. return NearLoop;
  711. }
  712. LoopInfo::LoopInfo(const DomTreeBase<BasicBlock> &DomTree) { analyze(DomTree); }
  713. bool LoopInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  714. FunctionAnalysisManager::Invalidator &) {
  715. // Check whether the analysis, all analyses on functions, or the function's
  716. // CFG have been preserved.
  717. auto PAC = PA.getChecker<LoopAnalysis>();
  718. return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
  719. PAC.preservedSet<CFGAnalyses>());
  720. }
  721. void LoopInfo::erase(Loop *Unloop) {
  722. assert(!Unloop->isInvalid() && "Loop has already been erased!");
  723. auto InvalidateOnExit = make_scope_exit([&]() { destroy(Unloop); });
  724. // First handle the special case of no parent loop to simplify the algorithm.
  725. if (!Unloop->getParentLoop()) {
  726. // Since BBLoop had no parent, Unloop blocks are no longer in a loop.
  727. for (Loop::block_iterator I = Unloop->block_begin(),
  728. E = Unloop->block_end();
  729. I != E; ++I) {
  730. // Don't reparent blocks in subloops.
  731. if (getLoopFor(*I) != Unloop)
  732. continue;
  733. // Blocks no longer have a parent but are still referenced by Unloop until
  734. // the Unloop object is deleted.
  735. changeLoopFor(*I, nullptr);
  736. }
  737. // Remove the loop from the top-level LoopInfo object.
  738. for (iterator I = begin();; ++I) {
  739. assert(I != end() && "Couldn't find loop");
  740. if (*I == Unloop) {
  741. removeLoop(I);
  742. break;
  743. }
  744. }
  745. // Move all of the subloops to the top-level.
  746. while (!Unloop->empty())
  747. addTopLevelLoop(Unloop->removeChildLoop(std::prev(Unloop->end())));
  748. return;
  749. }
  750. // Update the parent loop for all blocks within the loop. Blocks within
  751. // subloops will not change parents.
  752. UnloopUpdater Updater(Unloop, this);
  753. Updater.updateBlockParents();
  754. // Remove blocks from former ancestor loops.
  755. Updater.removeBlocksFromAncestors();
  756. // Add direct subloops as children in their new parent loop.
  757. Updater.updateSubloopParents();
  758. // Remove unloop from its parent loop.
  759. Loop *ParentLoop = Unloop->getParentLoop();
  760. for (Loop::iterator I = ParentLoop->begin();; ++I) {
  761. assert(I != ParentLoop->end() && "Couldn't find loop");
  762. if (*I == Unloop) {
  763. ParentLoop->removeChildLoop(I);
  764. break;
  765. }
  766. }
  767. }
  768. AnalysisKey LoopAnalysis::Key;
  769. LoopInfo LoopAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
  770. // FIXME: Currently we create a LoopInfo from scratch for every function.
  771. // This may prove to be too wasteful due to deallocating and re-allocating
  772. // memory each time for the underlying map and vector datastructures. At some
  773. // point it may prove worthwhile to use a freelist and recycle LoopInfo
  774. // objects. I don't want to add that kind of complexity until the scope of
  775. // the problem is better understood.
  776. LoopInfo LI;
  777. LI.analyze(AM.getResult<DominatorTreeAnalysis>(F));
  778. return LI;
  779. }
  780. PreservedAnalyses LoopPrinterPass::run(Function &F,
  781. FunctionAnalysisManager &AM) {
  782. AM.getResult<LoopAnalysis>(F).print(OS);
  783. return PreservedAnalyses::all();
  784. }
  785. void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
  786. if (forcePrintModuleIR()) {
  787. // handling -print-module-scope
  788. OS << Banner << " (loop: ";
  789. L.getHeader()->printAsOperand(OS, false);
  790. OS << ")\n";
  791. // printing whole module
  792. OS << *L.getHeader()->getModule();
  793. return;
  794. }
  795. OS << Banner;
  796. auto *PreHeader = L.getLoopPreheader();
  797. if (PreHeader) {
  798. OS << "\n; Preheader:";
  799. PreHeader->print(OS);
  800. OS << "\n; Loop:";
  801. }
  802. for (auto *Block : L.blocks())
  803. if (Block)
  804. Block->print(OS);
  805. else
  806. OS << "Printing <null> block";
  807. SmallVector<BasicBlock *, 8> ExitBlocks;
  808. L.getExitBlocks(ExitBlocks);
  809. if (!ExitBlocks.empty()) {
  810. OS << "\n; Exit blocks";
  811. for (auto *Block : ExitBlocks)
  812. if (Block)
  813. Block->print(OS);
  814. else
  815. OS << "Printing <null> block";
  816. }
  817. }
  818. MDNode *llvm::findOptionMDForLoopID(MDNode *LoopID, StringRef Name) {
  819. // No loop metadata node, no loop properties.
  820. if (!LoopID)
  821. return nullptr;
  822. // First operand should refer to the metadata node itself, for legacy reasons.
  823. assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
  824. assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
  825. // Iterate over the metdata node operands and look for MDString metadata.
  826. for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
  827. MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
  828. if (!MD || MD->getNumOperands() < 1)
  829. continue;
  830. MDString *S = dyn_cast<MDString>(MD->getOperand(0));
  831. if (!S)
  832. continue;
  833. // Return the operand node if MDString holds expected metadata.
  834. if (Name.equals(S->getString()))
  835. return MD;
  836. }
  837. // Loop property not found.
  838. return nullptr;
  839. }
  840. MDNode *llvm::findOptionMDForLoop(const Loop *TheLoop, StringRef Name) {
  841. return findOptionMDForLoopID(TheLoop->getLoopID(), Name);
  842. }
  843. bool llvm::isValidAsAccessGroup(MDNode *Node) {
  844. return Node->getNumOperands() == 0 && Node->isDistinct();
  845. }
  846. MDNode *llvm::makePostTransformationMetadata(LLVMContext &Context,
  847. MDNode *OrigLoopID,
  848. ArrayRef<StringRef> RemovePrefixes,
  849. ArrayRef<MDNode *> AddAttrs) {
  850. // First remove any existing loop metadata related to this transformation.
  851. SmallVector<Metadata *, 4> MDs;
  852. // Reserve first location for self reference to the LoopID metadata node.
  853. TempMDTuple TempNode = MDNode::getTemporary(Context, None);
  854. MDs.push_back(TempNode.get());
  855. // Remove metadata for the transformation that has been applied or that became
  856. // outdated.
  857. if (OrigLoopID) {
  858. for (unsigned i = 1, ie = OrigLoopID->getNumOperands(); i < ie; ++i) {
  859. bool IsVectorMetadata = false;
  860. Metadata *Op = OrigLoopID->getOperand(i);
  861. if (MDNode *MD = dyn_cast<MDNode>(Op)) {
  862. const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
  863. if (S)
  864. IsVectorMetadata =
  865. llvm::any_of(RemovePrefixes, [S](StringRef Prefix) -> bool {
  866. return S->getString().startswith(Prefix);
  867. });
  868. }
  869. if (!IsVectorMetadata)
  870. MDs.push_back(Op);
  871. }
  872. }
  873. // Add metadata to avoid reapplying a transformation, such as
  874. // llvm.loop.unroll.disable and llvm.loop.isvectorized.
  875. MDs.append(AddAttrs.begin(), AddAttrs.end());
  876. MDNode *NewLoopID = MDNode::getDistinct(Context, MDs);
  877. // Replace the temporary node with a self-reference.
  878. NewLoopID->replaceOperandWith(0, NewLoopID);
  879. return NewLoopID;
  880. }
  881. //===----------------------------------------------------------------------===//
  882. // LoopInfo implementation
  883. //
  884. char LoopInfoWrapperPass::ID = 0;
  885. INITIALIZE_PASS_BEGIN(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  886. true, true)
  887. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  888. INITIALIZE_PASS_END(LoopInfoWrapperPass, "loops", "Natural Loop Information",
  889. true, true)
  890. bool LoopInfoWrapperPass::runOnFunction(Function &) {
  891. releaseMemory();
  892. LI.analyze(getAnalysis<DominatorTreeWrapperPass>().getDomTree());
  893. return false;
  894. }
  895. void LoopInfoWrapperPass::verifyAnalysis() const {
  896. // LoopInfoWrapperPass is a FunctionPass, but verifying every loop in the
  897. // function each time verifyAnalysis is called is very expensive. The
  898. // -verify-loop-info option can enable this. In order to perform some
  899. // checking by default, LoopPass has been taught to call verifyLoop manually
  900. // during loop pass sequences.
  901. if (VerifyLoopInfo) {
  902. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  903. LI.verify(DT);
  904. }
  905. }
  906. void LoopInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  907. AU.setPreservesAll();
  908. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  909. }
  910. void LoopInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
  911. LI.print(OS);
  912. }
  913. PreservedAnalyses LoopVerifierPass::run(Function &F,
  914. FunctionAnalysisManager &AM) {
  915. LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
  916. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  917. LI.verify(DT);
  918. return PreservedAnalyses::all();
  919. }
  920. //===----------------------------------------------------------------------===//
  921. // LoopBlocksDFS implementation
  922. //
  923. /// Traverse the loop blocks and store the DFS result.
  924. /// Useful for clients that just want the final DFS result and don't need to
  925. /// visit blocks during the initial traversal.
  926. void LoopBlocksDFS::perform(LoopInfo *LI) {
  927. LoopBlocksTraversal Traversal(*this, LI);
  928. for (LoopBlocksTraversal::POTIterator POI = Traversal.begin(),
  929. POE = Traversal.end();
  930. POI != POE; ++POI)
  931. ;
  932. }