JumpThreading.cpp revision bd3401fa98b578080e227afce940bca80137dea6
18383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//===- JumpThreading.cpp - Thread control through conditional blocks ------===//
28383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//
38383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//                     The LLVM Compiler Infrastructure
48383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//
58383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// This file is distributed under the University of Illinois Open Source
68383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// License. See LICENSE.TXT for details.
78383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//
88383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//===----------------------------------------------------------------------===//
98383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//
10177480b7ede0441135572d641a2497df25a7d95fChris Lattner// This file implements the Jump Threading pass.
118383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//
128383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner//===----------------------------------------------------------------------===//
138383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
148383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#define DEBUG_TYPE "jump-threading"
158383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Transforms/Scalar.h"
16177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/IntrinsicInst.h"
178383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Pass.h"
18bd3401fa98b578080e227afce940bca80137dea6Chris Lattner#include "llvm/ADT/DenseMap.h"
198383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/ADT/Statistic.h"
20bd3401fa98b578080e227afce940bca80137dea6Chris Lattner#include "llvm/Transforms/Utils/Local.h"
218383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/CommandLine.h"
228383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/Compiler.h"
23177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/Support/Debug.h"
248383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerusing namespace llvm;
258383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
26bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumThreads, "Number of jumps threaded");
27bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumFolds,   "Number of terminators folded");
288383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
29177480b7ede0441135572d641a2497df25a7d95fChris Lattnerstatic cl::opt<unsigned>
30177480b7ede0441135572d641a2497df25a7d95fChris LattnerThreshold("jump-threading-threshold",
31177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::desc("Max block size to duplicate for jump threading"),
32177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::init(6), cl::Hidden);
33177480b7ede0441135572d641a2497df25a7d95fChris Lattner
348383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnernamespace {
35177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// This pass performs 'jump threading', which looks at blocks that have
36177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// multiple predecessors and multiple successors.  If one or more of the
37177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// predecessors of the block can be proven to always jump to one of the
38177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// successors, we forward the edge from the predecessor to the successor by
39177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// duplicating the contents of this block.
40177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
41177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// An example of when this can occur is code like this:
42177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
43177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   if () { ...
44177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///     X = 4;
45177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   }
46177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   if (X < 3) {
47177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
48177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// In this case, the unconditional branch at the end of the first if can be
49177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// revectored to the false side of the second if.
50177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
518383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  class VISIBILITY_HIDDEN JumpThreading : public FunctionPass {
528383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  public:
538383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    static char ID; // Pass identification
548383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    JumpThreading() : FunctionPass((intptr_t)&ID) {}
558383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
568383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    bool runOnFunction(Function &F);
57bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    bool ThreadBlock(BasicBlock *BB);
58bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    void ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, BasicBlock *SuccBB);
598383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  };
608383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  char JumpThreading::ID = 0;
618383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  RegisterPass<JumpThreading> X("jump-threading", "Jump Threading");
628383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
638383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
648383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// Public interface to the Jump Threading pass
658383a7b7a6acdae478d4b4c2520915153b73f676Chris LattnerFunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }
668383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
678383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// runOnFunction - Top level algorithm.
688383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner///
698383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerbool JumpThreading::runOnFunction(Function &F) {
70177480b7ede0441135572d641a2497df25a7d95fChris Lattner  DOUT << "Jump threading on function '" << F.getNameStart() << "'\n";
71bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
72bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  bool AnotherIteration = true, EverChanged = false;
73bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  while (AnotherIteration) {
74bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = false;
75bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    bool Changed = false;
76bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
77bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      while (ThreadBlock(I))
78bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        Changed = true;
79bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = Changed;
80bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    EverChanged |= Changed;
81bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
82bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  return EverChanged;
838383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
84177480b7ede0441135572d641a2497df25a7d95fChris Lattner
85177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
86177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// thread across it.
87bd3401fa98b578080e227afce940bca80137dea6Chris Lattnerstatic unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
88bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock::const_iterator I = BB->begin();
89177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// Ignore PHI nodes, these will be flattened when duplication happens.
90177480b7ede0441135572d641a2497df25a7d95fChris Lattner  while (isa<PHINode>(*I)) ++I;
91177480b7ede0441135572d641a2497df25a7d95fChris Lattner
92177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // Sum up the cost of each instruction until we get to the terminator.  Don't
93177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // include the terminator because the copy won't include it.
94177480b7ede0441135572d641a2497df25a7d95fChris Lattner  unsigned Size = 0;
95177480b7ede0441135572d641a2497df25a7d95fChris Lattner  for (; !isa<TerminatorInst>(I); ++I) {
96177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // Debugger intrinsics don't incur code size.
97177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (isa<DbgInfoIntrinsic>(I)) continue;
98177480b7ede0441135572d641a2497df25a7d95fChris Lattner
99177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // If this is a pointer->pointer bitcast, it is free.
100177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (isa<BitCastInst>(I) && isa<PointerType>(I->getType()))
101177480b7ede0441135572d641a2497df25a7d95fChris Lattner      continue;
102177480b7ede0441135572d641a2497df25a7d95fChris Lattner
103177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // All other instructions count for at least one unit.
104177480b7ede0441135572d641a2497df25a7d95fChris Lattner    ++Size;
105177480b7ede0441135572d641a2497df25a7d95fChris Lattner
106177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // Calls are more expensive.  If they are non-intrinsic calls, we model them
107177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // as having cost of 4.  If they are a non-vector intrinsic, we model them
108177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // as having cost of 2 total, and if they are a vector intrinsic, we model
109177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // them as having cost 1.
110177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (const CallInst *CI = dyn_cast<CallInst>(I)) {
111177480b7ede0441135572d641a2497df25a7d95fChris Lattner      if (!isa<IntrinsicInst>(CI))
112177480b7ede0441135572d641a2497df25a7d95fChris Lattner        Size += 3;
113177480b7ede0441135572d641a2497df25a7d95fChris Lattner      else if (isa<VectorType>(CI->getType()))
114177480b7ede0441135572d641a2497df25a7d95fChris Lattner        Size += 1;
115177480b7ede0441135572d641a2497df25a7d95fChris Lattner    }
116177480b7ede0441135572d641a2497df25a7d95fChris Lattner  }
117177480b7ede0441135572d641a2497df25a7d95fChris Lattner
118177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // Threading through a switch statement is particularly profitable.  If this
119177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // block ends in a switch, decrease its cost to make it more likely to happen.
120177480b7ede0441135572d641a2497df25a7d95fChris Lattner  if (isa<SwitchInst>(I))
121177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Size = Size > 6 ? Size-6 : 0;
122177480b7ede0441135572d641a2497df25a7d95fChris Lattner
123177480b7ede0441135572d641a2497df25a7d95fChris Lattner  return Size;
124177480b7ede0441135572d641a2497df25a7d95fChris Lattner}
125177480b7ede0441135572d641a2497df25a7d95fChris Lattner
126177480b7ede0441135572d641a2497df25a7d95fChris Lattner
127177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// ThreadBlock - If there are any predecessors whose control can be threaded
128177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now.
129bd3401fa98b578080e227afce940bca80137dea6Chris Lattnerbool JumpThreading::ThreadBlock(BasicBlock *BB) {
130177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if this block ends with a branch of switch.  If so, see if the
131177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // condition is a phi node.  If so, and if an entry of the phi node is a
132177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // constant, we can thread the block.
133177480b7ede0441135572d641a2497df25a7d95fChris Lattner  Value *Condition;
134bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
135bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Can't thread an unconditional jump.
136bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (BI->isUnconditional()) return false;
137177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = BI->getCondition();
138bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
139177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = SI->getCondition();
140177480b7ede0441135572d641a2497df25a7d95fChris Lattner  else
141177480b7ede0441135572d641a2497df25a7d95fChris Lattner    return false; // Must be an invoke.
142bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
143bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If the terminator of this block is branching on a constant, simplify the
144bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // terminator to an unconditional branch.  This can occur do to threading in
145bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // other blocks.
146bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (isa<ConstantInt>(Condition)) {
147bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    DOUT << "  In block '" << BB->getNameStart()
148bd3401fa98b578080e227afce940bca80137dea6Chris Lattner         << "' folding terminator: " << *BB->getTerminator();
149bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ++NumFolds;
150bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ConstantFoldTerminator(BB);
151bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    return true;
152bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
153bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
154bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If there is only a single predecessor of this block, nothing to fold.
155bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BB->getSinglePredecessor())
156bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    return false;
157177480b7ede0441135572d641a2497df25a7d95fChris Lattner
158177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if this is a phi node in the current block.
159177480b7ede0441135572d641a2497df25a7d95fChris Lattner  PHINode *PN = dyn_cast<PHINode>(Condition);
160bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (!PN || PN->getParent() != BB) return false;
161177480b7ede0441135572d641a2497df25a7d95fChris Lattner
162f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // See if the phi node has any constant values.  If so, we can determine where
163f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // the corresponding predecessor will branch.
164f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  unsigned PredNo = ~0U;
165bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ConstantInt *PredCst = 0;
166f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
167bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i)))) {
168f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner      PredNo = i;
169f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner      break;
170f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner    }
171f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  }
172f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner
173f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // If no incoming value has a constant, we don't know the destination of any
174f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // predecessors.
175f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  if (PredNo == ~0U)
176f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner    return false;
177f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner
178177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if the cost of duplicating this block is low enough.
179177480b7ede0441135572d641a2497df25a7d95fChris Lattner  unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
180177480b7ede0441135572d641a2497df25a7d95fChris Lattner  if (JumpThreadCost > Threshold) {
181bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    DOUT << "  Not threading BB '" << BB->getNameStart()
182f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner         << "' - Cost is too high: " << JumpThreadCost << "\n";
183177480b7ede0441135572d641a2497df25a7d95fChris Lattner    return false;
184177480b7ede0441135572d641a2497df25a7d95fChris Lattner  }
185bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
186bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If so, we can actually do this threading.  Figure out which predecessor and
187bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // which successor we are threading for.
188bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock *PredBB = PN->getIncomingBlock(PredNo);
189bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock *SuccBB;
190bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
191bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SuccBB = BI->getSuccessor(PredCst == ConstantInt::getFalse());
192bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  else {
193bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SwitchInst *SI = cast<SwitchInst>(BB->getTerminator());
194bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SuccBB = SI->getSuccessor(SI->findCaseValue(PredCst));
195bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
196bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
197bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // TODO: If there are multiple preds with the same incoming value for the PHI,
198bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // factor them together so we get one thread block for the whole group.  This
199bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // is important for things like "phi i1 [true, true, false, true, x]" where
200bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // we only need to clone the block for the true blocks once.
201bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
202bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  DOUT << "  Threading edge from '" << PredBB->getNameStart() << "' to '"
203bd3401fa98b578080e227afce940bca80137dea6Chris Lattner       << SuccBB->getNameStart() << "' with cost: " << JumpThreadCost
204bd3401fa98b578080e227afce940bca80137dea6Chris Lattner       << ", across block:\n    "
205bd3401fa98b578080e227afce940bca80137dea6Chris Lattner       << *BB;
206bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
207bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ThreadEdge(BB, PredBB, SuccBB);
208bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ++NumThreads;
209bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  return true;
210bd3401fa98b578080e227afce940bca80137dea6Chris Lattner}
211bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
212bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// ThreadEdge - We have decided that it is safe and profitable to thread an
213bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// edge from PredBB to SuccBB across BB.  Transform the IR to reflect this
214bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// change.
215bd3401fa98b578080e227afce940bca80137dea6Chris Lattnervoid JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB,
216bd3401fa98b578080e227afce940bca80137dea6Chris Lattner                               BasicBlock *SuccBB) {
217177480b7ede0441135572d641a2497df25a7d95fChris Lattner
218bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Jump Threading can not update SSA properties correctly if the values
219bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // defined in the duplicated block are used outside of the block itself.  For
220bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // this reason, we spill all values that are used outside of BB to the stack.
221bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
222bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (I->isUsedOutsideOfBlock(BB)) {
223bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      // We found a use of I outside of BB.  Create a new stack slot to
224bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      // break this inter-block usage pattern.
225bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      DemoteRegToStack(*I);
226bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    }
227bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
228bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We are going to have to map operands from the original BB block to the new
229bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // copy of the block 'NewBB'.  If there are PHI nodes in BB, evaluate them to
230bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // account for entry from PredBB.
231bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  DenseMap<Instruction*, Value*> ValueMapping;
232bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
233bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock *NewBB =
234bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    BasicBlock::Create(BB->getName()+".thread", BB->getParent(), BB);
235bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  NewBB->moveAfter(PredBB);
236bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
237bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock::iterator BI = BB->begin();
238bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
239bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
240bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
241bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Clone the non-phi instructions of BB into NewBB, keeping track of the
242bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // mapping and using it to remap operands in the cloned instructions.
243bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; !isa<TerminatorInst>(BI); ++BI) {
244bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    Instruction *New = BI->clone();
245bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    New->setName(BI->getNameStart());
246bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    NewBB->getInstList().push_back(New);
247bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[BI] = New;
248bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
249bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Remap operands to patch up intra-block references.
250bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
251bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i)))
252bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        if (Value *Remapped = ValueMapping[Inst])
253bd3401fa98b578080e227afce940bca80137dea6Chris Lattner          New->setOperand(i, Remapped);
254bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
255bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
256bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We didn't copy the terminator from BB over to NewBB, because there is now
257bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // an unconditional jump to SuccBB.  Insert the unconditional jump.
258bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BranchInst::Create(SuccBB, NewBB);
259bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
260bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
261bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // PHI nodes for NewBB now.
262bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (BasicBlock::iterator PNI = SuccBB->begin(); isa<PHINode>(PNI); ++PNI) {
263bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    PHINode *PN = cast<PHINode>(PNI);
264bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Ok, we have a PHI node.  Figure out what the incoming value was for the
265bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // DestBlock.
266bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    Value *IV = PN->getIncomingValueForBlock(BB);
267bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
268bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Remap the value if necessary.
269bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (Instruction *Inst = dyn_cast<Instruction>(IV))
270bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      if (Value *MappedIV = ValueMapping[Inst])
271bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        IV = MappedIV;
272bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    PN->addIncoming(IV, NewBB);
273bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
274177480b7ede0441135572d641a2497df25a7d95fChris Lattner
275bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Finally, NewBB is good to go.  Update the terminator of PredBB to jump to
276bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // NewBB instead of BB.  This eliminates predecessors from BB, which requires
277bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // us to simplify any PHI nodes in BB.
278bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  TerminatorInst *PredTerm = PredBB->getTerminator();
279bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
280bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (PredTerm->getSuccessor(i) == BB) {
281bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      BB->removePredecessor(PredBB);
282bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      PredTerm->setSuccessor(i, NewBB);
283bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    }
284177480b7ede0441135572d641a2497df25a7d95fChris Lattner}
285