JumpThreading.cpp revision 78f7a25f9826ba66610b5bca83ebea71793abf59
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 60978f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattnerstatic bool hasAddressTakenAndUsed(BasicBlock *BB) { 61078f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner if (!BB->hasAddressTaken()) return false; 61178f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner 61278f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner // If the block has its address taken, it may be a tree of dead constants 61378f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner // hanging off of it. These shouldn't keep the block alive. 61478f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner BlockAddress *BA = BlockAddress::get(BB); 61578f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner BA->removeDeadConstantUsers(); 61678f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner return !BA->use_empty(); 61778f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner} 61878f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner 619c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner/// ProcessBlock - If there are any predecessors whose control can be threaded 620177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now. 621c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattnerbool JumpThreading::ProcessBlock(BasicBlock *BB) { 6228231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // If the block is trivially dead, just return and let the caller nuke it. 6238231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // This simplifies other transformations. 6248231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner if (pred_begin(BB) == pred_end(BB) && 6258231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner BB != &BB->getParent()->getEntryBlock()) 6268231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner return false; 6276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 62869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If this block has a single predecessor, and if that pred has a single 62969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // successor, merge the blocks. This encourages recursive jump threading 63069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // because now the condition in this block can be threaded through 63169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors of our predecessor block. 6325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BasicBlock *SinglePred = BB->getSinglePredecessor()) { 633f5102a0f088e7c96f7028bf7ca1c24975c314fffChris Lattner if (SinglePred->getTerminator()->getNumSuccessors() == 1 && 63478f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner SinglePred != BB && !hasAddressTakenAndUsed(BB)) { 635fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If SinglePred was a loop header, BB becomes one. 636fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.erase(SinglePred)) 637fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.insert(BB); 6386f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 6393d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner // Remember if SinglePred was the entry block of the function. If so, we 6403d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner // will need to move BB back to the entry position. 6413d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); 642c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->eraseBlock(SinglePred); 64369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner MergeBasicBlockIntoOnlyPred(BB); 6446f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 6453d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner if (isEntry && BB != &BB->getParent()->getEntryBlock()) 6463d86d242c69a26ba2e6102f32b00b04884c4c9b1Chris Lattner BB->moveBefore(&BB->getParent()->getEntryBlock()); 64769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 64869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 6495729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 6505729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 6516033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // What kind of constant we're looking for. 6526033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference = WantInteger; 6536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 6546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // Look to see if the terminator is a conditional branch, switch or indirect 6556033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // branch, if not we can't thread it. 656177480b7ede0441135572d641a2497df25a7d95fChris Lattner Value *Condition; 6576033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Instruction *Terminator = BB->getTerminator(); 6586033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) { 659bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Can't thread an unconditional jump. 660bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (BI->isUnconditional()) return false; 661177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = BI->getCondition(); 6626033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) { 663177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = SI->getCondition(); 6646033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) { 6656033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Condition = IB->getAddress()->stripPointerCasts(); 6666033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Preference = WantBlockAddress; 6676033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else { 668177480b7ede0441135572d641a2497df25a7d95fChris Lattner return false; // Must be an invoke. 6696033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } 6706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 671421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // If the terminator is branching on an undef, we can pick any of the 672e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner // successors to branch to. Let GetBestDestForJumpOnUndef decide. 673421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner if (isa<UndefValue>(Condition)) { 674e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned BestSucc = GetBestDestForJumpOnUndef(BB); 6756f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 676421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // Fold the branch/switch. 677e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TerminatorInst *BBTerm = BB->getTerminator(); 678421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) { 679e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner if (i == BestSucc) continue; 68036c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BBTerm->getSuccessor(i)->removePredecessor(BB, true); 681421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 6826f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 683fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " In block '" << BB->getName() 68478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' folding undef terminator: " << *BBTerm << '\n'); 685e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm); 686421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner BBTerm->eraseFromParent(); 687421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return true; 688421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 6896f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 690ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // If the terminator of this block is branching on a constant, simplify the 691ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // terminator to an unconditional branch. This can occur due to threading in 692ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // other blocks. 6936033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (getKnownConstant(Condition, Preference)) { 694ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DEBUG(dbgs() << " In block '" << BB->getName() 695ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << "' folding terminator: " << *BB->getTerminator() << '\n'); 696ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel ++NumFolds; 697ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel ConstantFoldTerminator(BB); 698ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return true; 699ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel } 700ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 701421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Instruction *CondInst = dyn_cast<Instruction>(Condition); 702421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner 703421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // All the rest of our checks depend on the condition being an instruction. 70487e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner if (CondInst == 0) { 70587e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner // FIXME: Unify this with code below. 7066033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (ProcessThreadableEdges(Condition, BB, Preference)) 70787e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner return true; 708421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return false; 7096f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 7106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7116f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7129683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) { 713660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // For a comparison where the LHS is outside this block, it's possible 714fc2fb17fb764470626f3d7ecf2fb68fe73b0d757Owen Anderson // that we've branched on it before. Used LVI to see if we can simplify 715660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // the branch based on that. 716660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator()); 717660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1)); 718c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 719c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (CondBr && CondConst && CondBr->isConditional() && PI != PE && 720660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson (!isa<Instruction>(CondCmp->getOperand(0)) || 721660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson cast<Instruction>(CondCmp->getOperand(0))->getParent() != BB)) { 722660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // For predecessor edge, determine if the comparison is true or false 723660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // on that edge. If they're all true or all false, we can simplify the 724660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // branch. 725660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson // FIXME: We could handle mixed true/false by duplicating code. 7266f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel LazyValueInfo::Tristate Baseline = 727c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson LVI->getPredicateOnEdge(CondCmp->getPredicate(), CondCmp->getOperand(0), 728c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson CondConst, *PI, BB); 729c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (Baseline != LazyValueInfo::Unknown) { 730c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson // Check that all remaining incoming values match the first one. 731c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson while (++PI != PE) { 732bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner LazyValueInfo::Tristate Ret = 733bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner LVI->getPredicateOnEdge(CondCmp->getPredicate(), 734bdabacdccae980b82b5d92e10697b8aeb3b94cfaChris Lattner CondCmp->getOperand(0), CondConst, *PI, BB); 735c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (Ret != Baseline) break; 736c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson } 7376f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 738c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson // If we terminated early, then one of the values didn't match. 739c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson if (PI == PE) { 740c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson unsigned ToRemove = Baseline == LazyValueInfo::True ? 1 : 0; 741c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson unsigned ToKeep = Baseline == LazyValueInfo::True ? 0 : 1; 74236c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true); 743c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); 744c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson CondBr->eraseFromParent(); 745c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson return true; 746c1bdac66fff1e65aba02ec853cc0693dcc967f33Owen Anderson } 747660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson } 748660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson } 7499683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky } 75069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 75169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Check for some cases that are worth simplifying. Right now we want to look 75269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // for loads that are used by a switch or by the condition for the branch. If 75369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we see one, check to see if it's partially redundant. If so, insert a PHI 75469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // which can then be used to thread the values. 75569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // 756421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Value *SimplifyValue = CondInst; 75769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue)) 75869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (isa<Constant>(CondCmp->getOperand(1))) 75969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SimplifyValue = CondCmp->getOperand(0); 7606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7614e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: There are other places where load PRE would be profitable, such as 7624e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // more complex comparisons. 76369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue)) 76469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (SimplifyPartiallyRedundantLoad(LI)) 76569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 7666f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7676f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle a variety of cases where we are branching on something derived from 7695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a PHI node in the current block. If we can prove that any predecessors 7705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // compute a predictable value based on a PHI node, thread those predecessors. 7715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // 7726033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (ProcessThreadableEdges(CondInst, BB, Preference)) 773cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner return true; 7746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 77577beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // If this is an otherwise-unfoldable branch on a phi node in the current 77677beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // block, see if we can simplify. 77777beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PHINode *PN = dyn_cast<PHINode>(CondInst)) 77877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 77977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner return ProcessBranchOnPHI(PN); 7806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7822249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify. 7832249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (CondInst->getOpcode() == Instruction::Xor && 7842249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 7852249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst)); 7866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 7876f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 78869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // TODO: If we have: "br (X > 0)" and we have a predecessor where we know 78977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // "(X == 4)", thread through this block. 7906f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 791d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner return false; 792d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner} 793d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner 7943cda3cdab118fd2b185b3e6e05e2b19a4a70c1adChris Lattner 79569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant 79669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// load instruction, eliminate it by replacing it with a PHI node. This is an 79769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// important optimization that encourages jump threading, and needs to be run 79869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// interlaced with other jump threading tasks. 79969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattnerbool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) { 80069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Don't hack volatile loads. 80169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LI->isVolatile()) return false; 8026f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 80369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the load is defined in a block with exactly one predecessor, it can't be 80469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // partially redundant. 80569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *LoadBB = LI->getParent(); 80669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadBB->getSinglePredecessor()) 80769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 8086f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 80969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner Value *LoadedPtr = LI->getOperand(0); 81069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 81169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded operand is defined in the LoadBB, it can't be available. 8124e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: Could do simple PHI translation, that would be fun :) 81369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr)) 81469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PtrOp->getParent() == LoadBB) 81569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 8166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 81769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan a few instructions up from the load, to see if it is obviously live at 81869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // the entry to its block. 81969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock::iterator BBIt = LI; 82069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 8216f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel if (Value *AvailableVal = 8224e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner FindAvailableLoadedValue(LoadedPtr, LoadBB, BBIt, 6)) { 82369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the value if the load is locally available within the block, just use 82469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it. This frequently occurs for reg2mem'd allocas. 82569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n"; 8266f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8272a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // If the returned value is the load itself, replace with an undef. This can 8282a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // only happen in dead loops. 8299e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson if (AvailableVal == LI) AvailableVal = UndefValue::get(LI->getType()); 83069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(AvailableVal); 83169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 83269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 83369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 83469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 83569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, if we scanned the whole block and got to the top of the block, 83669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we know the block is locally transparent to the load. If not, something 83769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // might clobber its value. 83869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (BBIt != LoadBB->begin()) 83969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 8406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8416f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 84269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> PredsScanned; 84369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy; 84469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredsTy AvailablePreds; 84569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *OneUnavailablePred = 0; 8466f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 84769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we got here, the loaded value is transparent through to the start of the 84869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // block. Check to see if it is available in any of the predecessor blocks. 84969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 85069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PI != PE; ++PI) { 85169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *PredBB = *PI; 85269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 85369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we already scanned this predecessor, skip it. 85469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (!PredsScanned.insert(PredBB)) 85569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 85669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 85769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan the predecessor to see if the value is available in the pred. 85869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BBIt = PredBB->end(); 85952c95856b4a40ccae6c4b0e13b2a04101e1f79c9Chris Lattner Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6); 86069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (!PredAvailable) { 86169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred = PredBB; 86269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 86369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 8646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 86569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If so, this load is partially redundant. Remember this info so that we 86669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // can create a PHI node. 86769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable)); 86869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 8696f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 87069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded value isn't available in any predecessor, it isn't partially 87169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // redundant. 87269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (AvailablePreds.empty()) return false; 8736f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 87469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Okay, the loaded value is available in at least one (and maybe all!) 87569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors. If the value is unavailable in more than one unique 87669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessor, we want to insert a merge block for those common predecessors. 87769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // This ensures that we only have to insert one reload, thus not increasing 87869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // code size. 87969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *UnavailablePred = 0; 8806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 88169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If there is exactly one predecessor where the value is unavailable, the 88269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // already computed 'OneUnavailablePred' block is it. If it ends in an 88369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // unconditional branch, we know that it isn't a critical edge. 88469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PredsScanned.size() == AvailablePreds.size()+1 && 88569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) { 88669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred = OneUnavailablePred; 88769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } else if (PredsScanned.size() != AvailablePreds.size()) { 88869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, we had multiple unavailable predecessors or we had a critical 88969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // edge from the one. 89069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallVector<BasicBlock*, 8> PredsToSplit; 89169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> AvailablePredSet; 89269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 89369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i) 89469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredSet.insert(AvailablePreds[i].first); 89569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 89669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Add all the unavailable predecessors to the PredsToSplit list. 89769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 898e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner PI != PE; ++PI) { 899ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 900e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner // If the predecessor is an indirect goto, we can't split the edge. 901ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (isa<IndirectBrInst>(P->getTerminator())) 902e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner return false; 9036f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 904ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (!AvailablePredSet.count(P)) 905ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif PredsToSplit.push_back(P); 906e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner } 9076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 90869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Split them out to their own block. 90969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred = 91069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SplitBlockPredecessors(LoadBB, &PredsToSplit[0], PredsToSplit.size(), 9114e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner "thread-pre-split", this); 91269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9136f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 91469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the value isn't available in all predecessors, then there will be 91569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // exactly one where it isn't available. Insert a load on that edge and add 91669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it to the AvailablePreds list. 91769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (UnavailablePred) { 91869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 && 91969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Can't handle critical edge here!"); 9204e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner Value *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr", false, 9214e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner LI->getAlignment(), 92269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred->getTerminator()); 92369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal)); 92469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 92669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Now we know that each predecessor of this block has a value in 92769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // AvailablePreds, sort them for efficient access as we're walking the preds. 928a3522000ab9c821f48856d0c25ada8297c1c2914Chris Lattner array_pod_sort(AvailablePreds.begin(), AvailablePreds.end()); 9296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 93069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Create a PHI node at the start of the block for the PRE'd load value. 93169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PHINode *PN = PHINode::Create(LI->getType(), "", LoadBB->begin()); 93269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PN->takeName(LI); 9336f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 93469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Insert new entries into the PHI for each predecessor. A single block may 93569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // have multiple entries here. 93669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); PI != E; 93769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner ++PI) { 938ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 9396f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel AvailablePredsTy::iterator I = 94069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(), 941ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif std::make_pair(P, (Value*)0)); 9426f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 943ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif assert(I != AvailablePreds.end() && I->first == P && 94469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Didn't find entry for predecessor!"); 9456f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 94669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PN->addIncoming(I->second, I->first); 94769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 9486f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 94969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "PRE: " << *LI << *PN << "\n"; 9506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 95169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(PN); 95269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 9536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 95469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 95569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner} 95669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 9575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// FindMostPopularDest - The specified list contains multiple possible 9585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// threadable destinations. Pick the one that occurs the most frequently in 9595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// the list. 9605729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattnerstatic BasicBlock * 9615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris LattnerFindMostPopularDest(BasicBlock *BB, 9625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner const SmallVectorImpl<std::pair<BasicBlock*, 9635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock*> > &PredToDestList) { 9645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredToDestList.empty()); 9656f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine popularity. If there are multiple possible destinations, we 9675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // explicitly choose to ignore 'undef' destinations. We prefer to thread 9685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // blocks with known and real destinations to threading undef. We'll handle 9695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // them later if interesting. 9705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned> DestPopularity; 9715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 9725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second) 9735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DestPopularity[PredToDestList[i].second]++; 9746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Find the most popular dest. 9765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned>::iterator DPI = DestPopularity.begin(); 9775729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = DPI->first; 9785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner unsigned Popularity = DPI->second; 9795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 4> SamePopularity; 9806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9815729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (++DPI; DPI != DestPopularity.end(); ++DPI) { 9825729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the popularity of this entry isn't higher than the popularity we've 9835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // seen so far, ignore it. 9845729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (DPI->second < Popularity) 9855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ; // ignore. 9865729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (DPI->second == Popularity) { 9875729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is the same as what we've seen so far, keep track of it. 9885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(DPI->first); 9895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } else { 9905729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is more popular, remember it. 9915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.clear(); 9925729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = DPI->first; 9935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Popularity = DPI->second; 9946f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 99578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 9966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 99701abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel // Okay, now we know the most popular destination. If there is more than one 9985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination, we need to determine one. This is arbitrary, but we need 9995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to make a deterministic decision. Pick the first one that appears in the 10005729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // successor list. 10015729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (!SamePopularity.empty()) { 10025729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(MostPopularDest); 10035729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *TI = BB->getTerminator(); 10045729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0; ; ++i) { 10055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(i != TI->getNumSuccessors() && "Didn't find any successor!"); 10066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (std::find(SamePopularity.begin(), SamePopularity.end(), 10085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TI->getSuccessor(i)) == SamePopularity.end()) 10095729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; 10106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10115729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = TI->getSuccessor(i); 101212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel break; 101312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 101412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10165729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Okay, we have finally picked the most popular destination. 10175729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return MostPopularDest; 10185729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 10195729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 10206033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommelbool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, 10216033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference) { 10225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If threading this would thread across a loop header, don't even try to 10235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // thread the edge. 10245729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (LoopHeaders.count(BB)) 102512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10266f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1027ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy PredValues; 10286033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference)) 102912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredValues.empty() && 10325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 10335729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 1034fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "IN BB: " << *BB; 10355729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 1036ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel dbgs() << " BB '" << BB->getName() << "': FOUND condition = " 1037ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << *PredValues[i].first 1038ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << " for pred '" << PredValues[i].second->getName() << "'.\n"; 10395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner }); 10406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Decide what we want to thread through. Convert our list of known values to 10425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a list of known destinations for each pred. This also discards duplicate 10435729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors and keeps track of the undefined inputs (which are represented 1044e7e63fe9650fc01043c96e2bf8a1ecc19e49c5b7Chris Lattner // as a null dest in the PredToDestList). 10455729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallPtrSet<BasicBlock*, 16> SeenPreds; 10465729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList; 10476f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *OnlyDest = 0; 10495729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL; 10506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10515729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 10525729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredValues[i].second; 10535729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (!SeenPreds.insert(Pred)) 10545729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; // Duplicate predecessor entry. 10556f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the predecessor ends with an indirect goto, we can't change its 10575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination. 10585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (isa<IndirectBrInst>(Pred->getTerminator())) 105912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel continue; 10606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1061ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Constant *Val = PredValues[i].first; 10626f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *DestBB; 1064ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(Val)) 10655729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DestBB = 0; 10665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) 1067ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero()); 10686033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) 1069ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DestBB = SI->getSuccessor(SI->findCaseValue(cast<ConstantInt>(Val))); 10706033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel else { 10716033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(isa<IndirectBrInst>(BB->getTerminator()) 10726033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel && "Unexpected terminator"); 10736033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel DestBB = cast<BlockAddress>(Val)->getBasicBlock(); 107412d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 10765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If we have exactly one destination, remember it for efficiency below. 107701abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel if (PredToDestList.empty()) 10785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = DestBB; 10795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (OnlyDest != DestBB) 10805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = MultipleDestSentinel; 10816f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10825729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredToDestList.push_back(std::make_pair(Pred, DestBB)); 108312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 10846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If all edges were unthreadable, we fail. 10865729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList.empty()) 108712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 10886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine which is the most common successor. If we have many inputs and 10905729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // this block is a switch, we want to start by threading the batch that goes 10915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to the most popular destination first. If we only know about one 10925729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // threadable destination (the common case) we can avoid this. 10935729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = OnlyDest; 10946f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (MostPopularDest == MultipleDestSentinel) 10965729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = FindMostPopularDest(BB, PredToDestList); 10976f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Now that we know what the most popular destination is, factor all 10995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors that will jump to it into a single predecessor. 11005729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 16> PredsToFactor; 11015729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 11025729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second == MostPopularDest) { 11035729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredToDestList[i].first; 11046f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // This predecessor may be a switch or something else that has multiple 11065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // edges to the block. Factor each of these edges by listing them 11075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // according to # occurrences in PredsToFactor. 11085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *PredTI = Pred->getTerminator(); 11095729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredTI->getNumSuccessors(); i != e; ++i) 11105729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredTI->getSuccessor(i) == BB) 11115729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredsToFactor.push_back(Pred); 11125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 11135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 11145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the threadable edges are branching on an undefined value, we get to pick 11155729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // the destination that these predecessors should get to. 11165729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (MostPopularDest == 0) 11175729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = BB->getTerminator()-> 11185729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner getSuccessor(GetBestDestForJumpOnUndef(BB)); 11196f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 112012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel // Ok, try to thread it! 11215729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return ThreadEdge(BB, PredsToFactor, MostPopularDest); 11225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 11235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 112477beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on 112577beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// a PHI node in the current block. See if there are any simplifications we 112677beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// can do based on inputs to the phi node. 11276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 112877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattnerbool JumpThreading::ProcessBranchOnPHI(PHINode *PN) { 11295729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *BB = PN->getParent(); 11306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11312249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // TODO: We could make use of this to do it once for blocks with common PHI 11322249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // values. 11332249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 1> PredBBs; 11342249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs.resize(1); 11356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11365729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If any of the predecessor blocks end in an unconditional branch, we can 113777beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // *duplicate* the conditional branch into that block in order to further 113877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // encourage jump threading and to eliminate cases where we have branch on a 113977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // phi of an icmp (branch on icmp is much better). 11405729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 11415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB = PN->getIncomingBlock(i); 11425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator())) 11432249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBr->isUnconditional()) { 11442249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs[0] = PredBB; 11452249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 11462249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs)) 11472249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return true; 11482249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 11495729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 11505729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 11515729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return false; 115212d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel} 115312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel 11542249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on 11552249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// a xor instruction in the current block. See if there are any 11562249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// simplifications we can do based on inputs to the xor. 11576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 11582249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattnerbool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) { 11592249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *BB = BO->getParent(); 11606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11612249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If either the LHS or RHS of the xor is a constant, don't do this 11622249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // optimization. 11632249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (isa<ConstantInt>(BO->getOperand(0)) || 11642249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isa<ConstantInt>(BO->getOperand(1))) 11652249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 11666f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11672dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If the first instruction in BB isn't a phi, we won't be able to infer 11682dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // anything special about any particular predecessor. 11692dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (!isa<PHINode>(BB->front())) 11702dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return false; 11716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11722249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If we have a xor as the branch input to this block, and we know that the 11732249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // LHS or RHS of the xor in any predecessor is true/false, then we can clone 11742249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // the condition into the predecessor and fix that value to true, saving some 11752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // logical ops on that path and encouraging other paths to simplify. 11762249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11772249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // This copies something like this: 11782249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11792249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB: 11802249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %X = phi i1 [1], [%X'] 11812249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp eq i32 %A, %B 11822249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Z = xor i1 %X, %Y 11832249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // br i1 %Z, ... 11842249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 11852249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Into: 11862249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB': 11872249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp ne i32 %A, %B 11882249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // br i1 %Z, ... 11892249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 1190ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy XorOpValues; 11912249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner bool isLHS = true; 11926033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues, 11936033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger)) { 11942249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(XorOpValues.empty()); 11956033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues, 11966033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger)) 11972249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 11982249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isLHS = false; 11992249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 12006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12012249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!XorOpValues.empty() && 12022249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 12032249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 12042249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Scan the information to see which is most popular: true or false. The 12052249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // predecessors can be of the set true, false, or undef. 12062249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner unsigned NumTrue = 0, NumFalse = 0; 12072249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1208ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(XorOpValues[i].first)) 1209ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // Ignore undefs for the count. 1210ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 1211ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (cast<ConstantInt>(XorOpValues[i].first)->isZero()) 12122249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumFalse; 12132249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else 12142249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumTrue; 12152249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 12166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12172249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Determine which value to split on, true, false, or undef if neither. 12182249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ConstantInt *SplitVal = 0; 12192249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (NumTrue > NumFalse) 12202249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getTrue(BB->getContext()); 12212249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else if (NumTrue != 0 || NumFalse != 0) 12222249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getFalse(BB->getContext()); 12236f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12242249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Collect all of the blocks that this can be folded into so that we can 12252249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // factor this once and clone it once. 12262249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 8> BlocksToFoldInto; 12272249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1228ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (XorOpValues[i].first != SplitVal && 1229ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel !isa<UndefValue>(XorOpValues[i].first)) 1230ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 12312249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 12322249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BlocksToFoldInto.push_back(XorOpValues[i].second); 12332249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 12346f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12352dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If we inferred a value for all of the predecessors, then duplication won't 12362dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // help us. However, we can just replace the LHS or RHS with the constant. 12372dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (BlocksToFoldInto.size() == 12382dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner cast<PHINode>(BB->front()).getNumIncomingValues()) { 12392dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (SplitVal == 0) { 12402dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide undef, just nuke the xor, because it is undef too. 12412dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(UndefValue::get(BO->getType())); 12422dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 12432dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else if (SplitVal->isZero()) { 12442dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 0, replace the xor with the other input. 12452dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(BO->getOperand(isLHS)); 12462dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 12472dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else { 12482dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 1, set the computed value to 1. 12492dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->setOperand(!isLHS, SplitVal); 12502dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 12516f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12522dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return true; 12532dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 12546f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12552249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 1256797c440caf24e92b16b9a87a39ef2f5c4cfb60f0Chris Lattner return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto); 12572249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner} 12582249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 12592249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 126078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new 126178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// predecessor to the PHIBB block. If it has PHI nodes, add entries for 126278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// NewPred using the entries from OldPred (suitably mapped). 126378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, 126478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *OldPred, 126578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *NewPred, 126678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> &ValueMap) { 126778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator PNI = PHIBB->begin(); 126878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) { 126978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Ok, we have a PHI node. Figure out what the incoming value was for the 127078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // DestBlock. 127178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Value *IV = PN->getIncomingValueForBlock(OldPred); 12726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 127378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap the value if necessary. 127478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(IV)) { 127578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst); 127678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMap.end()) 127778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner IV = I->second; 1278bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 12796f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 128078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PN->addIncoming(IV, NewPred); 1281bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 1282fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump} 1283fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 12845729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// ThreadEdge - We have decided that it is safe and profitable to factor the 12855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB 12865729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// across BB. Transform the IR to reflect this change. 12876f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommelbool JumpThreading::ThreadEdge(BasicBlock *BB, 12886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel const SmallVectorImpl<BasicBlock*> &PredBBs, 1289bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner BasicBlock *SuccBB) { 1290eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner // If threading to the same block as we come from, we would infinite loop. 1291eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner if (SuccBB == BB) { 1292fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across BB '" << BB->getName() 129393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - would thread to self!\n"); 1294eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner return false; 1295eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner } 12966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1297fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If threading this would thread across a loop header, don't thread the edge. 1298fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // See the comments above FindLoopHeaders for justifications and caveats. 1299fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.count(BB)) { 1300fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across loop header BB '" << BB->getName() 130193b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' to dest BB '" << SuccBB->getName() 130293b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - it might create an irreducible loop!\n"); 1303fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return false; 1304fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump } 1305fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 130678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); 130778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (JumpThreadCost > Threshold) { 1308fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading BB '" << BB->getName() 130978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << JumpThreadCost << "\n"); 131078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 131178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 13126f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // And finally, do it! Start by factoring the predecessors is needed. 13145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB; 13155729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredBBs.size() == 1) 13165729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredBB = PredBBs[0]; 13175729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else { 1318fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Factoring out " << PredBBs.size() 13195729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner << " common predecessors.\n"); 13205729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredBB = SplitBlockPredecessors(BB, &PredBBs[0], PredBBs.size(), 13215729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ".thr_comm", this); 13225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 13236f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1324a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner // And finally, do it! 1325fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Threading edge from '" << PredBB->getName() << "' to '" 1326460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar << SuccBB->getName() << "' with cost: " << JumpThreadCost 132793b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << ", across block:\n " 132893b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << *BB << "\n"); 13296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1330c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->threadEdge(PredBB, BB, SuccBB); 13316f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1332bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We are going to have to map operands from the original BB block to the new 1333bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to 1334bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // account for entry from PredBB. 1335bd3401fa98b578080e227afce940bca80137dea6Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 13366f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13376f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), 13386f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BB->getName()+".thread", 13391d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BB->getParent(), BB); 1340bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->moveAfter(PredBB); 13416f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1342bd3401fa98b578080e227afce940bca80137dea6Chris Lattner BasicBlock::iterator BI = BB->begin(); 1343bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 1344bd3401fa98b578080e227afce940bca80137dea6Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 13456f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1346bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Clone the non-phi instructions of BB into NewBB, keeping track of the 1347bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // mapping and using it to remap operands in the cloned instructions. 1348bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; !isa<TerminatorInst>(BI); ++BI) { 13496776064d190701c5bae4d5403939eed2e480d1cdNick Lewycky Instruction *New = BI->clone(); 1350460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar New->setName(BI->getName()); 1351bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->getInstList().push_back(New); 1352bd3401fa98b578080e227afce940bca80137dea6Chris Lattner ValueMapping[BI] = New; 13536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1354bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Remap operands to patch up intra-block references. 1355bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 1356f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 1357f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 1358f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (I != ValueMapping.end()) 1359f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman New->setOperand(i, I->second); 1360f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman } 1361bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 13626f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1363bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We didn't copy the terminator from BB over to NewBB, because there is now 1364bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // an unconditional jump to SuccBB. Insert the unconditional jump. 1365bd3401fa98b578080e227afce940bca80137dea6Chris Lattner BranchInst::Create(SuccBB, NewBB); 13666f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1367bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the 1368bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // PHI nodes for NewBB now. 136978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); 13706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1371433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there were values defined in BB that are used outside the block, then we 1372433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // now have to update all uses of the value to use either the original value, 1373433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 1374433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 1375433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdater SSAUpdate; 1376433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SmallVector<Use*, 16> UsesToRename; 1377433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 1378433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // Scan all uses of this instruction to see if it is used outside of its 1379433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // block, and if so, record them in UsesToRename. 1380433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 1381433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner ++UI) { 1382433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner Instruction *User = cast<Instruction>(*UI); 1383433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 1384433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (UserPN->getIncomingBlock(UI) == BB) 1385433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 1386433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } else if (User->getParent() == BB) 1387433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 13886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1389433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner UsesToRename.push_back(&UI.getUse()); 1390433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 13916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1392433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there are no uses outside the block, we're done with this instruction. 1393433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (UsesToRename.empty()) 1394433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 13956f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1396fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 1397433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner 1398433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 1399433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 1400433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // with the two values we know. 1401fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 1402433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.AddAvailableValue(BB, I); 1403433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.AddAvailableValue(NewBB, ValueMapping[I]); 14046f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1405433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner while (!UsesToRename.empty()) 1406433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1407fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 1408433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 14096f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1411ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // Ok, NewBB is good to go. Update the terminator of PredBB to jump to 1412bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // NewBB instead of BB. This eliminates predecessors from BB, which requires 1413bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // us to simplify any PHI nodes in BB. 1414bd3401fa98b578080e227afce940bca80137dea6Chris Lattner TerminatorInst *PredTerm = PredBB->getTerminator(); 1415bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) 1416bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (PredTerm->getSuccessor(i) == BB) { 141736c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 1418bd3401fa98b578080e227afce940bca80137dea6Chris Lattner PredTerm->setSuccessor(i, NewBB); 1419bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 14206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1421ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // At this point, the IR is fully up to date and consistent. Do a quick scan 1422ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // over the new instructions and zap any that are constants or dead. This 1423ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // frequently happens because of phi translation. 1424972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner SimplifyInstructionsInBlock(NewBB, TD); 14256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1426fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // Threaded an edge! 1427fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump ++NumThreads; 1428fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return true; 1429177480b7ede0441135572d641a2497df25a7d95fChris Lattner} 143078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 143178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch 143278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// to BB which contains an i1 PHI node and a conditional branch on that PHI. 143378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// If we can duplicate the contents of BB up into PredBB do so now, this 143478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// improves the odds that the branch will be on an analyzable instruction like 143578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// a compare. 143678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerbool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, 14372249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner const SmallVectorImpl<BasicBlock *> &PredBBs) { 14382249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!PredBBs.empty() && "Can't handle an empty set"); 14392249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 144078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If BB is a loop header, then duplicating this block outside the loop would 144178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // cause us to transform this into an irreducible loop, don't do this. 144278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // See the comments above FindLoopHeaders for justifications and caveats. 144378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (LoopHeaders.count(BB)) { 1444fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName() 14452249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << "' into predecessor block '" << PredBBs[0]->getName() 144678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - it might create an irreducible loop!\n"); 144778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 144878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 14496f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 145078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned DuplicationCost = getJumpThreadDuplicationCost(BB); 145178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (DuplicationCost > Threshold) { 1452fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating BB '" << BB->getName() 145378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << DuplicationCost << "\n"); 145478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 145578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 14566f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14572249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // And finally, do it! Start by factoring the predecessors is needed. 14582249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *PredBB; 14592249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBBs.size() == 1) 14602249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = PredBBs[0]; 14612249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else { 14622249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner DEBUG(dbgs() << " Factoring out " << PredBBs.size() 14632249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << " common predecessors.\n"); 14642249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = SplitBlockPredecessors(BB, &PredBBs[0], PredBBs.size(), 14652249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ".thr_comm", this); 14662249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 14676f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 146878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Okay, we decided to do this! Clone all the instructions in BB onto the end 146978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // of PredBB. 1470fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Duplicating block '" << BB->getName() << "' into end of '" 147178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << PredBB->getName() << "' to eliminate branch on phi. Cost: " 147278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << DuplicationCost << " block is:" << *BB << "\n"); 14736f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14742249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Unless PredBB ends with an unconditional branch, split the edge so that we 14752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // can just clone the bits from BB into the end of the new PredBB. 1476d668839cb9b5db6865fd98e5e7dfccd64abf3e95Chris Lattner BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator()); 14776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1478d668839cb9b5db6865fd98e5e7dfccd64abf3e95Chris Lattner if (OldPredBranch == 0 || !OldPredBranch->isUnconditional()) { 14792249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = SplitEdge(PredBB, BB, this); 14802249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner OldPredBranch = cast<BranchInst>(PredBB->getTerminator()); 14812249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 14826f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 148378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We are going to have to map operands from the original BB block into the 148478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB block. Evaluate PHI nodes in BB. 148578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 14866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 148778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock::iterator BI = BB->begin(); 148878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 148978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 14906f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 149178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Clone the non-phi instructions of BB into PredBB, keeping track of the 149278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // mapping and using it to remap operands in the cloned instructions. 149378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; BI != BB->end(); ++BI) { 149478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Instruction *New = BI->clone(); 14956f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 149678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap operands to patch up intra-block references. 149778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 149878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 149978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 150078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMapping.end()) 150178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner New->setOperand(i, I->second); 150278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 1503972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner 1504972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // If this instruction can be simplified after the operands are updated, 1505972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // just use the simplified value instead. This frequently happens due to 1506972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // phi translation. 1507972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner if (Value *IV = SimplifyInstruction(New, TD)) { 1508972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner delete New; 1509972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner ValueMapping[BI] = IV; 1510972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } else { 1511972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // Otherwise, insert the new instruction into the block. 1512972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner New->setName(BI->getName()); 1513972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner PredBB->getInstList().insert(OldPredBranch, New); 1514972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner ValueMapping[BI] = New; 1515972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } 151678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 151878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Check to see if the targets of the branch had PHI nodes. If so, we need to 151978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // add entries to the PHI nodes for branch from PredBB now. 152078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator()); 152178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB, 152278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 152378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB, 152478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 15256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 152678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there were values defined in BB that are used outside the block, then we 152778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // now have to update all uses of the value to use either the original value, 152878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 152978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 153078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdater SSAUpdate; 153178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SmallVector<Use*, 16> UsesToRename; 153278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 153378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Scan all uses of this instruction to see if it is used outside of its 153478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // block, and if so, record them in UsesToRename. 153578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; 153678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++UI) { 153778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Instruction *User = cast<Instruction>(*UI); 153878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 153978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (UserPN->getIncomingBlock(UI) == BB) 154078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 154178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } else if (User->getParent() == BB) 154278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 15436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 154478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner UsesToRename.push_back(&UI.getUse()); 154578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15466f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 154778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there are no uses outside the block, we're done with this instruction. 154878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (UsesToRename.empty()) 154978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 15506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1551fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 15526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 155378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 155478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 155578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // with the two values we know. 1556fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 155778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.AddAvailableValue(BB, I); 155878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.AddAvailableValue(PredBB, ValueMapping[I]); 15596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 156078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner while (!UsesToRename.empty()) 156178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1562fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 156378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 15646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 156578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB no longer jumps to BB, remove entries in the PHI node for the edge 156678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // that we nuked. 156736c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 15686f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 156978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remove the unconditional branch at the end of the PredBB block. 157078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner OldPredBranch->eraseFromParent(); 15716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 157278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++NumDupes; 157378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return true; 157478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner} 157578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 157678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 1577