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#include "llvm/Transforms/Scalar.h" 15fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/DenseMap.h" 16cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson#include "llvm/ADT/DenseSet.h" 17fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/STLExtras.h" 18fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallPtrSet.h" 19fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#include "llvm/ADT/SmallSet.h" 20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h" 21cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/GlobalsModRef.h" 2281e480463d8bb57776d03cebfd083762909023f1Nick Lewycky#include "llvm/Analysis/CFG.h" 23cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/BlockFrequencyInfo.h" 24cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/BlockFrequencyInfoImpl.h" 25cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/BranchProbabilityInfo.h" 26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/ConstantFolding.h" 27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/InstructionSimplify.h" 28d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/LazyValueInfo.h" 29d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/Loads.h" 30cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/LoopInfo.h" 314c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/Analysis/TargetLibraryInfo.h" 32cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/Analysis/ValueTracking.h" 330b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 340b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h" 350b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h" 36cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm/IR/MDBuilder.h" 3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/IR/Metadata.h" 3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/ValueHandle.h" 39d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Pass.h" 408383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner#include "llvm/Support/CommandLine.h" 41177480b7ede0441135572d641a2497df25a7d95fChris Lattner#include "llvm/Support/Debug.h" 4293b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar#include "llvm/Support/raw_ostream.h" 43d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/BasicBlockUtils.h" 44d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/Local.h" 45d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/SSAUpdater.h" 46cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include <algorithm> 47cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include <memory> 488383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerusing namespace llvm; 498383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 50dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "jump-threading" 51dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 52bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumThreads, "Number of jumps threaded"); 53bd3401fa98b578080e227afce940bca80137dea6Chris LattnerSTATISTIC(NumFolds, "Number of terminators folded"); 5478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris LattnerSTATISTIC(NumDupes, "Number of branch blocks duplicated to eliminate phi"); 558383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 56177480b7ede0441135572d641a2497df25a7d95fChris Lattnerstatic cl::opt<unsigned> 5737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesBBDuplicateThreshold("jump-threading-threshold", 58177480b7ede0441135572d641a2497df25a7d95fChris Lattner cl::desc("Max block size to duplicate for jump threading"), 59177480b7ede0441135572d641a2497df25a7d95fChris Lattner cl::init(6), cl::Hidden); 60177480b7ede0441135572d641a2497df25a7d95fChris Lattner 61cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstatic cl::opt<unsigned> 62cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarImplicationSearchThreshold( 63cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "jump-threading-implication-search-threshold", 64cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar cl::desc("The number of predecessors to search for a stronger " 65cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar "condition to use to thread over a weaker condition"), 66cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar cl::init(3), cl::Hidden); 67cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 688383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnernamespace { 69ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // These are at global scope so static functions can use them too. 70ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel typedef SmallVectorImpl<std::pair<Constant*, BasicBlock*> > PredValueInfo; 71ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel typedef SmallVector<std::pair<Constant*, BasicBlock*>, 8> PredValueInfoTy; 72ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 736033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // This is used to keep track of what kind of constant we're currently hoping 746033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // to find. 756033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel enum ConstantPreference { 766033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantInteger, 776033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel WantBlockAddress 786033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel }; 796033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 8094019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// This pass performs 'jump threading', which looks at blocks that have 8194019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// multiple predecessors and multiple successors. If one or more of the 8294019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// predecessors of the block can be proven to always jump to one of the 8394019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// successors, we forward the edge from the predecessor to the successor by 8494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// duplicating the contents of this block. 8594019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 8694019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// An example of when this can occur is code like this: 8794019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 8894019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// if () { ... 8994019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// X = 4; 9094019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// } 9194019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// if (X < 3) { 9294019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 9394019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// In this case, the unconditional branch at the end of the first if can be 9494019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// revectored to the false side of the second if. 9594019f8e064bf84085b44c35ab0c1efb76fb94f7Chris Lattner /// 963e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner class JumpThreading : public FunctionPass { 97aab8e28d5e470711d80276bbf717408c3ab966fdChad Rosier TargetLibraryInfo *TLI; 98cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner LazyValueInfo *LVI; 99cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar std::unique_ptr<BlockFrequencyInfo> BFI; 100cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar std::unique_ptr<BranchProbabilityInfo> BPI; 101cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar bool HasProfileData; 102fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#ifdef NDEBUG 103fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallPtrSet<BasicBlock*, 16> LoopHeaders; 104fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#else 105fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallSet<AssertingVH<BasicBlock>, 16> LoopHeaders; 106fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump#endif 107cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson DenseSet<std::pair<Value*, BasicBlock*> > RecursionSet; 1086f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 10937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines unsigned BBDupThreshold; 11037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 1119ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // RAII helper for updating the recursion stack. 1129ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson struct RecursionSetRemover { 1139ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson DenseSet<std::pair<Value*, BasicBlock*> > &TheSet; 1149ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson std::pair<Value*, BasicBlock*> ThePair; 1156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1169ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson RecursionSetRemover(DenseSet<std::pair<Value*, BasicBlock*> > &S, 1179ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson std::pair<Value*, BasicBlock*> P) 1189ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson : TheSet(S), ThePair(P) { } 1196f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1209ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson ~RecursionSetRemover() { 1219ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson TheSet.erase(ThePair); 1229ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson } 1239ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson }; 1248383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner public: 1258383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner static char ID; // Pass identification 12637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines JumpThreading(int T = -1) : FunctionPass(ID) { 12737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines BBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T); 128081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson initializeJumpThreadingPass(*PassRegistry::getPassRegistry()); 129081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson } 1308383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 13136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool runOnFunction(Function &F) override; 1326f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines void getAnalysisUsage(AnalysisUsage &AU) const override { 134c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson AU.addRequired<LazyValueInfo>(); 135c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson AU.addPreserved<LazyValueInfo>(); 136cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar AU.addPreserved<GlobalsAAWrapperPass>(); 137ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines AU.addRequired<TargetLibraryInfoWrapperPass>(); 138cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 1396f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 140cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar void releaseMemory() override { 141cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI.reset(); 142cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BPI.reset(); 143cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 144cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 145cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner void FindLoopHeaders(Function &F); 146c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner bool ProcessBlock(BasicBlock *BB); 1475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock*> &PredBBs, 1485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *SuccBB); 14978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, 1502249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner const SmallVectorImpl<BasicBlock *> &PredBBs); 1516f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1525729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, 1536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel PredValueInfo &Result, 15437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines ConstantPreference Preference, 15537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Instruction *CxtI = nullptr); 1566033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB, 15737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines ConstantPreference Preference, 15837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Instruction *CxtI = nullptr); 1596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 16077beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner bool ProcessBranchOnPHI(PHINode *PN); 1612249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner bool ProcessBranchOnXOR(BinaryOperator *BO); 162cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar bool ProcessImpliedCondition(BasicBlock *BB); 1636f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 16469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner bool SimplifyPartiallyRedundantLoad(LoadInst *LI); 165c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB); 166cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 167cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar private: 168cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *SplitBlockPreds(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 169cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar const char *Suffix); 170cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar void UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB, BasicBlock *BB, 171cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *NewBB, BasicBlock *SuccBB); 1728383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner }; 1738383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner} 1748383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 175844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar JumpThreading::ID = 0; 1762ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading", 1772ab36d350293c77fc8941ce1023e4899df7e3a82Owen Anderson "Jump Threading", false, false) 1782ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_DEPENDENCY(LazyValueInfo) 179ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesINITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 1802ab36d350293c77fc8941ce1023e4899df7e3a82Owen AndersonINITIALIZE_PASS_END(JumpThreading, "jump-threading", 181ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson "Jump Threading", false, false) 182844731a7f1909f55935e3514c9e713a62d67662eDan Gohman 1838383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner// Public interface to the Jump Threading pass 18437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesFunctionPass *llvm::createJumpThreadingPass(int Threshold) { return new JumpThreading(Threshold); } 1858383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner 1868383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// runOnFunction - Top level algorithm. 1878383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner/// 1888383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattnerbool JumpThreading::runOnFunction(Function &F) { 18936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (skipOptnoneFunction(F)) 19036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 19136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 192fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n"); 193ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 194c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI = &getAnalysis<LazyValueInfo>(); 195cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI.reset(); 196cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BPI.reset(); 197cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // When profile data is available, we need to update edge weights after 198cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // successful jump threading, which requires both BPI and BFI being available. 199cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar HasProfileData = F.getEntryCount().hasValue(); 200cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (HasProfileData) { 201cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar LoopInfo LI{DominatorTree(F)}; 202cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BPI.reset(new BranchProbabilityInfo(F, LI)); 203cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI.reset(new BlockFrequencyInfo(F, *BPI, LI)); 204cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 2056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 206c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // Remove unreachable blocks from function as they may result in infinite 207c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // loop. We do threading if we found something profitable. Jump threading a 208c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // branch can create other opportunities. If these opportunities form a cycle 209cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // i.e. if any jump threading is undoing previous threading in the path, then 210c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // we will loop forever. We take care of this issue by not jump threading for 211c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // back edges. This works for normal cases but not for unreachable blocks as 212c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines // they may have cycle with no back edge. 213c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines removeUnreachableBlocks(F); 214c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines 215fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump FindLoopHeaders(F); 2166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 21766b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer bool Changed, EverChanged = false; 21866b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer do { 21966b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer Changed = false; 220421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner for (Function::iterator I = F.begin(), E = F.end(); I != E;) { 221cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *BB = &*I; 2226f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel // Thread all of the branches we can over this block. 223421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner while (ProcessBlock(BB)) 224bd3401fa98b578080e227afce940bca80137dea6Chris Lattner Changed = true; 2256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 226421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner ++I; 2276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 228421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // If the block is trivially dead, zap it. This eliminates the successor 229421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // edges which simplifies the CFG. 230ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (pred_empty(BB) && 23120fa76ef6f7c2d3073e0960cf347af8db64708fcChris Lattner BB != &BB->getParent()->getEntryBlock()) { 232fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " JT: Deleting dead block '" << BB->getName() 23378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' with terminator: " << *BB->getTerminator() << '\n'); 234fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.erase(BB); 235c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->eraseBlock(BB); 236421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner DeleteDeadBlock(BB); 237421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Changed = true; 238e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner continue; 239e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner } 240f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson 241e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); 242f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson 243e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // Can't thread an unconditional jump, but if the block is "almost 244e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // empty", we can replace uses of it with uses of the successor and make 245e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // this dead. 246e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (BI && BI->isUnconditional() && 247e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB != &BB->getParent()->getEntryBlock() && 248f3183f6da6321c6496daa107ea5d6d48b99d9330Chris Lattner // If the terminator is the only non-phi instruction, try to nuke it. 249e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB->getFirstNonPHIOrDbg()->isTerminator()) { 250e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // Since TryToSimplifyUncondBranchFromEmptyBlock may delete the 251e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // block, we have to make sure it isn't in the LoopHeaders set. We 252e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // reinsert afterward if needed. 253e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner bool ErasedFromLoopHeaders = LoopHeaders.erase(BB); 254e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BasicBlock *Succ = BI->getSuccessor(0); 255e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 256e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // FIXME: It is always conservatively correct to drop the info 257e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // for a block even if it doesn't get erased. This isn't totally 258e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // awesome, but it allows us to use AssertingVH to prevent nasty 259e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // dangling pointer issues within LazyValueInfo. 260e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner LVI->eraseBlock(BB); 261e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (TryToSimplifyUncondBranchFromEmptyBlock(BB)) { 262e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner Changed = true; 263e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // If we deleted BB and BB was the header of a loop, then the 264e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner // successor is now the header of the loop. 265e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner BB = Succ; 266f3183f6da6321c6496daa107ea5d6d48b99d9330Chris Lattner } 267e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner 268e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner if (ErasedFromLoopHeaders) 269e991b5fdd5ab583531300097cf35e1087d479ef0Chris Lattner LoopHeaders.insert(BB); 270421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 271421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 272bd3401fa98b578080e227afce940bca80137dea6Chris Lattner EverChanged |= Changed; 27366b581ef4965ca3a6c72450ee9916a5c2ab44461Benjamin Kramer } while (Changed); 2746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 275fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.clear(); 276bd3401fa98b578080e227afce940bca80137dea6Chris Lattner return EverChanged; 2778383a7b7a6acdae478d4b4c2520915153b73f676Chris Lattner} 278177480b7ede0441135572d641a2497df25a7d95fChris Lattner 27978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to 280c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem/// thread across it. Stop scanning the block when passing the threshold. 281c69908695af9cf509bf498a492854db5f0556e0fNadav Rotemstatic unsigned getJumpThreadDuplicationCost(const BasicBlock *BB, 282c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem unsigned Threshold) { 28378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner /// Ignore PHI nodes, these will be flattened when duplication happens. 284cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock::const_iterator I(BB->getFirstNonPHI()); 2856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 286b14b88a40ab12996c2982c4bc10fd35bb9a371d4Chris Lattner // FIXME: THREADING will delete values that are just used to compute the 287b14b88a40ab12996c2982c4bc10fd35bb9a371d4Chris Lattner // branch, so they shouldn't count against the duplication cost. 2886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 28978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Sum up the cost of each instruction until we get to the terminator. Don't 29078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // include the terminator because the copy won't include it. 29178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner unsigned Size = 0; 29278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; !isa<TerminatorInst>(I); ++I) { 293c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem 294c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem // Stop scanning the block if we've reached the threshold. 295c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem if (Size > Threshold) 296c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem return Size; 297c69908695af9cf509bf498a492854db5f0556e0fNadav Rotem 29878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Debugger intrinsics don't incur code size. 29978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (isa<DbgInfoIntrinsic>(I)) continue; 3006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 30178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If this is a pointer->pointer bitcast, it is free. 3021df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (isa<BitCastInst>(I) && I->getType()->isPointerTy()) 30378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 3046f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 305cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Bail out if this instruction gives back a token type, it is not possible 306cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // to duplicate it if it is used outside this BB. 307cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (I->getType()->isTokenTy() && I->isUsedOutsideOfBlock(BB)) 308cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return ~0U; 309cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 31078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // All other instructions count for at least one unit. 31178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++Size; 3126f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 31378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Calls are more expensive. If they are non-intrinsic calls, we model them 31478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // as having cost of 4. If they are a non-vector intrinsic, we model them 31578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // as having cost of 2 total, and if they are a vector intrinsic, we model 31678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // them as having cost 1. 31778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (const CallInst *CI = dyn_cast<CallInst>(I)) { 318cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CI->cannotDuplicate() || CI->isConvergent()) 31967ae13575900e8efd056672987249fd0adbf5e73James Molloy // Blocks with NoDuplicate are modelled as having infinite cost, so they 32067ae13575900e8efd056672987249fd0adbf5e73James Molloy // are never duplicated. 32167ae13575900e8efd056672987249fd0adbf5e73James Molloy return ~0U; 32267ae13575900e8efd056672987249fd0adbf5e73James Molloy else if (!isa<IntrinsicInst>(CI)) 32378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size += 3; 3241df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands else if (!CI->getType()->isVectorTy()) 32578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size += 1; 32678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 32778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 3286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 32978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Threading through a switch statement is particularly profitable. If this 33078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // block ends in a switch, decrease its cost to make it more likely to happen. 33178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (isa<SwitchInst>(I)) 33278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Size = Size > 6 ? Size-6 : 0; 3336f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 3346033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // The same holds for indirect branches, but slightly more so. 3356033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (isa<IndirectBrInst>(I)) 3366033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Size = Size > 8 ? Size-8 : 0; 3376033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 33878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return Size; 33978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner} 34078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 341fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// FindLoopHeaders - We do not want jump threading to turn proper loop 342fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// structures into irreducible loops. Doing this breaks up the loop nesting 343fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// hierarchy and pessimizes later transformations. To prevent this from 344fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// happening, we first have to find the loop headers. Here we approximate this 345fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// by finding targets of backedges in the CFG. 346fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// 347fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// Note that there definitely are cases when we want to allow threading of 348fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// edges across a loop header. For example, threading a jump from outside the 349fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// loop (the preheader) to an exit block of the loop is definitely profitable. 350fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// It is also almost always profitable to thread backedges from within the loop 351fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// to exit blocks, and is often profitable to thread backedges to other blocks 352fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// within the loop (forming a nested loop). This simple analysis is not rich 353fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// enough to track all of these properties and keep it up-to-date as the CFG 354fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// mutates, so we don't allow any of these transformations. 355fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump/// 356fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stumpvoid JumpThreading::FindLoopHeaders(Function &F) { 357fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges; 358fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump FindFunctionBackedges(F, Edges); 3596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 360fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump for (unsigned i = 0, e = Edges.size(); i != e; ++i) 361fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second)); 362fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump} 363fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 364ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// getKnownConstant - Helper method to determine if we can thread over a 365ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// terminator with the given value as its condition, and if so what value to 3666033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// use for that. What kind of value this is depends on whether we want an 3676033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// integer or a block address, but an undef is always accepted. 368ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel/// Returns null if Val is null or not an appropriate constant. 3696033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommelstatic Constant *getKnownConstant(Value *Val, ConstantPreference Preference) { 370ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (!Val) 371dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines return nullptr; 372ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 373ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // Undef is "known" enough. 374ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (UndefValue *U = dyn_cast<UndefValue>(Val)) 375ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return U; 376ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 3776033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Preference == WantBlockAddress) 3786033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel return dyn_cast<BlockAddress>(Val->stripPointerCasts()); 379ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 3806033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel return dyn_cast<ConstantInt>(Val); 3810eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson} 3820eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson 3835729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see 3846033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// if we can infer that the value is a known ConstantInt/BlockAddress or undef 3856033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// in any of our predecessors. If so, return the known list of value and pred 3866033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel/// BB in the result vector. 3875729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// 3885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// This returns true if there were any known values. 3895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// 3905729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattnerbool JumpThreading:: 3916033b346e20d6932cc62c754cf23ae51786724d6Frits van BommelComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result, 39237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines ConstantPreference Preference, 39337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Instruction *CxtI) { 3949ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // This method walks up use-def chains recursively. Because of this, we could 3959ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // get into an infinite loop going around loops in the use-def chain. To 3969ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // prevent this, keep track of what (value, block) pairs we've already visited 3979ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // and terminate the search if we loop back to them 398cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson if (!RecursionSet.insert(std::make_pair(V, BB)).second) 399cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson return false; 4006f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4019ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // An RAII help to remove this pair from the recursion set once the recursion 4029ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson // stack pops back out again. 4039ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB)); 4046f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 405ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // If V is a constant, then it is known in all predecessors. 4066033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(V, Preference)) { 407cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 408ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, *PI)); 4096f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4105729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return true; 4115729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4126f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If V is a non-instruction value, or an instruction in a different block, 4145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // then it can't be derived from a PHI. 4155729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Instruction *I = dyn_cast<Instruction>(V); 416dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!I || I->getParent() != BB) { 4176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 418cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // Okay, if this is a live-in value, see if it has a known value at the end 419cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // of any of our predecessors. 420cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // 421cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // FIXME: This should be an edge property, not a block end property. 422cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner /// TODO: Per PR2563, we could infer value range information about a 423cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner /// predecessor based on its terminator. 424cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner // 425c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // FIXME: change this to use the more-rich 'getPredicateOnEdge' method if 426c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // "I" is a non-local compare-with-a-constant instruction. This would be 427c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // able to handle value inequalities better, for example if the compare is 428c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // "X < 4" and "X < 3" is known true but "X < 4" itself is not available. 429c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // Perhaps getConstantOnEdge should be smart enough to do this? 4306f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 431c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 432c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson BasicBlock *P = *PI; 433c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // If the value is known by LazyValueInfo to be a constant in a 434c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // predecessor, use that information to try to thread this block. 43537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Constant *PredCst = LVI->getConstantOnEdge(V, P, BB, CxtI); 4366033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(PredCst, Preference)) 437ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, P)); 438cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 4396f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 440c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson return !Result.empty(); 441cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner } 4426f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4435729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner /// If I is a PHI node, then we know the incoming values for any constants. 4445729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PHINode *PN = dyn_cast<PHINode>(I)) { 4455729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 4465729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *InVal = PN->getIncomingValue(i); 4476033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(InVal, Preference)) { 448ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i))); 449c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson } else { 45062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *CI = LVI->getConstantOnEdge(InVal, 45137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines PN->getIncomingBlock(i), 45237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines BB, CxtI); 4536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(CI, Preference)) 4546033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i))); 4555729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 4595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 4606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 461ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals, RHSVals; 4625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 4635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle some boolean conditions. 4646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel if (I->getType()->getPrimitiveSizeInBits() == 1) { 4656033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference == WantInteger && "One-bit non-integer type?"); 4665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // X | true -> true 4675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // X & false -> false 4685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (I->getOpcode() == Instruction::Or || 4695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner I->getOpcode() == Instruction::And) { 4706033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals, 47137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI); 4726033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals, 47337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI); 4746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4759ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson if (LHSVals.empty() && RHSVals.empty()) 4765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return false; 4776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ConstantInt *InterestingVal; 4795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (I->getOpcode() == Instruction::Or) 4805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner InterestingVal = ConstantInt::getTrue(I->getContext()); 4815729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else 4825729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner InterestingVal = ConstantInt::getFalse(I->getContext()); 4836f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4842fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner SmallPtrSet<BasicBlock*, 4> LHSKnownBBs; 4856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 4861e452650c6044735b6aa922d41736bda5721adccChris Lattner // Scan for the sentinel. If we find an undef, force it to the 4871e452650c6044735b6aa922d41736bda5721adccChris Lattner // interesting value: x|undef -> true and x&undef -> false. 4885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) 489ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (LHSVals[i].first == InterestingVal || 490ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel isa<UndefValue>(LHSVals[i].first)) { 4915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Result.push_back(LHSVals[i]); 4921e452650c6044735b6aa922d41736bda5721adccChris Lattner Result.back().first = InterestingVal; 4932fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner LHSKnownBBs.insert(LHSVals[i].second); 4941e452650c6044735b6aa922d41736bda5721adccChris Lattner } 4955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = RHSVals.size(); i != e; ++i) 496ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (RHSVals[i].first == InterestingVal || 497ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel isa<UndefValue>(RHSVals[i].first)) { 4980a96144aac449114330a5264b649eb449dc19a37Chris Lattner // If we already inferred a value for this block on the LHS, don't 4990a96144aac449114330a5264b649eb449dc19a37Chris Lattner // re-add it. 5002fa7b48eb594db245dc6af6060c92bbd2b19546bChris Lattner if (!LHSKnownBBs.count(RHSVals[i].second)) { 5010a96144aac449114330a5264b649eb449dc19a37Chris Lattner Result.push_back(RHSVals[i]); 5020a96144aac449114330a5264b649eb449dc19a37Chris Lattner Result.back().first = InterestingVal; 5030a96144aac449114330a5264b649eb449dc19a37Chris Lattner } 5041e452650c6044735b6aa922d41736bda5721adccChris Lattner } 5056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 5075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 5086f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 509055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner // Handle the NOT form of XOR. 510055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner if (I->getOpcode() == Instruction::Xor && 511055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner isa<ConstantInt>(I->getOperand(1)) && 512055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner cast<ConstantInt>(I->getOperand(1))->isOne()) { 5136033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result, 51437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI); 5159ba353627a11349b1da7b596fe44e73cd5fe7a48Owen Anderson if (Result.empty()) 516055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner return false; 517055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner 518055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner // Invert the known values. 519055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner for (unsigned i = 0, e = Result.size(); i != e; ++i) 520ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result[i].first = ConstantExpr::getNot(Result[i].first); 5216f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 522055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner return true; 523055d046f483af3c66f4d19a32a17fc63ed2c22d5Chris Lattner } 5246f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 52562efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // Try to simplify some other binary operator values. 52662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { 5276033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference != WantBlockAddress 5286033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel && "A binary operator creating a block address?"); 5290eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { 530ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals; 5316033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals, 53237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI); 5336f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 534cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson // Try to use constant folding to simplify the binary operator. 535cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { 536906a675db8af7bacdd78708453fc7f7558e64033Chris Lattner Constant *V = LHSVals[i].first; 5370eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI); 5386f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5396033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Folded, WantInteger)) 5406033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, LHSVals[i].second)); 541cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson } 54262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 5436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 544cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson return !Result.empty(); 5455729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 5466f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle compare with phi operand, where the PHI is defined in this block. 5485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) { 5496033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(Preference == WantInteger && "Compares only produce integers"); 5505729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PHINode *PN = dyn_cast<PHINode>(Cmp->getOperand(0)); 5515729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PN && PN->getParent() == BB) { 5524c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar const DataLayout &DL = PN->getModule()->getDataLayout(); 5535729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // We can do this simplification if any comparisons fold to true or false. 5545729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // See if any do. 5555729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 5565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB = PN->getIncomingBlock(i); 5575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *LHS = PN->getIncomingValue(i); 5585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Value *RHS = Cmp->getOperand(1)->DoPHITranslation(BB, PredBB); 5596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 56036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Value *Res = SimplifyCmpInst(Cmp->getPredicate(), LHS, RHS, DL); 561dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!Res) { 562c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (!isa<Constant>(RHS)) 56366c04c48debfd4357beaf9310346b2b24046b685Chris Lattner continue; 5646f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5656f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel LazyValueInfo::Tristate 56666c04c48debfd4357beaf9310346b2b24046b685Chris Lattner ResT = LVI->getPredicateOnEdge(Cmp->getPredicate(), LHS, 56737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines cast<Constant>(RHS), PredBB, BB, 56837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CxtI ? CxtI : Cmp); 56966c04c48debfd4357beaf9310346b2b24046b685Chris Lattner if (ResT == LazyValueInfo::Unknown) 57066c04c48debfd4357beaf9310346b2b24046b685Chris Lattner continue; 57166c04c48debfd4357beaf9310346b2b24046b685Chris Lattner Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT); 57266c04c48debfd4357beaf9310346b2b24046b685Chris Lattner } 5736f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5746033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Res, WantInteger)) 5756033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, PredBB)); 5765729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 5776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return !Result.empty(); 5795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 5806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 5812ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner // If comparing a live-in value against a constant, see if we know the 5822ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner // live-in value on any predecessors. 583c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson if (isa<Constant>(Cmp->getOperand(1)) && Cmp->getType()->isIntegerTy()) { 58462efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson if (!isa<Instruction>(Cmp->getOperand(0)) || 585327ca7bec274e25c05a0a4ae5b51a8a2062012c7Owen Anderson cast<Instruction>(Cmp->getOperand(0))->getParent() != BB) { 58662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *RHSCst = cast<Constant>(Cmp->getOperand(1)); 587ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif 58862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);PI != E; ++PI){ 58962efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson BasicBlock *P = *PI; 59062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // If the value is known by LazyValueInfo to be a constant in a 59162efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // predecessor, use that information to try to thread this block. 59262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson LazyValueInfo::Tristate Res = 59362efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson LVI->getPredicateOnEdge(Cmp->getPredicate(), Cmp->getOperand(0), 59437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines RHSCst, P, BB, CxtI ? CxtI : Cmp); 59562efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson if (Res == LazyValueInfo::Unknown) 59662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson continue; 5970e0ff29271df58e3bc545e40f5432c436e8bd76bChris Lattner 59862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson Constant *ResC = ConstantInt::get(Cmp->getType(), Res); 599ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(ResC, P)); 60062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 601ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif 60262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson return !Result.empty(); 60362efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 6046f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 605cb21190cbd8ae1aece5b833ebe3814ada4260627Owen Anderson // Try to find a constant value for the LHS of a comparison, 60662efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson // and evaluate it statically if we can. 607327ca7bec274e25c05a0a4ae5b51a8a2062012c7Owen Anderson if (Constant *CmpConst = dyn_cast<Constant>(Cmp->getOperand(1))) { 608ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy LHSVals; 6096033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals, 61037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI); 6116f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 61262efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) { 613906a675db8af7bacdd78708453fc7f7558e64033Chris Lattner Constant *V = LHSVals[i].first; 6140eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(), 6150eb355ab6be61ebd7adb407e02a3604be032b99eOwen Anderson V, CmpConst); 6166033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(Folded, WantInteger)) 6176033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Result.push_back(std::make_pair(KC, LHSVals[i].second)); 61862efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 6196f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 62062efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson return !Result.empty(); 62162efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 6222ad00bf99d5738c39dbc8cab7e30a0b55e0714adChris Lattner } 6235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 6246f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 62526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (SelectInst *SI = dyn_cast<SelectInst>(I)) { 62626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Handle select instructions where at least one operand is a known constant 62726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // and we can figure out the condition value for any predecessor block. 62826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *TrueVal = getKnownConstant(SI->getTrueValue(), Preference); 62926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference); 63026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel PredValueInfoTy Conds; 63126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if ((TrueVal || FalseVal) && 63226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel ComputeValueKnownInPredecessors(SI->getCondition(), BB, Conds, 63337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, CxtI)) { 63426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel for (unsigned i = 0, e = Conds.size(); i != e; ++i) { 63526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Constant *Cond = Conds[i].first; 63626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 63726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Figure out what value to use for the condition. 63826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel bool KnownCond; 63926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) { 64026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // A known boolean. 64126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel KnownCond = CI->isOne(); 64226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } else { 64326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel assert(isa<UndefValue>(Cond) && "Unexpected condition value"); 64426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // Either operand will do, so be sure to pick the one that's a known 64526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // constant. 64626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // FIXME: Do this more cleverly if both values are known constants? 647dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines KnownCond = (TrueVal != nullptr); 64826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 64926e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 65026e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel // See if the select has a known constant value for this predecessor. 65126e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel if (Constant *Val = KnownCond ? TrueVal : FalseVal) 65226e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel Result.push_back(std::make_pair(Val, Conds[i].second)); 65326e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 65426e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 65526e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel return !Result.empty(); 65626e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 65726e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel } 65826e097ca4bdb31000655ff9ec39fe7d9b7ea226dFrits van Bommel 659c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson // If all else fails, see if LVI can figure out a constant value for us. 66037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Constant *CI = LVI->getConstant(V, BB, CxtI); 6616033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (Constant *KC = getKnownConstant(CI, Preference)) { 662c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 663ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Result.push_back(std::make_pair(KC, *PI)); 66462efd3b38576536cf06ed5bf2f39535eb7961e99Owen Anderson } 6656f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 666c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson return !Result.empty(); 6675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 6685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 6695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 6706bf77500c655520a04ea8640af6dccbcf995a754Chris Lattner 671e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// GetBestDestForBranchOnUndef - If we determine that the specified block ends 672e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// in an undefined jump, decide which block is best to revector to. 673e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// 674e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// Since we can pick an arbitrary destination, we pick the successor with the 675e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// fewest predecessors. This should reduce the in-degree of the others. 676e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner/// 677e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattnerstatic unsigned GetBestDestForJumpOnUndef(BasicBlock *BB) { 678e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TerminatorInst *BBTerm = BB->getTerminator(); 679e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned MinSucc = 0; 680e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc); 681e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner // Compute the successor with the minimum number of predecessors. 682e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned MinNumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); 683e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) { 684e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TestBB = BBTerm->getSuccessor(i); 685e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned NumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); 686f227b50a8e30598464c244a675d8e857b62a52acJakub Staszak if (NumPreds < MinNumPreds) { 687e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner MinSucc = i; 688f227b50a8e30598464c244a675d8e857b62a52acJakub Staszak MinNumPreds = NumPreds; 689f227b50a8e30598464c244a675d8e857b62a52acJakub Staszak } 690e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner } 6916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 692e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner return MinSucc; 693e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner} 694e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner 69578f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattnerstatic bool hasAddressTakenAndUsed(BasicBlock *BB) { 69678f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner if (!BB->hasAddressTaken()) return false; 697f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson 69878f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner // If the block has its address taken, it may be a tree of dead constants 69978f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner // hanging off of it. These shouldn't keep the block alive. 70078f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner BlockAddress *BA = BlockAddress::get(BB); 70178f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner BA->removeDeadConstantUsers(); 70278f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner return !BA->use_empty(); 70378f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner} 70478f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner 705c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattner/// ProcessBlock - If there are any predecessors whose control can be threaded 706177480b7ede0441135572d641a2497df25a7d95fChris Lattner/// through to a successor, transform them now. 707c7bcbf6903a741e5252d6003916fb3624be937e5Chris Lattnerbool JumpThreading::ProcessBlock(BasicBlock *BB) { 7088231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // If the block is trivially dead, just return and let the caller nuke it. 7098231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner // This simplifies other transformations. 710ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (pred_empty(BB) && 7118231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner BB != &BB->getParent()->getEntryBlock()) 7128231fd1e6ca940511843381ea5f0dbfbc740b1e6Chris Lattner return false; 7136f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 71469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If this block has a single predecessor, and if that pred has a single 71569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // successor, merge the blocks. This encourages recursive jump threading 71669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // because now the condition in this block can be threaded through 71769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors of our predecessor block. 7185729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BasicBlock *SinglePred = BB->getSinglePredecessor()) { 719cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar const TerminatorInst *TI = SinglePred->getTerminator(); 720cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (!TI->isExceptional() && TI->getNumSuccessors() == 1 && 72178f7a25f9826ba66610b5bca83ebea71793abf59Chris Lattner SinglePred != BB && !hasAddressTakenAndUsed(BB)) { 722fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If SinglePred was a loop header, BB becomes one. 723fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.erase(SinglePred)) 724fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump LoopHeaders.insert(BB); 7256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 726c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->eraseBlock(SinglePred); 72769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner MergeBasicBlockIntoOnlyPred(BB); 7286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 72969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 73069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 7315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 7325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 7336033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // What kind of constant we're looking for. 7346033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel ConstantPreference Preference = WantInteger; 7356033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel 7366033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // Look to see if the terminator is a conditional branch, switch or indirect 7376033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel // branch, if not we can't thread it. 738177480b7ede0441135572d641a2497df25a7d95fChris Lattner Value *Condition; 7396033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Instruction *Terminator = BB->getTerminator(); 7406033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) { 741bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Can't thread an unconditional jump. 742bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (BI->isUnconditional()) return false; 743177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = BI->getCondition(); 7446033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) { 745177480b7ede0441135572d641a2497df25a7d95fChris Lattner Condition = SI->getCondition(); 7466033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) { 747dd2fb6c10b30e70ab8f910e21e583be3e90bb88cRichard Osborne // Can't thread indirect branch with no successors. 748dd2fb6c10b30e70ab8f910e21e583be3e90bb88cRichard Osborne if (IB->getNumSuccessors() == 0) return false; 7496033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Condition = IB->getAddress()->stripPointerCasts(); 7506033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel Preference = WantBlockAddress; 7516033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } else { 752177480b7ede0441135572d641a2497df25a7d95fChris Lattner return false; // Must be an invoke. 7536033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel } 7546f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 755f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson // Run constant folding to see if we can reduce the condition to a simple 756f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson // constant. 757f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson if (Instruction *I = dyn_cast<Instruction>(Condition)) { 7584c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar Value *SimpleVal = 7594c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar ConstantFoldInstruction(I, BB->getModule()->getDataLayout(), TLI); 760f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson if (SimpleVal) { 761f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson I->replaceAllUsesWith(SimpleVal); 762f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson I->eraseFromParent(); 763f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson Condition = SimpleVal; 764f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson } 765f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson } 766f6832bbda08b6975caa264de183bf3ee1d472acaOwen Anderson 767421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // If the terminator is branching on an undef, we can pick any of the 768e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner // successors to branch to. Let GetBestDestForJumpOnUndef decide. 769421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner if (isa<UndefValue>(Condition)) { 770e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner unsigned BestSucc = GetBestDestForJumpOnUndef(BB); 7716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 772421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // Fold the branch/switch. 773e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner TerminatorInst *BBTerm = BB->getTerminator(); 774421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) { 775e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner if (i == BestSucc) continue; 77636c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BBTerm->getSuccessor(i)->removePredecessor(BB, true); 777421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 7786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 779fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " In block '" << BB->getName() 78078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' folding undef terminator: " << *BBTerm << '\n'); 781e33583b7c2b4d715a24e4d668bf446af9c836e54Chris Lattner BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm); 782421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner BBTerm->eraseFromParent(); 783421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return true; 784421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner } 7856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 786ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // If the terminator of this block is branching on a constant, simplify the 787ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // terminator to an unconditional branch. This can occur due to threading in 788ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // other blocks. 7896033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (getKnownConstant(Condition, Preference)) { 790ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DEBUG(dbgs() << " In block '" << BB->getName() 791ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << "' folding terminator: " << *BB->getTerminator() << '\n'); 792ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel ++NumFolds; 7935649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel ConstantFoldTerminator(BB, true); 794ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel return true; 795ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel } 796ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel 797421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Instruction *CondInst = dyn_cast<Instruction>(Condition); 798421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner 799421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner // All the rest of our checks depend on the condition being an instruction. 800dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!CondInst) { 80187e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner // FIXME: Unify this with code below. 80237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (ProcessThreadableEdges(Condition, BB, Preference, Terminator)) 80387e9f59c7a823ba86d8aafa4e15ac03e6db244c3Chris Lattner return true; 804421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner return false; 8056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 8066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8089683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) { 809cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // If we're branching on a conditional, LVI might be able to determine 810cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // it's value at the branch instruction. We only handle comparisons 811cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // against a constant at this time. 812cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // TODO: This should be extended to handle switches as well. 813660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator()); 814660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1)); 815cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CondBr && CondConst && CondBr->isConditional()) { 81637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LazyValueInfo::Tristate Ret = 81737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0), 818cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CondConst, CondBr); 81937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (Ret != LazyValueInfo::Unknown) { 82037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0; 82137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1; 82237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true); 82337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr); 82437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CondBr->eraseFromParent(); 825cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (CondCmp->use_empty()) 826cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CondCmp->eraseFromParent(); 827cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar else if (CondCmp->getParent() == BB) { 828cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // If the fact we just learned is true for all uses of the 829cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // condition, replace it with a constant value 830cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto *CI = Ret == LazyValueInfo::True ? 831cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ConstantInt::getTrue(CondCmp->getType()) : 832cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ConstantInt::getFalse(CondCmp->getType()); 833cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CondCmp->replaceAllUsesWith(CI); 834cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CondCmp->eraseFromParent(); 835cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 83637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines return true; 83737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines } 838660cab32fe5105bcaa17daa4704c24065ac0a7e6Owen Anderson } 839c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 840c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if (CondBr && CondConst && TryToUnfoldSelect(CondCmp, BB)) 841c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer return true; 8429683f181746c2312c8828c79b6e25a07fb441d57Nick Lewycky } 84369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 84469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Check for some cases that are worth simplifying. Right now we want to look 84569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // for loads that are used by a switch or by the condition for the branch. If 84669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we see one, check to see if it's partially redundant. If so, insert a PHI 84769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // which can then be used to thread the values. 84869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // 849421fa9e32e445c972df4a981c60fbec4e21ac187Chris Lattner Value *SimplifyValue = CondInst; 85069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue)) 85169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (isa<Constant>(CondCmp->getOperand(1))) 85269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SimplifyValue = CondCmp->getOperand(0); 8536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8544e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: There are other places where load PRE would be profitable, such as 8554e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // more complex comparisons. 85669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue)) 85769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (SimplifyPartiallyRedundantLoad(LI)) 85869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 8596f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Handle a variety of cases where we are branching on something derived from 8625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a PHI node in the current block. If we can prove that any predecessors 8635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // compute a predictable value based on a PHI node, thread those predecessors. 8645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // 86537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (ProcessThreadableEdges(CondInst, BB, Preference, Terminator)) 866cc4d3b25f336eef135cb7125716ecb2c1979e92eChris Lattner return true; 8676f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 86877beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // If this is an otherwise-unfoldable branch on a phi node in the current 86977beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // block, see if we can simplify. 87077beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PHINode *PN = dyn_cast<PHINode>(CondInst)) 87177beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 87277beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner return ProcessBranchOnPHI(PN); 8736f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8746f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 8752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If this is an otherwise-unfoldable branch on a XOR, see if we can simplify. 8762249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (CondInst->getOpcode() == Instruction::Xor && 8772249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator())) 8782249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst)); 8796f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 880cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Search for a stronger dominating condition that can be used to simplify a 881cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // conditional branch leaving BB. 882cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (ProcessImpliedCondition(BB)) 883cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return true; 884cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 885cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return false; 886cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar} 887cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 888cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarbool JumpThreading::ProcessImpliedCondition(BasicBlock *BB) { 889cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto *BI = dyn_cast<BranchInst>(BB->getTerminator()); 890cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (!BI || !BI->isConditional()) 891cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return false; 8926f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 893cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar Value *Cond = BI->getCondition(); 894cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *CurrentBB = BB; 895cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *CurrentPred = BB->getSinglePredecessor(); 896cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar unsigned Iter = 0; 897cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 898cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto &DL = BB->getModule()->getDataLayout(); 899cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 900cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar while (CurrentPred && Iter++ < ImplicationSearchThreshold) { 901cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto *PBI = dyn_cast<BranchInst>(CurrentPred->getTerminator()); 902cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (!PBI || !PBI->isConditional() || PBI->getSuccessor(0) != CurrentBB) 903cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return false; 904cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 905cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (isImpliedCondition(PBI->getCondition(), Cond, DL)) { 906cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BI->getSuccessor(1)->removePredecessor(BB); 907cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BranchInst::Create(BI->getSuccessor(0), BI); 908cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BI->eraseFromParent(); 909cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return true; 910cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 911cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CurrentBB = CurrentPred; 912cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar CurrentPred = CurrentBB->getSinglePredecessor(); 913cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 9146f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 915d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner return false; 916d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner} 917d38c14e6ed0136624a18cf8338c82f1709d804aaChris Lattner 91869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant 91969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// load instruction, eliminate it by replacing it with a PHI node. This is an 92069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// important optimization that encourages jump threading, and needs to be run 92169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner/// interlaced with other jump threading tasks. 92269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattnerbool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) { 9232bc3d52b9ab422ee9f7e42a1a4e3b818e623a5f7Eli Friedman // Don't hack volatile/atomic loads. 9242bc3d52b9ab422ee9f7e42a1a4e3b818e623a5f7Eli Friedman if (!LI->isSimple()) return false; 9256f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 92669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the load is defined in a block with exactly one predecessor, it can't be 92769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // partially redundant. 92869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *LoadBB = LI->getParent(); 92969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (LoadBB->getSinglePredecessor()) 93069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 9316f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 932cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // If the load is defined in an EH pad, it can't be partially redundant, 933cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // because the edges between the invoke and the EH pad cannot have other 9343e033f29239e48c190f29cdf3a02cdfbaf2fe72bBill Wendling // instructions between them. 935cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (LoadBB->isEHPad()) 9363e033f29239e48c190f29cdf3a02cdfbaf2fe72bBill Wendling return false; 9373e033f29239e48c190f29cdf3a02cdfbaf2fe72bBill Wendling 93869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner Value *LoadedPtr = LI->getOperand(0); 93969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 94069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded operand is defined in the LoadBB, it can't be available. 9414e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner // TODO: Could do simple PHI translation, that would be fun :) 94269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr)) 94369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PtrOp->getParent() == LoadBB) 94469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 9456f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 94669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan a few instructions up from the load, to see if it is obviously live at 94769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // the entry to its block. 948cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock::iterator BBIt(LI); 94969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 9506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel if (Value *AvailableVal = 951cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar FindAvailableLoadedValue(LoadedPtr, LoadBB, BBIt, DefMaxInstsToScan)) { 952cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // If the value of the load is locally available within the block, just use 95369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it. This frequently occurs for reg2mem'd allocas. 95469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n"; 9556f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 9562a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // If the returned value is the load itself, replace with an undef. This can 9572a99b482a62e3f1ac3fd716fba430ac32fedade4Chris Lattner // only happen in dead loops. 9589e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson if (AvailableVal == LI) AvailableVal = UndefValue::get(LI->getType()); 95937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (AvailableVal->getType() != LI->getType()) 960ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines AvailableVal = 961ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines CastInst::CreateBitOrPointerCast(AvailableVal, LI->getType(), "", LI); 96269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(AvailableVal); 96369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 96469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 96569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 96669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 96769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, if we scanned the whole block and got to the top of the block, 96869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // we know the block is locally transparent to the load. If not, something 96969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // might clobber its value. 97069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (BBIt != LoadBB->begin()) 97169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return false; 9726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 97337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // If all of the loads and stores that feed the value have the same AA tags, 97437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // then we can propagate them onto any newly inserted loads. 97537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines AAMDNodes AATags; 97637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines LI->getAAMetadata(AATags); 9776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 97869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> PredsScanned; 97969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy; 98069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredsTy AvailablePreds; 981dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BasicBlock *OneUnavailablePred = nullptr; 9826f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 98369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we got here, the loaded value is transparent through to the start of the 98469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // block. Check to see if it is available in any of the predecessor blocks. 98569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 98669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PI != PE; ++PI) { 98769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BasicBlock *PredBB = *PI; 98869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 98969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If we already scanned this predecessor, skip it. 99037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (!PredsScanned.insert(PredBB).second) 99169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 99269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 99369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Scan the predecessor to see if the value is available in the pred. 99469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner BBIt = PredBB->end(); 99537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines AAMDNodes ThisAATags; 996cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 997cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar DefMaxInstsToScan, 99837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines nullptr, &ThisAATags); 99969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (!PredAvailable) { 100069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred = PredBB; 100169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner continue; 100269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 1003a94d6e87c4c49f2e81b01d66d8bfb591277f8f96Nadav Rotem 100437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // If AA tags disagree or are not present, forget about them. 100537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (AATags != ThisAATags) AATags = AAMDNodes(); 10066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 100769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If so, this load is partially redundant. Remember this info so that we 100869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // can create a PHI node. 100969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable)); 101069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 10116f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 101269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the loaded value isn't available in any predecessor, it isn't partially 101369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // redundant. 101469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (AvailablePreds.empty()) return false; 10156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 101669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Okay, the loaded value is available in at least one (and maybe all!) 101769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessors. If the value is unavailable in more than one unique 101869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // predecessor, we want to insert a merge block for those common predecessors. 101969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // This ensures that we only have to insert one reload, thus not increasing 102069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // code size. 1021dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BasicBlock *UnavailablePred = nullptr; 10226f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 102369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If there is exactly one predecessor where the value is unavailable, the 102469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // already computed 'OneUnavailablePred' block is it. If it ends in an 102569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // unconditional branch, we know that it isn't a critical edge. 102669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (PredsScanned.size() == AvailablePreds.size()+1 && 102769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) { 102869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred = OneUnavailablePred; 102969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } else if (PredsScanned.size() != AvailablePreds.size()) { 103069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Otherwise, we had multiple unavailable predecessors or we had a critical 103169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // edge from the one. 103269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallVector<BasicBlock*, 8> PredsToSplit; 103369e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner SmallPtrSet<BasicBlock*, 8> AvailablePredSet; 103469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 103569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i) 103669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePredSet.insert(AvailablePreds[i].first); 103769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 103869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Add all the unavailable predecessors to the PredsToSplit list. 103969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); 1040e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner PI != PE; ++PI) { 1041ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 1042e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner // If the predecessor is an indirect goto, we can't split the edge. 1043ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (isa<IndirectBrInst>(P->getTerminator())) 1044e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner return false; 10456f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1046ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif if (!AvailablePredSet.count(P)) 1047ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif PredsToSplit.push_back(P); 1048e58867e55e472d81bbf5b1d26310a5b3918d2fb6Chris Lattner } 10496f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 105069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Split them out to their own block. 1051cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar UnavailablePred = SplitBlockPreds(LoadBB, PredsToSplit, "thread-pre-split"); 105269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 10536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 105469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // If the value isn't available in all predecessors, then there will be 105569e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // exactly one where it isn't available. Insert a load on that edge and add 105669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // it to the AvailablePreds list. 105769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner if (UnavailablePred) { 105869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 && 105969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Can't handle critical edge here!"); 106095a7de6b916e49265ebdae04a32f6beda7f89028Devang Patel LoadInst *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr", false, 10614e447ebc5897a466c37bd2e9f5514d3fc5135789Chris Lattner LI->getAlignment(), 106269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner UnavailablePred->getTerminator()); 106395a7de6b916e49265ebdae04a32f6beda7f89028Devang Patel NewVal->setDebugLoc(LI->getDebugLoc()); 106437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (AATags) 106537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines NewVal->setAAMetadata(AATags); 1066a94d6e87c4c49f2e81b01d66d8bfb591277f8f96Nadav Rotem 106769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal)); 106869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 10696f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 107069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Now we know that each predecessor of this block has a value in 107169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // AvailablePreds, sort them for efficient access as we're walking the preds. 1072a3522000ab9c821f48856d0c25ada8297c1c2914Chris Lattner array_pod_sort(AvailablePreds.begin(), AvailablePreds.end()); 10736f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 107469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Create a PHI node at the start of the block for the PRE'd load value. 1075d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad pred_iterator PB = pred_begin(LoadBB), PE = pred_end(LoadBB); 10763ecfc861b4365f341c5c969b40e1afccde676e6fJay Foad PHINode *PN = PHINode::Create(LI->getType(), std::distance(PB, PE), "", 1077cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar &LoadBB->front()); 107869e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner PN->takeName(LI); 107995a7de6b916e49265ebdae04a32f6beda7f89028Devang Patel PN->setDebugLoc(LI->getDebugLoc()); 10806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 108169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // Insert new entries into the PHI for each predecessor. A single block may 108269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner // have multiple entries here. 1083d8b4fb4aab4d6fedb2b14bed1b846451b17bde7cJay Foad for (pred_iterator PI = PB; PI != PE; ++PI) { 1084ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif BasicBlock *P = *PI; 10856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel AvailablePredsTy::iterator I = 108669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(), 1087dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines std::make_pair(P, (Value*)nullptr)); 10886f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1089ee1f44fba3f2ba01657f217d159b4d09f249e46dGabor Greif assert(I != AvailablePreds.end() && I->first == P && 109069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner "Didn't find entry for predecessor!"); 10916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 109237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // If we have an available predecessor but it requires casting, insert the 109337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // cast in the predecessor and use the cast. Note that we have to update the 109437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // AvailablePreds vector as we go so that all of the PHI entries for this 109537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines // predecessor use the same bitcast. 109637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Value *&PredV = I->second; 109737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (PredV->getType() != LI->getType()) 1098ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines PredV = CastInst::CreateBitOrPointerCast(PredV, LI->getType(), "", 1099ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines P->getTerminator()); 110037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines 110137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines PN->addIncoming(PredV, I->first); 110269e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner } 11036f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 110469e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner //cerr << "PRE: " << *LI << *PN << "\n"; 11056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 110669e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->replaceAllUsesWith(PN); 110769e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner LI->eraseFromParent(); 11086f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 110969e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner return true; 111069e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner} 111169e067fdd86d34cb81ccdffb82415b4f89144218Chris Lattner 11125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// FindMostPopularDest - The specified list contains multiple possible 11135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// threadable destinations. Pick the one that occurs the most frequently in 11145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// the list. 11155729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattnerstatic BasicBlock * 11165729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris LattnerFindMostPopularDest(BasicBlock *BB, 11175729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner const SmallVectorImpl<std::pair<BasicBlock*, 11185729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock*> > &PredToDestList) { 11195729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredToDestList.empty()); 11206f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11215729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine popularity. If there are multiple possible destinations, we 11225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // explicitly choose to ignore 'undef' destinations. We prefer to thread 11235729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // blocks with known and real destinations to threading undef. We'll handle 11245729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // them later if interesting. 11255729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned> DestPopularity; 11265729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 11275729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second) 11285729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DestPopularity[PredToDestList[i].second]++; 11296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11305729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Find the most popular dest. 11315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner DenseMap<BasicBlock*, unsigned>::iterator DPI = DestPopularity.begin(); 11325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = DPI->first; 11335729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner unsigned Popularity = DPI->second; 11345729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 4> SamePopularity; 11356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11365729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (++DPI; DPI != DestPopularity.end(); ++DPI) { 11375729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the popularity of this entry isn't higher than the popularity we've 11385729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // seen so far, ignore it. 11395729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (DPI->second < Popularity) 11405729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner ; // ignore. 11415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (DPI->second == Popularity) { 11425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is the same as what we've seen so far, keep track of it. 11435729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(DPI->first); 11445729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } else { 11455729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If it is more popular, remember it. 11465729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.clear(); 11475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = DPI->first; 11485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner Popularity = DPI->second; 11496f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel } 115078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 11516f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 115201abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel // Okay, now we know the most popular destination. If there is more than one 11535729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination, we need to determine one. This is arbitrary, but we need 11545729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to make a deterministic decision. Pick the first one that appears in the 11555729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // successor list. 11565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (!SamePopularity.empty()) { 11575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SamePopularity.push_back(MostPopularDest); 11585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *TI = BB->getTerminator(); 11595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0; ; ++i) { 11605729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(i != TI->getNumSuccessors() && "Didn't find any successor!"); 11616f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (std::find(SamePopularity.begin(), SamePopularity.end(), 11635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TI->getSuccessor(i)) == SamePopularity.end()) 11645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; 11656f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = TI->getSuccessor(i); 116712d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel break; 116812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 116912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 11706f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Okay, we have finally picked the most popular destination. 11725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return MostPopularDest; 11735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 11745729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 11756033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommelbool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, 117637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines ConstantPreference Preference, 117737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines Instruction *CxtI) { 11785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If threading this would thread across a loop header, don't even try to 11795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // thread the edge. 11805729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (LoopHeaders.count(BB)) 118112d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 11826f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1183ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy PredValues; 118437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference, CxtI)) 118512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 11866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11875729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner assert(!PredValues.empty() && 11885729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 11895729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 1190fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "IN BB: " << *BB; 11915729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 1192ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel dbgs() << " BB '" << BB->getName() << "': FOUND condition = " 1193ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << *PredValues[i].first 1194ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel << " for pred '" << PredValues[i].second->getName() << "'.\n"; 11955729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner }); 11966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 11975729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Decide what we want to thread through. Convert our list of known values to 11985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // a list of known destinations for each pred. This also discards duplicate 11995729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors and keeps track of the undefined inputs (which are represented 1200e7e63fe9650fc01043c96e2bf8a1ecc19e49c5b7Chris Lattner // as a null dest in the PredToDestList). 12015729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallPtrSet<BasicBlock*, 16> SeenPreds; 12025729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList; 12036f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1204dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines BasicBlock *OnlyDest = nullptr; 12055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL; 12066f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { 12085729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredValues[i].second; 120937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (!SeenPreds.insert(Pred).second) 12105729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner continue; // Duplicate predecessor entry. 12116f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12125729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the predecessor ends with an indirect goto, we can't change its 12135729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // destination. 12145729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (isa<IndirectBrInst>(Pred->getTerminator())) 121512d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel continue; 12166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1217ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel Constant *Val = PredValues[i].first; 12186f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12195729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *DestBB; 1220ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(Val)) 1221dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines DestBB = nullptr; 12225729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) 1223ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero()); 122424473120a253a05f3601cd3373403b47e6d03d41Stepan Dyatkovskiy else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { 1225c10fa6c801e48771b5eade50afc2fe6abaf08227Stepan Dyatkovskiy DestBB = SI->findCaseValue(cast<ConstantInt>(Val)).getCaseSuccessor(); 122624473120a253a05f3601cd3373403b47e6d03d41Stepan Dyatkovskiy } else { 12276033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel assert(isa<IndirectBrInst>(BB->getTerminator()) 12286033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel && "Unexpected terminator"); 12296033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel DestBB = cast<BlockAddress>(Val)->getBasicBlock(); 123012d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 12315729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 12325729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If we have exactly one destination, remember it for efficiency below. 123301abcf339f8d42921c680fefb2ff988cfeee1198Frits van Bommel if (PredToDestList.empty()) 12345729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = DestBB; 12355729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else if (OnlyDest != DestBB) 12365729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner OnlyDest = MultipleDestSentinel; 12376f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12385729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredToDestList.push_back(std::make_pair(Pred, DestBB)); 123912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel } 12406f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If all edges were unthreadable, we fail. 12425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList.empty()) 124312d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel return false; 12446f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12455729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Determine which is the most common successor. If we have many inputs and 12465729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // this block is a switch, we want to start by threading the batch that goes 12475729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // to the most popular destination first. If we only know about one 12485729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // threadable destination (the common case) we can avoid this. 12495729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *MostPopularDest = OnlyDest; 12506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12515729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (MostPopularDest == MultipleDestSentinel) 12525729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = FindMostPopularDest(BB, PredToDestList); 12536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12545729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // Now that we know what the most popular destination is, factor all 12555729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // predecessors that will jump to it into a single predecessor. 12565729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner SmallVector<BasicBlock*, 16> PredsToFactor; 12575729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i) 12585729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredToDestList[i].second == MostPopularDest) { 12595729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *Pred = PredToDestList[i].first; 12606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12615729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // This predecessor may be a switch or something else that has multiple 12625729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // edges to the block. Factor each of these edges by listing them 12635729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // according to # occurrences in PredsToFactor. 12645729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner TerminatorInst *PredTI = Pred->getTerminator(); 12655729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PredTI->getNumSuccessors(); i != e; ++i) 12665729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredTI->getSuccessor(i) == BB) 12675729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredsToFactor.push_back(Pred); 12685729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 12695729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 12705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If the threadable edges are branching on an undefined value, we get to pick 12715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // the destination that these predecessors should get to. 1272dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!MostPopularDest) 12735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner MostPopularDest = BB->getTerminator()-> 12745729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner getSuccessor(GetBestDestForJumpOnUndef(BB)); 12756f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 127612d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel // Ok, try to thread it! 12775729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return ThreadEdge(BB, PredsToFactor, MostPopularDest); 12785729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner} 12795729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 128077beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on 128177beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// a PHI node in the current block. See if there are any simplifications we 128277beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner/// can do based on inputs to the phi node. 12836f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 128477beb479f556093c5f1b4854fcbb095cda34f202Chris Lattnerbool JumpThreading::ProcessBranchOnPHI(PHINode *PN) { 12855729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *BB = PN->getParent(); 12866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12872249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // TODO: We could make use of this to do it once for blocks with common PHI 12882249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // values. 12892249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 1> PredBBs; 12902249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs.resize(1); 12916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 12925729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner // If any of the predecessor blocks end in an unconditional branch, we can 129377beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // *duplicate* the conditional branch into that block in order to further 129477beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // encourage jump threading and to eliminate cases where we have branch on a 129577beb479f556093c5f1b4854fcbb095cda34f202Chris Lattner // phi of an icmp (branch on icmp is much better). 12965729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 12975729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB = PN->getIncomingBlock(i); 12985729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator())) 12992249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBr->isUnconditional()) { 13002249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBBs[0] = PredBB; 13012249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 13022249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs)) 13032249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return true; 13042249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 13055729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 13065729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner 13075729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner return false; 130812d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel} 130912d53dba36905d20b02cf7c4cdae057ae4a5a5e1Devang Patel 13102249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on 13112249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// a xor instruction in the current block. See if there are any 13122249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner/// simplifications we can do based on inputs to the xor. 13136f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel/// 13142249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattnerbool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) { 13152249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *BB = BO->getParent(); 13166f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13172249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If either the LHS or RHS of the xor is a constant, don't do this 13182249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // optimization. 13192249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (isa<ConstantInt>(BO->getOperand(0)) || 13202249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isa<ConstantInt>(BO->getOperand(1))) 13212249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 13226f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13232dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If the first instruction in BB isn't a phi, we won't be able to infer 13242dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // anything special about any particular predecessor. 13252dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (!isa<PHINode>(BB->front())) 13262dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return false; 13276f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13282249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // If we have a xor as the branch input to this block, and we know that the 13292249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // LHS or RHS of the xor in any predecessor is true/false, then we can clone 13302249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // the condition into the predecessor and fix that value to true, saving some 13312249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // logical ops on that path and encouraging other paths to simplify. 13322249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 13332249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // This copies something like this: 13342249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 13352249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB: 13362249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %X = phi i1 [1], [%X'] 13372249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp eq i32 %A, %B 13382249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Z = xor i1 %X, %Y 13392249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // br i1 %Z, ... 13402249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // 13412249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Into: 13422249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // BB': 13432249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // %Y = icmp ne i32 %A, %B 1344cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // br i1 %Y, ... 13452249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 1346ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel PredValueInfoTy XorOpValues; 13472249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner bool isLHS = true; 13486033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues, 134937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, BO)) { 13502249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(XorOpValues.empty()); 13516033b346e20d6932cc62c754cf23ae51786724d6Frits van Bommel if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues, 135237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines WantInteger, BO)) 13532249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner return false; 13542249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner isLHS = false; 13552249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 13566f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13572249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!XorOpValues.empty() && 13582249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner "ComputeValueKnownInPredecessors returned true with no values"); 13592249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 13602249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Scan the information to see which is most popular: true or false. The 13612249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // predecessors can be of the set true, false, or undef. 13622249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner unsigned NumTrue = 0, NumFalse = 0; 13632249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1364ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (isa<UndefValue>(XorOpValues[i].first)) 1365ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel // Ignore undefs for the count. 1366ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 1367ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (cast<ConstantInt>(XorOpValues[i].first)->isZero()) 13682249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumFalse; 13692249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else 13702249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner ++NumTrue; 13712249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 13726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13732249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Determine which value to split on, true, false, or undef if neither. 1374dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ConstantInt *SplitVal = nullptr; 13752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (NumTrue > NumFalse) 13762249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getTrue(BB->getContext()); 13772249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else if (NumTrue != 0 || NumFalse != 0) 13782249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SplitVal = ConstantInt::getFalse(BB->getContext()); 13796f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13802249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Collect all of the blocks that this can be folded into so that we can 13812249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // factor this once and clone it once. 13822249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner SmallVector<BasicBlock*, 8> BlocksToFoldInto; 13832249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) { 1384ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel if (XorOpValues[i].first != SplitVal && 1385ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel !isa<UndefValue>(XorOpValues[i].first)) 1386ea388f217b8368db13f598e4c547bab5582eb82aFrits van Bommel continue; 13872249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 13882249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BlocksToFoldInto.push_back(XorOpValues[i].second); 13892249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 13906f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 13912dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If we inferred a value for all of the predecessors, then duplication won't 13922dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // help us. However, we can just replace the LHS or RHS with the constant. 13932dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner if (BlocksToFoldInto.size() == 13942dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner cast<PHINode>(BB->front()).getNumIncomingValues()) { 1395dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!SplitVal) { 13962dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide undef, just nuke the xor, because it is undef too. 13972dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(UndefValue::get(BO->getType())); 13982dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 13992dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else if (SplitVal->isZero()) { 14002dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 0, replace the xor with the other input. 14012dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->replaceAllUsesWith(BO->getOperand(isLHS)); 14022dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->eraseFromParent(); 14032dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } else { 14042dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner // If all preds provide 1, set the computed value to 1. 14052dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner BO->setOperand(!isLHS, SplitVal); 14062dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 14076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14082dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner return true; 14092dd7657a5b063e6f77c34f418b7e23654b6fe4a0Chris Lattner } 14106f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14112249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Try to duplicate BB into PredBB. 1412797c440caf24e92b16b9a87a39ef2f5c4cfb60f0Chris Lattner return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto); 14132249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner} 14142249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 14152249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 141678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new 141778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// predecessor to the PHIBB block. If it has PHI nodes, add entries for 141878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// NewPred using the entries from OldPred (suitably mapped). 141978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerstatic void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, 142078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *OldPred, 142178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock *NewPred, 142278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> &ValueMap) { 142378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator PNI = PHIBB->begin(); 142478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) { 142578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Ok, we have a PHI node. Figure out what the incoming value was for the 142678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // DestBlock. 142778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Value *IV = PN->getIncomingValueForBlock(OldPred); 14286f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 142978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap the value if necessary. 143078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(IV)) { 143178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst); 143278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMap.end()) 143378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner IV = I->second; 1434bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 14356f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 143678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner PN->addIncoming(IV, NewPred); 1437bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner } 1438fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump} 1439fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 14405729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// ThreadEdge - We have decided that it is safe and profitable to factor the 14415729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB 14425729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner/// across BB. Transform the IR to reflect this change. 14436f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommelbool JumpThreading::ThreadEdge(BasicBlock *BB, 14446f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel const SmallVectorImpl<BasicBlock*> &PredBBs, 1445bdbf1a177de085927c375efa986c52af2057fb7aChris Lattner BasicBlock *SuccBB) { 1446eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner // If threading to the same block as we come from, we would infinite loop. 1447eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner if (SuccBB == BB) { 1448fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across BB '" << BB->getName() 144993b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - would thread to self!\n"); 1450eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner return false; 1451eede65ce6cb29c8f3a701be8606e95c9a213efffChris Lattner } 14526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1453fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // If threading this would thread across a loop header, don't thread the edge. 1454fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // See the comments above FindLoopHeaders for justifications and caveats. 1455fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump if (LoopHeaders.count(BB)) { 1456fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading across loop header BB '" << BB->getName() 145793b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' to dest BB '" << SuccBB->getName() 145893b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << "' - it might create an irreducible loop!\n"); 1459fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return false; 1460fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump } 1461fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump 146237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB, BBDupThreshold); 146337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (JumpThreadCost > BBDupThreshold) { 1464fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not threading BB '" << BB->getName() 146578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << JumpThreadCost << "\n"); 146678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 146778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 14686f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1469cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // And finally, do it! Start by factoring the predecessors if needed. 14705729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner BasicBlock *PredBB; 14715729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner if (PredBBs.size() == 1) 14725729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner PredBB = PredBBs[0]; 14735729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner else { 1474fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Factoring out " << PredBBs.size() 14755729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner << " common predecessors.\n"); 1476cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm"); 14775729d38c81bc2e3b21a2bb7a80a8cde384fc7b7bChris Lattner } 14786f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1479a5ddb59a1319ccd23844c74809a64bc4d88f59d1Chris Lattner // And finally, do it! 1480fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Threading edge from '" << PredBB->getName() << "' to '" 1481460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar << SuccBB->getName() << "' with cost: " << JumpThreadCost 148293b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << ", across block:\n " 148393b67e40de356569493c285b86b138a3f11b5035Daniel Dunbar << *BB << "\n"); 14846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1485c809d90bca41a5578012959ea2e493f9d949f969Owen Anderson LVI->threadEdge(PredBB, BB, SuccBB); 14866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1487bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We are going to have to map operands from the original BB block to the new 1488bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to 1489bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // account for entry from PredBB. 1490bd3401fa98b578080e227afce940bca80137dea6Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 14916f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 14926f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), 14936f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel BB->getName()+".thread", 14941d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BB->getParent(), BB); 1495bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->moveAfter(PredBB); 14966f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1497cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Set the block frequency of NewBB. 1498cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (HasProfileData) { 1499cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto NewBBFreq = 1500cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB); 1501cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency()); 1502cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 1503cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1504bd3401fa98b578080e227afce940bca80137dea6Chris Lattner BasicBlock::iterator BI = BB->begin(); 1505bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 1506bd3401fa98b578080e227afce940bca80137dea6Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 15076f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1508bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Clone the non-phi instructions of BB into NewBB, keeping track of the 1509bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // mapping and using it to remap operands in the cloned instructions. 1510bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (; !isa<TerminatorInst>(BI); ++BI) { 15116776064d190701c5bae4d5403939eed2e480d1cdNick Lewycky Instruction *New = BI->clone(); 1512460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar New->setName(BI->getName()); 1513bd3401fa98b578080e227afce940bca80137dea6Chris Lattner NewBB->getInstList().push_back(New); 1514cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ValueMapping[&*BI] = New; 15156f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1516bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Remap operands to patch up intra-block references. 1517bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 1518f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 1519f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 1520f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman if (I != ValueMapping.end()) 1521f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman New->setOperand(i, I->second); 1522f530c92cd55f35f64904e42e38b3a2bc92b347cbDan Gohman } 1523bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 15246f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1525bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // We didn't copy the terminator from BB over to NewBB, because there is now 1526bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // an unconditional jump to SuccBB. Insert the unconditional jump. 1527cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BranchInst *NewBI = BranchInst::Create(SuccBB, NewBB); 152895a7de6b916e49265ebdae04a32f6beda7f89028Devang Patel NewBI->setDebugLoc(BB->getTerminator()->getDebugLoc()); 15296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1530bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the 1531bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // PHI nodes for NewBB now. 153278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping); 15336f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1534433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there were values defined in BB that are used outside the block, then we 1535433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // now have to update all uses of the value to use either the original value, 1536433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 1537433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 1538433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdater SSAUpdate; 1539433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SmallVector<Use*, 16> UsesToRename; 1540433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 1541433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // Scan all uses of this instruction to see if it is used outside of its 1542433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // block, and if so, record them in UsesToRename. 154336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (Use &U : I->uses()) { 154436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Instruction *User = cast<Instruction>(U.getUser()); 1545433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 154636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (UserPN->getIncomingBlock(U) == BB) 1547433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 1548433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } else if (User->getParent() == BB) 1549433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 15506f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 155136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines UsesToRename.push_back(&U); 1552433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 15536f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1554433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // If there are no uses outside the block, we're done with this instruction. 1555433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner if (UsesToRename.empty()) 1556433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner continue; 15576f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1558fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 1559433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner 1560433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 1561433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 1562433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner // with the two values we know. 1563fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 1564cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SSAUpdate.AddAvailableValue(BB, &*I); 1565cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SSAUpdate.AddAvailableValue(NewBB, ValueMapping[&*I]); 15666f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1567433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner while (!UsesToRename.empty()) 1568433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1569fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 1570433a0dbb262b16b42d75715a4dad618a475ba91bChris Lattner } 15716f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 15726f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1573ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // Ok, NewBB is good to go. Update the terminator of PredBB to jump to 1574bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // NewBB instead of BB. This eliminates predecessors from BB, which requires 1575bd3401fa98b578080e227afce940bca80137dea6Chris Lattner // us to simplify any PHI nodes in BB. 1576bd3401fa98b578080e227afce940bca80137dea6Chris Lattner TerminatorInst *PredTerm = PredBB->getTerminator(); 1577bd3401fa98b578080e227afce940bca80137dea6Chris Lattner for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) 1578bd3401fa98b578080e227afce940bca80137dea6Chris Lattner if (PredTerm->getSuccessor(i) == BB) { 157936c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 1580bd3401fa98b578080e227afce940bca80137dea6Chris Lattner PredTerm->setSuccessor(i, NewBB); 1581bd3401fa98b578080e227afce940bca80137dea6Chris Lattner } 15826f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1583ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // At this point, the IR is fully up to date and consistent. Do a quick scan 1584ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // over the new instructions and zap any that are constants or dead. This 1585ef0c6744d5050ad724e1ae90e6ac2b932ce01e13Chris Lattner // frequently happens because of phi translation. 15864c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar SimplifyInstructionsInBlock(NewBB, TLI); 15876f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1588cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Update the edge weight from BB to SuccBB, which should be less than before. 1589cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar UpdateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB); 1590cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1591fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump // Threaded an edge! 1592fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump ++NumThreads; 1593fe095f39e7009c51d1c86769792ccbcad8cdd2ecMike Stump return true; 1594177480b7ede0441135572d641a2497df25a7d95fChris Lattner} 159578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner 1596cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// Create a new basic block that will be the predecessor of BB and successor of 1597cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// all blocks in Preds. When profile data is availble, update the frequency of 1598cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// this new block. 1599cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarBasicBlock *JumpThreading::SplitBlockPreds(BasicBlock *BB, 1600cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ArrayRef<BasicBlock *> Preds, 1601cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar const char *Suffix) { 1602cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Collect the frequencies of all predecessors of BB, which will be used to 1603cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // update the edge weight on BB->SuccBB. 1604cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BlockFrequency PredBBFreq(0); 1605cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (HasProfileData) 1606cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar for (auto Pred : Preds) 1607cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar PredBBFreq += BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB); 1608cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1609cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *PredBB = SplitBlockPredecessors(BB, Preds, Suffix); 1610cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1611cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Set the block frequency of the newly created PredBB, which is the sum of 1612cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // frequencies of Preds. 1613cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (HasProfileData) 1614cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI->setBlockFreq(PredBB, PredBBFreq.getFrequency()); 1615cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return PredBB; 1616cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar} 1617cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1618cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// Update the block frequency of BB and branch weight and the metadata on the 1619cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// edge BB->SuccBB. This is done by scaling the weight of BB->SuccBB by 1 - 1620cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar/// Freq(PredBB->BB) / Freq(BB->SuccBB). 1621cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarvoid JumpThreading::UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB, 1622cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *BB, 1623cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *NewBB, 1624cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BasicBlock *SuccBB) { 1625cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (!HasProfileData) 1626cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar return; 1627cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1628cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar assert(BFI && BPI && "BFI & BPI should have been created here"); 1629cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1630cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // As the edge from PredBB to BB is deleted, we have to update the block 1631cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // frequency of BB. 1632cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto BBOrigFreq = BFI->getBlockFreq(BB); 1633cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto NewBBFreq = BFI->getBlockFreq(NewBB); 1634cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB); 1635cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto BBNewFreq = BBOrigFreq - NewBBFreq; 1636cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BFI->setBlockFreq(BB, BBNewFreq.getFrequency()); 1637cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1638cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Collect updated outgoing edges' frequencies from BB and use them to update 1639cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // edge weights. 1640cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SmallVector<uint64_t, 4> BBSuccFreq; 1641cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) { 1642cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto SuccFreq = (*I == SuccBB) 1643cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ? BB2SuccBBFreq - NewBBFreq 1644cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar : BBOrigFreq * BPI->getEdgeProbability(BB, *I); 1645cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BBSuccFreq.push_back(SuccFreq.getFrequency()); 1646cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 1647cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1648cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Normalize edge weights in Weights64 so that the sum of them can fit in 1649cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BranchProbability::normalizeEdgeWeights(BBSuccFreq.begin(), BBSuccFreq.end()); 1650cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1651cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SmallVector<uint32_t, 4> Weights; 1652cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar for (auto Freq : BBSuccFreq) 1653cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar Weights.push_back(static_cast<uint32_t>(Freq)); 1654cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1655cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // Update edge weights in BPI. 1656cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar for (int I = 0, E = Weights.size(); I < E; I++) 1657cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar BPI->setEdgeWeight(BB, I, Weights[I]); 1658cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 1659cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar if (Weights.size() >= 2) { 1660cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar auto TI = BB->getTerminator(); 1661cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar TI->setMetadata( 1662cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar LLVMContext::MD_prof, 1663cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar MDBuilder(TI->getParent()->getContext()).createBranchWeights(Weights)); 1664cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar } 1665cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar} 1666cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar 166778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch 166878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// to BB which contains an i1 PHI node and a conditional branch on that PHI. 166978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// If we can duplicate the contents of BB up into PredBB do so now, this 167078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// improves the odds that the branch will be on an analyzable instruction like 167178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner/// a compare. 167278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattnerbool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, 16732249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner const SmallVectorImpl<BasicBlock *> &PredBBs) { 16742249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner assert(!PredBBs.empty() && "Can't handle an empty set"); 16752249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner 167678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If BB is a loop header, then duplicating this block outside the loop would 167778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // cause us to transform this into an irreducible loop, don't do this. 167878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // See the comments above FindLoopHeaders for justifications and caveats. 167978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (LoopHeaders.count(BB)) { 1680fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName() 16812249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << "' into predecessor block '" << PredBBs[0]->getName() 168278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - it might create an irreducible loop!\n"); 168378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 168478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 16856f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 168637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines unsigned DuplicationCost = getJumpThreadDuplicationCost(BB, BBDupThreshold); 168737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines if (DuplicationCost > BBDupThreshold) { 1688fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Not duplicating BB '" << BB->getName() 168978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << "' - Cost is too high: " << DuplicationCost << "\n"); 169078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return false; 169178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 16926f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1693cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar // And finally, do it! Start by factoring the predecessors if needed. 16942249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner BasicBlock *PredBB; 16952249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner if (PredBBs.size() == 1) 16962249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner PredBB = PredBBs[0]; 16972249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner else { 16982249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner DEBUG(dbgs() << " Factoring out " << PredBBs.size() 16992249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner << " common predecessors.\n"); 1700cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm"); 17012249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 17026f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 170378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Okay, we decided to do this! Clone all the instructions in BB onto the end 170478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // of PredBB. 1705fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << " Duplicating block '" << BB->getName() << "' into end of '" 170678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << PredBB->getName() << "' to eliminate branch on phi. Cost: " 170778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner << DuplicationCost << " block is:" << *BB << "\n"); 17086f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 17092249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // Unless PredBB ends with an unconditional branch, split the edge so that we 17102249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner // can just clone the bits from BB into the end of the new PredBB. 1711d668839cb9b5db6865fd98e5e7dfccd64abf3e95Chris Lattner BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator()); 17126f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1713dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!OldPredBranch || !OldPredBranch->isUnconditional()) { 1714ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines PredBB = SplitEdge(PredBB, BB); 17152249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner OldPredBranch = cast<BranchInst>(PredBB->getTerminator()); 17162249a0b1bd0941e61c0b87f2f64a5c8cab45f14cChris Lattner } 17176f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 171878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We are going to have to map operands from the original BB block into the 171978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB block. Evaluate PHI nodes in BB. 172078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*> ValueMapping; 17216f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 172278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BasicBlock::iterator BI = BB->begin(); 172378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 172478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 172578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Clone the non-phi instructions of BB into PredBB, keeping track of the 172678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // mapping and using it to remap operands in the cloned instructions. 172778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (; BI != BB->end(); ++BI) { 172878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner Instruction *New = BI->clone(); 17296f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 173078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remap operands to patch up intra-block references. 173178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 173278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 173378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); 173478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (I != ValueMapping.end()) 173578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner New->setOperand(i, I->second); 173678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 1737972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner 1738972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // If this instruction can be simplified after the operands are updated, 1739972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // just use the simplified value instead. This frequently happens due to 1740972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // phi translation. 17414c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar if (Value *IV = 17424c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar SimplifyInstruction(New, BB->getModule()->getDataLayout())) { 1743972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner delete New; 1744cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ValueMapping[&*BI] = IV; 1745972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } else { 1746972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner // Otherwise, insert the new instruction into the block. 1747972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner New->setName(BI->getName()); 1748cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar PredBB->getInstList().insert(OldPredBranch->getIterator(), New); 1749cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar ValueMapping[&*BI] = New; 1750972a46c96a06ecb0e8ce73c796c313175abe76bbChris Lattner } 175178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 17526f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 175378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Check to see if the targets of the branch had PHI nodes. If so, we need to 175478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // add entries to the PHI nodes for branch from PredBB now. 175578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator()); 175678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB, 175778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 175878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB, 175978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ValueMapping); 17606f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 176178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there were values defined in BB that are used outside the block, then we 176278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // now have to update all uses of the value to use either the original value, 176378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // the cloned value, or some PHI derived value. This can require arbitrary 176478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PHI insertion, of which we are prepared to do, clean these up now. 176578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdater SSAUpdate; 176678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SmallVector<Use*, 16> UsesToRename; 176778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 176878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Scan all uses of this instruction to see if it is used outside of its 176978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // block, and if so, record them in UsesToRename. 177036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (Use &U : I->uses()) { 177136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Instruction *User = cast<Instruction>(U.getUser()); 177278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (PHINode *UserPN = dyn_cast<PHINode>(User)) { 177336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (UserPN->getIncomingBlock(U) == BB) 177478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 177578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } else if (User->getParent() == BB) 177678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 17776f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 177836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines UsesToRename.push_back(&U); 177978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 17806f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 178178c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // If there are no uses outside the block, we're done with this instruction. 178278c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner if (UsesToRename.empty()) 178378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner continue; 17846f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 1785fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n"); 17866f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 178778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // We found a use of I outside of BB. Rename all uses of I that are outside 178878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // its block to be uses of the appropriate PHI node etc. See ValuesInBlocks 178978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // with the two values we know. 1790fc6e29d4ab52b7d3efd83846ed495a9ca7e51e49Duncan Sands SSAUpdate.Initialize(I->getType(), I->getName()); 1791cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SSAUpdate.AddAvailableValue(BB, &*I); 1792cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar SSAUpdate.AddAvailableValue(PredBB, ValueMapping[&*I]); 17936f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 179478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner while (!UsesToRename.empty()) 179578c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner SSAUpdate.RewriteUse(*UsesToRename.pop_back_val()); 1796fe7fe6603fea2ca121282cd329f5300ea7d9fd51David Greene DEBUG(dbgs() << "\n"); 179778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner } 17986f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 179978c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // PredBB no longer jumps to BB, remove entries in the PHI node for the edge 180078c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // that we nuked. 180136c4debc94e7c68c769dfda781851a0c963dd746Owen Anderson BB->removePredecessor(PredBB, true); 18026f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 180378c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner // Remove the unconditional branch at the end of the PredBB block. 180478c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner OldPredBranch->eraseFromParent(); 18056f9a8307e087110d57b1e63dae154d351f6b0f6bFrits van Bommel 180678c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner ++NumDupes; 180778c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner return true; 180878c552ef309eb8c47d12aac2c0b3af7849c27ce9Chris Lattner} 1809c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1810c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// TryToUnfoldSelect - Look for blocks of the form 1811c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// bb1: 1812c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// %a = select 1813c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// br bb 1814c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// 1815c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// bb2: 1816c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// %p = phi [%a, %bb] ... 1817c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// %c = icmp %p 1818c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// br i1 %c 1819c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// 1820c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// And expand the select into a branch structure if one of its arms allows %c 1821c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer/// to be folded. This later enables threading from bb1 over bb2. 1822c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramerbool JumpThreading::TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { 1823c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator()); 1824c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer PHINode *CondLHS = dyn_cast<PHINode>(CondCmp->getOperand(0)); 1825c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer Constant *CondRHS = cast<Constant>(CondCmp->getOperand(1)); 1826c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1827c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if (!CondBr || !CondBr->isConditional() || !CondLHS || 1828c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer CondLHS->getParent() != BB) 1829c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer return false; 1830c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1831c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer for (unsigned I = 0, E = CondLHS->getNumIncomingValues(); I != E; ++I) { 1832c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BasicBlock *Pred = CondLHS->getIncomingBlock(I); 1833c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer SelectInst *SI = dyn_cast<SelectInst>(CondLHS->getIncomingValue(I)); 1834c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1835c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Look if one of the incoming values is a select in the corresponding 1836c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // predecessor. 1837c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if (!SI || SI->getParent() != Pred || !SI->hasOneUse()) 1838c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer continue; 1839c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1840c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BranchInst *PredTerm = dyn_cast<BranchInst>(Pred->getTerminator()); 1841c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if (!PredTerm || !PredTerm->isUnconditional()) 1842c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer continue; 1843c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1844c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Now check if one of the select values would allow us to constant fold the 1845c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // terminator in BB. We don't do the transform if both sides fold, those 1846c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // cases will be threaded in any case. 1847c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer LazyValueInfo::Tristate LHSFolds = 1848c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(1), 184937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CondRHS, Pred, BB, CondCmp); 1850c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer LazyValueInfo::Tristate RHSFolds = 1851c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer LVI->getPredicateOnEdge(CondCmp->getPredicate(), SI->getOperand(2), 185237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines CondRHS, Pred, BB, CondCmp); 1853c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if ((LHSFolds != LazyValueInfo::Unknown || 1854c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer RHSFolds != LazyValueInfo::Unknown) && 1855c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer LHSFolds != RHSFolds) { 1856c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Expand the select. 1857c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // 1858c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Pred -- 1859c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // | v 1860c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // | NewBB 1861c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // | | 1862c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // |----- 1863c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // v 1864c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // BB 1865c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "select.unfold", 1866c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BB->getParent(), BB); 1867c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Move the unconditional branch to NewBB. 1868c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer PredTerm->removeFromParent(); 1869c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer NewBB->getInstList().insert(NewBB->end(), PredTerm); 1870c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Create a conditional branch and update PHI nodes. 1871c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer BranchInst::Create(NewBB, BB, SI->getCondition(), Pred); 1872c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer CondLHS->setIncomingValue(I, SI->getFalseValue()); 1873c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer CondLHS->addIncoming(SI->getTrueValue(), NewBB); 1874c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // The select is now dead. 1875c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer SI->eraseFromParent(); 1876c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer 1877c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer // Update any other PHI nodes in BB. 1878c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer for (BasicBlock::iterator BI = BB->begin(); 1879c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer PHINode *Phi = dyn_cast<PHINode>(BI); ++BI) 1880c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer if (Phi != CondLHS) 1881c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB); 1882c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer return true; 1883c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer } 1884c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer } 1885c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer return false; 1886c11b107f21f8f1baf1021999fc7d01b93e00922bBenjamin Kramer} 1887