blob: 5cc7079301bee1ca1b72ae63416acfb5ef40221c [file] [log] [blame]
Chris Lattnerf48f7772004-04-19 18:07:02 +00001//===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnerf48f7772004-04-19 18:07:02 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnerf48f7772004-04-19 18:07:02 +00008//===----------------------------------------------------------------------===//
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 Lattnerf48f7772004-04-19 18:07:02 +000034#include "llvm/Analysis/LoopInfo.h"
35#include "llvm/Transforms/Utils/Cloning.h"
36#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerec6b40a2006-02-10 19:08:15 +000037#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000038#include "llvm/ADT/Statistic.h"
Chris Lattner89762192006-02-09 20:15:48 +000039#include "llvm/Support/Debug.h"
40#include "llvm/Support/CommandLine.h"
Alkis Evlogimenosa5c04ee2004-09-03 18:19:51 +000041#include <algorithm>
Chris Lattnerc597b8a2006-01-22 23:32:06 +000042#include <iostream>
Chris Lattner2826e052006-02-09 19:14:52 +000043#include <set>
Chris Lattnerf48f7772004-04-19 18:07:02 +000044using namespace llvm;
45
46namespace {
Chris Lattner0b8ec1a2006-02-14 01:01:41 +000047 Statistic<> NumBranches("loop-unswitch", "Number of branches unswitched");
48 Statistic<> NumSwitches("loop-unswitch", "Number of switches unswitched");
49 Statistic<> NumSelects ("loop-unswitch", "Number of selects unswitched");
50 Statistic<> NumTrivial ("loop-unswitch",
51 "Number of unswitches that are trivial");
Chris Lattner89762192006-02-09 20:15:48 +000052 cl::opt<unsigned>
53 Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"),
54 cl::init(10), cl::Hidden);
55
Chris Lattnerf48f7772004-04-19 18:07:02 +000056 class LoopUnswitch : public FunctionPass {
57 LoopInfo *LI; // Loop information
Chris Lattnerf48f7772004-04-19 18:07:02 +000058 public:
59 virtual bool runOnFunction(Function &F);
60 bool visitLoop(Loop *L);
61
62 /// This transformation requires natural loop information & requires that
63 /// loop preheaders be inserted into the CFG...
64 ///
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.addRequiredID(LoopSimplifyID);
Chris Lattner4f0e66d2006-02-09 22:15:42 +000067 AU.addPreservedID(LoopSimplifyID);
Chris Lattnerf48f7772004-04-19 18:07:02 +000068 AU.addRequired<LoopInfo>();
69 AU.addPreserved<LoopInfo>();
70 }
71
72 private:
Chris Lattnerfbadd7e2006-02-11 00:43:37 +000073 bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L);
Chris Lattnered7a67b2006-02-10 01:24:09 +000074 unsigned getLoopUnswitchCost(Loop *L, Value *LIC);
Chris Lattnerfbadd7e2006-02-11 00:43:37 +000075 void VersionLoop(Value *LIC, Constant *OnVal,
76 Loop *L, Loop *&Out1, Loop *&Out2);
Chris Lattnerfe4151e2006-02-10 23:16:39 +000077 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To);
Chris Lattnere5cb76d2006-02-15 22:03:36 +000078 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt);
Chris Lattnerfbadd7e2006-02-11 00:43:37 +000079 void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,Constant *Val,
80 bool isEqual);
Chris Lattner01db04e2006-02-15 01:44:42 +000081 void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
Chris Lattner49354172006-02-10 02:01:22 +000082 BasicBlock *ExitBlock);
Chris Lattnerf48f7772004-04-19 18:07:02 +000083 };
84 RegisterOpt<LoopUnswitch> X("loop-unswitch", "Unswitch loops");
85}
86
Jeff Coheneca0d0f2005-01-06 05:47:18 +000087FunctionPass *llvm::createLoopUnswitchPass() { return new LoopUnswitch(); }
Chris Lattnerf48f7772004-04-19 18:07:02 +000088
89bool LoopUnswitch::runOnFunction(Function &F) {
90 bool Changed = false;
91 LI = &getAnalysis<LoopInfo>();
Chris Lattnerf48f7772004-04-19 18:07:02 +000092
93 // Transform all the top-level loops. Copy the loop list so that the child
94 // can update the loop tree if it needs to delete the loop.
95 std::vector<Loop*> SubLoops(LI->begin(), LI->end());
96 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
97 Changed |= visitLoop(SubLoops[i]);
98
99 return Changed;
100}
101
Chris Lattner2826e052006-02-09 19:14:52 +0000102
Chris Lattnered7a67b2006-02-10 01:24:09 +0000103/// LoopValuesUsedOutsideLoop - Return true if there are any values defined in
104/// the loop that are used by instructions outside of it.
Chris Lattner2826e052006-02-09 19:14:52 +0000105static bool LoopValuesUsedOutsideLoop(Loop *L) {
106 // We will be doing lots of "loop contains block" queries. Loop::contains is
107 // linear time, use a set to speed this up.
108 std::set<BasicBlock*> LoopBlocks;
109
110 for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
111 BB != E; ++BB)
112 LoopBlocks.insert(*BB);
113
114 for (Loop::block_iterator BB = L->block_begin(), E = L->block_end();
115 BB != E; ++BB) {
116 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I)
117 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
118 ++UI) {
119 BasicBlock *UserBB = cast<Instruction>(*UI)->getParent();
120 if (!LoopBlocks.count(UserBB))
121 return true;
122 }
123 }
124 return false;
125}
126
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000127/// isTrivialLoopExitBlock - Check to see if all paths from BB either:
128/// 1. Exit the loop with no side effects.
129/// 2. Branch to the latch block with no side-effects.
130///
131/// If these conditions are true, we return true and set ExitBB to the block we
132/// exit through.
133///
134static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB,
135 BasicBlock *&ExitBB,
136 std::set<BasicBlock*> &Visited) {
Chris Lattner6e263152006-02-10 02:30:37 +0000137 BasicBlock *Header = L->getHeader();
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000138 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
139 if (!Visited.insert(*SI).second) {
140 // Already visited and Ok, end of recursion.
141 } else if (L->contains(*SI)) {
142 // Check to see if the successor is a trivial loop exit.
143 if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
144 return false;
145 } else {
146 // Otherwise, this is a loop exit, this is fine so long as this is the
147 // first exit.
148 if (ExitBB != 0) return false;
149 ExitBB = *SI;
150 }
Chris Lattner6e263152006-02-10 02:30:37 +0000151 }
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000152
153 // Okay, everything after this looks good, check to make sure that this block
154 // doesn't include any side effects.
155 for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
156 if (I->mayWriteToMemory())
157 return false;
158
159 return true;
Chris Lattner6e263152006-02-10 02:30:37 +0000160}
161
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000162static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) {
163 std::set<BasicBlock*> Visited;
164 Visited.insert(L->getHeader()); // Branches to header are ok.
165 Visited.insert(BB); // Don't revisit BB after we do.
166 BasicBlock *ExitBB = 0;
167 if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited))
168 return ExitBB;
169 return 0;
170}
Chris Lattner6e263152006-02-10 02:30:37 +0000171
Chris Lattnered7a67b2006-02-10 01:24:09 +0000172/// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
173/// trivial: that is, that the condition controls whether or not the loop does
174/// anything at all. If this is a trivial condition, unswitching produces no
175/// code duplications (equivalently, it produces a simpler loop and a new empty
176/// loop, which gets deleted).
177///
178/// If this is a trivial condition, return ConstantBool::True if the loop body
179/// runs when the condition is true, False if the loop body executes when the
180/// condition is false. Otherwise, return null to indicate a complex condition.
Chris Lattner49354172006-02-10 02:01:22 +0000181static bool IsTrivialUnswitchCondition(Loop *L, Value *Cond,
Chris Lattner01db04e2006-02-15 01:44:42 +0000182 Constant **Val = 0,
Chris Lattner49354172006-02-10 02:01:22 +0000183 BasicBlock **LoopExit = 0) {
Chris Lattnered7a67b2006-02-10 01:24:09 +0000184 BasicBlock *Header = L->getHeader();
185 BranchInst *HeaderTerm = dyn_cast<BranchInst>(Header->getTerminator());
Chris Lattnered7a67b2006-02-10 01:24:09 +0000186
187 // If the header block doesn't end with a conditional branch on Cond, we can't
188 // handle it.
189 if (!HeaderTerm || !HeaderTerm->isConditional() ||
190 HeaderTerm->getCondition() != Cond)
Chris Lattner49354172006-02-10 02:01:22 +0000191 return false;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000192
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000193 // Check to see if a successor of the branch is guaranteed to go to the latch
194 // block or exit through a one exit block without having any side-effects. If
195 // so, determine the value of Cond that causes it to do this.
196 BasicBlock *LoopExitBlock = 0;
197 if ((LoopExitBlock = isTrivialLoopExitBlock(L, HeaderTerm->getSuccessor(0)))){
Chris Lattner01db04e2006-02-15 01:44:42 +0000198 if (Val) *Val = ConstantBool::True;
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000199 } else if ((LoopExitBlock =
200 isTrivialLoopExitBlock(L, HeaderTerm->getSuccessor(1)))) {
Chris Lattner01db04e2006-02-15 01:44:42 +0000201 if (Val) *Val = ConstantBool::False;
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000202 }
203
204 if (!LoopExitBlock)
205 return false; // Can't handle this.
Chris Lattnered7a67b2006-02-10 01:24:09 +0000206
Chris Lattner6e263152006-02-10 02:30:37 +0000207 if (LoopExit) *LoopExit = LoopExitBlock;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000208
209 // We already know that nothing uses any scalar values defined inside of this
210 // loop. As such, we just have to check to see if this loop will execute any
211 // side-effecting instructions (e.g. stores, calls, volatile loads) in the
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000212 // part of the loop that the code *would* execute. We already checked the
213 // tail, check the header now.
Chris Lattnered7a67b2006-02-10 01:24:09 +0000214 for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
215 if (I->mayWriteToMemory())
Chris Lattner49354172006-02-10 02:01:22 +0000216 return false;
Chris Lattner49354172006-02-10 02:01:22 +0000217 return true;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000218}
219
220/// getLoopUnswitchCost - Return the cost (code size growth) that will happen if
221/// we choose to unswitch the specified loop on the specified value.
222///
223unsigned LoopUnswitch::getLoopUnswitchCost(Loop *L, Value *LIC) {
224 // If the condition is trivial, always unswitch. There is no code growth for
225 // this case.
226 if (IsTrivialUnswitchCondition(L, LIC))
227 return 0;
228
229 unsigned Cost = 0;
230 // FIXME: this is brain dead. It should take into consideration code
231 // shrinkage.
232 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
233 I != E; ++I) {
234 BasicBlock *BB = *I;
235 // Do not include empty blocks in the cost calculation. This happen due to
236 // loop canonicalization and will be removed.
237 if (BB->begin() == BasicBlock::iterator(BB->getTerminator()))
238 continue;
239
240 // Count basic blocks.
241 ++Cost;
242 }
243
244 return Cost;
245}
246
Chris Lattner6e263152006-02-10 02:30:37 +0000247/// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is
248/// invariant in the loop, or has an invariant piece, return the invariant.
249/// Otherwise, return null.
250static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) {
251 // Constants should be folded, not unswitched on!
252 if (isa<Constant>(Cond)) return false;
253
254 // TODO: Handle: br (VARIANT|INVARIANT).
255 // TODO: Hoist simple expressions out of loops.
256 if (L->isLoopInvariant(Cond)) return Cond;
257
258 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond))
259 if (BO->getOpcode() == Instruction::And ||
260 BO->getOpcode() == Instruction::Or) {
261 // If either the left or right side is invariant, we can unswitch on this,
262 // which will cause the branch to go away in one loop and the condition to
263 // simplify in the other one.
264 if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed))
265 return LHS;
266 if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed))
267 return RHS;
268 }
269
270 return 0;
271}
272
Chris Lattnerf48f7772004-04-19 18:07:02 +0000273bool LoopUnswitch::visitLoop(Loop *L) {
274 bool Changed = false;
275
276 // Recurse through all subloops before we process this loop. Copy the loop
277 // list so that the child can update the loop tree if it needs to delete the
278 // loop.
279 std::vector<Loop*> SubLoops(L->begin(), L->end());
280 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
281 Changed |= visitLoop(SubLoops[i]);
282
283 // Loop over all of the basic blocks in the loop. If we find an interior
284 // block that is branching on a loop-invariant condition, we can unswitch this
285 // loop.
286 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
287 I != E; ++I) {
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000288 TerminatorInst *TI = (*I)->getTerminator();
289 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
290 // If this isn't branching on an invariant condition, we can't unswitch
291 // it.
292 if (BI->isConditional()) {
293 // See if this, or some part of it, is loop invariant. If so, we can
294 // unswitch on it if we desire.
295 Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), L, Changed);
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000296 if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) {
297 ++NumBranches;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000298 return true;
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000299 }
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000300 }
301 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
302 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), L, Changed);
303 if (LoopCond && SI->getNumCases() > 1) {
304 // Find a value to unswitch on:
305 // FIXME: this should chose the most expensive case!
306 Constant *UnswitchVal = SI->getCaseValue(1);
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000307 if (UnswitchIfProfitable(LoopCond, UnswitchVal, L)) {
308 ++NumSwitches;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000309 return true;
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000310 }
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000311 }
312 }
313
314 // Scan the instructions to check for unswitchable values.
Chris Lattnerf1b15162006-02-10 23:26:14 +0000315 for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
316 BBI != E; ++BBI)
317 if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
318 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), L, Changed);
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000319 if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) {
320 ++NumSelects;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000321 return true;
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000322 }
Chris Lattnerf1b15162006-02-10 23:26:14 +0000323 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000324 }
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000325
Chris Lattnerf48f7772004-04-19 18:07:02 +0000326 return Changed;
327}
328
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000329/// UnswitchIfProfitable - We have found that we can unswitch L when
330/// LoopCond == Val to simplify the loop. If we decide that this is profitable,
331/// unswitch the loop, reprocess the pieces, then return true.
332bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L){
333 // Check to see if it would be profitable to unswitch this loop.
334 if (getLoopUnswitchCost(L, LoopCond) > Threshold) {
335 // FIXME: this should estimate growth by the amount of code shared by the
336 // resultant unswitched loops.
337 //
338 DEBUG(std::cerr << "NOT unswitching loop %"
339 << L->getHeader()->getName() << ", cost too high: "
340 << L->getBlocks().size() << "\n");
341 return false;
342 }
343
344 // If this loop has live-out values, we can't unswitch it. We need something
345 // like loop-closed SSA form in order to know how to insert PHI nodes for
346 // these values.
347 if (LoopValuesUsedOutsideLoop(L)) {
348 DEBUG(std::cerr << "NOT unswitching loop %" << L->getHeader()->getName()
349 << ", a loop value is used outside loop!\n");
350 return false;
351 }
352
353 //std::cerr << "BEFORE:\n"; LI->dump();
354 Loop *NewLoop1 = 0, *NewLoop2 = 0;
355
356 // If this is a trivial condition to unswitch (which results in no code
357 // duplication), do it now.
Chris Lattner01db04e2006-02-15 01:44:42 +0000358 Constant *CondVal;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000359 BasicBlock *ExitBlock;
Chris Lattner01db04e2006-02-15 01:44:42 +0000360 if (IsTrivialUnswitchCondition(L, LoopCond, &CondVal, &ExitBlock)){
361 UnswitchTrivialCondition(L, LoopCond, CondVal, ExitBlock);
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000362 NewLoop1 = L;
363 } else {
364 VersionLoop(LoopCond, Val, L, NewLoop1, NewLoop2);
365 }
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000366
367 //std::cerr << "AFTER:\n"; LI->dump();
368
369 // Try to unswitch each of our new loops now!
370 if (NewLoop1) visitLoop(NewLoop1);
371 if (NewLoop2) visitLoop(NewLoop2);
372 return true;
373}
374
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000375/// SplitBlock - Split the specified block at the specified instruction - every
376/// thing before SplitPt stays in Old and everything starting with SplitPt moves
377/// to a new block. The two blocks are joined by an unconditional branch and
378/// the loop info is updated.
379///
380BasicBlock *LoopUnswitch::SplitBlock(BasicBlock *Old, Instruction *SplitPt) {
381 while (isa<PHINode>(SplitPt))
382 ++SplitPt;
383 BasicBlock *New = Old->splitBasicBlock(SplitPt, Old->getName()+".split");
384
385 // The new block lives in whichever loop the old one did.
386 if (Loop *L = LI->getLoopFor(Old))
387 L->addBasicBlockToLoop(New, *LI);
388
389 return New;
390}
391
392
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000393BasicBlock *LoopUnswitch::SplitEdge(BasicBlock *BB, BasicBlock *Succ) {
394 TerminatorInst *LatchTerm = BB->getTerminator();
395 unsigned SuccNum = 0;
396 for (unsigned i = 0, e = LatchTerm->getNumSuccessors(); ; ++i) {
397 assert(i != e && "Didn't find edge?");
398 if (LatchTerm->getSuccessor(i) == Succ) {
399 SuccNum = i;
400 break;
401 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000402 }
Chris Lattnered7a67b2006-02-10 01:24:09 +0000403
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000404 // If this is a critical edge, let SplitCriticalEdge do it.
405 if (SplitCriticalEdge(BB->getTerminator(), SuccNum, this))
406 return LatchTerm->getSuccessor(SuccNum);
407
408 // If the edge isn't critical, then BB has a single successor or Succ has a
409 // single pred. Split the block.
410 BasicBlock *BlockToSplit;
411 BasicBlock::iterator SplitPoint;
412 if (BasicBlock *SP = Succ->getSinglePredecessor()) {
413 // If the successor only has a single pred, split the top of the successor
414 // block.
415 assert(SP == BB && "CFG broken");
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000416 return SplitBlock(Succ, Succ->begin());
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000417 } else {
418 // Otherwise, if BB has a single successor, split it at the bottom of the
419 // block.
420 assert(BB->getTerminator()->getNumSuccessors() == 1 &&
421 "Should have a single succ!");
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000422 return SplitBlock(BB, BB->getTerminator());
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000423 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000424}
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000425
Chris Lattnerf48f7772004-04-19 18:07:02 +0000426
427
Misha Brukmanb1c93172005-04-21 23:48:37 +0000428// RemapInstruction - Convert the instruction operands from referencing the
Chris Lattnerf48f7772004-04-19 18:07:02 +0000429// current values into those specified by ValueMap.
430//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000431static inline void RemapInstruction(Instruction *I,
Chris Lattnerf48f7772004-04-19 18:07:02 +0000432 std::map<const Value *, Value*> &ValueMap) {
433 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
434 Value *Op = I->getOperand(op);
435 std::map<const Value *, Value*>::iterator It = ValueMap.find(Op);
436 if (It != ValueMap.end()) Op = It->second;
437 I->setOperand(op, Op);
438 }
439}
440
441/// CloneLoop - Recursively clone the specified loop and all of its children,
442/// mapping the blocks with the specified map.
443static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM,
444 LoopInfo *LI) {
445 Loop *New = new Loop();
446
447 if (PL)
448 PL->addChildLoop(New);
449 else
450 LI->addTopLevelLoop(New);
451
452 // Add all of the blocks in L to the new loop.
453 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
454 I != E; ++I)
455 if (LI->getLoopFor(*I) == L)
456 New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
457
458 // Add all of the subloops to the new loop.
459 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
460 CloneLoop(*I, New, VM, LI);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000461
Chris Lattnerf48f7772004-04-19 18:07:02 +0000462 return New;
463}
464
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000465/// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
466/// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the
467/// code immediately before InsertPt.
468static void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
469 BasicBlock *TrueDest,
470 BasicBlock *FalseDest,
471 Instruction *InsertPt) {
472 // Insert a conditional branch on LIC to the two preheaders. The original
473 // code is the true version and the new code is the false version.
474 Value *BranchVal = LIC;
Chris Lattner65152d82006-02-15 19:05:52 +0000475 if (!isa<ConstantBool>(Val)) {
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000476 BranchVal = BinaryOperator::createSetEQ(LIC, Val, "tmp", InsertPt);
477 } else if (Val != ConstantBool::True) {
478 // We want to enter the new loop when the condition is true.
479 std::swap(TrueDest, FalseDest);
480 }
481
482 // Insert the new branch.
483 new BranchInst(TrueDest, FalseDest, BranchVal, InsertPt);
484}
485
486
Chris Lattnered7a67b2006-02-10 01:24:09 +0000487/// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
488/// condition in it (a cond branch from its header block to its latch block,
489/// where the path through the loop that doesn't execute its body has no
490/// side-effects), unswitch it. This doesn't involve any code duplication, just
491/// moving the conditional branch outside of the loop and updating loop info.
492void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond,
Chris Lattner01db04e2006-02-15 01:44:42 +0000493 Constant *Val,
Chris Lattner49354172006-02-10 02:01:22 +0000494 BasicBlock *ExitBlock) {
Chris Lattner3fc31482006-02-10 01:36:35 +0000495 DEBUG(std::cerr << "loop-unswitch: Trivial-Unswitch loop %"
496 << L->getHeader()->getName() << " [" << L->getBlocks().size()
497 << " blocks] in Function " << L->getHeader()->getParent()->getName()
498 << " on cond:" << *Cond << "\n");
499
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000500 // First step, split the preheader, so that we know that there is a safe place
Chris Lattnered7a67b2006-02-10 01:24:09 +0000501 // to insert the conditional branch. We will change 'OrigPH' to have a
502 // conditional branch on Cond.
503 BasicBlock *OrigPH = L->getLoopPreheader();
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000504 BasicBlock *NewPH = SplitEdge(OrigPH, L->getHeader());
Chris Lattnered7a67b2006-02-10 01:24:09 +0000505
506 // Now that we have a place to insert the conditional branch, create a place
Chris Lattner49354172006-02-10 02:01:22 +0000507 // to branch to: this is the exit block out of the loop that we should
508 // short-circuit to.
Chris Lattnered7a67b2006-02-10 01:24:09 +0000509
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000510 // Split this block now, so that the loop maintains its exit block, and so
511 // that the jump from the preheader can execute the contents of the exit block
512 // without actually branching to it (the exit block should be dominated by the
513 // loop header, not the preheader).
Chris Lattner49354172006-02-10 02:01:22 +0000514 assert(!L->contains(ExitBlock) && "Exit block is in the loop?");
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000515 BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin());
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000516
Chris Lattnered7a67b2006-02-10 01:24:09 +0000517 // Okay, now we have a position to branch from and a position to branch to,
518 // insert the new conditional branch.
Chris Lattner01db04e2006-02-15 01:44:42 +0000519 EmitPreheaderBranchOnCondition(Cond, Val, NewPH, NewExit,
520 OrigPH->getTerminator());
Chris Lattnered7a67b2006-02-10 01:24:09 +0000521 OrigPH->getTerminator()->eraseFromParent();
522
523 // Now that we know that the loop is never entered when this condition is a
524 // particular value, rewrite the loop with this info. We know that this will
525 // at least eliminate the old branch.
Chris Lattner01db04e2006-02-15 01:44:42 +0000526 RewriteLoopBodyWithConditionConstant(L, Cond, Val, true);
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000527 ++NumTrivial;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000528}
529
Chris Lattnerf48f7772004-04-19 18:07:02 +0000530
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000531/// VersionLoop - We determined that the loop is profitable to unswitch when LIC
532/// equal Val. Split it into loop versions and test the condition outside of
533/// either loop. Return the loops created as Out1/Out2.
534void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L,
535 Loop *&Out1, Loop *&Out2) {
Chris Lattnerf48f7772004-04-19 18:07:02 +0000536 Function *F = L->getHeader()->getParent();
Chris Lattnered7a67b2006-02-10 01:24:09 +0000537
Chris Lattnerf48f7772004-04-19 18:07:02 +0000538 DEBUG(std::cerr << "loop-unswitch: Unswitching loop %"
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000539 << L->getHeader()->getName() << " [" << L->getBlocks().size()
540 << " blocks] in Function " << F->getName()
541 << " when '" << *Val << "' == " << *LIC << "\n");
Chris Lattnerf48f7772004-04-19 18:07:02 +0000542
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000543 // LoopBlocks contains all of the basic blocks of the loop, including the
544 // preheader of the loop, the body of the loop, and the exit blocks of the
545 // loop, in that order.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000546 std::vector<BasicBlock*> LoopBlocks;
547
548 // First step, split the preheader and exit blocks, and add these blocks to
549 // the LoopBlocks list.
550 BasicBlock *OrigPreheader = L->getLoopPreheader();
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000551 LoopBlocks.push_back(SplitEdge(OrigPreheader, L->getHeader()));
Chris Lattnerf48f7772004-04-19 18:07:02 +0000552
553 // We want the loop to come after the preheader, but before the exit blocks.
554 LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
555
556 std::vector<BasicBlock*> ExitBlocks;
557 L->getExitBlocks(ExitBlocks);
558 std::sort(ExitBlocks.begin(), ExitBlocks.end());
559 ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
560 ExitBlocks.end());
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000561
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000562 // Split all of the edges from inside the loop to their exit blocks. This
563 // unswitching trivial: no phi nodes to update.
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000564 unsigned NumBlocks = L->getBlocks().size();
Chris Lattnered7a67b2006-02-10 01:24:09 +0000565 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000566 BasicBlock *ExitBlock = ExitBlocks[i];
567 std::vector<BasicBlock*> Preds(pred_begin(ExitBlock), pred_end(ExitBlock));
568
569 for (unsigned j = 0, e = Preds.size(); j != e; ++j) {
570 assert(L->contains(Preds[j]) &&
571 "All preds of loop exit blocks must be the same loop!");
572 SplitEdge(Preds[j], ExitBlock);
573 }
Chris Lattnered7a67b2006-02-10 01:24:09 +0000574 }
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000575
576 // The exit blocks may have been changed due to edge splitting, recompute.
577 ExitBlocks.clear();
578 L->getExitBlocks(ExitBlocks);
579 std::sort(ExitBlocks.begin(), ExitBlocks.end());
580 ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()),
581 ExitBlocks.end());
582
583 // Add exit blocks to the loop blocks.
584 LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
Chris Lattnerf48f7772004-04-19 18:07:02 +0000585
586 // Next step, clone all of the basic blocks that make up the loop (including
587 // the loop preheader and exit blocks), keeping track of the mapping between
588 // the instructions and blocks.
589 std::vector<BasicBlock*> NewBlocks;
590 NewBlocks.reserve(LoopBlocks.size());
591 std::map<const Value*, Value*> ValueMap;
592 for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000593 BasicBlock *New = CloneBasicBlock(LoopBlocks[i], ValueMap, ".us", F);
594 NewBlocks.push_back(New);
595 ValueMap[LoopBlocks[i]] = New; // Keep the BB mapping.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000596 }
597
598 // Splice the newly inserted blocks into the function right before the
599 // original preheader.
600 F->getBasicBlockList().splice(LoopBlocks[0], F->getBasicBlockList(),
601 NewBlocks[0], F->end());
602
603 // Now we create the new Loop object for the versioned loop.
604 Loop *NewLoop = CloneLoop(L, L->getParentLoop(), ValueMap, LI);
Chris Lattnerf1b15162006-02-10 23:26:14 +0000605 Loop *ParentLoop = L->getParentLoop();
606 if (ParentLoop) {
Chris Lattnerf48f7772004-04-19 18:07:02 +0000607 // Make sure to add the cloned preheader and exit blocks to the parent loop
608 // as well.
Chris Lattnerf1b15162006-02-10 23:26:14 +0000609 ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI);
610 }
611
612 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
613 BasicBlock *NewExit = cast<BasicBlock>(ValueMap[ExitBlocks[i]]);
614 if (ParentLoop)
615 ParentLoop->addBasicBlockToLoop(cast<BasicBlock>(NewExit), *LI);
616
617 assert(NewExit->getTerminator()->getNumSuccessors() == 1 &&
618 "Exit block should have been split to have one successor!");
619 BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0);
620
621 // If the successor of the exit block had PHI nodes, add an entry for
622 // NewExit.
623 PHINode *PN;
624 for (BasicBlock::iterator I = ExitSucc->begin();
625 (PN = dyn_cast<PHINode>(I)); ++I) {
626 Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
627 std::map<const Value *, Value*>::iterator It = ValueMap.find(V);
628 if (It != ValueMap.end()) V = It->second;
629 PN->addIncoming(V, NewExit);
630 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000631 }
632
633 // Rewrite the code to refer to itself.
634 for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
635 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
636 E = NewBlocks[i]->end(); I != E; ++I)
637 RemapInstruction(I, ValueMap);
Chris Lattner2826e052006-02-09 19:14:52 +0000638
Chris Lattnerf48f7772004-04-19 18:07:02 +0000639 // Rewrite the original preheader to select between versions of the loop.
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000640 BranchInst *OldBR = cast<BranchInst>(OrigPreheader->getTerminator());
641 assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
Chris Lattnerf48f7772004-04-19 18:07:02 +0000642 "Preheader splitting did not work correctly!");
Chris Lattnerf48f7772004-04-19 18:07:02 +0000643
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000644 // Emit the new branch that selects between the two versions of this loop.
645 EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
646 OldBR->eraseFromParent();
Chris Lattnerf48f7772004-04-19 18:07:02 +0000647
648 // Now we rewrite the original code to know that the condition is true and the
649 // new code to know that the condition is false.
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000650 RewriteLoopBodyWithConditionConstant(L, LIC, Val, false);
651 RewriteLoopBodyWithConditionConstant(NewLoop, LIC, Val, true);
Chris Lattner4f0e66d2006-02-09 22:15:42 +0000652 Out1 = L;
653 Out2 = NewLoop;
Chris Lattnerf48f7772004-04-19 18:07:02 +0000654}
655
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000656// RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
657// the value specified by Val in the specified loop, or we know it does NOT have
658// that value. Rewrite any uses of LIC or of properties correlated to it.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000659void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000660 Constant *Val,
661 bool IsEqual) {
Chris Lattnered7a67b2006-02-10 01:24:09 +0000662 assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000663
Chris Lattnerf48f7772004-04-19 18:07:02 +0000664 // FIXME: Support correlated properties, like:
665 // for (...)
666 // if (li1 < li2)
667 // ...
668 // if (li1 > li2)
669 // ...
Chris Lattnerf48f7772004-04-19 18:07:02 +0000670
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000671 // NotVal - If Val is a bool, this contains its inverse.
672 Constant *NotVal = 0;
673 if (ConstantBool *CB = dyn_cast<ConstantBool>(Val))
674 NotVal = ConstantBool::get(!CB->getValue());
675
Chris Lattner6e263152006-02-10 02:30:37 +0000676 // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches,
677 // selects, switches.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000678 std::vector<User*> Users(LIC->use_begin(), LIC->use_end());
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000679
680 // Haha, this loop could be unswitched. Get it? The unswitch pass could
681 // unswitch itself. Amazing.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000682 for (unsigned i = 0, e = Users.size(); i != e; ++i)
Chris Lattnered7a67b2006-02-10 01:24:09 +0000683 if (Instruction *U = cast<Instruction>(Users[i]))
Chris Lattnerf48f7772004-04-19 18:07:02 +0000684 if (L->contains(U->getParent()))
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000685 if (IsEqual) {
686 U->replaceUsesOfWith(LIC, Val);
687 } else if (NotVal) {
688 U->replaceUsesOfWith(LIC, NotVal);
689 } else {
690 // If we know that LIC is not Val, use this info to simplify code.
691 if (SwitchInst *SI = dyn_cast<SwitchInst>(U)) {
692 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) {
693 if (SI->getCaseValue(i) == Val) {
694 // Found a dead case value. Don't remove PHI nodes in the
695 // successor if they become single-entry, those PHI nodes may
696 // be in the Users list.
697 SI->getSuccessor(i)->removePredecessor(SI->getParent(), true);
698 SI->removeCase(i);
699 break;
700 }
701 }
702 }
703
704 // TODO: We could simplify stuff like X == C.
705 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000706}