JumpThreading.cpp revision a5ddb59a1319ccd23844c74809a64bc4d88f59d1
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"
202cc675180b06d0a2173365a4015606933f1a285dChris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21bd3401fa98b578080e227afce940bca80137dea6Chris Lattner#include "llvm/Transforms/Utils/Local.h"
228383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/CommandLine.h"
238383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/Compiler.h"
24177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/Support/Debug.h"
258383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerusing namespace llvm;
268383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
27bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumThreads, "Number of jumps threaded");
28bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumFolds,   "Number of terminators folded");
298383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
30177480b7ede0441135572d641a2497df25a7d95fChris Lattnerstatic cl::opt<unsigned>
31177480b7ede0441135572d641a2497df25a7d95fChris LattnerThreshold("jump-threading-threshold",
32177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::desc("Max block size to duplicate for jump threading"),
33177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::init(6), cl::Hidden);
34177480b7ede0441135572d641a2497df25a7d95fChris Lattner
358383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnernamespace {
36177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// This pass performs 'jump threading', which looks at blocks that have
37177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// multiple predecessors and multiple successors.  If one or more of the
38177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// predecessors of the block can be proven to always jump to one of the
39177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// successors, we forward the edge from the predecessor to the successor by
40177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// duplicating the contents of this block.
41177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
42177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// An example of when this can occur is code like this:
43177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
44177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   if () { ...
45177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///     X = 4;
46177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   }
47177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///   if (X < 3) {
48177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
49177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// In this case, the unconditional branch at the end of the first if can be
50177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// revectored to the false side of the second if.
51177480b7ede0441135572d641a2497df25a7d95fChris Lattner  ///
528383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  class VISIBILITY_HIDDEN JumpThreading : public FunctionPass {
538383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  public:
548383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    static char ID; // Pass identification
558383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    JumpThreading() : FunctionPass((intptr_t)&ID) {}
568383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
578383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    bool runOnFunction(Function &F);
58bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    bool ThreadBlock(BasicBlock *BB);
59bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    void ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, BasicBlock *SuccBB);
606bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    BasicBlock *FactorCommonPHIPreds(PHINode *PN, Constant *CstVal);
616bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
62d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner    bool ProcessJumpOnPHI(PHINode *PN);
63ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner    bool ProcessBranchOnLogical(Value *V, BasicBlock *BB, bool isAnd);
64a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    bool ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB);
658383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  };
668383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  char JumpThreading::ID = 0;
678383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  RegisterPass<JumpThreading> X("jump-threading", "Jump Threading");
688383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
698383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
708383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// Public interface to the Jump Threading pass
718383a7b7a6acdae478d4b4c2520915153b73f676Chris LattnerFunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }
728383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
738383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// runOnFunction - Top level algorithm.
748383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner///
758383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerbool JumpThreading::runOnFunction(Function &F) {
76177480b7ede0441135572d641a2497df25a7d95fChris Lattner  DOUT << "Jump threading on function '" << F.getNameStart() << "'\n";
77bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
78bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  bool AnotherIteration = true, EverChanged = false;
79bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  while (AnotherIteration) {
80bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = false;
81bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    bool Changed = false;
82bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
83bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      while (ThreadBlock(I))
84bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        Changed = true;
85bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = Changed;
86bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    EverChanged |= Changed;
87bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
88bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  return EverChanged;
898383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
90177480b7ede0441135572d641a2497df25a7d95fChris Lattner
916bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// FactorCommonPHIPreds - If there are multiple preds with the same incoming
926bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// value for the PHI, factor them together so we get one block to thread for
936bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// the whole group.
946bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// This is important for things like "phi i1 [true, true, false, true, x]"
956bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// where we only need to clone the block for the true blocks once.
966bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner///
976bf77500c655520a04ea8640af6dccbcf995a754Chris LattnerBasicBlock *JumpThreading::FactorCommonPHIPreds(PHINode *PN, Constant *CstVal) {
986bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  SmallVector<BasicBlock*, 16> CommonPreds;
996bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1006bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    if (PN->getIncomingValue(i) == CstVal)
1016bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner      CommonPreds.push_back(PN->getIncomingBlock(i));
1026bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
1036bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  if (CommonPreds.size() == 1)
1046bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    return CommonPreds[0];
1056bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
1066bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  DOUT << "  Factoring out " << CommonPreds.size()
1076bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner       << " common predecessors.\n";
1086bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  return SplitBlockPredecessors(PN->getParent(),
1096bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner                                &CommonPreds[0], CommonPreds.size(),
1106bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner                                ".thr_comm", this);
1116bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner}
1126bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
1136bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
114177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
115177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// thread across it.
116bd3401fa98b578080e227afce940bca80137dea6Chris Lattnerstatic unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
117bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock::const_iterator I = BB->begin();
118177480b7ede0441135572d641a2497df25a7d95fChris Lattner  /// Ignore PHI nodes, these will be flattened when duplication happens.
119177480b7ede0441135572d641a2497df25a7d95fChris Lattner  while (isa<PHINode>(*I)) ++I;
120177480b7ede0441135572d641a2497df25a7d95fChris Lattner
121177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // Sum up the cost of each instruction until we get to the terminator.  Don't
122177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // include the terminator because the copy won't include it.
123177480b7ede0441135572d641a2497df25a7d95fChris Lattner  unsigned Size = 0;
124177480b7ede0441135572d641a2497df25a7d95fChris Lattner  for (; !isa<TerminatorInst>(I); ++I) {
125177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // Debugger intrinsics don't incur code size.
126177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (isa<DbgInfoIntrinsic>(I)) continue;
127177480b7ede0441135572d641a2497df25a7d95fChris Lattner
128177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // If this is a pointer->pointer bitcast, it is free.
129177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (isa<BitCastInst>(I) && isa<PointerType>(I->getType()))
130177480b7ede0441135572d641a2497df25a7d95fChris Lattner      continue;
131177480b7ede0441135572d641a2497df25a7d95fChris Lattner
132177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // All other instructions count for at least one unit.
133177480b7ede0441135572d641a2497df25a7d95fChris Lattner    ++Size;
134177480b7ede0441135572d641a2497df25a7d95fChris Lattner
135177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // Calls are more expensive.  If they are non-intrinsic calls, we model them
136177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // as having cost of 4.  If they are a non-vector intrinsic, we model them
137177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // as having cost of 2 total, and if they are a vector intrinsic, we model
138177480b7ede0441135572d641a2497df25a7d95fChris Lattner    // them as having cost 1.
139177480b7ede0441135572d641a2497df25a7d95fChris Lattner    if (const CallInst *CI = dyn_cast<CallInst>(I)) {
140177480b7ede0441135572d641a2497df25a7d95fChris Lattner      if (!isa<IntrinsicInst>(CI))
141177480b7ede0441135572d641a2497df25a7d95fChris Lattner        Size += 3;
142177480b7ede0441135572d641a2497df25a7d95fChris Lattner      else if (isa<VectorType>(CI->getType()))
143177480b7ede0441135572d641a2497df25a7d95fChris Lattner        Size += 1;
144177480b7ede0441135572d641a2497df25a7d95fChris Lattner    }
145177480b7ede0441135572d641a2497df25a7d95fChris Lattner  }
146177480b7ede0441135572d641a2497df25a7d95fChris Lattner
147177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // Threading through a switch statement is particularly profitable.  If this
148177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // block ends in a switch, decrease its cost to make it more likely to happen.
149177480b7ede0441135572d641a2497df25a7d95fChris Lattner  if (isa<SwitchInst>(I))
150177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Size = Size > 6 ? Size-6 : 0;
151177480b7ede0441135572d641a2497df25a7d95fChris Lattner
152177480b7ede0441135572d641a2497df25a7d95fChris Lattner  return Size;
153177480b7ede0441135572d641a2497df25a7d95fChris Lattner}
154177480b7ede0441135572d641a2497df25a7d95fChris Lattner
155177480b7ede0441135572d641a2497df25a7d95fChris Lattner
156177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// ThreadBlock - If there are any predecessors whose control can be threaded
157177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now.
158bd3401fa98b578080e227afce940bca80137dea6Chris Lattnerbool JumpThreading::ThreadBlock(BasicBlock *BB) {
159177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if this block ends with a branch of switch.  If so, see if the
160177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // condition is a phi node.  If so, and if an entry of the phi node is a
161177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // constant, we can thread the block.
162177480b7ede0441135572d641a2497df25a7d95fChris Lattner  Value *Condition;
163bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
164bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Can't thread an unconditional jump.
165bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (BI->isUnconditional()) return false;
166177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = BI->getCondition();
167bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
168177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = SI->getCondition();
169177480b7ede0441135572d641a2497df25a7d95fChris Lattner  else
170177480b7ede0441135572d641a2497df25a7d95fChris Lattner    return false; // Must be an invoke.
171bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
172bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If the terminator of this block is branching on a constant, simplify the
173037c781de797242ba652db775f1f2c5d2d4eb19bChris Lattner  // terminator to an unconditional branch.  This can occur due to threading in
174bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // other blocks.
175bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (isa<ConstantInt>(Condition)) {
176bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    DOUT << "  In block '" << BB->getNameStart()
177bd3401fa98b578080e227afce940bca80137dea6Chris Lattner         << "' folding terminator: " << *BB->getTerminator();
178bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ++NumFolds;
179bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ConstantFoldTerminator(BB);
180bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    return true;
181bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
182bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
183bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If there is only a single predecessor of this block, nothing to fold.
184bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BB->getSinglePredecessor())
185bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    return false;
186177480b7ede0441135572d641a2497df25a7d95fChris Lattner
187177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if this is a phi node in the current block.
188177480b7ede0441135572d641a2497df25a7d95fChris Lattner  PHINode *PN = dyn_cast<PHINode>(Condition);
189d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner  if (PN && PN->getParent() == BB)
190d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner    return ProcessJumpOnPHI(PN);
191177480b7ede0441135572d641a2497df25a7d95fChris Lattner
1926bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // If this is a conditional branch whose condition is and/or of a phi, try to
1936bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // simplify it.
1946bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  if (BinaryOperator *CondI = dyn_cast<BinaryOperator>(Condition)) {
1956bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    if ((CondI->getOpcode() == Instruction::And ||
1966bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner         CondI->getOpcode() == Instruction::Or) &&
197ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner        isa<BranchInst>(BB->getTerminator()) &&
198ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner        ProcessBranchOnLogical(CondI, BB,
199ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner                               CondI->getOpcode() == Instruction::And))
200ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner      return true;
2016bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  }
2026bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
203a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // If we have "br (phi != 42)" and the phi node has any constant values as
204a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // operands, we can thread through this block.
205a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  if (CmpInst *CondCmp = dyn_cast<CmpInst>(Condition))
206a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if (isa<PHINode>(CondCmp->getOperand(0)) &&
207a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner        isa<Constant>(CondCmp->getOperand(1)) &&
208a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner        ProcessBranchOnCompare(CondCmp, BB))
209a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      return true;
210a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
211d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner  return false;
212d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner}
213d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner
214d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner/// ProcessJumpOnPHI - We have a conditional branch of switch on a PHI node in
215d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner/// the current block.  See if there are any simplifications we can do based on
216d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner/// inputs to the phi node.
217d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner///
218d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattnerbool JumpThreading::ProcessJumpOnPHI(PHINode *PN) {
219f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // See if the phi node has any constant values.  If so, we can determine where
220f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // the corresponding predecessor will branch.
221bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ConstantInt *PredCst = 0;
222a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
223a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i))))
224f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner      break;
225f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner
226f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // If no incoming value has a constant, we don't know the destination of any
227f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner  // predecessors.
228a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  if (PredCst == 0)
229f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner    return false;
230f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner
231177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if the cost of duplicating this block is low enough.
232d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner  BasicBlock *BB = PN->getParent();
233177480b7ede0441135572d641a2497df25a7d95fChris Lattner  unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
234177480b7ede0441135572d641a2497df25a7d95fChris Lattner  if (JumpThreadCost > Threshold) {
235bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    DOUT << "  Not threading BB '" << BB->getNameStart()
236f9065a904fca1018d6ea3e1536b9b3368c084725Chris Lattner         << "' - Cost is too high: " << JumpThreadCost << "\n";
237177480b7ede0441135572d641a2497df25a7d95fChris Lattner    return false;
238177480b7ede0441135572d641a2497df25a7d95fChris Lattner  }
239bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
2406bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // If so, we can actually do this threading.  Merge any common predecessors
2416bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // that will act the same.
2426bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
2436bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
2446bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // Next, figure out which successor we are threading to.
245bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock *SuccBB;
246bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
247bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SuccBB = BI->getSuccessor(PredCst == ConstantInt::getFalse());
248bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  else {
249bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SwitchInst *SI = cast<SwitchInst>(BB->getTerminator());
250bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    SuccBB = SI->getSuccessor(SI->findCaseValue(PredCst));
251bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
252bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
2536bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // And finally, do it!
254bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  DOUT << "  Threading edge from '" << PredBB->getNameStart() << "' to '"
255bd3401fa98b578080e227afce940bca80137dea6Chris Lattner       << SuccBB->getNameStart() << "' with cost: " << JumpThreadCost
256bd3401fa98b578080e227afce940bca80137dea6Chris Lattner       << ", across block:\n    "
2576bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner       << *BB << "\n";
258bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
259bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ThreadEdge(BB, PredBB, SuccBB);
260bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  ++NumThreads;
261bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  return true;
262bd3401fa98b578080e227afce940bca80137dea6Chris Lattner}
263bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
2646bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// ProcessJumpOnLogicalPHI - PN's basic block contains a conditional branch
2656bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// whose condition is an AND/OR where one side is PN.  If PN has constant
2666bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// operands that permit us to evaluate the condition for some operand, thread
2676bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// through the block.  For example with:
2686bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner///   br (and X, phi(Y, Z, false))
2696bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// the predecessor corresponding to the 'false' will always jump to the false
2706bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner/// destination of the branch.
2716bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner///
272ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattnerbool JumpThreading::ProcessBranchOnLogical(Value *V, BasicBlock *BB,
273ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner                                           bool isAnd) {
274ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  // If this is a binary operator tree of the same AND/OR opcode, check the
275ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  // LHS/RHS.
276ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
277ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner    if (isAnd && BO->getOpcode() == Instruction::And ||
278ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner        !isAnd && BO->getOpcode() == Instruction::Or) {
279ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner      if (ProcessBranchOnLogical(BO->getOperand(0), BB, isAnd))
280ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner        return true;
281ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner      if (ProcessBranchOnLogical(BO->getOperand(1), BB, isAnd))
282ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner        return true;
283ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner    }
284ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner
285ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  // If this isn't a PHI node, we can't handle it.
286ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  PHINode *PN = dyn_cast<PHINode>(V);
287ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner  if (!PN || PN->getParent() != BB) return false;
288ae65b3c7912138ec636b3bde5ff528d948161651Chris Lattner
2896bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // We can only do the simplification for phi nodes of 'false' with AND or
2906bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // 'true' with OR.  See if we have any entries in the phi for this.
2916bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  unsigned PredNo = ~0U;
2926bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  ConstantInt *PredCst = ConstantInt::get(Type::Int1Ty, !isAnd);
2936bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2946bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    if (PN->getIncomingValue(i) == PredCst) {
2956bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner      PredNo = i;
2966bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner      break;
2976bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    }
2986bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  }
2996bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3006bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // If no match, bail out.
3016bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  if (PredNo == ~0U)
3026bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    return false;
3036bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3046bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // See if the cost of duplicating this block is low enough.
3056bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
3066bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  if (JumpThreadCost > Threshold) {
3076bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    DOUT << "  Not threading BB '" << BB->getNameStart()
3086bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner         << "' - Cost is too high: " << JumpThreadCost << "\n";
3096bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner    return false;
3106bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  }
3116bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3126bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // If so, we can actually do this threading.  Merge any common predecessors
3136bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // that will act the same.
3146bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
3156bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3166bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // Next, figure out which successor we are threading to.  If this was an AND,
3176bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // the constant must be FALSE, and we must be targeting the 'false' block.
3186bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // If this is an OR, the constant must be TRUE, and we must be targeting the
3196bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // 'true' block.
3206bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(isAnd);
3216bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3226bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  // And finally, do it!
3236bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  DOUT << "  Threading edge through bool from '" << PredBB->getNameStart()
3246bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner       << "' to '" << SuccBB->getNameStart() << "' with cost: "
3256bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner       << JumpThreadCost << ", across block:\n    "
3266bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner       << *BB << "\n";
3276bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
3286bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  ThreadEdge(BB, PredBB, SuccBB);
3296bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  ++NumThreads;
3306bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner  return true;
3316bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner}
3326bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
333a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner/// ProcessBranchOnCompare - We found a branch on a comparison between a phi
334a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner/// node and a constant.  If the PHI node contains any constants as inputs, we
335a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner/// can fold the compare for that edge and thread through it.
336a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattnerbool JumpThreading::ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB) {
337a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  PHINode *PN = cast<PHINode>(Cmp->getOperand(0));
338a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  Constant *RHS = cast<Constant>(Cmp->getOperand(1));
339a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
340a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // If the phi isn't in the current block, an incoming edge to this block
341a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // doesn't control the destination.
342a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  if (PN->getParent() != BB)
343a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    return false;
344a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
345a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // We can do this simplification if any comparisons fold to true or false.
346a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // See if any do.
347a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  Constant *PredCst = 0;
348a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  bool TrueDirection = false;
349a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
350a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    PredCst = dyn_cast<Constant>(PN->getIncomingValue(i));
351a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if (PredCst == 0) continue;
352a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
353a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    Constant *Res;
354a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cmp))
355a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      Res = ConstantExpr::getICmp(ICI->getPredicate(), PredCst, RHS);
356a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    else
357a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      Res = ConstantExpr::getFCmp(cast<FCmpInst>(Cmp)->getPredicate(),
358a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner                                  PredCst, RHS);
359a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    // If this folded to a constant expr, we can't do anything.
360a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if (ConstantInt *ResC = dyn_cast<ConstantInt>(Res)) {
361a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      TrueDirection = ResC->getZExtValue();
362a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      break;
363a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    }
364a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    // If this folded to undef, just go the false way.
365a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    if (isa<UndefValue>(Res)) {
366a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      TrueDirection = false;
367a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner      break;
368a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    }
369a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
370a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    // Otherwise, we can't fold this input.
371a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    PredCst = 0;
372a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  }
373a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
374a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // If no match, bail out.
375a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  if (PredCst == 0)
376a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    return false;
377a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
378a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // See if the cost of duplicating this block is low enough.
379a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
380a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  if (JumpThreadCost > Threshold) {
381a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    DOUT << "  Not threading BB '" << BB->getNameStart()
382a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner         << "' - Cost is too high: " << JumpThreadCost << "\n";
383a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner    return false;
384a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  }
385a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
386a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // If so, we can actually do this threading.  Merge any common predecessors
387a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // that will act the same.
388a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
389a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
390a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // Next, get our successor.
391a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(!TrueDirection);
392a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
393a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // And finally, do it!
394a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  DOUT << "  Threading edge through bool from '" << PredBB->getNameStart()
395a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner       << "' to '" << SuccBB->getNameStart() << "' with cost: "
396a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner       << JumpThreadCost << ", across block:\n    "
397a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner       << *BB << "\n";
398a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
399a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  ThreadEdge(BB, PredBB, SuccBB);
400a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  ++NumThreads;
401a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  return true;
402a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner}
403a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
4046bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
405bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// ThreadEdge - We have decided that it is safe and profitable to thread an
406bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// edge from PredBB to SuccBB across BB.  Transform the IR to reflect this
407bd3401fa98b578080e227afce940bca80137dea6Chris Lattner/// change.
408bd3401fa98b578080e227afce940bca80137dea6Chris Lattnervoid JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB,
409bd3401fa98b578080e227afce940bca80137dea6Chris Lattner                               BasicBlock *SuccBB) {
410177480b7ede0441135572d641a2497df25a7d95fChris Lattner
411bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Jump Threading can not update SSA properties correctly if the values
412bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // defined in the duplicated block are used outside of the block itself.  For
413bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // this reason, we spill all values that are used outside of BB to the stack.
414bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
415bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (I->isUsedOutsideOfBlock(BB)) {
416bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      // We found a use of I outside of BB.  Create a new stack slot to
417bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      // break this inter-block usage pattern.
418bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      DemoteRegToStack(*I);
419bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    }
420bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
421bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We are going to have to map operands from the original BB block to the new
422bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // copy of the block 'NewBB'.  If there are PHI nodes in BB, evaluate them to
423bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // account for entry from PredBB.
424bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  DenseMap<Instruction*, Value*> ValueMapping;
425bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
426bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock *NewBB =
427bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    BasicBlock::Create(BB->getName()+".thread", BB->getParent(), BB);
428bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  NewBB->moveAfter(PredBB);
429bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
430bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock::iterator BI = BB->begin();
431bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
432bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
433bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
434bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Clone the non-phi instructions of BB into NewBB, keeping track of the
435bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // mapping and using it to remap operands in the cloned instructions.
436bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; !isa<TerminatorInst>(BI); ++BI) {
437bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    Instruction *New = BI->clone();
438bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    New->setName(BI->getNameStart());
439bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    NewBB->getInstList().push_back(New);
440bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[BI] = New;
441bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
442bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Remap operands to patch up intra-block references.
443bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
444bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i)))
445bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        if (Value *Remapped = ValueMapping[Inst])
446bd3401fa98b578080e227afce940bca80137dea6Chris Lattner          New->setOperand(i, Remapped);
447bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
448bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
449bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We didn't copy the terminator from BB over to NewBB, because there is now
450bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // an unconditional jump to SuccBB.  Insert the unconditional jump.
451bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BranchInst::Create(SuccBB, NewBB);
452bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
453bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
454bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // PHI nodes for NewBB now.
455bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (BasicBlock::iterator PNI = SuccBB->begin(); isa<PHINode>(PNI); ++PNI) {
456bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    PHINode *PN = cast<PHINode>(PNI);
457bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Ok, we have a PHI node.  Figure out what the incoming value was for the
458bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // DestBlock.
459bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    Value *IV = PN->getIncomingValueForBlock(BB);
460bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
461bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Remap the value if necessary.
462bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (Instruction *Inst = dyn_cast<Instruction>(IV))
463bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      if (Value *MappedIV = ValueMapping[Inst])
464bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        IV = MappedIV;
465bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    PN->addIncoming(IV, NewBB);
466bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
467177480b7ede0441135572d641a2497df25a7d95fChris Lattner
468bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Finally, NewBB is good to go.  Update the terminator of PredBB to jump to
469bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // NewBB instead of BB.  This eliminates predecessors from BB, which requires
470bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // us to simplify any PHI nodes in BB.
471bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  TerminatorInst *PredTerm = PredBB->getTerminator();
472bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
473bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (PredTerm->getSuccessor(i) == BB) {
474bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      BB->removePredecessor(PredBB);
475bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      PredTerm->setSuccessor(i, NewBB);
476bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    }
477177480b7ede0441135572d641a2497df25a7d95fChris Lattner}
478