blob: c698829f5b44cc7dcd8e0728c3e20afcdc787300 [file] [log] [blame]
Owen Anderson038a8742008-05-29 08:45:13 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner02e90d52001-06-30 06:39:11 +00009//
Owen Anderson038a8742008-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.
Chris Lattner02e90d52001-06-30 06:39:11 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner0e5f4992006-12-19 21:40:18 +000017#define DEBUG_TYPE "adce"
Chris Lattner022103b2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattnerede6ac62004-04-10 06:53:09 +000019#include "llvm/Instructions.h"
Owen Anderson038a8742008-05-29 08:45:13 +000020#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000021#include "llvm/Support/Compiler.h"
Owen Anderson038a8742008-05-29 08:45:13 +000022#include "llvm/Support/InstIterator.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/SmallPtrSet.h"
25
Chris Lattnerbd1a90e2003-12-19 09:08:34 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Owen Anderson038a8742008-05-29 08:45:13 +000028STATISTIC(NumRemoved, "Number of instructions removed");
Chris Lattnerdfe81ab2002-05-06 17:27:57 +000029
Chris Lattner0e5f4992006-12-19 21:40:18 +000030namespace {
Owen Anderson038a8742008-05-29 08:45:13 +000031 struct VISIBILITY_HIDDEN ADCE : public FunctionPass {
32 static char ID; // Pass identification, replacement for typeid
33 ADCE() : FunctionPass((intptr_t)&ID) {}
34
35 virtual bool runOnFunction(Function& F);
36
37 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
38 AU.setPreservesCFG();
39 }
40
41 };
42}
Chris Lattnerdfe81ab2002-05-06 17:27:57 +000043
Dan Gohman844731a2008-05-13 00:00:25 +000044char ADCE::ID = 0;
45static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
46
Owen Anderson038a8742008-05-29 08:45:13 +000047bool ADCE::runOnFunction(Function& F) {
48 SmallPtrSet<Instruction*, 32> alive;
49 std::vector<Instruction*> worklist;
Owen Anderson62849be2008-05-16 04:34:51 +000050
Owen Anderson038a8742008-05-29 08:45:13 +000051 // Collect the set of "root" instructions that are known live.
52 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
53 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
54 I->mayWriteToMemory()) {
55 alive.insert(I.getInstructionIterator());
56 worklist.push_back(I.getInstructionIterator());
Owen Anderson62849be2008-05-16 04:34:51 +000057 }
Owen Anderson038a8742008-05-29 08:45:13 +000058
59 // Propagate liveness backwards to operands.
60 while (!worklist.empty()) {
61 Instruction* curr = worklist.back();
62 worklist.pop_back();
Owen Anderson62849be2008-05-16 04:34:51 +000063
Owen Anderson038a8742008-05-29 08:45:13 +000064 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
65 OI != OE; ++OI)
66 if (Instruction* Inst = dyn_cast<Instruction>(OI))
67 if (alive.insert(Inst))
68 worklist.push_back(Inst);
69 }
70
71 // The inverse of the live set is the dead set. These are those instructions
72 // which have no side effects and do not influence the control flow or return
73 // value of the function, and may therefore be deleted safely.
74 SmallPtrSet<Instruction*, 32> dead;
75 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
76 if (!alive.count(I.getInstructionIterator())) {
77 dead.insert(I.getInstructionIterator());
78 I->dropAllReferences();
Chris Lattnerb8259dd2001-09-09 22:26:47 +000079 }
Owen Anderson62849be2008-05-16 04:34:51 +000080
Owen Anderson038a8742008-05-29 08:45:13 +000081 for (SmallPtrSet<Instruction*, 32>::iterator I = dead.begin(),
82 E = dead.end(); I != E; ++I) {
83 NumRemoved++;
84 (*I)->eraseFromParent();
Chris Lattnerb8259dd2001-09-09 22:26:47 +000085 }
Owen Anderson038a8742008-05-29 08:45:13 +000086
87 return !dead.empty();
Chris Lattner02e90d52001-06-30 06:39:11 +000088}
Owen Anderson038a8742008-05-29 08:45:13 +000089
90FunctionPass *llvm::createAggressiveDCEPass() {
91 return new ADCE();
Duncan Sandsa806a872008-05-29 14:38:23 +000092}