diff options
Diffstat (limited to 'contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp')
| -rw-r--r-- | contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp | 93 | 
1 files changed, 50 insertions, 43 deletions
diff --git a/contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp index b3ec2fc84c03..21f80385cf46 100644 --- a/contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/contrib/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -11,7 +11,6 @@  //  //===----------------------------------------------------------------------===// -#define DEBUG_TYPE "jump-threading"  #include "llvm/Transforms/Scalar.h"  #include "llvm/ADT/DenseMap.h"  #include "llvm/ADT/DenseSet.h" @@ -27,10 +26,10 @@  #include "llvm/IR/DataLayout.h"  #include "llvm/IR/IntrinsicInst.h"  #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/ValueHandle.h"  #include "llvm/Pass.h"  #include "llvm/Support/CommandLine.h"  #include "llvm/Support/Debug.h" -#include "llvm/Support/ValueHandle.h"  #include "llvm/Support/raw_ostream.h"  #include "llvm/Target/TargetLibraryInfo.h"  #include "llvm/Transforms/Utils/BasicBlockUtils.h" @@ -38,6 +37,8 @@  #include "llvm/Transforms/Utils/SSAUpdater.h"  using namespace llvm; +#define DEBUG_TYPE "jump-threading" +  STATISTIC(NumThreads, "Number of jumps threaded");  STATISTIC(NumFolds,   "Number of terminators folded");  STATISTIC(NumDupes,   "Number of branch blocks duplicated to eliminate phi"); @@ -76,7 +77,7 @@ namespace {    /// revectored to the false side of the second if.    ///    class JumpThreading : public FunctionPass { -    DataLayout *TD; +    const DataLayout *DL;      TargetLibraryInfo *TLI;      LazyValueInfo *LVI;  #ifdef NDEBUG @@ -105,9 +106,9 @@ namespace {        initializeJumpThreadingPass(*PassRegistry::getPassRegistry());      } -    bool runOnFunction(Function &F); +    bool runOnFunction(Function &F) override; -    virtual void getAnalysisUsage(AnalysisUsage &AU) const { +    void getAnalysisUsage(AnalysisUsage &AU) const override {        AU.addRequired<LazyValueInfo>();        AU.addPreserved<LazyValueInfo>();        AU.addRequired<TargetLibraryInfo>(); @@ -148,11 +149,24 @@ FunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }  /// runOnFunction - Top level algorithm.  ///  bool JumpThreading::runOnFunction(Function &F) { +  if (skipOptnoneFunction(F)) +    return false; +    DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n"); -  TD = getAnalysisIfAvailable<DataLayout>(); +  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); +  DL = DLP ? &DLP->getDataLayout() : nullptr;    TLI = &getAnalysis<TargetLibraryInfo>();    LVI = &getAnalysis<LazyValueInfo>(); +  // Remove unreachable blocks from function as they may result in infinite +  // loop. We do threading if we found something profitable. Jump threading a +  // branch can create other opportunities. If these opportunities form a cycle +  // i.e. if any jump treading is undoing previous threading in the path, then +  // we will loop forever. We take care of this issue by not jump threading for +  // back edges. This works for normal cases but not for unreachable blocks as +  // they may have cycle with no back edge. +  removeUnreachableBlocks(F); +    FindLoopHeaders(F);    bool Changed, EverChanged = false; @@ -251,7 +265,7 @@ static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB,      // as having cost of 2 total, and if they are a vector intrinsic, we model      // them as having cost 1.      if (const CallInst *CI = dyn_cast<CallInst>(I)) { -      if (CI->hasFnAttr(Attribute::NoDuplicate)) +      if (CI->cannotDuplicate())          // Blocks with NoDuplicate are modelled as having infinite cost, so they          // are never duplicated.          return ~0U; @@ -304,7 +318,7 @@ void JumpThreading::FindLoopHeaders(Function &F) {  /// Returns null if Val is null or not an appropriate constant.  static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) {    if (!Val) -    return 0; +    return nullptr;    // Undef is "known" enough.    if (UndefValue *U = dyn_cast<UndefValue>(Val)) @@ -348,7 +362,7 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,    // If V is a non-instruction value, or an instruction in a different block,    // then it can't be derived from a PHI.    Instruction *I = dyn_cast<Instruction>(V); -  if (I == 0 || I->getParent() != BB) { +  if (!I || I->getParent() != BB) {      // Okay, if this is a live-in value, see if it has a known value at the end      // of any of our predecessors. @@ -490,8 +504,8 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,          Value *LHS = PN->getIncomingValue(i);          Value *RHS = Cmp->getOperand(1)->DoPHITranslation(BB, PredBB); -        Value *Res = SimplifyCmpInst(Cmp->getPredicate(), LHS, RHS, TD); -        if (Res == 0) { +        Value *Res = SimplifyCmpInst(Cmp->getPredicate(), LHS, RHS, DL); +        if (!Res) {            if (!isa<Constant>(RHS))              continue; @@ -577,7 +591,7 @@ ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,            // Either operand will do, so be sure to pick the one that's a known            // constant.            // FIXME: Do this more cleverly if both values are known constants? -          KnownCond = (TrueVal != 0); +          KnownCond = (TrueVal != nullptr);          }          // See if the select has a known constant value for this predecessor. @@ -655,14 +669,9 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) {        if (LoopHeaders.erase(SinglePred))          LoopHeaders.insert(BB); -      // Remember if SinglePred was the entry block of the function.  If so, we -      // will need to move BB back to the entry position. -      bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();        LVI->eraseBlock(SinglePred);        MergeBasicBlockIntoOnlyPred(BB); -      if (isEntry && BB != &BB->getParent()->getEntryBlock()) -        BB->moveBefore(&BB->getParent()->getEntryBlock());        return true;      }    } @@ -692,7 +701,7 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) {    // Run constant folding to see if we can reduce the condition to a simple    // constant.    if (Instruction *I = dyn_cast<Instruction>(Condition)) { -    Value *SimpleVal = ConstantFoldInstruction(I, TD, TLI); +    Value *SimpleVal = ConstantFoldInstruction(I, DL, TLI);      if (SimpleVal) {        I->replaceAllUsesWith(SimpleVal);        I->eraseFromParent(); @@ -733,7 +742,7 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) {    Instruction *CondInst = dyn_cast<Instruction>(Condition);    // All the rest of our checks depend on the condition being an instruction. -  if (CondInst == 0) { +  if (!CondInst) {      // FIXME: Unify this with code below.      if (ProcessThreadableEdges(Condition, BB, Preference))        return true; @@ -886,7 +895,7 @@ bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {    SmallPtrSet<BasicBlock*, 8> PredsScanned;    typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy;    AvailablePredsTy AvailablePreds; -  BasicBlock *OneUnavailablePred = 0; +  BasicBlock *OneUnavailablePred = nullptr;    // If we got here, the loaded value is transparent through to the start of the    // block.  Check to see if it is available in any of the predecessor blocks. @@ -900,16 +909,16 @@ bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {      // Scan the predecessor to see if the value is available in the pred.      BBIt = PredBB->end(); -    MDNode *ThisTBAATag = 0; +    MDNode *ThisTBAATag = nullptr;      Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6, -                                                    0, &ThisTBAATag); +                                                    nullptr, &ThisTBAATag);      if (!PredAvailable) {        OneUnavailablePred = PredBB;        continue;      }      // If tbaa tags disagree or are not present, forget about them. -    if (TBAATag != ThisTBAATag) TBAATag = 0; +    if (TBAATag != ThisTBAATag) TBAATag = nullptr;      // If so, this load is partially redundant.  Remember this info so that we      // can create a PHI node. @@ -925,7 +934,7 @@ bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {    // predecessor, we want to insert a merge block for those common predecessors.    // This ensures that we only have to insert one reload, thus not increasing    // code size. -  BasicBlock *UnavailablePred = 0; +  BasicBlock *UnavailablePred = nullptr;    // If there is exactly one predecessor where the value is unavailable, the    // already computed 'OneUnavailablePred' block is it.  If it ends in an @@ -992,7 +1001,7 @@ bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {      BasicBlock *P = *PI;      AvailablePredsTy::iterator I =        std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(), -                       std::make_pair(P, (Value*)0)); +                       std::make_pair(P, (Value*)nullptr));      assert(I != AvailablePreds.end() && I->first == P &&             "Didn't find entry for predecessor!"); @@ -1099,7 +1108,7 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,    SmallPtrSet<BasicBlock*, 16> SeenPreds;    SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList; -  BasicBlock *OnlyDest = 0; +  BasicBlock *OnlyDest = nullptr;    BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL;    for (unsigned i = 0, e = PredValues.size(); i != e; ++i) { @@ -1116,7 +1125,7 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,      BasicBlock *DestBB;      if (isa<UndefValue>(Val)) -      DestBB = 0; +      DestBB = nullptr;      else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))        DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero());      else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { @@ -1167,7 +1176,7 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,    // If the threadable edges are branching on an undefined value, we get to pick    // the destination that these predecessors should get to. -  if (MostPopularDest == 0) +  if (!MostPopularDest)      MostPopularDest = BB->getTerminator()->                              getSuccessor(GetBestDestForJumpOnUndef(BB)); @@ -1269,7 +1278,7 @@ bool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) {    }    // Determine which value to split on, true, false, or undef if neither. -  ConstantInt *SplitVal = 0; +  ConstantInt *SplitVal = nullptr;    if (NumTrue > NumFalse)      SplitVal = ConstantInt::getTrue(BB->getContext());    else if (NumTrue != 0 || NumFalse != 0) @@ -1290,7 +1299,7 @@ bool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) {    // help us.  However, we can just replace the LHS or RHS with the constant.    if (BlocksToFoldInto.size() ==        cast<PHINode>(BB->front()).getNumIncomingValues()) { -    if (SplitVal == 0) { +    if (!SplitVal) {        // If all preds provide undef, just nuke the xor, because it is undef too.        BO->replaceAllUsesWith(UndefValue::get(BO->getType()));        BO->eraseFromParent(); @@ -1431,16 +1440,15 @@ bool JumpThreading::ThreadEdge(BasicBlock *BB,    for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {      // Scan all uses of this instruction to see if it is used outside of its      // block, and if so, record them in UsesToRename. -    for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; -         ++UI) { -      Instruction *User = cast<Instruction>(*UI); +    for (Use &U : I->uses()) { +      Instruction *User = cast<Instruction>(U.getUser());        if (PHINode *UserPN = dyn_cast<PHINode>(User)) { -        if (UserPN->getIncomingBlock(UI) == BB) +        if (UserPN->getIncomingBlock(U) == BB)            continue;        } else if (User->getParent() == BB)          continue; -      UsesToRename.push_back(&UI.getUse()); +      UsesToRename.push_back(&U);      }      // If there are no uses outside the block, we're done with this instruction. @@ -1475,7 +1483,7 @@ bool JumpThreading::ThreadEdge(BasicBlock *BB,    // At this point, the IR is fully up to date and consistent.  Do a quick scan    // over the new instructions and zap any that are constants or dead.  This    // frequently happens because of phi translation. -  SimplifyInstructionsInBlock(NewBB, TD, TLI); +  SimplifyInstructionsInBlock(NewBB, DL, TLI);    // Threaded an edge!    ++NumThreads; @@ -1528,7 +1536,7 @@ bool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,    // can just clone the bits from BB into the end of the new PredBB.    BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator()); -  if (OldPredBranch == 0 || !OldPredBranch->isUnconditional()) { +  if (!OldPredBranch || !OldPredBranch->isUnconditional()) {      PredBB = SplitEdge(PredBB, BB, this);      OldPredBranch = cast<BranchInst>(PredBB->getTerminator());    } @@ -1557,7 +1565,7 @@ bool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,      // If this instruction can be simplified after the operands are updated,      // just use the simplified value instead.  This frequently happens due to      // phi translation. -    if (Value *IV = SimplifyInstruction(New, TD)) { +    if (Value *IV = SimplifyInstruction(New, DL)) {        delete New;        ValueMapping[BI] = IV;      } else { @@ -1585,16 +1593,15 @@ bool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,    for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {      // Scan all uses of this instruction to see if it is used outside of its      // block, and if so, record them in UsesToRename. -    for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; -         ++UI) { -      Instruction *User = cast<Instruction>(*UI); +    for (Use &U : I->uses()) { +      Instruction *User = cast<Instruction>(U.getUser());        if (PHINode *UserPN = dyn_cast<PHINode>(User)) { -        if (UserPN->getIncomingBlock(UI) == BB) +        if (UserPN->getIncomingBlock(U) == BB)            continue;        } else if (User->getParent() == BB)          continue; -      UsesToRename.push_back(&UI.getUse()); +      UsesToRename.push_back(&U);      }      // If there are no uses outside the block, we're done with this instruction.  | 
