12bb61ba2SHal Finkel //===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===//
22bb61ba2SHal Finkel //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62bb61ba2SHal Finkel //
72bb61ba2SHal Finkel //===----------------------------------------------------------------------===//
82bb61ba2SHal Finkel //
92bb61ba2SHal Finkel // This file implements the Bit-Tracking Dead Code Elimination pass. Some
102bb61ba2SHal Finkel // instructions (shifts, some ands, ors, etc.) kill some of their input bits.
112bb61ba2SHal Finkel // We track these dead bits and remove instructions that compute only these
12*7bf16839SDiogo Sampaio // dead bits. We also simplify sext that generates unused extension bits,
13*7bf16839SDiogo Sampaio // converting it to a zext.
142bb61ba2SHal Finkel //
152bb61ba2SHal Finkel //===----------------------------------------------------------------------===//
162bb61ba2SHal Finkel 
17655a145eSDavide Italiano #include "llvm/Transforms/Scalar/BDCE.h"
18fe346f9fSSanjay Patel #include "llvm/ADT/SmallPtrSet.h"
192bb61ba2SHal Finkel #include "llvm/ADT/SmallVector.h"
202bb61ba2SHal Finkel #include "llvm/ADT/Statistic.h"
2187405c7fSJames Molloy #include "llvm/Analysis/DemandedBits.h"
22655a145eSDavide Italiano #include "llvm/Analysis/GlobalsModRef.h"
23*7bf16839SDiogo Sampaio #include "llvm/IR/IRBuilder.h"
242bb61ba2SHal Finkel #include "llvm/IR/InstIterator.h"
252bb61ba2SHal Finkel #include "llvm/IR/Instructions.h"
2605da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
272bb61ba2SHal Finkel #include "llvm/Pass.h"
282bb61ba2SHal Finkel #include "llvm/Support/Debug.h"
292bb61ba2SHal Finkel #include "llvm/Support/raw_ostream.h"
30655a145eSDavide Italiano #include "llvm/Transforms/Scalar.h"
3105da2fe5SReid Kleckner #include "llvm/Transforms/Utils/Local.h"
322bb61ba2SHal Finkel using namespace llvm;
332bb61ba2SHal Finkel 
342bb61ba2SHal Finkel #define DEBUG_TYPE "bdce"
352bb61ba2SHal Finkel 
362bb61ba2SHal Finkel STATISTIC(NumRemoved, "Number of instructions removed (unused)");
372bb61ba2SHal Finkel STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
38*7bf16839SDiogo Sampaio STATISTIC(NumSExt2ZExt,
39*7bf16839SDiogo Sampaio           "Number of sign extension instructions converted to zero extension");
402bb61ba2SHal Finkel 
41fe346f9fSSanjay Patel /// If an instruction is trivialized (dead), then the chain of users of that
42fe346f9fSSanjay Patel /// instruction may need to be cleared of assumptions that can no longer be
43fe346f9fSSanjay Patel /// guaranteed correct.
44fe346f9fSSanjay Patel static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
45110cf052SNikita Popov   assert(I->getType()->isIntOrIntVectorTy() &&
46110cf052SNikita Popov          "Trivializing a non-integer value?");
47fe346f9fSSanjay Patel 
48fe346f9fSSanjay Patel   // Initialize the worklist with eligible direct users.
49b0f764c7SFangrui Song   SmallPtrSet<Instruction *, 16> Visited;
50fe346f9fSSanjay Patel   SmallVector<Instruction *, 16> WorkList;
51fe346f9fSSanjay Patel   for (User *JU : I->users()) {
52a1067d9bSSanjay Patel     // If all bits of a user are demanded, then we know that nothing below that
53a1067d9bSSanjay Patel     // in the def-use chain needs to be changed.
54fe346f9fSSanjay Patel     auto *J = dyn_cast<Instruction>(JU);
55110cf052SNikita Popov     if (J && J->getType()->isIntOrIntVectorTy() &&
56b0f764c7SFangrui Song         !DB.getDemandedBits(J).isAllOnesValue()) {
57b0f764c7SFangrui Song       Visited.insert(J);
58fe346f9fSSanjay Patel       WorkList.push_back(J);
59b0f764c7SFangrui Song     }
609e54b709SHal Finkel 
61110cf052SNikita Popov     // Note that we need to check for non-int types above before asking for
629e54b709SHal Finkel     // demanded bits. Normally, the only way to reach an instruction with an
63110cf052SNikita Popov     // non-int type is via an instruction that has side effects (or otherwise
649e54b709SHal Finkel     // will demand its input bits). However, if we have a readnone function
659e54b709SHal Finkel     // that returns an unsized type (e.g., void), we must avoid asking for the
669e54b709SHal Finkel     // demanded bits of the function call's return value. A void-returning
679e54b709SHal Finkel     // readnone function is always dead (and so we can stop walking the use/def
689e54b709SHal Finkel     // chain here), but the check is necessary to avoid asserting.
69fe346f9fSSanjay Patel   }
70fe346f9fSSanjay Patel 
71fe346f9fSSanjay Patel   // DFS through subsequent users while tracking visits to avoid cycles.
72fe346f9fSSanjay Patel   while (!WorkList.empty()) {
73fe346f9fSSanjay Patel     Instruction *J = WorkList.pop_back_val();
74fe346f9fSSanjay Patel 
75fe346f9fSSanjay Patel     // NSW, NUW, and exact are based on operands that might have changed.
76fe346f9fSSanjay Patel     J->dropPoisonGeneratingFlags();
77fe346f9fSSanjay Patel 
78fe346f9fSSanjay Patel     // We do not have to worry about llvm.assume or range metadata:
79fe346f9fSSanjay Patel     // 1. llvm.assume demands its operand, so trivializing can't change it.
80fe346f9fSSanjay Patel     // 2. range metadata only applies to memory accesses which demand all bits.
81fe346f9fSSanjay Patel 
82fe346f9fSSanjay Patel     for (User *KU : J->users()) {
83a1067d9bSSanjay Patel       // If all bits of a user are demanded, then we know that nothing below
84a1067d9bSSanjay Patel       // that in the def-use chain needs to be changed.
85fe346f9fSSanjay Patel       auto *K = dyn_cast<Instruction>(KU);
86b0f764c7SFangrui Song       if (K && Visited.insert(K).second && K->getType()->isIntOrIntVectorTy() &&
879e54b709SHal Finkel           !DB.getDemandedBits(K).isAllOnesValue())
88fe346f9fSSanjay Patel         WorkList.push_back(K);
89fe346f9fSSanjay Patel     }
90fe346f9fSSanjay Patel   }
91fe346f9fSSanjay Patel }
92fe346f9fSSanjay Patel 
93655a145eSDavide Italiano static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
942bb61ba2SHal Finkel   SmallVector<Instruction*, 128> Worklist;
952bb61ba2SHal Finkel   bool Changed = false;
9678199518SNico Rieck   for (Instruction &I : instructions(F)) {
97043e6613SDavide Italiano     // If the instruction has side effects and no non-dbg uses,
981ed53963SDavide Italiano     // skip it. This way we avoid computing known bits on an instruction
991ed53963SDavide Italiano     // that will not help us.
100043e6613SDavide Italiano     if (I.mayHaveSideEffects() && I.use_empty())
101043e6613SDavide Italiano       continue;
102043e6613SDavide Italiano 
103cc6ef7f1SNikita Popov     // Remove instructions that are dead, either because they were not reached
104cc6ef7f1SNikita Popov     // during analysis or have no demanded bits.
105cc6ef7f1SNikita Popov     if (DB.isInstructionDead(&I) ||
106cc6ef7f1SNikita Popov         (I.getType()->isIntOrIntVectorTy() &&
107cc6ef7f1SNikita Popov          DB.getDemandedBits(&I).isNullValue() &&
108cc6ef7f1SNikita Popov          wouldInstructionBeTriviallyDead(&I))) {
109c6c65164SChris Jackson       salvageDebugInfo(I);
110bc9986e9SNikita Popov       Worklist.push_back(&I);
111bc9986e9SNikita Popov       I.dropAllReferences();
112bc9986e9SNikita Popov       Changed = true;
113bc9986e9SNikita Popov       continue;
114bc9986e9SNikita Popov     }
115bc9986e9SNikita Popov 
116*7bf16839SDiogo Sampaio     // Convert SExt into ZExt if none of the extension bits is required
117*7bf16839SDiogo Sampaio     if (SExtInst *SE = dyn_cast<SExtInst>(&I)) {
118*7bf16839SDiogo Sampaio       APInt Demanded = DB.getDemandedBits(SE);
119*7bf16839SDiogo Sampaio       const uint32_t SrcBitSize = SE->getSrcTy()->getScalarSizeInBits();
120*7bf16839SDiogo Sampaio       auto *const DstTy = SE->getDestTy();
121*7bf16839SDiogo Sampaio       const uint32_t DestBitSize = DstTy->getScalarSizeInBits();
122*7bf16839SDiogo Sampaio       if (Demanded.countLeadingZeros() >= (DestBitSize - SrcBitSize)) {
123*7bf16839SDiogo Sampaio         clearAssumptionsOfUsers(SE, DB);
124*7bf16839SDiogo Sampaio         IRBuilder<> Builder(SE);
125*7bf16839SDiogo Sampaio         I.replaceAllUsesWith(
126*7bf16839SDiogo Sampaio             Builder.CreateZExt(SE->getOperand(0), DstTy, SE->getName()));
127*7bf16839SDiogo Sampaio         Worklist.push_back(SE);
128*7bf16839SDiogo Sampaio         Changed = true;
129*7bf16839SDiogo Sampaio         NumSExt2ZExt++;
130*7bf16839SDiogo Sampaio         continue;
131*7bf16839SDiogo Sampaio       }
132*7bf16839SDiogo Sampaio     }
133*7bf16839SDiogo Sampaio 
134bc9986e9SNikita Popov     for (Use &U : I.operands()) {
135bc9986e9SNikita Popov       // DemandedBits only detects dead integer uses.
136bc9986e9SNikita Popov       if (!U->getType()->isIntOrIntVectorTy())
137bc9986e9SNikita Popov         continue;
138bc9986e9SNikita Popov 
1396658fce4SNikita Popov       if (!isa<Instruction>(U) && !isa<Argument>(U))
140bc9986e9SNikita Popov         continue;
141bc9986e9SNikita Popov 
142bc9986e9SNikita Popov       if (!DB.isUseDead(&U))
143bc9986e9SNikita Popov         continue;
144bc9986e9SNikita Popov 
145bc9986e9SNikita Popov       LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n");
146fe346f9fSSanjay Patel 
147fe346f9fSSanjay Patel       clearAssumptionsOfUsers(&I, DB);
148fe346f9fSSanjay Patel 
1492bb61ba2SHal Finkel       // FIXME: In theory we could substitute undef here instead of zero.
1502bb61ba2SHal Finkel       // This should be reconsidered once we settle on the semantics of
1512bb61ba2SHal Finkel       // undef, poison, etc.
152bc9986e9SNikita Popov       U.set(ConstantInt::get(U->getType(), 0));
1532bb61ba2SHal Finkel       ++NumSimplified;
1542bb61ba2SHal Finkel       Changed = true;
1552bb61ba2SHal Finkel     }
1562bb61ba2SHal Finkel   }
1572bb61ba2SHal Finkel 
1582bb61ba2SHal Finkel   for (Instruction *&I : Worklist) {
1592bb61ba2SHal Finkel     ++NumRemoved;
1602bb61ba2SHal Finkel     I->eraseFromParent();
1612bb61ba2SHal Finkel   }
1622bb61ba2SHal Finkel 
1632bb61ba2SHal Finkel   return Changed;
1642bb61ba2SHal Finkel }
1652bb61ba2SHal Finkel 
166655a145eSDavide Italiano PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
167655a145eSDavide Italiano   auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
168bdc29714SDavide Italiano   if (!bitTrackingDCE(F, DB))
169655a145eSDavide Italiano     return PreservedAnalyses::all();
170bdc29714SDavide Italiano 
171ca68a3ecSChandler Carruth   PreservedAnalyses PA;
172ca68a3ecSChandler Carruth   PA.preserveSet<CFGAnalyses>();
173bdc29714SDavide Italiano   PA.preserve<GlobalsAA>();
174bdc29714SDavide Italiano   return PA;
1752bb61ba2SHal Finkel }
1762bb61ba2SHal Finkel 
177655a145eSDavide Italiano namespace {
178655a145eSDavide Italiano struct BDCELegacyPass : public FunctionPass {
179655a145eSDavide Italiano   static char ID; // Pass identification, replacement for typeid
180655a145eSDavide Italiano   BDCELegacyPass() : FunctionPass(ID) {
181655a145eSDavide Italiano     initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
182655a145eSDavide Italiano   }
183655a145eSDavide Italiano 
184655a145eSDavide Italiano   bool runOnFunction(Function &F) override {
185655a145eSDavide Italiano     if (skipFunction(F))
186655a145eSDavide Italiano       return false;
187655a145eSDavide Italiano     auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
188655a145eSDavide Italiano     return bitTrackingDCE(F, DB);
189655a145eSDavide Italiano   }
190655a145eSDavide Italiano 
191655a145eSDavide Italiano   void getAnalysisUsage(AnalysisUsage &AU) const override {
192655a145eSDavide Italiano     AU.setPreservesCFG();
193655a145eSDavide Italiano     AU.addRequired<DemandedBitsWrapperPass>();
194655a145eSDavide Italiano     AU.addPreserved<GlobalsAAWrapperPass>();
195655a145eSDavide Italiano   }
196655a145eSDavide Italiano };
197655a145eSDavide Italiano }
198655a145eSDavide Italiano 
199655a145eSDavide Italiano char BDCELegacyPass::ID = 0;
200655a145eSDavide Italiano INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
201655a145eSDavide Italiano                       "Bit-Tracking Dead Code Elimination", false, false)
202655a145eSDavide Italiano INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
203655a145eSDavide Italiano INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
204655a145eSDavide Italiano                     "Bit-Tracking Dead Code Elimination", false, false)
205655a145eSDavide Italiano 
206655a145eSDavide Italiano FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }
207