BDCE.cpp revision 360784
12116Sjkh//===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===// 22116Sjkh// 32116Sjkh// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42116Sjkh// See https://llvm.org/LICENSE.txt for license information. 52116Sjkh// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 62116Sjkh// 72116Sjkh//===----------------------------------------------------------------------===// 82116Sjkh// 92116Sjkh// This file implements the Bit-Tracking Dead Code Elimination pass. Some 102116Sjkh// instructions (shifts, some ands, ors, etc.) kill some of their input bits. 118870Srgrimes// We track these dead bits and remove instructions that compute only these 122116Sjkh// dead bits. 132116Sjkh// 142116Sjkh//===----------------------------------------------------------------------===// 152116Sjkh 16176451Sdas#include "llvm/Transforms/Scalar/BDCE.h" 17176451Sdas#include "llvm/ADT/SmallPtrSet.h" 182116Sjkh#include "llvm/ADT/SmallVector.h" 192116Sjkh#include "llvm/ADT/Statistic.h" 202116Sjkh#include "llvm/Analysis/DemandedBits.h" 212116Sjkh#include "llvm/Analysis/GlobalsModRef.h" 222116Sjkh#include "llvm/IR/InstIterator.h" 232116Sjkh#include "llvm/IR/Instructions.h" 2497413Salfred#include "llvm/InitializePasses.h" 2597413Salfred#include "llvm/Pass.h" 262116Sjkh#include "llvm/Support/Debug.h" 272116Sjkh#include "llvm/Support/raw_ostream.h" 282116Sjkh#include "llvm/Transforms/Scalar.h" 292116Sjkh#include "llvm/Transforms/Utils/Local.h" 302116Sjkhusing namespace llvm; 312116Sjkh 322116Sjkh#define DEBUG_TYPE "bdce" 332116Sjkh 342116SjkhSTATISTIC(NumRemoved, "Number of instructions removed (unused)"); 352116SjkhSTATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)"); 362116Sjkh 372116Sjkh/// If an instruction is trivialized (dead), then the chain of users of that 382116Sjkh/// instruction may need to be cleared of assumptions that can no longer be 392116Sjkh/// guaranteed correct. 402116Sjkhstatic void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) { 412116Sjkh assert(I->getType()->isIntOrIntVectorTy() && 422116Sjkh "Trivializing a non-integer value?"); 432116Sjkh 442116Sjkh // Initialize the worklist with eligible direct users. 452116Sjkh SmallPtrSet<Instruction *, 16> Visited; 462116Sjkh SmallVector<Instruction *, 16> WorkList; 472116Sjkh for (User *JU : I->users()) { 482116Sjkh // If all bits of a user are demanded, then we know that nothing below that 492116Sjkh // in the def-use chain needs to be changed. 502116Sjkh auto *J = dyn_cast<Instruction>(JU); 51165839Sdas if (J && J->getType()->isIntOrIntVectorTy() && 52165839Sdas !DB.getDemandedBits(J).isAllOnesValue()) { 532116Sjkh Visited.insert(J); 542116Sjkh WorkList.push_back(J); 552116Sjkh } 562116Sjkh 572116Sjkh // Note that we need to check for non-int types above before asking for 58 // demanded bits. Normally, the only way to reach an instruction with an 59 // non-int type is via an instruction that has side effects (or otherwise 60 // will demand its input bits). However, if we have a readnone function 61 // that returns an unsized type (e.g., void), we must avoid asking for the 62 // demanded bits of the function call's return value. A void-returning 63 // readnone function is always dead (and so we can stop walking the use/def 64 // chain here), but the check is necessary to avoid asserting. 65 } 66 67 // DFS through subsequent users while tracking visits to avoid cycles. 68 while (!WorkList.empty()) { 69 Instruction *J = WorkList.pop_back_val(); 70 71 // NSW, NUW, and exact are based on operands that might have changed. 72 J->dropPoisonGeneratingFlags(); 73 74 // We do not have to worry about llvm.assume or range metadata: 75 // 1. llvm.assume demands its operand, so trivializing can't change it. 76 // 2. range metadata only applies to memory accesses which demand all bits. 77 78 for (User *KU : J->users()) { 79 // If all bits of a user are demanded, then we know that nothing below 80 // that in the def-use chain needs to be changed. 81 auto *K = dyn_cast<Instruction>(KU); 82 if (K && Visited.insert(K).second && K->getType()->isIntOrIntVectorTy() && 83 !DB.getDemandedBits(K).isAllOnesValue()) 84 WorkList.push_back(K); 85 } 86 } 87} 88 89static bool bitTrackingDCE(Function &F, DemandedBits &DB) { 90 SmallVector<Instruction*, 128> Worklist; 91 bool Changed = false; 92 for (Instruction &I : instructions(F)) { 93 // If the instruction has side effects and no non-dbg uses, 94 // skip it. This way we avoid computing known bits on an instruction 95 // that will not help us. 96 if (I.mayHaveSideEffects() && I.use_empty()) 97 continue; 98 99 // Remove instructions that are dead, either because they were not reached 100 // during analysis or have no demanded bits. 101 if (DB.isInstructionDead(&I) || 102 (I.getType()->isIntOrIntVectorTy() && 103 DB.getDemandedBits(&I).isNullValue() && 104 wouldInstructionBeTriviallyDead(&I))) { 105 salvageDebugInfoOrMarkUndef(I); 106 Worklist.push_back(&I); 107 I.dropAllReferences(); 108 Changed = true; 109 continue; 110 } 111 112 for (Use &U : I.operands()) { 113 // DemandedBits only detects dead integer uses. 114 if (!U->getType()->isIntOrIntVectorTy()) 115 continue; 116 117 if (!isa<Instruction>(U) && !isa<Argument>(U)) 118 continue; 119 120 if (!DB.isUseDead(&U)) 121 continue; 122 123 LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n"); 124 125 clearAssumptionsOfUsers(&I, DB); 126 127 // FIXME: In theory we could substitute undef here instead of zero. 128 // This should be reconsidered once we settle on the semantics of 129 // undef, poison, etc. 130 U.set(ConstantInt::get(U->getType(), 0)); 131 ++NumSimplified; 132 Changed = true; 133 } 134 } 135 136 for (Instruction *&I : Worklist) { 137 ++NumRemoved; 138 I->eraseFromParent(); 139 } 140 141 return Changed; 142} 143 144PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) { 145 auto &DB = AM.getResult<DemandedBitsAnalysis>(F); 146 if (!bitTrackingDCE(F, DB)) 147 return PreservedAnalyses::all(); 148 149 PreservedAnalyses PA; 150 PA.preserveSet<CFGAnalyses>(); 151 PA.preserve<GlobalsAA>(); 152 return PA; 153} 154 155namespace { 156struct BDCELegacyPass : public FunctionPass { 157 static char ID; // Pass identification, replacement for typeid 158 BDCELegacyPass() : FunctionPass(ID) { 159 initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry()); 160 } 161 162 bool runOnFunction(Function &F) override { 163 if (skipFunction(F)) 164 return false; 165 auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); 166 return bitTrackingDCE(F, DB); 167 } 168 169 void getAnalysisUsage(AnalysisUsage &AU) const override { 170 AU.setPreservesCFG(); 171 AU.addRequired<DemandedBitsWrapperPass>(); 172 AU.addPreserved<GlobalsAAWrapperPass>(); 173 } 174}; 175} 176 177char BDCELegacyPass::ID = 0; 178INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce", 179 "Bit-Tracking Dead Code Elimination", false, false) 180INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) 181INITIALIZE_PASS_END(BDCELegacyPass, "bdce", 182 "Bit-Tracking Dead Code Elimination", false, false) 183 184FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); } 185