blob: 65a68c1797763d9d514041c94ec0ba8ff2701e8a [file] [log] [blame]
Hal Finkel2bb61ba2015-02-17 01:36:59 +00001//===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Bit-Tracking Dead Code Elimination pass. Some
11// instructions (shifts, some ands, ors, etc.) kill some of their input bits.
12// We track these dead bits and remove instructions that compute only these
13// dead bits.
14//
15//===----------------------------------------------------------------------===//
16
Davide Italiano655a1452016-05-25 01:57:04 +000017#include "llvm/Transforms/Scalar/BDCE.h"
Sanjay Patelfe346f92017-08-12 16:41:08 +000018#include "llvm/ADT/SmallPtrSet.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Statistic.h"
James Molloy87405c72015-08-14 11:09:09 +000021#include "llvm/Analysis/DemandedBits.h"
Davide Italiano655a1452016-05-25 01:57:04 +000022#include "llvm/Analysis/GlobalsModRef.h"
David Blaikie31b98d22018-06-04 21:23:21 +000023#include "llvm/Transforms/Utils/Local.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000024#include "llvm/IR/InstIterator.h"
25#include "llvm/IR/Instructions.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000026#include "llvm/Pass.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Davide Italiano655a1452016-05-25 01:57:04 +000029#include "llvm/Transforms/Scalar.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000030using namespace llvm;
31
32#define DEBUG_TYPE "bdce"
33
34STATISTIC(NumRemoved, "Number of instructions removed (unused)");
35STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
36
Sanjay Patelfe346f92017-08-12 16:41:08 +000037/// If an instruction is trivialized (dead), then the chain of users of that
38/// instruction may need to be cleared of assumptions that can no longer be
39/// guaranteed correct.
40static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
Nikita Popov110cf052018-12-07 15:38:13 +000041 assert(I->getType()->isIntOrIntVectorTy() &&
42 "Trivializing a non-integer value?");
Sanjay Patelfe346f92017-08-12 16:41:08 +000043
44 // Initialize the worklist with eligible direct users.
45 SmallVector<Instruction *, 16> WorkList;
46 for (User *JU : I->users()) {
Sanjay Patela1067d92017-08-14 15:13:46 +000047 // If all bits of a user are demanded, then we know that nothing below that
48 // in the def-use chain needs to be changed.
Sanjay Patelfe346f92017-08-12 16:41:08 +000049 auto *J = dyn_cast<Instruction>(JU);
Nikita Popov110cf052018-12-07 15:38:13 +000050 if (J && J->getType()->isIntOrIntVectorTy() &&
Hal Finkel9e54b702017-08-16 16:09:22 +000051 !DB.getDemandedBits(J).isAllOnesValue())
Sanjay Patelfe346f92017-08-12 16:41:08 +000052 WorkList.push_back(J);
Hal Finkel9e54b702017-08-16 16:09:22 +000053
Nikita Popov110cf052018-12-07 15:38:13 +000054 // Note that we need to check for non-int types above before asking for
Hal Finkel9e54b702017-08-16 16:09:22 +000055 // demanded bits. Normally, the only way to reach an instruction with an
Nikita Popov110cf052018-12-07 15:38:13 +000056 // non-int type is via an instruction that has side effects (or otherwise
Hal Finkel9e54b702017-08-16 16:09:22 +000057 // will demand its input bits). However, if we have a readnone function
58 // that returns an unsized type (e.g., void), we must avoid asking for the
59 // demanded bits of the function call's return value. A void-returning
60 // readnone function is always dead (and so we can stop walking the use/def
61 // chain here), but the check is necessary to avoid asserting.
Sanjay Patelfe346f92017-08-12 16:41:08 +000062 }
63
64 // DFS through subsequent users while tracking visits to avoid cycles.
65 SmallPtrSet<Instruction *, 16> Visited;
66 while (!WorkList.empty()) {
67 Instruction *J = WorkList.pop_back_val();
68
69 // NSW, NUW, and exact are based on operands that might have changed.
70 J->dropPoisonGeneratingFlags();
71
72 // We do not have to worry about llvm.assume or range metadata:
73 // 1. llvm.assume demands its operand, so trivializing can't change it.
74 // 2. range metadata only applies to memory accesses which demand all bits.
75
76 Visited.insert(J);
77
78 for (User *KU : J->users()) {
Sanjay Patela1067d92017-08-14 15:13:46 +000079 // If all bits of a user are demanded, then we know that nothing below
80 // that in the def-use chain needs to be changed.
Sanjay Patelfe346f92017-08-12 16:41:08 +000081 auto *K = dyn_cast<Instruction>(KU);
Nikita Popov110cf052018-12-07 15:38:13 +000082 if (K && !Visited.count(K) && K->getType()->isIntOrIntVectorTy() &&
Hal Finkel9e54b702017-08-16 16:09:22 +000083 !DB.getDemandedBits(K).isAllOnesValue())
Sanjay Patelfe346f92017-08-12 16:41:08 +000084 WorkList.push_back(K);
85 }
86 }
87}
88
Davide Italiano655a1452016-05-25 01:57:04 +000089static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
Hal Finkel2bb61ba2015-02-17 01:36:59 +000090 SmallVector<Instruction*, 128> Worklist;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000091 bool Changed = false;
Nico Rieck78199512015-08-06 19:10:45 +000092 for (Instruction &I : instructions(F)) {
Davide Italiano043e6612016-12-06 21:52:47 +000093 // If the instruction has side effects and no non-dbg uses,
Davide Italiano1ed53962016-12-07 21:47:32 +000094 // skip it. This way we avoid computing known bits on an instruction
95 // that will not help us.
Davide Italiano043e6612016-12-06 21:52:47 +000096 if (I.mayHaveSideEffects() && I.use_empty())
97 continue;
98
Nikita Popovbc9986e2019-01-01 10:05:26 +000099 // Remove instructions not reached during analysis.
100 if (DB.isInstructionDead(&I)) {
101 salvageDebugInfo(I);
102 Worklist.push_back(&I);
103 I.dropAllReferences();
104 Changed = true;
105 continue;
106 }
107
108 for (Use &U : I.operands()) {
109 // DemandedBits only detects dead integer uses.
110 if (!U->getType()->isIntOrIntVectorTy())
111 continue;
112
113 // TODO: We could also find dead non-instruction uses, e.g. arguments.
114 if (!isa<Instruction>(U))
115 continue;
116
117 if (!DB.isUseDead(&U))
118 continue;
119
120 LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n");
Sanjay Patelfe346f92017-08-12 16:41:08 +0000121
122 clearAssumptionsOfUsers(&I, DB);
123
James Molloy87405c72015-08-14 11:09:09 +0000124 // FIXME: In theory we could substitute undef here instead of zero.
125 // This should be reconsidered once we settle on the semantics of
126 // undef, poison, etc.
Nikita Popovbc9986e2019-01-01 10:05:26 +0000127 U.set(ConstantInt::get(U->getType(), 0));
James Molloy87405c72015-08-14 11:09:09 +0000128 ++NumSimplified;
James Molloy87405c72015-08-14 11:09:09 +0000129 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000130 }
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000131 }
132
133 for (Instruction *&I : Worklist) {
134 ++NumRemoved;
135 I->eraseFromParent();
136 }
137
138 return Changed;
139}
140
Davide Italiano655a1452016-05-25 01:57:04 +0000141PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
142 auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
Davide Italianobdc29712016-05-31 17:53:22 +0000143 if (!bitTrackingDCE(F, DB))
144 return PreservedAnalyses::all();
145
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000146 PreservedAnalyses PA;
147 PA.preserveSet<CFGAnalyses>();
Davide Italianobdc29712016-05-31 17:53:22 +0000148 PA.preserve<GlobalsAA>();
149 return PA;
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000150}
151
Davide Italiano655a1452016-05-25 01:57:04 +0000152namespace {
153struct BDCELegacyPass : public FunctionPass {
154 static char ID; // Pass identification, replacement for typeid
155 BDCELegacyPass() : FunctionPass(ID) {
156 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
157 }
158
159 bool runOnFunction(Function &F) override {
160 if (skipFunction(F))
161 return false;
162 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
163 return bitTrackingDCE(F, DB);
164 }
165
166 void getAnalysisUsage(AnalysisUsage &AU) const override {
167 AU.setPreservesCFG();
168 AU.addRequired<DemandedBitsWrapperPass>();
169 AU.addPreserved<GlobalsAAWrapperPass>();
170 }
171};
172}
173
174char BDCELegacyPass::ID = 0;
175INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
176 "Bit-Tracking Dead Code Elimination", false, false)
177INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
178INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
179 "Bit-Tracking Dead Code Elimination", false, false)
180
181FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }