JumpThreading.cpp revision 01abcf339f8d42921c680fefb2ff988cfeee1198
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" 199819ef7742cc2e3af92a0b760fa33e30218ff7bbChris Lattner#include "llvm/Analysis/InstructionSimplify.h" 20cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner#include "llvm/Analysis/LazyValueInfo.h" 21dd9344f3face8f1978a7f9f393c31b628144d1f6Dan Gohman#include "llvm/Analysis/Loads.h" 222cc675180b06d0a2173365a4015606933f1a285dChris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h" 23bd3401fa98b578080e227afce940bca80137dea6Chris Lattner#include "llvm/Transforms/Utils/Local.h" 24433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner#include "llvm/Transforms/Utils/SSAUpdater.h" 25ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner#include "llvm/Target/TargetData.h" 26fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/DenseMap.h" 27cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson#include "llvm/ADT/DenseSet.h" 28fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/Statistic.h" 29fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/STLExtras.h" 30fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallPtrSet.h" 31fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallSet.h" 328383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/CommandLine.h" 33177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/Support/Debug.h" 345660846f15847e540066ae320a4adef7357d597dChris Lattner#include "llvm/Support/ValueHandle.h" 3593b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar#include "llvm/Support/raw_ostream.h" 368383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerusing namespace llvm; 378383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 38bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumThreads, "Number of jumps threaded"); 39bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumFolds, "Number of terminators folded"); 4078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris LattnerSTATISTIC(NumDupes, "Number of branch blocks duplicated to eliminate phi"); 418383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 42177480b7ede0441135572d641a2497df25a7d95fChris Lattnerstatic cl::opt<unsigned> 436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van BommelThreshold("jump-threading-threshold", 44177480b7ede0441135572d641a2497df25a7d95fChris Lattner cl::desc("Max block size to duplicate for jump threading"), 45177480b7ede0441135572d641a2497df25a7d95fChris Lattner cl::init(6), cl::Hidden); 46177480b7ede0441135572d641a2497df25a7d95fChris Lattner 478383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnernamespace { 48ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // These are at global scope so static functions can use them too. 49ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel typedef SmallVectorImpl<std::pair<Constant*, BasicBlock*> > PredValueInfo; 50ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel typedef SmallVector<std::pair<Constant*, BasicBlock*>, 8> PredValueInfoTy; 51ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 526033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // This is used to keep track of what kind of constant we're currently hoping 536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // to find. 546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel enum ConstantPreference { 556033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger, 566033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantBlockAddress 576033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel }; 586033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 5994019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// This pass performs 'jump threading', which looks at blocks that have 6094019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// multiple predecessors and multiple successors. If one or more of the 6194019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// predecessors of the block can be proven to always jump to one of the 6294019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// successors, we forward the edge from the predecessor to the successor by 6394019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// duplicating the contents of this block. 6494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 6594019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// An example of when this can occur is code like this: 6694019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 6794019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// if () { ... 6894019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// X = 4; 6994019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// } 7094019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// if (X < 3) { 7194019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 7294019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// In this case, the unconditional branch at the end of the first if can be 7394019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// revectored to the false side of the second if. 7494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 753e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner class JumpThreading : public FunctionPass { 76ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner TargetData *TD; 77cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner LazyValueInfo *LVI; 78fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#ifdef NDEBUG 79fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallPtrSet<BasicBlock*, 16> LoopHeaders; 80fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#else 81fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallSet<AssertingVH<BasicBlock>, 16> LoopHeaders; 82fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#endif 83cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson DenseSet<std::pair<Value*, BasicBlock*> > RecursionSet; 846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 859ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // RAII helper for updating the recursion stack. 869ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson struct RecursionSetRemover { 879ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson DenseSet<std::pair<Value*, BasicBlock*> > &TheSet; 889ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson std::pair<Value*, BasicBlock*> ThePair; 896f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 909ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson RecursionSetRemover(DenseSet<std::pair<Value*, BasicBlock*> > &S, 919ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson std::pair<Value*, BasicBlock*> P) 929ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson : TheSet(S), ThePair(P) { } 936f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 949ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson ~RecursionSetRemover() { 959ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson TheSet.erase(ThePair); 969ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson } 979ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson }; 988383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner public: 998383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner static char ID; // Pass identification 100081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson JumpThreading() : FunctionPass(ID) { 101081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson initializeJumpThreadingPass(*PassRegistry::getPassRegistry()); 102081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson } 1038383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 1048383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner bool runOnFunction(Function &F); 1056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 106cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const { 107c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson AU.addRequired<LazyValueInfo>(); 108c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson AU.addPreserved<LazyValueInfo>(); 109cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 1106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 111cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner void FindLoopHeaders(Function &F); 112c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner bool ProcessBlock(BasicBlock *BB); 1135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock*> &PredBBs, 1145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *SuccBB); 11578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, 1162249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner const SmallVectorImpl<BasicBlock *> &PredBBs); 1176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1185729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, 1196033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel PredValueInfo &Result, 1206033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference); 1216033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB, 1226033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference); 1236f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12477beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner bool ProcessBranchOnPHI(PHINode *PN); 1252249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner bool ProcessBranchOnXOR(BinaryOperator *BO); 1266f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner bool SimplifyPartiallyRedundantLoad(LoadInst *LI); 1288383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner }; 1298383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner} 1308383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 131844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar JumpThreading::ID = 0; 1322ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading", 1332ab36d350293c77fc8941ce1023e4899df7e3a82Owen Anderson "Jump Threading", false, false) 1342ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_DEPENDENCY(LazyValueInfo) 1352ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_END(JumpThreading, "jump-threading", 136ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson "Jump Threading", false, false) 137844731a7f1909f55935e3514c9e713a62d67662eDan Gohman 1388383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// Public interface to the Jump Threading pass 1398383a7b7a6acdae478d4b4c2520915153b73f676Chris LattnerFunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); } 1408383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 1418383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// runOnFunction - Top level algorithm. 1428383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// 1438383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerbool JumpThreading::runOnFunction(Function &F) { 144fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n"); 14502a436c48ecff9e34d50ce0a2f861e5acdd9bf3fDan Gohman TD = getAnalysisIfAvailable<TargetData>(); 146c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI = &getAnalysis<LazyValueInfo>(); 1476f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 148fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump FindLoopHeaders(F); 1496f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 15066b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer bool Changed, EverChanged = false; 15166b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer do { 15266b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer Changed = false; 153421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner for (Function::iterator I = F.begin(), E = F.end(); I != E;) { 154421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner BasicBlock *BB = I; 1556f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel // Thread all of the branches we can over this block. 156421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner while (ProcessBlock(BB)) 157bd3401fa98b578080e227afce940bca80137dea6Chris Lattner Changed = true; 1586f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 159421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner ++I; 1606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 161421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // If the block is trivially dead, zap it. This eliminates the successor 162421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // edges which simplifies the CFG. 163421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner if (pred_begin(BB) == pred_end(BB) && 16420fa76ef6f7c2d3073e0960cf347af8db64708fcChris Lattner BB != &BB->getParent()->getEntryBlock()) { 165fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " JT: Deleting dead block '" << BB->getName() 16678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' with terminator: " << *BB->getTerminator() << '\n'); 167fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.erase(BB); 168c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->eraseBlock(BB); 169421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner DeleteDeadBlock(BB); 170421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Changed = true; 171e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner continue; 172e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner } 173e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 174e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 175e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 176e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // Can't thread an unconditional jump, but if the block is "almost 177e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // empty", we can replace uses of it with uses of the successor and make 178e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // this dead. 179e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (BI && BI->isUnconditional() && 180e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB != &BB->getParent()->getEntryBlock() && 181f3183f6da6321c6496daa107ea5d6d48b99d9330Chris Lattner // If the terminator is the only non-phi instruction, try to nuke it. 182e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB->getFirstNonPHIOrDbg()->isTerminator()) { 183e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // Since TryToSimplifyUncondBranchFromEmptyBlock may delete the 184e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // block, we have to make sure it isn't in the LoopHeaders set. We 185e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // reinsert afterward if needed. 186e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner bool ErasedFromLoopHeaders = LoopHeaders.erase(BB); 187e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BasicBlock *Succ = BI->getSuccessor(0); 188e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 189e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // FIXME: It is always conservatively correct to drop the info 190e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // for a block even if it doesn't get erased. This isn't totally 191e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // awesome, but it allows us to use AssertingVH to prevent nasty 192e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // dangling pointer issues within LazyValueInfo. 193e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner LVI->eraseBlock(BB); 194e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (TryToSimplifyUncondBranchFromEmptyBlock(BB)) { 195e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner Changed = true; 196e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // If we deleted BB and BB was the header of a loop, then the 197e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // successor is now the header of the loop. 198e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB = Succ; 199f3183f6da6321c6496daa107ea5d6d48b99d9330Chris Lattner } 200e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 201e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (ErasedFromLoopHeaders) 202e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner LoopHeaders.insert(BB); 203421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 204421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 205bd3401fa98b578080e227afce940bca80137dea6Chris Lattner EverChanged |= Changed; 20666b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer } while (Changed); 2076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 208fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.clear(); 209bd3401fa98b578080e227afce940bca80137dea6Chris Lattner return EverChanged; 2108383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner} 211177480b7ede0441135572d641a2497df25a7d95fChris Lattner 21278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to 21378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// thread across it. 21478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) { 21578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner /// Ignore PHI nodes, these will be flattened when duplication happens. 21678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock::const_iterator I = BB->getFirstNonPHI(); 2176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 218b14b88a40ab12996c2982c4bc10fd35bb9a371d4Chris Lattner // FIXME: THREADING will delete values that are just used to compute the 219b14b88a40ab12996c2982c4bc10fd35bb9a371d4Chris Lattner // branch, so they shouldn't count against the duplication cost. 2206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 2216f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 22278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Sum up the cost of each instruction until we get to the terminator. Don't 22378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // include the terminator because the copy won't include it. 22478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned Size = 0; 22578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; !isa<TerminatorInst>(I); ++I) { 22678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Debugger intrinsics don't incur code size. 22778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (isa<DbgInfoIntrinsic>(I)) continue; 2286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 22978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If this is a pointer->pointer bitcast, it is free. 2301df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (isa<BitCastInst>(I) && I->getType()->isPointerTy()) 23178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 2326f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 23378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // All other instructions count for at least one unit. 23478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++Size; 2356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 23678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Calls are more expensive. If they are non-intrinsic calls, we model them 23778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // as having cost of 4. If they are a non-vector intrinsic, we model them 23878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // as having cost of 2 total, and if they are a vector intrinsic, we model 23978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // them as having cost 1. 24078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (const CallInst *CI = dyn_cast<CallInst>(I)) { 24178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (!isa<IntrinsicInst>(CI)) 24278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size += 3; 2431df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands else if (!CI->getType()->isVectorTy()) 24478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size += 1; 24578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 24678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 2476f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 24878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Threading through a switch statement is particularly profitable. If this 24978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // block ends in a switch, decrease its cost to make it more likely to happen. 25078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (isa<SwitchInst>(I)) 25178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size = Size > 6 ? Size-6 : 0; 2526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 2536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // The same holds for indirect branches, but slightly more so. 2546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (isa<IndirectBrInst>(I)) 2556033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Size = Size > 8 ? Size-8 : 0; 2566033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 25778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return Size; 25878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner} 25978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 260fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// FindLoopHeaders - We do not want jump threading to turn proper loop 261fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// structures into irreducible loops. Doing this breaks up the loop nesting 262fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// hierarchy and pessimizes later transformations. To prevent this from 263fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// happening, we first have to find the loop headers. Here we approximate this 264fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// by finding targets of backedges in the CFG. 265fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// 266fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// Note that there definitely are cases when we want to allow threading of 267fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// edges across a loop header. For example, threading a jump from outside the 268fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// loop (the preheader) to an exit block of the loop is definitely profitable. 269fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// It is also almost always profitable to thread backedges from within the loop 270fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// to exit blocks, and is often profitable to thread backedges to other blocks 271fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// within the loop (forming a nested loop). This simple analysis is not rich 272fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// enough to track all of these properties and keep it up-to-date as the CFG 273fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// mutates, so we don't allow any of these transformations. 274fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// 275fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stumpvoid JumpThreading::FindLoopHeaders(Function &F) { 276fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges; 277fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump FindFunctionBackedges(F, Edges); 2786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 279fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump for (unsigned i = 0, e = Edges.size(); i != e; ++i) 280fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second)); 281fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump} 282fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 283ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// getKnownConstant - Helper method to determine if we can thread over a 284ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// terminator with the given value as its condition, and if so what value to 2856033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// use for that. What kind of value this is depends on whether we want an 2866033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// integer or a block address, but an undef is always accepted. 287ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// Returns null if Val is null or not an appropriate constant. 2886033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommelstatic Constant *getKnownConstant(Value *Val, ConstantPreference Preference) { 289ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (!Val) 290ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return 0; 291ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 292ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // Undef is "known" enough. 293ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (UndefValue *U = dyn_cast<UndefValue>(Val)) 294ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return U; 295ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 2966033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Preference == WantBlockAddress) 2976033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel return dyn_cast<BlockAddress>(Val->stripPointerCasts()); 298ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 2996033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel return dyn_cast<ConstantInt>(Val); 3000eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson} 3010eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson 3025729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see 3036033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// if we can infer that the value is a known ConstantInt/BlockAddress or undef 3046033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// in any of our predecessors. If so, return the known list of value and pred 3056033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// BB in the result vector. 3065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// 3075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// This returns true if there were any known values. 3085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// 3095729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattnerbool JumpThreading:: 3106033b346e20d6932cc62c754cf23ae51786724d6Frits van BommelComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result, 3116033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference) { 3129ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // This method walks up use-def chains recursively. Because of this, we could 3139ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // get into an infinite loop going around loops in the use-def chain. To 3149ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // prevent this, keep track of what (value, block) pairs we've already visited 3159ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // and terminate the search if we loop back to them 316cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson if (!RecursionSet.insert(std::make_pair(V, BB)).second) 317cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson return false; 3186f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3199ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // An RAII help to remove this pair from the recursion set once the recursion 3209ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // stack pops back out again. 3219ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB)); 3226f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 323ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // If V is a constant, then it is known in all predecessors. 3246033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(V, Preference)) { 325cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 326ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, *PI)); 3276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3285729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return true; 3295729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 3306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If V is a non-instruction value, or an instruction in a different block, 3325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // then it can't be derived from a PHI. 3335729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Instruction *I = dyn_cast<Instruction>(V); 334cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner if (I == 0 || I->getParent() != BB) { 3356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 336cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // Okay, if this is a live-in value, see if it has a known value at the end 337cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // of any of our predecessors. 338cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // 339cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // FIXME: This should be an edge property, not a block end property. 340cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner /// TODO: Per PR2563, we could infer value range information about a 341cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner /// predecessor based on its terminator. 342cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // 343c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // FIXME: change this to use the more-rich 'getPredicateOnEdge' method if 344c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // "I" is a non-local compare-with-a-constant instruction. This would be 345c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // able to handle value inequalities better, for example if the compare is 346c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // "X < 4" and "X < 3" is known true but "X < 4" itself is not available. 347c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // Perhaps getConstantOnEdge should be smart enough to do this? 3486f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 349c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 350c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson BasicBlock *P = *PI; 351c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // If the value is known by LazyValueInfo to be a constant in a 352c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // predecessor, use that information to try to thread this block. 353c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson Constant *PredCst = LVI->getConstantOnEdge(V, P, BB); 3546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(PredCst, Preference)) 355ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, P)); 356cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 3576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 358c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson return !Result.empty(); 359cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 3606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner /// If I is a PHI node, then we know the incoming values for any constants. 3625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PHINode *PN = dyn_cast<PHINode>(I)) { 3635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 3645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *InVal = PN->getIncomingValue(i); 3656033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(InVal, Preference)) { 366ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i))); 367c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson } else { 36862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *CI = LVI->getConstantOnEdge(InVal, 36962efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson PN->getIncomingBlock(i), BB); 3706033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(CI, Preference)) 3716033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i))); 3725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 3735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 3746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 3765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 3776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 378ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals, RHSVals; 3795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 3805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle some boolean conditions. 3816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel if (I->getType()->getPrimitiveSizeInBits() == 1) { 3826033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference == WantInteger && "One-bit non-integer type?"); 3835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // X | true -> true 3845729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // X & false -> false 3855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (I->getOpcode() == Instruction::Or || 3865729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner I->getOpcode() == Instruction::And) { 3876033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals, 3886033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger); 3896033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals, 3906033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger); 3916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3929ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson if (LHSVals.empty() && RHSVals.empty()) 3935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return false; 3946f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ConstantInt *InterestingVal; 3965729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (I->getOpcode() == Instruction::Or) 3975729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner InterestingVal = ConstantInt::getTrue(I->getContext()); 3985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else 3995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner InterestingVal = ConstantInt::getFalse(I->getContext()); 4006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4012fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner SmallPtrSet<BasicBlock*, 4> LHSKnownBBs; 4026f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4031e452650c6044735b6aa922d41736bda5721adccChris Lattner // Scan for the sentinel. If we find an undef, force it to the 4041e452650c6044735b6aa922d41736bda5721adccChris Lattner // interesting value: x|undef -> true and x&undef -> false. 4055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) 406ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (LHSVals[i].first == InterestingVal || 407ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel isa<UndefValue>(LHSVals[i].first)) { 4085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Result.push_back(LHSVals[i]); 4091e452650c6044735b6aa922d41736bda5721adccChris Lattner Result.back().first = InterestingVal; 4102fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner LHSKnownBBs.insert(LHSVals[i].second); 4111e452650c6044735b6aa922d41736bda5721adccChris Lattner } 4125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = RHSVals.size(); i != e; ++i) 413ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (RHSVals[i].first == InterestingVal || 414ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel isa<UndefValue>(RHSVals[i].first)) { 4150a96144aac449114330a5264b649eb449dc19a37Chris Lattner // If we already inferred a value for this block on the LHS, don't 4160a96144aac449114330a5264b649eb449dc19a37Chris Lattner // re-add it. 4172fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner if (!LHSKnownBBs.count(RHSVals[i].second)) { 4180a96144aac449114330a5264b649eb449dc19a37Chris Lattner Result.push_back(RHSVals[i]); 4190a96144aac449114330a5264b649eb449dc19a37Chris Lattner Result.back().first = InterestingVal; 4200a96144aac449114330a5264b649eb449dc19a37Chris Lattner } 4211e452650c6044735b6aa922d41736bda5721adccChris Lattner } 4226f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 4245729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 426055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner // Handle the NOT form of XOR. 427055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner if (I->getOpcode() == Instruction::Xor && 428055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner isa<ConstantInt>(I->getOperand(1)) && 429055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner cast<ConstantInt>(I->getOperand(1))->isOne()) { 4306033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result, 4316033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger); 4329ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson if (Result.empty()) 433055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner return false; 434055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner 435055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner // Invert the known values. 436055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner for (unsigned i = 0, e = Result.size(); i != e; ++i) 437ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result[i].first = ConstantExpr::getNot(Result[i].first); 4386f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 439055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner return true; 440055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner } 4416f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 44262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // Try to simplify some other binary operator values. 44362efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { 4446033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference != WantBlockAddress 4456033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel && "A binary operator creating a block address?"); 4460eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { 447ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals; 4486033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals, 4496033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger); 4506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 451cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson // Try to use constant folding to simplify the binary operator. 452cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { 453906a675db8af7bacdd78708453fc7f7558e64033Chris Lattner Constant *V = LHSVals[i].first; 4540eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI); 4556f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4566033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Folded, WantInteger)) 4576033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, LHSVals[i].second)); 458cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson } 45962efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 4606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 461cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson return !Result.empty(); 4625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4636f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle compare with phi operand, where the PHI is defined in this block. 4655729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) { 4666033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference == WantInteger && "Compares only produce integers"); 4675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PHINode *PN = dyn_cast<PHINode>(Cmp->getOperand(0)); 4685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PN && PN->getParent() == BB) { 4695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // We can do this simplification if any comparisons fold to true or false. 4705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // See if any do. 4715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 4725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB = PN->getIncomingBlock(i); 4735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *LHS = PN->getIncomingValue(i); 4745729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *RHS = Cmp->getOperand(1)->DoPHITranslation(BB, PredBB); 4756f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4762ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner Value *Res = SimplifyCmpInst(Cmp->getPredicate(), LHS, RHS, TD); 47766c04c48debfd4357beaf9310346b2b24046b685Chris Lattner if (Res == 0) { 478c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (!isa<Constant>(RHS)) 47966c04c48debfd4357beaf9310346b2b24046b685Chris Lattner continue; 4806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel LazyValueInfo::Tristate 48266c04c48debfd4357beaf9310346b2b24046b685Chris Lattner ResT = LVI->getPredicateOnEdge(Cmp->getPredicate(), LHS, 48366c04c48debfd4357beaf9310346b2b24046b685Chris Lattner cast<Constant>(RHS), PredBB, BB); 48466c04c48debfd4357beaf9310346b2b24046b685Chris Lattner if (ResT == LazyValueInfo::Unknown) 48566c04c48debfd4357beaf9310346b2b24046b685Chris Lattner continue; 48666c04c48debfd4357beaf9310346b2b24046b685Chris Lattner Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT); 48766c04c48debfd4357beaf9310346b2b24046b685Chris Lattner } 4886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4896033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Res, WantInteger)) 4906033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, PredBB)); 4915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4926f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 4945729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4956f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4972ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner // If comparing a live-in value against a constant, see if we know the 4982ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner // live-in value on any predecessors. 499c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (isa<Constant>(Cmp->getOperand(1)) && Cmp->getType()->isIntegerTy()) { 50062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson if (!isa<Instruction>(Cmp->getOperand(0)) || 501327ca7bec274e25c05a0a4ae5b51a8a2062012c7Owen Anderson cast<Instruction>(Cmp->getOperand(0))->getParent() != BB) { 50262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *RHSCst = cast<Constant>(Cmp->getOperand(1)); 503ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif 50462efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);PI != E; ++PI){ 50562efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson BasicBlock *P = *PI; 50662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // If the value is known by LazyValueInfo to be a constant in a 50762efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // predecessor, use that information to try to thread this block. 50862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson LazyValueInfo::Tristate Res = 50962efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson LVI->getPredicateOnEdge(Cmp->getPredicate(), Cmp->getOperand(0), 51062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson RHSCst, P, BB); 51162efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson if (Res == LazyValueInfo::Unknown) 51262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson continue; 5130e0ff29271df58e3bc545e40f5432c436e8bd76bChris Lattner 51462efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *ResC = ConstantInt::get(Cmp->getType(), Res); 515ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(ResC, P)); 51662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 517ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif 51862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson return !Result.empty(); 51962efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 5206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 521cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson // Try to find a constant value for the LHS of a comparison, 52262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // and evaluate it statically if we can. 523327ca7bec274e25c05a0a4ae5b51a8a2062012c7Owen Anderson if (Constant *CmpConst = dyn_cast<Constant>(Cmp->getOperand(1))) { 524ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals; 5256033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals, 5266033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger); 5276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 52862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { 529906a675db8af7bacdd78708453fc7f7558e64033Chris Lattner Constant *V = LHSVals[i].first; 5300eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(), 5310eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson V, CmpConst); 5326033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Folded, WantInteger)) 5336033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, LHSVals[i].second)); 53462efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 5356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 53662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson return !Result.empty(); 53762efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 5382ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner } 5395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 5406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 54126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (SelectInst *SI = dyn_cast<SelectInst>(I)) { 54226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Handle select instructions where at least one operand is a known constant 54326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // and we can figure out the condition value for any predecessor block. 54426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *TrueVal = getKnownConstant(SI->getTrueValue(), Preference); 54526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference); 54626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel PredValueInfoTy Conds; 54726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if ((TrueVal || FalseVal) && 54826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel ComputeValueKnownInPredecessors(SI->getCondition(), BB, Conds, 54926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel WantInteger)) { 55026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel for (unsigned i = 0, e = Conds.size(); i != e; ++i) { 55126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *Cond = Conds[i].first; 55226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 55326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Figure out what value to use for the condition. 55426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel bool KnownCond; 55526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) { 55626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // A known boolean. 55726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel KnownCond = CI->isOne(); 55826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } else { 55926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel assert(isa<UndefValue>(Cond) && "Unexpected condition value"); 56026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Either operand will do, so be sure to pick the one that's a known 56126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // constant. 56226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // FIXME: Do this more cleverly if both values are known constants? 56326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel KnownCond = (TrueVal != 0); 56426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 56526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 56626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // See if the select has a known constant value for this predecessor. 56726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (Constant *Val = KnownCond ? TrueVal : FalseVal) 56826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Result.push_back(std::make_pair(Val, Conds[i].second)); 56926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 57026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 57126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel return !Result.empty(); 57226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 57326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 57426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 575c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // If all else fails, see if LVI can figure out a constant value for us. 576c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson Constant *CI = LVI->getConstant(V, BB); 5776033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(CI, Preference)) { 578c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 579ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, *PI)); 58062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 5816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 582c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson return !Result.empty(); 5835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 5845729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 5855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 5866bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner 587e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// GetBestDestForBranchOnUndef - If we determine that the specified block ends 588e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// in an undefined jump, decide which block is best to revector to. 589e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// 590e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// Since we can pick an arbitrary destination, we pick the successor with the 591e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// fewest predecessors. This should reduce the in-degree of the others. 592e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// 593e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattnerstatic unsigned GetBestDestForJumpOnUndef(BasicBlock *BB) { 594e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TerminatorInst *BBTerm = BB->getTerminator(); 595e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned MinSucc = 0; 596e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc); 597e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner // Compute the successor with the minimum number of predecessors. 598e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned MinNumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); 599e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) { 600e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TestBB = BBTerm->getSuccessor(i); 601e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned NumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); 602e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner if (NumPreds < MinNumPreds) 603e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner MinSucc = i; 604e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner } 6056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 606e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner return MinSucc; 607e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner} 608e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner 609c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner/// ProcessBlock - If there are any predecessors whose control can be threaded 610177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now. 611c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattnerbool JumpThreading::ProcessBlock(BasicBlock *BB) { 6128231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // If the block is trivially dead, just return and let the caller nuke it. 6138231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // This simplifies other transformations. 6148231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner if (pred_begin(BB) == pred_end(BB) && 6158231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner BB != &BB->getParent()->getEntryBlock()) 6168231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner return false; 6176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 61869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If this block has a single predecessor, and if that pred has a single 61969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // successor, merge the blocks. This encourages recursive jump threading 62069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // because now the condition in this block can be threaded through 62169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors of our predecessor block. 6225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BasicBlock *SinglePred = BB->getSinglePredecessor()) { 623f5102a0f088e7c96f7028bf7ca1c24975c314fffChris Lattner if (SinglePred->getTerminator()->getNumSuccessors() == 1 && 624f5102a0f088e7c96f7028bf7ca1c24975c314fffChris Lattner SinglePred != BB) { 625fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If SinglePred was a loop header, BB becomes one. 626fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.erase(SinglePred)) 627fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.insert(BB); 6286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 6293d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner // Remember if SinglePred was the entry block of the function. If so, we 6303d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner // will need to move BB back to the entry position. 6313d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); 632c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->eraseBlock(SinglePred); 63369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner MergeBasicBlockIntoOnlyPred(BB); 6346f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 6353d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner if (isEntry && BB != &BB->getParent()->getEntryBlock()) 6363d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner BB->moveBefore(&BB->getParent()->getEntryBlock()); 63769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 63869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 6395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 6405729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 6416033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // What kind of constant we're looking for. 6426033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference = WantInteger; 6436033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 6446033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // Look to see if the terminator is a conditional branch, switch or indirect 6456033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // branch, if not we can't thread it. 646177480b7ede0441135572d641a2497df25a7d95fChris Lattner Value *Condition; 6476033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Instruction *Terminator = BB->getTerminator(); 6486033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) { 649bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Can't thread an unconditional jump. 650bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (BI->isUnconditional()) return false; 651177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = BI->getCondition(); 6526033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) { 653177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = SI->getCondition(); 6546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) { 6556033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Condition = IB->getAddress()->stripPointerCasts(); 6566033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Preference = WantBlockAddress; 6576033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else { 658177480b7ede0441135572d641a2497df25a7d95fChris Lattner return false; // Must be an invoke. 6596033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } 6606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 661421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // If the terminator is branching on an undef, we can pick any of the 662e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner // successors to branch to. Let GetBestDestForJumpOnUndef decide. 663421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner if (isa<UndefValue>(Condition)) { 664e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned BestSucc = GetBestDestForJumpOnUndef(BB); 6656f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 666421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // Fold the branch/switch. 667e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TerminatorInst *BBTerm = BB->getTerminator(); 668421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) { 669e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner if (i == BestSucc) continue; 67036c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BBTerm->getSuccessor(i)->removePredecessor(BB, true); 671421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 6726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 673fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " In block '" << BB->getName() 67478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' folding undef terminator: " << *BBTerm << '\n'); 675e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm); 676421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner BBTerm->eraseFromParent(); 677421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return true; 678421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 6796f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 680ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // If the terminator of this block is branching on a constant, simplify the 681ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // terminator to an unconditional branch. This can occur due to threading in 682ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // other blocks. 6836033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (getKnownConstant(Condition, Preference)) { 684ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DEBUG(dbgs() << " In block '" << BB->getName() 685ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << "' folding terminator: " << *BB->getTerminator() << '\n'); 686ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel ++NumFolds; 687ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel ConstantFoldTerminator(BB); 688ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return true; 689ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel } 690ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 691421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Instruction *CondInst = dyn_cast<Instruction>(Condition); 692421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner 693421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // All the rest of our checks depend on the condition being an instruction. 69487e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner if (CondInst == 0) { 69587e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner // FIXME: Unify this with code below. 6966033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (ProcessThreadableEdges(Condition, BB, Preference)) 69787e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner return true; 698421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return false; 6996f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 7006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7016f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7029683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) { 703660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // For a comparison where the LHS is outside this block, it's possible 704fc2fb17fb764470626f3d7ecf2fb68fe73b0d757Owen Anderson // that we've branched on it before. Used LVI to see if we can simplify 705660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // the branch based on that. 706660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator()); 707660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1)); 708c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 709c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (CondBr && CondConst && CondBr->isConditional() && PI != PE && 710660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson (!isa<Instruction>(CondCmp->getOperand(0)) || 711660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson cast<Instruction>(CondCmp->getOperand(0))->getParent() != BB)) { 712660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // For predecessor edge, determine if the comparison is true or false 713660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // on that edge. If they're all true or all false, we can simplify the 714660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // branch. 715660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // FIXME: We could handle mixed true/false by duplicating code. 7166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel LazyValueInfo::Tristate Baseline = 717c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson LVI->getPredicateOnEdge(CondCmp->getPredicate(), CondCmp->getOperand(0), 718c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson CondConst, *PI, BB); 719c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (Baseline != LazyValueInfo::Unknown) { 720c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson // Check that all remaining incoming values match the first one. 721c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson while (++PI != PE) { 722bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner LazyValueInfo::Tristate Ret = 723bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner LVI->getPredicateOnEdge(CondCmp->getPredicate(), 724bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner CondCmp->getOperand(0), CondConst, *PI, BB); 725c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (Ret != Baseline) break; 726c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson } 7276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 728c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson // If we terminated early, then one of the values didn't match. 729c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (PI == PE) { 730c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson unsigned ToRemove = Baseline == LazyValueInfo::True ? 1 : 0; 731c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson unsigned ToKeep = Baseline == LazyValueInfo::True ? 0 : 1; 73236c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true); 733c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); 734c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson CondBr->eraseFromParent(); 735c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson return true; 736c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson } 737660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson } 738660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson } 7399683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky } 74069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 74169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Check for some cases that are worth simplifying. Right now we want to look 74269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // for loads that are used by a switch or by the condition for the branch. If 74369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we see one, check to see if it's partially redundant. If so, insert a PHI 74469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // which can then be used to thread the values. 74569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // 746421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Value *SimplifyValue = CondInst; 74769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue)) 74869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (isa<Constant>(CondCmp->getOperand(1))) 74969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SimplifyValue = CondCmp->getOperand(0); 7506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7514e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: There are other places where load PRE would be profitable, such as 7524e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // more complex comparisons. 75369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue)) 75469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (SimplifyPartiallyRedundantLoad(LI)) 75569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 7566f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle a variety of cases where we are branching on something derived from 7595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a PHI node in the current block. If we can prove that any predecessors 7605729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // compute a predictable value based on a PHI node, thread those predecessors. 7615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // 7626033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (ProcessThreadableEdges(CondInst, BB, Preference)) 763cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner return true; 7646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 76577beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // If this is an otherwise-unfoldable branch on a phi node in the current 76677beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // block, see if we can simplify. 76777beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PHINode *PN = dyn_cast<PHINode>(CondInst)) 76877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 76977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner return ProcessBranchOnPHI(PN); 7706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7722249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify. 7732249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (CondInst->getOpcode() == Instruction::Xor && 7742249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 7752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst)); 7766f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 77869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // TODO: If we have: "br (X > 0)" and we have a predecessor where we know 77977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // "(X == 4)", thread through this block. 7806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 781d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner return false; 782d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner} 783d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner 7843cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner 78569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant 78669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// load instruction, eliminate it by replacing it with a PHI node. This is an 78769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// important optimization that encourages jump threading, and needs to be run 78869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// interlaced with other jump threading tasks. 78969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattnerbool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) { 79069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Don't hack volatile loads. 79169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LI->isVolatile()) return false; 7926f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 79369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the load is defined in a block with exactly one predecessor, it can't be 79469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // partially redundant. 79569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *LoadBB = LI->getParent(); 79669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadBB->getSinglePredecessor()) 79769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 7986f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 79969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner Value *LoadedPtr = LI->getOperand(0); 80069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 80169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded operand is defined in the LoadBB, it can't be available. 8024e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: Could do simple PHI translation, that would be fun :) 80369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr)) 80469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PtrOp->getParent() == LoadBB) 80569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 8066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 80769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan a few instructions up from the load, to see if it is obviously live at 80869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // the entry to its block. 80969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock::iterator BBIt = LI; 81069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 8116f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel if (Value *AvailableVal = 8124e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner FindAvailableLoadedValue(LoadedPtr, LoadBB, BBIt, 6)) { 81369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the value if the load is locally available within the block, just use 81469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it. This frequently occurs for reg2mem'd allocas. 81569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n"; 8166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8172a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // If the returned value is the load itself, replace with an undef. This can 8182a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // only happen in dead loops. 8199e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson if (AvailableVal == LI) AvailableVal = UndefValue::get(LI->getType()); 82069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(AvailableVal); 82169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 82269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 82369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 82469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 82569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, if we scanned the whole block and got to the top of the block, 82669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we know the block is locally transparent to the load. If not, something 82769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // might clobber its value. 82869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (BBIt != LoadBB->begin()) 82969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 8306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8316f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 83269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> PredsScanned; 83369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy; 83469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredsTy AvailablePreds; 83569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *OneUnavailablePred = 0; 8366f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 83769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we got here, the loaded value is transparent through to the start of the 83869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // block. Check to see if it is available in any of the predecessor blocks. 83969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 84069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PI != PE; ++PI) { 84169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *PredBB = *PI; 84269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 84369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we already scanned this predecessor, skip it. 84469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (!PredsScanned.insert(PredBB)) 84569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 84669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 84769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan the predecessor to see if the value is available in the pred. 84869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BBIt = PredBB->end(); 84952c95856b4a40ccae6c4b0e13b2a04101e1f79c9Chris Lattner Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6); 85069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (!PredAvailable) { 85169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred = PredBB; 85269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 85369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 8546f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 85569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If so, this load is partially redundant. Remember this info so that we 85669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // can create a PHI node. 85769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable)); 85869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 8596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 86069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded value isn't available in any predecessor, it isn't partially 86169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // redundant. 86269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (AvailablePreds.empty()) return false; 8636f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 86469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Okay, the loaded value is available in at least one (and maybe all!) 86569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors. If the value is unavailable in more than one unique 86669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessor, we want to insert a merge block for those common predecessors. 86769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // This ensures that we only have to insert one reload, thus not increasing 86869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // code size. 86969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *UnavailablePred = 0; 8706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 87169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If there is exactly one predecessor where the value is unavailable, the 87269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // already computed 'OneUnavailablePred' block is it. If it ends in an 87369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // unconditional branch, we know that it isn't a critical edge. 87469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PredsScanned.size() == AvailablePreds.size()+1 && 87569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) { 87669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred = OneUnavailablePred; 87769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } else if (PredsScanned.size() != AvailablePreds.size()) { 87869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, we had multiple unavailable predecessors or we had a critical 87969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // edge from the one. 88069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallVector<BasicBlock*, 8> PredsToSplit; 88169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> AvailablePredSet; 88269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 88369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i) 88469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredSet.insert(AvailablePreds[i].first); 88569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 88669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Add all the unavailable predecessors to the PredsToSplit list. 88769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 888e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner PI != PE; ++PI) { 889ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 890e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner // If the predecessor is an indirect goto, we can't split the edge. 891ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (isa<IndirectBrInst>(P->getTerminator())) 892e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner return false; 8936f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 894ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (!AvailablePredSet.count(P)) 895ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif PredsToSplit.push_back(P); 896e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner } 8976f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 89869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Split them out to their own block. 89969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred = 90069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SplitBlockPredecessors(LoadBB, &PredsToSplit[0], PredsToSplit.size(), 9014e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner "thread-pre-split", this); 90269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9036f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 90469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the value isn't available in all predecessors, then there will be 90569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // exactly one where it isn't available. Insert a load on that edge and add 90669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it to the AvailablePreds list. 90769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (UnavailablePred) { 90869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 && 90969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Can't handle critical edge here!"); 9104e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner Value *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr", false, 9114e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner LI->getAlignment(), 91269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred->getTerminator()); 91369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal)); 91469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 91669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Now we know that each predecessor of this block has a value in 91769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // AvailablePreds, sort them for efficient access as we're walking the preds. 918a3522000ab9c821f48856d0c25ada8297c1c2914Chris Lattner array_pod_sort(AvailablePreds.begin(), AvailablePreds.end()); 9196f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 92069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Create a PHI node at the start of the block for the PRE'd load value. 92169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PHINode *PN = PHINode::Create(LI->getType(), "", LoadBB->begin()); 92269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PN->takeName(LI); 9236f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 92469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Insert new entries into the PHI for each predecessor. A single block may 92569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // have multiple entries here. 92669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); PI != E; 92769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner ++PI) { 928ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 9296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel AvailablePredsTy::iterator I = 93069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(), 931ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif std::make_pair(P, (Value*)0)); 9326f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 933ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif assert(I != AvailablePreds.end() && I->first == P && 93469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Didn't find entry for predecessor!"); 9356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 93669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PN->addIncoming(I->second, I->first); 93769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9386f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 93969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "PRE: " << *LI << *PN << "\n"; 9406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 94169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(PN); 94269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 9436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 94469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 94569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner} 94669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 9475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// FindMostPopularDest - The specified list contains multiple possible 9485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// threadable destinations. Pick the one that occurs the most frequently in 9495729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// the list. 9505729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattnerstatic BasicBlock * 9515729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris LattnerFindMostPopularDest(BasicBlock *BB, 9525729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner const SmallVectorImpl<std::pair<BasicBlock*, 9535729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock*> > &PredToDestList) { 9545729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredToDestList.empty()); 9556f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine popularity. If there are multiple possible destinations, we 9575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // explicitly choose to ignore 'undef' destinations. We prefer to thread 9585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // blocks with known and real destinations to threading undef. We'll handle 9595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // them later if interesting. 9605729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned> DestPopularity; 9615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 9625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second) 9635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DestPopularity[PredToDestList[i].second]++; 9646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9655729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Find the most popular dest. 9665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned>::iterator DPI = DestPopularity.begin(); 9675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = DPI->first; 9685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner unsigned Popularity = DPI->second; 9695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 4> SamePopularity; 9706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (++DPI; DPI != DestPopularity.end(); ++DPI) { 9725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the popularity of this entry isn't higher than the popularity we've 9735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // seen so far, ignore it. 9745729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (DPI->second < Popularity) 9755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ; // ignore. 9765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (DPI->second == Popularity) { 9775729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is the same as what we've seen so far, keep track of it. 9785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(DPI->first); 9795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } else { 9805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is more popular, remember it. 9815729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.clear(); 9825729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = DPI->first; 9835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Popularity = DPI->second; 9846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 98578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 9866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 98701abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel // Okay, now we know the most popular destination. If there is more than one 9885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination, we need to determine one. This is arbitrary, but we need 9895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to make a deterministic decision. Pick the first one that appears in the 9905729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // successor list. 9915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (!SamePopularity.empty()) { 9925729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(MostPopularDest); 9935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *TI = BB->getTerminator(); 9945729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0; ; ++i) { 9955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(i != TI->getNumSuccessors() && "Didn't find any successor!"); 9966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9975729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (std::find(SamePopularity.begin(), SamePopularity.end(), 9985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TI->getSuccessor(i)) == SamePopularity.end()) 9995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; 10006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10015729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = TI->getSuccessor(i); 100212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel break; 100312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 100412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Okay, we have finally picked the most popular destination. 10075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return MostPopularDest; 10085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 10095729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 10106033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommelbool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, 10116033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference) { 10125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If threading this would thread across a loop header, don't even try to 10135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // thread the edge. 10145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (LoopHeaders.count(BB)) 101512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1017ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy PredValues; 10186033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference)) 101912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10215729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredValues.empty() && 10225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 10235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 1024fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "IN BB: " << *BB; 10255729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 1026ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel dbgs() << " BB '" << BB->getName() << "': FOUND condition = " 1027ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << *PredValues[i].first 1028ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << " for pred '" << PredValues[i].second->getName() << "'.\n"; 10295729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner }); 10306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Decide what we want to thread through. Convert our list of known values to 10325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a list of known destinations for each pred. This also discards duplicate 10335729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors and keeps track of the undefined inputs (which are represented 1034e7e63fe9650fc01043c96e2bf8a1ecc19e49c5b7Chris Lattner // as a null dest in the PredToDestList). 10355729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallPtrSet<BasicBlock*, 16> SeenPreds; 10365729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList; 10376f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10385729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *OnlyDest = 0; 10395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL; 10406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 10425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredValues[i].second; 10435729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (!SeenPreds.insert(Pred)) 10445729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; // Duplicate predecessor entry. 10456f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10465729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the predecessor ends with an indirect goto, we can't change its 10475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination. 10485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (isa<IndirectBrInst>(Pred->getTerminator())) 104912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel continue; 10506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1051ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Constant *Val = PredValues[i].first; 10526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10535729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *DestBB; 1054ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(Val)) 10555729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DestBB = 0; 10565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) 1057ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero()); 10586033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) 1059ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DestBB = SI->getSuccessor(SI->findCaseValue(cast<ConstantInt>(Val))); 10606033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel else { 10616033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(isa<IndirectBrInst>(BB->getTerminator()) 10626033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel && "Unexpected terminator"); 10636033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel DestBB = cast<BlockAddress>(Val)->getBasicBlock(); 106412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10655729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 10665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If we have exactly one destination, remember it for efficiency below. 106701abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel if (PredToDestList.empty()) 10685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = DestBB; 10695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (OnlyDest != DestBB) 10705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = MultipleDestSentinel; 10716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredToDestList.push_back(std::make_pair(Pred, DestBB)); 107312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If all edges were unthreadable, we fail. 10765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList.empty()) 107712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine which is the most common successor. If we have many inputs and 10805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // this block is a switch, we want to start by threading the batch that goes 10815729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to the most popular destination first. If we only know about one 10825729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // threadable destination (the common case) we can avoid this. 10835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = OnlyDest; 10846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (MostPopularDest == MultipleDestSentinel) 10865729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = FindMostPopularDest(BB, PredToDestList); 10876f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Now that we know what the most popular destination is, factor all 10895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors that will jump to it into a single predecessor. 10905729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 16> PredsToFactor; 10915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 10925729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second == MostPopularDest) { 10935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredToDestList[i].first; 10946f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // This predecessor may be a switch or something else that has multiple 10965729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // edges to the block. Factor each of these edges by listing them 10975729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // according to # occurrences in PredsToFactor. 10985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *PredTI = Pred->getTerminator(); 10995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredTI->getNumSuccessors(); i != e; ++i) 11005729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredTI->getSuccessor(i) == BB) 11015729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredsToFactor.push_back(Pred); 11025729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 11035729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 11045729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the threadable edges are branching on an undefined value, we get to pick 11055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // the destination that these predecessors should get to. 11065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (MostPopularDest == 0) 11075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = BB->getTerminator()-> 11085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner getSuccessor(GetBestDestForJumpOnUndef(BB)); 11096f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 111012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel // Ok, try to thread it! 11115729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return ThreadEdge(BB, PredsToFactor, MostPopularDest); 11125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 11135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 111477beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on 111577beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// a PHI node in the current block. See if there are any simplifications we 111677beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// can do based on inputs to the phi node. 11176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 111877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattnerbool JumpThreading::ProcessBranchOnPHI(PHINode *PN) { 11195729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *BB = PN->getParent(); 11206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11212249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // TODO: We could make use of this to do it once for blocks with common PHI 11222249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // values. 11232249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 1> PredBBs; 11242249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs.resize(1); 11256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11265729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If any of the predecessor blocks end in an unconditional branch, we can 112777beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // *duplicate* the conditional branch into that block in order to further 112877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // encourage jump threading and to eliminate cases where we have branch on a 112977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // phi of an icmp (branch on icmp is much better). 11305729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 11315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB = PN->getIncomingBlock(i); 11325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator())) 11332249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBr->isUnconditional()) { 11342249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs[0] = PredBB; 11352249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 11362249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs)) 11372249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return true; 11382249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 11395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 11405729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 11415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return false; 114212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel} 114312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel 11442249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on 11452249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// a xor instruction in the current block. See if there are any 11462249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// simplifications we can do based on inputs to the xor. 11476f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 11482249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattnerbool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) { 11492249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *BB = BO->getParent(); 11506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11512249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If either the LHS or RHS of the xor is a constant, don't do this 11522249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // optimization. 11532249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (isa<ConstantInt>(BO->getOperand(0)) || 11542249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isa<ConstantInt>(BO->getOperand(1))) 11552249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 11566f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11572dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If the first instruction in BB isn't a phi, we won't be able to infer 11582dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // anything special about any particular predecessor. 11592dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (!isa<PHINode>(BB->front())) 11602dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return false; 11616f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11622249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If we have a xor as the branch input to this block, and we know that the 11632249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // LHS or RHS of the xor in any predecessor is true/false, then we can clone 11642249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // the condition into the predecessor and fix that value to true, saving some 11652249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // logical ops on that path and encouraging other paths to simplify. 11662249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11672249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // This copies something like this: 11682249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11692249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB: 11702249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %X = phi i1 [1], [%X'] 11712249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp eq i32 %A, %B 11722249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Z = xor i1 %X, %Y 11732249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // br i1 %Z, ... 11742249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Into: 11762249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB': 11772249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp ne i32 %A, %B 11782249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // br i1 %Z, ... 11792249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 1180ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy XorOpValues; 11812249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner bool isLHS = true; 11826033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues, 11836033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger)) { 11842249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(XorOpValues.empty()); 11856033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues, 11866033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger)) 11872249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 11882249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isLHS = false; 11892249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 11906f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11912249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!XorOpValues.empty() && 11922249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 11932249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 11942249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Scan the information to see which is most popular: true or false. The 11952249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // predecessors can be of the set true, false, or undef. 11962249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner unsigned NumTrue = 0, NumFalse = 0; 11972249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1198ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(XorOpValues[i].first)) 1199ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // Ignore undefs for the count. 1200ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 1201ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (cast<ConstantInt>(XorOpValues[i].first)->isZero()) 12022249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumFalse; 12032249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else 12042249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumTrue; 12052249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 12066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12072249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Determine which value to split on, true, false, or undef if neither. 12082249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ConstantInt *SplitVal = 0; 12092249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (NumTrue > NumFalse) 12102249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getTrue(BB->getContext()); 12112249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else if (NumTrue != 0 || NumFalse != 0) 12122249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getFalse(BB->getContext()); 12136f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12142249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Collect all of the blocks that this can be folded into so that we can 12152249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // factor this once and clone it once. 12162249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 8> BlocksToFoldInto; 12172249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1218ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (XorOpValues[i].first != SplitVal && 1219ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel !isa<UndefValue>(XorOpValues[i].first)) 1220ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 12212249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 12222249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BlocksToFoldInto.push_back(XorOpValues[i].second); 12232249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 12246f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12252dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If we inferred a value for all of the predecessors, then duplication won't 12262dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // help us. However, we can just replace the LHS or RHS with the constant. 12272dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (BlocksToFoldInto.size() == 12282dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner cast<PHINode>(BB->front()).getNumIncomingValues()) { 12292dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (SplitVal == 0) { 12302dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide undef, just nuke the xor, because it is undef too. 12312dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(UndefValue::get(BO->getType())); 12322dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 12332dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else if (SplitVal->isZero()) { 12342dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 0, replace the xor with the other input. 12352dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(BO->getOperand(isLHS)); 12362dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 12372dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else { 12382dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 1, set the computed value to 1. 12392dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->setOperand(!isLHS, SplitVal); 12402dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 12416f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12422dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return true; 12432dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 12446f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12452249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 1246797c440caf24e92b16b9a87a39ef2f5c4cfb60f0Chris Lattner return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto); 12472249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner} 12482249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 12492249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 125078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new 125178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// predecessor to the PHIBB block. If it has PHI nodes, add entries for 125278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// NewPred using the entries from OldPred (suitably mapped). 125378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, 125478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *OldPred, 125578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *NewPred, 125678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> &ValueMap) { 125778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator PNI = PHIBB->begin(); 125878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) { 125978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Ok, we have a PHI node. Figure out what the incoming value was for the 126078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // DestBlock. 126178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Value *IV = PN->getIncomingValueForBlock(OldPred); 12626f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 126378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap the value if necessary. 126478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(IV)) { 126578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst); 126678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMap.end()) 126778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner IV = I->second; 1268bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 12696f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 127078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PN->addIncoming(IV, NewPred); 1271bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 1272fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump} 1273fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 12745729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// ThreadEdge - We have decided that it is safe and profitable to factor the 12755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB 12765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// across BB. Transform the IR to reflect this change. 12776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommelbool JumpThreading::ThreadEdge(BasicBlock *BB, 12786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel const SmallVectorImpl<BasicBlock*> &PredBBs, 1279bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner BasicBlock *SuccBB) { 1280eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner // If threading to the same block as we come from, we would infinite loop. 1281eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner if (SuccBB == BB) { 1282fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across BB '" << BB->getName() 128393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - would thread to self!\n"); 1284eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner return false; 1285eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner } 12866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1287fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If threading this would thread across a loop header, don't thread the edge. 1288fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // See the comments above FindLoopHeaders for justifications and caveats. 1289fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.count(BB)) { 1290fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across loop header BB '" << BB->getName() 129193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' to dest BB '" << SuccBB->getName() 129293b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - it might create an irreducible loop!\n"); 1293fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return false; 1294fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump } 1295fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 129678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); 129778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (JumpThreadCost > Threshold) { 1298fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading BB '" << BB->getName() 129978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << JumpThreadCost << "\n"); 130078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 130178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 13026f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13035729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // And finally, do it! Start by factoring the predecessors is needed. 13045729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB; 13055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredBBs.size() == 1) 13065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredBB = PredBBs[0]; 13075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else { 1308fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Factoring out " << PredBBs.size() 13095729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner << " common predecessors.\n"); 13105729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredBB = SplitBlockPredecessors(BB, &PredBBs[0], PredBBs.size(), 13115729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ".thr_comm", this); 13125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 13136f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1314a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner // And finally, do it! 1315fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Threading edge from '" << PredBB->getName() << "' to '" 1316460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar << SuccBB->getName() << "' with cost: " << JumpThreadCost 131793b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << ", across block:\n " 131893b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << *BB << "\n"); 13196f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1320c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->threadEdge(PredBB, BB, SuccBB); 13216f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1322bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We are going to have to map operands from the original BB block to the new 1323bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to 1324bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // account for entry from PredBB. 1325bd3401fa98b578080e227afce940bca80137dea6Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 13266f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), 13286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BB->getName()+".thread", 13291d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BB->getParent(), BB); 1330bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->moveAfter(PredBB); 13316f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1332bd3401fa98b578080e227afce940bca80137dea6Chris Lattner BasicBlock::iterator BI = BB->begin(); 1333bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 1334bd3401fa98b578080e227afce940bca80137dea6Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 13356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1336bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Clone the non-phi instructions of BB into NewBB, keeping track of the 1337bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // mapping and using it to remap operands in the cloned instructions. 1338bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; !isa<TerminatorInst>(BI); ++BI) { 13396776064d190701c5bae4d5403939eed2e480d1cdNick Lewycky Instruction *New = BI->clone(); 1340460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar New->setName(BI->getName()); 1341bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->getInstList().push_back(New); 1342bd3401fa98b578080e227afce940bca80137dea6Chris Lattner ValueMapping[BI] = New; 13436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1344bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Remap operands to patch up intra-block references. 1345bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 1346f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 1347f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 1348f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (I != ValueMapping.end()) 1349f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman New->setOperand(i, I->second); 1350f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman } 1351bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 13526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1353bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We didn't copy the terminator from BB over to NewBB, because there is now 1354bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // an unconditional jump to SuccBB. Insert the unconditional jump. 1355bd3401fa98b578080e227afce940bca80137dea6Chris Lattner BranchInst::Create(SuccBB, NewBB); 13566f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1357bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the 1358bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // PHI nodes for NewBB now. 135978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); 13606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1361433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there were values defined in BB that are used outside the block, then we 1362433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // now have to update all uses of the value to use either the original value, 1363433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 1364433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 1365433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdater SSAUpdate; 1366433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SmallVector<Use*, 16> UsesToRename; 1367433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 1368433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // Scan all uses of this instruction to see if it is used outside of its 1369433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // block, and if so, record them in UsesToRename. 1370433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 1371433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner ++UI) { 1372433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner Instruction *User = cast<Instruction>(*UI); 1373433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 1374433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (UserPN->getIncomingBlock(UI) == BB) 1375433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 1376433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } else if (User->getParent() == BB) 1377433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 13786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1379433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner UsesToRename.push_back(&UI.getUse()); 1380433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 13816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1382433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there are no uses outside the block, we're done with this instruction. 1383433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (UsesToRename.empty()) 1384433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 13856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1386fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 1387433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner 1388433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 1389433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 1390433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // with the two values we know. 1391fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 1392433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.AddAvailableValue(BB, I); 1393433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.AddAvailableValue(NewBB, ValueMapping[I]); 13946f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1395433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner while (!UsesToRename.empty()) 1396433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1397fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 1398433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 13996f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1401ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // Ok, NewBB is good to go. Update the terminator of PredBB to jump to 1402bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // NewBB instead of BB. This eliminates predecessors from BB, which requires 1403bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // us to simplify any PHI nodes in BB. 1404bd3401fa98b578080e227afce940bca80137dea6Chris Lattner TerminatorInst *PredTerm = PredBB->getTerminator(); 1405bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) 1406bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (PredTerm->getSuccessor(i) == BB) { 140736c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 1408bd3401fa98b578080e227afce940bca80137dea6Chris Lattner PredTerm->setSuccessor(i, NewBB); 1409bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 14106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1411ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // At this point, the IR is fully up to date and consistent. Do a quick scan 1412ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // over the new instructions and zap any that are constants or dead. This 1413ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // frequently happens because of phi translation. 1414972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner SimplifyInstructionsInBlock(NewBB, TD); 14156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1416fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // Threaded an edge! 1417fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump ++NumThreads; 1418fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return true; 1419177480b7ede0441135572d641a2497df25a7d95fChris Lattner} 142078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 142178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch 142278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// to BB which contains an i1 PHI node and a conditional branch on that PHI. 142378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// If we can duplicate the contents of BB up into PredBB do so now, this 142478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// improves the odds that the branch will be on an analyzable instruction like 142578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// a compare. 142678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerbool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, 14272249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner const SmallVectorImpl<BasicBlock *> &PredBBs) { 14282249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!PredBBs.empty() && "Can't handle an empty set"); 14292249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 143078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If BB is a loop header, then duplicating this block outside the loop would 143178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // cause us to transform this into an irreducible loop, don't do this. 143278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // See the comments above FindLoopHeaders for justifications and caveats. 143378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (LoopHeaders.count(BB)) { 1434fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName() 14352249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << "' into predecessor block '" << PredBBs[0]->getName() 143678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - it might create an irreducible loop!\n"); 143778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 143878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 14396f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 144078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned DuplicationCost = getJumpThreadDuplicationCost(BB); 144178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (DuplicationCost > Threshold) { 1442fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating BB '" << BB->getName() 144378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << DuplicationCost << "\n"); 144478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 144578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 14466f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14472249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // And finally, do it! Start by factoring the predecessors is needed. 14482249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *PredBB; 14492249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBBs.size() == 1) 14502249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = PredBBs[0]; 14512249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else { 14522249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner DEBUG(dbgs() << " Factoring out " << PredBBs.size() 14532249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << " common predecessors.\n"); 14542249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = SplitBlockPredecessors(BB, &PredBBs[0], PredBBs.size(), 14552249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ".thr_comm", this); 14562249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 14576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 145878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Okay, we decided to do this! Clone all the instructions in BB onto the end 145978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // of PredBB. 1460fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Duplicating block '" << BB->getName() << "' into end of '" 146178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << PredBB->getName() << "' to eliminate branch on phi. Cost: " 146278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << DuplicationCost << " block is:" << *BB << "\n"); 14636f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14642249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Unless PredBB ends with an unconditional branch, split the edge so that we 14652249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // can just clone the bits from BB into the end of the new PredBB. 1466d668839cb9b5db6865fd98e5e7dfccd64abf3e95Chris Lattner BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator()); 14676f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1468d668839cb9b5db6865fd98e5e7dfccd64abf3e95Chris Lattner if (OldPredBranch == 0 || !OldPredBranch->isUnconditional()) { 14692249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = SplitEdge(PredBB, BB, this); 14702249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner OldPredBranch = cast<BranchInst>(PredBB->getTerminator()); 14712249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 14726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 147378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We are going to have to map operands from the original BB block into the 147478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB block. Evaluate PHI nodes in BB. 147578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 14766f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 147778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock::iterator BI = BB->begin(); 147878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 147978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 14806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 148178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Clone the non-phi instructions of BB into PredBB, keeping track of the 148278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // mapping and using it to remap operands in the cloned instructions. 148378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; BI != BB->end(); ++BI) { 148478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Instruction *New = BI->clone(); 14856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 148678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap operands to patch up intra-block references. 148778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 148878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 148978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 149078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMapping.end()) 149178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner New->setOperand(i, I->second); 149278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 1493972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner 1494972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // If this instruction can be simplified after the operands are updated, 1495972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // just use the simplified value instead. This frequently happens due to 1496972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // phi translation. 1497972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner if (Value *IV = SimplifyInstruction(New, TD)) { 1498972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner delete New; 1499972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner ValueMapping[BI] = IV; 1500972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } else { 1501972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // Otherwise, insert the new instruction into the block. 1502972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner New->setName(BI->getName()); 1503972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner PredBB->getInstList().insert(OldPredBranch, New); 1504972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner ValueMapping[BI] = New; 1505972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } 150678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 150878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Check to see if the targets of the branch had PHI nodes. If so, we need to 150978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // add entries to the PHI nodes for branch from PredBB now. 151078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator()); 151178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB, 151278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 151378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB, 151478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 15156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 151678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there were values defined in BB that are used outside the block, then we 151778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // now have to update all uses of the value to use either the original value, 151878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 151978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 152078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdater SSAUpdate; 152178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SmallVector<Use*, 16> UsesToRename; 152278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 152378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Scan all uses of this instruction to see if it is used outside of its 152478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // block, and if so, record them in UsesToRename. 152578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 152678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++UI) { 152778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Instruction *User = cast<Instruction>(*UI); 152878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 152978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (UserPN->getIncomingBlock(UI) == BB) 153078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 153178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } else if (User->getParent() == BB) 153278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 15336f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 153478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner UsesToRename.push_back(&UI.getUse()); 153578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15366f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 153778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there are no uses outside the block, we're done with this instruction. 153878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (UsesToRename.empty()) 153978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 15406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1541fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 15426f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 154378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 154478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 154578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // with the two values we know. 1546fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 154778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.AddAvailableValue(BB, I); 154878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.AddAvailableValue(PredBB, ValueMapping[I]); 15496f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 155078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner while (!UsesToRename.empty()) 155178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1552fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 155378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15546f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 155578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB no longer jumps to BB, remove entries in the PHI node for the edge 155678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // that we nuked. 155736c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 15586f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 155978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remove the unconditional branch at the end of the PredBB block. 156078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner OldPredBranch->eraseFromParent(); 15616f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 156278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++NumDupes; 156378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return true; 156478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner} 156578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 156678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 1567