blob: effd87e59a899602118e6bcd3aa01aa4fc33c654 [file] [log] [blame]
Owen Anderson7686b552008-05-29 08:45:13 +00001//===- DCE.cpp - Code to perform dead code elimination --------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb28986f2001-06-30 06:39:11 +00009//
Owen Anderson7686b552008-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,
Nadav Rotem465834c2012-07-24 10:51:42 +000012// allowing it to eliminate dead computations that other DCE passes do not
Owen Anderson7686b552008-05-29 08:45:13 +000013// catch, particularly involving loop computations.
Chris Lattnerb28986f2001-06-30 06:39:11 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/DepthFirstIterator.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000023#include "llvm/IR/CFG.h"
Chandler Carruth83948572014-03-04 10:30:26 +000024#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
Owen Anderson7686b552008-05-29 08:45:13 +000027#include "llvm/Pass.h"
Chris Lattnerfc7bdac2003-12-19 09:08:34 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "adce"
31
Owen Anderson7686b552008-05-29 08:45:13 +000032STATISTIC(NumRemoved, "Number of instructions removed");
Chris Lattner019f3642002-05-06 17:27:57 +000033
Chris Lattner79a42ac2006-12-19 21:40:18 +000034namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000035 struct ADCE : public FunctionPass {
Owen Anderson7686b552008-05-29 08:45:13 +000036 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000037 ADCE() : FunctionPass(ID) {
38 initializeADCEPass(*PassRegistry::getPassRegistry());
39 }
Nadav Rotem465834c2012-07-24 10:51:42 +000040
Craig Topper3e4c6972014-03-05 09:10:37 +000041 bool runOnFunction(Function& F) override;
Nadav Rotem465834c2012-07-24 10:51:42 +000042
Craig Topper3e4c6972014-03-05 09:10:37 +000043 void getAnalysisUsage(AnalysisUsage& AU) const override {
Owen Anderson7686b552008-05-29 08:45:13 +000044 AU.setPreservesCFG();
45 }
Nadav Rotem465834c2012-07-24 10:51:42 +000046
Owen Anderson7686b552008-05-29 08:45:13 +000047 };
48}
Chris Lattner019f3642002-05-06 17:27:57 +000049
Dan Gohmand78c4002008-05-13 00:00:25 +000050char ADCE::ID = 0;
Owen Andersondf7a4f22010-10-07 22:25:06 +000051INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000052
Owen Anderson7686b552008-05-29 08:45:13 +000053bool ADCE::runOnFunction(Function& F) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000054 if (skipOptnoneFunction(F))
55 return false;
56
Hal Finkel75901292015-02-15 15:45:28 +000057 SmallPtrSet<Instruction*, 128> Alive;
58 SmallVector<Instruction*, 128> Worklist;
Nadav Rotem465834c2012-07-24 10:51:42 +000059
Owen Anderson7686b552008-05-29 08:45:13 +000060 // Collect the set of "root" instructions that are known live.
Hal Finkel92fb2d32015-02-15 15:51:23 +000061 for (Instruction &I : inst_range(F)) {
62 if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
63 isa<LandingPadInst>(I) || I.mayHaveSideEffects()) {
64 Alive.insert(&I);
65 Worklist.push_back(&I);
Owen Andersonc7d6ece2008-05-16 04:34:51 +000066 }
Hal Finkel92fb2d32015-02-15 15:51:23 +000067 }
Nadav Rotem465834c2012-07-24 10:51:42 +000068
Owen Anderson7686b552008-05-29 08:45:13 +000069 // Propagate liveness backwards to operands.
Hal Finkel75901292015-02-15 15:45:28 +000070 while (!Worklist.empty()) {
Hal Finkelc6035cf2015-02-15 15:47:52 +000071 Instruction *Curr = Worklist.pop_back_val();
Hal Finkel234d8fe2015-02-15 15:45:30 +000072 for (Instruction::op_iterator OI = Curr->op_begin(), OE = Curr->op_end();
Owen Anderson7686b552008-05-29 08:45:13 +000073 OI != OE; ++OI)
Hal Finkelc6035cf2015-02-15 15:47:52 +000074 if (Instruction *Inst = dyn_cast<Instruction>(OI))
Hal Finkel75901292015-02-15 15:45:28 +000075 if (Alive.insert(Inst).second)
76 Worklist.push_back(Inst);
Owen Anderson7686b552008-05-29 08:45:13 +000077 }
Nadav Rotem465834c2012-07-24 10:51:42 +000078
Owen Anderson7686b552008-05-29 08:45:13 +000079 // The inverse of the live set is the dead set. These are those instructions
80 // which have no side effects and do not influence the control flow or return
81 // value of the function, and may therefore be deleted safely.
Hal Finkel75901292015-02-15 15:45:28 +000082 // NOTE: We reuse the Worklist vector here for memory efficiency.
Hal Finkel92fb2d32015-02-15 15:51:23 +000083 for (Instruction &I : inst_range(F)) {
84 if (!Alive.count(&I)) {
85 Worklist.push_back(&I);
86 I.dropAllReferences();
Chris Lattneracfd27d2001-09-09 22:26:47 +000087 }
Hal Finkel92fb2d32015-02-15 15:51:23 +000088 }
Nadav Rotem465834c2012-07-24 10:51:42 +000089
Hal Finkel92fb2d32015-02-15 15:51:23 +000090 for (Instruction *&I : Worklist) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000091 ++NumRemoved;
Hal Finkel92fb2d32015-02-15 15:51:23 +000092 I->eraseFromParent();
Chris Lattneracfd27d2001-09-09 22:26:47 +000093 }
Devang Patel53b39b52008-11-11 00:54:10 +000094
Hal Finkel75901292015-02-15 15:45:28 +000095 return !Worklist.empty();
Chris Lattnerb28986f2001-06-30 06:39:11 +000096}
Owen Anderson7686b552008-05-29 08:45:13 +000097
98FunctionPass *llvm::createAggressiveDCEPass() {
99 return new ADCE();
Duncan Sands9e064a22008-05-29 14:38:23 +0000100}