blob: 251b3870776975d5305b4072a71577fa1579b9f5 [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)) {
Davide Italiano043e6612016-12-06 21:52:47 +000042 // If the instruction has side effects and no non-dbg uses,
Davide Italiano1ed53962016-12-07 21:47:32 +000043 // skip it. This way we avoid computing known bits on an instruction
44 // that will not help us.
Davide Italiano043e6612016-12-06 21:52:47 +000045 if (I.mayHaveSideEffects() && I.use_empty())
46 continue;
47
James Molloy87405c72015-08-14 11:09:09 +000048 if (I.getType()->isIntegerTy() &&
49 !DB.getDemandedBits(&I).getBoolValue()) {
50 // For live instructions that have all dead bits, first make them dead by
51 // replacing all uses with something else. Then, if they don't need to
52 // remain live (because they have side effects, etc.) we can remove them.
53 DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
54 // FIXME: In theory we could substitute undef here instead of zero.
55 // This should be reconsidered once we settle on the semantics of
56 // undef, poison, etc.
57 Value *Zero = ConstantInt::get(I.getType(), 0);
58 ++NumSimplified;
Davide Italiano1ed53962016-12-07 21:47:32 +000059 I.replaceNonMetadataUsesWith(Zero);
James Molloy87405c72015-08-14 11:09:09 +000060 Changed = true;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000061 }
James Molloy87405c72015-08-14 11:09:09 +000062 if (!DB.isInstructionDead(&I))
Hal Finkel2bb61ba2015-02-17 01:36:59 +000063 continue;
64
Hal Finkel2bb61ba2015-02-17 01:36:59 +000065 Worklist.push_back(&I);
66 I.dropAllReferences();
67 Changed = true;
68 }
69
70 for (Instruction *&I : Worklist) {
71 ++NumRemoved;
72 I->eraseFromParent();
73 }
74
75 return Changed;
76}
77
Davide Italiano655a1452016-05-25 01:57:04 +000078PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
79 auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
Davide Italianobdc29712016-05-31 17:53:22 +000080 if (!bitTrackingDCE(F, DB))
81 return PreservedAnalyses::all();
82
Michael Kuperstein835facd2016-06-28 00:54:12 +000083 // FIXME: This should also 'preserve the CFG'.
Davide Italianobdc29712016-05-31 17:53:22 +000084 auto PA = PreservedAnalyses();
85 PA.preserve<GlobalsAA>();
86 return PA;
Hal Finkel2bb61ba2015-02-17 01:36:59 +000087}
88
Davide Italiano655a1452016-05-25 01:57:04 +000089namespace {
90struct BDCELegacyPass : public FunctionPass {
91 static char ID; // Pass identification, replacement for typeid
92 BDCELegacyPass() : FunctionPass(ID) {
93 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
94 }
95
96 bool runOnFunction(Function &F) override {
97 if (skipFunction(F))
98 return false;
99 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
100 return bitTrackingDCE(F, DB);
101 }
102
103 void getAnalysisUsage(AnalysisUsage &AU) const override {
104 AU.setPreservesCFG();
105 AU.addRequired<DemandedBitsWrapperPass>();
106 AU.addPreserved<GlobalsAAWrapperPass>();
107 }
108};
109}
110
111char BDCELegacyPass::ID = 0;
112INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
113 "Bit-Tracking Dead Code Elimination", false, false)
114INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
115INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
116 "Bit-Tracking Dead Code Elimination", false, false)
117
118FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }