blob: a1331ddf44a8f28e1a358e040a06dc67c7843b41 [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"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
James Molloy87405c72015-08-14 11:09:09 +000020#include "llvm/Analysis/DemandedBits.h"
Davide Italiano655a1452016-05-25 01:57:04 +000021#include "llvm/Analysis/GlobalsModRef.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000022#include "llvm/IR/CFG.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000023#include "llvm/IR/InstIterator.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000026#include "llvm/IR/Operator.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
Davide Italiano655a1452016-05-25 01:57:04 +000030#include "llvm/Transforms/Scalar.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000031using namespace llvm;
32
33#define DEBUG_TYPE "bdce"
34
35STATISTIC(NumRemoved, "Number of instructions removed (unused)");
36STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
37
Davide Italiano655a1452016-05-25 01:57:04 +000038static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
Hal Finkel2bb61ba2015-02-17 01:36:59 +000039 SmallVector<Instruction*, 128> Worklist;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000040 bool Changed = false;
Nico Rieck78199512015-08-06 19:10:45 +000041 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +000042 if (I.getType()->isIntegerTy() &&
43 !DB.getDemandedBits(&I).getBoolValue()) {
44 // For live instructions that have all dead bits, first make them dead by
45 // replacing all uses with something else. Then, if they don't need to
46 // remain live (because they have side effects, etc.) we can remove them.
47 DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
48 // FIXME: In theory we could substitute undef here instead of zero.
49 // This should be reconsidered once we settle on the semantics of
50 // undef, poison, etc.
51 Value *Zero = ConstantInt::get(I.getType(), 0);
52 ++NumSimplified;
53 I.replaceAllUsesWith(Zero);
54 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000055 }
James Molloy87405c72015-08-14 11:09:09 +000056 if (!DB.isInstructionDead(&I))
Hal Finkel2bb61ba2015-02-17 01:36:59 +000057 continue;
58
Hal Finkel2bb61ba2015-02-17 01:36:59 +000059 Worklist.push_back(&I);
60 I.dropAllReferences();
61 Changed = true;
62 }
63
64 for (Instruction *&I : Worklist) {
65 ++NumRemoved;
66 I->eraseFromParent();
67 }
68
69 return Changed;
70}
71
Davide Italiano655a1452016-05-25 01:57:04 +000072PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
73 auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
74 if (bitTrackingDCE(F, DB))
75 return PreservedAnalyses::none();
76 return PreservedAnalyses::all();
Hal Finkel2bb61ba2015-02-17 01:36:59 +000077}
78
Davide Italiano655a1452016-05-25 01:57:04 +000079namespace {
80struct BDCELegacyPass : public FunctionPass {
81 static char ID; // Pass identification, replacement for typeid
82 BDCELegacyPass() : FunctionPass(ID) {
83 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
84 }
85
86 bool runOnFunction(Function &F) override {
87 if (skipFunction(F))
88 return false;
89 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
90 return bitTrackingDCE(F, DB);
91 }
92
93 void getAnalysisUsage(AnalysisUsage &AU) const override {
94 AU.setPreservesCFG();
95 AU.addRequired<DemandedBitsWrapperPass>();
96 AU.addPreserved<GlobalsAAWrapperPass>();
97 }
98};
99}
100
101char BDCELegacyPass::ID = 0;
102INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
103 "Bit-Tracking Dead Code Elimination", false, false)
104INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
105INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
106 "Bit-Tracking Dead Code Elimination", false, false)
107
108FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }