1*9880d681SAndroid Build Coastguard Worker //===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the Bit-Tracking Dead Code Elimination pass. Some
11*9880d681SAndroid Build Coastguard Worker // instructions (shifts, some ands, ors, etc.) kill some of their input bits.
12*9880d681SAndroid Build Coastguard Worker // We track these dead bits and remove instructions that compute only these
13*9880d681SAndroid Build Coastguard Worker // dead bits.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar/BDCE.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/DemandedBits.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "bdce"
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker STATISTIC(NumRemoved, "Number of instructions removed (unused)");
36*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
37*9880d681SAndroid Build Coastguard Worker
bitTrackingDCE(Function & F,DemandedBits & DB)38*9880d681SAndroid Build Coastguard Worker static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
39*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction*, 128> Worklist;
40*9880d681SAndroid Build Coastguard Worker bool Changed = false;
41*9880d681SAndroid Build Coastguard Worker for (Instruction &I : instructions(F)) {
42*9880d681SAndroid Build Coastguard Worker if (I.getType()->isIntegerTy() &&
43*9880d681SAndroid Build Coastguard Worker !DB.getDemandedBits(&I).getBoolValue()) {
44*9880d681SAndroid Build Coastguard Worker // For live instructions that have all dead bits, first make them dead by
45*9880d681SAndroid Build Coastguard Worker // replacing all uses with something else. Then, if they don't need to
46*9880d681SAndroid Build Coastguard Worker // remain live (because they have side effects, etc.) we can remove them.
47*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
48*9880d681SAndroid Build Coastguard Worker // FIXME: In theory we could substitute undef here instead of zero.
49*9880d681SAndroid Build Coastguard Worker // This should be reconsidered once we settle on the semantics of
50*9880d681SAndroid Build Coastguard Worker // undef, poison, etc.
51*9880d681SAndroid Build Coastguard Worker Value *Zero = ConstantInt::get(I.getType(), 0);
52*9880d681SAndroid Build Coastguard Worker ++NumSimplified;
53*9880d681SAndroid Build Coastguard Worker I.replaceAllUsesWith(Zero);
54*9880d681SAndroid Build Coastguard Worker Changed = true;
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker if (!DB.isInstructionDead(&I))
57*9880d681SAndroid Build Coastguard Worker continue;
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker Worklist.push_back(&I);
60*9880d681SAndroid Build Coastguard Worker I.dropAllReferences();
61*9880d681SAndroid Build Coastguard Worker Changed = true;
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker for (Instruction *&I : Worklist) {
65*9880d681SAndroid Build Coastguard Worker ++NumRemoved;
66*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
67*9880d681SAndroid Build Coastguard Worker }
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker return Changed;
70*9880d681SAndroid Build Coastguard Worker }
71*9880d681SAndroid Build Coastguard Worker
run(Function & F,FunctionAnalysisManager & AM)72*9880d681SAndroid Build Coastguard Worker PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
73*9880d681SAndroid Build Coastguard Worker auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
74*9880d681SAndroid Build Coastguard Worker if (!bitTrackingDCE(F, DB))
75*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker // FIXME: This should also 'preserve the CFG'.
78*9880d681SAndroid Build Coastguard Worker auto PA = PreservedAnalyses();
79*9880d681SAndroid Build Coastguard Worker PA.preserve<GlobalsAA>();
80*9880d681SAndroid Build Coastguard Worker return PA;
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker namespace {
84*9880d681SAndroid Build Coastguard Worker struct BDCELegacyPass : public FunctionPass {
85*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
BDCELegacyPass__anonc37ecc190111::BDCELegacyPass86*9880d681SAndroid Build Coastguard Worker BDCELegacyPass() : FunctionPass(ID) {
87*9880d681SAndroid Build Coastguard Worker initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker
runOnFunction__anonc37ecc190111::BDCELegacyPass90*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override {
91*9880d681SAndroid Build Coastguard Worker if (skipFunction(F))
92*9880d681SAndroid Build Coastguard Worker return false;
93*9880d681SAndroid Build Coastguard Worker auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
94*9880d681SAndroid Build Coastguard Worker return bitTrackingDCE(F, DB);
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anonc37ecc190111::BDCELegacyPass97*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
98*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
99*9880d681SAndroid Build Coastguard Worker AU.addRequired<DemandedBitsWrapperPass>();
100*9880d681SAndroid Build Coastguard Worker AU.addPreserved<GlobalsAAWrapperPass>();
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker };
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker char BDCELegacyPass::ID = 0;
106*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
107*9880d681SAndroid Build Coastguard Worker "Bit-Tracking Dead Code Elimination", false, false)
INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)108*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
109*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
110*9880d681SAndroid Build Coastguard Worker "Bit-Tracking Dead Code Elimination", false, false)
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }
113