Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 1 | //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Loop Rotation Pass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 08eebe2 | 2015-07-23 09:34:01 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionCache.h" |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CodeMetrics.h" |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopPass.h" |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Chandler Carruth | bb9caa9 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetTransformInfo.h" |
Andrew Trick | 10cc453 | 2012-02-14 00:00:23 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 27 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/IR/IntrinsicInst.h" |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Module.h" |
Owen Anderson | 115aa16 | 2014-05-26 08:58:51 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 36 | #include "llvm/Transforms/Utils/Local.h" |
| 37 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 38 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "loop-rotate" |
| 42 | |
Owen Anderson | 115aa16 | 2014-05-26 08:58:51 +0000 | [diff] [blame] | 43 | static cl::opt<unsigned> |
| 44 | DefaultRotationThreshold("rotation-max-header-size", cl::init(16), cl::Hidden, |
| 45 | cl::desc("The default maximum header size for automatic loop rotation")); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 46 | |
| 47 | STATISTIC(NumRotated, "Number of loops rotated"); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 49 | /// RewriteUsesOfClonedInstructions - We just cloned the instructions from the |
| 50 | /// old header into the preheader. If there were uses of the values produced by |
| 51 | /// these instruction that were outside of the loop, we have to insert PHI nodes |
| 52 | /// to merge the two values. Do this now. |
| 53 | static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader, |
| 54 | BasicBlock *OrigPreheader, |
| 55 | ValueToValueMapTy &ValueMap) { |
| 56 | // Remove PHI node entries that are no longer live. |
| 57 | BasicBlock::iterator I, E = OrigHeader->end(); |
| 58 | for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 59 | PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreheader)); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 61 | // Now fix up users of the instructions in OrigHeader, inserting PHI nodes |
| 62 | // as necessary. |
| 63 | SSAUpdater SSA; |
| 64 | for (I = OrigHeader->begin(); I != E; ++I) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 65 | Value *OrigHeaderVal = &*I; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 67 | // If there are no uses of the value (e.g. because it returns void), there |
| 68 | // is nothing to rewrite. |
| 69 | if (OrigHeaderVal->use_empty()) |
| 70 | continue; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 72 | Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal]; |
| 73 | |
| 74 | // The value now exits in two versions: the initial value in the preheader |
| 75 | // and the loop "next" value in the original header. |
| 76 | SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName()); |
| 77 | SSA.AddAvailableValue(OrigHeader, OrigHeaderVal); |
| 78 | SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 80 | // Visit each use of the OrigHeader instruction. |
| 81 | for (Value::use_iterator UI = OrigHeaderVal->use_begin(), |
| 82 | UE = OrigHeaderVal->use_end(); UI != UE; ) { |
| 83 | // Grab the use before incrementing the iterator. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 84 | Use &U = *UI; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 86 | // Increment the iterator before removing the use from the list. |
| 87 | ++UI; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 89 | // SSAUpdater can't handle a non-PHI use in the same block as an |
| 90 | // earlier def. We can easily handle those cases manually. |
| 91 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 92 | if (!isa<PHINode>(UserInst)) { |
| 93 | BasicBlock *UserBB = UserInst->getParent(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 95 | // The original users in the OrigHeader are already using the |
| 96 | // original definitions. |
| 97 | if (UserBB == OrigHeader) |
| 98 | continue; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 100 | // Users in the OrigPreHeader need to use the value to which the |
| 101 | // original definitions are mapped. |
| 102 | if (UserBB == OrigPreheader) { |
| 103 | U = OrigPreHeaderVal; |
| 104 | continue; |
| 105 | } |
| 106 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 108 | // Anything else can be handled by SSAUpdater. |
| 109 | SSA.RewriteUse(U); |
| 110 | } |
| 111 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 113 | |
Dan Gohman | b5650eb | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 114 | /// Rotate loop LP. Return true if the loop is rotated. |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 115 | /// |
| 116 | /// \param SimplifiedLatch is true if the latch was just folded into the final |
| 117 | /// loop exit. In this case we may want to rotate even though the new latch is |
| 118 | /// now an exiting branch. This rotation would have happened had the latch not |
| 119 | /// been simplified. However, if SimplifiedLatch is false, then we avoid |
| 120 | /// rotating loops in which the latch exits to avoid excessive or endless |
| 121 | /// rotation. LoopRotate should be repeatable and converge to a canonical |
| 122 | /// form. This property is satisfied because simplifying the loop latch can only |
| 123 | /// happen once across multiple invocations of the LoopRotate pass. |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 124 | static bool rotateLoop(Loop *L, unsigned MaxHeaderSize, LoopInfo *LI, |
| 125 | const TargetTransformInfo *TTI, AssumptionCache *AC, |
| 126 | DominatorTree *DT, ScalarEvolution *SE, |
| 127 | bool SimplifiedLatch) { |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 128 | // If the loop has only one block then there is not much to rotate. |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 129 | if (L->getBlocks().size() == 1) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 130 | return false; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 132 | BasicBlock *OrigHeader = L->getHeader(); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 133 | BasicBlock *OrigLatch = L->getLoopLatch(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 135 | BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 136 | if (!BI || BI->isUnconditional()) |
Chris Lattner | 7fab23b | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 137 | return false; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 138 | |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 139 | // If the loop header is not one of the loop exiting blocks then |
| 140 | // either this loop is already rotated or it is not |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 141 | // suitable for loop rotation transformations. |
Dan Gohman | 8f4078b | 2009-10-24 23:34:26 +0000 | [diff] [blame] | 142 | if (!L->isLoopExiting(OrigHeader)) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 143 | return false; |
| 144 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 145 | // If the loop latch already contains a branch that leaves the loop then the |
| 146 | // loop is already rotated. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 147 | if (!OrigLatch) |
Andrew Trick | 9c72b07 | 2013-05-06 17:58:18 +0000 | [diff] [blame] | 148 | return false; |
| 149 | |
| 150 | // Rotate if either the loop latch does *not* exit the loop, or if the loop |
| 151 | // latch was just simplified. |
| 152 | if (L->isLoopExiting(OrigLatch) && !SimplifiedLatch) |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 153 | return false; |
| 154 | |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 155 | // Check size of original header and reject loop if it is very big or we can't |
| 156 | // duplicate blocks inside it. |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 157 | { |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 158 | SmallPtrSet<const Value *, 32> EphValues; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 159 | CodeMetrics::collectEphemeralValues(L, AC, EphValues); |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 161 | CodeMetrics Metrics; |
Hal Finkel | 57f03dd | 2014-09-07 13:49:57 +0000 | [diff] [blame] | 162 | Metrics.analyzeBasicBlock(OrigHeader, *TTI, EphValues); |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 163 | if (Metrics.notDuplicatable) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 164 | DEBUG(dbgs() << "LoopRotation: NOT rotating - contains non-duplicatable" |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 165 | << " instructions: "; L->dump()); |
| 166 | return false; |
| 167 | } |
Justin Lebar | df04d2a | 2016-02-12 21:01:33 +0000 | [diff] [blame^] | 168 | if (Metrics.convergent) { |
| 169 | DEBUG(dbgs() << "LoopRotation: NOT rotating - contains convergent " |
| 170 | "instructions: "; L->dump()); |
| 171 | return false; |
| 172 | } |
Owen Anderson | 115aa16 | 2014-05-26 08:58:51 +0000 | [diff] [blame] | 173 | if (Metrics.NumInsts > MaxHeaderSize) |
Chris Lattner | 679572e | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 174 | return false; |
Devang Patel | bab43b4 | 2009-03-06 03:51:30 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 177 | // Now, this loop is suitable for rotation. |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 178 | BasicBlock *OrigPreheader = L->getLoopPreheader(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 88974f4 | 2011-04-09 07:25:58 +0000 | [diff] [blame] | 180 | // If the loop could not be converted to canonical form, it must have an |
| 181 | // indirectbr in it, just give up. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 182 | if (!OrigPreheader) |
Chris Lattner | 88974f4 | 2011-04-09 07:25:58 +0000 | [diff] [blame] | 183 | return false; |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 184 | |
Dan Gohman | fc20b67 | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 185 | // Anything ScalarEvolution may know about this loop or the PHI nodes |
| 186 | // in its header will soon be invalidated. |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 187 | if (SE) |
| 188 | SE->forgetLoop(L); |
Dan Gohman | fc20b67 | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 189 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 190 | DEBUG(dbgs() << "LoopRotation: rotating "; L->dump()); |
| 191 | |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 192 | // Find new Loop header. NewHeader is a Header's one and only successor |
Chris Lattner | 57cb472 | 2009-01-26 01:57:01 +0000 | [diff] [blame] | 193 | // that is inside loop. Header's other successor is outside the |
| 194 | // loop. Otherwise loop is not suitable for rotation. |
Chris Lattner | 385f2ec | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 195 | BasicBlock *Exit = BI->getSuccessor(0); |
| 196 | BasicBlock *NewHeader = BI->getSuccessor(1); |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 197 | if (L->contains(Exit)) |
| 198 | std::swap(Exit, NewHeader); |
Chris Lattner | d67aaa6 | 2009-01-26 01:38:24 +0000 | [diff] [blame] | 199 | assert(NewHeader && "Unable to determine new loop header"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 200 | assert(L->contains(NewHeader) && !L->contains(Exit) && |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 201 | "Unable to determine loop header and exit blocks"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 202 | |
Dan Gohman | 091e440 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 203 | // This code assumes that the new header has exactly one predecessor. |
| 204 | // Remove any single-entry PHI nodes in it. |
Chris Lattner | 7b6647c | 2009-01-26 02:11:30 +0000 | [diff] [blame] | 205 | assert(NewHeader->getSinglePredecessor() && |
| 206 | "New header doesn't have one pred!"); |
| 207 | FoldSingleEntryPHINodes(NewHeader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 208 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 209 | // Begin by walking OrigHeader and populating ValueMap with an entry for |
| 210 | // each Instruction. |
Devang Patel | 88bc2c6 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 211 | BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end(); |
Chris Lattner | 2b3f20e | 2011-01-08 07:21:31 +0000 | [diff] [blame] | 212 | ValueToValueMapTy ValueMap; |
Devang Patel | b9af574 | 2007-04-09 19:04:21 +0000 | [diff] [blame] | 213 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 214 | // For PHI nodes, the value available in OldPreHeader is just the |
| 215 | // incoming value from OldPreHeader. |
| 216 | for (; PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Jay Foad | 372ad64 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 217 | ValueMap[PN] = PN->getIncomingValueForBlock(OrigPreheader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 218 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 219 | const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); |
| 220 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 221 | // For the rest of the instructions, either hoist to the OrigPreheader if |
| 222 | // possible or create a clone in the OldPreHeader if not. |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 223 | TerminatorInst *LoopEntryBranch = OrigPreheader->getTerminator(); |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 224 | while (I != E) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 225 | Instruction *Inst = &*I++; |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 226 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 227 | // If the instruction's operands are invariant and it doesn't read or write |
| 228 | // memory, then it is safe to hoist. Doing this doesn't change the order of |
| 229 | // execution in the preheader, but does prevent the instruction from |
| 230 | // executing in each iteration of the loop. This means it is safe to hoist |
| 231 | // something that might trap, but isn't safe to hoist something that reads |
| 232 | // memory (without proving that the loop doesn't write). |
| 233 | if (L->hasLoopInvariantOperands(Inst) && |
| 234 | !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() && |
Eli Friedman | c458885 | 2012-02-16 00:41:10 +0000 | [diff] [blame] | 235 | !isa<TerminatorInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst) && |
| 236 | !isa<AllocaInst>(Inst)) { |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 237 | Inst->moveBefore(LoopEntryBranch); |
| 238 | continue; |
| 239 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 240 | |
Chris Lattner | b01c24a | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 241 | // Otherwise, create a duplicate of the instruction. |
| 242 | Instruction *C = Inst->clone(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 244 | // Eagerly remap the operands of the instruction. |
| 245 | RemapInstruction(C, ValueMap, |
| 246 | RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 248 | // With the operands remapped, see if the instruction constant folds or is |
| 249 | // otherwise simplifyable. This commonly occurs because the entry from PHI |
| 250 | // nodes allows icmps and other instructions to fold. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 251 | // FIXME: Provide TLI, DT, AC to SimplifyInstruction. |
| 252 | Value *V = SimplifyInstruction(C, DL); |
Chris Lattner | 25ba40a | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 253 | if (V && LI->replacementPreservesLCSSAForm(C, V)) { |
Chris Lattner | 8c5defd | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 254 | // If so, then delete the temporary instruction and stick the folded value |
| 255 | // in the map. |
| 256 | delete C; |
| 257 | ValueMap[Inst] = V; |
| 258 | } else { |
| 259 | // Otherwise, stick the new instruction into the new block! |
| 260 | C->setName(Inst->getName()); |
| 261 | C->insertBefore(LoopEntryBranch); |
| 262 | ValueMap[Inst] = C; |
| 263 | } |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 266 | // Along with all the other instructions, we just cloned OrigHeader's |
| 267 | // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's |
| 268 | // successors by duplicating their incoming values for OrigHeader. |
| 269 | TerminatorInst *TI = OrigHeader->getTerminator(); |
Pete Cooper | ebcd748 | 2015-08-06 20:22:46 +0000 | [diff] [blame] | 270 | for (BasicBlock *SuccBB : TI->successors()) |
| 271 | for (BasicBlock::iterator BI = SuccBB->begin(); |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 272 | PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 273 | PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 274 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 275 | // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove |
| 276 | // OrigPreHeader's old terminator (the original branch into the loop), and |
| 277 | // remove the corresponding incoming values from the PHI nodes in OrigHeader. |
| 278 | LoopEntryBranch->eraseFromParent(); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 30f318e | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 280 | // If there were any uses of instructions in the duplicated block outside the |
| 281 | // loop, update them, inserting PHI nodes as required |
| 282 | RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 283 | |
Dan Gohman | b979794 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 284 | // NewHeader is now the header of the loop. |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 285 | L->moveToHeader(NewHeader); |
Chris Lattner | 2615130 | 2011-01-08 19:10:28 +0000 | [diff] [blame] | 286 | assert(L->getHeader() == NewHeader && "Latch block is our new header"); |
Devang Patel | f42389f | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 287 | |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 289 | // At this point, we've finished our major CFG changes. As part of cloning |
| 290 | // the loop into the preheader we've simplified instructions and the |
| 291 | // duplicated conditional branch may now be branching on a constant. If it is |
| 292 | // branching on a constant and if that constant means that we enter the loop, |
| 293 | // then we fold away the cond branch to an uncond branch. This simplifies the |
| 294 | // loop in cases important for nested loops, and it also means we don't have |
| 295 | // to split as many edges. |
| 296 | BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator()); |
| 297 | assert(PHBI->isConditional() && "Should be clone of BI condbr!"); |
| 298 | if (!isa<ConstantInt>(PHBI->getCondition()) || |
| 299 | PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) |
| 300 | != NewHeader) { |
| 301 | // The conditional branch can't be folded, handle the general case. |
| 302 | // Update DominatorTree to reflect the CFG change we just made. Then split |
| 303 | // edges as necessary to preserve LoopSimplify form. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 304 | if (DT) { |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 305 | // Everything that was dominated by the old loop header is now dominated |
| 306 | // by the original loop preheader. Conceptually the header was merged |
| 307 | // into the preheader, even though we reuse the actual block as a new |
| 308 | // loop latch. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 309 | DomTreeNode *OrigHeaderNode = DT->getNode(OrigHeader); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 310 | SmallVector<DomTreeNode *, 8> HeaderChildren(OrigHeaderNode->begin(), |
| 311 | OrigHeaderNode->end()); |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 312 | DomTreeNode *OrigPreheaderNode = DT->getNode(OrigPreheader); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 313 | for (unsigned I = 0, E = HeaderChildren.size(); I != E; ++I) |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 314 | DT->changeImmediateDominator(HeaderChildren[I], OrigPreheaderNode); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 315 | |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 316 | assert(DT->getNode(Exit)->getIDom() == OrigPreheaderNode); |
| 317 | assert(DT->getNode(NewHeader)->getIDom() == OrigPreheaderNode); |
Benjamin Kramer | 3be6a48 | 2012-09-01 12:04:51 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 319 | // Update OrigHeader to be dominated by the new header block. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 320 | DT->changeImmediateDominator(OrigHeader, OrigLatch); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 321 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 323 | // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 324 | // thus is not a preheader anymore. |
| 325 | // Split the edge to form a real preheader. |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 326 | BasicBlock *NewPH = SplitCriticalEdge( |
| 327 | OrigPreheader, NewHeader, |
| 328 | CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA()); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 329 | NewPH->setName(NewHeader->getName() + ".lr.ph"); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 330 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 331 | // Preserve canonical loop form, which means that 'Exit' should have only |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 332 | // one predecessor. Note that Exit could be an exit block for multiple |
| 333 | // nested loops, causing both of the edges to now be critical and need to |
| 334 | // be split. |
| 335 | SmallVector<BasicBlock *, 4> ExitPreds(pred_begin(Exit), pred_end(Exit)); |
| 336 | bool SplitLatchEdge = false; |
| 337 | for (SmallVectorImpl<BasicBlock *>::iterator PI = ExitPreds.begin(), |
| 338 | PE = ExitPreds.end(); |
| 339 | PI != PE; ++PI) { |
| 340 | // We only need to split loop exit edges. |
| 341 | Loop *PredLoop = LI->getLoopFor(*PI); |
| 342 | if (!PredLoop || PredLoop->contains(Exit)) |
| 343 | continue; |
Benjamin Kramer | 911d5b3 | 2015-02-20 20:49:25 +0000 | [diff] [blame] | 344 | if (isa<IndirectBrInst>((*PI)->getTerminator())) |
| 345 | continue; |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 346 | SplitLatchEdge |= L->getLoopLatch() == *PI; |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 347 | BasicBlock *ExitSplit = SplitCriticalEdge( |
| 348 | *PI, Exit, CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA()); |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 349 | ExitSplit->moveBefore(Exit); |
| 350 | } |
| 351 | assert(SplitLatchEdge && |
| 352 | "Despite splitting all preds, failed to split latch exit?"); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 353 | } else { |
| 354 | // We can fold the conditional branch in the preheader, this makes things |
| 355 | // simpler. The first step is to remove the extra edge to the Exit block. |
| 356 | Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/); |
Devang Patel | c1f7c1d | 2011-04-29 20:38:55 +0000 | [diff] [blame] | 357 | BranchInst *NewBI = BranchInst::Create(NewHeader, PHBI); |
| 358 | NewBI->setDebugLoc(PHBI->getDebugLoc()); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 359 | PHBI->eraseFromParent(); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 361 | // With our CFG finalized, update DomTree if it is available. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 362 | if (DT) { |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 363 | // Update OrigHeader to be dominated by the new header block. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 364 | DT->changeImmediateDominator(NewHeader, OrigPreheader); |
| 365 | DT->changeImmediateDominator(OrigHeader, OrigLatch); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 366 | |
| 367 | // Brute force incremental dominator tree update. Call |
| 368 | // findNearestCommonDominator on all CFG predecessors of each child of the |
| 369 | // original header. |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 370 | DomTreeNode *OrigHeaderNode = DT->getNode(OrigHeader); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 371 | SmallVector<DomTreeNode *, 8> HeaderChildren(OrigHeaderNode->begin(), |
| 372 | OrigHeaderNode->end()); |
| 373 | bool Changed; |
| 374 | do { |
| 375 | Changed = false; |
| 376 | for (unsigned I = 0, E = HeaderChildren.size(); I != E; ++I) { |
| 377 | DomTreeNode *Node = HeaderChildren[I]; |
| 378 | BasicBlock *BB = Node->getBlock(); |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 379 | |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 380 | pred_iterator PI = pred_begin(BB); |
| 381 | BasicBlock *NearestDom = *PI; |
| 382 | for (pred_iterator PE = pred_end(BB); PI != PE; ++PI) |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 383 | NearestDom = DT->findNearestCommonDominator(NearestDom, *PI); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 384 | |
| 385 | // Remember if this changes the DomTree. |
| 386 | if (Node->getIDom()->getBlock() != NearestDom) { |
Chandler Carruth | 9420909 | 2015-01-18 02:08:05 +0000 | [diff] [blame] | 387 | DT->changeImmediateDominator(BB, NearestDom); |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 388 | Changed = true; |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 389 | } |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Benjamin Kramer | 599a4bb | 2012-09-02 11:57:22 +0000 | [diff] [blame] | 392 | // If the dominator changed, this may have an effect on other |
| 393 | // predecessors, continue until we reach a fixpoint. |
| 394 | } while (Changed); |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 395 | } |
Devang Patel | fac4d1f | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 396 | } |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 59c82f8 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 398 | assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation"); |
Chris Lattner | 063dca0 | 2011-01-08 18:52:51 +0000 | [diff] [blame] | 399 | assert(L->getLoopLatch() && "Invalid loop latch after loop rotation"); |
Chris Lattner | fee37c5 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 63fe78d | 2011-01-11 07:47:59 +0000 | [diff] [blame] | 401 | // Now that the CFG and DomTree are in a consistent state again, try to merge |
| 402 | // the OrigHeader block into OrigLatch. This will succeed if they are |
| 403 | // connected by an unconditional branch. This is just a cleanup so the |
| 404 | // emitted code isn't too gross in this common case. |
Chandler Carruth | b5c1153 | 2015-01-18 02:11:23 +0000 | [diff] [blame] | 405 | MergeBlockIntoPredecessor(OrigHeader, DT, LI); |
Andrew Trick | a20f198 | 2012-02-14 00:00:19 +0000 | [diff] [blame] | 406 | |
Benjamin Kramer | afdfdb5 | 2012-08-30 15:39:42 +0000 | [diff] [blame] | 407 | DEBUG(dbgs() << "LoopRotation: into "; L->dump()); |
| 408 | |
Chris Lattner | fee37c5 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 409 | ++NumRotated; |
| 410 | return true; |
Devang Patel | 8541978 | 2007-04-09 20:19:46 +0000 | [diff] [blame] | 411 | } |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 412 | |
| 413 | /// Determine whether the instructions in this range may be safely and cheaply |
| 414 | /// speculated. This is not an important enough situation to develop complex |
| 415 | /// heuristics. We handle a single arithmetic instruction along with any type |
| 416 | /// conversions. |
| 417 | static bool shouldSpeculateInstrs(BasicBlock::iterator Begin, |
| 418 | BasicBlock::iterator End, Loop *L) { |
| 419 | bool seenIncrement = false; |
| 420 | bool MultiExitLoop = false; |
| 421 | |
| 422 | if (!L->getExitingBlock()) |
| 423 | MultiExitLoop = true; |
| 424 | |
| 425 | for (BasicBlock::iterator I = Begin; I != End; ++I) { |
| 426 | |
| 427 | if (!isSafeToSpeculativelyExecute(&*I)) |
| 428 | return false; |
| 429 | |
| 430 | if (isa<DbgInfoIntrinsic>(I)) |
| 431 | continue; |
| 432 | |
| 433 | switch (I->getOpcode()) { |
| 434 | default: |
| 435 | return false; |
| 436 | case Instruction::GetElementPtr: |
| 437 | // GEPs are cheap if all indices are constant. |
| 438 | if (!cast<GEPOperator>(I)->hasAllConstantIndices()) |
| 439 | return false; |
| 440 | // fall-thru to increment case |
| 441 | case Instruction::Add: |
| 442 | case Instruction::Sub: |
| 443 | case Instruction::And: |
| 444 | case Instruction::Or: |
| 445 | case Instruction::Xor: |
| 446 | case Instruction::Shl: |
| 447 | case Instruction::LShr: |
| 448 | case Instruction::AShr: { |
| 449 | Value *IVOpnd = !isa<Constant>(I->getOperand(0)) |
| 450 | ? I->getOperand(0) |
| 451 | : !isa<Constant>(I->getOperand(1)) |
| 452 | ? I->getOperand(1) |
| 453 | : nullptr; |
| 454 | if (!IVOpnd) |
| 455 | return false; |
| 456 | |
| 457 | // If increment operand is used outside of the loop, this speculation |
| 458 | // could cause extra live range interference. |
| 459 | if (MultiExitLoop) { |
| 460 | for (User *UseI : IVOpnd->users()) { |
| 461 | auto *UserInst = cast<Instruction>(UseI); |
| 462 | if (!L->contains(UserInst)) |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (seenIncrement) |
| 468 | return false; |
| 469 | seenIncrement = true; |
| 470 | break; |
| 471 | } |
| 472 | case Instruction::Trunc: |
| 473 | case Instruction::ZExt: |
| 474 | case Instruction::SExt: |
| 475 | // ignore type conversions |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | /// Fold the loop tail into the loop exit by speculating the loop tail |
| 483 | /// instructions. Typically, this is a single post-increment. In the case of a |
| 484 | /// simple 2-block loop, hoisting the increment can be much better than |
| 485 | /// duplicating the entire loop header. In the case of loops with early exits, |
| 486 | /// rotation will not work anyway, but simplifyLoopLatch will put the loop in |
| 487 | /// canonical form so downstream passes can handle it. |
| 488 | /// |
| 489 | /// I don't believe this invalidates SCEV. |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 490 | static bool simplifyLoopLatch(Loop *L, LoopInfo *LI, DominatorTree *DT) { |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 491 | BasicBlock *Latch = L->getLoopLatch(); |
| 492 | if (!Latch || Latch->hasAddressTaken()) |
| 493 | return false; |
| 494 | |
| 495 | BranchInst *Jmp = dyn_cast<BranchInst>(Latch->getTerminator()); |
| 496 | if (!Jmp || !Jmp->isUnconditional()) |
| 497 | return false; |
| 498 | |
| 499 | BasicBlock *LastExit = Latch->getSinglePredecessor(); |
| 500 | if (!LastExit || !L->isLoopExiting(LastExit)) |
| 501 | return false; |
| 502 | |
| 503 | BranchInst *BI = dyn_cast<BranchInst>(LastExit->getTerminator()); |
| 504 | if (!BI) |
| 505 | return false; |
| 506 | |
| 507 | if (!shouldSpeculateInstrs(Latch->begin(), Jmp->getIterator(), L)) |
| 508 | return false; |
| 509 | |
| 510 | DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into " |
| 511 | << LastExit->getName() << "\n"); |
| 512 | |
| 513 | // Hoist the instructions from Latch into LastExit. |
| 514 | LastExit->getInstList().splice(BI->getIterator(), Latch->getInstList(), |
| 515 | Latch->begin(), Jmp->getIterator()); |
| 516 | |
| 517 | unsigned FallThruPath = BI->getSuccessor(0) == Latch ? 0 : 1; |
| 518 | BasicBlock *Header = Jmp->getSuccessor(0); |
| 519 | assert(Header == L->getHeader() && "expected a backward branch"); |
| 520 | |
| 521 | // Remove Latch from the CFG so that LastExit becomes the new Latch. |
| 522 | BI->setSuccessor(FallThruPath, Header); |
| 523 | Latch->replaceSuccessorsPhiUsesWith(LastExit); |
| 524 | Jmp->eraseFromParent(); |
| 525 | |
| 526 | // Nuke the Latch block. |
| 527 | assert(Latch->empty() && "unable to evacuate Latch"); |
| 528 | LI->removeBlock(Latch); |
| 529 | if (DT) |
| 530 | DT->eraseNode(Latch); |
| 531 | Latch->eraseFromParent(); |
| 532 | return true; |
| 533 | } |
| 534 | |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 535 | /// Rotate \c L as many times as possible. Return true if the loop is rotated |
| 536 | /// at least once. |
| 537 | static bool iterativelyRotateLoop(Loop *L, unsigned MaxHeaderSize, LoopInfo *LI, |
| 538 | const TargetTransformInfo *TTI, |
| 539 | AssumptionCache *AC, DominatorTree *DT, |
| 540 | ScalarEvolution *SE) { |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 541 | // Save the loop metadata. |
| 542 | MDNode *LoopMD = L->getLoopID(); |
| 543 | |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 544 | // Simplify the loop latch before attempting to rotate the header |
| 545 | // upward. Rotation may not be needed if the loop tail can be folded into the |
| 546 | // loop exit. |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 547 | bool SimplifiedLatch = simplifyLoopLatch(L, LI, DT); |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 548 | |
| 549 | // One loop can be rotated multiple times. |
| 550 | bool MadeChange = false; |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 551 | while (rotateLoop(L, MaxHeaderSize, LI, TTI, AC, DT, SE, SimplifiedLatch)) { |
Justin Bogner | a730045 | 2015-12-14 23:22:44 +0000 | [diff] [blame] | 552 | MadeChange = true; |
| 553 | SimplifiedLatch = false; |
| 554 | } |
| 555 | |
| 556 | // Restore the loop metadata. |
| 557 | // NB! We presume LoopRotation DOESN'T ADD its own metadata. |
| 558 | if ((MadeChange || SimplifiedLatch) && LoopMD) |
| 559 | L->setLoopID(LoopMD); |
| 560 | |
| 561 | return MadeChange; |
| 562 | } |
Justin Bogner | 6291b58 | 2015-12-14 23:22:48 +0000 | [diff] [blame] | 563 | |
| 564 | namespace { |
| 565 | |
| 566 | class LoopRotate : public LoopPass { |
| 567 | unsigned MaxHeaderSize; |
| 568 | |
| 569 | public: |
| 570 | static char ID; // Pass ID, replacement for typeid |
| 571 | LoopRotate(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) { |
| 572 | initializeLoopRotatePass(*PassRegistry::getPassRegistry()); |
| 573 | if (SpecifiedMaxHeaderSize == -1) |
| 574 | MaxHeaderSize = DefaultRotationThreshold; |
| 575 | else |
| 576 | MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize); |
| 577 | } |
| 578 | |
| 579 | // LCSSA form makes instruction renaming easier. |
| 580 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 581 | AU.addPreserved<AAResultsWrapperPass>(); |
| 582 | AU.addRequired<AssumptionCacheTracker>(); |
| 583 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 584 | AU.addRequired<LoopInfoWrapperPass>(); |
| 585 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 586 | AU.addRequiredID(LoopSimplifyID); |
| 587 | AU.addPreservedID(LoopSimplifyID); |
| 588 | AU.addRequiredID(LCSSAID); |
| 589 | AU.addPreservedID(LCSSAID); |
| 590 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
| 591 | AU.addPreserved<SCEVAAWrapperPass>(); |
| 592 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 593 | AU.addPreserved<BasicAAWrapperPass>(); |
| 594 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 595 | } |
| 596 | |
| 597 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 598 | if (skipOptnoneFunction(L)) |
| 599 | return false; |
| 600 | Function &F = *L->getHeader()->getParent(); |
| 601 | |
| 602 | auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 603 | const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 604 | auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
| 605 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 606 | auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 607 | auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>(); |
| 608 | auto *SE = SEWP ? &SEWP->getSE() : nullptr; |
| 609 | |
| 610 | return iterativelyRotateLoop(L, MaxHeaderSize, LI, TTI, AC, DT, SE); |
| 611 | } |
| 612 | }; |
| 613 | } |
| 614 | |
| 615 | char LoopRotate::ID = 0; |
| 616 | INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
| 617 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 618 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 619 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 620 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 621 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 622 | INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass) |
| 623 | INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass) |
| 624 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
| 625 | INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
| 626 | |
| 627 | Pass *llvm::createLoopRotatePass(int MaxHeaderSize) { |
| 628 | return new LoopRotate(MaxHeaderSize); |
| 629 | } |