LICM.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass performs loop invariant code motion, attempting to remove as much
  11. // code from the body of a loop as possible. It does this by either hoisting
  12. // code into the preheader block, or by sinking code to the exit blocks if it is
  13. // safe. This pass also promotes must-aliased memory locations in the loop to
  14. // live in registers, thus hoisting and sinking "invariant" loads and stores.
  15. //
  16. // This pass uses alias analysis for two purposes:
  17. //
  18. // 1. Moving loop invariant loads and calls out of loops. If we can determine
  19. // that a load or call inside of a loop never aliases anything stored to,
  20. // we can hoist it or sink it like any other instruction.
  21. // 2. Scalar Promotion of Memory - If there is a store instruction inside of
  22. // the loop, we try to move the store to happen AFTER the loop instead of
  23. // inside of the loop. This can only happen if a few conditions are true:
  24. // A. The pointer stored through is loop invariant
  25. // B. There are no stores or loads in the loop which _may_ alias the
  26. // pointer. There are no calls in the loop which mod/ref the pointer.
  27. // If these conditions are true, we can promote the loads and stores in the
  28. // loop of the pointer to use a temporary alloca'd variable. We then use
  29. // the SSAUpdater to construct the appropriate SSA form for the value.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #include "llvm/Transforms/Scalar.h"
  33. #include "llvm/ADT/Statistic.h"
  34. #include "llvm/Analysis/AliasAnalysis.h"
  35. #include "llvm/Analysis/AliasSetTracker.h"
  36. #include "llvm/Analysis/ConstantFolding.h"
  37. #include "llvm/Analysis/LoopInfo.h"
  38. #include "llvm/Analysis/LoopPass.h"
  39. #include "llvm/Analysis/ScalarEvolution.h"
  40. #include "llvm/Analysis/TargetLibraryInfo.h"
  41. #include "llvm/Analysis/ValueTracking.h"
  42. #include "llvm/IR/CFG.h"
  43. #include "llvm/IR/Constants.h"
  44. #include "llvm/IR/DataLayout.h"
  45. #include "llvm/IR/DerivedTypes.h"
  46. #include "llvm/IR/Dominators.h"
  47. #include "llvm/IR/Instructions.h"
  48. #include "llvm/IR/IntrinsicInst.h"
  49. #include "llvm/IR/LLVMContext.h"
  50. #include "llvm/IR/Metadata.h"
  51. #include "llvm/IR/PredIteratorCache.h"
  52. #include "llvm/Support/CommandLine.h"
  53. #include "llvm/Support/Debug.h"
  54. #include "llvm/Support/raw_ostream.h"
  55. #include "llvm/Transforms/Utils/Local.h"
  56. #include "llvm/Transforms/Utils/LoopUtils.h"
  57. #include "llvm/Transforms/Utils/SSAUpdater.h"
  58. #include <algorithm>
  59. using namespace llvm;
  60. #define DEBUG_TYPE "licm"
  61. STATISTIC(NumSunk , "Number of instructions sunk out of loop");
  62. STATISTIC(NumHoisted , "Number of instructions hoisted out of loop");
  63. STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
  64. STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
  65. STATISTIC(NumPromoted , "Number of memory locations promoted to registers");
  66. static cl::opt<bool>
  67. DisablePromotion("disable-licm-promotion", cl::Hidden,
  68. cl::desc("Disable memory promotion in LICM pass"));
  69. static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
  70. static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop);
  71. static bool hoist(Instruction &I, BasicBlock *Preheader);
  72. static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
  73. const Loop *CurLoop, AliasSetTracker *CurAST );
  74. static bool isGuaranteedToExecute(const Instruction &Inst,
  75. const DominatorTree *DT,
  76. const Loop *CurLoop,
  77. const LICMSafetyInfo *SafetyInfo);
  78. static bool isSafeToExecuteUnconditionally(const Instruction &Inst,
  79. const DominatorTree *DT,
  80. const TargetLibraryInfo *TLI,
  81. const Loop *CurLoop,
  82. const LICMSafetyInfo *SafetyInfo,
  83. const Instruction *CtxI = nullptr);
  84. static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
  85. const AAMDNodes &AAInfo,
  86. AliasSetTracker *CurAST);
  87. static Instruction *CloneInstructionInExitBlock(const Instruction &I,
  88. BasicBlock &ExitBlock,
  89. PHINode &PN,
  90. const LoopInfo *LI);
  91. static bool canSinkOrHoistInst(Instruction &I, AliasAnalysis *AA,
  92. DominatorTree *DT, TargetLibraryInfo *TLI,
  93. Loop *CurLoop, AliasSetTracker *CurAST,
  94. LICMSafetyInfo *SafetyInfo);
  95. namespace {
  96. struct LICM : public LoopPass {
  97. static char ID; // Pass identification, replacement for typeid
  98. LICM() : LoopPass(ID) {
  99. initializeLICMPass(*PassRegistry::getPassRegistry());
  100. }
  101. bool runOnLoop(Loop *L, LPPassManager &LPM) override;
  102. /// This transformation requires natural loop information & requires that
  103. /// loop preheaders be inserted into the CFG...
  104. ///
  105. void getAnalysisUsage(AnalysisUsage &AU) const override {
  106. AU.setPreservesCFG();
  107. AU.addRequired<DominatorTreeWrapperPass>();
  108. AU.addRequired<LoopInfoWrapperPass>();
  109. AU.addRequiredID(LoopSimplifyID);
  110. AU.addPreservedID(LoopSimplifyID);
  111. AU.addRequiredID(LCSSAID);
  112. AU.addPreservedID(LCSSAID);
  113. AU.addRequired<AliasAnalysis>();
  114. AU.addPreserved<AliasAnalysis>();
  115. AU.addPreserved<ScalarEvolution>();
  116. AU.addRequired<TargetLibraryInfoWrapperPass>();
  117. }
  118. using llvm::Pass::doFinalization;
  119. bool doFinalization() override {
  120. assert(LoopToAliasSetMap.empty() && "Didn't free loop alias sets");
  121. return false;
  122. }
  123. private:
  124. AliasAnalysis *AA; // Current AliasAnalysis information
  125. LoopInfo *LI; // Current LoopInfo
  126. DominatorTree *DT; // Dominator Tree for the current Loop.
  127. TargetLibraryInfo *TLI; // TargetLibraryInfo for constant folding.
  128. // State that is updated as we process loops.
  129. bool Changed; // Set to true when we change anything.
  130. BasicBlock *Preheader; // The preheader block of the current loop...
  131. Loop *CurLoop; // The current loop we are working on...
  132. AliasSetTracker *CurAST; // AliasSet information for the current loop...
  133. DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap;
  134. /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
  135. void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
  136. Loop *L) override;
  137. /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
  138. /// set.
  139. void deleteAnalysisValue(Value *V, Loop *L) override;
  140. /// Simple Analysis hook. Delete loop L from alias set map.
  141. void deleteAnalysisLoop(Loop *L) override;
  142. };
  143. }
  144. char LICM::ID = 0;
  145. INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false)
  146. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  147. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  148. INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
  149. INITIALIZE_PASS_DEPENDENCY(LCSSA)
  150. INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
  151. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  152. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  153. INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false)
  154. Pass *llvm::createLICMPass() { return new LICM(); }
  155. /// Hoist expressions out of the specified loop. Note, alias info for inner
  156. /// loop is not preserved so it is not a good idea to run LICM multiple
  157. /// times on one loop.
  158. ///
  159. bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
  160. if (skipOptnoneFunction(L))
  161. return false;
  162. Changed = false;
  163. // Get our Loop and Alias Analysis information...
  164. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  165. AA = &getAnalysis<AliasAnalysis>();
  166. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  167. TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  168. assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
  169. CurAST = new AliasSetTracker(*AA);
  170. // Collect Alias info from subloops.
  171. for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end();
  172. LoopItr != LoopItrE; ++LoopItr) {
  173. Loop *InnerL = *LoopItr;
  174. AliasSetTracker *InnerAST = LoopToAliasSetMap[InnerL];
  175. assert(InnerAST && "Where is my AST?");
  176. // What if InnerLoop was modified by other passes ?
  177. CurAST->add(*InnerAST);
  178. // Once we've incorporated the inner loop's AST into ours, we don't need the
  179. // subloop's anymore.
  180. delete InnerAST;
  181. LoopToAliasSetMap.erase(InnerL);
  182. }
  183. CurLoop = L;
  184. // Get the preheader block to move instructions into...
  185. Preheader = L->getLoopPreheader();
  186. // Loop over the body of this loop, looking for calls, invokes, and stores.
  187. // Because subloops have already been incorporated into AST, we skip blocks in
  188. // subloops.
  189. //
  190. for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
  191. I != E; ++I) {
  192. BasicBlock *BB = *I;
  193. if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops.
  194. CurAST->add(*BB); // Incorporate the specified basic block
  195. }
  196. // Compute loop safety information.
  197. LICMSafetyInfo SafetyInfo;
  198. computeLICMSafetyInfo(&SafetyInfo, CurLoop);
  199. // We want to visit all of the instructions in this loop... that are not parts
  200. // of our subloops (they have already had their invariants hoisted out of
  201. // their loop, into this loop, so there is no need to process the BODIES of
  202. // the subloops).
  203. //
  204. // Traverse the body of the loop in depth first order on the dominator tree so
  205. // that we are guaranteed to see definitions before we see uses. This allows
  206. // us to sink instructions in one pass, without iteration. After sinking
  207. // instructions, we perform another pass to hoist them out of the loop.
  208. //
  209. if (L->hasDedicatedExits())
  210. Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, CurLoop,
  211. CurAST, &SafetyInfo);
  212. if (Preheader)
  213. Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI,
  214. CurLoop, CurAST, &SafetyInfo);
  215. // Now that all loop invariants have been removed from the loop, promote any
  216. // memory references to scalars that we can.
  217. if (!DisablePromotion && (Preheader || L->hasDedicatedExits())) {
  218. SmallVector<BasicBlock *, 8> ExitBlocks;
  219. SmallVector<Instruction *, 8> InsertPts;
  220. PredIteratorCache PIC;
  221. // Loop over all of the alias sets in the tracker object.
  222. for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
  223. I != E; ++I)
  224. Changed |= promoteLoopAccessesToScalars(*I, ExitBlocks, InsertPts,
  225. PIC, LI, DT, CurLoop,
  226. CurAST, &SafetyInfo);
  227. // Once we have promoted values across the loop body we have to recursively
  228. // reform LCSSA as any nested loop may now have values defined within the
  229. // loop used in the outer loop.
  230. // FIXME: This is really heavy handed. It would be a bit better to use an
  231. // SSAUpdater strategy during promotion that was LCSSA aware and reformed
  232. // it as it went.
  233. if (Changed)
  234. formLCSSARecursively(*L, *DT, LI,
  235. getAnalysisIfAvailable<ScalarEvolution>());
  236. }
  237. // Check that neither this loop nor its parent have had LCSSA broken. LICM is
  238. // specifically moving instructions across the loop boundary and so it is
  239. // especially in need of sanity checking here.
  240. assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!");
  241. assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&
  242. "Parent loop not left in LCSSA form after LICM!");
  243. // Clear out loops state information for the next iteration
  244. CurLoop = nullptr;
  245. Preheader = nullptr;
  246. // If this loop is nested inside of another one, save the alias information
  247. // for when we process the outer loop.
  248. if (L->getParentLoop())
  249. LoopToAliasSetMap[L] = CurAST;
  250. else
  251. delete CurAST;
  252. return Changed;
  253. }
  254. /// Walk the specified region of the CFG (defined by all blocks dominated by
  255. /// the specified block, and that are in the current loop) in reverse depth
  256. /// first order w.r.t the DominatorTree. This allows us to visit uses before
  257. /// definitions, allowing us to sink a loop body in one pass without iteration.
  258. ///
  259. bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
  260. DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
  261. AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
  262. // Verify inputs.
  263. assert(N != nullptr && AA != nullptr && LI != nullptr &&
  264. DT != nullptr && CurLoop != nullptr && CurAST != nullptr &&
  265. SafetyInfo != nullptr && "Unexpected input to sinkRegion");
  266. // Set changed as false.
  267. bool Changed = false;
  268. // Get basic block
  269. BasicBlock *BB = N->getBlock();
  270. // If this subregion is not in the top level loop at all, exit.
  271. if (!CurLoop->contains(BB)) return Changed;
  272. // We are processing blocks in reverse dfo, so process children first.
  273. const std::vector<DomTreeNode*> &Children = N->getChildren();
  274. for (unsigned i = 0, e = Children.size(); i != e; ++i)
  275. Changed |=
  276. sinkRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
  277. // Only need to process the contents of this block if it is not part of a
  278. // subloop (which would already have been processed).
  279. if (inSubLoop(BB,CurLoop,LI)) return Changed;
  280. for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) {
  281. Instruction &I = *--II;
  282. // If the instruction is dead, we would try to sink it because it isn't used
  283. // in the loop, instead, just delete it.
  284. if (isInstructionTriviallyDead(&I, TLI)) {
  285. DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n');
  286. ++II;
  287. CurAST->deleteValue(&I);
  288. I.eraseFromParent();
  289. Changed = true;
  290. continue;
  291. }
  292. // Check to see if we can sink this instruction to the exit blocks
  293. // of the loop. We can do this if the all users of the instruction are
  294. // outside of the loop. In this case, it doesn't even matter if the
  295. // operands of the instruction are loop invariant.
  296. //
  297. if (isNotUsedInLoop(I, CurLoop) &&
  298. canSinkOrHoistInst(I, AA, DT, TLI, CurLoop, CurAST, SafetyInfo)) {
  299. ++II;
  300. Changed |= sink(I, LI, DT, CurLoop, CurAST);
  301. }
  302. }
  303. return Changed;
  304. }
  305. /// Walk the specified region of the CFG (defined by all blocks dominated by
  306. /// the specified block, and that are in the current loop) in depth first
  307. /// order w.r.t the DominatorTree. This allows us to visit definitions before
  308. /// uses, allowing us to hoist a loop body in one pass without iteration.
  309. ///
  310. bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
  311. DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
  312. AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
  313. // Verify inputs.
  314. assert(N != nullptr && AA != nullptr && LI != nullptr &&
  315. DT != nullptr && CurLoop != nullptr && CurAST != nullptr &&
  316. SafetyInfo != nullptr && "Unexpected input to hoistRegion");
  317. // Set changed as false.
  318. bool Changed = false;
  319. // Get basic block
  320. BasicBlock *BB = N->getBlock();
  321. // If this subregion is not in the top level loop at all, exit.
  322. if (!CurLoop->contains(BB)) return Changed;
  323. // Only need to process the contents of this block if it is not part of a
  324. // subloop (which would already have been processed).
  325. if (!inSubLoop(BB, CurLoop, LI))
  326. for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
  327. Instruction &I = *II++;
  328. // Try constant folding this instruction. If all the operands are
  329. // constants, it is technically hoistable, but it would be better to just
  330. // fold it.
  331. if (Constant *C = ConstantFoldInstruction(
  332. &I, I.getModule()->getDataLayout(), TLI)) {
  333. DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n');
  334. CurAST->copyValue(&I, C);
  335. CurAST->deleteValue(&I);
  336. I.replaceAllUsesWith(C);
  337. I.eraseFromParent();
  338. continue;
  339. }
  340. // Try hoisting the instruction out to the preheader. We can only do this
  341. // if all of the operands of the instruction are loop invariant and if it
  342. // is safe to hoist the instruction.
  343. //
  344. if (CurLoop->hasLoopInvariantOperands(&I) &&
  345. canSinkOrHoistInst(I, AA, DT, TLI, CurLoop, CurAST, SafetyInfo) &&
  346. isSafeToExecuteUnconditionally(I, DT, TLI, CurLoop, SafetyInfo,
  347. CurLoop->getLoopPreheader()->getTerminator()))
  348. Changed |= hoist(I, CurLoop->getLoopPreheader());
  349. }
  350. const std::vector<DomTreeNode*> &Children = N->getChildren();
  351. for (unsigned i = 0, e = Children.size(); i != e; ++i)
  352. Changed |=
  353. hoistRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
  354. return Changed;
  355. }
  356. /// Computes loop safety information, checks loop body & header
  357. /// for the possiblity of may throw exception.
  358. ///
  359. void llvm::computeLICMSafetyInfo(LICMSafetyInfo * SafetyInfo, Loop * CurLoop) {
  360. assert(CurLoop != nullptr && "CurLoop cant be null");
  361. BasicBlock *Header = CurLoop->getHeader();
  362. // Setting default safety values.
  363. SafetyInfo->MayThrow = false;
  364. SafetyInfo->HeaderMayThrow = false;
  365. // Iterate over header and compute dafety info.
  366. for (BasicBlock::iterator I = Header->begin(), E = Header->end();
  367. (I != E) && !SafetyInfo->HeaderMayThrow; ++I)
  368. SafetyInfo->HeaderMayThrow |= I->mayThrow();
  369. SafetyInfo->MayThrow = SafetyInfo->HeaderMayThrow;
  370. // Iterate over loop instructions and compute safety info.
  371. for (Loop::block_iterator BB = CurLoop->block_begin(),
  372. BBE = CurLoop->block_end(); (BB != BBE) && !SafetyInfo->MayThrow ; ++BB)
  373. for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
  374. (I != E) && !SafetyInfo->MayThrow; ++I)
  375. SafetyInfo->MayThrow |= I->mayThrow();
  376. }
  377. /// canSinkOrHoistInst - Return true if the hoister and sinker can handle this
  378. /// instruction.
  379. ///
  380. bool canSinkOrHoistInst(Instruction &I, AliasAnalysis *AA, DominatorTree *DT,
  381. TargetLibraryInfo *TLI, Loop *CurLoop,
  382. AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
  383. // Loads have extra constraints we have to verify before we can hoist them.
  384. if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
  385. if (!LI->isUnordered())
  386. return false; // Don't hoist volatile/atomic loads!
  387. // Loads from constant memory are always safe to move, even if they end up
  388. // in the same alias set as something that ends up being modified.
  389. if (AA->pointsToConstantMemory(LI->getOperand(0)))
  390. return true;
  391. if (LI->getMetadata(LLVMContext::MD_invariant_load))
  392. return true;
  393. // Don't hoist loads which have may-aliased stores in loop.
  394. uint64_t Size = 0;
  395. if (LI->getType()->isSized())
  396. Size = AA->getTypeStoreSize(LI->getType());
  397. AAMDNodes AAInfo;
  398. LI->getAAMetadata(AAInfo);
  399. return !pointerInvalidatedByLoop(LI->getOperand(0), Size, AAInfo, CurAST);
  400. } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  401. // Don't sink or hoist dbg info; it's legal, but not useful.
  402. if (isa<DbgInfoIntrinsic>(I))
  403. return false;
  404. // Handle simple cases by querying alias analysis.
  405. AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
  406. if (Behavior == AliasAnalysis::DoesNotAccessMemory)
  407. return true;
  408. if (AliasAnalysis::onlyReadsMemory(Behavior)) {
  409. // If this call only reads from memory and there are no writes to memory
  410. // in the loop, we can hoist or sink the call as appropriate.
  411. bool FoundMod = false;
  412. for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
  413. I != E; ++I) {
  414. AliasSet &AS = *I;
  415. if (!AS.isForwardingAliasSet() && AS.isMod()) {
  416. FoundMod = true;
  417. break;
  418. }
  419. }
  420. if (!FoundMod) return true;
  421. }
  422. // FIXME: This should use mod/ref information to see if we can hoist or
  423. // sink the call.
  424. return false;
  425. }
  426. // Only these instructions are hoistable/sinkable.
  427. if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) &&
  428. !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) &&
  429. !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) &&
  430. !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) &&
  431. !isa<InsertValueInst>(I))
  432. return false;
  433. // TODO: Plumb the context instruction through to make hoisting and sinking
  434. // more powerful. Hoisting of loads already works due to the special casing
  435. // above.
  436. return isSafeToExecuteUnconditionally(I, DT, TLI, CurLoop, SafetyInfo,
  437. nullptr);
  438. }
  439. /// Returns true if a PHINode is a trivially replaceable with an
  440. /// Instruction.
  441. /// This is true when all incoming values are that instruction.
  442. /// This pattern occurs most often with LCSSA PHI nodes.
  443. ///
  444. static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I) {
  445. for (const Value *IncValue : PN.incoming_values())
  446. if (IncValue != &I)
  447. return false;
  448. return true;
  449. }
  450. /// Return true if the only users of this instruction are outside of
  451. /// the loop. If this is true, we can sink the instruction to the exit
  452. /// blocks of the loop.
  453. ///
  454. static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop) {
  455. for (const User *U : I.users()) {
  456. const Instruction *UI = cast<Instruction>(U);
  457. if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
  458. // A PHI node where all of the incoming values are this instruction are
  459. // special -- they can just be RAUW'ed with the instruction and thus
  460. // don't require a use in the predecessor. This is a particular important
  461. // special case because it is the pattern found in LCSSA form.
  462. if (isTriviallyReplacablePHI(*PN, I)) {
  463. if (CurLoop->contains(PN))
  464. return false;
  465. else
  466. continue;
  467. }
  468. // Otherwise, PHI node uses occur in predecessor blocks if the incoming
  469. // values. Check for such a use being inside the loop.
  470. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  471. if (PN->getIncomingValue(i) == &I)
  472. if (CurLoop->contains(PN->getIncomingBlock(i)))
  473. return false;
  474. continue;
  475. }
  476. if (CurLoop->contains(UI))
  477. return false;
  478. }
  479. return true;
  480. }
  481. static Instruction *CloneInstructionInExitBlock(const Instruction &I,
  482. BasicBlock &ExitBlock,
  483. PHINode &PN,
  484. const LoopInfo *LI) {
  485. Instruction *New = I.clone();
  486. ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
  487. if (!I.getName().empty()) New->setName(I.getName() + ".le");
  488. // Build LCSSA PHI nodes for any in-loop operands. Note that this is
  489. // particularly cheap because we can rip off the PHI node that we're
  490. // replacing for the number and blocks of the predecessors.
  491. // OPT: If this shows up in a profile, we can instead finish sinking all
  492. // invariant instructions, and then walk their operands to re-establish
  493. // LCSSA. That will eliminate creating PHI nodes just to nuke them when
  494. // sinking bottom-up.
  495. for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE;
  496. ++OI)
  497. if (Instruction *OInst = dyn_cast<Instruction>(*OI))
  498. if (Loop *OLoop = LI->getLoopFor(OInst->getParent()))
  499. if (!OLoop->contains(&PN)) {
  500. PHINode *OpPN =
  501. PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
  502. OInst->getName() + ".lcssa", ExitBlock.begin());
  503. for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
  504. OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
  505. *OI = OpPN;
  506. }
  507. return New;
  508. }
  509. /// When an instruction is found to only be used outside of the loop, this
  510. /// function moves it to the exit blocks and patches up SSA form as needed.
  511. /// This method is guaranteed to remove the original instruction from its
  512. /// position, and may either delete it or move it to outside of the loop.
  513. ///
  514. static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
  515. const Loop *CurLoop, AliasSetTracker *CurAST ) {
  516. DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
  517. bool Changed = false;
  518. if (isa<LoadInst>(I)) ++NumMovedLoads;
  519. else if (isa<CallInst>(I)) ++NumMovedCalls;
  520. ++NumSunk;
  521. Changed = true;
  522. #ifndef NDEBUG
  523. SmallVector<BasicBlock *, 32> ExitBlocks;
  524. CurLoop->getUniqueExitBlocks(ExitBlocks);
  525. SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
  526. ExitBlocks.end());
  527. #endif
  528. // Clones of this instruction. Don't create more than one per exit block!
  529. SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies;
  530. // If this instruction is only used outside of the loop, then all users are
  531. // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
  532. // the instruction.
  533. while (!I.use_empty()) {
  534. Value::user_iterator UI = I.user_begin();
  535. auto *User = cast<Instruction>(*UI);
  536. if (!DT->isReachableFromEntry(User->getParent())) {
  537. User->replaceUsesOfWith(&I, UndefValue::get(I.getType()));
  538. continue;
  539. }
  540. // The user must be a PHI node.
  541. PHINode *PN = cast<PHINode>(User);
  542. // Surprisingly, instructions can be used outside of loops without any
  543. // exits. This can only happen in PHI nodes if the incoming block is
  544. // unreachable.
  545. Use &U = UI.getUse();
  546. BasicBlock *BB = PN->getIncomingBlock(U);
  547. if (!DT->isReachableFromEntry(BB)) {
  548. U = UndefValue::get(I.getType());
  549. continue;
  550. }
  551. BasicBlock *ExitBlock = PN->getParent();
  552. assert(ExitBlockSet.count(ExitBlock) &&
  553. "The LCSSA PHI is not in an exit block!");
  554. Instruction *New;
  555. auto It = SunkCopies.find(ExitBlock);
  556. if (It != SunkCopies.end())
  557. New = It->second;
  558. else
  559. New = SunkCopies[ExitBlock] =
  560. CloneInstructionInExitBlock(I, *ExitBlock, *PN, LI);
  561. PN->replaceAllUsesWith(New);
  562. PN->eraseFromParent();
  563. }
  564. CurAST->deleteValue(&I);
  565. I.eraseFromParent();
  566. return Changed;
  567. }
  568. /// When an instruction is found to only use loop invariant operands that
  569. /// is safe to hoist, this instruction is called to do the dirty work.
  570. ///
  571. static bool hoist(Instruction &I, BasicBlock *Preheader) {
  572. DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": "
  573. << I << "\n");
  574. // Move the new node to the Preheader, before its terminator.
  575. I.moveBefore(Preheader->getTerminator());
  576. if (isa<LoadInst>(I)) ++NumMovedLoads;
  577. else if (isa<CallInst>(I)) ++NumMovedCalls;
  578. ++NumHoisted;
  579. return true;
  580. }
  581. /// Only sink or hoist an instruction if it is not a trapping instruction,
  582. /// or if the instruction is known not to trap when moved to the preheader.
  583. /// or if it is a trapping instruction and is guaranteed to execute.
  584. static bool isSafeToExecuteUnconditionally(const Instruction &Inst,
  585. const DominatorTree *DT,
  586. const TargetLibraryInfo *TLI,
  587. const Loop *CurLoop,
  588. const LICMSafetyInfo *SafetyInfo,
  589. const Instruction *CtxI) {
  590. if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT, TLI))
  591. return true;
  592. return isGuaranteedToExecute(Inst, DT, CurLoop, SafetyInfo);
  593. }
  594. static bool isGuaranteedToExecute(const Instruction &Inst,
  595. const DominatorTree *DT,
  596. const Loop *CurLoop,
  597. const LICMSafetyInfo * SafetyInfo) {
  598. // We have to check to make sure that the instruction dominates all
  599. // of the exit blocks. If it doesn't, then there is a path out of the loop
  600. // which does not execute this instruction, so we can't hoist it.
  601. // If the instruction is in the header block for the loop (which is very
  602. // common), it is always guaranteed to dominate the exit blocks. Since this
  603. // is a common case, and can save some work, check it now.
  604. if (Inst.getParent() == CurLoop->getHeader())
  605. // If there's a throw in the header block, we can't guarantee we'll reach
  606. // Inst.
  607. return !SafetyInfo->HeaderMayThrow;
  608. // Somewhere in this loop there is an instruction which may throw and make us
  609. // exit the loop.
  610. if (SafetyInfo->MayThrow)
  611. return false;
  612. // Get the exit blocks for the current loop.
  613. SmallVector<BasicBlock*, 8> ExitBlocks;
  614. CurLoop->getExitBlocks(ExitBlocks);
  615. // Verify that the block dominates each of the exit blocks of the loop.
  616. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
  617. if (!DT->dominates(Inst.getParent(), ExitBlocks[i]))
  618. return false;
  619. // As a degenerate case, if the loop is statically infinite then we haven't
  620. // proven anything since there are no exit blocks.
  621. if (ExitBlocks.empty())
  622. return false;
  623. return true;
  624. }
  625. namespace {
  626. class LoopPromoter : public LoadAndStorePromoter {
  627. Value *SomePtr; // Designated pointer to store to.
  628. SmallPtrSetImpl<Value*> &PointerMustAliases;
  629. SmallVectorImpl<BasicBlock*> &LoopExitBlocks;
  630. SmallVectorImpl<Instruction*> &LoopInsertPts;
  631. PredIteratorCache &PredCache;
  632. AliasSetTracker &AST;
  633. LoopInfo &LI;
  634. DebugLoc DL;
  635. int Alignment;
  636. AAMDNodes AATags;
  637. Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
  638. if (Instruction *I = dyn_cast<Instruction>(V))
  639. if (Loop *L = LI.getLoopFor(I->getParent()))
  640. if (!L->contains(BB)) {
  641. // We need to create an LCSSA PHI node for the incoming value and
  642. // store that.
  643. PHINode *PN = PHINode::Create(
  644. I->getType(), PredCache.size(BB),
  645. I->getName() + ".lcssa", BB->begin());
  646. for (BasicBlock *Pred : PredCache.get(BB))
  647. PN->addIncoming(I, Pred);
  648. return PN;
  649. }
  650. return V;
  651. }
  652. public:
  653. LoopPromoter(Value *SP,
  654. ArrayRef<const Instruction *> Insts,
  655. SSAUpdater &S, SmallPtrSetImpl<Value *> &PMA,
  656. SmallVectorImpl<BasicBlock *> &LEB,
  657. SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC,
  658. AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
  659. const AAMDNodes &AATags)
  660. : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
  661. LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
  662. LI(li), DL(dl), Alignment(alignment), AATags(AATags) {}
  663. bool isInstInList(Instruction *I,
  664. const SmallVectorImpl<Instruction*> &) const override {
  665. Value *Ptr;
  666. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  667. Ptr = LI->getOperand(0);
  668. else
  669. Ptr = cast<StoreInst>(I)->getPointerOperand();
  670. return PointerMustAliases.count(Ptr);
  671. }
  672. void doExtraRewritesBeforeFinalDeletion() const override {
  673. // Insert stores after in the loop exit blocks. Each exit block gets a
  674. // store of the live-out values that feed them. Since we've already told
  675. // the SSA updater about the defs in the loop and the preheader
  676. // definition, it is all set and we can start using it.
  677. for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
  678. BasicBlock *ExitBlock = LoopExitBlocks[i];
  679. Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
  680. LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
  681. Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
  682. Instruction *InsertPos = LoopInsertPts[i];
  683. StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
  684. NewSI->setAlignment(Alignment);
  685. NewSI->setDebugLoc(DL);
  686. if (AATags) NewSI->setAAMetadata(AATags);
  687. }
  688. }
  689. void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
  690. // Update alias analysis.
  691. AST.copyValue(LI, V);
  692. }
  693. void instructionDeleted(Instruction *I) const override {
  694. AST.deleteValue(I);
  695. }
  696. };
  697. } // end anon namespace
  698. /// Try to promote memory values to scalars by sinking stores out of the
  699. /// loop and moving loads to before the loop. We do this by looping over
  700. /// the stores in the loop, looking for stores to Must pointers which are
  701. /// loop invariant.
  702. ///
  703. bool llvm::promoteLoopAccessesToScalars(AliasSet &AS,
  704. SmallVectorImpl<BasicBlock*>&ExitBlocks,
  705. SmallVectorImpl<Instruction*>&InsertPts,
  706. PredIteratorCache &PIC, LoopInfo *LI,
  707. DominatorTree *DT, Loop *CurLoop,
  708. AliasSetTracker *CurAST,
  709. LICMSafetyInfo * SafetyInfo) {
  710. // Verify inputs.
  711. assert(LI != nullptr && DT != nullptr &&
  712. CurLoop != nullptr && CurAST != nullptr &&
  713. SafetyInfo != nullptr &&
  714. "Unexpected Input to promoteLoopAccessesToScalars");
  715. // Initially set Changed status to false.
  716. bool Changed = false;
  717. // We can promote this alias set if it has a store, if it is a "Must" alias
  718. // set, if the pointer is loop invariant, and if we are not eliminating any
  719. // volatile loads or stores.
  720. if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
  721. AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue()))
  722. return Changed;
  723. assert(!AS.empty() &&
  724. "Must alias set should have at least one pointer element in it!");
  725. Value *SomePtr = AS.begin()->getValue();
  726. BasicBlock * Preheader = CurLoop->getLoopPreheader();
  727. // It isn't safe to promote a load/store from the loop if the load/store is
  728. // conditional. For example, turning:
  729. //
  730. // for () { if (c) *P += 1; }
  731. //
  732. // into:
  733. //
  734. // tmp = *P; for () { if (c) tmp +=1; } *P = tmp;
  735. //
  736. // is not safe, because *P may only be valid to access if 'c' is true.
  737. //
  738. // It is safe to promote P if all uses are direct load/stores and if at
  739. // least one is guaranteed to be executed.
  740. bool GuaranteedToExecute = false;
  741. SmallVector<Instruction*, 64> LoopUses;
  742. SmallPtrSet<Value*, 4> PointerMustAliases;
  743. // We start with an alignment of one and try to find instructions that allow
  744. // us to prove better alignment.
  745. unsigned Alignment = 1;
  746. AAMDNodes AATags;
  747. bool HasDedicatedExits = CurLoop->hasDedicatedExits();
  748. // Check that all of the pointers in the alias set have the same type. We
  749. // cannot (yet) promote a memory location that is loaded and stored in
  750. // different sizes. While we are at it, collect alignment and AA info.
  751. for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
  752. Value *ASIV = ASI->getValue();
  753. PointerMustAliases.insert(ASIV);
  754. // Check that all of the pointers in the alias set have the same type. We
  755. // cannot (yet) promote a memory location that is loaded and stored in
  756. // different sizes.
  757. if (SomePtr->getType() != ASIV->getType())
  758. return Changed;
  759. for (User *U : ASIV->users()) {
  760. // Ignore instructions that are outside the loop.
  761. Instruction *UI = dyn_cast<Instruction>(U);
  762. if (!UI || !CurLoop->contains(UI))
  763. continue;
  764. // If there is an non-load/store instruction in the loop, we can't promote
  765. // it.
  766. if (const LoadInst *load = dyn_cast<LoadInst>(UI)) {
  767. assert(!load->isVolatile() && "AST broken");
  768. if (!load->isSimple())
  769. return Changed;
  770. } else if (const StoreInst *store = dyn_cast<StoreInst>(UI)) {
  771. // Stores *of* the pointer are not interesting, only stores *to* the
  772. // pointer.
  773. if (UI->getOperand(1) != ASIV)
  774. continue;
  775. assert(!store->isVolatile() && "AST broken");
  776. if (!store->isSimple())
  777. return Changed;
  778. // Don't sink stores from loops without dedicated block exits. Exits
  779. // containing indirect branches are not transformed by loop simplify,
  780. // make sure we catch that. An additional load may be generated in the
  781. // preheader for SSA updater, so also avoid sinking when no preheader
  782. // is available.
  783. if (!HasDedicatedExits || !Preheader)
  784. return Changed;
  785. // Note that we only check GuaranteedToExecute inside the store case
  786. // so that we do not introduce stores where they did not exist before
  787. // (which would break the LLVM concurrency model).
  788. // If the alignment of this instruction allows us to specify a more
  789. // restrictive (and performant) alignment and if we are sure this
  790. // instruction will be executed, update the alignment.
  791. // Larger is better, with the exception of 0 being the best alignment.
  792. unsigned InstAlignment = store->getAlignment();
  793. if ((InstAlignment > Alignment || InstAlignment == 0) && Alignment != 0)
  794. if (isGuaranteedToExecute(*UI, DT, CurLoop, SafetyInfo)) {
  795. GuaranteedToExecute = true;
  796. Alignment = InstAlignment;
  797. }
  798. if (!GuaranteedToExecute)
  799. GuaranteedToExecute = isGuaranteedToExecute(*UI, DT,
  800. CurLoop, SafetyInfo);
  801. } else
  802. return Changed; // Not a load or store.
  803. // Merge the AA tags.
  804. if (LoopUses.empty()) {
  805. // On the first load/store, just take its AA tags.
  806. UI->getAAMetadata(AATags);
  807. } else if (AATags) {
  808. UI->getAAMetadata(AATags, /* Merge = */ true);
  809. }
  810. LoopUses.push_back(UI);
  811. }
  812. }
  813. // If there isn't a guaranteed-to-execute instruction, we can't promote.
  814. if (!GuaranteedToExecute)
  815. return Changed;
  816. // Otherwise, this is safe to promote, lets do it!
  817. DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " <<*SomePtr<<'\n');
  818. Changed = true;
  819. ++NumPromoted;
  820. // Grab a debug location for the inserted loads/stores; given that the
  821. // inserted loads/stores have little relation to the original loads/stores,
  822. // this code just arbitrarily picks a location from one, since any debug
  823. // location is better than none.
  824. DebugLoc DL = LoopUses[0]->getDebugLoc();
  825. // Figure out the loop exits and their insertion points, if this is the
  826. // first promotion.
  827. if (ExitBlocks.empty()) {
  828. CurLoop->getUniqueExitBlocks(ExitBlocks);
  829. InsertPts.resize(ExitBlocks.size());
  830. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
  831. InsertPts[i] = ExitBlocks[i]->getFirstInsertionPt();
  832. }
  833. // We use the SSAUpdater interface to insert phi nodes as required.
  834. SmallVector<PHINode*, 16> NewPHIs;
  835. SSAUpdater SSA(&NewPHIs);
  836. LoopPromoter Promoter(SomePtr, LoopUses, SSA,
  837. PointerMustAliases, ExitBlocks,
  838. InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
  839. // Set up the preheader to have a definition of the value. It is the live-out
  840. // value from the preheader that uses in the loop will use.
  841. LoadInst *PreheaderLoad =
  842. new LoadInst(SomePtr, SomePtr->getName()+".promoted",
  843. Preheader->getTerminator());
  844. PreheaderLoad->setAlignment(Alignment);
  845. PreheaderLoad->setDebugLoc(DL);
  846. if (AATags) PreheaderLoad->setAAMetadata(AATags);
  847. SSA.AddAvailableValue(Preheader, PreheaderLoad);
  848. // Rewrite all the loads in the loop and remember all the definitions from
  849. // stores in the loop.
  850. Promoter.run(LoopUses);
  851. // If the SSAUpdater didn't use the load in the preheader, just zap it now.
  852. if (PreheaderLoad->use_empty())
  853. PreheaderLoad->eraseFromParent();
  854. return Changed;
  855. }
  856. /// Simple Analysis hook. Clone alias set info.
  857. ///
  858. void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) {
  859. AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
  860. if (!AST)
  861. return;
  862. AST->copyValue(From, To);
  863. }
  864. /// Simple Analysis hook. Delete value V from alias set
  865. ///
  866. void LICM::deleteAnalysisValue(Value *V, Loop *L) {
  867. AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
  868. if (!AST)
  869. return;
  870. AST->deleteValue(V);
  871. }
  872. /// Simple Analysis hook. Delete value L from alias set map.
  873. ///
  874. void LICM::deleteAnalysisLoop(Loop *L) {
  875. AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
  876. if (!AST)
  877. return;
  878. delete AST;
  879. LoopToAliasSetMap.erase(L);
  880. }
  881. /// Return true if the body of this loop may store into the memory
  882. /// location pointed to by V.
  883. ///
  884. static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
  885. const AAMDNodes &AAInfo,
  886. AliasSetTracker *CurAST) {
  887. // Check to see if any of the basic blocks in CurLoop invalidate *V.
  888. return CurAST->getAliasSetForPointer(V, Size, AAInfo).isMod();
  889. }
  890. /// Little predicate that returns true if the specified basic block is in
  891. /// a subloop of the current one, not the current one itself.
  892. ///
  893. static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) {
  894. assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
  895. return LI->getLoopFor(BB) != CurLoop;
  896. }