aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms/Scalar/ADCE.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-04-10 06:53:09 +0000
committerChris Lattner <sabre@nondot.org>2004-04-10 06:53:09 +0000
commitede6ac60a542f597c2659b138ffd0918cc8e8f47 (patch)
treeb6a354c6416ae59fd5c78c720a0b9197a8ce5f5c /lib/Transforms/Scalar/ADCE.cpp
parentdc4736f8e60376f2edb698cbd43bb0be7ffde680 (diff)
downloadexternal_llvm-ede6ac60a542f597c2659b138ffd0918cc8e8f47.zip
external_llvm-ede6ac60a542f597c2659b138ffd0918cc8e8f47.tar.gz
external_llvm-ede6ac60a542f597c2659b138ffd0918cc8e8f47.tar.bz2
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
Diffstat (limited to 'lib/Transforms/Scalar/ADCE.cpp')
-rw-r--r--lib/Transforms/Scalar/ADCE.cpp53
1 files changed, 42 insertions, 11 deletions
diff --git a/lib/Transforms/Scalar/ADCE.cpp b/lib/Transforms/Scalar/ADCE.cpp
index 0bced41..afc24f9 100644
--- a/lib/Transforms/Scalar/ADCE.cpp
+++ b/lib/Transforms/Scalar/ADCE.cpp
@@ -14,11 +14,11 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar.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/iTerminators.h"
-#include "llvm/iPHINode.h"
-#include "llvm/Constant.h"
#include "llvm/Support/CFG.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
@@ -33,6 +33,7 @@ using namespace llvm;
namespace {
Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
+ Statistic<> NumCallRemoved ("adce", "Number of calls and invokes removed");
//===----------------------------------------------------------------------===//
// ADCE Class
@@ -42,6 +43,7 @@ namespace {
//
class ADCE : public FunctionPass {
Function *Func; // The function that we are working on
+ AliasAnalysis *AA; // Current AliasAnalysis object
std::vector<Instruction*> WorkList; // Instructions that just became live
std::set<Instruction*> LiveSet; // The set of live instructions
@@ -53,6 +55,7 @@ public:
//
virtual bool runOnFunction(Function &F) {
Func = &F;
+ AA = &getAnalysis<AliasAnalysis>();
bool Changed = doADCE();
assert(WorkList.empty());
LiveSet.clear();
@@ -64,6 +67,7 @@ public:
// We require that all function nodes are unified, because otherwise code
// can be marked live that wouldn't necessarily be otherwise.
AU.addRequired<UnifyFunctionExitNodes>();
+ AU.addRequired<AliasAnalysis>();
AU.addRequired<PostDominatorTree>();
AU.addRequired<PostDominanceFrontier>();
}
@@ -189,16 +193,43 @@ bool ADCE::doADCE() {
BBI != BBE; ++BBI) {
BasicBlock *BB = *BBI;
for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
- if (II->mayWriteToMemory() || isa<ReturnInst>(II) || isa<UnwindInst>(II)){
- markInstructionLive(II);
- ++II; // Increment the inst iterator if the inst wasn't deleted
- } else if (isInstructionTriviallyDead(II)) {
+ Instruction *I = II++;
+ if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ Function *F = CI->getCalledFunction();
+ if (F && AA->onlyReadsMemory(F)) {
+ if (CI->use_empty()) {
+ BB->getInstList().erase(CI);
+ ++NumCallRemoved;
+ }
+ } else {
+ 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...
- II = BB->getInstList().erase(II);
+ BB->getInstList().erase(I);
++NumInstRemoved;
- MadeChanges = true;
- } else {
- ++II; // Increment the inst iterator if the inst wasn't deleted
}
}
}