blob: 8f808f3e78719b34bc157574ec05339d11ae7900 [file] [log] [blame]
NAKAMURA Takumi84965032015-09-22 11:14:12 +00001//===---- DemandedBits.cpp - Determine demanded bits ----------------------===//
James Molloy87405c72015-08-14 11:09:09 +00002//
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 pass implements a demanded bits analysis. A demanded bit is one that
11// contributes to a result; bits that are not demanded can be either zero or
12// one without affecting control or data flow. For example in this sequence:
13//
14// %1 = add i32 %x, %y
15// %2 = trunc i32 %1 to i16
16//
17// Only the lowest 16 bits of %1 are demanded; the rest are removed by the
18// trunc.
19//
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Analysis/DemandedBits.h"
James Molloy87405c72015-08-14 11:09:09 +000023#include "llvm/ADT/DepthFirstIterator.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SmallVector.h"
James Molloybcd7f0a2015-10-08 12:39:59 +000026#include "llvm/ADT/StringExtras.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000027#include "llvm/Analysis/AssumptionCache.h"
James Molloy87405c72015-08-14 11:09:09 +000028#include "llvm/Analysis/ValueTracking.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/CFG.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/Dominators.h"
33#include "llvm/IR/InstIterator.h"
34#include "llvm/IR/Instructions.h"
35#include "llvm/IR/IntrinsicInst.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/Pass.h"
39#include "llvm/Support/Debug.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000040#include "llvm/Support/KnownBits.h"
James Molloy87405c72015-08-14 11:09:09 +000041#include "llvm/Support/raw_ostream.h"
42using namespace llvm;
43
44#define DEBUG_TYPE "demanded-bits"
45
Michael Kupersteinde16b442016-04-18 23:55:01 +000046char DemandedBitsWrapperPass::ID = 0;
47INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
48 "Demanded bits analysis", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000049INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
James Molloy87405c72015-08-14 11:09:09 +000050INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Michael Kupersteinde16b442016-04-18 23:55:01 +000051INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
52 "Demanded bits analysis", false, false)
James Molloy87405c72015-08-14 11:09:09 +000053
Michael Kupersteinde16b442016-04-18 23:55:01 +000054DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
55 initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
James Molloy87405c72015-08-14 11:09:09 +000056}
57
Michael Kupersteinde16b442016-04-18 23:55:01 +000058void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
James Molloy87405c72015-08-14 11:09:09 +000059 AU.setPreservesCFG();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000060 AU.addRequired<AssumptionCacheTracker>();
James Molloy87405c72015-08-14 11:09:09 +000061 AU.addRequired<DominatorTreeWrapperPass>();
62 AU.setPreservesAll();
63}
64
Michael Kupersteinde16b442016-04-18 23:55:01 +000065void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
66 DB->print(OS);
67}
68
James Molloy87405c72015-08-14 11:09:09 +000069static bool isAlwaysLive(Instruction *I) {
70 return isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
71 I->isEHPad() || I->mayHaveSideEffects();
72}
73
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000074void DemandedBits::determineLiveOperandBits(
75 const Instruction *UserI, const Instruction *I, unsigned OperandNo,
Craig Topperb45eabc2017-04-26 16:39:58 +000076 const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2) {
James Molloy87405c72015-08-14 11:09:09 +000077 unsigned BitWidth = AB.getBitWidth();
78
79 // We're called once per operand, but for some instructions, we need to
80 // compute known bits of both operands in order to determine the live bits of
81 // either (when both operands are instructions themselves). We don't,
82 // however, want to do this twice, so we cache the result in APInts that live
83 // in the caller. For the two-relevant-operands case, both operand values are
84 // provided here.
85 auto ComputeKnownBits =
86 [&](unsigned BitWidth, const Value *V1, const Value *V2) {
87 const DataLayout &DL = I->getModule()->getDataLayout();
Craig Topperb45eabc2017-04-26 16:39:58 +000088 Known = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +000089 computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +000090
91 if (V2) {
Craig Topperb45eabc2017-04-26 16:39:58 +000092 Known2 = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +000093 computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +000094 }
95 };
96
97 switch (UserI->getOpcode()) {
98 default: break;
99 case Instruction::Call:
100 case Instruction::Invoke:
101 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
102 switch (II->getIntrinsicID()) {
103 default: break;
104 case Intrinsic::bswap:
105 // The alive bits of the input are the swapped alive bits of
106 // the output.
107 AB = AOut.byteSwap();
108 break;
Brian Gesiak0a7894d2017-04-13 16:44:25 +0000109 case Intrinsic::bitreverse:
110 AB = AOut.reverseBits();
111 break;
James Molloy87405c72015-08-14 11:09:09 +0000112 case Intrinsic::ctlz:
113 if (OperandNo == 0) {
114 // We need some output bits, so we need all bits of the
115 // input to the left of, and including, the leftmost bit
116 // known to be one.
117 ComputeKnownBits(BitWidth, I, nullptr);
118 AB = APInt::getHighBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000119 std::min(BitWidth, Known.countMaxLeadingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000120 }
121 break;
122 case Intrinsic::cttz:
123 if (OperandNo == 0) {
124 // We need some output bits, so we need all bits of the
125 // input to the right of, and including, the rightmost bit
126 // known to be one.
127 ComputeKnownBits(BitWidth, I, nullptr);
128 AB = APInt::getLowBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000129 std::min(BitWidth, Known.countMaxTrailingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000130 }
131 break;
132 }
133 break;
134 case Instruction::Add:
135 case Instruction::Sub:
James Molloybcd7f0a2015-10-08 12:39:59 +0000136 case Instruction::Mul:
James Molloy87405c72015-08-14 11:09:09 +0000137 // Find the highest live output bit. We don't need any more input
138 // bits than that (adds, and thus subtracts, ripple only to the
139 // left).
140 AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
141 break;
142 case Instruction::Shl:
143 if (OperandNo == 0)
144 if (ConstantInt *CI =
145 dyn_cast<ConstantInt>(UserI->getOperand(1))) {
146 uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
147 AB = AOut.lshr(ShiftAmt);
148
149 // If the shift is nuw/nsw, then the high bits are not dead
150 // (because we've promised that they *must* be zero).
151 const ShlOperator *S = cast<ShlOperator>(UserI);
152 if (S->hasNoSignedWrap())
153 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
154 else if (S->hasNoUnsignedWrap())
155 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
156 }
157 break;
158 case Instruction::LShr:
159 if (OperandNo == 0)
160 if (ConstantInt *CI =
161 dyn_cast<ConstantInt>(UserI->getOperand(1))) {
162 uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
163 AB = AOut.shl(ShiftAmt);
164
165 // If the shift is exact, then the low bits are not dead
166 // (they must be zero).
167 if (cast<LShrOperator>(UserI)->isExact())
168 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
169 }
170 break;
171 case Instruction::AShr:
172 if (OperandNo == 0)
173 if (ConstantInt *CI =
174 dyn_cast<ConstantInt>(UserI->getOperand(1))) {
175 uint64_t ShiftAmt = CI->getLimitedValue(BitWidth-1);
176 AB = AOut.shl(ShiftAmt);
177 // Because the high input bit is replicated into the
178 // high-order bits of the result, if we need any of those
179 // bits, then we must keep the highest input bit.
180 if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
181 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000182 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000183
184 // If the shift is exact, then the low bits are not dead
185 // (they must be zero).
186 if (cast<AShrOperator>(UserI)->isExact())
187 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
188 }
189 break;
190 case Instruction::And:
191 AB = AOut;
192
193 // For bits that are known zero, the corresponding bits in the
194 // other operand are dead (unless they're both zero, in which
195 // case they can't both be dead, so just mark the LHS bits as
196 // dead).
197 if (OperandNo == 0) {
198 ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000199 AB &= ~Known2.Zero;
James Molloy87405c72015-08-14 11:09:09 +0000200 } else {
201 if (!isa<Instruction>(UserI->getOperand(0)))
202 ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
Craig Topperb45eabc2017-04-26 16:39:58 +0000203 AB &= ~(Known.Zero & ~Known2.Zero);
James Molloy87405c72015-08-14 11:09:09 +0000204 }
205 break;
206 case Instruction::Or:
207 AB = AOut;
208
209 // For bits that are known one, the corresponding bits in the
210 // other operand are dead (unless they're both one, in which
211 // case they can't both be dead, so just mark the LHS bits as
212 // dead).
213 if (OperandNo == 0) {
214 ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000215 AB &= ~Known2.One;
James Molloy87405c72015-08-14 11:09:09 +0000216 } else {
217 if (!isa<Instruction>(UserI->getOperand(0)))
218 ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
Craig Topperb45eabc2017-04-26 16:39:58 +0000219 AB &= ~(Known.One & ~Known2.One);
James Molloy87405c72015-08-14 11:09:09 +0000220 }
221 break;
222 case Instruction::Xor:
223 case Instruction::PHI:
224 AB = AOut;
225 break;
226 case Instruction::Trunc:
227 AB = AOut.zext(BitWidth);
228 break;
229 case Instruction::ZExt:
230 AB = AOut.trunc(BitWidth);
231 break;
232 case Instruction::SExt:
233 AB = AOut.trunc(BitWidth);
234 // Because the high input bit is replicated into the
235 // high-order bits of the result, if we need any of those
236 // bits, then we must keep the highest input bit.
237 if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
238 AOut.getBitWidth() - BitWidth))
239 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000240 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000241 break;
242 case Instruction::Select:
243 if (OperandNo != 0)
244 AB = AOut;
245 break;
246 }
247}
248
Michael Kupersteinde16b442016-04-18 23:55:01 +0000249bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000250 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000251 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000252 DB.emplace(F, AC, DT);
James Molloyab9fdb92015-10-08 12:39:50 +0000253 return false;
254}
James Molloy87405c72015-08-14 11:09:09 +0000255
Michael Kupersteinde16b442016-04-18 23:55:01 +0000256void DemandedBitsWrapperPass::releaseMemory() {
257 DB.reset();
258}
259
James Molloyab9fdb92015-10-08 12:39:50 +0000260void DemandedBits::performAnalysis() {
261 if (Analyzed)
262 // Analysis already completed for this function.
263 return;
264 Analyzed = true;
James Molloyab9fdb92015-10-08 12:39:50 +0000265
James Molloy87405c72015-08-14 11:09:09 +0000266 Visited.clear();
267 AliveBits.clear();
268
269 SmallVector<Instruction*, 128> Worklist;
270
271 // Collect the set of "root" instructions that are known live.
Michael Kupersteinde16b442016-04-18 23:55:01 +0000272 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +0000273 if (!isAlwaysLive(&I))
274 continue;
275
276 DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
277 // For integer-valued instructions, set up an initial empty set of alive
278 // bits and add the instruction to the work list. For other instructions
279 // add their operands to the work list (for integer values operands, mark
280 // all bits as live).
281 if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
Benjamin Kramera9e477b2016-07-21 13:37:55 +0000282 if (AliveBits.try_emplace(&I, IT->getBitWidth(), 0).second)
James Molloy87405c72015-08-14 11:09:09 +0000283 Worklist.push_back(&I);
James Molloy87405c72015-08-14 11:09:09 +0000284
285 continue;
286 }
287
288 // Non-integer-typed instructions...
289 for (Use &OI : I.operands()) {
290 if (Instruction *J = dyn_cast<Instruction>(OI)) {
291 if (IntegerType *IT = dyn_cast<IntegerType>(J->getType()))
292 AliveBits[J] = APInt::getAllOnesValue(IT->getBitWidth());
293 Worklist.push_back(J);
294 }
295 }
296 // To save memory, we don't add I to the Visited set here. Instead, we
297 // check isAlwaysLive on every instruction when searching for dead
298 // instructions later (we need to check isAlwaysLive for the
299 // integer-typed instructions anyway).
300 }
301
302 // Propagate liveness backwards to operands.
303 while (!Worklist.empty()) {
304 Instruction *UserI = Worklist.pop_back_val();
305
306 DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
307 APInt AOut;
308 if (UserI->getType()->isIntegerTy()) {
309 AOut = AliveBits[UserI];
310 DEBUG(dbgs() << " Alive Out: " << AOut);
311 }
312 DEBUG(dbgs() << "\n");
313
314 if (!UserI->getType()->isIntegerTy())
315 Visited.insert(UserI);
316
Craig Topperb45eabc2017-04-26 16:39:58 +0000317 KnownBits Known, Known2;
James Molloy87405c72015-08-14 11:09:09 +0000318 // Compute the set of alive bits for each operand. These are anded into the
319 // existing set, if any, and if that changes the set of alive bits, the
320 // operand is added to the work-list.
321 for (Use &OI : UserI->operands()) {
322 if (Instruction *I = dyn_cast<Instruction>(OI)) {
323 if (IntegerType *IT = dyn_cast<IntegerType>(I->getType())) {
324 unsigned BitWidth = IT->getBitWidth();
325 APInt AB = APInt::getAllOnesValue(BitWidth);
326 if (UserI->getType()->isIntegerTy() && !AOut &&
327 !isAlwaysLive(UserI)) {
328 AB = APInt(BitWidth, 0);
329 } else {
NAKAMURA Takumi84965032015-09-22 11:14:12 +0000330 // If all bits of the output are dead, then all bits of the input
James Molloy87405c72015-08-14 11:09:09 +0000331 // Bits of each operand that are used to compute alive bits of the
332 // output are alive, all others are dead.
333 determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB,
Craig Topperb45eabc2017-04-26 16:39:58 +0000334 Known, Known2);
James Molloy87405c72015-08-14 11:09:09 +0000335 }
336
337 // If we've added to the set of alive bits (or the operand has not
338 // been previously visited), then re-queue the operand to be visited
339 // again.
340 APInt ABPrev(BitWidth, 0);
341 auto ABI = AliveBits.find(I);
342 if (ABI != AliveBits.end())
343 ABPrev = ABI->second;
344
345 APInt ABNew = AB | ABPrev;
346 if (ABNew != ABPrev || ABI == AliveBits.end()) {
347 AliveBits[I] = std::move(ABNew);
348 Worklist.push_back(I);
349 }
350 } else if (!Visited.count(I)) {
351 Worklist.push_back(I);
352 }
353 }
354 }
355 }
James Molloy87405c72015-08-14 11:09:09 +0000356}
357
358APInt DemandedBits::getDemandedBits(Instruction *I) {
James Molloyab9fdb92015-10-08 12:39:50 +0000359 performAnalysis();
360
James Molloy87405c72015-08-14 11:09:09 +0000361 const DataLayout &DL = I->getParent()->getModule()->getDataLayout();
Benjamin Kramera9e477b2016-07-21 13:37:55 +0000362 auto Found = AliveBits.find(I);
363 if (Found != AliveBits.end())
364 return Found->second;
James Molloy87405c72015-08-14 11:09:09 +0000365 return APInt::getAllOnesValue(DL.getTypeSizeInBits(I->getType()));
366}
367
368bool DemandedBits::isInstructionDead(Instruction *I) {
James Molloyab9fdb92015-10-08 12:39:50 +0000369 performAnalysis();
370
James Molloy87405c72015-08-14 11:09:09 +0000371 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
372 !isAlwaysLive(I);
373}
374
Michael Kupersteinde16b442016-04-18 23:55:01 +0000375void DemandedBits::print(raw_ostream &OS) {
376 performAnalysis();
James Molloybcd7f0a2015-10-08 12:39:59 +0000377 for (auto &KV : AliveBits) {
378 OS << "DemandedBits: 0x" << utohexstr(KV.second.getLimitedValue()) << " for "
379 << *KV.first << "\n";
380 }
381}
382
Michael Kupersteinde16b442016-04-18 23:55:01 +0000383FunctionPass *llvm::createDemandedBitsWrapperPass() {
384 return new DemandedBitsWrapperPass();
385}
386
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000387AnalysisKey DemandedBitsAnalysis::Key;
Michael Kupersteinde16b442016-04-18 23:55:01 +0000388
389DemandedBits DemandedBitsAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000390 FunctionAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000391 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000392 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000393 return DemandedBits(F, AC, DT);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000394}
395
396PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
397 FunctionAnalysisManager &AM) {
398 AM.getResult<DemandedBitsAnalysis>(F).print(OS);
399 return PreservedAnalyses::all();
James Molloy87405c72015-08-14 11:09:09 +0000400}