Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 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 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/BDCE.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/DemandedBits.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/GlobalsModRef.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CFG.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstIterator.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Operator.h" |
| 27 | #include "llvm/Pass.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 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 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 38 | static bool bitTrackingDCE(Function &F, DemandedBits &DB) { |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 39 | SmallVector<Instruction*, 128> Worklist; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 40 | bool Changed = false; |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 41 | for (Instruction &I : instructions(F)) { |
Davide Italiano | 043e661 | 2016-12-06 21:52:47 +0000 | [diff] [blame^] | 42 | // If the instruction has side effects and no non-dbg uses, |
| 43 | // BDCE should skip it. |
| 44 | if (I.mayHaveSideEffects() && I.use_empty()) |
| 45 | continue; |
| 46 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 47 | if (I.getType()->isIntegerTy() && |
| 48 | !DB.getDemandedBits(&I).getBoolValue()) { |
| 49 | // For live instructions that have all dead bits, first make them dead by |
| 50 | // replacing all uses with something else. Then, if they don't need to |
| 51 | // remain live (because they have side effects, etc.) we can remove them. |
| 52 | DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n"); |
| 53 | // FIXME: In theory we could substitute undef here instead of zero. |
| 54 | // This should be reconsidered once we settle on the semantics of |
| 55 | // undef, poison, etc. |
| 56 | Value *Zero = ConstantInt::get(I.getType(), 0); |
| 57 | ++NumSimplified; |
| 58 | I.replaceAllUsesWith(Zero); |
| 59 | Changed = true; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 60 | } |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 61 | if (!DB.isInstructionDead(&I)) |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 62 | continue; |
| 63 | |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 64 | Worklist.push_back(&I); |
| 65 | I.dropAllReferences(); |
| 66 | Changed = true; |
| 67 | } |
| 68 | |
| 69 | for (Instruction *&I : Worklist) { |
| 70 | ++NumRemoved; |
| 71 | I->eraseFromParent(); |
| 72 | } |
| 73 | |
| 74 | return Changed; |
| 75 | } |
| 76 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 77 | PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 78 | auto &DB = AM.getResult<DemandedBitsAnalysis>(F); |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 79 | if (!bitTrackingDCE(F, DB)) |
| 80 | return PreservedAnalyses::all(); |
| 81 | |
Michael Kuperstein | 835facd | 2016-06-28 00:54:12 +0000 | [diff] [blame] | 82 | // FIXME: This should also 'preserve the CFG'. |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 83 | auto PA = PreservedAnalyses(); |
| 84 | PA.preserve<GlobalsAA>(); |
| 85 | return PA; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 88 | namespace { |
| 89 | struct BDCELegacyPass : public FunctionPass { |
| 90 | static char ID; // Pass identification, replacement for typeid |
| 91 | BDCELegacyPass() : FunctionPass(ID) { |
| 92 | initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 93 | } |
| 94 | |
| 95 | bool runOnFunction(Function &F) override { |
| 96 | if (skipFunction(F)) |
| 97 | return false; |
| 98 | auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); |
| 99 | return bitTrackingDCE(F, DB); |
| 100 | } |
| 101 | |
| 102 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 103 | AU.setPreservesCFG(); |
| 104 | AU.addRequired<DemandedBitsWrapperPass>(); |
| 105 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | char BDCELegacyPass::ID = 0; |
| 111 | INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce", |
| 112 | "Bit-Tracking Dead Code Elimination", false, false) |
| 113 | INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) |
| 114 | INITIALIZE_PASS_END(BDCELegacyPass, "bdce", |
| 115 | "Bit-Tracking Dead Code Elimination", false, false) |
| 116 | |
| 117 | FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); } |