blob: 7b01ec150862baddb2fccbaa1e2b7f1668c682a4 [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 Molloy87405c72015-08-14 11:09:09 +000020#include "llvm/Analysis/DemandedBits.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000021#include "llvm/IR/CFG.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000022#include "llvm/IR/InstIterator.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000025#include "llvm/IR/Operator.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Hal Finkel2bb61ba2015-02-17 01:36:59 +000029using namespace llvm;
30
31#define DEBUG_TYPE "bdce"
32
33STATISTIC(NumRemoved, "Number of instructions removed (unused)");
34STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
35
36namespace {
37struct BDCE : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 BDCE() : FunctionPass(ID) {
40 initializeBDCEPass(*PassRegistry::getPassRegistry());
41 }
42
43 bool runOnFunction(Function& F) override;
44
45 void getAnalysisUsage(AnalysisUsage& AU) const override {
46 AU.setPreservesCFG();
James Molloy87405c72015-08-14 11:09:09 +000047 AU.addRequired<DemandedBits>();
Hal Finkel2bb61ba2015-02-17 01:36:59 +000048 }
Hal Finkel2bb61ba2015-02-17 01:36:59 +000049};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}
Hal Finkel2bb61ba2015-02-17 01:36:59 +000051
52char BDCE::ID = 0;
53INITIALIZE_PASS_BEGIN(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
54 false, false)
James Molloy87405c72015-08-14 11:09:09 +000055INITIALIZE_PASS_DEPENDENCY(DemandedBits)
Hal Finkel2bb61ba2015-02-17 01:36:59 +000056INITIALIZE_PASS_END(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
57 false, false)
58
Hal Finkel2bb61ba2015-02-17 01:36:59 +000059bool BDCE::runOnFunction(Function& F) {
60 if (skipOptnoneFunction(F))
61 return false;
James Molloy87405c72015-08-14 11:09:09 +000062 DemandedBits &DB = getAnalysis<DemandedBits>();
Hal Finkel2bb61ba2015-02-17 01:36:59 +000063
Hal Finkel2bb61ba2015-02-17 01:36:59 +000064 SmallVector<Instruction*, 128> Worklist;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000065 bool Changed = false;
Nico Rieck78199512015-08-06 19:10:45 +000066 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +000067 if (I.getType()->isIntegerTy() &&
68 !DB.getDemandedBits(&I).getBoolValue()) {
69 // For live instructions that have all dead bits, first make them dead by
70 // replacing all uses with something else. Then, if they don't need to
71 // remain live (because they have side effects, etc.) we can remove them.
72 DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
73 // FIXME: In theory we could substitute undef here instead of zero.
74 // This should be reconsidered once we settle on the semantics of
75 // undef, poison, etc.
76 Value *Zero = ConstantInt::get(I.getType(), 0);
77 ++NumSimplified;
78 I.replaceAllUsesWith(Zero);
79 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000080 }
James Molloy87405c72015-08-14 11:09:09 +000081 if (!DB.isInstructionDead(&I))
Hal Finkel2bb61ba2015-02-17 01:36:59 +000082 continue;
83
Hal Finkel2bb61ba2015-02-17 01:36:59 +000084 Worklist.push_back(&I);
85 I.dropAllReferences();
86 Changed = true;
87 }
88
89 for (Instruction *&I : Worklist) {
90 ++NumRemoved;
91 I->eraseFromParent();
92 }
93
94 return Changed;
95}
96
97FunctionPass *llvm::createBitTrackingDCEPass() {
98 return new BDCE();
99}
100