LoopSimplify.cpp revision baec98d00bda1cc904405d92716ea9d2f4c1fe9d
1//===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs several transformations to transform natural loops into a
11// simpler form, which makes subsequent analyses and transformations simpler and
12// more effective.
13//
14// Loop pre-header insertion guarantees that there is a single, non-critical
15// entry edge from outside of the loop to the loop header.  This simplifies a
16// number of analyses and transformations, such as LICM.
17//
18// Loop exit-block insertion guarantees that all exit blocks from the loop
19// (blocks which are outside of the loop that have predecessors inside of the
20// loop) only have predecessors from inside of the loop (and are thus dominated
21// by the loop header).  This simplifies transformations such as store-sinking
22// that are built into LICM.
23//
24// This pass also guarantees that loops will have exactly one backedge.
25//
26// Note that the simplifycfg pass will clean up blocks which are split out but
27// end up being unnecessary, so usage of this pass should not pessimize
28// generated code.
29//
30// This pass obviously modifies the CFG, but updates loop information and
31// dominator information.
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/Transforms/Scalar.h"
36#include "llvm/Constant.h"
37#include "llvm/Instructions.h"
38#include "llvm/Function.h"
39#include "llvm/Type.h"
40#include "llvm/Analysis/AliasAnalysis.h"
41#include "llvm/Analysis/Dominators.h"
42#include "llvm/Analysis/LoopInfo.h"
43#include "llvm/Support/CFG.h"
44#include "llvm/ADT/SetOperations.h"
45#include "llvm/ADT/SetVector.h"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/ADT/DepthFirstIterator.h"
48using namespace llvm;
49
50namespace {
51  Statistic<>
52  NumInserted("loopsimplify", "Number of pre-header or exit blocks inserted");
53  Statistic<>
54  NumNested("loopsimplify", "Number of nested loops split out");
55
56  struct LoopSimplify : public FunctionPass {
57    // AA - If we have an alias analysis object to update, this is it, otherwise
58    // this is null.
59    AliasAnalysis *AA;
60
61    virtual bool runOnFunction(Function &F);
62
63    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64      // We need loop information to identify the loops...
65      AU.addRequired<LoopInfo>();
66      AU.addRequired<DominatorSet>();
67      AU.addRequired<DominatorTree>();
68
69      AU.addPreserved<LoopInfo>();
70      AU.addPreserved<DominatorSet>();
71      AU.addPreserved<ImmediateDominators>();
72      AU.addPreserved<ETForest>();
73      AU.addPreserved<DominatorTree>();
74      AU.addPreserved<DominanceFrontier>();
75      AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
76    }
77  private:
78    bool ProcessLoop(Loop *L);
79    BasicBlock *SplitBlockPredecessors(BasicBlock *BB, const char *Suffix,
80                                       const std::vector<BasicBlock*> &Preds);
81    BasicBlock *RewriteLoopExitBlock(Loop *L, BasicBlock *Exit);
82    void InsertPreheaderForLoop(Loop *L);
83    Loop *SeparateNestedLoop(Loop *L);
84    void InsertUniqueBackedgeBlock(Loop *L);
85
86    void UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB,
87                                         std::vector<BasicBlock*> &PredBlocks);
88  };
89
90  RegisterOpt<LoopSimplify>
91  X("loopsimplify", "Canonicalize natural loops", true);
92}
93
94// Publically exposed interface to pass...
95const PassInfo *llvm::LoopSimplifyID = X.getPassInfo();
96FunctionPass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }
97
98/// runOnFunction - Run down all loops in the CFG (recursively, but we could do
99/// it in any convenient order) inserting preheaders...
100///
101bool LoopSimplify::runOnFunction(Function &F) {
102  bool Changed = false;
103  LoopInfo &LI = getAnalysis<LoopInfo>();
104  AA = getAnalysisToUpdate<AliasAnalysis>();
105
106  for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
107    Changed |= ProcessLoop(*I);
108
109  return Changed;
110}
111
112
113/// ProcessLoop - Walk the loop structure in depth first order, ensuring that
114/// all loops have preheaders.
115///
116bool LoopSimplify::ProcessLoop(Loop *L) {
117  bool Changed = false;
118
119  // Check to see that no blocks (other than the header) in the loop have
120  // predecessors that are not in the loop.  This is not valid for natural
121  // loops, but can occur if the blocks are unreachable.  Since they are
122  // unreachable we can just shamelessly destroy their terminators to make them
123  // not branch into the loop!
124  assert(L->getBlocks()[0] == L->getHeader() &&
125         "Header isn't first block in loop?");
126  for (unsigned i = 1, e = L->getBlocks().size(); i != e; ++i) {
127    BasicBlock *LoopBB = L->getBlocks()[i];
128  Retry:
129    for (pred_iterator PI = pred_begin(LoopBB), E = pred_end(LoopBB);
130         PI != E; ++PI)
131      if (!L->contains(*PI)) {
132        // This predecessor is not in the loop.  Kill its terminator!
133        BasicBlock *DeadBlock = *PI;
134        for (succ_iterator SI = succ_begin(DeadBlock), E = succ_end(DeadBlock);
135             SI != E; ++SI)
136          (*SI)->removePredecessor(DeadBlock);  // Remove PHI node entries
137
138        // Delete the dead terminator.
139        if (AA) AA->deleteValue(&DeadBlock->back());
140        DeadBlock->getInstList().pop_back();
141
142        Value *RetVal = 0;
143        if (LoopBB->getParent()->getReturnType() != Type::VoidTy)
144          RetVal = Constant::getNullValue(LoopBB->getParent()->getReturnType());
145        new ReturnInst(RetVal, DeadBlock);
146        goto Retry;  // We just invalidated the pred_iterator.  Retry.
147      }
148  }
149
150  // Does the loop already have a preheader?  If so, don't modify the loop...
151  if (L->getLoopPreheader() == 0) {
152    InsertPreheaderForLoop(L);
153    NumInserted++;
154    Changed = true;
155  }
156
157  // Next, check to make sure that all exit nodes of the loop only have
158  // predecessors that are inside of the loop.  This check guarantees that the
159  // loop preheader/header will dominate the exit blocks.  If the exit block has
160  // predecessors from outside of the loop, split the edge now.
161  std::vector<BasicBlock*> ExitBlocks;
162  L->getExitBlocks(ExitBlocks);
163
164  SetVector<BasicBlock*> ExitBlockSet(ExitBlocks.begin(), ExitBlocks.end());
165  for (SetVector<BasicBlock*>::iterator I = ExitBlockSet.begin(),
166         E = ExitBlockSet.end(); I != E; ++I) {
167    BasicBlock *ExitBlock = *I;
168    for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
169         PI != PE; ++PI)
170      if (!L->contains(*PI)) {
171        RewriteLoopExitBlock(L, ExitBlock);
172        NumInserted++;
173        Changed = true;
174        break;
175      }
176  }
177
178  // If the header has more than two predecessors at this point (from the
179  // preheader and from multiple backedges), we must adjust the loop.
180  if (L->getNumBackEdges() != 1) {
181    // If this is really a nested loop, rip it out into a child loop.
182    if (Loop *NL = SeparateNestedLoop(L)) {
183      ++NumNested;
184      // This is a big restructuring change, reprocess the whole loop.
185      ProcessLoop(NL);
186      return true;
187    }
188
189    InsertUniqueBackedgeBlock(L);
190    NumInserted++;
191    Changed = true;
192  }
193
194  // Scan over the PHI nodes in the loop header.  Since they now have only two
195  // incoming values (the loop is canonicalized), we may have simplified the PHI
196  // down to 'X = phi [X, Y]', which should be replaced with 'Y'.
197  PHINode *PN;
198  DominatorSet &DS = getAnalysis<DominatorSet>();
199  for (BasicBlock::iterator I = L->getHeader()->begin();
200       (PN = dyn_cast<PHINode>(I++)); )
201    if (Value *V = PN->hasConstantValue()) {
202        PN->replaceAllUsesWith(V);
203        PN->eraseFromParent();
204      }
205
206  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
207    Changed |= ProcessLoop(*I);
208
209  return Changed;
210}
211
212/// SplitBlockPredecessors - Split the specified block into two blocks.  We want
213/// to move the predecessors specified in the Preds list to point to the new
214/// block, leaving the remaining predecessors pointing to BB.  This method
215/// updates the SSA PHINode's, but no other analyses.
216///
217BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB,
218                                                 const char *Suffix,
219                                       const std::vector<BasicBlock*> &Preds) {
220
221  // Create new basic block, insert right before the original block...
222  BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB->getParent(), BB);
223
224  // The preheader first gets an unconditional branch to the loop header...
225  BranchInst *BI = new BranchInst(BB, NewBB);
226
227  // For every PHI node in the block, insert a PHI node into NewBB where the
228  // incoming values from the out of loop edges are moved to NewBB.  We have two
229  // possible cases here.  If the loop is dead, we just insert dummy entries
230  // into the PHI nodes for the new edge.  If the loop is not dead, we move the
231  // incoming edges in BB into new PHI nodes in NewBB.
232  //
233  if (!Preds.empty()) {  // Is the loop not obviously dead?
234    // Check to see if the values being merged into the new block need PHI
235    // nodes.  If so, insert them.
236    for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) {
237      PHINode *PN = cast<PHINode>(I);
238      ++I;
239
240      // Check to see if all of the values coming in are the same.  If so, we
241      // don't need to create a new PHI node.
242      Value *InVal = PN->getIncomingValueForBlock(Preds[0]);
243      for (unsigned i = 1, e = Preds.size(); i != e; ++i)
244        if (InVal != PN->getIncomingValueForBlock(Preds[i])) {
245          InVal = 0;
246          break;
247        }
248
249      // If the values coming into the block are not the same, we need a PHI.
250      if (InVal == 0) {
251        // Create the new PHI node, insert it into NewBB at the end of the block
252        PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI);
253        if (AA) AA->copyValue(PN, NewPHI);
254
255        // Move all of the edges from blocks outside the loop to the new PHI
256        for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
257          Value *V = PN->removeIncomingValue(Preds[i], false);
258          NewPHI->addIncoming(V, Preds[i]);
259        }
260        InVal = NewPHI;
261      } else {
262        // Remove all of the edges coming into the PHI nodes from outside of the
263        // block.
264        for (unsigned i = 0, e = Preds.size(); i != e; ++i)
265          PN->removeIncomingValue(Preds[i], false);
266      }
267
268      // Add an incoming value to the PHI node in the loop for the preheader
269      // edge.
270      PN->addIncoming(InVal, NewBB);
271
272      // Can we eliminate this phi node now?
273      if (Value *V = PN->hasConstantValue(true)) {
274        if (!isa<Instruction>(V) ||
275            getAnalysis<DominatorSet>().dominates(cast<Instruction>(V), PN)) {
276          PN->replaceAllUsesWith(V);
277          if (AA) AA->deleteValue(PN);
278          BB->getInstList().erase(PN);
279        }
280      }
281    }
282
283    // Now that the PHI nodes are updated, actually move the edges from
284    // Preds to point to NewBB instead of BB.
285    //
286    for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
287      TerminatorInst *TI = Preds[i]->getTerminator();
288      for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s)
289        if (TI->getSuccessor(s) == BB)
290          TI->setSuccessor(s, NewBB);
291    }
292
293  } else {                       // Otherwise the loop is dead...
294    for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) {
295      PHINode *PN = cast<PHINode>(I);
296      // Insert dummy values as the incoming value...
297      PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB);
298    }
299  }
300  return NewBB;
301}
302
303/// InsertPreheaderForLoop - Once we discover that a loop doesn't have a
304/// preheader, this method is called to insert one.  This method has two phases:
305/// preheader insertion and analysis updating.
306///
307void LoopSimplify::InsertPreheaderForLoop(Loop *L) {
308  BasicBlock *Header = L->getHeader();
309
310  // Compute the set of predecessors of the loop that are not in the loop.
311  std::vector<BasicBlock*> OutsideBlocks;
312  for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
313       PI != PE; ++PI)
314      if (!L->contains(*PI))           // Coming in from outside the loop?
315        OutsideBlocks.push_back(*PI);  // Keep track of it...
316
317  // Split out the loop pre-header
318  BasicBlock *NewBB =
319    SplitBlockPredecessors(Header, ".preheader", OutsideBlocks);
320
321  //===--------------------------------------------------------------------===//
322  //  Update analysis results now that we have performed the transformation
323  //
324
325  // We know that we have loop information to update... update it now.
326  if (Loop *Parent = L->getParentLoop())
327    Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>());
328
329  DominatorSet &DS = getAnalysis<DominatorSet>();  // Update dominator info
330  DominatorTree &DT = getAnalysis<DominatorTree>();
331
332
333  // Update the dominator tree information.
334  // The immediate dominator of the preheader is the immediate dominator of
335  // the old header.
336  DominatorTree::Node *PHDomTreeNode =
337    DT.createNewNode(NewBB, DT.getNode(Header)->getIDom());
338  BasicBlock *oldHeaderIDom = DT.getNode(Header)->getIDom()->getBlock();
339
340  // Change the header node so that PNHode is the new immediate dominator
341  DT.changeImmediateDominator(DT.getNode(Header), PHDomTreeNode);
342
343  {
344    // The blocks that dominate NewBB are the blocks that dominate Header,
345    // minus Header, plus NewBB.
346    DominatorSet::DomSetType DomSet = DS.getDominators(Header);
347    DomSet.erase(Header);  // Header does not dominate us...
348    DS.addBasicBlock(NewBB, DomSet);
349
350    // The newly created basic block dominates all nodes dominated by Header.
351    for (df_iterator<DominatorTree::Node*> DFI = df_begin(PHDomTreeNode),
352           E = df_end(PHDomTreeNode); DFI != E; ++DFI)
353      DS.addDominator((*DFI)->getBlock(), NewBB);
354  }
355
356  // Update immediate dominator information if we have it...
357  if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) {
358    // Whatever i-dominated the header node now immediately dominates NewBB
359    ID->addNewBlock(NewBB, ID->get(Header));
360
361    // The preheader now is the immediate dominator for the header node...
362    ID->setImmediateDominator(Header, NewBB);
363  }
364
365  // Update ET Forest information if we have it...
366  if (ETForest *EF = getAnalysisToUpdate<ETForest>()) {
367    // Whatever i-dominated the header node now immediately dominates NewBB
368    EF->addNewBlock(NewBB, oldHeaderIDom);
369
370    // The preheader now is the immediate dominator for the header node...
371    EF->setImmediateDominator(Header, NewBB);
372  }
373
374  // Update dominance frontier information...
375  if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) {
376    // The DF(NewBB) is just (DF(Header)-Header), because NewBB dominates
377    // everything that Header does, and it strictly dominates Header in
378    // addition.
379    assert(DF->find(Header) != DF->end() && "Header node doesn't have DF set?");
380    DominanceFrontier::DomSetType NewDFSet = DF->find(Header)->second;
381    NewDFSet.erase(Header);
382    DF->addBasicBlock(NewBB, NewDFSet);
383
384    // Now we must loop over all of the dominance frontiers in the function,
385    // replacing occurrences of Header with NewBB in some cases.  If a block
386    // dominates a (now) predecessor of NewBB, but did not strictly dominate
387    // Header, it will have Header in it's DF set, but should now have NewBB in
388    // its set.
389    for (unsigned i = 0, e = OutsideBlocks.size(); i != e; ++i) {
390      // Get all of the dominators of the predecessor...
391      const DominatorSet::DomSetType &PredDoms =
392        DS.getDominators(OutsideBlocks[i]);
393      for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(),
394             PDE = PredDoms.end(); PDI != PDE; ++PDI) {
395        BasicBlock *PredDom = *PDI;
396        // If the loop header is in DF(PredDom), then PredDom didn't dominate
397        // the header but did dominate a predecessor outside of the loop.  Now
398        // we change this entry to include the preheader in the DF instead of
399        // the header.
400        DominanceFrontier::iterator DFI = DF->find(PredDom);
401        assert(DFI != DF->end() && "No dominance frontier for node?");
402        if (DFI->second.count(Header)) {
403          DF->removeFromFrontier(DFI, Header);
404          DF->addToFrontier(DFI, NewBB);
405        }
406      }
407    }
408  }
409}
410
411/// RewriteLoopExitBlock - Ensure that the loop preheader dominates all exit
412/// blocks.  This method is used to split exit blocks that have predecessors
413/// outside of the loop.
414BasicBlock *LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) {
415  DominatorSet &DS = getAnalysis<DominatorSet>();
416
417  std::vector<BasicBlock*> LoopBlocks;
418  for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I)
419    if (L->contains(*I))
420      LoopBlocks.push_back(*I);
421
422  assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?");
423  BasicBlock *NewBB = SplitBlockPredecessors(Exit, ".loopexit", LoopBlocks);
424
425  // Update Loop Information - we know that the new block will be in the parent
426  // loop of L.
427  if (Loop *Parent = L->getParentLoop())
428    Parent->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>());
429
430  // Update dominator information (set, immdom, domtree, and domfrontier)
431  UpdateDomInfoForRevectoredPreds(NewBB, LoopBlocks);
432  return NewBB;
433}
434
435/// AddBlockAndPredsToSet - Add the specified block, and all of its
436/// predecessors, to the specified set, if it's not already in there.  Stop
437/// predecessor traversal when we reach StopBlock.
438static void AddBlockAndPredsToSet(BasicBlock *BB, BasicBlock *StopBlock,
439                                  std::set<BasicBlock*> &Blocks) {
440  if (!Blocks.insert(BB).second) return;  // already processed.
441  if (BB == StopBlock) return;  // Stop here!
442
443  for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I)
444    AddBlockAndPredsToSet(*I, StopBlock, Blocks);
445}
446
447/// FindPHIToPartitionLoops - The first part of loop-nestification is to find a
448/// PHI node that tells us how to partition the loops.
449static PHINode *FindPHIToPartitionLoops(Loop *L, DominatorSet &DS,
450                                        AliasAnalysis *AA) {
451  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {
452    PHINode *PN = cast<PHINode>(I);
453    ++I;
454    if (Value *V = PN->hasConstantValue())
455      if (!isa<Instruction>(V) || DS.dominates(cast<Instruction>(V), PN)) {
456        // This is a degenerate PHI already, don't modify it!
457        PN->replaceAllUsesWith(V);
458        if (AA) AA->deleteValue(PN);
459        PN->eraseFromParent();
460        continue;
461      }
462
463    // Scan this PHI node looking for a use of the PHI node by itself.
464    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
465      if (PN->getIncomingValue(i) == PN &&
466          L->contains(PN->getIncomingBlock(i)))
467        // We found something tasty to remove.
468        return PN;
469  }
470  return 0;
471}
472
473/// SeparateNestedLoop - If this loop has multiple backedges, try to pull one of
474/// them out into a nested loop.  This is important for code that looks like
475/// this:
476///
477///  Loop:
478///     ...
479///     br cond, Loop, Next
480///     ...
481///     br cond2, Loop, Out
482///
483/// To identify this common case, we look at the PHI nodes in the header of the
484/// loop.  PHI nodes with unchanging values on one backedge correspond to values
485/// that change in the "outer" loop, but not in the "inner" loop.
486///
487/// If we are able to separate out a loop, return the new outer loop that was
488/// created.
489///
490Loop *LoopSimplify::SeparateNestedLoop(Loop *L) {
491  PHINode *PN = FindPHIToPartitionLoops(L, getAnalysis<DominatorSet>(), AA);
492  if (PN == 0) return 0;  // No known way to partition.
493
494  // Pull out all predecessors that have varying values in the loop.  This
495  // handles the case when a PHI node has multiple instances of itself as
496  // arguments.
497  std::vector<BasicBlock*> OuterLoopPreds;
498  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
499    if (PN->getIncomingValue(i) != PN ||
500        !L->contains(PN->getIncomingBlock(i)))
501      OuterLoopPreds.push_back(PN->getIncomingBlock(i));
502
503  BasicBlock *Header = L->getHeader();
504  BasicBlock *NewBB = SplitBlockPredecessors(Header, ".outer", OuterLoopPreds);
505
506  // Update dominator information (set, immdom, domtree, and domfrontier)
507  UpdateDomInfoForRevectoredPreds(NewBB, OuterLoopPreds);
508
509  // Create the new outer loop.
510  Loop *NewOuter = new Loop();
511
512  LoopInfo &LI = getAnalysis<LoopInfo>();
513
514  // Change the parent loop to use the outer loop as its child now.
515  if (Loop *Parent = L->getParentLoop())
516    Parent->replaceChildLoopWith(L, NewOuter);
517  else
518    LI.changeTopLevelLoop(L, NewOuter);
519
520  // This block is going to be our new header block: add it to this loop and all
521  // parent loops.
522  NewOuter->addBasicBlockToLoop(NewBB, getAnalysis<LoopInfo>());
523
524  // L is now a subloop of our outer loop.
525  NewOuter->addChildLoop(L);
526
527  for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i)
528    NewOuter->addBlockEntry(L->getBlocks()[i]);
529
530  // Determine which blocks should stay in L and which should be moved out to
531  // the Outer loop now.
532  DominatorSet &DS = getAnalysis<DominatorSet>();
533  std::set<BasicBlock*> BlocksInL;
534  for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); PI!=E; ++PI)
535    if (DS.dominates(Header, *PI))
536      AddBlockAndPredsToSet(*PI, Header, BlocksInL);
537
538
539  // Scan all of the loop children of L, moving them to OuterLoop if they are
540  // not part of the inner loop.
541  for (Loop::iterator I = L->begin(); I != L->end(); )
542    if (BlocksInL.count((*I)->getHeader()))
543      ++I;   // Loop remains in L
544    else
545      NewOuter->addChildLoop(L->removeChildLoop(I));
546
547  // Now that we know which blocks are in L and which need to be moved to
548  // OuterLoop, move any blocks that need it.
549  for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
550    BasicBlock *BB = L->getBlocks()[i];
551    if (!BlocksInL.count(BB)) {
552      // Move this block to the parent, updating the exit blocks sets
553      L->removeBlockFromLoop(BB);
554      if (LI[BB] == L)
555        LI.changeLoopFor(BB, NewOuter);
556      --i;
557    }
558  }
559
560  return NewOuter;
561}
562
563
564
565/// InsertUniqueBackedgeBlock - This method is called when the specified loop
566/// has more than one backedge in it.  If this occurs, revector all of these
567/// backedges to target a new basic block and have that block branch to the loop
568/// header.  This ensures that loops have exactly one backedge.
569///
570void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) {
571  assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!");
572
573  // Get information about the loop
574  BasicBlock *Preheader = L->getLoopPreheader();
575  BasicBlock *Header = L->getHeader();
576  Function *F = Header->getParent();
577
578  // Figure out which basic blocks contain back-edges to the loop header.
579  std::vector<BasicBlock*> BackedgeBlocks;
580  for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I)
581    if (*I != Preheader) BackedgeBlocks.push_back(*I);
582
583  // Create and insert the new backedge block...
584  BasicBlock *BEBlock = new BasicBlock(Header->getName()+".backedge", F);
585  BranchInst *BETerminator = new BranchInst(Header, BEBlock);
586
587  // Move the new backedge block to right after the last backedge block.
588  Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos;
589  F->getBasicBlockList().splice(InsertPos, F->getBasicBlockList(), BEBlock);
590
591  // Now that the block has been inserted into the function, create PHI nodes in
592  // the backedge block which correspond to any PHI nodes in the header block.
593  for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
594    PHINode *PN = cast<PHINode>(I);
595    PHINode *NewPN = new PHINode(PN->getType(), PN->getName()+".be",
596                                 BETerminator);
597    NewPN->reserveOperandSpace(BackedgeBlocks.size());
598    if (AA) AA->copyValue(PN, NewPN);
599
600    // Loop over the PHI node, moving all entries except the one for the
601    // preheader over to the new PHI node.
602    unsigned PreheaderIdx = ~0U;
603    bool HasUniqueIncomingValue = true;
604    Value *UniqueValue = 0;
605    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
606      BasicBlock *IBB = PN->getIncomingBlock(i);
607      Value *IV = PN->getIncomingValue(i);
608      if (IBB == Preheader) {
609        PreheaderIdx = i;
610      } else {
611        NewPN->addIncoming(IV, IBB);
612        if (HasUniqueIncomingValue) {
613          if (UniqueValue == 0)
614            UniqueValue = IV;
615          else if (UniqueValue != IV)
616            HasUniqueIncomingValue = false;
617        }
618      }
619    }
620
621    // Delete all of the incoming values from the old PN except the preheader's
622    assert(PreheaderIdx != ~0U && "PHI has no preheader entry??");
623    if (PreheaderIdx != 0) {
624      PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx));
625      PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx));
626    }
627    // Nuke all entries except the zero'th.
628    for (unsigned i = 0, e = PN->getNumIncomingValues()-1; i != e; ++i)
629      PN->removeIncomingValue(e-i, false);
630
631    // Finally, add the newly constructed PHI node as the entry for the BEBlock.
632    PN->addIncoming(NewPN, BEBlock);
633
634    // As an optimization, if all incoming values in the new PhiNode (which is a
635    // subset of the incoming values of the old PHI node) have the same value,
636    // eliminate the PHI Node.
637    if (HasUniqueIncomingValue) {
638      NewPN->replaceAllUsesWith(UniqueValue);
639      if (AA) AA->deleteValue(NewPN);
640      BEBlock->getInstList().erase(NewPN);
641    }
642  }
643
644  // Now that all of the PHI nodes have been inserted and adjusted, modify the
645  // backedge blocks to just to the BEBlock instead of the header.
646  for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
647    TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
648    for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
649      if (TI->getSuccessor(Op) == Header)
650        TI->setSuccessor(Op, BEBlock);
651  }
652
653  //===--- Update all analyses which we must preserve now -----------------===//
654
655  // Update Loop Information - we know that this block is now in the current
656  // loop and all parent loops.
657  L->addBasicBlockToLoop(BEBlock, getAnalysis<LoopInfo>());
658
659  // Update dominator information (set, immdom, domtree, and domfrontier)
660  UpdateDomInfoForRevectoredPreds(BEBlock, BackedgeBlocks);
661}
662
663/// UpdateDomInfoForRevectoredPreds - This method is used to update the four
664/// different kinds of dominator information (dominator sets, immediate
665/// dominators, dominator trees, and dominance frontiers) after a new block has
666/// been added to the CFG.
667///
668/// This only supports the case when an existing block (known as "NewBBSucc"),
669/// had some of its predecessors factored into a new basic block.  This
670/// transformation inserts a new basic block ("NewBB"), with a single
671/// unconditional branch to NewBBSucc, and moves some predecessors of
672/// "NewBBSucc" to now branch to NewBB.  These predecessors are listed in
673/// PredBlocks, even though they are the same as
674/// pred_begin(NewBB)/pred_end(NewBB).
675///
676void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB,
677                                         std::vector<BasicBlock*> &PredBlocks) {
678  assert(!PredBlocks.empty() && "No predblocks??");
679  assert(succ_begin(NewBB) != succ_end(NewBB) &&
680         ++succ_begin(NewBB) == succ_end(NewBB) &&
681         "NewBB should have a single successor!");
682  BasicBlock *NewBBSucc = *succ_begin(NewBB);
683  DominatorSet &DS = getAnalysis<DominatorSet>();
684
685  // Update dominator information...  The blocks that dominate NewBB are the
686  // intersection of the dominators of predecessors, plus the block itself.
687  //
688  DominatorSet::DomSetType NewBBDomSet = DS.getDominators(PredBlocks[0]);
689  for (unsigned i = 1, e = PredBlocks.size(); i != e; ++i)
690    set_intersect(NewBBDomSet, DS.getDominators(PredBlocks[i]));
691  NewBBDomSet.insert(NewBB);  // All blocks dominate themselves...
692  DS.addBasicBlock(NewBB, NewBBDomSet);
693
694  // The newly inserted basic block will dominate existing basic blocks iff the
695  // PredBlocks dominate all of the non-pred blocks.  If all predblocks dominate
696  // the non-pred blocks, then they all must be the same block!
697  //
698  bool NewBBDominatesNewBBSucc = true;
699  {
700    BasicBlock *OnePred = PredBlocks[0];
701    for (unsigned i = 1, e = PredBlocks.size(); i != e; ++i)
702      if (PredBlocks[i] != OnePred) {
703        NewBBDominatesNewBBSucc = false;
704        break;
705      }
706
707    if (NewBBDominatesNewBBSucc)
708      for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
709           PI != E; ++PI)
710        if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) {
711          NewBBDominatesNewBBSucc = false;
712          break;
713        }
714  }
715
716  // The other scenario where the new block can dominate its successors are when
717  // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc
718  // already.
719  if (!NewBBDominatesNewBBSucc) {
720    NewBBDominatesNewBBSucc = true;
721    for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc);
722         PI != E; ++PI)
723      if (*PI != NewBB && !DS.dominates(NewBBSucc, *PI)) {
724        NewBBDominatesNewBBSucc = false;
725        break;
726      }
727  }
728
729  // If NewBB dominates some blocks, then it will dominate all blocks that
730  // NewBBSucc does.
731  if (NewBBDominatesNewBBSucc) {
732    BasicBlock *PredBlock = PredBlocks[0];
733    Function *F = NewBB->getParent();
734    for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
735      if (DS.dominates(NewBBSucc, I))
736        DS.addDominator(I, NewBB);
737  }
738
739  // Update immediate dominator information if we have it...
740  BasicBlock *NewBBIDom = 0;
741  if (ImmediateDominators *ID = getAnalysisToUpdate<ImmediateDominators>()) {
742    // To find the immediate dominator of the new exit node, we trace up the
743    // immediate dominators of a predecessor until we find a basic block that
744    // dominates the exit block.
745    //
746    BasicBlock *Dom = PredBlocks[0];  // Some random predecessor...
747    while (!NewBBDomSet.count(Dom)) {  // Loop until we find a dominator...
748      assert(Dom != 0 && "No shared dominator found???");
749      Dom = ID->get(Dom);
750    }
751
752    // Set the immediate dominator now...
753    ID->addNewBlock(NewBB, Dom);
754    NewBBIDom = Dom;   // Reuse this if calculating DominatorTree info...
755
756    // If NewBB strictly dominates other blocks, we need to update their idom's
757    // now.  The only block that need adjustment is the NewBBSucc block, whose
758    // idom should currently be set to PredBlocks[0].
759    if (NewBBDominatesNewBBSucc)
760      ID->setImmediateDominator(NewBBSucc, NewBB);
761  }
762
763  // Update DominatorTree information if it is active.
764  if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) {
765    // If we don't have ImmediateDominator info around, calculate the idom as
766    // above.
767    DominatorTree::Node *NewBBIDomNode;
768    if (NewBBIDom) {
769      NewBBIDomNode = DT->getNode(NewBBIDom);
770    } else {
771      NewBBIDomNode = DT->getNode(PredBlocks[0]); // Random pred
772      while (!NewBBDomSet.count(NewBBIDomNode->getBlock())) {
773        NewBBIDomNode = NewBBIDomNode->getIDom();
774        assert(NewBBIDomNode && "No shared dominator found??");
775      }
776      NewBBIDom = NewBBIDomNode->getBlock();
777    }
778
779    // Create the new dominator tree node... and set the idom of NewBB.
780    DominatorTree::Node *NewBBNode = DT->createNewNode(NewBB, NewBBIDomNode);
781
782    // If NewBB strictly dominates other blocks, then it is now the immediate
783    // dominator of NewBBSucc.  Update the dominator tree as appropriate.
784    if (NewBBDominatesNewBBSucc) {
785      DominatorTree::Node *NewBBSuccNode = DT->getNode(NewBBSucc);
786      DT->changeImmediateDominator(NewBBSuccNode, NewBBNode);
787    }
788  }
789
790  // Update ET-Forest information if it is active.
791  if (ETForest *EF = getAnalysisToUpdate<ETForest>()) {
792    EF->addNewBlock(NewBB, NewBBIDom);
793    if (NewBBDominatesNewBBSucc)
794      EF->setImmediateDominator(NewBBSucc, NewBB);
795  }
796
797  // Update dominance frontier information...
798  if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) {
799    // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
800    // DF(PredBlocks[0]) without the stuff that the new block does not dominate
801    // a predecessor of.
802    if (NewBBDominatesNewBBSucc) {
803      DominanceFrontier::iterator DFI = DF->find(PredBlocks[0]);
804      if (DFI != DF->end()) {
805        DominanceFrontier::DomSetType Set = DFI->second;
806        // Filter out stuff in Set that we do not dominate a predecessor of.
807        for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
808               E = Set.end(); SetI != E;) {
809          bool DominatesPred = false;
810          for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
811               PI != E; ++PI)
812            if (DS.dominates(NewBB, *PI))
813              DominatesPred = true;
814          if (!DominatesPred)
815            Set.erase(SetI++);
816          else
817            ++SetI;
818        }
819
820        DF->addBasicBlock(NewBB, Set);
821      }
822
823    } else {
824      // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
825      // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
826      // NewBBSucc)).  NewBBSucc is the single successor of NewBB.
827      DominanceFrontier::DomSetType NewDFSet;
828      NewDFSet.insert(NewBBSucc);
829      DF->addBasicBlock(NewBB, NewDFSet);
830    }
831
832    // Now we must loop over all of the dominance frontiers in the function,
833    // replacing occurrences of NewBBSucc with NewBB in some cases.  All
834    // blocks that dominate a block in PredBlocks and contained NewBBSucc in
835    // their dominance frontier must be updated to contain NewBB instead.
836    //
837    for (unsigned i = 0, e = PredBlocks.size(); i != e; ++i) {
838      BasicBlock *Pred = PredBlocks[i];
839      // Get all of the dominators of the predecessor...
840      const DominatorSet::DomSetType &PredDoms = DS.getDominators(Pred);
841      for (DominatorSet::DomSetType::const_iterator PDI = PredDoms.begin(),
842             PDE = PredDoms.end(); PDI != PDE; ++PDI) {
843        BasicBlock *PredDom = *PDI;
844
845        // If the NewBBSucc node is in DF(PredDom), then PredDom didn't
846        // dominate NewBBSucc but did dominate a predecessor of it.  Now we
847        // change this entry to include NewBB in the DF instead of NewBBSucc.
848        DominanceFrontier::iterator DFI = DF->find(PredDom);
849        assert(DFI != DF->end() && "No dominance frontier for node?");
850        if (DFI->second.count(NewBBSucc)) {
851          // If NewBBSucc should not stay in our dominator frontier, remove it.
852          // We remove it unless there is a predecessor of NewBBSucc that we
853          // dominate, but we don't strictly dominate NewBBSucc.
854          bool ShouldRemove = true;
855          if (PredDom == NewBBSucc || !DS.dominates(PredDom, NewBBSucc)) {
856            // Okay, we know that PredDom does not strictly dominate NewBBSucc.
857            // Check to see if it dominates any predecessors of NewBBSucc.
858            for (pred_iterator PI = pred_begin(NewBBSucc),
859                   E = pred_end(NewBBSucc); PI != E; ++PI)
860              if (DS.dominates(PredDom, *PI)) {
861                ShouldRemove = false;
862                break;
863              }
864          }
865
866          if (ShouldRemove)
867            DF->removeFromFrontier(DFI, NewBBSucc);
868          DF->addToFrontier(DFI, NewBB);
869        }
870      }
871    }
872  }
873}
874
875