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