blob: cb9b8b6fffc84113baa5d5a46062b271c7871523 [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
17#include "llvm/Transforms/Scalar.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
James Molloyefbba722015-09-10 10:22:12 +000020#include "llvm/Analysis/GlobalsModRef.h"
James Molloy87405c72015-08-14 11:09:09 +000021#include "llvm/Analysis/DemandedBits.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"
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
37namespace {
38struct BDCE : public FunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 BDCE() : FunctionPass(ID) {
41 initializeBDCEPass(*PassRegistry::getPassRegistry());
42 }
43
44 bool runOnFunction(Function& F) override;
45
46 void getAnalysisUsage(AnalysisUsage& AU) const override {
47 AU.setPreservesCFG();
James Molloy87405c72015-08-14 11:09:09 +000048 AU.addRequired<DemandedBits>();
James Molloyefbba722015-09-10 10:22:12 +000049 AU.addPreserved<GlobalsAAWrapperPass>();
Hal Finkel2bb61ba2015-02-17 01:36:59 +000050 }
Hal Finkel2bb61ba2015-02-17 01:36:59 +000051};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Hal Finkel2bb61ba2015-02-17 01:36:59 +000053
54char BDCE::ID = 0;
55INITIALIZE_PASS_BEGIN(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
56 false, false)
James Molloy87405c72015-08-14 11:09:09 +000057INITIALIZE_PASS_DEPENDENCY(DemandedBits)
Hal Finkel2bb61ba2015-02-17 01:36:59 +000058INITIALIZE_PASS_END(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
59 false, false)
60
Hal Finkel2bb61ba2015-02-17 01:36:59 +000061bool BDCE::runOnFunction(Function& F) {
62 if (skipOptnoneFunction(F))
63 return false;
James Molloy87405c72015-08-14 11:09:09 +000064 DemandedBits &DB = getAnalysis<DemandedBits>();
Hal Finkel2bb61ba2015-02-17 01:36:59 +000065
Hal Finkel2bb61ba2015-02-17 01:36:59 +000066 SmallVector<Instruction*, 128> Worklist;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000067 bool Changed = false;
Nico Rieck78199512015-08-06 19:10:45 +000068 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +000069 if (I.getType()->isIntegerTy() &&
70 !DB.getDemandedBits(&I).getBoolValue()) {
71 // For live instructions that have all dead bits, first make them dead by
72 // replacing all uses with something else. Then, if they don't need to
73 // remain live (because they have side effects, etc.) we can remove them.
74 DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
75 // FIXME: In theory we could substitute undef here instead of zero.
76 // This should be reconsidered once we settle on the semantics of
77 // undef, poison, etc.
78 Value *Zero = ConstantInt::get(I.getType(), 0);
79 ++NumSimplified;
80 I.replaceAllUsesWith(Zero);
81 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000082 }
James Molloy87405c72015-08-14 11:09:09 +000083 if (!DB.isInstructionDead(&I))
Hal Finkel2bb61ba2015-02-17 01:36:59 +000084 continue;
85
Hal Finkel2bb61ba2015-02-17 01:36:59 +000086 Worklist.push_back(&I);
87 I.dropAllReferences();
88 Changed = true;
89 }
90
91 for (Instruction *&I : Worklist) {
92 ++NumRemoved;
93 I->eraseFromParent();
94 }
95
96 return Changed;
97}
98
99FunctionPass *llvm::createBitTrackingDCEPass() {
100 return new BDCE();
101}
102