blob: 9d7997be1eb55f0aa0db3a3426e7a679491bcf07 [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"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000023#include "llvm/IR/CFG.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000024#include "llvm/IR/InstIterator.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000027#include "llvm/IR/Operator.h"
28#include "llvm/Pass.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Davide Italiano655a1452016-05-25 01:57:04 +000031#include "llvm/Transforms/Scalar.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000032using namespace llvm;
33
34#define DEBUG_TYPE "bdce"
35
36STATISTIC(NumRemoved, "Number of instructions removed (unused)");
37STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
38
Sanjay Patelfe346f92017-08-12 16:41:08 +000039/// If an instruction is trivialized (dead), then the chain of users of that
40/// instruction may need to be cleared of assumptions that can no longer be
41/// guaranteed correct.
42static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
Sanjay Patela1067d92017-08-14 15:13:46 +000043 assert(I->getType()->isIntegerTy() && "Trivializing a non-integer value?");
Sanjay Patelfe346f92017-08-12 16:41:08 +000044
45 // Initialize the worklist with eligible direct users.
46 SmallVector<Instruction *, 16> WorkList;
47 for (User *JU : I->users()) {
Sanjay Patela1067d92017-08-14 15:13:46 +000048 // If all bits of a user are demanded, then we know that nothing below that
49 // in the def-use chain needs to be changed.
Sanjay Patelfe346f92017-08-12 16:41:08 +000050 auto *J = dyn_cast<Instruction>(JU);
Hal Finkel9e54b702017-08-16 16:09:22 +000051 if (J && J->getType()->isSized() &&
52 !DB.getDemandedBits(J).isAllOnesValue())
Sanjay Patelfe346f92017-08-12 16:41:08 +000053 WorkList.push_back(J);
Hal Finkel9e54b702017-08-16 16:09:22 +000054
55 // Note that we need to check for unsized types above before asking for
56 // demanded bits. Normally, the only way to reach an instruction with an
57 // unsized type is via an instruction that has side effects (or otherwise
58 // will demand its input bits). However, if we have a readnone function
59 // that returns an unsized type (e.g., void), we must avoid asking for the
60 // demanded bits of the function call's return value. A void-returning
61 // readnone function is always dead (and so we can stop walking the use/def
62 // chain here), but the check is necessary to avoid asserting.
Sanjay Patelfe346f92017-08-12 16:41:08 +000063 }
64
65 // DFS through subsequent users while tracking visits to avoid cycles.
66 SmallPtrSet<Instruction *, 16> Visited;
67 while (!WorkList.empty()) {
68 Instruction *J = WorkList.pop_back_val();
69
70 // NSW, NUW, and exact are based on operands that might have changed.
71 J->dropPoisonGeneratingFlags();
72
73 // We do not have to worry about llvm.assume or range metadata:
74 // 1. llvm.assume demands its operand, so trivializing can't change it.
75 // 2. range metadata only applies to memory accesses which demand all bits.
76
77 Visited.insert(J);
78
79 for (User *KU : J->users()) {
Sanjay Patela1067d92017-08-14 15:13:46 +000080 // If all bits of a user are demanded, then we know that nothing below
81 // that in the def-use chain needs to be changed.
Sanjay Patelfe346f92017-08-12 16:41:08 +000082 auto *K = dyn_cast<Instruction>(KU);
Hal Finkel9e54b702017-08-16 16:09:22 +000083 if (K && !Visited.count(K) && K->getType()->isSized() &&
84 !DB.getDemandedBits(K).isAllOnesValue())
Sanjay Patelfe346f92017-08-12 16:41:08 +000085 WorkList.push_back(K);
86 }
87 }
88}
89
Davide Italiano655a1452016-05-25 01:57:04 +000090static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
Hal Finkel2bb61ba2015-02-17 01:36:59 +000091 SmallVector<Instruction*, 128> Worklist;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000092 bool Changed = false;
Nico Rieck78199512015-08-06 19:10:45 +000093 for (Instruction &I : instructions(F)) {
Davide Italiano043e6612016-12-06 21:52:47 +000094 // If the instruction has side effects and no non-dbg uses,
Davide Italiano1ed53962016-12-07 21:47:32 +000095 // skip it. This way we avoid computing known bits on an instruction
96 // that will not help us.
Davide Italiano043e6612016-12-06 21:52:47 +000097 if (I.mayHaveSideEffects() && I.use_empty())
98 continue;
99
James Molloy87405c72015-08-14 11:09:09 +0000100 if (I.getType()->isIntegerTy() &&
101 !DB.getDemandedBits(&I).getBoolValue()) {
102 // For live instructions that have all dead bits, first make them dead by
103 // replacing all uses with something else. Then, if they don't need to
104 // remain live (because they have side effects, etc.) we can remove them.
105 DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
Sanjay Patelfe346f92017-08-12 16:41:08 +0000106
107 clearAssumptionsOfUsers(&I, DB);
108
James Molloy87405c72015-08-14 11:09:09 +0000109 // FIXME: In theory we could substitute undef here instead of zero.
110 // This should be reconsidered once we settle on the semantics of
111 // undef, poison, etc.
112 Value *Zero = ConstantInt::get(I.getType(), 0);
113 ++NumSimplified;
Davide Italiano1ed53962016-12-07 21:47:32 +0000114 I.replaceNonMetadataUsesWith(Zero);
James Molloy87405c72015-08-14 11:09:09 +0000115 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000116 }
James Molloy87405c72015-08-14 11:09:09 +0000117 if (!DB.isInstructionDead(&I))
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000118 continue;
119
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000120 Worklist.push_back(&I);
121 I.dropAllReferences();
122 Changed = true;
123 }
124
125 for (Instruction *&I : Worklist) {
126 ++NumRemoved;
127 I->eraseFromParent();
128 }
129
130 return Changed;
131}
132
Davide Italiano655a1452016-05-25 01:57:04 +0000133PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
134 auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
Davide Italianobdc29712016-05-31 17:53:22 +0000135 if (!bitTrackingDCE(F, DB))
136 return PreservedAnalyses::all();
137
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000138 PreservedAnalyses PA;
139 PA.preserveSet<CFGAnalyses>();
Davide Italianobdc29712016-05-31 17:53:22 +0000140 PA.preserve<GlobalsAA>();
141 return PA;
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000142}
143
Davide Italiano655a1452016-05-25 01:57:04 +0000144namespace {
145struct BDCELegacyPass : public FunctionPass {
146 static char ID; // Pass identification, replacement for typeid
147 BDCELegacyPass() : FunctionPass(ID) {
148 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
149 }
150
151 bool runOnFunction(Function &F) override {
152 if (skipFunction(F))
153 return false;
154 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
155 return bitTrackingDCE(F, DB);
156 }
157
158 void getAnalysisUsage(AnalysisUsage &AU) const override {
159 AU.setPreservesCFG();
160 AU.addRequired<DemandedBitsWrapperPass>();
161 AU.addPreserved<GlobalsAAWrapperPass>();
162 }
163};
164}
165
166char BDCELegacyPass::ID = 0;
167INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
168 "Bit-Tracking Dead Code Elimination", false, false)
169INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
170INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
171 "Bit-Tracking Dead Code Elimination", false, false)
172
173FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }