Simplify code a bit, and use alias analysis to allow us to delete unused

call and invoke instructions that are known to not write to memory.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12807 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-04-10 06:53:09 +00:00
parent dc4736f8e6
commit ede6ac60a5

View File

@ -14,11 +14,11 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Type.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/Constant.h" #include "llvm/Constant.h"
#include "llvm/Instructions.h"
#include "llvm/Type.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/Local.h"
@ -33,6 +33,7 @@ using namespace llvm;
namespace { namespace {
Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed"); Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
Statistic<> NumInstRemoved ("adce", "Number of instructions removed"); Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
Statistic<> NumCallRemoved ("adce", "Number of calls and invokes removed");
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// ADCE Class // ADCE Class
@ -42,6 +43,7 @@ namespace {
// //
class ADCE : public FunctionPass { class ADCE : public FunctionPass {
Function *Func; // The function that we are working on Function *Func; // The function that we are working on
AliasAnalysis *AA; // Current AliasAnalysis object
std::vector<Instruction*> WorkList; // Instructions that just became live std::vector<Instruction*> WorkList; // Instructions that just became live
std::set<Instruction*> LiveSet; // The set of live instructions std::set<Instruction*> LiveSet; // The set of live instructions
@ -53,6 +55,7 @@ public:
// //
virtual bool runOnFunction(Function &F) { virtual bool runOnFunction(Function &F) {
Func = &F; Func = &F;
AA = &getAnalysis<AliasAnalysis>();
bool Changed = doADCE(); bool Changed = doADCE();
assert(WorkList.empty()); assert(WorkList.empty());
LiveSet.clear(); LiveSet.clear();
@ -64,6 +67,7 @@ public:
// We require that all function nodes are unified, because otherwise code // We require that all function nodes are unified, because otherwise code
// can be marked live that wouldn't necessarily be otherwise. // can be marked live that wouldn't necessarily be otherwise.
AU.addRequired<UnifyFunctionExitNodes>(); AU.addRequired<UnifyFunctionExitNodes>();
AU.addRequired<AliasAnalysis>();
AU.addRequired<PostDominatorTree>(); AU.addRequired<PostDominatorTree>();
AU.addRequired<PostDominanceFrontier>(); AU.addRequired<PostDominanceFrontier>();
} }
@ -189,16 +193,43 @@ bool ADCE::doADCE() {
BBI != BBE; ++BBI) { BBI != BBE; ++BBI) {
BasicBlock *BB = *BBI; BasicBlock *BB = *BBI;
for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
if (II->mayWriteToMemory() || isa<ReturnInst>(II) || isa<UnwindInst>(II)){ Instruction *I = II++;
markInstructionLive(II); if (CallInst *CI = dyn_cast<CallInst>(I)) {
++II; // Increment the inst iterator if the inst wasn't deleted Function *F = CI->getCalledFunction();
} else if (isInstructionTriviallyDead(II)) { if (F && AA->onlyReadsMemory(F)) {
// Remove the instruction from it's basic block... if (CI->use_empty()) {
II = BB->getInstList().erase(II); BB->getInstList().erase(CI);
++NumInstRemoved; ++NumCallRemoved;
MadeChanges = true; }
} else { } else {
++II; // Increment the inst iterator if the inst wasn't deleted markInstructionLive(I);
}
} else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
Function *F = II->getCalledFunction();
if (F && AA->onlyReadsMemory(F)) {
// The function cannot unwind. Convert it to a call with a branch
// after it to the normal destination.
std::vector<Value*> Args(II->op_begin()+1, II->op_end());
std::string Name = II->getName(); II->setName("");
Instruction *NewCall = new CallInst(F, Args, Name, II);
II->replaceAllUsesWith(NewCall);
new BranchInst(II->getNormalDest(), II);
BB->getInstList().erase(II);
if (NewCall->use_empty()) {
BB->getInstList().erase(NewCall);
++NumCallRemoved;
}
} else {
markInstructionLive(I);
}
} else if (I->mayWriteToMemory() || isa<ReturnInst>(I) ||
isa<UnwindInst>(I)) {
markInstructionLive(I);
} else if (isInstructionTriviallyDead(I)) {
// Remove the instruction from it's basic block...
BB->getInstList().erase(I);
++NumInstRemoved;
} }
} }
} }