JumpThreading.cpp revision 12d53dba36905d20b02cf7c4cdae057ae4a5a5e1
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"
171ff50b380e6f5549f5ddd9e6c390dcb00332e3e9Owen Anderson#include "llvm/LLVMContext.h"
188383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Pass.h"
19ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner#include "llvm/Analysis/ConstantFolding.h"
202cc675180b06d0a2173365a4015606933f1a285dChris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21bd3401fa98b578080e227afce940bca80137dea6Chris Lattner#include "llvm/Transforms/Utils/Local.h"
22433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner#include "llvm/Transforms/Utils/SSAUpdater.h"
23ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner#include "llvm/Target/TargetData.h"
24fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/DenseMap.h"
25fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/Statistic.h"
26fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/STLExtras.h"
27fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallPtrSet.h"
28fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallSet.h"
298383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/CommandLine.h"
30177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/Support/Debug.h"
3193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar#include "llvm/Support/raw_ostream.h"
328383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerusing namespace llvm;
338383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
34bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumThreads, "Number of jumps threaded");
35bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumFolds,   "Number of terminators folded");
3678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris LattnerSTATISTIC(NumDupes,   "Number of branch blocks duplicated to eliminate phi");
378383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
38177480b7ede0441135572d641a2497df25a7d95fChris Lattnerstatic cl::opt<unsigned>
39177480b7ede0441135572d641a2497df25a7d95fChris LattnerThreshold("jump-threading-threshold",
40177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::desc("Max block size to duplicate for jump threading"),
41177480b7ede0441135572d641a2497df25a7d95fChris Lattner          cl::init(6), cl::Hidden);
42177480b7ede0441135572d641a2497df25a7d95fChris Lattner
438383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnernamespace {
4494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// This pass performs 'jump threading', which looks at blocks that have
4594019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// multiple predecessors and multiple successors.  If one or more of the
4694019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// predecessors of the block can be proven to always jump to one of the
4794019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// successors, we forward the edge from the predecessor to the successor by
4894019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// duplicating the contents of this block.
4994019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///
5094019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// An example of when this can occur is code like this:
5194019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///
5294019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///   if () { ...
5394019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///     X = 4;
5494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///   }
5594019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///   if (X < 3) {
5694019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///
5794019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// In this case, the unconditional branch at the end of the first if can be
5894019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  /// revectored to the false side of the second if.
5994019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner  ///
603e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  class JumpThreading : public FunctionPass {
61ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner    TargetData *TD;
62fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#ifdef NDEBUG
63fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump    SmallPtrSet<BasicBlock*, 16> LoopHeaders;
64fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#else
65fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump    SmallSet<AssertingVH<BasicBlock>, 16> LoopHeaders;
66fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#endif
678383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  public:
688383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    static char ID; // Pass identification
69ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman    JumpThreading() : FunctionPass(&ID) {}
708383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
718383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner    bool runOnFunction(Function &F);
72fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump    void FindLoopHeaders(Function &F);
73fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
74c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner    bool ProcessBlock(BasicBlock *BB);
75bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner    bool ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, BasicBlock *SuccBB);
7678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,
7778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                          BasicBlock *PredBB);
7812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
7912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    BasicBlock *FactorCommonPHIPreds(PHINode *PN, Value *Val);
80421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    bool ProcessBranchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB);
813cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    bool ProcessSwitchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB);
826bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
83d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner    bool ProcessJumpOnPHI(PHINode *PN);
8412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    bool ProcessBranchOnLogical(Value *V, BasicBlock *BB, bool isAnd);
8512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    bool ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB);
8669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
8769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    bool SimplifyPartiallyRedundantLoad(LoadInst *LI);
888383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner  };
898383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
908383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
91844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar JumpThreading::ID = 0;
92844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterPass<JumpThreading>
93844731a7f1909f55935e3514c9e713a62d67662eDan GohmanX("jump-threading", "Jump Threading");
94844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
958383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// Public interface to the Jump Threading pass
968383a7b7a6acdae478d4b4c2520915153b73f676Chris LattnerFunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }
978383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner
988383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// runOnFunction - Top level algorithm.
998383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner///
1008383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerbool JumpThreading::runOnFunction(Function &F) {
10193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar  DEBUG(errs() << "Jump threading on function '" << F.getName() << "'\n");
10202a436c48ecff9e34d50ce0a2f861e5acdd9bf3fDan Gohman  TD = getAnalysisIfAvailable<TargetData>();
103bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
104fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  FindLoopHeaders(F);
105fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
106bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  bool AnotherIteration = true, EverChanged = false;
107bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  while (AnotherIteration) {
108bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = false;
109bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    bool Changed = false;
110421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
111421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      BasicBlock *BB = I;
112421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      while (ProcessBlock(BB))
113bd3401fa98b578080e227afce940bca80137dea6Chris Lattner        Changed = true;
114421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
115421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      ++I;
116421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
117421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      // If the block is trivially dead, zap it.  This eliminates the successor
118421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      // edges which simplifies the CFG.
119421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      if (pred_begin(BB) == pred_end(BB) &&
12020fa76ef6f7c2d3073e0960cf347af8db64708fcChris Lattner          BB != &BB->getParent()->getEntryBlock()) {
12193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar        DEBUG(errs() << "  JT: Deleting dead block '" << BB->getName()
12278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner              << "' with terminator: " << *BB->getTerminator() << '\n');
123fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump        LoopHeaders.erase(BB);
124421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner        DeleteDeadBlock(BB);
125421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner        Changed = true;
126421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      }
127421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    }
128bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    AnotherIteration = Changed;
129bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    EverChanged |= Changed;
130bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
131fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
132fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  LoopHeaders.clear();
133bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  return EverChanged;
1348383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner}
135177480b7ede0441135572d641a2497df25a7d95fChris Lattner
13678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
13778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// thread across it.
13878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
13978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  /// Ignore PHI nodes, these will be flattened when duplication happens.
14078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  BasicBlock::const_iterator I = BB->getFirstNonPHI();
14178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
14278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Sum up the cost of each instruction until we get to the terminator.  Don't
14378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // include the terminator because the copy won't include it.
14478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  unsigned Size = 0;
14578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (; !isa<TerminatorInst>(I); ++I) {
14678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Debugger intrinsics don't incur code size.
14778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (isa<DbgInfoIntrinsic>(I)) continue;
14878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
14978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // If this is a pointer->pointer bitcast, it is free.
15078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (isa<BitCastInst>(I) && isa<PointerType>(I->getType()))
15178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      continue;
15278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
15378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // All other instructions count for at least one unit.
15478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    ++Size;
15578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
15678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Calls are more expensive.  If they are non-intrinsic calls, we model them
15778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // as having cost of 4.  If they are a non-vector intrinsic, we model them
15878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // as having cost of 2 total, and if they are a vector intrinsic, we model
15978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // them as having cost 1.
16078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (const CallInst *CI = dyn_cast<CallInst>(I)) {
16178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      if (!isa<IntrinsicInst>(CI))
16278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        Size += 3;
16378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      else if (!isa<VectorType>(CI->getType()))
16478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        Size += 1;
16578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    }
16678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
16778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
16878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Threading through a switch statement is particularly profitable.  If this
16978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // block ends in a switch, decrease its cost to make it more likely to happen.
17078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  if (isa<SwitchInst>(I))
17178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    Size = Size > 6 ? Size-6 : 0;
17278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
17378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  return Size;
17478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner}
17578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
17678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
17778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
178fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// FindLoopHeaders - We do not want jump threading to turn proper loop
179fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// structures into irreducible loops.  Doing this breaks up the loop nesting
180fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// hierarchy and pessimizes later transformations.  To prevent this from
181fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// happening, we first have to find the loop headers.  Here we approximate this
182fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// by finding targets of backedges in the CFG.
183fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump///
184fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// Note that there definitely are cases when we want to allow threading of
185fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// edges across a loop header.  For example, threading a jump from outside the
186fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// loop (the preheader) to an exit block of the loop is definitely profitable.
187fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// It is also almost always profitable to thread backedges from within the loop
188fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// to exit blocks, and is often profitable to thread backedges to other blocks
189fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// within the loop (forming a nested loop).  This simple analysis is not rich
190fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// enough to track all of these properties and keep it up-to-date as the CFG
191fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// mutates, so we don't allow any of these transformations.
192fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump///
193fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stumpvoid JumpThreading::FindLoopHeaders(Function &F) {
194fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
195fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  FindFunctionBackedges(F, Edges);
196fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
197fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  for (unsigned i = 0, e = Edges.size(); i != e; ++i)
198fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump    LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second));
199fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump}
200fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
20112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// FactorCommonPHIPreds - If there are multiple preds with the same incoming
20212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// value for the PHI, factor them together so we get one block to thread for
20312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// the whole group.
20412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// This is important for things like "phi i1 [true, true, false, true, x]"
20512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// where we only need to clone the block for the true blocks once.
206785672534d32d196d04ad022c111fde3864e0d28Chris Lattner///
20712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang PatelBasicBlock *JumpThreading::FactorCommonPHIPreds(PHINode *PN, Value *Val) {
20812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  SmallVector<BasicBlock*, 16> CommonPreds;
20912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
21012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (PN->getIncomingValue(i) == Val)
21112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      CommonPreds.push_back(PN->getIncomingBlock(i));
21212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
21312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (CommonPreds.size() == 1)
21412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    return CommonPreds[0];
215785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
21612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  DEBUG(errs() << "  Factoring out " << CommonPreds.size()
21712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        << " common predecessors.\n");
21812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  return SplitBlockPredecessors(PN->getParent(),
21912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                &CommonPreds[0], CommonPreds.size(),
22012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                ".thr_comm", this);
221785672534d32d196d04ad022c111fde3864e0d28Chris Lattner}
22212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
2236bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner
224e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// GetBestDestForBranchOnUndef - If we determine that the specified block ends
225e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// in an undefined jump, decide which block is best to revector to.
226e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner///
227e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// Since we can pick an arbitrary destination, we pick the successor with the
228e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// fewest predecessors.  This should reduce the in-degree of the others.
229e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner///
230e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattnerstatic unsigned GetBestDestForJumpOnUndef(BasicBlock *BB) {
231e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  TerminatorInst *BBTerm = BB->getTerminator();
232e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  unsigned MinSucc = 0;
233e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc);
234e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  // Compute the successor with the minimum number of predecessors.
235e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  unsigned MinNumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB));
236e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) {
237e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    TestBB = BBTerm->getSuccessor(i);
238e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    unsigned NumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB));
239e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    if (NumPreds < MinNumPreds)
240e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner      MinSucc = i;
241e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  }
242e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner
243e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  return MinSucc;
244e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner}
245e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner
246c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner/// ProcessBlock - If there are any predecessors whose control can be threaded
247177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now.
248c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattnerbool JumpThreading::ProcessBlock(BasicBlock *BB) {
24969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If this block has a single predecessor, and if that pred has a single
25069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // successor, merge the blocks.  This encourages recursive jump threading
25169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // because now the condition in this block can be threaded through
25269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // predecessors of our predecessor block.
25312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (BasicBlock *SinglePred = BB->getSinglePredecessor())
254f5102a0f088e7c96f7028bf7ca1c24975c314fffChris Lattner    if (SinglePred->getTerminator()->getNumSuccessors() == 1 &&
255f5102a0f088e7c96f7028bf7ca1c24975c314fffChris Lattner        SinglePred != BB) {
256fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump      // If SinglePred was a loop header, BB becomes one.
257fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump      if (LoopHeaders.erase(SinglePred))
258fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump        LoopHeaders.insert(BB);
259fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
2603d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner      // Remember if SinglePred was the entry block of the function.  If so, we
2613d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner      // will need to move BB back to the entry position.
2623d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner      bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
26369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      MergeBasicBlockIntoOnlyPred(BB);
2643d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner
2653d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner      if (isEntry && BB != &BB->getParent()->getEntryBlock())
2663d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner        BB->moveBefore(&BB->getParent()->getEntryBlock());
26769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      return true;
26869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    }
26912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
27012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // See if this block ends with a branch or switch.  If so, see if the
27112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // condition is a phi node.  If so, and if an entry of the phi node is a
27212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // constant, we can thread the block.
273177480b7ede0441135572d641a2497df25a7d95fChris Lattner  Value *Condition;
274bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
275bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Can't thread an unconditional jump.
276bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (BI->isUnconditional()) return false;
277177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = BI->getCondition();
278bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator()))
279177480b7ede0441135572d641a2497df25a7d95fChris Lattner    Condition = SI->getCondition();
280177480b7ede0441135572d641a2497df25a7d95fChris Lattner  else
281177480b7ede0441135572d641a2497df25a7d95fChris Lattner    return false; // Must be an invoke.
282bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
283bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // If the terminator of this block is branching on a constant, simplify the
284037c781de797242ba652db775f1f2c5d2d4eb19bChris Lattner  // terminator to an unconditional branch.  This can occur due to threading in
285bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // other blocks.
286bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  if (isa<ConstantInt>(Condition)) {
28793b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  In block '" << BB->getName()
28878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' folding terminator: " << *BB->getTerminator() << '\n');
289bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ++NumFolds;
290bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ConstantFoldTerminator(BB);
291bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    return true;
292bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
293bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
294421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // If the terminator is branching on an undef, we can pick any of the
295e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner  // successors to branch to.  Let GetBestDestForJumpOnUndef decide.
296421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (isa<UndefValue>(Condition)) {
297e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
298421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
299421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    // Fold the branch/switch.
300e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    TerminatorInst *BBTerm = BB->getTerminator();
301421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
302e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner      if (i == BestSucc) continue;
303421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      BBTerm->getSuccessor(i)->removePredecessor(BB);
304421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    }
305421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
30693b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  In block '" << BB->getName()
30778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' folding undef terminator: " << *BBTerm << '\n');
308e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner    BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
309421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    BBTerm->eraseFromParent();
310421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    return true;
311421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  }
312421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
313421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  Instruction *CondInst = dyn_cast<Instruction>(Condition);
314421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
315421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // If the condition is an instruction defined in another block, see if a
316421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // predecessor has the same condition:
317421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  //     br COND, BBX, BBY
318421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  //  BBX:
319421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  //     br COND, BBZ, BBW
320421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (!Condition->hasOneUse() && // Multiple uses.
321421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      (CondInst == 0 || CondInst->getParent() != BB)) { // Non-local definition.
322421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    pred_iterator PI = pred_begin(BB), E = pred_end(BB);
323421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    if (isa<BranchInst>(BB->getTerminator())) {
324421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      for (; PI != E; ++PI)
325421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner        if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
326421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner          if (PBI->isConditional() && PBI->getCondition() == Condition &&
327421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner              ProcessBranchOnDuplicateCond(*PI, BB))
328421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner            return true;
3293cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    } else {
3303cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      assert(isa<SwitchInst>(BB->getTerminator()) && "Unknown jump terminator");
3313cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      for (; PI != E; ++PI)
3323cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner        if (SwitchInst *PSI = dyn_cast<SwitchInst>((*PI)->getTerminator()))
3333cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner          if (PSI->getCondition() == Condition &&
3343cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner              ProcessSwitchOnDuplicateCond(*PI, BB))
3353cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner            return true;
336421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    }
337421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  }
338421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
339421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // All the rest of our checks depend on the condition being an instruction.
340421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (CondInst == 0)
341421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    return false;
342421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
343177480b7ede0441135572d641a2497df25a7d95fChris Lattner  // See if this is a phi node in the current block.
344421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (PHINode *PN = dyn_cast<PHINode>(CondInst))
345421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    if (PN->getParent() == BB)
346421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner      return ProcessJumpOnPHI(PN);
347177480b7ede0441135572d641a2497df25a7d95fChris Lattner
34812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If this is a conditional branch whose condition is and/or of a phi, try to
34912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // simplify it.
35012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if ((CondInst->getOpcode() == Instruction::And ||
35112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel       CondInst->getOpcode() == Instruction::Or) &&
35212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      isa<BranchInst>(BB->getTerminator()) &&
35312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      ProcessBranchOnLogical(CondInst, BB,
35412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                             CondInst->getOpcode() == Instruction::And))
35512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    return true;
35612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
3579683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky  if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
35812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (isa<PHINode>(CondCmp->getOperand(0))) {
35912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // If we have "br (phi != 42)" and the phi node has any constant values
36012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // as operands, we can thread through this block.
36112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      //
36212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // If we have "br (cmp phi, x)" and the phi node contains x such that the
36312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // comparison uniquely identifies the branch target, we can thread
36412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // through this block.
36512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
36612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      if (ProcessBranchOnCompare(CondCmp, BB))
36712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        return true;
36812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
36912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
37012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // If we have a comparison, loop over the predecessors to see if there is
37112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // a condition with the same value.
37212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    pred_iterator PI = pred_begin(BB), E = pred_end(BB);
37312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    for (; PI != E; ++PI)
37412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
37512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        if (PBI->isConditional() && *PI != BB) {
37612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel          if (CmpInst *CI = dyn_cast<CmpInst>(PBI->getCondition())) {
37712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel            if (CI->getOperand(0) == CondCmp->getOperand(0) &&
37812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                CI->getOperand(1) == CondCmp->getOperand(1) &&
37912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                CI->getPredicate() == CondCmp->getPredicate()) {
38012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel              // TODO: Could handle things like (x != 4) --> (x == 17)
38112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel              if (ProcessBranchOnDuplicateCond(*PI, BB))
38212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                return true;
38379c740ff479dde322aceafe15887b162c19ea195Chris Lattner            }
38479c740ff479dde322aceafe15887b162c19ea195Chris Lattner          }
38512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        }
3869683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky  }
38769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
38869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Check for some cases that are worth simplifying.  Right now we want to look
38969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // for loads that are used by a switch or by the condition for the branch.  If
39069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // we see one, check to see if it's partially redundant.  If so, insert a PHI
39169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // which can then be used to thread the values.
39269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  //
39369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // This is particularly important because reg2mem inserts loads and stores all
39469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // over the place, and this blocks jump threading if we don't zap them.
395421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  Value *SimplifyValue = CondInst;
39669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
39769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    if (isa<Constant>(CondCmp->getOperand(1)))
39869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      SimplifyValue = CondCmp->getOperand(0);
39969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
40069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue))
40169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    if (SimplifyPartiallyRedundantLoad(LI))
40269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      return true;
40369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
40469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // TODO: If we have: "br (X > 0)"  and we have a predecessor where we know
40569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // "(X == 4)" thread through this block.
406a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
407d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner  return false;
408d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner}
409d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner
410421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner/// ProcessBranchOnDuplicateCond - We found a block and a predecessor of that
411421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner/// block that jump on exactly the same condition.  This means that we almost
412421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner/// always know the direction of the edge in the DESTBB:
413421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner///  PREDBB:
414421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner///     br COND, DESTBB, BBY
415421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner///  DESTBB:
416421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner///     br COND, BBZ, BBW
417421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner///
418421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner/// If DESTBB has multiple predecessors, we can't just constant fold the branch
419421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner/// in DESTBB, we have to thread over it.
420421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattnerbool JumpThreading::ProcessBranchOnDuplicateCond(BasicBlock *PredBB,
421421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner                                                 BasicBlock *BB) {
422421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  BranchInst *PredBI = cast<BranchInst>(PredBB->getTerminator());
423421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
424421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // If both successors of PredBB go to DESTBB, we don't know anything.  We can
425421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // fold the branch to an unconditional one, which allows other recursive
426421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // simplifications.
427421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  bool BranchDir;
428421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (PredBI->getSuccessor(1) != BB)
429421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    BranchDir = true;
430421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  else if (PredBI->getSuccessor(0) != BB)
431421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    BranchDir = false;
432421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  else {
43393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  In block '" << PredBB->getName()
43478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' folding terminator: " << *PredBB->getTerminator() << '\n');
435421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    ++NumFolds;
436421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    ConstantFoldTerminator(PredBB);
437421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    return true;
438421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  }
439421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
440421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  BranchInst *DestBI = cast<BranchInst>(BB->getTerminator());
441421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
442421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // If the dest block has one predecessor, just fix the branch condition to a
443421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // constant and fold it.
444421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  if (BB->getSinglePredecessor()) {
44593b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  In block '" << BB->getName()
44693b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar          << "' folding condition to '" << BranchDir << "': "
44778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << *BB->getTerminator() << '\n');
448421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    ++NumFolds;
4495a06cf644f35b873686c8694968192ad3385e36aChris Lattner    Value *OldCond = DestBI->getCondition();
4501d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    DestBI->setCondition(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
4511d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                          BranchDir));
452421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    ConstantFoldTerminator(BB);
4535a06cf644f35b873686c8694968192ad3385e36aChris Lattner    RecursivelyDeleteTriviallyDeadInstructions(OldCond);
454421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner    return true;
455421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  }
456bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner
457421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
458421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  // Next, figure out which successor we are threading to.
459421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner  BasicBlock *SuccBB = DestBI->getSuccessor(!BranchDir);
460421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
461fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  // Ok, try to thread it!
462bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner  return ThreadEdge(BB, PredBB, SuccBB);
463421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner}
464421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner
4653cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner/// ProcessSwitchOnDuplicateCond - We found a block and a predecessor of that
4663cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner/// block that switch on exactly the same condition.  This means that we almost
4673cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner/// always know the direction of the edge in the DESTBB:
4683cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner///  PREDBB:
4693cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner///     switch COND [... DESTBB, BBY ... ]
4703cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner///  DESTBB:
4713cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner///     switch COND [... BBZ, BBW ]
4723cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner///
4733cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner/// Optimizing switches like this is very important, because simplifycfg builds
4743cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner/// switches out of repeated 'if' conditions.
4753cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattnerbool JumpThreading::ProcessSwitchOnDuplicateCond(BasicBlock *PredBB,
4763cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner                                                 BasicBlock *DestBB) {
4772c7ed11d93239dacf81540e2307f0db456bb9122Chris Lattner  // Can't thread edge to self.
4782c7ed11d93239dacf81540e2307f0db456bb9122Chris Lattner  if (PredBB == DestBB)
4792c7ed11d93239dacf81540e2307f0db456bb9122Chris Lattner    return false;
4802c7ed11d93239dacf81540e2307f0db456bb9122Chris Lattner
4813cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  SwitchInst *PredSI = cast<SwitchInst>(PredBB->getTerminator());
4823cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  SwitchInst *DestSI = cast<SwitchInst>(DestBB->getTerminator());
4833cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
4843cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  // There are a variety of optimizations that we can potentially do on these
4853cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  // blocks: we order them from most to least preferable.
4863cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
4873cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  // If DESTBB *just* contains the switch, then we can forward edges from PREDBB
4883cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  // directly to their destination.  This does not introduce *any* code size
4896b233395025069f63156ea2b524cdb708a14731fDale Johannesen  // growth.  Skip debug info first.
4906b233395025069f63156ea2b524cdb708a14731fDale Johannesen  BasicBlock::iterator BBI = DestBB->begin();
4916b233395025069f63156ea2b524cdb708a14731fDale Johannesen  while (isa<DbgInfoIntrinsic>(BBI))
4926b233395025069f63156ea2b524cdb708a14731fDale Johannesen    BBI++;
4933cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
4943cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  // FIXME: Thread if it just contains a PHI.
4956b233395025069f63156ea2b524cdb708a14731fDale Johannesen  if (isa<SwitchInst>(BBI)) {
4963cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    bool MadeChange = false;
4973cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    // Ignore the default edge for now.
4983cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    for (unsigned i = 1, e = DestSI->getNumSuccessors(); i != e; ++i) {
4993cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      ConstantInt *DestVal = DestSI->getCaseValue(i);
5003cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      BasicBlock *DestSucc = DestSI->getSuccessor(i);
5013cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5023cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // Okay, DestSI has a case for 'DestVal' that goes to 'DestSucc'.  See if
5033cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // PredSI has an explicit case for it.  If so, forward.  If it is covered
5043cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // by the default case, we can't update PredSI.
5053cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      unsigned PredCase = PredSI->findCaseValue(DestVal);
5063cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      if (PredCase == 0) continue;
5073cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5083cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // If PredSI doesn't go to DestBB on this value, then it won't reach the
5093cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // case on this condition.
5103cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      if (PredSI->getSuccessor(PredCase) != DestBB &&
5113cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner          DestSI->getSuccessor(i) != DestBB)
5123cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner        continue;
5133cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5143cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // Otherwise, we're safe to make the change.  Make sure that the edge from
5153cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // DestSI to DestSucc is not critical and has no PHI nodes.
51693b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar      DEBUG(errs() << "FORWARDING EDGE " << *DestVal << "   FROM: " << *PredSI);
51793b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar      DEBUG(errs() << "THROUGH: " << *DestSI);
5183cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5193cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // If the destination has PHI nodes, just split the edge for updating
5203cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      // simplicity.
5213cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      if (isa<PHINode>(DestSucc->begin()) && !DestSucc->getSinglePredecessor()){
5223cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner        SplitCriticalEdge(DestSI, i, this);
5233cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner        DestSucc = DestSI->getSuccessor(i);
5243cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      }
5253cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      FoldSingleEntryPHINodes(DestSucc);
5263cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      PredSI->setSuccessor(PredCase, DestSucc);
5273cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      MadeChange = true;
5283cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    }
5293cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5303cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner    if (MadeChange)
5313cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner      return true;
5323cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  }
5333cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5343cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner  return false;
5353cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner}
5363cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
5373cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner
53869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant
53969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// load instruction, eliminate it by replacing it with a PHI node.  This is an
54069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// important optimization that encourages jump threading, and needs to be run
54169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// interlaced with other jump threading tasks.
54269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattnerbool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {
54369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Don't hack volatile loads.
54469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (LI->isVolatile()) return false;
54569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
54669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If the load is defined in a block with exactly one predecessor, it can't be
54769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // partially redundant.
54869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  BasicBlock *LoadBB = LI->getParent();
54969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (LoadBB->getSinglePredecessor())
55069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    return false;
55169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
55269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  Value *LoadedPtr = LI->getOperand(0);
55369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
55469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If the loaded operand is defined in the LoadBB, it can't be available.
55569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // FIXME: Could do PHI translation, that would be fun :)
55669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr))
55769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    if (PtrOp->getParent() == LoadBB)
55869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      return false;
55969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
56069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Scan a few instructions up from the load, to see if it is obviously live at
56169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // the entry to its block.
56269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  BasicBlock::iterator BBIt = LI;
56369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
56452c95856b4a40ccae6c4b0e13b2a04101e1f79c9Chris Lattner  if (Value *AvailableVal = FindAvailableLoadedValue(LoadedPtr, LoadBB,
56552c95856b4a40ccae6c4b0e13b2a04101e1f79c9Chris Lattner                                                     BBIt, 6)) {
56669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // If the value if the load is locally available within the block, just use
56769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // it.  This frequently occurs for reg2mem'd allocas.
56869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    //cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n";
5692a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner
5702a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner    // If the returned value is the load itself, replace with an undef. This can
5712a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner    // only happen in dead loops.
5729e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    if (AvailableVal == LI) AvailableVal = UndefValue::get(LI->getType());
57369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    LI->replaceAllUsesWith(AvailableVal);
57469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    LI->eraseFromParent();
57569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    return true;
57669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  }
57769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
57869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Otherwise, if we scanned the whole block and got to the top of the block,
57969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // we know the block is locally transparent to the load.  If not, something
58069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // might clobber its value.
58169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (BBIt != LoadBB->begin())
58269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    return false;
58369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
58469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
58569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  SmallPtrSet<BasicBlock*, 8> PredsScanned;
58669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy;
58769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  AvailablePredsTy AvailablePreds;
58869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  BasicBlock *OneUnavailablePred = 0;
58969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
59069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If we got here, the loaded value is transparent through to the start of the
59169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // block.  Check to see if it is available in any of the predecessor blocks.
59269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
59369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner       PI != PE; ++PI) {
59469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    BasicBlock *PredBB = *PI;
59569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
59669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // If we already scanned this predecessor, skip it.
59769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    if (!PredsScanned.insert(PredBB))
59869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      continue;
59969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
60069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // Scan the predecessor to see if the value is available in the pred.
60169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    BBIt = PredBB->end();
60252c95856b4a40ccae6c4b0e13b2a04101e1f79c9Chris Lattner    Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6);
60369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    if (!PredAvailable) {
60469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      OneUnavailablePred = PredBB;
60569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      continue;
60669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    }
60769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
60869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // If so, this load is partially redundant.  Remember this info so that we
60969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // can create a PHI node.
61069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable));
61169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  }
61269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
61369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If the loaded value isn't available in any predecessor, it isn't partially
61469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // redundant.
61569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (AvailablePreds.empty()) return false;
61669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
61769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Okay, the loaded value is available in at least one (and maybe all!)
61869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // predecessors.  If the value is unavailable in more than one unique
61969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // predecessor, we want to insert a merge block for those common predecessors.
62069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // This ensures that we only have to insert one reload, thus not increasing
62169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // code size.
62269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  BasicBlock *UnavailablePred = 0;
62369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
62469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If there is exactly one predecessor where the value is unavailable, the
62569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // already computed 'OneUnavailablePred' block is it.  If it ends in an
62669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // unconditional branch, we know that it isn't a critical edge.
62769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (PredsScanned.size() == AvailablePreds.size()+1 &&
62869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) {
62969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    UnavailablePred = OneUnavailablePred;
63069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  } else if (PredsScanned.size() != AvailablePreds.size()) {
63169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // Otherwise, we had multiple unavailable predecessors or we had a critical
63269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // edge from the one.
63369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    SmallVector<BasicBlock*, 8> PredsToSplit;
63469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    SmallPtrSet<BasicBlock*, 8> AvailablePredSet;
63569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
63669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i)
63769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      AvailablePredSet.insert(AvailablePreds[i].first);
63869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
63969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // Add all the unavailable predecessors to the PredsToSplit list.
64069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
64169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner         PI != PE; ++PI)
64269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      if (!AvailablePredSet.count(*PI))
64369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner        PredsToSplit.push_back(*PI);
64469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
64569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    // Split them out to their own block.
64669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    UnavailablePred =
64769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      SplitBlockPredecessors(LoadBB, &PredsToSplit[0], PredsToSplit.size(),
64869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner                             "thread-split", this);
64969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  }
65069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
65169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // If the value isn't available in all predecessors, then there will be
65269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // exactly one where it isn't available.  Insert a load on that edge and add
65369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // it to the AvailablePreds list.
65469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  if (UnavailablePred) {
65569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 &&
65669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner           "Can't handle critical edge here!");
65769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    Value *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr",
65869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner                                 UnavailablePred->getTerminator());
65969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal));
66069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  }
66169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
66269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Now we know that each predecessor of this block has a value in
66369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // AvailablePreds, sort them for efficient access as we're walking the preds.
664a3522000ab9c821f48856d0c25ada8297c1c2914Chris Lattner  array_pod_sort(AvailablePreds.begin(), AvailablePreds.end());
66569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
66669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Create a PHI node at the start of the block for the PRE'd load value.
66769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  PHINode *PN = PHINode::Create(LI->getType(), "", LoadBB->begin());
66869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  PN->takeName(LI);
66969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
67069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // Insert new entries into the PHI for each predecessor.  A single block may
67169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  // have multiple entries here.
67269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); PI != E;
67369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner       ++PI) {
67469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    AvailablePredsTy::iterator I =
67569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner      std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(),
67669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner                       std::make_pair(*PI, (Value*)0));
67769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
67869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    assert(I != AvailablePreds.end() && I->first == *PI &&
67969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner           "Didn't find entry for predecessor!");
68069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
68169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner    PN->addIncoming(I->second, I->first);
68269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  }
68369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
68469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  //cerr << "PRE: " << *LI << *PN << "\n";
68569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
68669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  LI->replaceAllUsesWith(PN);
68769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  LI->eraseFromParent();
68869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
68969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner  return true;
69069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner}
69169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner
692785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
69312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// ProcessJumpOnPHI - We have a conditional branch or switch on a PHI node in
69412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// the current block.  See if there are any simplifications we can do based on
69512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// inputs to the phi node.
69612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel///
69712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patelbool JumpThreading::ProcessJumpOnPHI(PHINode *PN) {
69812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  BasicBlock *BB = PN->getParent();
699785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
70012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // See if the phi node has any constant integer or undef values.  If so, we
70112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // can determine where the corresponding predecessor will branch.
70212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
70312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    Value *PredVal = PN->getIncomingValue(i);
704785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
70512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // Check to see if this input is a constant integer.  If so, the direction
70612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // of the branch is predictable.
70712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (ConstantInt *CI = dyn_cast<ConstantInt>(PredVal)) {
70812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // Merge any common predecessors that will act the same.
70912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      BasicBlock *PredBB = FactorCommonPHIPreds(PN, CI);
71012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
71112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      BasicBlock *SuccBB;
71212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
71312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        SuccBB = BI->getSuccessor(CI->isZero());
71412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      else {
71512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        SwitchInst *SI = cast<SwitchInst>(BB->getTerminator());
71612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        SuccBB = SI->getSuccessor(SI->findCaseValue(CI));
71712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      }
71812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
71912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // Ok, try to thread it!
72012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      return ThreadEdge(BB, PredBB, SuccBB);
721785672534d32d196d04ad022c111fde3864e0d28Chris Lattner    }
722785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
72312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // If the input is an undef, then it doesn't matter which way it will go.
72412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // Pick an arbitrary dest and thread the edge.
72512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (UndefValue *UV = dyn_cast<UndefValue>(PredVal)) {
72612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // Merge any common predecessors that will act the same.
72712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      BasicBlock *PredBB = FactorCommonPHIPreds(PN, UV);
72812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      BasicBlock *SuccBB =
72912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        BB->getTerminator()->getSuccessor(GetBestDestForJumpOnUndef(BB));
73079f0332c203b33c4afc0fb6ad946fbe041164e6eChris Lattner
73112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      // Ok, try to thread it!
73212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      return ThreadEdge(BB, PredBB, SuccBB);
73379f0332c203b33c4afc0fb6ad946fbe041164e6eChris Lattner    }
734785672534d32d196d04ad022c111fde3864e0d28Chris Lattner  }
735785672534d32d196d04ad022c111fde3864e0d28Chris Lattner
73612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If the incoming values are all variables, we don't know the destination of
73712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // any predecessors.  However, if any of the predecessor blocks end in an
73812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // unconditional branch, we can *duplicate* the jump into that block in order
73912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // to further encourage jump threading and to eliminate cases where we have
74012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // branch on a phi of an icmp (branch on icmp is much better).
74178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
74278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // We don't want to do this tranformation for switches, because we don't
74378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // really want to duplicate a switch.
74478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  if (isa<SwitchInst>(BB->getTerminator()))
74578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    return false;
74678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
74778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Look for unconditional branch predecessors.
74878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
74978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    BasicBlock *PredBB = PN->getIncomingBlock(i);
75078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator()))
75178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      if (PredBr->isUnconditional() &&
75278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          // Try to duplicate BB into PredBB.
75378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          DuplicateCondBranchOnPHIIntoPred(BB, PredBB))
75478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        return true;
75578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
75678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
7576b65f47ad5d4da8bab31802a2c3df88379f0c9d9Chris Lattner  return false;
758bd3401fa98b578080e227afce940bca80137dea6Chris Lattner}
759bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
76078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
76112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// ProcessJumpOnLogicalPHI - PN's basic block contains a conditional branch
76212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// whose condition is an AND/OR where one side is PN.  If PN has constant
76312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// operands that permit us to evaluate the condition for some operand, thread
76412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// through the block.  For example with:
76512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel///   br (and X, phi(Y, Z, false))
76612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// the predecessor corresponding to the 'false' will always jump to the false
76712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// destination of the branch.
76812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel///
76912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patelbool JumpThreading::ProcessBranchOnLogical(Value *V, BasicBlock *BB,
77012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                           bool isAnd) {
77112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If this is a binary operator tree of the same AND/OR opcode, check the
77212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // LHS/RHS.
77312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
77412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if ((isAnd && BO->getOpcode() == Instruction::And) ||
77512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        (!isAnd && BO->getOpcode() == Instruction::Or)) {
77612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      if (ProcessBranchOnLogical(BO->getOperand(0), BB, isAnd))
77712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        return true;
77812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      if (ProcessBranchOnLogical(BO->getOperand(1), BB, isAnd))
77912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel        return true;
78012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
78112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
78212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If this isn't a PHI node, we can't handle it.
78312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  PHINode *PN = dyn_cast<PHINode>(V);
78412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (!PN || PN->getParent() != BB) return false;
78512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
78612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // We can only do the simplification for phi nodes of 'false' with AND or
78712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // 'true' with OR.  See if we have any entries in the phi for this.
78812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  unsigned PredNo = ~0U;
78912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  ConstantInt *PredCst = ConstantInt::get(Type::getInt1Ty(BB->getContext()),
79012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                          !isAnd);
79112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
79212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (PN->getIncomingValue(i) == PredCst) {
79312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      PredNo = i;
79412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      break;
79512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
79612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  }
79712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
79812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If no match, bail out.
79912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (PredNo == ~0U)
80012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    return false;
80112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
80212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If so, we can actually do this threading.  Merge any common predecessors
80312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // that will act the same.
80412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst);
80512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
80612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // Next, figure out which successor we are threading to.  If this was an AND,
80712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // the constant must be FALSE, and we must be targeting the 'false' block.
80812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If this is an OR, the constant must be TRUE, and we must be targeting the
80912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // 'true' block.
81012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(isAnd);
81112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
81212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // Ok, try to thread it!
81312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  return ThreadEdge(BB, PredBB, SuccBB);
81412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel}
81512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
81612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// GetResultOfComparison - Given an icmp/fcmp predicate and the left and right
81712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// hand sides of the compare instruction, try to determine the result. If the
81812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// result can not be determined, a null pointer is returned.
81912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patelstatic Constant *GetResultOfComparison(CmpInst::Predicate pred,
82012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                       Value *LHS, Value *RHS,
82112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                       LLVMContext &Context) {
82212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (Constant *CLHS = dyn_cast<Constant>(LHS))
82312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (Constant *CRHS = dyn_cast<Constant>(RHS))
82412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      return ConstantExpr::getCompare(pred, CLHS, CRHS);
82512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
82612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (LHS == RHS)
82712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (isa<IntegerType>(LHS->getType()) || isa<PointerType>(LHS->getType()))
82812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      return ICmpInst::isTrueWhenEqual(pred) ?
82912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                 ConstantInt::getTrue(Context) : ConstantInt::getFalse(Context);
83012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
83112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  return 0;
83212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel}
83312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
83412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// ProcessBranchOnCompare - We found a branch on a comparison between a phi
83512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// node and a value.  If we can identify when the comparison is true between
83612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// the phi inputs and the value, we can fold the compare for that edge and
83712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel/// thread through it.
83812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patelbool JumpThreading::ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB) {
83912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  PHINode *PN = cast<PHINode>(Cmp->getOperand(0));
84012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  Value *RHS = Cmp->getOperand(1);
84112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
84212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If the phi isn't in the current block, an incoming edge to this block
84312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // doesn't control the destination.
84412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (PN->getParent() != BB)
84512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    return false;
84612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
84712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // We can do this simplification if any comparisons fold to true or false.
84812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // See if any do.
84912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  Value *PredVal = 0;
85012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  bool TrueDirection = false;
85112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
85212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    PredVal = PN->getIncomingValue(i);
85312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
85412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    Constant *Res = GetResultOfComparison(Cmp->getPredicate(), PredVal,
85512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel                                          RHS, Cmp->getContext());
85612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (!Res) {
85712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      PredVal = 0;
85812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      continue;
85912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
86012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
86112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // If this folded to a constant expr, we can't do anything.
86212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (ConstantInt *ResC = dyn_cast<ConstantInt>(Res)) {
86312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      TrueDirection = ResC->getZExtValue();
86412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      break;
86512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
86612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // If this folded to undef, just go the false way.
86712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    if (isa<UndefValue>(Res)) {
86812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      TrueDirection = false;
86912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel      break;
87012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    }
87112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
87212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    // Otherwise, we can't fold this input.
87312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    PredVal = 0;
87412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  }
87512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
87612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If no match, bail out.
87712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  if (PredVal == 0)
87812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel    return false;
87912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
88012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // If so, we can actually do this threading.  Merge any common predecessors
88112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // that will act the same.
88212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredVal);
88312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
88412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // Next, get our successor.
88512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(!TrueDirection);
88612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
88712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  // Ok, try to thread it!
88812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel  return ThreadEdge(BB, PredBB, SuccBB);
88912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel}
89012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
89112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel
89278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new
89378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// predecessor to the PHIBB block.  If it has PHI nodes, add entries for
89478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// NewPred using the entries from OldPred (suitably mapped).
89578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
89678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                            BasicBlock *OldPred,
89778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                            BasicBlock *NewPred,
89878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                     DenseMap<Instruction*, Value*> &ValueMap) {
89978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (BasicBlock::iterator PNI = PHIBB->begin();
90078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner       PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) {
90178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Ok, we have a PHI node.  Figure out what the incoming value was for the
90278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // DestBlock.
90378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    Value *IV = PN->getIncomingValueForBlock(OldPred);
904bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner
90578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Remap the value if necessary.
90678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (Instruction *Inst = dyn_cast<Instruction>(IV)) {
90778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst);
90878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      if (I != ValueMap.end())
90978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        IV = I->second;
910bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner    }
91178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
91278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    PN->addIncoming(IV, NewPred);
913bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner  }
914fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump}
915fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
916fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// ThreadEdge - We have decided that it is safe and profitable to thread an
917fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// edge from PredBB to SuccBB across BB.  Transform the IR to reflect this
918fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// change.
919fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stumpbool JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB,
920bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner                               BasicBlock *SuccBB) {
921eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner  // If threading to the same block as we come from, we would infinite loop.
922eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner  if (SuccBB == BB) {
92393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  Not threading across BB '" << BB->getName()
92493b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar          << "' - would thread to self!\n");
925eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner    return false;
926eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner  }
927eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner
928fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  // If threading this would thread across a loop header, don't thread the edge.
929fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  // See the comments above FindLoopHeaders for justifications and caveats.
930fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  if (LoopHeaders.count(BB)) {
93193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar    DEBUG(errs() << "  Not threading from '" << PredBB->getName()
93293b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar          << "' across loop header BB '" << BB->getName()
93393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar          << "' to dest BB '" << SuccBB->getName()
93493b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar          << "' - it might create an irreducible loop!\n");
935fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump    return false;
936fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  }
937fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
93878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
93978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  if (JumpThreadCost > Threshold) {
94078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    DEBUG(errs() << "  Not threading BB '" << BB->getName()
94178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' - Cost is too high: " << JumpThreadCost << "\n");
94278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    return false;
94378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
94478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
945a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner  // And finally, do it!
94693b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar  DEBUG(errs() << "  Threading edge from '" << PredBB->getName() << "' to '"
947460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar        << SuccBB->getName() << "' with cost: " << JumpThreadCost
94893b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar        << ", across block:\n    "
94993b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar        << *BB << "\n");
950a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner
951bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We are going to have to map operands from the original BB block to the new
952bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // copy of the block 'NewBB'.  If there are PHI nodes in BB, evaluate them to
953bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // account for entry from PredBB.
954bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  DenseMap<Instruction*, Value*> ValueMapping;
955bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
9561d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  BasicBlock *NewBB = BasicBlock::Create(BB->getContext(),
9571d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                         BB->getName()+".thread",
9581d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                         BB->getParent(), BB);
959bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  NewBB->moveAfter(PredBB);
960bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
961bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BasicBlock::iterator BI = BB->begin();
962bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
963bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
964bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
965bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Clone the non-phi instructions of BB into NewBB, keeping track of the
966bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // mapping and using it to remap operands in the cloned instructions.
967bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (; !isa<TerminatorInst>(BI); ++BI) {
9686776064d190701c5bae4d5403939eed2e480d1cdNick Lewycky    Instruction *New = BI->clone();
969460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar    New->setName(BI->getName());
970bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    NewBB->getInstList().push_back(New);
971bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    ValueMapping[BI] = New;
972bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
973bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    // Remap operands to patch up intra-block references.
974bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
975f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman      if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
976f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman        DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
977f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman        if (I != ValueMapping.end())
978f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman          New->setOperand(i, I->second);
979f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman      }
980bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  }
981bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
982bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // We didn't copy the terminator from BB over to NewBB, because there is now
983bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // an unconditional jump to SuccBB.  Insert the unconditional jump.
984bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  BranchInst::Create(SuccBB, NewBB);
985bd3401fa98b578080e227afce940bca80137dea6Chris Lattner
986bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
987bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // PHI nodes for NewBB now.
98878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping);
989177480b7ede0441135572d641a2497df25a7d95fChris Lattner
990433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  // If there were values defined in BB that are used outside the block, then we
991433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  // now have to update all uses of the value to use either the original value,
992433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  // the cloned value, or some PHI derived value.  This can require arbitrary
993433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  // PHI insertion, of which we are prepared to do, clean these up now.
994433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  SSAUpdater SSAUpdate;
995433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  SmallVector<Use*, 16> UsesToRename;
996433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
997433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // Scan all uses of this instruction to see if it is used outside of its
998433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // block, and if so, record them in UsesToRename.
999433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
1000433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner         ++UI) {
1001433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      Instruction *User = cast<Instruction>(*UI);
1002433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
1003433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner        if (UserPN->getIncomingBlock(UI) == BB)
1004433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner          continue;
1005433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      } else if (User->getParent() == BB)
1006433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner        continue;
1007433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1008433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      UsesToRename.push_back(&UI.getUse());
1009433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    }
1010433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1011433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // If there are no uses outside the block, we're done with this instruction.
1012433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    if (UsesToRename.empty())
1013433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      continue;
1014433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1015433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    DEBUG(errs() << "JT: Renaming non-local uses of: " << *I << "\n");
1016433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1017433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // We found a use of I outside of BB.  Rename all uses of I that are outside
1018433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // its block to be uses of the appropriate PHI node etc.  See ValuesInBlocks
1019433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    // with the two values we know.
1020433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    SSAUpdate.Initialize(I);
1021433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    SSAUpdate.AddAvailableValue(BB, I);
1022433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    SSAUpdate.AddAvailableValue(NewBB, ValueMapping[I]);
1023433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1024433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    while (!UsesToRename.empty())
1025433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner      SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
1026433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner    DEBUG(errs() << "\n");
1027433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner  }
1028433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1029433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner
1030ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  // Ok, NewBB is good to go.  Update the terminator of PredBB to jump to
1031bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // NewBB instead of BB.  This eliminates predecessors from BB, which requires
1032bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  // us to simplify any PHI nodes in BB.
1033bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  TerminatorInst *PredTerm = PredBB->getTerminator();
1034bd3401fa98b578080e227afce940bca80137dea6Chris Lattner  for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
1035bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    if (PredTerm->getSuccessor(i) == BB) {
1036bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      BB->removePredecessor(PredBB);
1037bd3401fa98b578080e227afce940bca80137dea6Chris Lattner      PredTerm->setSuccessor(i, NewBB);
1038bd3401fa98b578080e227afce940bca80137dea6Chris Lattner    }
1039ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner
1040ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  // At this point, the IR is fully up to date and consistent.  Do a quick scan
1041ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  // over the new instructions and zap any that are constants or dead.  This
1042ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  // frequently happens because of phi translation.
1043ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  BI = NewBB->begin();
1044ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  for (BasicBlock::iterator E = NewBB->end(); BI != E; ) {
1045ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner    Instruction *Inst = BI++;
10467b550ccfc5a3346c17e0390a59e2d6d19bc52705Chris Lattner    if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
1047ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner      Inst->replaceAllUsesWith(C);
1048ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner      Inst->eraseFromParent();
1049ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner      continue;
1050ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner    }
1051ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner
1052ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner    RecursivelyDeleteTriviallyDeadInstructions(Inst);
1053ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner  }
1054fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump
1055fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  // Threaded an edge!
1056fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  ++NumThreads;
1057fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump  return true;
1058177480b7ede0441135572d641a2497df25a7d95fChris Lattner}
105978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
106078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch
106178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// to BB which contains an i1 PHI node and a conditional branch on that PHI.
106278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// If we can duplicate the contents of BB up into PredBB do so now, this
106378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// improves the odds that the branch will be on an analyzable instruction like
106478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// a compare.
106578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerbool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,
106678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                                     BasicBlock *PredBB) {
106778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // If BB is a loop header, then duplicating this block outside the loop would
106878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // cause us to transform this into an irreducible loop, don't do this.
106978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // See the comments above FindLoopHeaders for justifications and caveats.
107078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  if (LoopHeaders.count(BB)) {
107178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    DEBUG(errs() << "  Not duplicating loop header '" << BB->getName()
107278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' into predecessor block '" << PredBB->getName()
107378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' - it might create an irreducible loop!\n");
107478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    return false;
107578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
107678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
107778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  unsigned DuplicationCost = getJumpThreadDuplicationCost(BB);
107878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  if (DuplicationCost > Threshold) {
107978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    DEBUG(errs() << "  Not duplicating BB '" << BB->getName()
108078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          << "' - Cost is too high: " << DuplicationCost << "\n");
108178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    return false;
108278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
108378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
108478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Okay, we decided to do this!  Clone all the instructions in BB onto the end
108578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // of PredBB.
108678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  DEBUG(errs() << "  Duplicating block '" << BB->getName() << "' into end of '"
108778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        << PredBB->getName() << "' to eliminate branch on phi.  Cost: "
108878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        << DuplicationCost << " block is:" << *BB << "\n");
108978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
109078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // We are going to have to map operands from the original BB block into the
109178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // PredBB block.  Evaluate PHI nodes in BB.
109278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  DenseMap<Instruction*, Value*> ValueMapping;
109378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
109478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  BasicBlock::iterator BI = BB->begin();
109578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
109678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
109778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
109878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  BranchInst *OldPredBranch = cast<BranchInst>(PredBB->getTerminator());
109978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
110078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Clone the non-phi instructions of BB into PredBB, keeping track of the
110178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // mapping and using it to remap operands in the cloned instructions.
110278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (; BI != BB->end(); ++BI) {
110378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    Instruction *New = BI->clone();
110478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    New->setName(BI->getName());
110578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    PredBB->getInstList().insert(OldPredBranch, New);
110678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    ValueMapping[BI] = New;
110778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
110878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Remap operands to patch up intra-block references.
110978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
111078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
111178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
111278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        if (I != ValueMapping.end())
111378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          New->setOperand(i, I->second);
111478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      }
111578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
111678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
111778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Check to see if the targets of the branch had PHI nodes. If so, we need to
111878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // add entries to the PHI nodes for branch from PredBB now.
111978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator());
112078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB,
112178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                  ValueMapping);
112278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
112378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner                                  ValueMapping);
112478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
112578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // If there were values defined in BB that are used outside the block, then we
112678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // now have to update all uses of the value to use either the original value,
112778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // the cloned value, or some PHI derived value.  This can require arbitrary
112878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // PHI insertion, of which we are prepared to do, clean these up now.
112978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  SSAUpdater SSAUpdate;
113078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  SmallVector<Use*, 16> UsesToRename;
113178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
113278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // Scan all uses of this instruction to see if it is used outside of its
113378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // block, and if so, record them in UsesToRename.
113478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
113578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner         ++UI) {
113678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      Instruction *User = cast<Instruction>(*UI);
113778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
113878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        if (UserPN->getIncomingBlock(UI) == BB)
113978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner          continue;
114078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      } else if (User->getParent() == BB)
114178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner        continue;
114278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
114378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      UsesToRename.push_back(&UI.getUse());
114478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    }
114578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
114678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // If there are no uses outside the block, we're done with this instruction.
114778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    if (UsesToRename.empty())
114878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      continue;
114978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
115078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    DEBUG(errs() << "JT: Renaming non-local uses of: " << *I << "\n");
115178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
115278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // We found a use of I outside of BB.  Rename all uses of I that are outside
115378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // its block to be uses of the appropriate PHI node etc.  See ValuesInBlocks
115478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    // with the two values we know.
115578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    SSAUpdate.Initialize(I);
115678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    SSAUpdate.AddAvailableValue(BB, I);
115778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    SSAUpdate.AddAvailableValue(PredBB, ValueMapping[I]);
115878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
115978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    while (!UsesToRename.empty())
116078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner      SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
116178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner    DEBUG(errs() << "\n");
116278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  }
116378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
116478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // PredBB no longer jumps to BB, remove entries in the PHI node for the edge
116578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // that we nuked.
116678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  BB->removePredecessor(PredBB);
116778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
116878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  // Remove the unconditional branch at the end of the PredBB block.
116978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  OldPredBranch->eraseFromParent();
117078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
117178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  ++NumDupes;
117278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner  return true;
117378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner}
117478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
117578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner
1176