blob: ea5a509eeb88f77b9734f83705b5f3a60e9f7b7e [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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +0000194 }
James Molloy87405c72015-08-14 11:09:09 +0000195 break;
196 case Instruction::LShr:
Nikita Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +0000208 }
James Molloy87405c72015-08-14 11:09:09 +0000209 break;
210 case Instruction::AShr:
Nikita Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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 Popovcf65b922018-12-06 23:50:32 +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();
317
318 SmallVector<Instruction*, 128> Worklist;
319
320 // Collect the set of "root" instructions that are known live.
Michael Kupersteinde16b442016-04-18 23:55:01 +0000321 for (Instruction &I : instructions(F)) {
James Molloy87405c72015-08-14 11:09:09 +0000322 if (!isAlwaysLive(&I))
323 continue;
324
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000325 LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
James Molloy87405c72015-08-14 11:09:09 +0000326 // For integer-valued instructions, set up an initial empty set of alive
327 // bits and add the instruction to the work list. For other instructions
328 // add their operands to the work list (for integer values operands, mark
329 // all bits as live).
Nikita Popovcf65b922018-12-06 23:50:32 +0000330 Type *T = I.getType();
331 if (T->isIntOrIntVectorTy()) {
332 if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
James Molloy87405c72015-08-14 11:09:09 +0000333 Worklist.push_back(&I);
James Molloy87405c72015-08-14 11:09:09 +0000334
335 continue;
336 }
337
338 // Non-integer-typed instructions...
339 for (Use &OI : I.operands()) {
340 if (Instruction *J = dyn_cast<Instruction>(OI)) {
Nikita Popovcf65b922018-12-06 23:50:32 +0000341 Type *T = J->getType();
342 if (T->isIntOrIntVectorTy())
343 AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits());
James Molloy87405c72015-08-14 11:09:09 +0000344 Worklist.push_back(J);
345 }
346 }
347 // To save memory, we don't add I to the Visited set here. Instead, we
348 // check isAlwaysLive on every instruction when searching for dead
349 // instructions later (we need to check isAlwaysLive for the
350 // integer-typed instructions anyway).
351 }
352
353 // Propagate liveness backwards to operands.
354 while (!Worklist.empty()) {
355 Instruction *UserI = Worklist.pop_back_val();
356
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000357 LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
James Molloy87405c72015-08-14 11:09:09 +0000358 APInt AOut;
Nikita Popovcf65b922018-12-06 23:50:32 +0000359 if (UserI->getType()->isIntOrIntVectorTy()) {
James Molloy87405c72015-08-14 11:09:09 +0000360 AOut = AliveBits[UserI];
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000361 LLVM_DEBUG(dbgs() << " Alive Out: " << AOut);
James Molloy87405c72015-08-14 11:09:09 +0000362 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000363 LLVM_DEBUG(dbgs() << "\n");
James Molloy87405c72015-08-14 11:09:09 +0000364
Nikita Popovcf65b922018-12-06 23:50:32 +0000365 if (!UserI->getType()->isIntOrIntVectorTy())
James Molloy87405c72015-08-14 11:09:09 +0000366 Visited.insert(UserI);
367
Craig Topperb45eabc2017-04-26 16:39:58 +0000368 KnownBits Known, Known2;
James Molloy87405c72015-08-14 11:09:09 +0000369 // Compute the set of alive bits for each operand. These are anded into the
370 // existing set, if any, and if that changes the set of alive bits, the
371 // operand is added to the work-list.
372 for (Use &OI : UserI->operands()) {
373 if (Instruction *I = dyn_cast<Instruction>(OI)) {
Nikita Popovcf65b922018-12-06 23:50:32 +0000374 Type *T = I->getType();
375 if (T->isIntOrIntVectorTy()) {
376 unsigned BitWidth = T->getScalarSizeInBits();
James Molloy87405c72015-08-14 11:09:09 +0000377 APInt AB = APInt::getAllOnesValue(BitWidth);
Nikita Popovcf65b922018-12-06 23:50:32 +0000378 if (UserI->getType()->isIntOrIntVectorTy() && !AOut &&
James Molloy87405c72015-08-14 11:09:09 +0000379 !isAlwaysLive(UserI)) {
380 AB = APInt(BitWidth, 0);
381 } else {
NAKAMURA Takumi84965032015-09-22 11:14:12 +0000382 // If all bits of the output are dead, then all bits of the input
James Molloy87405c72015-08-14 11:09:09 +0000383 // Bits of each operand that are used to compute alive bits of the
384 // output are alive, all others are dead.
385 determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB,
Craig Topperb45eabc2017-04-26 16:39:58 +0000386 Known, Known2);
James Molloy87405c72015-08-14 11:09:09 +0000387 }
388
389 // If we've added to the set of alive bits (or the operand has not
390 // been previously visited), then re-queue the operand to be visited
391 // again.
392 APInt ABPrev(BitWidth, 0);
393 auto ABI = AliveBits.find(I);
394 if (ABI != AliveBits.end())
395 ABPrev = ABI->second;
396
397 APInt ABNew = AB | ABPrev;
398 if (ABNew != ABPrev || ABI == AliveBits.end()) {
399 AliveBits[I] = std::move(ABNew);
400 Worklist.push_back(I);
401 }
402 } else if (!Visited.count(I)) {
403 Worklist.push_back(I);
404 }
405 }
406 }
407 }
James Molloy87405c72015-08-14 11:09:09 +0000408}
409
410APInt DemandedBits::getDemandedBits(Instruction *I) {
Nikita Popovcf65b922018-12-06 23:50:32 +0000411 assert(I->getType()->isIntOrIntVectorTy() &&
412 "Not an integer or vector of integer instruction");
Fangrui Songf78650a2018-07-30 19:41:25 +0000413
Nikita Popovcf65b922018-12-06 23:50:32 +0000414 performAnalysis();
Benjamin Kramera9e477b2016-07-21 13:37:55 +0000415 auto Found = AliveBits.find(I);
416 if (Found != AliveBits.end())
417 return Found->second;
Nikita Popovcf65b922018-12-06 23:50:32 +0000418 return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits());
James Molloy87405c72015-08-14 11:09:09 +0000419}
420
421bool DemandedBits::isInstructionDead(Instruction *I) {
James Molloyab9fdb92015-10-08 12:39:50 +0000422 performAnalysis();
423
James Molloy87405c72015-08-14 11:09:09 +0000424 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
425 !isAlwaysLive(I);
426}
427
Michael Kupersteinde16b442016-04-18 23:55:01 +0000428void DemandedBits::print(raw_ostream &OS) {
429 performAnalysis();
James Molloybcd7f0a2015-10-08 12:39:59 +0000430 for (auto &KV : AliveBits) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000431 OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue())
432 << " for " << *KV.first << '\n';
James Molloybcd7f0a2015-10-08 12:39:59 +0000433 }
434}
435
Michael Kupersteinde16b442016-04-18 23:55:01 +0000436FunctionPass *llvm::createDemandedBitsWrapperPass() {
437 return new DemandedBitsWrapperPass();
438}
439
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000440AnalysisKey DemandedBitsAnalysis::Key;
Michael Kupersteinde16b442016-04-18 23:55:01 +0000441
442DemandedBits DemandedBitsAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000443 FunctionAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000444 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000445 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000446 return DemandedBits(F, AC, DT);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000447}
448
449PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
450 FunctionAnalysisManager &AM) {
451 AM.getResult<DemandedBitsAnalysis>(F).print(OS);
452 return PreservedAnalyses::all();
James Molloy87405c72015-08-14 11:09:09 +0000453}