blob: 34f785fb02be01b16f4ab97eb3628ae9bbad938e [file] [log] [blame]
Eugene Zelenko15a56d42017-07-21 21:37:46 +00001//===- DemandedBits.cpp - Determine demanded bits -------------------------===//
James Molloy26e17392015-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 Zelenko15a56d42017-07-21 21:37:46 +000023#include "llvm/ADT/APInt.h"
Nikita Popov14b956f2019-01-12 09:09:15 +000024#include "llvm/ADT/SetVector.h"
James Molloy6f819bd2015-10-08 12:39:59 +000025#include "llvm/ADT/StringExtras.h"
Daniel Jasper8de3a542016-12-19 08:22:17 +000026#include "llvm/Analysis/AssumptionCache.h"
James Molloy26e17392015-08-14 11:09:09 +000027#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/IR/BasicBlock.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000029#include "llvm/IR/Constants.h"
James Molloy26e17392015-08-14 11:09:09 +000030#include "llvm/IR/DataLayout.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000031#include "llvm/IR/DerivedTypes.h"
James Molloy26e17392015-08-14 11:09:09 +000032#include "llvm/IR/Dominators.h"
33#include "llvm/IR/InstIterator.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000034#include "llvm/IR/InstrTypes.h"
35#include "llvm/IR/Instruction.h"
James Molloy26e17392015-08-14 11:09:09 +000036#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000037#include "llvm/IR/Intrinsics.h"
James Molloy26e17392015-08-14 11:09:09 +000038#include "llvm/IR/Module.h"
39#include "llvm/IR/Operator.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000040#include "llvm/IR/PassManager.h"
Nikita Popov38880e62018-12-07 15:38:13 +000041#include "llvm/IR/PatternMatch.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000042#include "llvm/IR/Type.h"
43#include "llvm/IR/Use.h"
James Molloy26e17392015-08-14 11:09:09 +000044#include "llvm/Pass.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000045#include "llvm/Support/Casting.h"
James Molloy26e17392015-08-14 11:09:09 +000046#include "llvm/Support/Debug.h"
Craig Topper58c7fe62017-04-26 16:39:58 +000047#include "llvm/Support/KnownBits.h"
James Molloy26e17392015-08-14 11:09:09 +000048#include "llvm/Support/raw_ostream.h"
Eugene Zelenko15a56d42017-07-21 21:37:46 +000049#include <algorithm>
50#include <cstdint>
51
James Molloy26e17392015-08-14 11:09:09 +000052using namespace llvm;
Nikita Popov38880e62018-12-07 15:38:13 +000053using namespace llvm::PatternMatch;
James Molloy26e17392015-08-14 11:09:09 +000054
55#define DEBUG_TYPE "demanded-bits"
56
Michael Kuperstein3445cc72016-04-18 23:55:01 +000057char DemandedBitsWrapperPass::ID = 0;
Eugene Zelenko15a56d42017-07-21 21:37:46 +000058
Michael Kuperstein3445cc72016-04-18 23:55:01 +000059INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
60 "Demanded bits analysis", false, false)
Daniel Jasper8de3a542016-12-19 08:22:17 +000061INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
James Molloy26e17392015-08-14 11:09:09 +000062INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Michael Kuperstein3445cc72016-04-18 23:55:01 +000063INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
64 "Demanded bits analysis", false, false)
James Molloy26e17392015-08-14 11:09:09 +000065
Michael Kuperstein3445cc72016-04-18 23:55:01 +000066DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
67 initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
James Molloy26e17392015-08-14 11:09:09 +000068}
69
Michael Kuperstein3445cc72016-04-18 23:55:01 +000070void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
James Molloy26e17392015-08-14 11:09:09 +000071 AU.setPreservesCFG();
Daniel Jasper8de3a542016-12-19 08:22:17 +000072 AU.addRequired<AssumptionCacheTracker>();
James Molloy26e17392015-08-14 11:09:09 +000073 AU.addRequired<DominatorTreeWrapperPass>();
74 AU.setPreservesAll();
75}
76
Michael Kuperstein3445cc72016-04-18 23:55:01 +000077void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
78 DB->print(OS);
79}
80
James Molloy26e17392015-08-14 11:09:09 +000081static bool isAlwaysLive(Instruction *I) {
Chandler Carruth9179aee2018-08-26 09:51:22 +000082 return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() ||
83 I->mayHaveSideEffects();
James Molloy26e17392015-08-14 11:09:09 +000084}
85
NAKAMURA Takumi09c0ea52015-09-22 11:15:07 +000086void DemandedBits::determineLiveOperandBits(
Nikita Popovef0f0e12019-01-04 21:21:43 +000087 const Instruction *UserI, const Value *Val, unsigned OperandNo,
88 const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,
89 bool &KnownBitsComputed) {
James Molloy26e17392015-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) {
Nikita Popovef0f0e12019-01-04 21:21:43 +0000100 if (KnownBitsComputed)
101 return;
102 KnownBitsComputed = true;
103
104 const DataLayout &DL = UserI->getModule()->getDataLayout();
Craig Topper58c7fe62017-04-26 16:39:58 +0000105 Known = KnownBits(BitWidth);
Craig Toppera7f9de42017-05-13 17:22:16 +0000106 computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
James Molloy26e17392015-08-14 11:09:09 +0000107
108 if (V2) {
Craig Topper58c7fe62017-04-26 16:39:58 +0000109 Known2 = KnownBits(BitWidth);
Craig Toppera7f9de42017-05-13 17:22:16 +0000110 computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
James Molloy26e17392015-08-14 11:09:09 +0000111 }
112 };
113
114 switch (UserI->getOpcode()) {
115 default: break;
116 case Instruction::Call:
117 case Instruction::Invoke:
118 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
119 switch (II->getIntrinsicID()) {
120 default: break;
121 case Intrinsic::bswap:
122 // The alive bits of the input are the swapped alive bits of
123 // the output.
124 AB = AOut.byteSwap();
125 break;
Brian Gesiakb2b7c4b2017-04-13 16:44:25 +0000126 case Intrinsic::bitreverse:
Xin Tongb06e4062017-06-19 20:10:41 +0000127 // The alive bits of the input are the reversed alive bits of
128 // the output.
Brian Gesiakb2b7c4b2017-04-13 16:44:25 +0000129 AB = AOut.reverseBits();
130 break;
James Molloy26e17392015-08-14 11:09:09 +0000131 case Intrinsic::ctlz:
132 if (OperandNo == 0) {
133 // We need some output bits, so we need all bits of the
134 // input to the left of, and including, the leftmost bit
135 // known to be one.
Nikita Popovef0f0e12019-01-04 21:21:43 +0000136 ComputeKnownBits(BitWidth, Val, nullptr);
James Molloy26e17392015-08-14 11:09:09 +0000137 AB = APInt::getHighBitsSet(BitWidth,
Craig Topperd4934442017-05-12 17:20:30 +0000138 std::min(BitWidth, Known.countMaxLeadingZeros()+1));
James Molloy26e17392015-08-14 11:09:09 +0000139 }
140 break;
141 case Intrinsic::cttz:
142 if (OperandNo == 0) {
143 // We need some output bits, so we need all bits of the
144 // input to the right of, and including, the rightmost bit
145 // known to be one.
Nikita Popovef0f0e12019-01-04 21:21:43 +0000146 ComputeKnownBits(BitWidth, Val, nullptr);
James Molloy26e17392015-08-14 11:09:09 +0000147 AB = APInt::getLowBitsSet(BitWidth,
Craig Topperd4934442017-05-12 17:20:30 +0000148 std::min(BitWidth, Known.countMaxTrailingZeros()+1));
James Molloy26e17392015-08-14 11:09:09 +0000149 }
150 break;
Nikita Popov753cb242018-11-26 15:36:57 +0000151 case Intrinsic::fshl:
Nikita Popov38880e62018-12-07 15:38:13 +0000152 case Intrinsic::fshr: {
153 const APInt *SA;
Nikita Popov753cb242018-11-26 15:36:57 +0000154 if (OperandNo == 2) {
155 // Shift amount is modulo the bitwidth. For powers of two we have
156 // SA % BW == SA & (BW - 1).
157 if (isPowerOf2_32(BitWidth))
158 AB = BitWidth - 1;
Nikita Popov38880e62018-12-07 15:38:13 +0000159 } else if (match(II->getOperand(2), m_APInt(SA))) {
Nikita Popov753cb242018-11-26 15:36:57 +0000160 // Normalize to funnel shift left. APInt shifts of BitWidth are well-
161 // defined, so no need to special-case zero shifts here.
Nikita Popov38880e62018-12-07 15:38:13 +0000162 uint64_t ShiftAmt = SA->urem(BitWidth);
Nikita Popov753cb242018-11-26 15:36:57 +0000163 if (II->getIntrinsicID() == Intrinsic::fshr)
164 ShiftAmt = BitWidth - ShiftAmt;
165
166 if (OperandNo == 0)
167 AB = AOut.lshr(ShiftAmt);
168 else if (OperandNo == 1)
169 AB = AOut.shl(BitWidth - ShiftAmt);
170 }
171 break;
James Molloy26e17392015-08-14 11:09:09 +0000172 }
Nikita Popov38880e62018-12-07 15:38:13 +0000173 }
James Molloy26e17392015-08-14 11:09:09 +0000174 break;
175 case Instruction::Add:
176 case Instruction::Sub:
James Molloy6f819bd2015-10-08 12:39:59 +0000177 case Instruction::Mul:
James Molloy26e17392015-08-14 11:09:09 +0000178 // Find the highest live output bit. We don't need any more input
179 // bits than that (adds, and thus subtracts, ripple only to the
180 // left).
181 AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
182 break;
183 case Instruction::Shl:
Nikita Popov38880e62018-12-07 15:38:13 +0000184 if (OperandNo == 0) {
185 const APInt *ShiftAmtC;
186 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel28bebe42017-07-07 14:39:26 +0000187 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy26e17392015-08-14 11:09:09 +0000188 AB = AOut.lshr(ShiftAmt);
189
190 // If the shift is nuw/nsw, then the high bits are not dead
191 // (because we've promised that they *must* be zero).
192 const ShlOperator *S = cast<ShlOperator>(UserI);
193 if (S->hasNoSignedWrap())
194 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
195 else if (S->hasNoUnsignedWrap())
196 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
197 }
Nikita Popov38880e62018-12-07 15:38:13 +0000198 }
James Molloy26e17392015-08-14 11:09:09 +0000199 break;
200 case Instruction::LShr:
Nikita Popov38880e62018-12-07 15:38:13 +0000201 if (OperandNo == 0) {
202 const APInt *ShiftAmtC;
203 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel28bebe42017-07-07 14:39:26 +0000204 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy26e17392015-08-14 11:09:09 +0000205 AB = AOut.shl(ShiftAmt);
206
207 // If the shift is exact, then the low bits are not dead
208 // (they must be zero).
209 if (cast<LShrOperator>(UserI)->isExact())
210 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
211 }
Nikita Popov38880e62018-12-07 15:38:13 +0000212 }
James Molloy26e17392015-08-14 11:09:09 +0000213 break;
214 case Instruction::AShr:
Nikita Popov38880e62018-12-07 15:38:13 +0000215 if (OperandNo == 0) {
216 const APInt *ShiftAmtC;
217 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
Sanjay Patel28bebe42017-07-07 14:39:26 +0000218 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
James Molloy26e17392015-08-14 11:09:09 +0000219 AB = AOut.shl(ShiftAmt);
220 // Because the high input bit is replicated into the
221 // high-order bits of the result, if we need any of those
222 // bits, then we must keep the highest input bit.
223 if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
224 .getBoolValue())
Craig Topper78b412f2017-04-28 16:58:05 +0000225 AB.setSignBit();
James Molloy26e17392015-08-14 11:09:09 +0000226
227 // If the shift is exact, then the low bits are not dead
228 // (they must be zero).
229 if (cast<AShrOperator>(UserI)->isExact())
230 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
231 }
Nikita Popov38880e62018-12-07 15:38:13 +0000232 }
James Molloy26e17392015-08-14 11:09:09 +0000233 break;
234 case Instruction::And:
235 AB = AOut;
236
237 // For bits that are known zero, the corresponding bits in the
238 // other operand are dead (unless they're both zero, in which
239 // case they can't both be dead, so just mark the LHS bits as
240 // dead).
Nikita Popovef0f0e12019-01-04 21:21:43 +0000241 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
242 if (OperandNo == 0)
Craig Topper58c7fe62017-04-26 16:39:58 +0000243 AB &= ~Known2.Zero;
Nikita Popovef0f0e12019-01-04 21:21:43 +0000244 else
Craig Topper58c7fe62017-04-26 16:39:58 +0000245 AB &= ~(Known.Zero & ~Known2.Zero);
James Molloy26e17392015-08-14 11:09:09 +0000246 break;
247 case Instruction::Or:
248 AB = AOut;
249
250 // For bits that are known one, the corresponding bits in the
251 // other operand are dead (unless they're both one, in which
252 // case they can't both be dead, so just mark the LHS bits as
253 // dead).
Nikita Popovef0f0e12019-01-04 21:21:43 +0000254 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
255 if (OperandNo == 0)
Craig Topper58c7fe62017-04-26 16:39:58 +0000256 AB &= ~Known2.One;
Nikita Popovef0f0e12019-01-04 21:21:43 +0000257 else
Craig Topper58c7fe62017-04-26 16:39:58 +0000258 AB &= ~(Known.One & ~Known2.One);
James Molloy26e17392015-08-14 11:09:09 +0000259 break;
260 case Instruction::Xor:
261 case Instruction::PHI:
262 AB = AOut;
263 break;
264 case Instruction::Trunc:
265 AB = AOut.zext(BitWidth);
266 break;
267 case Instruction::ZExt:
268 AB = AOut.trunc(BitWidth);
269 break;
270 case Instruction::SExt:
271 AB = AOut.trunc(BitWidth);
272 // Because the high input bit is replicated into the
273 // high-order bits of the result, if we need any of those
274 // bits, then we must keep the highest input bit.
275 if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
276 AOut.getBitWidth() - BitWidth))
277 .getBoolValue())
Craig Topper78b412f2017-04-28 16:58:05 +0000278 AB.setSignBit();
James Molloy26e17392015-08-14 11:09:09 +0000279 break;
280 case Instruction::Select:
281 if (OperandNo != 0)
282 AB = AOut;
283 break;
Nikita Popov38880e62018-12-07 15:38:13 +0000284 case Instruction::ExtractElement:
285 if (OperandNo == 0)
286 AB = AOut;
287 break;
288 case Instruction::InsertElement:
289 case Instruction::ShuffleVector:
290 if (OperandNo == 0 || OperandNo == 1)
291 AB = AOut;
292 break;
James Molloy26e17392015-08-14 11:09:09 +0000293 }
294}
295
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000296bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
Daniel Jasper8de3a542016-12-19 08:22:17 +0000297 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000298 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Daniel Jasper8de3a542016-12-19 08:22:17 +0000299 DB.emplace(F, AC, DT);
James Molloycbaa8532015-10-08 12:39:50 +0000300 return false;
301}
James Molloy26e17392015-08-14 11:09:09 +0000302
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000303void DemandedBitsWrapperPass::releaseMemory() {
304 DB.reset();
305}
306
James Molloycbaa8532015-10-08 12:39:50 +0000307void DemandedBits::performAnalysis() {
308 if (Analyzed)
309 // Analysis already completed for this function.
310 return;
311 Analyzed = true;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000312
James Molloy26e17392015-08-14 11:09:09 +0000313 Visited.clear();
314 AliveBits.clear();
Nikita Popove0020752019-01-01 10:05:26 +0000315 DeadUses.clear();
James Molloy26e17392015-08-14 11:09:09 +0000316
Nikita Popov14b956f2019-01-12 09:09:15 +0000317 SmallSetVector<Instruction*, 16> Worklist;
James Molloy26e17392015-08-14 11:09:09 +0000318
319 // Collect the set of "root" instructions that are known live.
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000320 for (Instruction &I : instructions(F)) {
James Molloy26e17392015-08-14 11:09:09 +0000321 if (!isAlwaysLive(&I))
322 continue;
323
Nicola Zaghen0818e782018-05-14 12:53:11 +0000324 LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
James Molloy26e17392015-08-14 11:09:09 +0000325 // For integer-valued instructions, set up an initial empty set of alive
326 // bits and add the instruction to the work list. For other instructions
327 // add their operands to the work list (for integer values operands, mark
328 // all bits as live).
Nikita Popov38880e62018-12-07 15:38:13 +0000329 Type *T = I.getType();
330 if (T->isIntOrIntVectorTy()) {
331 if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
Nikita Popov14b956f2019-01-12 09:09:15 +0000332 Worklist.insert(&I);
James Molloy26e17392015-08-14 11:09:09 +0000333
334 continue;
335 }
336
337 // Non-integer-typed instructions...
338 for (Use &OI : I.operands()) {
339 if (Instruction *J = dyn_cast<Instruction>(OI)) {
Nikita Popov38880e62018-12-07 15:38:13 +0000340 Type *T = J->getType();
341 if (T->isIntOrIntVectorTy())
342 AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits());
Nikita Popov14b956f2019-01-12 09:09:15 +0000343 Worklist.insert(J);
James Molloy26e17392015-08-14 11:09:09 +0000344 }
345 }
346 // To save memory, we don't add I to the Visited set here. Instead, we
347 // check isAlwaysLive on every instruction when searching for dead
348 // instructions later (we need to check isAlwaysLive for the
349 // integer-typed instructions anyway).
350 }
351
352 // Propagate liveness backwards to operands.
353 while (!Worklist.empty()) {
354 Instruction *UserI = Worklist.pop_back_val();
355
Nicola Zaghen0818e782018-05-14 12:53:11 +0000356 LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
James Molloy26e17392015-08-14 11:09:09 +0000357 APInt AOut;
Nikita Popov38880e62018-12-07 15:38:13 +0000358 if (UserI->getType()->isIntOrIntVectorTy()) {
James Molloy26e17392015-08-14 11:09:09 +0000359 AOut = AliveBits[UserI];
Nikita Popove0020752019-01-01 10:05:26 +0000360 LLVM_DEBUG(dbgs() << " Alive Out: 0x"
361 << Twine::utohexstr(AOut.getLimitedValue()));
James Molloy26e17392015-08-14 11:09:09 +0000362 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000363 LLVM_DEBUG(dbgs() << "\n");
James Molloy26e17392015-08-14 11:09:09 +0000364
Nikita Popov38880e62018-12-07 15:38:13 +0000365 if (!UserI->getType()->isIntOrIntVectorTy())
James Molloy26e17392015-08-14 11:09:09 +0000366 Visited.insert(UserI);
367
Craig Topper58c7fe62017-04-26 16:39:58 +0000368 KnownBits Known, Known2;
Nikita Popovef0f0e12019-01-04 21:21:43 +0000369 bool KnownBitsComputed = false;
James Molloy26e17392015-08-14 11:09:09 +0000370 // Compute the set of alive bits for each operand. These are anded into the
371 // existing set, if any, and if that changes the set of alive bits, the
372 // operand is added to the work-list.
373 for (Use &OI : UserI->operands()) {
Nikita Popovef0f0e12019-01-04 21:21:43 +0000374 // We also want to detect dead uses of arguments, but will only store
375 // demanded bits for instructions.
376 Instruction *I = dyn_cast<Instruction>(OI);
377 if (!I && !isa<Argument>(OI))
378 continue;
Nikita Popove0020752019-01-01 10:05:26 +0000379
Nikita Popovef0f0e12019-01-04 21:21:43 +0000380 Type *T = OI->getType();
381 if (T->isIntOrIntVectorTy()) {
382 unsigned BitWidth = T->getScalarSizeInBits();
383 APInt AB = APInt::getAllOnesValue(BitWidth);
384 if (UserI->getType()->isIntOrIntVectorTy() && !AOut &&
385 !isAlwaysLive(UserI)) {
386 // If all bits of the output are dead, then all bits of the input
387 // are also dead.
388 AB = APInt(BitWidth, 0);
389 } else {
390 // Bits of each operand that are used to compute alive bits of the
391 // output are alive, all others are dead.
392 determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
393 Known, Known2, KnownBitsComputed);
Nikita Popovc93c3832018-12-19 19:56:21 +0000394
Nikita Popovef0f0e12019-01-04 21:21:43 +0000395 // Keep track of uses which have no demanded bits.
396 if (AB.isNullValue())
397 DeadUses.insert(&OI);
398 else
399 DeadUses.erase(&OI);
400 }
401
402 if (I) {
Nikita Popovc93c3832018-12-19 19:56:21 +0000403 // If we've added to the set of alive bits (or the operand has not
404 // been previously visited), then re-queue the operand to be visited
405 // again.
Nikita Popov992a5452018-12-19 22:09:02 +0000406 APInt ABPrev(BitWidth, 0);
407 auto ABI = AliveBits.find(I);
408 if (ABI != AliveBits.end())
409 ABPrev = ABI->second;
410
James Molloy26e17392015-08-14 11:09:09 +0000411 APInt ABNew = AB | ABPrev;
412 if (ABNew != ABPrev || ABI == AliveBits.end()) {
413 AliveBits[I] = std::move(ABNew);
Nikita Popov14b956f2019-01-12 09:09:15 +0000414 Worklist.insert(I);
James Molloy26e17392015-08-14 11:09:09 +0000415 }
James Molloy26e17392015-08-14 11:09:09 +0000416 }
Nikita Popovef0f0e12019-01-04 21:21:43 +0000417 } else if (I && !Visited.count(I)) {
Nikita Popov14b956f2019-01-12 09:09:15 +0000418 Worklist.insert(I);
James Molloy26e17392015-08-14 11:09:09 +0000419 }
420 }
421 }
James Molloy26e17392015-08-14 11:09:09 +0000422}
423
424APInt DemandedBits::getDemandedBits(Instruction *I) {
Nikita Popov6e9e89f2018-12-06 23:50:32 +0000425 performAnalysis();
Nikita Popov0ac91b62018-12-07 00:42:03 +0000426
Benjamin Kramerffecbde2016-07-21 13:37:55 +0000427 auto Found = AliveBits.find(I);
428 if (Found != AliveBits.end())
429 return Found->second;
Nikita Popov38880e62018-12-07 15:38:13 +0000430
431 const DataLayout &DL = I->getModule()->getDataLayout();
432 return APInt::getAllOnesValue(
433 DL.getTypeSizeInBits(I->getType()->getScalarType()));
James Molloy26e17392015-08-14 11:09:09 +0000434}
435
436bool DemandedBits::isInstructionDead(Instruction *I) {
James Molloycbaa8532015-10-08 12:39:50 +0000437 performAnalysis();
438
James Molloy26e17392015-08-14 11:09:09 +0000439 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
440 !isAlwaysLive(I);
441}
442
Nikita Popove0020752019-01-01 10:05:26 +0000443bool DemandedBits::isUseDead(Use *U) {
444 // We only track integer uses, everything else is assumed live.
445 if (!(*U)->getType()->isIntOrIntVectorTy())
446 return false;
447
448 // Uses by always-live instructions are never dead.
449 Instruction *UserI = cast<Instruction>(U->getUser());
450 if (isAlwaysLive(UserI))
451 return false;
452
453 performAnalysis();
454 if (DeadUses.count(U))
455 return true;
456
457 // If no output bits are demanded, no input bits are demanded and the use
458 // is dead. These uses might not be explicitly present in the DeadUses map.
459 if (UserI->getType()->isIntOrIntVectorTy()) {
460 auto Found = AliveBits.find(UserI);
461 if (Found != AliveBits.end() && Found->second.isNullValue())
462 return true;
463 }
464
465 return false;
466}
467
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000468void DemandedBits::print(raw_ostream &OS) {
469 performAnalysis();
James Molloy6f819bd2015-10-08 12:39:59 +0000470 for (auto &KV : AliveBits) {
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000471 OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue())
472 << " for " << *KV.first << '\n';
James Molloy6f819bd2015-10-08 12:39:59 +0000473 }
474}
475
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000476FunctionPass *llvm::createDemandedBitsWrapperPass() {
477 return new DemandedBitsWrapperPass();
478}
479
Chandler Carruth33d56812016-11-23 17:53:26 +0000480AnalysisKey DemandedBitsAnalysis::Key;
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000481
482DemandedBits DemandedBitsAnalysis::run(Function &F,
Sean Silva20b343c2016-08-09 00:28:15 +0000483 FunctionAnalysisManager &AM) {
Daniel Jasper8de3a542016-12-19 08:22:17 +0000484 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000485 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Daniel Jasper8de3a542016-12-19 08:22:17 +0000486 return DemandedBits(F, AC, DT);
Michael Kuperstein3445cc72016-04-18 23:55:01 +0000487}
488
489PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
490 FunctionAnalysisManager &AM) {
491 AM.getResult<DemandedBitsAnalysis>(F).print(OS);
492 return PreservedAnalyses::all();
James Molloy26e17392015-08-14 11:09:09 +0000493}