blob: 5a4984158ea01eb5f0e491e4d4bd73f22eef09f7 [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"
Owen Anderson49ff8a42008-07-02 18:05:19 +000019#include "llvm/BasicBlock.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Instructions.h"
Dale Johannesen6daff542009-03-04 21:24:04 +000021#include "llvm/IntrinsicInst.h"
Owen Anderson871d8eb2008-05-29 08:45:13 +000022#include "llvm/Pass.h"
Owen Anderson49ff8a42008-07-02 18:05:19 +000023#include "llvm/Support/CFG.h"
Owen Anderson871d8eb2008-05-29 08:45:13 +000024#include "llvm/Support/InstIterator.h"
Owen Anderson49ff8a42008-07-02 18:05:19 +000025#include "llvm/ADT/DepthFirstIterator.h"
Owen Anderson73b03a22008-07-02 18:41:09 +000026#include "llvm/ADT/SmallPtrSet.h"
Owen Anderson40e5a552008-06-23 06:13:12 +000027#include "llvm/ADT/SmallVector.h"
Owen Anderson73b03a22008-07-02 18:41:09 +000028#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
Owen Anderson871d8eb2008-05-29 08:45:13 +000031STATISTIC(NumRemoved, "Number of instructions removed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032
33namespace {
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000034 struct ADCE : public FunctionPass {
Owen Anderson871d8eb2008-05-29 08:45:13 +000035 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000036 ADCE() : FunctionPass(&ID) {}
Owen Anderson871d8eb2008-05-29 08:45:13 +000037
38 virtual bool runOnFunction(Function& F);
39
40 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
41 AU.setPreservesCFG();
42 }
43
44 };
45}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
Dan Gohman089efff2008-05-13 00:00:25 +000047char ADCE::ID = 0;
48static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
49
Owen Anderson871d8eb2008-05-29 08:45:13 +000050bool ADCE::runOnFunction(Function& F) {
Owen Anderson73b03a22008-07-02 18:41:09 +000051 SmallPtrSet<Instruction*, 128> alive;
52 SmallVector<Instruction*, 128> worklist;
53
Owen Anderson871d8eb2008-05-29 08:45:13 +000054 // Collect the set of "root" instructions that are known live.
55 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
56 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
Dale Johannesen6daff542009-03-04 21:24:04 +000057 isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
Duncan Sands2f500832009-05-06 06:49:50 +000058 I->mayHaveSideEffects()) {
Owen Anderson871d8eb2008-05-29 08:45:13 +000059 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()) {
Dan Gohman3e7816b2010-01-05 16:27:25 +000065 Instruction* curr = worklist.pop_back_val();
Owen Anderson8cf41ad2008-05-16 04:34:51 +000066
Owen Anderson871d8eb2008-05-29 08:45:13 +000067 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
68 OI != OE; ++OI)
69 if (Instruction* Inst = dyn_cast<Instruction>(OI))
70 if (alive.insert(Inst))
71 worklist.push_back(Inst);
72 }
73
74 // The inverse of the live set is the dead set. These are those instructions
75 // which have no side effects and do not influence the control flow or return
76 // value of the function, and may therefore be deleted safely.
Owen Anderson40e5a552008-06-23 06:13:12 +000077 // NOTE: We reuse the worklist vector here for memory efficiency.
Owen Anderson871d8eb2008-05-29 08:45:13 +000078 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
79 if (!alive.count(I.getInstructionIterator())) {
Owen Anderson40e5a552008-06-23 06:13:12 +000080 worklist.push_back(I.getInstructionIterator());
Owen Anderson871d8eb2008-05-29 08:45:13 +000081 I->dropAllReferences();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 }
Owen Anderson8cf41ad2008-05-16 04:34:51 +000083
Owen Anderson40e5a552008-06-23 06:13:12 +000084 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
85 E = worklist.end(); I != E; ++I) {
Owen Anderson871d8eb2008-05-29 08:45:13 +000086 NumRemoved++;
87 (*I)->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 }
Devang Patelf78cd102008-11-11 00:54:10 +000089
Devang Patel7c047572008-11-19 18:59:41 +000090 return !worklist.empty();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091}
Owen Anderson871d8eb2008-05-29 08:45:13 +000092
93FunctionPass *llvm::createAggressiveDCEPass() {
94 return new ADCE();
Duncan Sandsb7581232008-05-29 14:38:23 +000095}