1*9880d681SAndroid Build Coastguard Worker //===---- DemandedBits.cpp - Determine demanded bits ----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass implements a demanded bits analysis. A demanded bit is one that
11*9880d681SAndroid Build Coastguard Worker // contributes to a result; bits that are not demanded can be either zero or
12*9880d681SAndroid Build Coastguard Worker // one without affecting control or data flow. For example in this sequence:
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker // %1 = add i32 %x, %y
15*9880d681SAndroid Build Coastguard Worker // %2 = trunc i32 %1 to i16
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
18*9880d681SAndroid Build Coastguard Worker // trunc.
19*9880d681SAndroid Build Coastguard Worker //
20*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/DemandedBits.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstIterator.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
41*9880d681SAndroid Build Coastguard Worker using namespace llvm;
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "demanded-bits"
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker char DemandedBitsWrapperPass::ID = 0;
46*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
47*9880d681SAndroid Build Coastguard Worker "Demanded bits analysis", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)48*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
49*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
50*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
51*9880d681SAndroid Build Coastguard Worker "Demanded bits analysis", false, false)
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
54*9880d681SAndroid Build Coastguard Worker initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const57*9880d681SAndroid Build Coastguard Worker void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
58*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
59*9880d681SAndroid Build Coastguard Worker AU.addRequired<AssumptionCacheTracker>();
60*9880d681SAndroid Build Coastguard Worker AU.addRequired<DominatorTreeWrapperPass>();
61*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
62*9880d681SAndroid Build Coastguard Worker }
63*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,const Module * M) const64*9880d681SAndroid Build Coastguard Worker void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
65*9880d681SAndroid Build Coastguard Worker DB->print(OS);
66*9880d681SAndroid Build Coastguard Worker }
67*9880d681SAndroid Build Coastguard Worker
isAlwaysLive(Instruction * I)68*9880d681SAndroid Build Coastguard Worker static bool isAlwaysLive(Instruction *I) {
69*9880d681SAndroid Build Coastguard Worker return isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
70*9880d681SAndroid Build Coastguard Worker I->isEHPad() || I->mayHaveSideEffects();
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
determineLiveOperandBits(const Instruction * UserI,const Instruction * I,unsigned OperandNo,const APInt & AOut,APInt & AB,APInt & KnownZero,APInt & KnownOne,APInt & KnownZero2,APInt & KnownOne2)73*9880d681SAndroid Build Coastguard Worker void DemandedBits::determineLiveOperandBits(
74*9880d681SAndroid Build Coastguard Worker const Instruction *UserI, const Instruction *I, unsigned OperandNo,
75*9880d681SAndroid Build Coastguard Worker const APInt &AOut, APInt &AB, APInt &KnownZero, APInt &KnownOne,
76*9880d681SAndroid Build Coastguard Worker APInt &KnownZero2, APInt &KnownOne2) {
77*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = AB.getBitWidth();
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker // We're called once per operand, but for some instructions, we need to
80*9880d681SAndroid Build Coastguard Worker // compute known bits of both operands in order to determine the live bits of
81*9880d681SAndroid Build Coastguard Worker // either (when both operands are instructions themselves). We don't,
82*9880d681SAndroid Build Coastguard Worker // however, want to do this twice, so we cache the result in APInts that live
83*9880d681SAndroid Build Coastguard Worker // in the caller. For the two-relevant-operands case, both operand values are
84*9880d681SAndroid Build Coastguard Worker // provided here.
85*9880d681SAndroid Build Coastguard Worker auto ComputeKnownBits =
86*9880d681SAndroid Build Coastguard Worker [&](unsigned BitWidth, const Value *V1, const Value *V2) {
87*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = I->getModule()->getDataLayout();
88*9880d681SAndroid Build Coastguard Worker KnownZero = APInt(BitWidth, 0);
89*9880d681SAndroid Build Coastguard Worker KnownOne = APInt(BitWidth, 0);
90*9880d681SAndroid Build Coastguard Worker computeKnownBits(const_cast<Value *>(V1), KnownZero, KnownOne, DL, 0,
91*9880d681SAndroid Build Coastguard Worker &AC, UserI, &DT);
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker if (V2) {
94*9880d681SAndroid Build Coastguard Worker KnownZero2 = APInt(BitWidth, 0);
95*9880d681SAndroid Build Coastguard Worker KnownOne2 = APInt(BitWidth, 0);
96*9880d681SAndroid Build Coastguard Worker computeKnownBits(const_cast<Value *>(V2), KnownZero2, KnownOne2, DL,
97*9880d681SAndroid Build Coastguard Worker 0, &AC, UserI, &DT);
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker };
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker switch (UserI->getOpcode()) {
102*9880d681SAndroid Build Coastguard Worker default: break;
103*9880d681SAndroid Build Coastguard Worker case Instruction::Call:
104*9880d681SAndroid Build Coastguard Worker case Instruction::Invoke:
105*9880d681SAndroid Build Coastguard Worker if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
106*9880d681SAndroid Build Coastguard Worker switch (II->getIntrinsicID()) {
107*9880d681SAndroid Build Coastguard Worker default: break;
108*9880d681SAndroid Build Coastguard Worker case Intrinsic::bswap:
109*9880d681SAndroid Build Coastguard Worker // The alive bits of the input are the swapped alive bits of
110*9880d681SAndroid Build Coastguard Worker // the output.
111*9880d681SAndroid Build Coastguard Worker AB = AOut.byteSwap();
112*9880d681SAndroid Build Coastguard Worker break;
113*9880d681SAndroid Build Coastguard Worker case Intrinsic::ctlz:
114*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0) {
115*9880d681SAndroid Build Coastguard Worker // We need some output bits, so we need all bits of the
116*9880d681SAndroid Build Coastguard Worker // input to the left of, and including, the leftmost bit
117*9880d681SAndroid Build Coastguard Worker // known to be one.
118*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, I, nullptr);
119*9880d681SAndroid Build Coastguard Worker AB = APInt::getHighBitsSet(BitWidth,
120*9880d681SAndroid Build Coastguard Worker std::min(BitWidth, KnownOne.countLeadingZeros()+1));
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker break;
123*9880d681SAndroid Build Coastguard Worker case Intrinsic::cttz:
124*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0) {
125*9880d681SAndroid Build Coastguard Worker // We need some output bits, so we need all bits of the
126*9880d681SAndroid Build Coastguard Worker // input to the right of, and including, the rightmost bit
127*9880d681SAndroid Build Coastguard Worker // known to be one.
128*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, I, nullptr);
129*9880d681SAndroid Build Coastguard Worker AB = APInt::getLowBitsSet(BitWidth,
130*9880d681SAndroid Build Coastguard Worker std::min(BitWidth, KnownOne.countTrailingZeros()+1));
131*9880d681SAndroid Build Coastguard Worker }
132*9880d681SAndroid Build Coastguard Worker break;
133*9880d681SAndroid Build Coastguard Worker }
134*9880d681SAndroid Build Coastguard Worker break;
135*9880d681SAndroid Build Coastguard Worker case Instruction::Add:
136*9880d681SAndroid Build Coastguard Worker case Instruction::Sub:
137*9880d681SAndroid Build Coastguard Worker case Instruction::Mul:
138*9880d681SAndroid Build Coastguard Worker // Find the highest live output bit. We don't need any more input
139*9880d681SAndroid Build Coastguard Worker // bits than that (adds, and thus subtracts, ripple only to the
140*9880d681SAndroid Build Coastguard Worker // left).
141*9880d681SAndroid Build Coastguard Worker AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
142*9880d681SAndroid Build Coastguard Worker break;
143*9880d681SAndroid Build Coastguard Worker case Instruction::Shl:
144*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0)
145*9880d681SAndroid Build Coastguard Worker if (ConstantInt *CI =
146*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantInt>(UserI->getOperand(1))) {
147*9880d681SAndroid Build Coastguard Worker uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
148*9880d681SAndroid Build Coastguard Worker AB = AOut.lshr(ShiftAmt);
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker // If the shift is nuw/nsw, then the high bits are not dead
151*9880d681SAndroid Build Coastguard Worker // (because we've promised that they *must* be zero).
152*9880d681SAndroid Build Coastguard Worker const ShlOperator *S = cast<ShlOperator>(UserI);
153*9880d681SAndroid Build Coastguard Worker if (S->hasNoSignedWrap())
154*9880d681SAndroid Build Coastguard Worker AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
155*9880d681SAndroid Build Coastguard Worker else if (S->hasNoUnsignedWrap())
156*9880d681SAndroid Build Coastguard Worker AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
157*9880d681SAndroid Build Coastguard Worker }
158*9880d681SAndroid Build Coastguard Worker break;
159*9880d681SAndroid Build Coastguard Worker case Instruction::LShr:
160*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0)
161*9880d681SAndroid Build Coastguard Worker if (ConstantInt *CI =
162*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantInt>(UserI->getOperand(1))) {
163*9880d681SAndroid Build Coastguard Worker uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
164*9880d681SAndroid Build Coastguard Worker AB = AOut.shl(ShiftAmt);
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker // If the shift is exact, then the low bits are not dead
167*9880d681SAndroid Build Coastguard Worker // (they must be zero).
168*9880d681SAndroid Build Coastguard Worker if (cast<LShrOperator>(UserI)->isExact())
169*9880d681SAndroid Build Coastguard Worker AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
170*9880d681SAndroid Build Coastguard Worker }
171*9880d681SAndroid Build Coastguard Worker break;
172*9880d681SAndroid Build Coastguard Worker case Instruction::AShr:
173*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0)
174*9880d681SAndroid Build Coastguard Worker if (ConstantInt *CI =
175*9880d681SAndroid Build Coastguard Worker dyn_cast<ConstantInt>(UserI->getOperand(1))) {
176*9880d681SAndroid Build Coastguard Worker uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
177*9880d681SAndroid Build Coastguard Worker AB = AOut.shl(ShiftAmt);
178*9880d681SAndroid Build Coastguard Worker // Because the high input bit is replicated into the
179*9880d681SAndroid Build Coastguard Worker // high-order bits of the result, if we need any of those
180*9880d681SAndroid Build Coastguard Worker // bits, then we must keep the highest input bit.
181*9880d681SAndroid Build Coastguard Worker if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
182*9880d681SAndroid Build Coastguard Worker .getBoolValue())
183*9880d681SAndroid Build Coastguard Worker AB.setBit(BitWidth-1);
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker // If the shift is exact, then the low bits are not dead
186*9880d681SAndroid Build Coastguard Worker // (they must be zero).
187*9880d681SAndroid Build Coastguard Worker if (cast<AShrOperator>(UserI)->isExact())
188*9880d681SAndroid Build Coastguard Worker AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker break;
191*9880d681SAndroid Build Coastguard Worker case Instruction::And:
192*9880d681SAndroid Build Coastguard Worker AB = AOut;
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // For bits that are known zero, the corresponding bits in the
195*9880d681SAndroid Build Coastguard Worker // other operand are dead (unless they're both zero, in which
196*9880d681SAndroid Build Coastguard Worker // case they can't both be dead, so just mark the LHS bits as
197*9880d681SAndroid Build Coastguard Worker // dead).
198*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0) {
199*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
200*9880d681SAndroid Build Coastguard Worker AB &= ~KnownZero2;
201*9880d681SAndroid Build Coastguard Worker } else {
202*9880d681SAndroid Build Coastguard Worker if (!isa<Instruction>(UserI->getOperand(0)))
203*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
204*9880d681SAndroid Build Coastguard Worker AB &= ~(KnownZero & ~KnownZero2);
205*9880d681SAndroid Build Coastguard Worker }
206*9880d681SAndroid Build Coastguard Worker break;
207*9880d681SAndroid Build Coastguard Worker case Instruction::Or:
208*9880d681SAndroid Build Coastguard Worker AB = AOut;
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker // For bits that are known one, the corresponding bits in the
211*9880d681SAndroid Build Coastguard Worker // other operand are dead (unless they're both one, in which
212*9880d681SAndroid Build Coastguard Worker // case they can't both be dead, so just mark the LHS bits as
213*9880d681SAndroid Build Coastguard Worker // dead).
214*9880d681SAndroid Build Coastguard Worker if (OperandNo == 0) {
215*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
216*9880d681SAndroid Build Coastguard Worker AB &= ~KnownOne2;
217*9880d681SAndroid Build Coastguard Worker } else {
218*9880d681SAndroid Build Coastguard Worker if (!isa<Instruction>(UserI->getOperand(0)))
219*9880d681SAndroid Build Coastguard Worker ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
220*9880d681SAndroid Build Coastguard Worker AB &= ~(KnownOne & ~KnownOne2);
221*9880d681SAndroid Build Coastguard Worker }
222*9880d681SAndroid Build Coastguard Worker break;
223*9880d681SAndroid Build Coastguard Worker case Instruction::Xor:
224*9880d681SAndroid Build Coastguard Worker case Instruction::PHI:
225*9880d681SAndroid Build Coastguard Worker AB = AOut;
226*9880d681SAndroid Build Coastguard Worker break;
227*9880d681SAndroid Build Coastguard Worker case Instruction::Trunc:
228*9880d681SAndroid Build Coastguard Worker AB = AOut.zext(BitWidth);
229*9880d681SAndroid Build Coastguard Worker break;
230*9880d681SAndroid Build Coastguard Worker case Instruction::ZExt:
231*9880d681SAndroid Build Coastguard Worker AB = AOut.trunc(BitWidth);
232*9880d681SAndroid Build Coastguard Worker break;
233*9880d681SAndroid Build Coastguard Worker case Instruction::SExt:
234*9880d681SAndroid Build Coastguard Worker AB = AOut.trunc(BitWidth);
235*9880d681SAndroid Build Coastguard Worker // Because the high input bit is replicated into the
236*9880d681SAndroid Build Coastguard Worker // high-order bits of the result, if we need any of those
237*9880d681SAndroid Build Coastguard Worker // bits, then we must keep the highest input bit.
238*9880d681SAndroid Build Coastguard Worker if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
239*9880d681SAndroid Build Coastguard Worker AOut.getBitWidth() - BitWidth))
240*9880d681SAndroid Build Coastguard Worker .getBoolValue())
241*9880d681SAndroid Build Coastguard Worker AB.setBit(BitWidth-1);
242*9880d681SAndroid Build Coastguard Worker break;
243*9880d681SAndroid Build Coastguard Worker case Instruction::Select:
244*9880d681SAndroid Build Coastguard Worker if (OperandNo != 0)
245*9880d681SAndroid Build Coastguard Worker AB = AOut;
246*9880d681SAndroid Build Coastguard Worker break;
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)250*9880d681SAndroid Build Coastguard Worker bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
251*9880d681SAndroid Build Coastguard Worker auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
252*9880d681SAndroid Build Coastguard Worker auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
253*9880d681SAndroid Build Coastguard Worker DB.emplace(F, AC, DT);
254*9880d681SAndroid Build Coastguard Worker return false;
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker
releaseMemory()257*9880d681SAndroid Build Coastguard Worker void DemandedBitsWrapperPass::releaseMemory() {
258*9880d681SAndroid Build Coastguard Worker DB.reset();
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker
performAnalysis()261*9880d681SAndroid Build Coastguard Worker void DemandedBits::performAnalysis() {
262*9880d681SAndroid Build Coastguard Worker if (Analyzed)
263*9880d681SAndroid Build Coastguard Worker // Analysis already completed for this function.
264*9880d681SAndroid Build Coastguard Worker return;
265*9880d681SAndroid Build Coastguard Worker Analyzed = true;
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker Visited.clear();
268*9880d681SAndroid Build Coastguard Worker AliveBits.clear();
269*9880d681SAndroid Build Coastguard Worker
270*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction*, 128> Worklist;
271*9880d681SAndroid Build Coastguard Worker
272*9880d681SAndroid Build Coastguard Worker // Collect the set of "root" instructions that are known live.
273*9880d681SAndroid Build Coastguard Worker for (Instruction &I : instructions(F)) {
274*9880d681SAndroid Build Coastguard Worker if (!isAlwaysLive(&I))
275*9880d681SAndroid Build Coastguard Worker continue;
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
278*9880d681SAndroid Build Coastguard Worker // For integer-valued instructions, set up an initial empty set of alive
279*9880d681SAndroid Build Coastguard Worker // bits and add the instruction to the work list. For other instructions
280*9880d681SAndroid Build Coastguard Worker // add their operands to the work list (for integer values operands, mark
281*9880d681SAndroid Build Coastguard Worker // all bits as live).
282*9880d681SAndroid Build Coastguard Worker if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
283*9880d681SAndroid Build Coastguard Worker if (!AliveBits.count(&I)) {
284*9880d681SAndroid Build Coastguard Worker AliveBits[&I] = APInt(IT->getBitWidth(), 0);
285*9880d681SAndroid Build Coastguard Worker Worklist.push_back(&I);
286*9880d681SAndroid Build Coastguard Worker }
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker continue;
289*9880d681SAndroid Build Coastguard Worker }
290*9880d681SAndroid Build Coastguard Worker
291*9880d681SAndroid Build Coastguard Worker // Non-integer-typed instructions...
292*9880d681SAndroid Build Coastguard Worker for (Use &OI : I.operands()) {
293*9880d681SAndroid Build Coastguard Worker if (Instruction *J = dyn_cast<Instruction>(OI)) {
294*9880d681SAndroid Build Coastguard Worker if (IntegerType *IT = dyn_cast<IntegerType>(J->getType()))
295*9880d681SAndroid Build Coastguard Worker AliveBits[J] = APInt::getAllOnesValue(IT->getBitWidth());
296*9880d681SAndroid Build Coastguard Worker Worklist.push_back(J);
297*9880d681SAndroid Build Coastguard Worker }
298*9880d681SAndroid Build Coastguard Worker }
299*9880d681SAndroid Build Coastguard Worker // To save memory, we don't add I to the Visited set here. Instead, we
300*9880d681SAndroid Build Coastguard Worker // check isAlwaysLive on every instruction when searching for dead
301*9880d681SAndroid Build Coastguard Worker // instructions later (we need to check isAlwaysLive for the
302*9880d681SAndroid Build Coastguard Worker // integer-typed instructions anyway).
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker // Propagate liveness backwards to operands.
306*9880d681SAndroid Build Coastguard Worker while (!Worklist.empty()) {
307*9880d681SAndroid Build Coastguard Worker Instruction *UserI = Worklist.pop_back_val();
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
310*9880d681SAndroid Build Coastguard Worker APInt AOut;
311*9880d681SAndroid Build Coastguard Worker if (UserI->getType()->isIntegerTy()) {
312*9880d681SAndroid Build Coastguard Worker AOut = AliveBits[UserI];
313*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " Alive Out: " << AOut);
314*9880d681SAndroid Build Coastguard Worker }
315*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\n");
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard Worker if (!UserI->getType()->isIntegerTy())
318*9880d681SAndroid Build Coastguard Worker Visited.insert(UserI);
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker APInt KnownZero, KnownOne, KnownZero2, KnownOne2;
321*9880d681SAndroid Build Coastguard Worker // Compute the set of alive bits for each operand. These are anded into the
322*9880d681SAndroid Build Coastguard Worker // existing set, if any, and if that changes the set of alive bits, the
323*9880d681SAndroid Build Coastguard Worker // operand is added to the work-list.
324*9880d681SAndroid Build Coastguard Worker for (Use &OI : UserI->operands()) {
325*9880d681SAndroid Build Coastguard Worker if (Instruction *I = dyn_cast<Instruction>(OI)) {
326*9880d681SAndroid Build Coastguard Worker if (IntegerType *IT = dyn_cast<IntegerType>(I->getType())) {
327*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = IT->getBitWidth();
328*9880d681SAndroid Build Coastguard Worker APInt AB = APInt::getAllOnesValue(BitWidth);
329*9880d681SAndroid Build Coastguard Worker if (UserI->getType()->isIntegerTy() && !AOut &&
330*9880d681SAndroid Build Coastguard Worker !isAlwaysLive(UserI)) {
331*9880d681SAndroid Build Coastguard Worker AB = APInt(BitWidth, 0);
332*9880d681SAndroid Build Coastguard Worker } else {
333*9880d681SAndroid Build Coastguard Worker // If all bits of the output are dead, then all bits of the input
334*9880d681SAndroid Build Coastguard Worker // Bits of each operand that are used to compute alive bits of the
335*9880d681SAndroid Build Coastguard Worker // output are alive, all others are dead.
336*9880d681SAndroid Build Coastguard Worker determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB,
337*9880d681SAndroid Build Coastguard Worker KnownZero, KnownOne,
338*9880d681SAndroid Build Coastguard Worker KnownZero2, KnownOne2);
339*9880d681SAndroid Build Coastguard Worker }
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker // If we've added to the set of alive bits (or the operand has not
342*9880d681SAndroid Build Coastguard Worker // been previously visited), then re-queue the operand to be visited
343*9880d681SAndroid Build Coastguard Worker // again.
344*9880d681SAndroid Build Coastguard Worker APInt ABPrev(BitWidth, 0);
345*9880d681SAndroid Build Coastguard Worker auto ABI = AliveBits.find(I);
346*9880d681SAndroid Build Coastguard Worker if (ABI != AliveBits.end())
347*9880d681SAndroid Build Coastguard Worker ABPrev = ABI->second;
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker APInt ABNew = AB | ABPrev;
350*9880d681SAndroid Build Coastguard Worker if (ABNew != ABPrev || ABI == AliveBits.end()) {
351*9880d681SAndroid Build Coastguard Worker AliveBits[I] = std::move(ABNew);
352*9880d681SAndroid Build Coastguard Worker Worklist.push_back(I);
353*9880d681SAndroid Build Coastguard Worker }
354*9880d681SAndroid Build Coastguard Worker } else if (!Visited.count(I)) {
355*9880d681SAndroid Build Coastguard Worker Worklist.push_back(I);
356*9880d681SAndroid Build Coastguard Worker }
357*9880d681SAndroid Build Coastguard Worker }
358*9880d681SAndroid Build Coastguard Worker }
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
getDemandedBits(Instruction * I)362*9880d681SAndroid Build Coastguard Worker APInt DemandedBits::getDemandedBits(Instruction *I) {
363*9880d681SAndroid Build Coastguard Worker performAnalysis();
364*9880d681SAndroid Build Coastguard Worker
365*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = I->getParent()->getModule()->getDataLayout();
366*9880d681SAndroid Build Coastguard Worker if (AliveBits.count(I))
367*9880d681SAndroid Build Coastguard Worker return AliveBits[I];
368*9880d681SAndroid Build Coastguard Worker return APInt::getAllOnesValue(DL.getTypeSizeInBits(I->getType()));
369*9880d681SAndroid Build Coastguard Worker }
370*9880d681SAndroid Build Coastguard Worker
isInstructionDead(Instruction * I)371*9880d681SAndroid Build Coastguard Worker bool DemandedBits::isInstructionDead(Instruction *I) {
372*9880d681SAndroid Build Coastguard Worker performAnalysis();
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
375*9880d681SAndroid Build Coastguard Worker !isAlwaysLive(I);
376*9880d681SAndroid Build Coastguard Worker }
377*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS)378*9880d681SAndroid Build Coastguard Worker void DemandedBits::print(raw_ostream &OS) {
379*9880d681SAndroid Build Coastguard Worker performAnalysis();
380*9880d681SAndroid Build Coastguard Worker for (auto &KV : AliveBits) {
381*9880d681SAndroid Build Coastguard Worker OS << "DemandedBits: 0x" << utohexstr(KV.second.getLimitedValue()) << " for "
382*9880d681SAndroid Build Coastguard Worker << *KV.first << "\n";
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker
createDemandedBitsWrapperPass()386*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createDemandedBitsWrapperPass() {
387*9880d681SAndroid Build Coastguard Worker return new DemandedBitsWrapperPass();
388*9880d681SAndroid Build Coastguard Worker }
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker char DemandedBitsAnalysis::PassID;
391*9880d681SAndroid Build Coastguard Worker
run(Function & F,AnalysisManager<Function> & AM)392*9880d681SAndroid Build Coastguard Worker DemandedBits DemandedBitsAnalysis::run(Function &F,
393*9880d681SAndroid Build Coastguard Worker AnalysisManager<Function> &AM) {
394*9880d681SAndroid Build Coastguard Worker auto &AC = AM.getResult<AssumptionAnalysis>(F);
395*9880d681SAndroid Build Coastguard Worker auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
396*9880d681SAndroid Build Coastguard Worker return DemandedBits(F, AC, DT);
397*9880d681SAndroid Build Coastguard Worker }
398*9880d681SAndroid Build Coastguard Worker
run(Function & F,FunctionAnalysisManager & AM)399*9880d681SAndroid Build Coastguard Worker PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
400*9880d681SAndroid Build Coastguard Worker FunctionAnalysisManager &AM) {
401*9880d681SAndroid Build Coastguard Worker AM.getResult<DemandedBitsAnalysis>(F).print(OS);
402*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
403*9880d681SAndroid Build Coastguard Worker }
404