blob: 4aaa118211646f2bbda5f3e5c1cb31e868cd8747 [file] [log] [blame]
Eugene Zelenko38c02bc2017-07-21 21:37:46 +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"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000023#include "llvm/ADT/APInt.h"
James Molloy87405c72015-08-14 11:09:09 +000024#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"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000030#include "llvm/IR/Constants.h"
James Molloy87405c72015-08-14 11:09:09 +000031#include "llvm/IR/DataLayout.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000032#include "llvm/IR/DerivedTypes.h"
James Molloy87405c72015-08-14 11:09:09 +000033#include "llvm/IR/Dominators.h"
34#include "llvm/IR/InstIterator.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000035#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instruction.h"
James Molloy87405c72015-08-14 11:09:09 +000037#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000038#include "llvm/IR/Intrinsics.h"
James Molloy87405c72015-08-14 11:09:09 +000039#include "llvm/IR/Module.h"
40#include "llvm/IR/Operator.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000041#include "llvm/IR/PassManager.h"
Nikita Popov110cf052018-12-07 15:38:13 +000042#include "llvm/IR/PatternMatch.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000043#include "llvm/IR/Type.h"
44#include "llvm/IR/Use.h"
James Molloy87405c72015-08-14 11:09:09 +000045#include "llvm/Pass.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000046#include "llvm/Support/Casting.h"
James Molloy87405c72015-08-14 11:09:09 +000047#include "llvm/Support/Debug.h"
Craig Topperb45eabc2017-04-26 16:39:58 +000048#include "llvm/Support/KnownBits.h"
James Molloy87405c72015-08-14 11:09:09 +000049#include "llvm/Support/raw_ostream.h"
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000050#include <algorithm>
51#include <cstdint>
52
James Molloy87405c72015-08-14 11:09:09 +000053using namespace llvm;
Nikita Popov110cf052018-12-07 15:38:13 +000054using namespace llvm::PatternMatch;
James Molloy87405c72015-08-14 11:09:09 +000055
56#define DEBUG_TYPE "demanded-bits"
57
Michael Kupersteinde16b442016-04-18 23:55:01 +000058char DemandedBitsWrapperPass::ID = 0;
Eugene Zelenko38c02bc2017-07-21 21:37:46 +000059
Michael Kupersteinde16b442016-04-18 23:55:01 +000060INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
61 "Demanded bits analysis", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000062INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
James Molloy87405c72015-08-14 11:09:09 +000063INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Michael Kupersteinde16b442016-04-18 23:55:01 +000064INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
65 "Demanded bits analysis", false, false)
James Molloy87405c72015-08-14 11:09:09 +000066
Michael Kupersteinde16b442016-04-18 23:55:01 +000067DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
68 initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
James Molloy87405c72015-08-14 11:09:09 +000069}
70
Michael Kupersteinde16b442016-04-18 23:55:01 +000071void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
James Molloy87405c72015-08-14 11:09:09 +000072 AU.setPreservesCFG();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000073 AU.addRequired<AssumptionCacheTracker>();
James Molloy87405c72015-08-14 11:09:09 +000074 AU.addRequired<DominatorTreeWrapperPass>();
75 AU.setPreservesAll();
76}
77
Michael Kupersteinde16b442016-04-18 23:55:01 +000078void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
79 DB->print(OS);
80}
81
James Molloy87405c72015-08-14 11:09:09 +000082static bool isAlwaysLive(Instruction *I) {
Chandler Carruth9ae926b2018-08-26 09:51:22 +000083 return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() ||
84 I->mayHaveSideEffects();
James Molloy87405c72015-08-14 11:09:09 +000085}
86
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +000087void DemandedBits::determineLiveOperandBits(
88 const Instruction *UserI, const Instruction *I, unsigned OperandNo,
Craig Topperb45eabc2017-04-26 16:39:58 +000089 const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2) {
James Molloy87405c72015-08-14 11:09:09 +000090 unsigned BitWidth = AB.getBitWidth();
91
92 // We're called once per operand, but for some instructions, we need to
93 // compute known bits of both operands in order to determine the live bits of
94 // either (when both operands are instructions themselves). We don't,
95 // however, want to do this twice, so we cache the result in APInts that live
96 // in the caller. For the two-relevant-operands case, both operand values are
97 // provided here.
98 auto ComputeKnownBits =
99 [&](unsigned BitWidth, const Value *V1, const Value *V2) {
100 const DataLayout &DL = I->getModule()->getDataLayout();
Craig Topperb45eabc2017-04-26 16:39:58 +0000101 Known = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +0000102 computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +0000103
104 if (V2) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000105 Known2 = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +0000106 computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +0000107 }
108 };
109
110 switch (UserI->getOpcode()) {
111 default: break;
112 case Instruction::Call:
113 case Instruction::Invoke:
114 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
115 switch (II->getIntrinsicID()) {
116 default: break;
117 case Intrinsic::bswap:
118 // The alive bits of the input are the swapped alive bits of
119 // the output.
120 AB = AOut.byteSwap();
121 break;
Brian Gesiak0a7894d2017-04-13 16:44:25 +0000122 case Intrinsic::bitreverse:
Xin Tongbb8dbcf2017-06-19 20:10:41 +0000123 // The alive bits of the input are the reversed alive bits of
124 // the output.
Brian Gesiak0a7894d2017-04-13 16:44:25 +0000125 AB = AOut.reverseBits();
126 break;
James Molloy87405c72015-08-14 11:09:09 +0000127 case Intrinsic::ctlz:
128 if (OperandNo == 0) {
129 // We need some output bits, so we need all bits of the
130 // input to the left of, and including, the leftmost bit
131 // known to be one.
132 ComputeKnownBits(BitWidth, I, nullptr);
133 AB = APInt::getHighBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000134 std::min(BitWidth, Known.countMaxLeadingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000135 }
136 break;
137 case Intrinsic::cttz:
138 if (OperandNo == 0) {
139 // We need some output bits, so we need all bits of the
140 // input to the right of, and including, the rightmost bit
141 // known to be one.
142 ComputeKnownBits(BitWidth, I, nullptr);
143 AB = APInt::getLowBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000144 std::min(BitWidth, Known.countMaxTrailingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000145 }
146 break;
Nikita Popovf94c8f02018-11-26 15:36:57 +0000147 case Intrinsic::fshl:
Nikita Popov110cf052018-12-07 15:38:13 +0000148 case Intrinsic::fshr: {
149 const APInt *SA;
Nikita Popovf94c8f02018-11-26 15:36:57 +0000150 if (OperandNo == 2) {
151 // Shift amount is modulo the bitwidth. For powers of two we have
152 // SA % BW == SA & (BW - 1).
153 if (isPowerOf2_32(BitWidth))
154 AB = BitWidth - 1;
Nikita Popov110cf052018-12-07 15:38:13 +0000155 } else if (match(II->getOperand(2), m_APInt(SA))) {
Nikita Popovf94c8f02018-11-26 15:36:57 +0000156 // Normalize to funnel shift left. APInt shifts of BitWidth are well-
157 // defined, so no need to special-case zero shifts here.
Nikita Popov110cf052018-12-07 15:38:13 +0000158 uint64_t ShiftAmt = SA->urem(BitWidth);
Nikita Popovf94c8f02018-11-26 15:36:57 +0000159 if (II->getIntrinsicID() == Intrinsic::fshr)
160 ShiftAmt = BitWidth - ShiftAmt;
161
162 if (OperandNo == 0)
163 AB = AOut.lshr(ShiftAmt);
164 else if (OperandNo == 1)
165 AB = AOut.shl(BitWidth - ShiftAmt);
166 }
167 break;
James Molloy87405c72015-08-14 11:09:09 +0000168 }
Nikita Popov110cf052018-12-07 15:38:13 +0000169 }
James Molloy87405c72015-08-14 11:09:09 +0000170 break;
171 case Instruction::Add:
172 case Instruction::Sub:
James Molloybcd7f0a2015-10-08 12:39:59 +0000173 case Instruction::Mul:
James Molloy87405c72015-08-14 11:09:09 +0000174 // Find the highest live output bit. We don't need any more input
175 // bits than that (adds, and thus subtracts, ripple only to the
176 // left).
177 AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
178 break;
179 case Instruction::Shl:
Nikita Popov110cf052018-12-07 15:38:13 +0000180 if (OperandNo == 0) {
181 const APInt *ShiftAmtC;
182 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000183 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000184 AB = AOut.lshr(ShiftAmt);
185
186 // If the shift is nuw/nsw, then the high bits are not dead
187 // (because we've promised that they *must* be zero).
188 const ShlOperator *S = cast<ShlOperator>(UserI);
189 if (S->hasNoSignedWrap())
190 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
191 else if (S->hasNoUnsignedWrap())
192 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
193 }
Nikita Popov110cf052018-12-07 15:38:13 +0000194 }
James Molloy87405c72015-08-14 11:09:09 +0000195 break;
196 case Instruction::LShr:
Nikita Popov110cf052018-12-07 15:38:13 +0000197 if (OperandNo == 0) {
198 const APInt *ShiftAmtC;
199 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000200 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000201 AB = AOut.shl(ShiftAmt);
202
203 // If the shift is exact, then the low bits are not dead
204 // (they must be zero).
205 if (cast<LShrOperator>(UserI)->isExact())
206 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
207 }
Nikita Popov110cf052018-12-07 15:38:13 +0000208 }
James Molloy87405c72015-08-14 11:09:09 +0000209 break;
210 case Instruction::AShr:
Nikita Popov110cf052018-12-07 15:38:13 +0000211 if (OperandNo == 0) {
212 const APInt *ShiftAmtC;
213 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000214 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000215 AB = AOut.shl(ShiftAmt);
216 // Because the high input bit is replicated into the
217 // high-order bits of the result, if we need any of those
218 // bits, then we must keep the highest input bit.
219 if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
220 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000221 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000222
223 // If the shift is exact, then the low bits are not dead
224 // (they must be zero).
225 if (cast<AShrOperator>(UserI)->isExact())
226 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
227 }
Nikita Popov110cf052018-12-07 15:38:13 +0000228 }
James Molloy87405c72015-08-14 11:09:09 +0000229 break;
230 case Instruction::And:
231 AB = AOut;
232
233 // For bits that are known zero, the corresponding bits in the
234 // other operand are dead (unless they're both zero, in which
235 // case they can't both be dead, so just mark the LHS bits as
236 // dead).
237 if (OperandNo == 0) {
238 ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000239 AB &= ~Known2.Zero;
James Molloy87405c72015-08-14 11:09:09 +0000240 } else {
241 if (!isa<Instruction>(UserI->getOperand(0)))
242 ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
Craig Topperb45eabc2017-04-26 16:39:58 +0000243 AB &= ~(Known.Zero & ~Known2.Zero);
James Molloy87405c72015-08-14 11:09:09 +0000244 }
245 break;
246 case Instruction::Or:
247 AB = AOut;
248
249 // For bits that are known one, the corresponding bits in the
250 // other operand are dead (unless they're both one, in which
251 // case they can't both be dead, so just mark the LHS bits as
252 // dead).
253 if (OperandNo == 0) {
254 ComputeKnownBits(BitWidth, I, UserI->getOperand(1));
Craig Topperb45eabc2017-04-26 16:39:58 +0000255 AB &= ~Known2.One;
James Molloy87405c72015-08-14 11:09:09 +0000256 } else {
257 if (!isa<Instruction>(UserI->getOperand(0)))
258 ComputeKnownBits(BitWidth, UserI->getOperand(0), I);
Craig Topperb45eabc2017-04-26 16:39:58 +0000259 AB &= ~(Known.One & ~Known2.One);
James Molloy87405c72015-08-14 11:09:09 +0000260 }
261 break;
262 case Instruction::Xor:
263 case Instruction::PHI:
264 AB = AOut;
265 break;
266 case Instruction::Trunc:
267 AB = AOut.zext(BitWidth);
268 break;
269 case Instruction::ZExt:
270 AB = AOut.trunc(BitWidth);
271 break;
272 case Instruction::SExt:
273 AB = AOut.trunc(BitWidth);
274 // Because the high input bit is replicated into the
275 // high-order bits of the result, if we need any of those
276 // bits, then we must keep the highest input bit.
277 if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
278 AOut.getBitWidth() - BitWidth))
279 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000280 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000281 break;
282 case Instruction::Select:
283 if (OperandNo != 0)
284 AB = AOut;
285 break;
Nikita Popov110cf052018-12-07 15:38:13 +0000286 case Instruction::ExtractElement:
287 if (OperandNo == 0)
288 AB = AOut;
289 break;
290 case Instruction::InsertElement:
291 case Instruction::ShuffleVector:
292 if (OperandNo == 0 || OperandNo == 1)
293 AB = AOut;
294 break;
James Molloy87405c72015-08-14 11:09:09 +0000295 }
296}
297
Michael Kupersteinde16b442016-04-18 23:55:01 +0000298bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000299 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000300 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000301 DB.emplace(F, AC, DT);
James Molloyab9fdb92015-10-08 12:39:50 +0000302 return false;
303}
James Molloy87405c72015-08-14 11:09:09 +0000304
Michael Kupersteinde16b442016-04-18 23:55:01 +0000305void DemandedBitsWrapperPass::releaseMemory() {
306 DB.reset();
307}
308
James Molloyab9fdb92015-10-08 12:39:50 +0000309void DemandedBits::performAnalysis() {
310 if (Analyzed)
311 // Analysis already completed for this function.
312 return;
313 Analyzed = true;
Fangrui Songf78650a2018-07-30 19:41:25 +0000314
James Molloy87405c72015-08-14 11:09:09 +0000315 Visited.clear();
316 AliveBits.clear();
Nikita Popovbc9986e2019-01-01 10:05:26 +0000317 DeadUses.clear();
James Molloy87405c72015-08-14 11:09:09 +0000318
319 SmallVector<Instruction*, 128> Worklist;
320
321 // Collect the set of "root" instructions that are known live.
Michael Kupersteinde16b442016-04-18 23:55:01 +0000322 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +0000323 if (!isAlwaysLive(&I))
324 continue;
325
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000326 LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
James Molloy87405c72015-08-14 11:09:09 +0000327 // For integer-valued instructions, set up an initial empty set of alive
328 // bits and add the instruction to the work list. For other instructions
329 // add their operands to the work list (for integer values operands, mark
330 // all bits as live).
Nikita Popov110cf052018-12-07 15:38:13 +0000331 Type *T = I.getType();
332 if (T->isIntOrIntVectorTy()) {
333 if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
James Molloy87405c72015-08-14 11:09:09 +0000334 Worklist.push_back(&I);
James Molloy87405c72015-08-14 11:09:09 +0000335
336 continue;
337 }
338
339 // Non-integer-typed instructions...
340 for (Use &OI : I.operands()) {
341 if (Instruction *J = dyn_cast<Instruction>(OI)) {
Nikita Popov110cf052018-12-07 15:38:13 +0000342 Type *T = J->getType();
343 if (T->isIntOrIntVectorTy())
344 AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits());
James Molloy87405c72015-08-14 11:09:09 +0000345 Worklist.push_back(J);
346 }
347 }
348 // To save memory, we don't add I to the Visited set here. Instead, we
349 // check isAlwaysLive on every instruction when searching for dead
350 // instructions later (we need to check isAlwaysLive for the
351 // integer-typed instructions anyway).
352 }
353
354 // Propagate liveness backwards to operands.
355 while (!Worklist.empty()) {
356 Instruction *UserI = Worklist.pop_back_val();
357
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000358 LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
James Molloy87405c72015-08-14 11:09:09 +0000359 APInt AOut;
Nikita Popov110cf052018-12-07 15:38:13 +0000360 if (UserI->getType()->isIntOrIntVectorTy()) {
James Molloy87405c72015-08-14 11:09:09 +0000361 AOut = AliveBits[UserI];
Nikita Popovbc9986e2019-01-01 10:05:26 +0000362 LLVM_DEBUG(dbgs() << " Alive Out: 0x"
363 << Twine::utohexstr(AOut.getLimitedValue()));
James Molloy87405c72015-08-14 11:09:09 +0000364 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000365 LLVM_DEBUG(dbgs() << "\n");
James Molloy87405c72015-08-14 11:09:09 +0000366
Nikita Popov110cf052018-12-07 15:38:13 +0000367 if (!UserI->getType()->isIntOrIntVectorTy())
James Molloy87405c72015-08-14 11:09:09 +0000368 Visited.insert(UserI);
369
Craig Topperb45eabc2017-04-26 16:39:58 +0000370 KnownBits Known, Known2;
James Molloy87405c72015-08-14 11:09:09 +0000371 // Compute the set of alive bits for each operand. These are anded into the
372 // existing set, if any, and if that changes the set of alive bits, the
373 // operand is added to the work-list.
374 for (Use &OI : UserI->operands()) {
375 if (Instruction *I = dyn_cast<Instruction>(OI)) {
Nikita Popov110cf052018-12-07 15:38:13 +0000376 Type *T = I->getType();
377 if (T->isIntOrIntVectorTy()) {
378 unsigned BitWidth = T->getScalarSizeInBits();
Nikita Popov649e1252018-12-19 19:56:21 +0000379 APInt AB = APInt::getAllOnesValue(BitWidth);
380 if (UserI->getType()->isIntOrIntVectorTy() && !AOut &&
381 !isAlwaysLive(UserI)) {
Nikita Popovbc9986e2019-01-01 10:05:26 +0000382 // If all bits of the output are dead, then all bits of the input
383 // are also dead.
Nikita Popov649e1252018-12-19 19:56:21 +0000384 AB = APInt(BitWidth, 0);
385 } else {
386 // Bits of each operand that are used to compute alive bits of the
387 // output are alive, all others are dead.
388 determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB,
389 Known, Known2);
Nikita Popovbc9986e2019-01-01 10:05:26 +0000390
391 // Keep track of uses which have no demanded bits.
392 if (AB.isNullValue())
393 DeadUses.insert(&OI);
394 else
395 DeadUses.erase(&OI);
Nikita Popov649e1252018-12-19 19:56:21 +0000396 }
397
398 // If we've added to the set of alive bits (or the operand has not
399 // been previously visited), then re-queue the operand to be visited
400 // again.
Nikita Popov3817ee7902018-12-19 22:09:02 +0000401 APInt ABPrev(BitWidth, 0);
402 auto ABI = AliveBits.find(I);
403 if (ABI != AliveBits.end())
404 ABPrev = ABI->second;
405
James Molloy87405c72015-08-14 11:09:09 +0000406 APInt ABNew = AB | ABPrev;
407 if (ABNew != ABPrev || ABI == AliveBits.end()) {
408 AliveBits[I] = std::move(ABNew);
409 Worklist.push_back(I);
410 }
411 } else if (!Visited.count(I)) {
412 Worklist.push_back(I);
413 }
414 }
415 }
416 }
James Molloy87405c72015-08-14 11:09:09 +0000417}
418
419APInt DemandedBits::getDemandedBits(Instruction *I) {
Nikita Popovcf65b922018-12-06 23:50:32 +0000420 performAnalysis();
Nikita Popov14ca9a82018-12-07 00:42:03 +0000421
Benjamin Kramera9e477b2016-07-21 13:37:55 +0000422 auto Found = AliveBits.find(I);
423 if (Found != AliveBits.end())
424 return Found->second;
Nikita Popov110cf052018-12-07 15:38:13 +0000425
426 const DataLayout &DL = I->getModule()->getDataLayout();
427 return APInt::getAllOnesValue(
428 DL.getTypeSizeInBits(I->getType()->getScalarType()));
James Molloy87405c72015-08-14 11:09:09 +0000429}
430
431bool DemandedBits::isInstructionDead(Instruction *I) {
James Molloyab9fdb92015-10-08 12:39:50 +0000432 performAnalysis();
433
James Molloy87405c72015-08-14 11:09:09 +0000434 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
435 !isAlwaysLive(I);
436}
437
Nikita Popovbc9986e2019-01-01 10:05:26 +0000438bool DemandedBits::isUseDead(Use *U) {
439 // We only track integer uses, everything else is assumed live.
440 if (!(*U)->getType()->isIntOrIntVectorTy())
441 return false;
442
443 // Uses by always-live instructions are never dead.
444 Instruction *UserI = cast<Instruction>(U->getUser());
445 if (isAlwaysLive(UserI))
446 return false;
447
448 performAnalysis();
449 if (DeadUses.count(U))
450 return true;
451
452 // If no output bits are demanded, no input bits are demanded and the use
453 // is dead. These uses might not be explicitly present in the DeadUses map.
454 if (UserI->getType()->isIntOrIntVectorTy()) {
455 auto Found = AliveBits.find(UserI);
456 if (Found != AliveBits.end() && Found->second.isNullValue())
457 return true;
458 }
459
460 return false;
461}
462
Michael Kupersteinde16b442016-04-18 23:55:01 +0000463void DemandedBits::print(raw_ostream &OS) {
464 performAnalysis();
James Molloybcd7f0a2015-10-08 12:39:59 +0000465 for (auto &KV : AliveBits) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000466 OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue())
467 << " for " << *KV.first << '\n';
James Molloybcd7f0a2015-10-08 12:39:59 +0000468 }
469}
470
Michael Kupersteinde16b442016-04-18 23:55:01 +0000471FunctionPass *llvm::createDemandedBitsWrapperPass() {
472 return new DemandedBitsWrapperPass();
473}
474
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000475AnalysisKey DemandedBitsAnalysis::Key;
Michael Kupersteinde16b442016-04-18 23:55:01 +0000476
477DemandedBits DemandedBitsAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000478 FunctionAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000479 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000480 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000481 return DemandedBits(F, AC, DT);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000482}
483
484PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
485 FunctionAnalysisManager &AM) {
486 AM.getResult<DemandedBitsAnalysis>(F).print(OS);
487 return PreservedAnalyses::all();
James Molloy87405c72015-08-14 11:09:09 +0000488}