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 | |
| 17 | #include "llvm/Transforms/Scalar.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 | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/GlobalsModRef.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/DemandedBits.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" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "bdce" |
| 33 | |
| 34 | STATISTIC(NumRemoved, "Number of instructions removed (unused)"); |
| 35 | STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)"); |
| 36 | |
| 37 | namespace { |
| 38 | struct BDCE : public FunctionPass { |
| 39 | static char ID; // Pass identification, replacement for typeid |
| 40 | BDCE() : FunctionPass(ID) { |
| 41 | initializeBDCEPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
| 43 | |
| 44 | bool runOnFunction(Function& F) override; |
| 45 | |
| 46 | void getAnalysisUsage(AnalysisUsage& AU) const override { |
| 47 | AU.setPreservesCFG(); |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 48 | AU.addRequired<DemandedBitsWrapperPass>(); |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 49 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 50 | } |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 51 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 52 | } |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 53 | |
| 54 | char BDCE::ID = 0; |
| 55 | INITIALIZE_PASS_BEGIN(BDCE, "bdce", "Bit-Tracking Dead Code Elimination", |
| 56 | false, false) |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 57 | INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 58 | INITIALIZE_PASS_END(BDCE, "bdce", "Bit-Tracking Dead Code Elimination", |
| 59 | false, false) |
| 60 | |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 61 | bool BDCE::runOnFunction(Function& F) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 62 | if (skipFunction(F)) |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 63 | return false; |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 64 | auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 65 | |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 66 | SmallVector<Instruction*, 128> Worklist; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 67 | bool Changed = false; |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 68 | for (Instruction &I : instructions(F)) { |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 69 | if (I.getType()->isIntegerTy() && |
| 70 | !DB.getDemandedBits(&I).getBoolValue()) { |
| 71 | // For live instructions that have all dead bits, first make them dead by |
| 72 | // replacing all uses with something else. Then, if they don't need to |
| 73 | // remain live (because they have side effects, etc.) we can remove them. |
| 74 | DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n"); |
| 75 | // FIXME: In theory we could substitute undef here instead of zero. |
| 76 | // This should be reconsidered once we settle on the semantics of |
| 77 | // undef, poison, etc. |
| 78 | Value *Zero = ConstantInt::get(I.getType(), 0); |
| 79 | ++NumSimplified; |
| 80 | I.replaceAllUsesWith(Zero); |
| 81 | Changed = true; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 82 | } |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 83 | if (!DB.isInstructionDead(&I)) |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 84 | continue; |
| 85 | |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 86 | Worklist.push_back(&I); |
| 87 | I.dropAllReferences(); |
| 88 | Changed = true; |
| 89 | } |
| 90 | |
| 91 | for (Instruction *&I : Worklist) { |
| 92 | ++NumRemoved; |
| 93 | I->eraseFromParent(); |
| 94 | } |
| 95 | |
| 96 | return Changed; |
| 97 | } |
| 98 | |
| 99 | FunctionPass *llvm::createBitTrackingDCEPass() { |
| 100 | return new BDCE(); |
| 101 | } |
| 102 | |