Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1 | //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass transforms loops that contain branches on loop-invariant conditions |
| 11 | // to have multiple loops. For example, it turns the left into the right code: |
| 12 | // |
| 13 | // for (...) if (lic) |
| 14 | // A for (...) |
| 15 | // if (lic) A; B; C |
| 16 | // B else |
| 17 | // C for (...) |
| 18 | // A; C |
| 19 | // |
| 20 | // This can increase the size of the code exponentially (doubling it every time |
| 21 | // a loop is unswitched) so we only unswitch if the resultant code will be |
| 22 | // smaller than a threshold. |
| 23 | // |
| 24 | // This pass expects LICM to be run before it to hoist invariant conditions out |
| 25 | // of the loop, to make the unswitching opportunity obvious. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #define DEBUG_TYPE "loop-unswitch" |
| 30 | #include "llvm/Transforms/Scalar.h" |
| 31 | #include "llvm/Constants.h" |
| 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/Instructions.h" |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/LoopInfo.h" |
| 35 | #include "llvm/Transforms/Utils/Cloning.h" |
| 36 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 8976219 | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/CommandLine.h" |
Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Chris Lattner | c597b8a | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 41 | #include <iostream> |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 42 | #include <set> |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
| 45 | namespace { |
| 46 | Statistic<> NumUnswitched("loop-unswitch", "Number of loops unswitched"); |
Chris Lattner | 8976219 | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 47 | cl::opt<unsigned> |
| 48 | Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), |
| 49 | cl::init(10), cl::Hidden); |
| 50 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 51 | class LoopUnswitch : public FunctionPass { |
| 52 | LoopInfo *LI; // Loop information |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 53 | public: |
| 54 | virtual bool runOnFunction(Function &F); |
| 55 | bool visitLoop(Loop *L); |
| 56 | |
| 57 | /// This transformation requires natural loop information & requires that |
| 58 | /// loop preheaders be inserted into the CFG... |
| 59 | /// |
| 60 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 61 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | 4f0e66d | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 62 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 63 | AU.addRequired<LoopInfo>(); |
| 64 | AU.addPreserved<LoopInfo>(); |
| 65 | } |
| 66 | |
| 67 | private: |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 68 | unsigned getLoopUnswitchCost(Loop *L, Value *LIC); |
Chris Lattner | 4f0e66d | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 69 | void VersionLoop(Value *LIC, Loop *L, Loop *&Out1, Loop *&Out2); |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 70 | BasicBlock *SplitBlock(BasicBlock *BB, bool SplitAtTop); |
| 71 | void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, bool Val); |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 72 | void UnswitchTrivialCondition(Loop *L, Value *Cond, bool EntersLoopOnCond, |
| 73 | BasicBlock *ExitBlock); |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 74 | }; |
| 75 | RegisterOpt<LoopUnswitch> X("loop-unswitch", "Unswitch loops"); |
| 76 | } |
| 77 | |
Jeff Cohen | eca0d0f | 2005-01-06 05:47:18 +0000 | [diff] [blame] | 78 | FunctionPass *llvm::createLoopUnswitchPass() { return new LoopUnswitch(); } |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 79 | |
| 80 | bool LoopUnswitch::runOnFunction(Function &F) { |
| 81 | bool Changed = false; |
| 82 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 83 | |
| 84 | // Transform all the top-level loops. Copy the loop list so that the child |
| 85 | // can update the loop tree if it needs to delete the loop. |
| 86 | std::vector<Loop*> SubLoops(LI->begin(), LI->end()); |
| 87 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 88 | Changed |= visitLoop(SubLoops[i]); |
| 89 | |
| 90 | return Changed; |
| 91 | } |
| 92 | |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 93 | |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 94 | /// LoopValuesUsedOutsideLoop - Return true if there are any values defined in |
| 95 | /// the loop that are used by instructions outside of it. |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 96 | static bool LoopValuesUsedOutsideLoop(Loop *L) { |
| 97 | // We will be doing lots of "loop contains block" queries. Loop::contains is |
| 98 | // linear time, use a set to speed this up. |
| 99 | std::set<BasicBlock*> LoopBlocks; |
| 100 | |
| 101 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 102 | BB != E; ++BB) |
| 103 | LoopBlocks.insert(*BB); |
| 104 | |
| 105 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 106 | BB != E; ++BB) { |
| 107 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I) |
| 108 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 109 | ++UI) { |
| 110 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
| 111 | if (!LoopBlocks.count(UserBB)) |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 118 | /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is |
| 119 | /// trivial: that is, that the condition controls whether or not the loop does |
| 120 | /// anything at all. If this is a trivial condition, unswitching produces no |
| 121 | /// code duplications (equivalently, it produces a simpler loop and a new empty |
| 122 | /// loop, which gets deleted). |
| 123 | /// |
| 124 | /// If this is a trivial condition, return ConstantBool::True if the loop body |
| 125 | /// runs when the condition is true, False if the loop body executes when the |
| 126 | /// condition is false. Otherwise, return null to indicate a complex condition. |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 127 | static bool IsTrivialUnswitchCondition(Loop *L, Value *Cond, |
| 128 | bool *CondEntersLoop = 0, |
| 129 | BasicBlock **LoopExit = 0) { |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 130 | BasicBlock *Header = L->getHeader(); |
| 131 | BranchInst *HeaderTerm = dyn_cast<BranchInst>(Header->getTerminator()); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 132 | |
| 133 | // If the header block doesn't end with a conditional branch on Cond, we can't |
| 134 | // handle it. |
| 135 | if (!HeaderTerm || !HeaderTerm->isConditional() || |
| 136 | HeaderTerm->getCondition() != Cond) |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 137 | return false; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 138 | |
| 139 | // Check to see if the conditional branch goes to the latch block. If not, |
| 140 | // it's not trivial. This also determines the value of Cond that will execute |
| 141 | // the loop. |
| 142 | BasicBlock *Latch = L->getLoopLatch(); |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 143 | if (HeaderTerm->getSuccessor(1) == Latch) { |
| 144 | if (CondEntersLoop) *CondEntersLoop = true; |
| 145 | } else if (HeaderTerm->getSuccessor(0) == Latch) |
| 146 | if (CondEntersLoop) *CondEntersLoop = false; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 147 | else |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 148 | return false; // Doesn't branch to latch block. |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 149 | |
| 150 | // The latch block must end with a conditional branch where one edge goes to |
| 151 | // the header (this much we know) and one edge goes OUT of the loop. |
| 152 | BranchInst *LatchBranch = dyn_cast<BranchInst>(Latch->getTerminator()); |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 153 | if (!LatchBranch || !LatchBranch->isConditional()) return false; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 154 | |
| 155 | if (LatchBranch->getSuccessor(0) == Header) { |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 156 | if (L->contains(LatchBranch->getSuccessor(1))) return false; |
| 157 | if (LoopExit) *LoopExit = LatchBranch->getSuccessor(1); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 158 | } else { |
| 159 | assert(LatchBranch->getSuccessor(1) == Header); |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 160 | if (L->contains(LatchBranch->getSuccessor(0))) return false; |
| 161 | if (LoopExit) *LoopExit = LatchBranch->getSuccessor(0); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // We already know that nothing uses any scalar values defined inside of this |
| 165 | // loop. As such, we just have to check to see if this loop will execute any |
| 166 | // side-effecting instructions (e.g. stores, calls, volatile loads) in the |
| 167 | // part of the loop that the code *would* execute. |
| 168 | for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I) |
| 169 | if (I->mayWriteToMemory()) |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 170 | return false; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 171 | for (BasicBlock::iterator I = Latch->begin(), E = Latch->end(); I != E; ++I) |
| 172 | if (I->mayWriteToMemory()) |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 173 | return false; |
| 174 | return true; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /// getLoopUnswitchCost - Return the cost (code size growth) that will happen if |
| 178 | /// we choose to unswitch the specified loop on the specified value. |
| 179 | /// |
| 180 | unsigned LoopUnswitch::getLoopUnswitchCost(Loop *L, Value *LIC) { |
| 181 | // If the condition is trivial, always unswitch. There is no code growth for |
| 182 | // this case. |
| 183 | if (IsTrivialUnswitchCondition(L, LIC)) |
| 184 | return 0; |
| 185 | |
| 186 | unsigned Cost = 0; |
| 187 | // FIXME: this is brain dead. It should take into consideration code |
| 188 | // shrinkage. |
| 189 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 190 | I != E; ++I) { |
| 191 | BasicBlock *BB = *I; |
| 192 | // Do not include empty blocks in the cost calculation. This happen due to |
| 193 | // loop canonicalization and will be removed. |
| 194 | if (BB->begin() == BasicBlock::iterator(BB->getTerminator())) |
| 195 | continue; |
| 196 | |
| 197 | // Count basic blocks. |
| 198 | ++Cost; |
| 199 | } |
| 200 | |
| 201 | return Cost; |
| 202 | } |
| 203 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 204 | bool LoopUnswitch::visitLoop(Loop *L) { |
| 205 | bool Changed = false; |
| 206 | |
| 207 | // Recurse through all subloops before we process this loop. Copy the loop |
| 208 | // list so that the child can update the loop tree if it needs to delete the |
| 209 | // loop. |
| 210 | std::vector<Loop*> SubLoops(L->begin(), L->end()); |
| 211 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 212 | Changed |= visitLoop(SubLoops[i]); |
| 213 | |
| 214 | // Loop over all of the basic blocks in the loop. If we find an interior |
| 215 | // block that is branching on a loop-invariant condition, we can unswitch this |
| 216 | // loop. |
| 217 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 218 | I != E; ++I) { |
| 219 | TerminatorInst *TI = (*I)->getTerminator(); |
| 220 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 221 | if (!isa<Constant>(SI) && L->isLoopInvariant(SI->getCondition())) |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 222 | DEBUG(std::cerr << "TODO: Implement unswitching 'switch' loop %" |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 223 | << L->getHeader()->getName() << ", cost = " |
| 224 | << L->getBlocks().size() << "\n" << **I); |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | |
| 228 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
| 229 | if (!BI) continue; |
| 230 | |
| 231 | // If this isn't branching on an invariant condition, we can't unswitch it. |
| 232 | if (!BI->isConditional() || isa<Constant>(BI->getCondition()) || |
| 233 | !L->isLoopInvariant(BI->getCondition())) |
| 234 | continue; |
| 235 | |
| 236 | // Check to see if it would be profitable to unswitch this loop. |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 237 | if (getLoopUnswitchCost(L, BI->getCondition()) > Threshold) { |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 238 | // FIXME: this should estimate growth by the amount of code shared by the |
| 239 | // resultant unswitched loops. This should have no code growth: |
| 240 | // for () { if (iv) {...} } |
| 241 | // as one copy of the loop will be empty. |
| 242 | // |
| 243 | DEBUG(std::cerr << "NOT unswitching loop %" |
| 244 | << L->getHeader()->getName() << ", cost too high: " |
| 245 | << L->getBlocks().size() << "\n"); |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | // If this loop has live-out values, we can't unswitch it. We need something |
| 250 | // like loop-closed SSA form in order to know how to insert PHI nodes for |
| 251 | // these values. |
| 252 | if (LoopValuesUsedOutsideLoop(L)) { |
| 253 | DEBUG(std::cerr << "NOT unswitching loop %" |
| 254 | << L->getHeader()->getName() |
| 255 | << ", a loop value is used outside loop!\n"); |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | //std::cerr << "BEFORE:\n"; LI->dump(); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 260 | Loop *NewLoop1 = 0, *NewLoop2 = 0; |
| 261 | |
| 262 | // If this is a trivial condition to unswitch (which results in no code |
| 263 | // duplication), do it now. |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 264 | bool EntersLoopOnCond; |
| 265 | BasicBlock *ExitBlock; |
| 266 | if (IsTrivialUnswitchCondition(L, BI->getCondition(), &EntersLoopOnCond, |
| 267 | &ExitBlock)) { |
| 268 | UnswitchTrivialCondition(L, BI->getCondition(), |
| 269 | EntersLoopOnCond, ExitBlock); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 270 | NewLoop1 = L; |
| 271 | } else { |
| 272 | VersionLoop(BI->getCondition(), L, NewLoop1, NewLoop2); |
| 273 | } |
| 274 | |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 275 | //std::cerr << "AFTER:\n"; LI->dump(); |
| 276 | |
Chris Lattner | 4f0e66d | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 277 | // Try to unswitch each of our new loops now! |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 278 | if (NewLoop1) visitLoop(NewLoop1); |
| 279 | if (NewLoop2) visitLoop(NewLoop2); |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 280 | return true; |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 281 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 282 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 283 | return Changed; |
| 284 | } |
| 285 | |
| 286 | /// SplitBlock - Split the specified basic block into two pieces. If SplitAtTop |
| 287 | /// is false, this splits the block so the second half only has an unconditional |
| 288 | /// branch. If SplitAtTop is true, it makes it so the first half of the block |
| 289 | /// only has an unconditional branch in it. |
| 290 | /// |
| 291 | /// This method updates the LoopInfo for this function to correctly reflect the |
| 292 | /// CFG changes made. |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 293 | /// |
| 294 | /// This routine returns the new basic block that was inserted, which is always |
| 295 | /// the later part of the block. |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 296 | BasicBlock *LoopUnswitch::SplitBlock(BasicBlock *BB, bool SplitAtTop) { |
| 297 | BasicBlock::iterator SplitPoint; |
| 298 | if (!SplitAtTop) |
| 299 | SplitPoint = BB->getTerminator(); |
| 300 | else { |
| 301 | SplitPoint = BB->begin(); |
| 302 | while (isa<PHINode>(SplitPoint)) ++SplitPoint; |
| 303 | } |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 304 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 305 | BasicBlock *New = BB->splitBasicBlock(SplitPoint, BB->getName()+".tail"); |
| 306 | // New now lives in whichever loop that BB used to. |
| 307 | if (Loop *L = LI->getLoopFor(BB)) |
| 308 | L->addBasicBlockToLoop(New, *LI); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 309 | return New; |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 313 | // RemapInstruction - Convert the instruction operands from referencing the |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 314 | // current values into those specified by ValueMap. |
| 315 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 316 | static inline void RemapInstruction(Instruction *I, |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 317 | std::map<const Value *, Value*> &ValueMap) { |
| 318 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 319 | Value *Op = I->getOperand(op); |
| 320 | std::map<const Value *, Value*>::iterator It = ValueMap.find(Op); |
| 321 | if (It != ValueMap.end()) Op = It->second; |
| 322 | I->setOperand(op, Op); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /// CloneLoop - Recursively clone the specified loop and all of its children, |
| 327 | /// mapping the blocks with the specified map. |
| 328 | static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM, |
| 329 | LoopInfo *LI) { |
| 330 | Loop *New = new Loop(); |
| 331 | |
| 332 | if (PL) |
| 333 | PL->addChildLoop(New); |
| 334 | else |
| 335 | LI->addTopLevelLoop(New); |
| 336 | |
| 337 | // Add all of the blocks in L to the new loop. |
| 338 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 339 | I != E; ++I) |
| 340 | if (LI->getLoopFor(*I) == L) |
| 341 | New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI); |
| 342 | |
| 343 | // Add all of the subloops to the new loop. |
| 344 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 345 | CloneLoop(*I, New, VM, LI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 346 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 347 | return New; |
| 348 | } |
| 349 | |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 350 | /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable |
| 351 | /// condition in it (a cond branch from its header block to its latch block, |
| 352 | /// where the path through the loop that doesn't execute its body has no |
| 353 | /// side-effects), unswitch it. This doesn't involve any code duplication, just |
| 354 | /// moving the conditional branch outside of the loop and updating loop info. |
| 355 | void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 356 | bool EnterOnCond, |
| 357 | BasicBlock *ExitBlock) { |
Chris Lattner | 3fc3148 | 2006-02-10 01:36:35 +0000 | [diff] [blame] | 358 | DEBUG(std::cerr << "loop-unswitch: Trivial-Unswitch loop %" |
| 359 | << L->getHeader()->getName() << " [" << L->getBlocks().size() |
| 360 | << " blocks] in Function " << L->getHeader()->getParent()->getName() |
| 361 | << " on cond:" << *Cond << "\n"); |
| 362 | |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 363 | // First step, split the preahder, so that we know that there is a safe place |
| 364 | // to insert the conditional branch. We will change 'OrigPH' to have a |
| 365 | // conditional branch on Cond. |
| 366 | BasicBlock *OrigPH = L->getLoopPreheader(); |
| 367 | BasicBlock *NewPH = SplitBlock(OrigPH, false); |
| 368 | |
| 369 | // Now that we have a place to insert the conditional branch, create a place |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 370 | // to branch to: this is the exit block out of the loop that we should |
| 371 | // short-circuit to. |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 372 | |
| 373 | // Split this block now, so that the loop maintains its exit block. |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 374 | assert(!L->contains(ExitBlock) && "Exit block is in the loop?"); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 375 | BasicBlock *NewExit = SplitBlock(ExitBlock, true); |
| 376 | |
| 377 | // Okay, now we have a position to branch from and a position to branch to, |
| 378 | // insert the new conditional branch. |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 379 | new BranchInst(EnterOnCond ? NewPH : NewExit, EnterOnCond ? NewExit : NewPH, |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 380 | Cond, OrigPH->getTerminator()); |
| 381 | OrigPH->getTerminator()->eraseFromParent(); |
| 382 | |
| 383 | // Now that we know that the loop is never entered when this condition is a |
| 384 | // particular value, rewrite the loop with this info. We know that this will |
| 385 | // at least eliminate the old branch. |
Chris Lattner | 4935417 | 2006-02-10 02:01:22 +0000 | [diff] [blame^] | 386 | RewriteLoopBodyWithConditionConstant(L, Cond, EnterOnCond); |
Chris Lattner | 3fc3148 | 2006-02-10 01:36:35 +0000 | [diff] [blame] | 387 | |
| 388 | ++NumUnswitched; |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 391 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 392 | /// VersionLoop - We determined that the loop is profitable to unswitch and |
| 393 | /// contains a branch on a loop invariant condition. Split it into loop |
Chris Lattner | 4f0e66d | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 394 | /// versions and test the condition outside of either loop. Return the loops |
| 395 | /// created as Out1/Out2. |
| 396 | void LoopUnswitch::VersionLoop(Value *LIC, Loop *L, Loop *&Out1, Loop *&Out2) { |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 397 | Function *F = L->getHeader()->getParent(); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 398 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 399 | DEBUG(std::cerr << "loop-unswitch: Unswitching loop %" |
| 400 | << L->getHeader()->getName() << " [" << L->getBlocks().size() |
| 401 | << " blocks] in Function " << F->getName() |
| 402 | << " on cond:" << *LIC << "\n"); |
| 403 | |
| 404 | std::vector<BasicBlock*> LoopBlocks; |
| 405 | |
| 406 | // First step, split the preheader and exit blocks, and add these blocks to |
| 407 | // the LoopBlocks list. |
| 408 | BasicBlock *OrigPreheader = L->getLoopPreheader(); |
| 409 | LoopBlocks.push_back(SplitBlock(OrigPreheader, false)); |
| 410 | |
| 411 | // We want the loop to come after the preheader, but before the exit blocks. |
| 412 | LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); |
| 413 | |
| 414 | std::vector<BasicBlock*> ExitBlocks; |
| 415 | L->getExitBlocks(ExitBlocks); |
| 416 | std::sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 417 | ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()), |
| 418 | ExitBlocks.end()); |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 419 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 420 | SplitBlock(ExitBlocks[i], true); |
| 421 | LoopBlocks.push_back(ExitBlocks[i]); |
| 422 | } |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 423 | |
| 424 | // Next step, clone all of the basic blocks that make up the loop (including |
| 425 | // the loop preheader and exit blocks), keeping track of the mapping between |
| 426 | // the instructions and blocks. |
| 427 | std::vector<BasicBlock*> NewBlocks; |
| 428 | NewBlocks.reserve(LoopBlocks.size()); |
| 429 | std::map<const Value*, Value*> ValueMap; |
| 430 | for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { |
| 431 | NewBlocks.push_back(CloneBasicBlock(LoopBlocks[i], ValueMap, ".us", F)); |
| 432 | ValueMap[LoopBlocks[i]] = NewBlocks.back(); // Keep the BB mapping. |
| 433 | } |
| 434 | |
| 435 | // Splice the newly inserted blocks into the function right before the |
| 436 | // original preheader. |
| 437 | F->getBasicBlockList().splice(LoopBlocks[0], F->getBasicBlockList(), |
| 438 | NewBlocks[0], F->end()); |
| 439 | |
| 440 | // Now we create the new Loop object for the versioned loop. |
| 441 | Loop *NewLoop = CloneLoop(L, L->getParentLoop(), ValueMap, LI); |
| 442 | if (Loop *Parent = L->getParentLoop()) { |
| 443 | // Make sure to add the cloned preheader and exit blocks to the parent loop |
| 444 | // as well. |
| 445 | Parent->addBasicBlockToLoop(NewBlocks[0], *LI); |
| 446 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 447 | Parent->addBasicBlockToLoop(cast<BasicBlock>(ValueMap[ExitBlocks[i]]), |
| 448 | *LI); |
| 449 | } |
| 450 | |
| 451 | // Rewrite the code to refer to itself. |
| 452 | for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) |
| 453 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 454 | E = NewBlocks[i]->end(); I != E; ++I) |
| 455 | RemapInstruction(I, ValueMap); |
Chris Lattner | 2826e05 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 456 | |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 457 | // Rewrite the original preheader to select between versions of the loop. |
| 458 | assert(isa<BranchInst>(OrigPreheader->getTerminator()) && |
| 459 | cast<BranchInst>(OrigPreheader->getTerminator())->isUnconditional() && |
| 460 | OrigPreheader->getTerminator()->getSuccessor(0) == LoopBlocks[0] && |
| 461 | "Preheader splitting did not work correctly!"); |
| 462 | // Remove the unconditional branch to LoopBlocks[0]. |
| 463 | OrigPreheader->getInstList().pop_back(); |
| 464 | |
| 465 | // Insert a conditional branch on LIC to the two preheaders. The original |
| 466 | // code is the true version and the new code is the false version. |
| 467 | new BranchInst(LoopBlocks[0], NewBlocks[0], LIC, OrigPreheader); |
| 468 | |
| 469 | // Now we rewrite the original code to know that the condition is true and the |
| 470 | // new code to know that the condition is false. |
| 471 | RewriteLoopBodyWithConditionConstant(L, LIC, true); |
| 472 | RewriteLoopBodyWithConditionConstant(NewLoop, LIC, false); |
| 473 | ++NumUnswitched; |
Chris Lattner | 4f0e66d | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 474 | Out1 = L; |
| 475 | Out2 = NewLoop; |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // RewriteLoopBodyWithConditionConstant - We know that the boolean value LIC has |
| 479 | // the value specified by Val in the specified loop. Rewrite any uses of LIC or |
| 480 | // of properties correlated to it. |
| 481 | void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
| 482 | bool Val) { |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 483 | assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 484 | // FIXME: Support correlated properties, like: |
| 485 | // for (...) |
| 486 | // if (li1 < li2) |
| 487 | // ... |
| 488 | // if (li1 > li2) |
| 489 | // ... |
| 490 | ConstantBool *BoolVal = ConstantBool::get(Val); |
| 491 | |
| 492 | std::vector<User*> Users(LIC->use_begin(), LIC->use_end()); |
| 493 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
Chris Lattner | ed7a67b | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 494 | if (Instruction *U = cast<Instruction>(Users[i])) |
Chris Lattner | f48f777 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 495 | if (L->contains(U->getParent())) |
| 496 | U->replaceUsesOfWith(LIC, BoolVal); |
| 497 | } |