LLVM  4.0.0
BDCE.cpp
Go to the documentation of this file.
1 //===---- 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 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/Debug.h"
30 #include "llvm/Transforms/Scalar.h"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "bdce"
34 
35 STATISTIC(NumRemoved, "Number of instructions removed (unused)");
36 STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
37 
38 static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
40  bool Changed = false;
41  for (Instruction &I : instructions(F)) {
42  // If the instruction has side effects and no non-dbg uses,
43  // skip it. This way we avoid computing known bits on an instruction
44  // that will not help us.
45  if (I.mayHaveSideEffects() && I.use_empty())
46  continue;
47 
48  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;
59  I.replaceNonMetadataUsesWith(Zero);
60  Changed = true;
61  }
62  if (!DB.isInstructionDead(&I))
63  continue;
64 
65  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 
79  auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
80  if (!bitTrackingDCE(F, DB))
81  return PreservedAnalyses::all();
82 
83  // FIXME: This should also 'preserve the CFG'.
84  auto PA = PreservedAnalyses();
85  PA.preserve<GlobalsAA>();
86  return PA;
87 }
88 
89 namespace {
90 struct BDCELegacyPass : public FunctionPass {
91  static char ID; // Pass identification, replacement for typeid
92  BDCELegacyPass() : FunctionPass(ID) {
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();
107  }
108 };
109 }
110 
111 char BDCELegacyPass::ID = 0;
112 INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
113  "Bit-Tracking Dead Code Elimination", false, false)
115 INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
116  "Bit-Tracking Dead Code Elimination", false, false)
117 
118 FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }
Legacy wrapper pass to provide the GlobalsAAResult object.
void initializeBDCELegacyPassPass(PassRegistry &)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
This is the interface for a simple mod/ref and alias analysis over globals.
bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: BDCE.cpp:78
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
static bool bitTrackingDCE(Function &F, DemandedBits &DB)
Definition: BDCE.cpp:38
An analysis that produces DemandedBits for a function.
Definition: DemandedBits.h:90
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:405
#define F(x, y, z)
Definition: MD5.cpp:51
Bit Tracking Dead Code false
Definition: BDCE.cpp:115
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass * createBitTrackingDCEPass()
Definition: BDCE.cpp:118
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
Bit Tracking Dead Code Elimination
Definition: BDCE.cpp:115
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:558
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bdce
Definition: BDCE.cpp:115
#define I(x, y, z)
Definition: MD5.cpp:54
INITIALIZE_PASS_BEGIN(BDCELegacyPass,"bdce","Bit-Tracking Dead Code Elimination", false, false) INITIALIZE_PASS_END(BDCELegacyPass
static volatile int Zero
LLVM Value Representation.
Definition: Value.h:71
#define DEBUG(X)
Definition: Debug.h:100
inst_range instructions(Function *F)
Definition: InstIterator.h:132
A container for analyses that lazily runs them and caches their results.