diff options
| author | Chris Lattner <sabre@nondot.org> | 2009-11-02 02:47:51 +0000 | 
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2009-11-02 02:47:51 +0000 | 
| commit | e405ed9651ac4e6b2548af7b28975c3841131edc (patch) | |
| tree | cdfc108b774b24246470e014750fb7f6fb16c62b | |
| parent | a3c39d394dea7a2d7f9f4fac3f94d7da3ee9e093 (diff) | |
| download | bcm5719-llvm-e405ed9651ac4e6b2548af7b28975c3841131edc.tar.gz bcm5719-llvm-e405ed9651ac4e6b2548af7b28975c3841131edc.zip | |
factor duplicated code into a new DeleteInstructionInBlock
function, eliminate temporary (and pointless) smallvector.
llvm-svn: 85776
| -rw-r--r-- | llvm/lib/Transforms/Scalar/SCCP.cpp | 172 | 
1 files changed, 80 insertions, 92 deletions
| diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp index 1c227be8d93..7421d4f349b 100644 --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -47,7 +47,6 @@ STATISTIC(NumInstRemoved, "Number of instructions removed");  STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");  STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP"); -STATISTIC(IPNumDeadBlocks , "Number of basic blocks unreachable by IPSCCP");  STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");  STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP"); @@ -136,6 +135,10 @@ public:      Val.setPointer(V);    }  }; +} // end anonymous namespace. + + +namespace {  //===----------------------------------------------------------------------===//  // @@ -380,7 +383,6 @@ private:    // visit implementations - Something changed in this instruction.  Either an    // operand made a transition, or the instruction is newly executable.  Change    // the value type of I to reflect these changes if appropriate. -  //    void visitPHINode(PHINode &I);    // Terminators @@ -1566,6 +1568,21 @@ FunctionPass *llvm::createSCCPPass() {    return new SCCP();  } +static void DeleteInstructionInBlock(BasicBlock *BB) { +  DEBUG(errs() << "  BasicBlock Dead:" << *BB); +  ++NumDeadBlocks; +   +  // Delete the instructions backwards, as it has a reduced likelihood of +  // having to update as many def-use and use-def chains. +  while (!isa<TerminatorInst>(BB->begin())) { +    Instruction *I = --BasicBlock::iterator(BB->getTerminator()); +     +    if (!I->use_empty()) +      I->replaceAllUsesWith(UndefValue::get(I->getType())); +    BB->getInstList().erase(I); +    ++NumInstRemoved; +  } +}  // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,  // and return true if the function was modified. @@ -1595,56 +1612,42 @@ bool SCCP::runOnFunction(Function &F) {    // delete their contents now.  Note that we cannot actually delete the blocks,    // as we cannot modify the CFG of the function.    // -  SmallVector<Instruction*, 512> Insts;    std::map<Value*, LatticeVal> &Values = Solver.getValueMapping(); -  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) +  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {      if (!Solver.isBlockExecutable(BB)) { -      DEBUG(errs() << "  BasicBlock Dead:" << *BB); -      ++NumDeadBlocks; - -      // Delete the instructions backwards, as it has a reduced likelihood of -      // having to update as many def-use and use-def chains. -      for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator(); -           I != E; ++I) -        Insts.push_back(I); -      while (!Insts.empty()) { -        Instruction *I = Insts.back(); -        Insts.pop_back(); -        if (!I->use_empty()) -          I->replaceAllUsesWith(UndefValue::get(I->getType())); -        BB->getInstList().erase(I); -        MadeChanges = true; -        ++NumInstRemoved; -      } -    } else { -      // Iterate over all of the instructions in a function, replacing them with -      // constants if we have found them to be of constant values. -      // -      for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { -        Instruction *Inst = BI++; -        if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst)) -          continue; -         -        LatticeVal &IV = Values[Inst]; -        if (!IV.isConstant() && !IV.isUndefined()) -          continue; -         -        Constant *Const = IV.isConstant() -          ? IV.getConstant() : UndefValue::get(Inst->getType()); -        DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst); +      DeleteInstructionInBlock(BB); +      MadeChanges = true; +      continue; +    } +   +    // Iterate over all of the instructions in a function, replacing them with +    // constants if we have found them to be of constant values. +    // +    for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { +      Instruction *Inst = BI++; +      if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst)) +        continue; +       +      LatticeVal &IV = Values[Inst]; +      if (!IV.isConstant() && !IV.isUndefined()) +        continue; +       +      Constant *Const = IV.isConstant() +        ? IV.getConstant() : UndefValue::get(Inst->getType()); +      DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst); -        // Replaces all of the uses of a variable with uses of the constant. -        Inst->replaceAllUsesWith(Const); -         -        // Delete the instruction. -        Inst->eraseFromParent(); -         -        // Hey, we just changed something! -        MadeChanges = true; -        ++NumInstRemoved; -      } +      // Replaces all of the uses of a variable with uses of the constant. +      Inst->replaceAllUsesWith(Const); +       +      // Delete the instruction. +      Inst->eraseFromParent(); +       +      // Hey, we just changed something! +      MadeChanges = true; +      ++NumInstRemoved;      } +  }    return MadeChanges;  } @@ -1738,7 +1741,6 @@ bool IPSCCP::runOnModule(Module &M) {    // Iterate over all of the instructions in the module, replacing them with    // constants if we have found them to be of constant values.    // -  SmallVector<Instruction*, 512> Insts;    SmallVector<BasicBlock*, 512> BlocksToErase;    std::map<Value*, LatticeVal> &Values = Solver.getValueMapping(); @@ -1759,27 +1761,12 @@ bool IPSCCP::runOnModule(Module &M) {          }        } -    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) +    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {        if (!Solver.isBlockExecutable(BB)) { -        DEBUG(errs() << "  BasicBlock Dead:" << *BB); -        ++IPNumDeadBlocks; +        DeleteInstructionInBlock(BB); +        MadeChanges = true; -        // Delete the instructions backwards, as it has a reduced likelihood of -        // having to update as many def-use and use-def chains.          TerminatorInst *TI = BB->getTerminator(); -        for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I) -          Insts.push_back(I); - -        while (!Insts.empty()) { -          Instruction *I = Insts.back(); -          Insts.pop_back(); -          if (!I->use_empty()) -            I->replaceAllUsesWith(UndefValue::get(I->getType())); -          BB->getInstList().erase(I); -          MadeChanges = true; -          ++IPNumInstRemoved; -        } -          for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {            BasicBlock *Succ = TI->getSuccessor(i);            if (!Succ->empty() && isa<PHINode>(Succ->begin())) @@ -1787,40 +1774,41 @@ bool IPSCCP::runOnModule(Module &M) {          }          if (!TI->use_empty())            TI->replaceAllUsesWith(UndefValue::get(TI->getType())); -        BB->getInstList().erase(TI); +        TI->eraseFromParent();          if (&*BB != &F->front())            BlocksToErase.push_back(BB);          else            new UnreachableInst(M.getContext(), BB); +        continue; +      } +       +      for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { +        Instruction *Inst = BI++; +        if (Inst->getType()->isVoidTy()) +          continue; +         +        LatticeVal &IV = Values[Inst]; +        if (!IV.isConstant() && !IV.isUndefined()) +          continue; +         +        Constant *Const = IV.isConstant() +          ? IV.getConstant() : UndefValue::get(Inst->getType()); +        DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst); -      } else { -        for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { -          Instruction *Inst = BI++; -          if (Inst->getType()->isVoidTy()) -            continue; -           -          LatticeVal &IV = Values[Inst]; -          if (!IV.isConstant() && !IV.isUndefined()) -            continue; -           -          Constant *Const = IV.isConstant() -            ? IV.getConstant() : UndefValue::get(Inst->getType()); -          DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst); - -          // Replaces all of the uses of a variable with uses of the -          // constant. -          Inst->replaceAllUsesWith(Const); -           -          // Delete the instruction. -          if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst)) -            Inst->eraseFromParent(); +        // Replaces all of the uses of a variable with uses of the +        // constant. +        Inst->replaceAllUsesWith(Const); +         +        // Delete the instruction. +        if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst)) +          Inst->eraseFromParent(); -          // Hey, we just changed something! -          MadeChanges = true; -          ++IPNumInstRemoved; -        } +        // Hey, we just changed something! +        MadeChanges = true; +        ++IPNumInstRemoved;        } +    }      // Now that all instructions in the function are constant folded, erase dead      // blocks, because we can now use ConstantFoldTerminator to get rid of | 

