blob: ba310d8d3210aefc78b14eee4d75807b98c4abb7 [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"
Nikita Popov8dd19ed2019-01-07 18:15:11 +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(
Nikita Popov6658fce2019-01-04 21:21:43 +000088 const Instruction *UserI, const Value *Val, unsigned OperandNo,
89 const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,
90 bool &KnownBitsComputed) {
James Molloy87405c72015-08-14 11:09:09 +000091 unsigned BitWidth = AB.getBitWidth();
92
93 // We're called once per operand, but for some instructions, we need to
94 // compute known bits of both operands in order to determine the live bits of
95 // either (when both operands are instructions themselves). We don't,
96 // however, want to do this twice, so we cache the result in APInts that live
97 // in the caller. For the two-relevant-operands case, both operand values are
98 // provided here.
99 auto ComputeKnownBits =
100 [&](unsigned BitWidth, const Value *V1, const Value *V2) {
Nikita Popov6658fce2019-01-04 21:21:43 +0000101 if (KnownBitsComputed)
102 return;
103 KnownBitsComputed = true;
104
105 const DataLayout &DL = UserI->getModule()->getDataLayout();
Craig Topperb45eabc2017-04-26 16:39:58 +0000106 Known = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +0000107 computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +0000108
109 if (V2) {
Craig Topperb45eabc2017-04-26 16:39:58 +0000110 Known2 = KnownBits(BitWidth);
Craig Topper9fe35792017-05-13 17:22:16 +0000111 computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
James Molloy87405c72015-08-14 11:09:09 +0000112 }
113 };
114
115 switch (UserI->getOpcode()) {
116 default: break;
117 case Instruction::Call:
118 case Instruction::Invoke:
119 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
120 switch (II->getIntrinsicID()) {
121 default: break;
122 case Intrinsic::bswap:
123 // The alive bits of the input are the swapped alive bits of
124 // the output.
125 AB = AOut.byteSwap();
126 break;
Brian Gesiak0a7894d2017-04-13 16:44:25 +0000127 case Intrinsic::bitreverse:
Xin Tongbb8dbcf2017-06-19 20:10:41 +0000128 // The alive bits of the input are the reversed alive bits of
129 // the output.
Brian Gesiak0a7894d2017-04-13 16:44:25 +0000130 AB = AOut.reverseBits();
131 break;
James Molloy87405c72015-08-14 11:09:09 +0000132 case Intrinsic::ctlz:
133 if (OperandNo == 0) {
134 // We need some output bits, so we need all bits of the
135 // input to the left of, and including, the leftmost bit
136 // known to be one.
Nikita Popov6658fce2019-01-04 21:21:43 +0000137 ComputeKnownBits(BitWidth, Val, nullptr);
James Molloy87405c72015-08-14 11:09:09 +0000138 AB = APInt::getHighBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000139 std::min(BitWidth, Known.countMaxLeadingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000140 }
141 break;
142 case Intrinsic::cttz:
143 if (OperandNo == 0) {
144 // We need some output bits, so we need all bits of the
145 // input to the right of, and including, the rightmost bit
146 // known to be one.
Nikita Popov6658fce2019-01-04 21:21:43 +0000147 ComputeKnownBits(BitWidth, Val, nullptr);
James Molloy87405c72015-08-14 11:09:09 +0000148 AB = APInt::getLowBitsSet(BitWidth,
Craig Topper8df66c62017-05-12 17:20:30 +0000149 std::min(BitWidth, Known.countMaxTrailingZeros()+1));
James Molloy87405c72015-08-14 11:09:09 +0000150 }
151 break;
Nikita Popovf94c8f02018-11-26 15:36:57 +0000152 case Intrinsic::fshl:
Nikita Popov110cf052018-12-07 15:38:13 +0000153 case Intrinsic::fshr: {
154 const APInt *SA;
Nikita Popovf94c8f02018-11-26 15:36:57 +0000155 if (OperandNo == 2) {
156 // Shift amount is modulo the bitwidth. For powers of two we have
157 // SA % BW == SA & (BW - 1).
158 if (isPowerOf2_32(BitWidth))
159 AB = BitWidth - 1;
Nikita Popov110cf052018-12-07 15:38:13 +0000160 } else if (match(II->getOperand(2), m_APInt(SA))) {
Nikita Popovf94c8f02018-11-26 15:36:57 +0000161 // Normalize to funnel shift left. APInt shifts of BitWidth are well-
162 // defined, so no need to special-case zero shifts here.
Nikita Popov110cf052018-12-07 15:38:13 +0000163 uint64_t ShiftAmt = SA->urem(BitWidth);
Nikita Popovf94c8f02018-11-26 15:36:57 +0000164 if (II->getIntrinsicID() == Intrinsic::fshr)
165 ShiftAmt = BitWidth - ShiftAmt;
166
167 if (OperandNo == 0)
168 AB = AOut.lshr(ShiftAmt);
169 else if (OperandNo == 1)
170 AB = AOut.shl(BitWidth - ShiftAmt);
171 }
172 break;
James Molloy87405c72015-08-14 11:09:09 +0000173 }
Nikita Popov110cf052018-12-07 15:38:13 +0000174 }
James Molloy87405c72015-08-14 11:09:09 +0000175 break;
176 case Instruction::Add:
177 case Instruction::Sub:
James Molloybcd7f0a2015-10-08 12:39:59 +0000178 case Instruction::Mul:
James Molloy87405c72015-08-14 11:09:09 +0000179 // Find the highest live output bit. We don't need any more input
180 // bits than that (adds, and thus subtracts, ripple only to the
181 // left).
182 AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
183 break;
184 case Instruction::Shl:
Nikita Popov110cf052018-12-07 15:38:13 +0000185 if (OperandNo == 0) {
186 const APInt *ShiftAmtC;
187 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000188 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000189 AB = AOut.lshr(ShiftAmt);
190
191 // If the shift is nuw/nsw, then the high bits are not dead
192 // (because we've promised that they *must* be zero).
193 const ShlOperator *S = cast<ShlOperator>(UserI);
194 if (S->hasNoSignedWrap())
195 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
196 else if (S->hasNoUnsignedWrap())
197 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
198 }
Nikita Popov110cf052018-12-07 15:38:13 +0000199 }
James Molloy87405c72015-08-14 11:09:09 +0000200 break;
201 case Instruction::LShr:
Nikita Popov110cf052018-12-07 15:38:13 +0000202 if (OperandNo == 0) {
203 const APInt *ShiftAmtC;
204 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000205 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000206 AB = AOut.shl(ShiftAmt);
207
208 // If the shift is exact, then the low bits are not dead
209 // (they must be zero).
210 if (cast<LShrOperator>(UserI)->isExact())
211 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
212 }
Nikita Popov110cf052018-12-07 15:38:13 +0000213 }
James Molloy87405c72015-08-14 11:09:09 +0000214 break;
215 case Instruction::AShr:
Nikita Popov110cf052018-12-07 15:38:13 +0000216 if (OperandNo == 0) {
217 const APInt *ShiftAmtC;
218 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel1bbdf4e2017-07-07 14:39:26 +0000219 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy87405c72015-08-14 11:09:09 +0000220 AB = AOut.shl(ShiftAmt);
221 // Because the high input bit is replicated into the
222 // high-order bits of the result, if we need any of those
223 // bits, then we must keep the highest input bit.
224 if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
225 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000226 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000227
228 // If the shift is exact, then the low bits are not dead
229 // (they must be zero).
230 if (cast<AShrOperator>(UserI)->isExact())
231 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
232 }
Nikita Popov110cf052018-12-07 15:38:13 +0000233 }
James Molloy87405c72015-08-14 11:09:09 +0000234 break;
235 case Instruction::And:
236 AB = AOut;
237
238 // For bits that are known zero, the corresponding bits in the
239 // other operand are dead (unless they're both zero, in which
240 // case they can't both be dead, so just mark the LHS bits as
241 // dead).
Nikita Popov6658fce2019-01-04 21:21:43 +0000242 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
243 if (OperandNo == 0)
Craig Topperb45eabc2017-04-26 16:39:58 +0000244 AB &= ~Known2.Zero;
Nikita Popov6658fce2019-01-04 21:21:43 +0000245 else
Craig Topperb45eabc2017-04-26 16:39:58 +0000246 AB &= ~(Known.Zero & ~Known2.Zero);
James Molloy87405c72015-08-14 11:09:09 +0000247 break;
248 case Instruction::Or:
249 AB = AOut;
250
251 // For bits that are known one, the corresponding bits in the
252 // other operand are dead (unless they're both one, in which
253 // case they can't both be dead, so just mark the LHS bits as
254 // dead).
Nikita Popov6658fce2019-01-04 21:21:43 +0000255 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
256 if (OperandNo == 0)
Craig Topperb45eabc2017-04-26 16:39:58 +0000257 AB &= ~Known2.One;
Nikita Popov6658fce2019-01-04 21:21:43 +0000258 else
Craig Topperb45eabc2017-04-26 16:39:58 +0000259 AB &= ~(Known.One & ~Known2.One);
James Molloy87405c72015-08-14 11:09:09 +0000260 break;
261 case Instruction::Xor:
262 case Instruction::PHI:
263 AB = AOut;
264 break;
265 case Instruction::Trunc:
266 AB = AOut.zext(BitWidth);
267 break;
268 case Instruction::ZExt:
269 AB = AOut.trunc(BitWidth);
270 break;
271 case Instruction::SExt:
272 AB = AOut.trunc(BitWidth);
273 // Because the high input bit is replicated into the
274 // high-order bits of the result, if we need any of those
275 // bits, then we must keep the highest input bit.
276 if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
277 AOut.getBitWidth() - BitWidth))
278 .getBoolValue())
Craig Topper24db6b82017-04-28 16:58:05 +0000279 AB.setSignBit();
James Molloy87405c72015-08-14 11:09:09 +0000280 break;
281 case Instruction::Select:
282 if (OperandNo != 0)
283 AB = AOut;
284 break;
Nikita Popov110cf052018-12-07 15:38:13 +0000285 case Instruction::ExtractElement:
286 if (OperandNo == 0)
287 AB = AOut;
288 break;
289 case Instruction::InsertElement:
290 case Instruction::ShuffleVector:
291 if (OperandNo == 0 || OperandNo == 1)
292 AB = AOut;
293 break;
James Molloy87405c72015-08-14 11:09:09 +0000294 }
295}
296
Michael Kupersteinde16b442016-04-18 23:55:01 +0000297bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000298 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000299 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000300 DB.emplace(F, AC, DT);
James Molloyab9fdb92015-10-08 12:39:50 +0000301 return false;
302}
James Molloy87405c72015-08-14 11:09:09 +0000303
Michael Kupersteinde16b442016-04-18 23:55:01 +0000304void DemandedBitsWrapperPass::releaseMemory() {
305 DB.reset();
306}
307
James Molloyab9fdb92015-10-08 12:39:50 +0000308void DemandedBits::performAnalysis() {
309 if (Analyzed)
310 // Analysis already completed for this function.
311 return;
312 Analyzed = true;
Fangrui Songf78650a2018-07-30 19:41:25 +0000313
James Molloy87405c72015-08-14 11:09:09 +0000314 Visited.clear();
315 AliveBits.clear();
Nikita Popovbc9986e2019-01-01 10:05:26 +0000316 DeadUses.clear();
James Molloy87405c72015-08-14 11:09:09 +0000317
Nikita Popov8dd19ed2019-01-07 18:15:11 +0000318 SmallVector<Instruction*, 128> Worklist;
James Molloy87405c72015-08-14 11:09:09 +0000319
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 Popov110cf052018-12-07 15:38:13 +0000330 Type *T = I.getType();
331 if (T->isIntOrIntVectorTy()) {
332 if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
Nikita Popov8dd19ed2019-01-07 18:15:11 +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 Popov110cf052018-12-07 15:38:13 +0000341 Type *T = J->getType();
342 if (T->isIntOrIntVectorTy())
343 AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits());
Nikita Popov8dd19ed2019-01-07 18:15:11 +0000344 Worklist.push_back(J);
James Molloy87405c72015-08-14 11:09:09 +0000345 }
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 Popov110cf052018-12-07 15:38:13 +0000359 if (UserI->getType()->isIntOrIntVectorTy()) {
James Molloy87405c72015-08-14 11:09:09 +0000360 AOut = AliveBits[UserI];
Nikita Popovbc9986e2019-01-01 10:05:26 +0000361 LLVM_DEBUG(dbgs() << " Alive Out: 0x"
362 << Twine::utohexstr(AOut.getLimitedValue()));
James Molloy87405c72015-08-14 11:09:09 +0000363 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000364 LLVM_DEBUG(dbgs() << "\n");
James Molloy87405c72015-08-14 11:09:09 +0000365
Nikita Popov110cf052018-12-07 15:38:13 +0000366 if (!UserI->getType()->isIntOrIntVectorTy())
James Molloy87405c72015-08-14 11:09:09 +0000367 Visited.insert(UserI);
368
Craig Topperb45eabc2017-04-26 16:39:58 +0000369 KnownBits Known, Known2;
Nikita Popov6658fce2019-01-04 21:21:43 +0000370 bool KnownBitsComputed = false;
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()) {
Nikita Popov6658fce2019-01-04 21:21:43 +0000375 // We also want to detect dead uses of arguments, but will only store
376 // demanded bits for instructions.
377 Instruction *I = dyn_cast<Instruction>(OI);
378 if (!I && !isa<Argument>(OI))
379 continue;
Nikita Popovbc9986e2019-01-01 10:05:26 +0000380
Nikita Popov6658fce2019-01-04 21:21:43 +0000381 Type *T = OI->getType();
382 if (T->isIntOrIntVectorTy()) {
383 unsigned BitWidth = T->getScalarSizeInBits();
384 APInt AB = APInt::getAllOnesValue(BitWidth);
385 if (UserI->getType()->isIntOrIntVectorTy() && !AOut &&
386 !isAlwaysLive(UserI)) {
387 // If all bits of the output are dead, then all bits of the input
388 // are also dead.
389 AB = APInt(BitWidth, 0);
390 } else {
391 // Bits of each operand that are used to compute alive bits of the
392 // output are alive, all others are dead.
393 determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
394 Known, Known2, KnownBitsComputed);
Nikita Popov649e1252018-12-19 19:56:21 +0000395
Nikita Popov6658fce2019-01-04 21:21:43 +0000396 // Keep track of uses which have no demanded bits.
397 if (AB.isNullValue())
398 DeadUses.insert(&OI);
399 else
400 DeadUses.erase(&OI);
401 }
402
403 if (I) {
Nikita Popov649e1252018-12-19 19:56:21 +0000404 // If we've added to the set of alive bits (or the operand has not
405 // been previously visited), then re-queue the operand to be visited
406 // again.
Nikita Popov3817ee7902018-12-19 22:09:02 +0000407 APInt ABPrev(BitWidth, 0);
408 auto ABI = AliveBits.find(I);
409 if (ABI != AliveBits.end())
410 ABPrev = ABI->second;
411
James Molloy87405c72015-08-14 11:09:09 +0000412 APInt ABNew = AB | ABPrev;
413 if (ABNew != ABPrev || ABI == AliveBits.end()) {
414 AliveBits[I] = std::move(ABNew);
Nikita Popov8dd19ed2019-01-07 18:15:11 +0000415 Worklist.push_back(I);
James Molloy87405c72015-08-14 11:09:09 +0000416 }
James Molloy87405c72015-08-14 11:09:09 +0000417 }
Nikita Popov6658fce2019-01-04 21:21:43 +0000418 } else if (I && !Visited.count(I)) {
Nikita Popov8dd19ed2019-01-07 18:15:11 +0000419 Worklist.push_back(I);
James Molloy87405c72015-08-14 11:09:09 +0000420 }
421 }
422 }
James Molloy87405c72015-08-14 11:09:09 +0000423}
424
425APInt DemandedBits::getDemandedBits(Instruction *I) {
Nikita Popovcf65b922018-12-06 23:50:32 +0000426 performAnalysis();
Nikita Popov14ca9a82018-12-07 00:42:03 +0000427
Benjamin Kramera9e477b2016-07-21 13:37:55 +0000428 auto Found = AliveBits.find(I);
429 if (Found != AliveBits.end())
430 return Found->second;
Nikita Popov110cf052018-12-07 15:38:13 +0000431
432 const DataLayout &DL = I->getModule()->getDataLayout();
433 return APInt::getAllOnesValue(
434 DL.getTypeSizeInBits(I->getType()->getScalarType()));
James Molloy87405c72015-08-14 11:09:09 +0000435}
436
437bool DemandedBits::isInstructionDead(Instruction *I) {
James Molloyab9fdb92015-10-08 12:39:50 +0000438 performAnalysis();
439
James Molloy87405c72015-08-14 11:09:09 +0000440 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
441 !isAlwaysLive(I);
442}
443
Nikita Popovbc9986e2019-01-01 10:05:26 +0000444bool DemandedBits::isUseDead(Use *U) {
445 // We only track integer uses, everything else is assumed live.
446 if (!(*U)->getType()->isIntOrIntVectorTy())
447 return false;
448
449 // Uses by always-live instructions are never dead.
450 Instruction *UserI = cast<Instruction>(U->getUser());
451 if (isAlwaysLive(UserI))
452 return false;
453
454 performAnalysis();
455 if (DeadUses.count(U))
456 return true;
457
458 // If no output bits are demanded, no input bits are demanded and the use
459 // is dead. These uses might not be explicitly present in the DeadUses map.
460 if (UserI->getType()->isIntOrIntVectorTy()) {
461 auto Found = AliveBits.find(UserI);
462 if (Found != AliveBits.end() && Found->second.isNullValue())
463 return true;
464 }
465
466 return false;
467}
468
Michael Kupersteinde16b442016-04-18 23:55:01 +0000469void DemandedBits::print(raw_ostream &OS) {
470 performAnalysis();
James Molloybcd7f0a2015-10-08 12:39:59 +0000471 for (auto &KV : AliveBits) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000472 OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue())
473 << " for " << *KV.first << '\n';
James Molloybcd7f0a2015-10-08 12:39:59 +0000474 }
475}
476
Michael Kupersteinde16b442016-04-18 23:55:01 +0000477FunctionPass *llvm::createDemandedBitsWrapperPass() {
478 return new DemandedBitsWrapperPass();
479}
480
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000481AnalysisKey DemandedBitsAnalysis::Key;
Michael Kupersteinde16b442016-04-18 23:55:01 +0000482
483DemandedBits DemandedBitsAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000484 FunctionAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000485 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000486 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000487 return DemandedBits(F, AC, DT);
Michael Kupersteinde16b442016-04-18 23:55:01 +0000488}
489
490PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
491 FunctionAnalysisManager &AM) {
492 AM.getResult<DemandedBitsAnalysis>(F).print(OS);
493 return PreservedAnalyses::all();
James Molloy87405c72015-08-14 11:09:09 +0000494}