blob: 9c55f664ebbd0e723c1b9f71d982b6b62a1605eb [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"
Owen Anderson77d76b72008-07-02 18:05:19 +000019#include "llvm/BasicBlock.h"
Chris Lattnerede6ac62004-04-10 06:53:09 +000020#include "llvm/Instructions.h"
Dale Johannesen6129e242009-03-04 21:24:04 +000021#include "llvm/IntrinsicInst.h"
Owen Anderson038a8742008-05-29 08:45:13 +000022#include "llvm/Pass.h"
Owen Anderson77d76b72008-07-02 18:05:19 +000023#include "llvm/Support/CFG.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Owen Anderson038a8742008-05-29 08:45:13 +000025#include "llvm/Support/InstIterator.h"
Owen Anderson77d76b72008-07-02 18:05:19 +000026#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersonea6462b2008-07-02 18:41:09 +000027#include "llvm/ADT/SmallPtrSet.h"
Owen Andersonae18bd42008-06-23 06:13:12 +000028#include "llvm/ADT/SmallVector.h"
Owen Andersonea6462b2008-07-02 18:41:09 +000029#include "llvm/ADT/Statistic.h"
Owen Anderson038a8742008-05-29 08:45:13 +000030
Chris Lattnerbd1a90e2003-12-19 09:08:34 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Owen Anderson038a8742008-05-29 08:45:13 +000033STATISTIC(NumRemoved, "Number of instructions removed");
Chris Lattnerdfe81ab2002-05-06 17:27:57 +000034
Chris Lattner0e5f4992006-12-19 21:40:18 +000035namespace {
Owen Anderson038a8742008-05-29 08:45:13 +000036 struct VISIBILITY_HIDDEN ADCE : public FunctionPass {
37 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000038 ADCE() : FunctionPass(&ID) {}
Owen Anderson038a8742008-05-29 08:45:13 +000039
40 virtual bool runOnFunction(Function& F);
41
42 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
43 AU.setPreservesCFG();
44 }
45
46 };
47}
Chris Lattnerdfe81ab2002-05-06 17:27:57 +000048
Dan Gohman844731a2008-05-13 00:00:25 +000049char ADCE::ID = 0;
50static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
51
Owen Anderson038a8742008-05-29 08:45:13 +000052bool ADCE::runOnFunction(Function& F) {
Owen Andersonea6462b2008-07-02 18:41:09 +000053 SmallPtrSet<Instruction*, 128> alive;
54 SmallVector<Instruction*, 128> worklist;
55
Owen Anderson038a8742008-05-29 08:45:13 +000056 // Collect the set of "root" instructions that are known live.
57 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
58 if (isa<TerminatorInst>(I.getInstructionIterator()) ||
Dale Johannesen6129e242009-03-04 21:24:04 +000059 isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
Duncan Sands7af1c782009-05-06 06:49:50 +000060 I->mayHaveSideEffects()) {
Owen Anderson038a8742008-05-29 08:45:13 +000061 alive.insert(I.getInstructionIterator());
62 worklist.push_back(I.getInstructionIterator());
Owen Anderson62849be2008-05-16 04:34:51 +000063 }
Owen Anderson038a8742008-05-29 08:45:13 +000064
65 // Propagate liveness backwards to operands.
66 while (!worklist.empty()) {
67 Instruction* curr = worklist.back();
68 worklist.pop_back();
Owen Anderson62849be2008-05-16 04:34:51 +000069
Owen Anderson038a8742008-05-29 08:45:13 +000070 for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
71 OI != OE; ++OI)
72 if (Instruction* Inst = dyn_cast<Instruction>(OI))
73 if (alive.insert(Inst))
74 worklist.push_back(Inst);
75 }
76
77 // The inverse of the live set is the dead set. These are those instructions
78 // which have no side effects and do not influence the control flow or return
79 // value of the function, and may therefore be deleted safely.
Owen Andersonae18bd42008-06-23 06:13:12 +000080 // NOTE: We reuse the worklist vector here for memory efficiency.
Owen Anderson038a8742008-05-29 08:45:13 +000081 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
82 if (!alive.count(I.getInstructionIterator())) {
Owen Andersonae18bd42008-06-23 06:13:12 +000083 worklist.push_back(I.getInstructionIterator());
Owen Anderson038a8742008-05-29 08:45:13 +000084 I->dropAllReferences();
Chris Lattnerb8259dd2001-09-09 22:26:47 +000085 }
Owen Anderson62849be2008-05-16 04:34:51 +000086
Owen Andersonae18bd42008-06-23 06:13:12 +000087 for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
88 E = worklist.end(); I != E; ++I) {
Owen Anderson038a8742008-05-29 08:45:13 +000089 NumRemoved++;
90 (*I)->eraseFromParent();
Chris Lattnerb8259dd2001-09-09 22:26:47 +000091 }
Devang Patel35275f42008-11-11 00:54:10 +000092
Devang Patelb8f69c62008-11-19 18:59:41 +000093 return !worklist.empty();
Chris Lattner02e90d52001-06-30 06:39:11 +000094}
Owen Anderson038a8742008-05-29 08:45:13 +000095
96FunctionPass *llvm::createAggressiveDCEPass() {
97 return new ADCE();
Duncan Sandsa806a872008-05-29 14:38:23 +000098}