blob: b9b828dde09a77c21322d60eb4beea29d5c4d3f5 [file] [log] [blame]
Owen Anderson871d8eb2008-05-29 08:45:13 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Owen Anderson871d8eb2008-05-29 08:45:13 +000010// This file implements the Aggressive Dead Code Elimination pass. This pass
11// optimistically assumes that all instructions are dead until proven otherwise,
12// allowing it to eliminate dead computations that other DCE passes do not
13// catch, particularly involving loop computations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "adce"
18#include "llvm/Transforms/Scalar.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Instructions.h"
Owen Anderson871d8eb2008-05-29 08:45:13 +000020#include "llvm/Pass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Support/Compiler.h"
Owen Anderson871d8eb2008-05-29 08:45:13 +000022#include "llvm/Support/InstIterator.h"
23#include "llvm/ADT/Statistic.h"
Owen Andersona8745252008-07-02 17:32:04 +000024#include "llvm/ADT/DenseSet.h"
Owen Anderson40e5a552008-06-23 06:13:12 +000025#include "llvm/ADT/SmallVector.h"
Owen Anderson871d8eb2008-05-29 08:45:13 +000026
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
Owen Anderson871d8eb2008-05-29 08:45:13 +000029STATISTIC(NumRemoved, "Number of instructions removed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
31namespace {
Owen Anderson871d8eb2008-05-29 08:45:13 +000032 struct VISIBILITY_HIDDEN ADCE : public FunctionPass {
33 static char ID; // Pass identification, replacement for typeid
34 ADCE() : FunctionPass((intptr_t)&ID) {}
35
Owen Andersona8745252008-07-02 17:32:04 +000036 DenseSet<Instruction*> alive;
Owen Anderson40e5a552008-06-23 06:13:12 +000037 SmallVector<Instruction*, 1024> worklist;
38
Owen Anderson871d8eb2008-05-29 08:45:13 +000039 virtual bool runOnFunction(Function& F);
40
41 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
42 AU.setPreservesCFG();
43 }
44
45 };
46}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Dan Gohman089efff2008-05-13 00:00:25 +000048char ADCE::ID = 0;
49static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
50
Owen Anderson871d8eb2008-05-29 08:45:13 +000051bool ADCE::runOnFunction(Function& F) {
Owen Anderson40e5a552008-06-23 06:13:12 +000052 alive.clear();
53 worklist.clear();
Owen Anderson8cf41ad2008-05-16 04:34:51 +000054
Owen Anderson871d8eb2008-05-29 08:45:13 +000055 // Collect the set of "root" instructions that are known live.
56 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
57 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
58 I->mayWriteToMemory()) {
59 alive.insert(I.getInstructionIterator());
60 worklist.push_back(I.getInstructionIterator());
Owen Anderson8cf41ad2008-05-16 04:34:51 +000061 }
Owen Anderson871d8eb2008-05-29 08:45:13 +000062
63 // Propagate liveness backwards to operands.
64 while (!worklist.empty()) {
65 Instruction* curr = worklist.back();
66 worklist.pop_back();
Owen Anderson8cf41ad2008-05-16 04:34:51 +000067
Owen Anderson871d8eb2008-05-29 08:45:13 +000068 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
69 OI != OE; ++OI)
70 if (Instruction* Inst = dyn_cast<Instruction>(OI))
71 if (alive.insert(Inst))
72 worklist.push_back(Inst);
73 }
74
75 // The inverse of the live set is the dead set. These are those instructions
76 // which have no side effects and do not influence the control flow or return
77 // value of the function, and may therefore be deleted safely.
Owen Anderson40e5a552008-06-23 06:13:12 +000078 // NOTE: We reuse the worklist vector here for memory efficiency.
Owen Anderson871d8eb2008-05-29 08:45:13 +000079 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
80 if (!alive.count(I.getInstructionIterator())) {
Owen Anderson40e5a552008-06-23 06:13:12 +000081 worklist.push_back(I.getInstructionIterator());
Owen Anderson871d8eb2008-05-29 08:45:13 +000082 I->dropAllReferences();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 }
Owen Anderson8cf41ad2008-05-16 04:34:51 +000084
Owen Anderson40e5a552008-06-23 06:13:12 +000085 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
86 E = worklist.end(); I != E; ++I) {
Owen Anderson871d8eb2008-05-29 08:45:13 +000087 NumRemoved++;
88 (*I)->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 }
Owen Anderson871d8eb2008-05-29 08:45:13 +000090
Owen Anderson40e5a552008-06-23 06:13:12 +000091 return !worklist.empty();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092}
Owen Anderson871d8eb2008-05-29 08:45:13 +000093
94FunctionPass *llvm::createAggressiveDCEPass() {
95 return new ADCE();
Duncan Sandsb7581232008-05-29 14:38:23 +000096}