1d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner//
10d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner// BreakCriticalEdges pass - Break all of the critical edges in the CFG by
11d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner// inserting a dummy basic block.  This pass may be "required" by passes that
12d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner// cannot deal with critical edges.  For this usage, the structure type is
13d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner// forward declared.  This pass obviously invalidates the CFG, but can update
14301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich// dominator trees.
15d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner//
16d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner//===----------------------------------------------------------------------===//
17d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner
18d216e8ba60494caacf919cbf5fef110d48f0d162Chris Lattner#define DEBUG_TYPE "break-crit-edges"
19d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner#include "llvm/Transforms/Scalar.h"
20d23520cd9403c3c6fe8e7ea974ae0b593772345cChris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich#include "llvm/Analysis/Dominators.h"
220ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner#include "llvm/Analysis/LoopInfo.h"
23ff5dfdff56dc2355a6c4740623dddd5fab40d885Andreas Neustifter#include "llvm/Analysis/ProfileInfo.h"
24d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner#include "llvm/Function.h"
2547b14a4a6a455c7be169cfd312fcbe796f0ad426Misha Brukman#include "llvm/Instructions.h"
265b3a4553c1da7e417a240379e2f510c77532c5c1Chris Lattner#include "llvm/Type.h"
27eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner#include "llvm/Support/CFG.h"
28c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
2986f7b2100c7b6b426869178327e352d122056f73Chris Lattner#include "llvm/ADT/SmallVector.h"
30551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
31f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
32d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
33d216e8ba60494caacf919cbf5fef110d48f0d162Chris LattnerSTATISTIC(NumBroken, "Number of blocks inserted");
346de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner
35d216e8ba60494caacf919cbf5fef110d48f0d162Chris Lattnernamespace {
366726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky  struct BreakCriticalEdges : public FunctionPass {
37ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
38081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    BreakCriticalEdges() : FunctionPass(ID) {
39081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
40081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
41794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
426de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner    virtual bool runOnFunction(Function &F);
43fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
446de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45a3ca0b648cd03cb3c003e09f2079f570ce2ac600Owen Anderson      AU.addPreserved<DominatorTree>();
460ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner      AU.addPreserved<LoopInfo>();
47ff5dfdff56dc2355a6c4740623dddd5fab40d885Andreas Neustifter      AU.addPreserved<ProfileInfo>();
4898bf436e2e2ab463d79c54a42a46b12028905330Chris Lattner
4998bf436e2e2ab463d79c54a42a46b12028905330Chris Lattner      // No loop canonicalization guarantees are broken by this pass.
5098bf436e2e2ab463d79c54a42a46b12028905330Chris Lattner      AU.addPreservedID(LoopSimplifyID);
516de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner    }
526de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner  };
536de302bbdb0ac8a595ca35a06e5bbc1605e1da3eChris Lattner}
54d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner
55844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar BreakCriticalEdges::ID = 0;
5602dd53e1c5b941ca5f60fca1b95ebcaf9ccd1dfcOwen AndersonINITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
57ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                "Break critical edges in CFG", false, false)
58844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
597a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// Publicly exposed interface to pass...
6090c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Andersonchar &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
611e5fdf8ba0453baafe062525dc8472846da3ec1fChris LattnerFunctionPass *llvm::createBreakCriticalEdgesPass() {
621e5fdf8ba0453baafe062525dc8472846da3ec1fChris Lattner  return new BreakCriticalEdges();
631e5fdf8ba0453baafe062525dc8472846da3ec1fChris Lattner}
64d76efa018660e806cd87c0a24512e3c532fc1d36Chris Lattner
65363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner// runOnFunction - Loop over all of the edges in the CFG, breaking critical
66363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner// edges as they are found.
67363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner//
68363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattnerbool BreakCriticalEdges::runOnFunction(Function &F) {
69363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner  bool Changed = false;
70363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
71363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner    TerminatorInst *TI = I->getTerminator();
721b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner    if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
73363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner      for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
74363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner        if (SplitCriticalEdge(TI, i, this)) {
75363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner          ++NumBroken;
76363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner          Changed = true;
77363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner        }
78363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner  }
79363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner
80363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner  return Changed;
81363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner}
82363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner
83363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner//===----------------------------------------------------------------------===//
84363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner//    Implementation of the external critical edge manipulation functions
85363ca610d1186c34f25ecad00e8dea61cc91b36aChris Lattner//===----------------------------------------------------------------------===//
86eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
87eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner// isCriticalEdge - Return true if the specified edge is a critical edge.
88eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner// Critical edges are edges from a block with multiple successors to a block
89eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner// with multiple predecessors.
90eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner//
91b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattnerbool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
92b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner                          bool AllowIdenticalEdges) {
93eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
94e802a023d98b06307831cd122e61da86431e8dacChris Lattner  if (TI->getNumSuccessors() == 1) return false;
95eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
96eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  const BasicBlock *Dest = TI->getSuccessor(SuccNum);
9744424646ac9db5c4d3919462bd0831ec22783085Gabor Greif  const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
98eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
99eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  // If there is more than one predecessor, this is a critical edge...
100eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  assert(I != E && "No preds, but we have an edge to the block?");
101b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner  const BasicBlock *FirstPred = *I;
102eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  ++I;        // Skip one edge due to the incoming arc from TI.
103b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner  if (!AllowIdenticalEdges)
104b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner    return I != E;
1052aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
106b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner  // If AllowIdenticalEdges is true, then we allow this edge to be considered
107b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner  // non-critical iff all preds come from TI's block.
1084bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel  while (I != E) {
109e80ba4f88483ce0da33c0219f08e97618cfb0407Gabor Greif    const BasicBlock *P = *I;
110e80ba4f88483ce0da33c0219f08e97618cfb0407Gabor Greif    if (P != FirstPred)
1114bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel      return true;
1124bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel    // Note: leave this as is until no one ever compiles with either gcc 4.0.1
1134bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel    // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207
114e80ba4f88483ce0da33c0219f08e97618cfb0407Gabor Greif    E = pred_end(P);
1154bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel    ++I;
1164bf393a13e779d7a8eac3647df1781068a6dc732Scott Michel  }
117b57de3328d3826e1f270f3a38256ff67aaec1871Chris Lattner  return false;
118eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner}
119eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
120c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman/// CreatePHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
121c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman/// may require new PHIs in the new exit block. This function inserts the
122c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman/// new PHIs, as needed.  Preds is a list of preds inside the loop, SplitBB
123c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman/// is the new loop exit block, and DestBB is the old loop exit, now the
124c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman/// successor of SplitBB.
125c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohmanstatic void CreatePHIsForSplitLoopExit(SmallVectorImpl<BasicBlock *> &Preds,
126c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman                                       BasicBlock *SplitBB,
127c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman                                       BasicBlock *DestBB) {
128c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman  // SplitBB shouldn't have anything non-trivial in it yet.
129c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman  assert(SplitBB->getFirstNonPHI() == SplitBB->getTerminator() &&
130c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman         "SplitBB has non-PHI nodes!");
131c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman
132c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman  // For each PHI in the destination block...
133c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman  for (BasicBlock::iterator I = DestBB->begin();
134c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman       PHINode *PN = dyn_cast<PHINode>(I); ++I) {
135c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    unsigned Idx = PN->getBasicBlockIndex(SplitBB);
136c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    Value *V = PN->getIncomingValue(Idx);
137c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    // If the input is a PHI which already satisfies LCSSA, don't create
138c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    // a new one.
139c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    if (const PHINode *VP = dyn_cast<PHINode>(V))
140c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman      if (VP->getParent() == SplitBB)
141c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman        continue;
142c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    // Otherwise a new PHI is needed. Create one and populate it.
1433ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad    PHINode *NewPN = PHINode::Create(PN->getType(), Preds.size(), "split",
144c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman                                     SplitBB->getTerminator());
145c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    for (unsigned i = 0, e = Preds.size(); i != e; ++i)
146c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman      NewPN->addIncoming(V, Preds[i]);
147c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    // Update the original PHI.
148c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman    PN->setIncomingValue(Idx, NewPN);
149c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman  }
150c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman}
151c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman
152862f27ebab52aa93784462fbeaa93dacdc6a4c78Chris Lattner/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
153301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich/// split the critical edge.  This will update DominatorTree information if it
154301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich/// is available, thus calling this pass will not invalidate either of them.
155301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich/// This returns the new block if the edge was split, null otherwise.
1561b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner///
1571b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
1582aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick/// specified successor will be merged into the same critical edge block.
1592aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick/// This is most commonly interesting with switch instructions, which may
1601b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// have many edges to any one destination.  This ensures that all edges to that
1612aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick/// dest go to one block instead of each going to a different block, but isn't
1621b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// the standard definition of a "critical edge".
1631b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner///
1641b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// It is invalid to call this function on a critical edge that starts at an
1651b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// IndirectBrInst.  Splitting these edges will almost always create an invalid
166280f3fedbfc8d76014dbb25aecfd57a847fd6183Chris Lattner/// program because the address of the new block won't be the one that is jumped
1671b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner/// to.
1681b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner///
1695c89b5240c90eb8171f999e5f06f815502d0321cDan GohmanBasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
170f143b79b78d1d244809fa59320f2af2edf4e1a86Andrew Trick                                    Pass *P, bool MergeIdenticalEdges,
171f143b79b78d1d244809fa59320f2af2edf4e1a86Andrew Trick                                    bool DontDeleteUselessPhis) {
1725c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return 0;
1732aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
1741b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner  assert(!isa<IndirectBrInst>(TI) &&
1751b98ff3e4fb81f83a9c8d04f6b063cbb2114af65Chris Lattner         "Cannot split critical edge from IndirectBrInst");
1762aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
177eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  BasicBlock *TIBB = TI->getParent();
1786918c079a1393be8ae551d699479fbfa39b99277Chris Lattner  BasicBlock *DestBB = TI->getSuccessor(SuccNum);
179eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
180490606e613566d1c7d49d2544c76c207a2555915Bill Wendling  // Splitting the critical edge to a landing pad block is non-trivial. Don't do
181490606e613566d1c7d49d2544c76c207a2555915Bill Wendling  // it in this generic function.
182f0dc257a8fe1325f2226d1c274c4d10c614b9e8bBill Wendling  if (DestBB->isLandingPad()) return 0;
183490606e613566d1c7d49d2544c76c207a2555915Bill Wendling
184eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  // Create a new basic block, linking it into the CFG.
1851d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
1861d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                      TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
187277cccc58fcbc6da6c83b5ccdb2c0ec0ab46bbc1Chris Lattner  // Create our unconditional branch.
188c9ea771cb89567ba0df4a0dac58d707429e3fd31Devang Patel  BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
189c9ea771cb89567ba0df4a0dac58d707429e3fd31Devang Patel  NewBI->setDebugLoc(TI->getDebugLoc());
190fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
19127e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  // Branch to the new block, breaking the edge.
192eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  TI->setSuccessor(SuccNum, NewBB);
193eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
194eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  // Insert the block into the function... right after the block TI lives in.
195eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  Function &F = *TIBB->getParent();
196261cdfbe5e6e11d56ca1c49a75f26fece3b139c8Chris Lattner  Function::iterator FBBI = TIBB;
197261cdfbe5e6e11d56ca1c49a75f26fece3b139c8Chris Lattner  F.getBasicBlockList().insert(++FBBI, NewBB);
1982aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
199eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  // If there are any PHI nodes in DestBB, we need to update them so that they
200eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  // merge incoming values from NewBB instead of from TIBB.
20195c3e48f9557adb6064d580684bb14cacec2f826Jay Foad  {
20295c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    unsigned BBIdx = 0;
20395c3e48f9557adb6064d580684bb14cacec2f826Jay Foad    for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
20495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // We no longer enter through TIBB, now we come in through NewBB.
20595c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // Revector exactly one entry in the PHI node that used to come from
20695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // TIBB to come from NewBB.
20795c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      PHINode *PN = cast<PHINode>(I);
20895c3e48f9557adb6064d580684bb14cacec2f826Jay Foad
20995c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // Reuse the previous value of BBIdx if it lines up.  In cases where we
21095c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // have multiple phi nodes with *lots* of predecessors, this is a speed
21195c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // win because we don't have to scan the PHI looking for TIBB.  This
21295c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // happens because the BB list of PHI nodes are usually in the same
21395c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      // order.
21495c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      if (PN->getIncomingBlock(BBIdx) != TIBB)
2152aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick        BBIdx = PN->getBasicBlockIndex(TIBB);
21695c3e48f9557adb6064d580684bb14cacec2f826Jay Foad      PN->setIncomingBlock(BBIdx, NewBB);
2176686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner    }
218eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  }
2192aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
22027e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  // If there are any other edges from TIBB to DestBB, update those to go
22127e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  // through the split block, making those edges non-critical as well (and
22227e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  // reducing the number of phi entries in the DestBB if relevant).
22327e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  if (MergeIdenticalEdges) {
22427e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner    for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
22527e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner      if (TI->getSuccessor(i) != DestBB) continue;
2262aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
22727e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner      // Remove an entry for TIBB from DestBB phi nodes.
228f143b79b78d1d244809fa59320f2af2edf4e1a86Andrew Trick      DestBB->removePredecessor(TIBB, DontDeleteUselessPhis);
2292aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
23027e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner      // We found another edge to DestBB, go to NewBB instead.
23127e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner      TI->setSuccessor(i, NewBB);
23227e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner    }
23327e1f90d8551b84db910c93ab21c941031c18b60Chris Lattner  }
2342aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
2352aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
236eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
237e802a023d98b06307831cd122e61da86431e8dacChris Lattner  // If we don't have a pass object, we can't update anything...
2385c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  if (P == 0) return NewBB;
2392aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
2406686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
2416686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
2426686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
2432aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
2446686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  // If we have nothing to update, just return.
245301278719b67dcdd1159d9f91b4db5ef57f025c6Cameron Zwarich  if (DT == 0 && LI == 0 && PI == 0)
2466686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner    return NewBB;
247e802a023d98b06307831cd122e61da86431e8dacChris Lattner
24886f7b2100c7b6b426869178327e352d122056f73Chris Lattner  // Now update analysis information.  Since the only predecessor of NewBB is
24986f7b2100c7b6b426869178327e352d122056f73Chris Lattner  // the TIBB, TIBB clearly dominates NewBB.  TIBB usually doesn't dominate
25086f7b2100c7b6b426869178327e352d122056f73Chris Lattner  // anything, as there are other successors of DestBB.  However, if all other
25186f7b2100c7b6b426869178327e352d122056f73Chris Lattner  // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
25286f7b2100c7b6b426869178327e352d122056f73Chris Lattner  // loop header) then NewBB dominates DestBB.
25386f7b2100c7b6b426869178327e352d122056f73Chris Lattner  SmallVector<BasicBlock*, 8> OtherPreds;
254eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner
2552f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner  // If there is a PHI in the block, loop over predecessors with it, which is
2562f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner  // faster than iterating pred_begin/end.
2572f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner  if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
2582f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2592f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner      if (PN->getIncomingBlock(i) != NewBB)
2602f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner        OtherPreds.push_back(PN->getIncomingBlock(i));
2612f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner  } else {
2622f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner    for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
2637556cf541bd91247127f1552fbd36c979777f70cGabor Greif         I != E; ++I) {
2647556cf541bd91247127f1552fbd36c979777f70cGabor Greif      BasicBlock *P = *I;
2657556cf541bd91247127f1552fbd36c979777f70cGabor Greif      if (P != NewBB)
26670e5e222dbae95cd75ae0610aa6740f886a7907eChris Lattner        OtherPreds.push_back(P);
2677556cf541bd91247127f1552fbd36c979777f70cGabor Greif    }
2682f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner  }
2697556cf541bd91247127f1552fbd36c979777f70cGabor Greif
27086f7b2100c7b6b426869178327e352d122056f73Chris Lattner  bool NewBBDominatesDestBB = true;
2712aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
272c178d9459a0e659bedbc1b3c79459ee168453376Chris Lattner  // Should we update DominatorTree information?
2736686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  if (DT) {
27426042420d642e810f5cdfb2da6156b74aaf80945Devang Patel    DomTreeNode *TINode = DT->getNode(TIBB);
275fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
276c178d9459a0e659bedbc1b3c79459ee168453376Chris Lattner    // The new block is not the immediate dominator for any other nodes, but
277c178d9459a0e659bedbc1b3c79459ee168453376Chris Lattner    // TINode is the immediate dominator for the new node.
278c178d9459a0e659bedbc1b3c79459ee168453376Chris Lattner    //
27986f7b2100c7b6b426869178327e352d122056f73Chris Lattner    if (TINode) {       // Don't break unreachable code!
28083beaee227dad622a7e378897c6f29b511388fa0Devang Patel      DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
28126042420d642e810f5cdfb2da6156b74aaf80945Devang Patel      DomTreeNode *DestBBNode = 0;
2822aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
28386f7b2100c7b6b426869178327e352d122056f73Chris Lattner      // If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
28486f7b2100c7b6b426869178327e352d122056f73Chris Lattner      if (!OtherPreds.empty()) {
28586f7b2100c7b6b426869178327e352d122056f73Chris Lattner        DestBBNode = DT->getNode(DestBB);
28686f7b2100c7b6b426869178327e352d122056f73Chris Lattner        while (!OtherPreds.empty() && NewBBDominatesDestBB) {
28726042420d642e810f5cdfb2da6156b74aaf80945Devang Patel          if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
2889a51157db555395f7a6ad89faec40b3afa121091Devang Patel            NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
28986f7b2100c7b6b426869178327e352d122056f73Chris Lattner          OtherPreds.pop_back();
29086f7b2100c7b6b426869178327e352d122056f73Chris Lattner        }
29186f7b2100c7b6b426869178327e352d122056f73Chris Lattner        OtherPreds.clear();
29286f7b2100c7b6b426869178327e352d122056f73Chris Lattner      }
2932aeb8027d649320d5ad3a30d9bbeb71c783b50d4Andrew Trick
29486f7b2100c7b6b426869178327e352d122056f73Chris Lattner      // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
29586f7b2100c7b6b426869178327e352d122056f73Chris Lattner      // doesn't dominate anything.
29686f7b2100c7b6b426869178327e352d122056f73Chris Lattner      if (NewBBDominatesDestBB) {
29786f7b2100c7b6b426869178327e352d122056f73Chris Lattner        if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
29886f7b2100c7b6b426869178327e352d122056f73Chris Lattner        DT->changeImmediateDominator(DestBBNode, NewBBNode);
29986f7b2100c7b6b426869178327e352d122056f73Chris Lattner      }
30086f7b2100c7b6b426869178327e352d122056f73Chris Lattner    }
301eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner  }
3026918c079a1393be8ae551d699479fbfa39b99277Chris Lattner
3030ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner  // Update LoopInfo if it is around.
3046686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  if (LI) {
3055c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    if (Loop *TIL = LI->getLoopFor(TIBB)) {
3065c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // If one or the other blocks were not in a loop, the new block is not
3075c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // either, and thus LI doesn't need to be updated.
3080ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner      if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
3090ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner        if (TIL == DestLoop) {
3100ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          // Both in the same loop, the NewBB joins loop.
311d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
31292329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman        } else if (TIL->contains(DestLoop)) {
31386f7b2100c7b6b426869178327e352d122056f73Chris Lattner          // Edge from an outer loop to an inner loop.  Add to the outer loop.
314d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          TIL->addBasicBlockToLoop(NewBB, LI->getBase());
31592329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman        } else if (DestLoop->contains(TIL)) {
31686f7b2100c7b6b426869178327e352d122056f73Chris Lattner          // Edge from an inner loop to an outer loop.  Add to the outer loop.
317d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson          DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
3180ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner        } else {
3190ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          // Edge from two loops with no containment relation.  Because these
3200ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          // are natural loops, we know that the destination block must be the
3210ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          // header of its loop (adding a branch into a loop elsewhere would
3220ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          // create an irreducible loop).
3230ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          assert(DestLoop->getHeader() == DestBB &&
3240ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner                 "Should not create irreducible loops!");
3250ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner          if (Loop *P = DestLoop->getParentLoop())
326d735ee85dbab8e4f66f9ec157f19956e0d11ec7aOwen Anderson            P->addBasicBlockToLoop(NewBB, LI->getBase());
3270ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner        }
3280ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner      }
3295c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // If TIBB is in a loop and DestBB is outside of that loop, split the
3305c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // other exit blocks of the loop that also have predecessors outside
3315c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // the loop, to maintain a LoopSimplify guarantee.
3325c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      if (!TIL->contains(DestBB) &&
3335c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          P->mustPreserveAnalysisID(LoopSimplifyID)) {
334c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman        assert(!TIL->contains(NewBB) &&
335c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman               "Split point for loop exit is contained in loop!");
336c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman
337c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman        // Update LCSSA form in the newly created exit block.
338c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman        if (P->mustPreserveAnalysisID(LCSSAID)) {
339c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          SmallVector<BasicBlock *, 1> OrigPred;
340c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          OrigPred.push_back(TIBB);
341c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          CreatePHIsForSplitLoopExit(OrigPred, NewBB, DestBB);
342c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman        }
343c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman
3445c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        // For each unique exit block...
34536106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman        // FIXME: This code is functionally equivalent to the corresponding
34636106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman        // loop in LoopSimplify.
3475c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        SmallVector<BasicBlock *, 4> ExitBlocks;
3485c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        TIL->getExitBlocks(ExitBlocks);
3495c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
3505c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // Collect all the preds that are inside the loop, and note
3515c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // whether there are any preds outside the loop.
3525c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          SmallVector<BasicBlock *, 4> Preds;
353c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          bool HasPredOutsideOfLoop = false;
3545c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          BasicBlock *Exit = ExitBlocks[i];
3555c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit);
3569085fcab8276a8aaba33dc78bec2cdb0845351baGabor Greif               I != E; ++I) {
3579085fcab8276a8aaba33dc78bec2cdb0845351baGabor Greif            BasicBlock *P = *I;
35836106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman            if (TIL->contains(P)) {
35936106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman              if (isa<IndirectBrInst>(P->getTerminator())) {
36036106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman                Preds.clear();
36136106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman                break;
36236106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman              }
3639085fcab8276a8aaba33dc78bec2cdb0845351baGabor Greif              Preds.push_back(P);
36436106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman            } else {
365c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman              HasPredOutsideOfLoop = true;
36636106040862e876a53a56f5765d6d3cd1181dd04Eli Friedman            }
3679085fcab8276a8aaba33dc78bec2cdb0845351baGabor Greif          }
3685c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // If there are any preds not in the loop, we'll need to split
3695c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // the edges. The Preds.empty() check is needed because a block
3705c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // may appear multiple times in the list. We can't use
3715c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // getUniqueExitBlocks above because that depends on LoopSimplify
3725c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman          // form, which we're in the process of restoring!
373c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          if (!Preds.empty() && HasPredOutsideOfLoop) {
374c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman            BasicBlock *NewExitBB =
3752fac1d5d61a83c45dcf44119c41dce15ef10e9dcJakub Staszak              SplitBlockPredecessors(Exit, Preds, "split", P);
376c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman            if (P->mustPreserveAnalysisID(LCSSAID))
377c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman              CreatePHIsForSplitLoopExit(Preds, NewExitBB, Exit);
378c292caf55c8f2794965542124d6571b5b59f0ba8Dan Gohman          }
3795c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman        }
3805c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      }
3815c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // LCSSA form was updated above for the case where LoopSimplify is
3825c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // available, which means that all predecessors of loop exit blocks
3835c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // are within the loop. Without LoopSimplify form, it would be
3845c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      // necessary to insert a new phi.
3855c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman      assert((!P->mustPreserveAnalysisID(LCSSAID) ||
3865c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman              P->mustPreserveAnalysisID(LoopSimplifyID)) &&
3875c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman             "SplitCriticalEdge doesn't know how to update LCCSA form "
3885c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman             "without LoopSimplify!");
3895c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman    }
3900ae380a8ac48cbf3131f96318a15dc5dae8a6c78Chris Lattner  }
3915c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman
392ff5dfdff56dc2355a6c4740623dddd5fab40d885Andreas Neustifter  // Update ProfileInfo if it is around.
3936686c6bf68148190309451aa9a79d2efd38ce698Chris Lattner  if (PI)
3942f36ea8b74f2d81b0d14cbaf11f2158c97355dfdChris Lattner    PI->splitEdge(TIBB, DestBB, NewBB, MergeIdenticalEdges);
395ff5dfdff56dc2355a6c4740623dddd5fab40d885Andreas Neustifter
3965c89b5240c90eb8171f999e5f06f815502d0321cDan Gohman  return NewBB;
397eb0456c8fd60e6c2ef844d8696baa39d5d55f082Chris Lattner}
398