blob: f3ad9556080418a270d1c57caf17a0355e77a471 [file] [log] [blame]
Justin Bogner0638b7ba2015-09-25 21:03:46 +00001//===- ADCE.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
Justin Bogner19b67992015-10-30 23:13:18 +000017#include "llvm/Transforms/Scalar/ADCE.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"
James Molloyefbba722015-09-10 10:22:12 +000022#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000024#include "llvm/IR/CFG.h"
Chandler Carruth83948572014-03-04 10:30:26 +000025#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Instructions.h"
27#include "llvm/IR/IntrinsicInst.h"
Owen Anderson7686b552008-05-29 08:45:13 +000028#include "llvm/Pass.h"
Justin Bogner19b67992015-10-30 23:13:18 +000029#include "llvm/Transforms/Scalar.h"
Chris Lattnerfc7bdac2003-12-19 09:08:34 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "adce"
33
Owen Anderson7686b552008-05-29 08:45:13 +000034STATISTIC(NumRemoved, "Number of instructions removed");
Chris Lattner019f3642002-05-06 17:27:57 +000035
Justin Bogner19b67992015-10-30 23:13:18 +000036static bool aggressiveDCE(Function& F) {
Matthias Braunb30f2f512016-01-30 01:24:31 +000037 SmallPtrSet<Instruction*, 32> Alive;
Hal Finkel75901292015-02-15 15:45:28 +000038 SmallVector<Instruction*, 128> Worklist;
Nadav Rotem465834c2012-07-24 10:51:42 +000039
Owen Anderson7686b552008-05-29 08:45:13 +000040 // Collect the set of "root" instructions that are known live.
Nico Rieck78199512015-08-06 19:10:45 +000041 for (Instruction &I : instructions(F)) {
David Majnemer654e1302015-07-31 17:58:14 +000042 if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) || I.isEHPad() ||
43 I.mayHaveSideEffects()) {
Hal Finkel92fb2d32015-02-15 15:51:23 +000044 Alive.insert(&I);
45 Worklist.push_back(&I);
Owen Andersonc7d6ece2008-05-16 04:34:51 +000046 }
Hal Finkel92fb2d32015-02-15 15:51:23 +000047 }
Nadav Rotem465834c2012-07-24 10:51:42 +000048
Owen Anderson7686b552008-05-29 08:45:13 +000049 // Propagate liveness backwards to operands.
Hal Finkel75901292015-02-15 15:45:28 +000050 while (!Worklist.empty()) {
Hal Finkelc6035cf2015-02-15 15:47:52 +000051 Instruction *Curr = Worklist.pop_back_val();
Hal Finkel8626ed22015-02-15 15:51:25 +000052 for (Use &OI : Curr->operands()) {
Hal Finkelc6035cf2015-02-15 15:47:52 +000053 if (Instruction *Inst = dyn_cast<Instruction>(OI))
Hal Finkel75901292015-02-15 15:45:28 +000054 if (Alive.insert(Inst).second)
55 Worklist.push_back(Inst);
Hal Finkel8626ed22015-02-15 15:51:25 +000056 }
Owen Anderson7686b552008-05-29 08:45:13 +000057 }
Nadav Rotem465834c2012-07-24 10:51:42 +000058
Owen Anderson7686b552008-05-29 08:45:13 +000059 // The inverse of the live set is the dead set. These are those instructions
60 // which have no side effects and do not influence the control flow or return
61 // value of the function, and may therefore be deleted safely.
Hal Finkel75901292015-02-15 15:45:28 +000062 // NOTE: We reuse the Worklist vector here for memory efficiency.
Nico Rieck78199512015-08-06 19:10:45 +000063 for (Instruction &I : instructions(F)) {
Hal Finkel92fb2d32015-02-15 15:51:23 +000064 if (!Alive.count(&I)) {
65 Worklist.push_back(&I);
66 I.dropAllReferences();
Chris Lattneracfd27d2001-09-09 22:26:47 +000067 }
Hal Finkel92fb2d32015-02-15 15:51:23 +000068 }
Nadav Rotem465834c2012-07-24 10:51:42 +000069
Hal Finkel92fb2d32015-02-15 15:51:23 +000070 for (Instruction *&I : Worklist) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +000071 ++NumRemoved;
Hal Finkel92fb2d32015-02-15 15:51:23 +000072 I->eraseFromParent();
Chris Lattneracfd27d2001-09-09 22:26:47 +000073 }
Devang Patel53b39b52008-11-11 00:54:10 +000074
Hal Finkel75901292015-02-15 15:45:28 +000075 return !Worklist.empty();
Chris Lattnerb28986f2001-06-30 06:39:11 +000076}
Owen Anderson7686b552008-05-29 08:45:13 +000077
Justin Bogner19b67992015-10-30 23:13:18 +000078PreservedAnalyses ADCEPass::run(Function &F) {
79 if (aggressiveDCE(F))
80 return PreservedAnalyses::none();
81 return PreservedAnalyses::all();
Duncan Sands9e064a22008-05-29 14:38:23 +000082}
Justin Bogner19b67992015-10-30 23:13:18 +000083
84namespace {
85struct ADCELegacyPass : public FunctionPass {
86 static char ID; // Pass identification, replacement for typeid
87 ADCELegacyPass() : FunctionPass(ID) {
88 initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
89 }
90
91 bool runOnFunction(Function& F) override {
92 if (skipOptnoneFunction(F))
93 return false;
94 return aggressiveDCE(F);
95 }
96
97 void getAnalysisUsage(AnalysisUsage& AU) const override {
98 AU.setPreservesCFG();
99 AU.addPreserved<GlobalsAAWrapperPass>();
100 }
101};
102}
103
104char ADCELegacyPass::ID = 0;
105INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
106 false, false)
107
108FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }