LoopUnrollRuntime.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. //===-- UnrollLoopRuntime.cpp - Runtime Loop unrolling utilities ----------===//
  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 file implements some loop unrolling utilities for loops with run-time
  11. // trip counts. See LoopUnroll.cpp for unrolling loops with compile-time
  12. // trip counts.
  13. //
  14. // The functions in this file are used to generate extra code when the
  15. // run-time trip count modulo the unroll factor is not 0. When this is the
  16. // case, we need to generate code to execute these 'left over' iterations.
  17. //
  18. // The current strategy generates an if-then-else sequence prior to the
  19. // unrolled loop to execute the 'left over' iterations before or after the
  20. // unrolled loop.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include "llvm/ADT/SmallPtrSet.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/Analysis/LoopIterator.h"
  27. #include "llvm/Analysis/ScalarEvolution.h"
  28. #include "llvm/Analysis/ScalarEvolutionExpander.h"
  29. #include "llvm/IR/BasicBlock.h"
  30. #include "llvm/IR/Dominators.h"
  31. #include "llvm/IR/Metadata.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/Support/Debug.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/Transforms/Utils.h"
  36. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  37. #include "llvm/Transforms/Utils/Cloning.h"
  38. #include "llvm/Transforms/Utils/LoopUtils.h"
  39. #include "llvm/Transforms/Utils/UnrollLoop.h"
  40. #include <algorithm>
  41. using namespace llvm;
  42. #define DEBUG_TYPE "loop-unroll"
  43. STATISTIC(NumRuntimeUnrolled,
  44. "Number of loops unrolled with run-time trip counts");
  45. static cl::opt<bool> UnrollRuntimeMultiExit(
  46. "unroll-runtime-multi-exit", cl::init(false), cl::Hidden,
  47. cl::desc("Allow runtime unrolling for loops with multiple exits, when "
  48. "epilog is generated"));
  49. /// Connect the unrolling prolog code to the original loop.
  50. /// The unrolling prolog code contains code to execute the
  51. /// 'extra' iterations if the run-time trip count modulo the
  52. /// unroll count is non-zero.
  53. ///
  54. /// This function performs the following:
  55. /// - Create PHI nodes at prolog end block to combine values
  56. /// that exit the prolog code and jump around the prolog.
  57. /// - Add a PHI operand to a PHI node at the loop exit block
  58. /// for values that exit the prolog and go around the loop.
  59. /// - Branch around the original loop if the trip count is less
  60. /// than the unroll factor.
  61. ///
  62. static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
  63. BasicBlock *PrologExit,
  64. BasicBlock *OriginalLoopLatchExit,
  65. BasicBlock *PreHeader, BasicBlock *NewPreHeader,
  66. ValueToValueMapTy &VMap, DominatorTree *DT,
  67. LoopInfo *LI, bool PreserveLCSSA) {
  68. // Loop structure should be the following:
  69. // Preheader
  70. // PrologHeader
  71. // ...
  72. // PrologLatch
  73. // PrologExit
  74. // NewPreheader
  75. // Header
  76. // ...
  77. // Latch
  78. // LatchExit
  79. BasicBlock *Latch = L->getLoopLatch();
  80. assert(Latch && "Loop must have a latch");
  81. BasicBlock *PrologLatch = cast<BasicBlock>(VMap[Latch]);
  82. // Create a PHI node for each outgoing value from the original loop
  83. // (which means it is an outgoing value from the prolog code too).
  84. // The new PHI node is inserted in the prolog end basic block.
  85. // The new PHI node value is added as an operand of a PHI node in either
  86. // the loop header or the loop exit block.
  87. for (BasicBlock *Succ : successors(Latch)) {
  88. for (PHINode &PN : Succ->phis()) {
  89. // Add a new PHI node to the prolog end block and add the
  90. // appropriate incoming values.
  91. // TODO: This code assumes that the PrologExit (or the LatchExit block for
  92. // prolog loop) contains only one predecessor from the loop, i.e. the
  93. // PrologLatch. When supporting multiple-exiting block loops, we can have
  94. // two or more blocks that have the LatchExit as the target in the
  95. // original loop.
  96. PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr",
  97. PrologExit->getFirstNonPHI());
  98. // Adding a value to the new PHI node from the original loop preheader.
  99. // This is the value that skips all the prolog code.
  100. if (L->contains(&PN)) {
  101. // Succ is loop header.
  102. NewPN->addIncoming(PN.getIncomingValueForBlock(NewPreHeader),
  103. PreHeader);
  104. } else {
  105. // Succ is LatchExit.
  106. NewPN->addIncoming(UndefValue::get(PN.getType()), PreHeader);
  107. }
  108. Value *V = PN.getIncomingValueForBlock(Latch);
  109. if (Instruction *I = dyn_cast<Instruction>(V)) {
  110. if (L->contains(I)) {
  111. V = VMap.lookup(I);
  112. }
  113. }
  114. // Adding a value to the new PHI node from the last prolog block
  115. // that was created.
  116. NewPN->addIncoming(V, PrologLatch);
  117. // Update the existing PHI node operand with the value from the
  118. // new PHI node. How this is done depends on if the existing
  119. // PHI node is in the original loop block, or the exit block.
  120. if (L->contains(&PN)) {
  121. PN.setIncomingValue(PN.getBasicBlockIndex(NewPreHeader), NewPN);
  122. } else {
  123. PN.addIncoming(NewPN, PrologExit);
  124. }
  125. }
  126. }
  127. // Make sure that created prolog loop is in simplified form
  128. SmallVector<BasicBlock *, 4> PrologExitPreds;
  129. Loop *PrologLoop = LI->getLoopFor(PrologLatch);
  130. if (PrologLoop) {
  131. for (BasicBlock *PredBB : predecessors(PrologExit))
  132. if (PrologLoop->contains(PredBB))
  133. PrologExitPreds.push_back(PredBB);
  134. SplitBlockPredecessors(PrologExit, PrologExitPreds, ".unr-lcssa", DT, LI,
  135. nullptr, PreserveLCSSA);
  136. }
  137. // Create a branch around the original loop, which is taken if there are no
  138. // iterations remaining to be executed after running the prologue.
  139. Instruction *InsertPt = PrologExit->getTerminator();
  140. IRBuilder<> B(InsertPt);
  141. assert(Count != 0 && "nonsensical Count!");
  142. // If BECount <u (Count - 1) then (BECount + 1) % Count == (BECount + 1)
  143. // This means %xtraiter is (BECount + 1) and all of the iterations of this
  144. // loop were executed by the prologue. Note that if BECount <u (Count - 1)
  145. // then (BECount + 1) cannot unsigned-overflow.
  146. Value *BrLoopExit =
  147. B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1));
  148. // Split the exit to maintain loop canonicalization guarantees
  149. SmallVector<BasicBlock *, 4> Preds(predecessors(OriginalLoopLatchExit));
  150. SplitBlockPredecessors(OriginalLoopLatchExit, Preds, ".unr-lcssa", DT, LI,
  151. nullptr, PreserveLCSSA);
  152. // Add the branch to the exit block (around the unrolled loop)
  153. B.CreateCondBr(BrLoopExit, OriginalLoopLatchExit, NewPreHeader);
  154. InsertPt->eraseFromParent();
  155. if (DT)
  156. DT->changeImmediateDominator(OriginalLoopLatchExit, PrologExit);
  157. }
  158. /// Connect the unrolling epilog code to the original loop.
  159. /// The unrolling epilog code contains code to execute the
  160. /// 'extra' iterations if the run-time trip count modulo the
  161. /// unroll count is non-zero.
  162. ///
  163. /// This function performs the following:
  164. /// - Update PHI nodes at the unrolling loop exit and epilog loop exit
  165. /// - Create PHI nodes at the unrolling loop exit to combine
  166. /// values that exit the unrolling loop code and jump around it.
  167. /// - Update PHI operands in the epilog loop by the new PHI nodes
  168. /// - Branch around the epilog loop if extra iters (ModVal) is zero.
  169. ///
  170. static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
  171. BasicBlock *Exit, BasicBlock *PreHeader,
  172. BasicBlock *EpilogPreHeader, BasicBlock *NewPreHeader,
  173. ValueToValueMapTy &VMap, DominatorTree *DT,
  174. LoopInfo *LI, bool PreserveLCSSA) {
  175. BasicBlock *Latch = L->getLoopLatch();
  176. assert(Latch && "Loop must have a latch");
  177. BasicBlock *EpilogLatch = cast<BasicBlock>(VMap[Latch]);
  178. // Loop structure should be the following:
  179. //
  180. // PreHeader
  181. // NewPreHeader
  182. // Header
  183. // ...
  184. // Latch
  185. // NewExit (PN)
  186. // EpilogPreHeader
  187. // EpilogHeader
  188. // ...
  189. // EpilogLatch
  190. // Exit (EpilogPN)
  191. // Update PHI nodes at NewExit and Exit.
  192. for (PHINode &PN : NewExit->phis()) {
  193. // PN should be used in another PHI located in Exit block as
  194. // Exit was split by SplitBlockPredecessors into Exit and NewExit
  195. // Basicaly it should look like:
  196. // NewExit:
  197. // PN = PHI [I, Latch]
  198. // ...
  199. // Exit:
  200. // EpilogPN = PHI [PN, EpilogPreHeader]
  201. //
  202. // There is EpilogPreHeader incoming block instead of NewExit as
  203. // NewExit was spilt 1 more time to get EpilogPreHeader.
  204. assert(PN.hasOneUse() && "The phi should have 1 use");
  205. PHINode *EpilogPN = cast<PHINode>(PN.use_begin()->getUser());
  206. assert(EpilogPN->getParent() == Exit && "EpilogPN should be in Exit block");
  207. // Add incoming PreHeader from branch around the Loop
  208. PN.addIncoming(UndefValue::get(PN.getType()), PreHeader);
  209. Value *V = PN.getIncomingValueForBlock(Latch);
  210. Instruction *I = dyn_cast<Instruction>(V);
  211. if (I && L->contains(I))
  212. // If value comes from an instruction in the loop add VMap value.
  213. V = VMap.lookup(I);
  214. // For the instruction out of the loop, constant or undefined value
  215. // insert value itself.
  216. EpilogPN->addIncoming(V, EpilogLatch);
  217. assert(EpilogPN->getBasicBlockIndex(EpilogPreHeader) >= 0 &&
  218. "EpilogPN should have EpilogPreHeader incoming block");
  219. // Change EpilogPreHeader incoming block to NewExit.
  220. EpilogPN->setIncomingBlock(EpilogPN->getBasicBlockIndex(EpilogPreHeader),
  221. NewExit);
  222. // Now PHIs should look like:
  223. // NewExit:
  224. // PN = PHI [I, Latch], [undef, PreHeader]
  225. // ...
  226. // Exit:
  227. // EpilogPN = PHI [PN, NewExit], [VMap[I], EpilogLatch]
  228. }
  229. // Create PHI nodes at NewExit (from the unrolling loop Latch and PreHeader).
  230. // Update corresponding PHI nodes in epilog loop.
  231. for (BasicBlock *Succ : successors(Latch)) {
  232. // Skip this as we already updated phis in exit blocks.
  233. if (!L->contains(Succ))
  234. continue;
  235. for (PHINode &PN : Succ->phis()) {
  236. // Add new PHI nodes to the loop exit block and update epilog
  237. // PHIs with the new PHI values.
  238. PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr",
  239. NewExit->getFirstNonPHI());
  240. // Adding a value to the new PHI node from the unrolling loop preheader.
  241. NewPN->addIncoming(PN.getIncomingValueForBlock(NewPreHeader), PreHeader);
  242. // Adding a value to the new PHI node from the unrolling loop latch.
  243. NewPN->addIncoming(PN.getIncomingValueForBlock(Latch), Latch);
  244. // Update the existing PHI node operand with the value from the new PHI
  245. // node. Corresponding instruction in epilog loop should be PHI.
  246. PHINode *VPN = cast<PHINode>(VMap[&PN]);
  247. VPN->setIncomingValue(VPN->getBasicBlockIndex(EpilogPreHeader), NewPN);
  248. }
  249. }
  250. Instruction *InsertPt = NewExit->getTerminator();
  251. IRBuilder<> B(InsertPt);
  252. Value *BrLoopExit = B.CreateIsNotNull(ModVal, "lcmp.mod");
  253. assert(Exit && "Loop must have a single exit block only");
  254. // Split the epilogue exit to maintain loop canonicalization guarantees
  255. SmallVector<BasicBlock*, 4> Preds(predecessors(Exit));
  256. SplitBlockPredecessors(Exit, Preds, ".epilog-lcssa", DT, LI, nullptr,
  257. PreserveLCSSA);
  258. // Add the branch to the exit block (around the unrolling loop)
  259. B.CreateCondBr(BrLoopExit, EpilogPreHeader, Exit);
  260. InsertPt->eraseFromParent();
  261. if (DT)
  262. DT->changeImmediateDominator(Exit, NewExit);
  263. // Split the main loop exit to maintain canonicalization guarantees.
  264. SmallVector<BasicBlock*, 4> NewExitPreds{Latch};
  265. SplitBlockPredecessors(NewExit, NewExitPreds, ".loopexit", DT, LI, nullptr,
  266. PreserveLCSSA);
  267. }
  268. /// Create a clone of the blocks in a loop and connect them together.
  269. /// If CreateRemainderLoop is false, loop structure will not be cloned,
  270. /// otherwise a new loop will be created including all cloned blocks, and the
  271. /// iterator of it switches to count NewIter down to 0.
  272. /// The cloned blocks should be inserted between InsertTop and InsertBot.
  273. /// If loop structure is cloned InsertTop should be new preheader, InsertBot
  274. /// new loop exit.
  275. /// Return the new cloned loop that is created when CreateRemainderLoop is true.
  276. static Loop *
  277. CloneLoopBlocks(Loop *L, Value *NewIter, const bool CreateRemainderLoop,
  278. const bool UseEpilogRemainder, const bool UnrollRemainder,
  279. BasicBlock *InsertTop,
  280. BasicBlock *InsertBot, BasicBlock *Preheader,
  281. std::vector<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
  282. ValueToValueMapTy &VMap, DominatorTree *DT, LoopInfo *LI) {
  283. StringRef suffix = UseEpilogRemainder ? "epil" : "prol";
  284. BasicBlock *Header = L->getHeader();
  285. BasicBlock *Latch = L->getLoopLatch();
  286. Function *F = Header->getParent();
  287. LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
  288. LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
  289. Loop *ParentLoop = L->getParentLoop();
  290. NewLoopsMap NewLoops;
  291. NewLoops[ParentLoop] = ParentLoop;
  292. if (!CreateRemainderLoop)
  293. NewLoops[L] = ParentLoop;
  294. // For each block in the original loop, create a new copy,
  295. // and update the value map with the newly created values.
  296. for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
  297. BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, "." + suffix, F);
  298. NewBlocks.push_back(NewBB);
  299. // If we're unrolling the outermost loop, there's no remainder loop,
  300. // and this block isn't in a nested loop, then the new block is not
  301. // in any loop. Otherwise, add it to loopinfo.
  302. if (CreateRemainderLoop || LI->getLoopFor(*BB) != L || ParentLoop)
  303. addClonedBlockToLoopInfo(*BB, NewBB, LI, NewLoops);
  304. VMap[*BB] = NewBB;
  305. if (Header == *BB) {
  306. // For the first block, add a CFG connection to this newly
  307. // created block.
  308. InsertTop->getTerminator()->setSuccessor(0, NewBB);
  309. }
  310. if (DT) {
  311. if (Header == *BB) {
  312. // The header is dominated by the preheader.
  313. DT->addNewBlock(NewBB, InsertTop);
  314. } else {
  315. // Copy information from original loop to unrolled loop.
  316. BasicBlock *IDomBB = DT->getNode(*BB)->getIDom()->getBlock();
  317. DT->addNewBlock(NewBB, cast<BasicBlock>(VMap[IDomBB]));
  318. }
  319. }
  320. if (Latch == *BB) {
  321. // For the last block, if CreateRemainderLoop is false, create a direct
  322. // jump to InsertBot. If not, create a loop back to cloned head.
  323. VMap.erase((*BB)->getTerminator());
  324. BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
  325. BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
  326. IRBuilder<> Builder(LatchBR);
  327. if (!CreateRemainderLoop) {
  328. Builder.CreateBr(InsertBot);
  329. } else {
  330. PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2,
  331. suffix + ".iter",
  332. FirstLoopBB->getFirstNonPHI());
  333. Value *IdxSub =
  334. Builder.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
  335. NewIdx->getName() + ".sub");
  336. Value *IdxCmp =
  337. Builder.CreateIsNotNull(IdxSub, NewIdx->getName() + ".cmp");
  338. Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot);
  339. NewIdx->addIncoming(NewIter, InsertTop);
  340. NewIdx->addIncoming(IdxSub, NewBB);
  341. }
  342. LatchBR->eraseFromParent();
  343. }
  344. }
  345. // Change the incoming values to the ones defined in the preheader or
  346. // cloned loop.
  347. for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
  348. PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
  349. if (!CreateRemainderLoop) {
  350. if (UseEpilogRemainder) {
  351. unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
  352. NewPHI->setIncomingBlock(idx, InsertTop);
  353. NewPHI->removeIncomingValue(Latch, false);
  354. } else {
  355. VMap[&*I] = NewPHI->getIncomingValueForBlock(Preheader);
  356. cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
  357. }
  358. } else {
  359. unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
  360. NewPHI->setIncomingBlock(idx, InsertTop);
  361. BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
  362. idx = NewPHI->getBasicBlockIndex(Latch);
  363. Value *InVal = NewPHI->getIncomingValue(idx);
  364. NewPHI->setIncomingBlock(idx, NewLatch);
  365. if (Value *V = VMap.lookup(InVal))
  366. NewPHI->setIncomingValue(idx, V);
  367. }
  368. }
  369. if (CreateRemainderLoop) {
  370. Loop *NewLoop = NewLoops[L];
  371. MDNode *LoopID = NewLoop->getLoopID();
  372. assert(NewLoop && "L should have been cloned");
  373. // Only add loop metadata if the loop is not going to be completely
  374. // unrolled.
  375. if (UnrollRemainder)
  376. return NewLoop;
  377. Optional<MDNode *> NewLoopID = makeFollowupLoopID(
  378. LoopID, {LLVMLoopUnrollFollowupAll, LLVMLoopUnrollFollowupRemainder});
  379. if (NewLoopID.hasValue()) {
  380. NewLoop->setLoopID(NewLoopID.getValue());
  381. // Do not setLoopAlreadyUnrolled if loop attributes have been defined
  382. // explicitly.
  383. return NewLoop;
  384. }
  385. // Add unroll disable metadata to disable future unrolling for this loop.
  386. NewLoop->setLoopAlreadyUnrolled();
  387. return NewLoop;
  388. }
  389. else
  390. return nullptr;
  391. }
  392. /// Returns true if we can safely unroll a multi-exit/exiting loop. OtherExits
  393. /// is populated with all the loop exit blocks other than the LatchExit block.
  394. static bool
  395. canSafelyUnrollMultiExitLoop(Loop *L, SmallVectorImpl<BasicBlock *> &OtherExits,
  396. BasicBlock *LatchExit, bool PreserveLCSSA,
  397. bool UseEpilogRemainder) {
  398. // We currently have some correctness constrains in unrolling a multi-exit
  399. // loop. Check for these below.
  400. // We rely on LCSSA form being preserved when the exit blocks are transformed.
  401. if (!PreserveLCSSA)
  402. return false;
  403. SmallVector<BasicBlock *, 4> Exits;
  404. L->getUniqueExitBlocks(Exits);
  405. for (auto *BB : Exits)
  406. if (BB != LatchExit)
  407. OtherExits.push_back(BB);
  408. // TODO: Support multiple exiting blocks jumping to the `LatchExit` when
  409. // UnrollRuntimeMultiExit is true. This will need updating the logic in
  410. // connectEpilog/connectProlog.
  411. if (!LatchExit->getSinglePredecessor()) {
  412. LLVM_DEBUG(
  413. dbgs() << "Bailout for multi-exit handling when latch exit has >1 "
  414. "predecessor.\n");
  415. return false;
  416. }
  417. // FIXME: We bail out of multi-exit unrolling when epilog loop is generated
  418. // and L is an inner loop. This is because in presence of multiple exits, the
  419. // outer loop is incorrect: we do not add the EpilogPreheader and exit to the
  420. // outer loop. This is automatically handled in the prolog case, so we do not
  421. // have that bug in prolog generation.
  422. if (UseEpilogRemainder && L->getParentLoop())
  423. return false;
  424. // All constraints have been satisfied.
  425. return true;
  426. }
  427. /// Returns true if we can profitably unroll the multi-exit loop L. Currently,
  428. /// we return true only if UnrollRuntimeMultiExit is set to true.
  429. static bool canProfitablyUnrollMultiExitLoop(
  430. Loop *L, SmallVectorImpl<BasicBlock *> &OtherExits, BasicBlock *LatchExit,
  431. bool PreserveLCSSA, bool UseEpilogRemainder) {
  432. #if !defined(NDEBUG)
  433. SmallVector<BasicBlock *, 8> OtherExitsDummyCheck;
  434. assert(canSafelyUnrollMultiExitLoop(L, OtherExitsDummyCheck, LatchExit,
  435. PreserveLCSSA, UseEpilogRemainder) &&
  436. "Should be safe to unroll before checking profitability!");
  437. #endif
  438. // Priority goes to UnrollRuntimeMultiExit if it's supplied.
  439. if (UnrollRuntimeMultiExit.getNumOccurrences())
  440. return UnrollRuntimeMultiExit;
  441. // The main pain point with multi-exit loop unrolling is that once unrolled,
  442. // we will not be able to merge all blocks into a straight line code.
  443. // There are branches within the unrolled loop that go to the OtherExits.
  444. // The second point is the increase in code size, but this is true
  445. // irrespective of multiple exits.
  446. // Note: Both the heuristics below are coarse grained. We are essentially
  447. // enabling unrolling of loops that have a single side exit other than the
  448. // normal LatchExit (i.e. exiting into a deoptimize block).
  449. // The heuristics considered are:
  450. // 1. low number of branches in the unrolled version.
  451. // 2. high predictability of these extra branches.
  452. // We avoid unrolling loops that have more than two exiting blocks. This
  453. // limits the total number of branches in the unrolled loop to be atmost
  454. // the unroll factor (since one of the exiting blocks is the latch block).
  455. SmallVector<BasicBlock*, 4> ExitingBlocks;
  456. L->getExitingBlocks(ExitingBlocks);
  457. if (ExitingBlocks.size() > 2)
  458. return false;
  459. // The second heuristic is that L has one exit other than the latchexit and
  460. // that exit is a deoptimize block. We know that deoptimize blocks are rarely
  461. // taken, which also implies the branch leading to the deoptimize block is
  462. // highly predictable.
  463. return (OtherExits.size() == 1 &&
  464. OtherExits[0]->getTerminatingDeoptimizeCall());
  465. // TODO: These can be fine-tuned further to consider code size or deopt states
  466. // that are captured by the deoptimize exit block.
  467. // Also, we can extend this to support more cases, if we actually
  468. // know of kinds of multiexit loops that would benefit from unrolling.
  469. }
  470. /// Insert code in the prolog/epilog code when unrolling a loop with a
  471. /// run-time trip-count.
  472. ///
  473. /// This method assumes that the loop unroll factor is total number
  474. /// of loop bodies in the loop after unrolling. (Some folks refer
  475. /// to the unroll factor as the number of *extra* copies added).
  476. /// We assume also that the loop unroll factor is a power-of-two. So, after
  477. /// unrolling the loop, the number of loop bodies executed is 2,
  478. /// 4, 8, etc. Note - LLVM converts the if-then-sequence to a switch
  479. /// instruction in SimplifyCFG.cpp. Then, the backend decides how code for
  480. /// the switch instruction is generated.
  481. ///
  482. /// ***Prolog case***
  483. /// extraiters = tripcount % loopfactor
  484. /// if (extraiters == 0) jump Loop:
  485. /// else jump Prol:
  486. /// Prol: LoopBody;
  487. /// extraiters -= 1 // Omitted if unroll factor is 2.
  488. /// if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
  489. /// if (tripcount < loopfactor) jump End:
  490. /// Loop:
  491. /// ...
  492. /// End:
  493. ///
  494. /// ***Epilog case***
  495. /// extraiters = tripcount % loopfactor
  496. /// if (tripcount < loopfactor) jump LoopExit:
  497. /// unroll_iters = tripcount - extraiters
  498. /// Loop: LoopBody; (executes unroll_iter times);
  499. /// unroll_iter -= 1
  500. /// if (unroll_iter != 0) jump Loop:
  501. /// LoopExit:
  502. /// if (extraiters == 0) jump EpilExit:
  503. /// Epil: LoopBody; (executes extraiters times)
  504. /// extraiters -= 1 // Omitted if unroll factor is 2.
  505. /// if (extraiters != 0) jump Epil: // Omitted if unroll factor is 2.
  506. /// EpilExit:
  507. bool llvm::UnrollRuntimeLoopRemainder(Loop *L, unsigned Count,
  508. bool AllowExpensiveTripCount,
  509. bool UseEpilogRemainder,
  510. bool UnrollRemainder, LoopInfo *LI,
  511. ScalarEvolution *SE, DominatorTree *DT,
  512. AssumptionCache *AC, bool PreserveLCSSA,
  513. Loop **ResultLoop) {
  514. LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
  515. LLVM_DEBUG(L->dump());
  516. LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
  517. : dbgs() << "Using prolog remainder.\n");
  518. // Make sure the loop is in canonical form.
  519. if (!L->isLoopSimplifyForm()) {
  520. LLVM_DEBUG(dbgs() << "Not in simplify form!\n");
  521. return false;
  522. }
  523. // Guaranteed by LoopSimplifyForm.
  524. BasicBlock *Latch = L->getLoopLatch();
  525. BasicBlock *Header = L->getHeader();
  526. BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
  527. if (!LatchBR || LatchBR->isUnconditional()) {
  528. // The loop-rotate pass can be helpful to avoid this in many cases.
  529. LLVM_DEBUG(
  530. dbgs()
  531. << "Loop latch not terminated by a conditional branch.\n");
  532. return false;
  533. }
  534. unsigned ExitIndex = LatchBR->getSuccessor(0) == Header ? 1 : 0;
  535. BasicBlock *LatchExit = LatchBR->getSuccessor(ExitIndex);
  536. if (L->contains(LatchExit)) {
  537. // Cloning the loop basic blocks (`CloneLoopBlocks`) requires that one of the
  538. // targets of the Latch be an exit block out of the loop.
  539. LLVM_DEBUG(
  540. dbgs()
  541. << "One of the loop latch successors must be the exit block.\n");
  542. return false;
  543. }
  544. // These are exit blocks other than the target of the latch exiting block.
  545. SmallVector<BasicBlock *, 4> OtherExits;
  546. bool isMultiExitUnrollingEnabled =
  547. canSafelyUnrollMultiExitLoop(L, OtherExits, LatchExit, PreserveLCSSA,
  548. UseEpilogRemainder) &&
  549. canProfitablyUnrollMultiExitLoop(L, OtherExits, LatchExit, PreserveLCSSA,
  550. UseEpilogRemainder);
  551. // Support only single exit and exiting block unless multi-exit loop unrolling is enabled.
  552. if (!isMultiExitUnrollingEnabled &&
  553. (!L->getExitingBlock() || OtherExits.size())) {
  554. LLVM_DEBUG(
  555. dbgs()
  556. << "Multiple exit/exiting blocks in loop and multi-exit unrolling not "
  557. "enabled!\n");
  558. return false;
  559. }
  560. // Use Scalar Evolution to compute the trip count. This allows more loops to
  561. // be unrolled than relying on induction var simplification.
  562. if (!SE)
  563. return false;
  564. // Only unroll loops with a computable trip count, and the trip count needs
  565. // to be an int value (allowing a pointer type is a TODO item).
  566. // We calculate the backedge count by using getExitCount on the Latch block,
  567. // which is proven to be the only exiting block in this loop. This is same as
  568. // calculating getBackedgeTakenCount on the loop (which computes SCEV for all
  569. // exiting blocks).
  570. const SCEV *BECountSC = SE->getExitCount(L, Latch);
  571. if (isa<SCEVCouldNotCompute>(BECountSC) ||
  572. !BECountSC->getType()->isIntegerTy()) {
  573. LLVM_DEBUG(dbgs() << "Could not compute exit block SCEV\n");
  574. return false;
  575. }
  576. unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();
  577. // Add 1 since the backedge count doesn't include the first loop iteration.
  578. const SCEV *TripCountSC =
  579. SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
  580. if (isa<SCEVCouldNotCompute>(TripCountSC)) {
  581. LLVM_DEBUG(dbgs() << "Could not compute trip count SCEV.\n");
  582. return false;
  583. }
  584. BasicBlock *PreHeader = L->getLoopPreheader();
  585. BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
  586. const DataLayout &DL = Header->getModule()->getDataLayout();
  587. SCEVExpander Expander(*SE, DL, "loop-unroll");
  588. if (!AllowExpensiveTripCount &&
  589. Expander.isHighCostExpansion(TripCountSC, L, PreHeaderBR)) {
  590. LLVM_DEBUG(dbgs() << "High cost for expanding trip count scev!\n");
  591. return false;
  592. }
  593. // This constraint lets us deal with an overflowing trip count easily; see the
  594. // comment on ModVal below.
  595. if (Log2_32(Count) > BEWidth) {
  596. LLVM_DEBUG(
  597. dbgs()
  598. << "Count failed constraint on overflow trip count calculation.\n");
  599. return false;
  600. }
  601. // Loop structure is the following:
  602. //
  603. // PreHeader
  604. // Header
  605. // ...
  606. // Latch
  607. // LatchExit
  608. BasicBlock *NewPreHeader;
  609. BasicBlock *NewExit = nullptr;
  610. BasicBlock *PrologExit = nullptr;
  611. BasicBlock *EpilogPreHeader = nullptr;
  612. BasicBlock *PrologPreHeader = nullptr;
  613. if (UseEpilogRemainder) {
  614. // If epilog remainder
  615. // Split PreHeader to insert a branch around loop for unrolling.
  616. NewPreHeader = SplitBlock(PreHeader, PreHeader->getTerminator(), DT, LI);
  617. NewPreHeader->setName(PreHeader->getName() + ".new");
  618. // Split LatchExit to create phi nodes from branch above.
  619. SmallVector<BasicBlock*, 4> Preds(predecessors(LatchExit));
  620. NewExit = SplitBlockPredecessors(LatchExit, Preds, ".unr-lcssa", DT, LI,
  621. nullptr, PreserveLCSSA);
  622. // NewExit gets its DebugLoc from LatchExit, which is not part of the
  623. // original Loop.
  624. // Fix this by setting Loop's DebugLoc to NewExit.
  625. auto *NewExitTerminator = NewExit->getTerminator();
  626. NewExitTerminator->setDebugLoc(Header->getTerminator()->getDebugLoc());
  627. // Split NewExit to insert epilog remainder loop.
  628. EpilogPreHeader = SplitBlock(NewExit, NewExitTerminator, DT, LI);
  629. EpilogPreHeader->setName(Header->getName() + ".epil.preheader");
  630. } else {
  631. // If prolog remainder
  632. // Split the original preheader twice to insert prolog remainder loop
  633. PrologPreHeader = SplitEdge(PreHeader, Header, DT, LI);
  634. PrologPreHeader->setName(Header->getName() + ".prol.preheader");
  635. PrologExit = SplitBlock(PrologPreHeader, PrologPreHeader->getTerminator(),
  636. DT, LI);
  637. PrologExit->setName(Header->getName() + ".prol.loopexit");
  638. // Split PrologExit to get NewPreHeader.
  639. NewPreHeader = SplitBlock(PrologExit, PrologExit->getTerminator(), DT, LI);
  640. NewPreHeader->setName(PreHeader->getName() + ".new");
  641. }
  642. // Loop structure should be the following:
  643. // Epilog Prolog
  644. //
  645. // PreHeader PreHeader
  646. // *NewPreHeader *PrologPreHeader
  647. // Header *PrologExit
  648. // ... *NewPreHeader
  649. // Latch Header
  650. // *NewExit ...
  651. // *EpilogPreHeader Latch
  652. // LatchExit LatchExit
  653. // Calculate conditions for branch around loop for unrolling
  654. // in epilog case and around prolog remainder loop in prolog case.
  655. // Compute the number of extra iterations required, which is:
  656. // extra iterations = run-time trip count % loop unroll factor
  657. PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
  658. Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
  659. PreHeaderBR);
  660. Value *BECount = Expander.expandCodeFor(BECountSC, BECountSC->getType(),
  661. PreHeaderBR);
  662. IRBuilder<> B(PreHeaderBR);
  663. Value *ModVal;
  664. // Calculate ModVal = (BECount + 1) % Count.
  665. // Note that TripCount is BECount + 1.
  666. if (isPowerOf2_32(Count)) {
  667. // When Count is power of 2 we don't BECount for epilog case, however we'll
  668. // need it for a branch around unrolling loop for prolog case.
  669. ModVal = B.CreateAnd(TripCount, Count - 1, "xtraiter");
  670. // 1. There are no iterations to be run in the prolog/epilog loop.
  671. // OR
  672. // 2. The addition computing TripCount overflowed.
  673. //
  674. // If (2) is true, we know that TripCount really is (1 << BEWidth) and so
  675. // the number of iterations that remain to be run in the original loop is a
  676. // multiple Count == (1 << Log2(Count)) because Log2(Count) <= BEWidth (we
  677. // explicitly check this above).
  678. } else {
  679. // As (BECount + 1) can potentially unsigned overflow we count
  680. // (BECount % Count) + 1 which is overflow safe as BECount % Count < Count.
  681. Value *ModValTmp = B.CreateURem(BECount,
  682. ConstantInt::get(BECount->getType(),
  683. Count));
  684. Value *ModValAdd = B.CreateAdd(ModValTmp,
  685. ConstantInt::get(ModValTmp->getType(), 1));
  686. // At that point (BECount % Count) + 1 could be equal to Count.
  687. // To handle this case we need to take mod by Count one more time.
  688. ModVal = B.CreateURem(ModValAdd,
  689. ConstantInt::get(BECount->getType(), Count),
  690. "xtraiter");
  691. }
  692. Value *BranchVal =
  693. UseEpilogRemainder ? B.CreateICmpULT(BECount,
  694. ConstantInt::get(BECount->getType(),
  695. Count - 1)) :
  696. B.CreateIsNotNull(ModVal, "lcmp.mod");
  697. BasicBlock *RemainderLoop = UseEpilogRemainder ? NewExit : PrologPreHeader;
  698. BasicBlock *UnrollingLoop = UseEpilogRemainder ? NewPreHeader : PrologExit;
  699. // Branch to either remainder (extra iterations) loop or unrolling loop.
  700. B.CreateCondBr(BranchVal, RemainderLoop, UnrollingLoop);
  701. PreHeaderBR->eraseFromParent();
  702. if (DT) {
  703. if (UseEpilogRemainder)
  704. DT->changeImmediateDominator(NewExit, PreHeader);
  705. else
  706. DT->changeImmediateDominator(PrologExit, PreHeader);
  707. }
  708. Function *F = Header->getParent();
  709. // Get an ordered list of blocks in the loop to help with the ordering of the
  710. // cloned blocks in the prolog/epilog code
  711. LoopBlocksDFS LoopBlocks(L);
  712. LoopBlocks.perform(LI);
  713. //
  714. // For each extra loop iteration, create a copy of the loop's basic blocks
  715. // and generate a condition that branches to the copy depending on the
  716. // number of 'left over' iterations.
  717. //
  718. std::vector<BasicBlock *> NewBlocks;
  719. ValueToValueMapTy VMap;
  720. // For unroll factor 2 remainder loop will have 1 iterations.
  721. // Do not create 1 iteration loop.
  722. bool CreateRemainderLoop = (Count != 2);
  723. // Clone all the basic blocks in the loop. If Count is 2, we don't clone
  724. // the loop, otherwise we create a cloned loop to execute the extra
  725. // iterations. This function adds the appropriate CFG connections.
  726. BasicBlock *InsertBot = UseEpilogRemainder ? LatchExit : PrologExit;
  727. BasicBlock *InsertTop = UseEpilogRemainder ? EpilogPreHeader : PrologPreHeader;
  728. Loop *remainderLoop = CloneLoopBlocks(
  729. L, ModVal, CreateRemainderLoop, UseEpilogRemainder, UnrollRemainder,
  730. InsertTop, InsertBot,
  731. NewPreHeader, NewBlocks, LoopBlocks, VMap, DT, LI);
  732. // Insert the cloned blocks into the function.
  733. F->getBasicBlockList().splice(InsertBot->getIterator(),
  734. F->getBasicBlockList(),
  735. NewBlocks[0]->getIterator(),
  736. F->end());
  737. // Now the loop blocks are cloned and the other exiting blocks from the
  738. // remainder are connected to the original Loop's exit blocks. The remaining
  739. // work is to update the phi nodes in the original loop, and take in the
  740. // values from the cloned region.
  741. for (auto *BB : OtherExits) {
  742. for (auto &II : *BB) {
  743. // Given we preserve LCSSA form, we know that the values used outside the
  744. // loop will be used through these phi nodes at the exit blocks that are
  745. // transformed below.
  746. if (!isa<PHINode>(II))
  747. break;
  748. PHINode *Phi = cast<PHINode>(&II);
  749. unsigned oldNumOperands = Phi->getNumIncomingValues();
  750. // Add the incoming values from the remainder code to the end of the phi
  751. // node.
  752. for (unsigned i =0; i < oldNumOperands; i++){
  753. Value *newVal = VMap.lookup(Phi->getIncomingValue(i));
  754. // newVal can be a constant or derived from values outside the loop, and
  755. // hence need not have a VMap value. Also, since lookup already generated
  756. // a default "null" VMap entry for this value, we need to populate that
  757. // VMap entry correctly, with the mapped entry being itself.
  758. if (!newVal) {
  759. newVal = Phi->getIncomingValue(i);
  760. VMap[Phi->getIncomingValue(i)] = Phi->getIncomingValue(i);
  761. }
  762. Phi->addIncoming(newVal,
  763. cast<BasicBlock>(VMap[Phi->getIncomingBlock(i)]));
  764. }
  765. }
  766. #if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
  767. for (BasicBlock *SuccBB : successors(BB)) {
  768. assert(!(any_of(OtherExits,
  769. [SuccBB](BasicBlock *EB) { return EB == SuccBB; }) ||
  770. SuccBB == LatchExit) &&
  771. "Breaks the definition of dedicated exits!");
  772. }
  773. #endif
  774. }
  775. // Update the immediate dominator of the exit blocks and blocks that are
  776. // reachable from the exit blocks. This is needed because we now have paths
  777. // from both the original loop and the remainder code reaching the exit
  778. // blocks. While the IDom of these exit blocks were from the original loop,
  779. // now the IDom is the preheader (which decides whether the original loop or
  780. // remainder code should run).
  781. if (DT && !L->getExitingBlock()) {
  782. SmallVector<BasicBlock *, 16> ChildrenToUpdate;
  783. // NB! We have to examine the dom children of all loop blocks, not just
  784. // those which are the IDom of the exit blocks. This is because blocks
  785. // reachable from the exit blocks can have their IDom as the nearest common
  786. // dominator of the exit blocks.
  787. for (auto *BB : L->blocks()) {
  788. auto *DomNodeBB = DT->getNode(BB);
  789. for (auto *DomChild : DomNodeBB->getChildren()) {
  790. auto *DomChildBB = DomChild->getBlock();
  791. if (!L->contains(LI->getLoopFor(DomChildBB)))
  792. ChildrenToUpdate.push_back(DomChildBB);
  793. }
  794. }
  795. for (auto *BB : ChildrenToUpdate)
  796. DT->changeImmediateDominator(BB, PreHeader);
  797. }
  798. // Loop structure should be the following:
  799. // Epilog Prolog
  800. //
  801. // PreHeader PreHeader
  802. // NewPreHeader PrologPreHeader
  803. // Header PrologHeader
  804. // ... ...
  805. // Latch PrologLatch
  806. // NewExit PrologExit
  807. // EpilogPreHeader NewPreHeader
  808. // EpilogHeader Header
  809. // ... ...
  810. // EpilogLatch Latch
  811. // LatchExit LatchExit
  812. // Rewrite the cloned instruction operands to use the values created when the
  813. // clone is created.
  814. for (BasicBlock *BB : NewBlocks) {
  815. for (Instruction &I : *BB) {
  816. RemapInstruction(&I, VMap,
  817. RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
  818. }
  819. }
  820. if (UseEpilogRemainder) {
  821. // Connect the epilog code to the original loop and update the
  822. // PHI functions.
  823. ConnectEpilog(L, ModVal, NewExit, LatchExit, PreHeader,
  824. EpilogPreHeader, NewPreHeader, VMap, DT, LI,
  825. PreserveLCSSA);
  826. // Update counter in loop for unrolling.
  827. // I should be multiply of Count.
  828. IRBuilder<> B2(NewPreHeader->getTerminator());
  829. Value *TestVal = B2.CreateSub(TripCount, ModVal, "unroll_iter");
  830. BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
  831. B2.SetInsertPoint(LatchBR);
  832. PHINode *NewIdx = PHINode::Create(TestVal->getType(), 2, "niter",
  833. Header->getFirstNonPHI());
  834. Value *IdxSub =
  835. B2.CreateSub(NewIdx, ConstantInt::get(NewIdx->getType(), 1),
  836. NewIdx->getName() + ".nsub");
  837. Value *IdxCmp;
  838. if (LatchBR->getSuccessor(0) == Header)
  839. IdxCmp = B2.CreateIsNotNull(IdxSub, NewIdx->getName() + ".ncmp");
  840. else
  841. IdxCmp = B2.CreateIsNull(IdxSub, NewIdx->getName() + ".ncmp");
  842. NewIdx->addIncoming(TestVal, NewPreHeader);
  843. NewIdx->addIncoming(IdxSub, Latch);
  844. LatchBR->setCondition(IdxCmp);
  845. } else {
  846. // Connect the prolog code to the original loop and update the
  847. // PHI functions.
  848. ConnectProlog(L, BECount, Count, PrologExit, LatchExit, PreHeader,
  849. NewPreHeader, VMap, DT, LI, PreserveLCSSA);
  850. }
  851. // If this loop is nested, then the loop unroller changes the code in the any
  852. // of its parent loops, so the Scalar Evolution pass needs to be run again.
  853. SE->forgetTopmostLoop(L);
  854. // Verify that the Dom Tree is correct.
  855. #if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
  856. if (DT)
  857. assert(DT->verify(DominatorTree::VerificationLevel::Full));
  858. #endif
  859. // Canonicalize to LoopSimplifyForm both original and remainder loops. We
  860. // cannot rely on the LoopUnrollPass to do this because it only does
  861. // canonicalization for parent/subloops and not the sibling loops.
  862. if (OtherExits.size() > 0) {
  863. // Generate dedicated exit blocks for the original loop, to preserve
  864. // LoopSimplifyForm.
  865. formDedicatedExitBlocks(L, DT, LI, PreserveLCSSA);
  866. // Generate dedicated exit blocks for the remainder loop if one exists, to
  867. // preserve LoopSimplifyForm.
  868. if (remainderLoop)
  869. formDedicatedExitBlocks(remainderLoop, DT, LI, PreserveLCSSA);
  870. }
  871. auto UnrollResult = LoopUnrollResult::Unmodified;
  872. if (remainderLoop && UnrollRemainder) {
  873. LLVM_DEBUG(dbgs() << "Unrolling remainder loop\n");
  874. UnrollResult =
  875. UnrollLoop(remainderLoop, /*Count*/ Count - 1, /*TripCount*/ Count - 1,
  876. /*Force*/ false, /*AllowRuntime*/ false,
  877. /*AllowExpensiveTripCount*/ false, /*PreserveCondBr*/ true,
  878. /*PreserveOnlyFirst*/ false, /*TripMultiple*/ 1,
  879. /*PeelCount*/ 0, /*UnrollRemainder*/ false, LI, SE, DT, AC,
  880. /*ORE*/ nullptr, PreserveLCSSA);
  881. }
  882. if (ResultLoop && UnrollResult != LoopUnrollResult::FullyUnrolled)
  883. *ResultLoop = remainderLoop;
  884. NumRuntimeUnrolled++;
  885. return true;
  886. }