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 17 #include "llvm/Transforms/Scalar/BDCE.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/DemandedBits.h" 21 #include "llvm/Analysis/GlobalsModRef.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" 29 #include "llvm/Support/raw_ostream.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) { 39 SmallVector<Instruction*, 128> Worklist; 40 bool Changed = false; 41 for (Instruction &I : instructions(F)) { 42 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; 55 } 56 if (!DB.isInstructionDead(&I)) 57 continue; 58 59 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 72 PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) { 73 auto &DB = AM.getResult<DemandedBitsAnalysis>(F); 74 if (!bitTrackingDCE(F, DB)) 75 return PreservedAnalyses::all(); 76 77 // FIXME: BDCE should also 'preserve the CFG'. 78 // The new pass manager has currently no way to do it. 79 auto PA = PreservedAnalyses(); 80 PA.preserve<GlobalsAA>(); 81 return PA; 82 } 83 84 namespace { 85 struct BDCELegacyPass : public FunctionPass { 86 static char ID; // Pass identification, replacement for typeid 87 BDCELegacyPass() : FunctionPass(ID) { 88 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry()); 89 } 90 91 bool runOnFunction(Function &F) override { 92 if (skipFunction(F)) 93 return false; 94 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); 95 return bitTrackingDCE(F, DB); 96 } 97 98 void getAnalysisUsage(AnalysisUsage &AU) const override { 99 AU.setPreservesCFG(); 100 AU.addRequired<DemandedBitsWrapperPass>(); 101 AU.addPreserved<GlobalsAAWrapperPass>(); 102 } 103 }; 104 } 105 106 char BDCELegacyPass::ID = 0; 107 INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce", 108 "Bit-Tracking Dead Code Elimination", false, false) 109 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) 110 INITIALIZE_PASS_END(BDCELegacyPass, "bdce", 111 "Bit-Tracking Dead Code Elimination", false, false) 112 113 FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); } 114