LLVM 22.0.0git
DemandedBits.cpp
Go to the documentation of this file.
1//===- DemandedBits.cpp - Determine demanded bits -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass implements a demanded bits analysis. A demanded bit is one that
10// contributes to a result; bits that are not demanded can be either zero or
11// one without affecting control or data flow. For example in this sequence:
12//
13// %1 = add i32 %x, %y
14// %2 = trunc i32 %1 to i16
15//
16// Only the lowest 16 bits of %1 are demanded; the rest are removed by the
17// trunc.
18//
19//===----------------------------------------------------------------------===//
20
22#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/SetVector.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/Dominators.h"
29#include "llvm/IR/Instruction.h"
31#include "llvm/IR/Operator.h"
32#include "llvm/IR/PassManager.h"
34#include "llvm/IR/Type.h"
35#include "llvm/IR/Use.h"
37#include "llvm/Support/Debug.h"
40#include <algorithm>
41#include <cstdint>
42
43using namespace llvm;
44using namespace llvm::PatternMatch;
45
46#define DEBUG_TYPE "demanded-bits"
47
48static bool isAlwaysLive(Instruction *I) {
49 return I->isTerminator() || I->isEHPad() || I->mayHaveSideEffects();
50}
51
52void DemandedBits::determineLiveOperandBits(
53 const Instruction *UserI, const Value *Val, unsigned OperandNo,
54 const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,
55 bool &KnownBitsComputed) {
56 unsigned BitWidth = AB.getBitWidth();
57
58 // We're called once per operand, but for some instructions, we need to
59 // compute known bits of both operands in order to determine the live bits of
60 // either (when both operands are instructions themselves). We don't,
61 // however, want to do this twice, so we cache the result in APInts that live
62 // in the caller. For the two-relevant-operands case, both operand values are
63 // provided here.
64 auto ComputeKnownBits =
65 [&](unsigned BitWidth, const Value *V1, const Value *V2) {
66 if (KnownBitsComputed)
67 return;
68 KnownBitsComputed = true;
69
70 const DataLayout &DL = UserI->getDataLayout();
71 Known = KnownBits(BitWidth);
72 computeKnownBits(V1, Known, DL, &AC, UserI, &DT);
73
74 if (V2) {
75 Known2 = KnownBits(BitWidth);
76 computeKnownBits(V2, Known2, DL, &AC, UserI, &DT);
77 }
78 };
79 auto GetShiftedRange = [&](uint64_t Min, uint64_t Max, bool ShiftLeft) {
80 auto ShiftF = [ShiftLeft](const APInt &Mask, unsigned ShiftAmnt) {
81 return ShiftLeft ? Mask.shl(ShiftAmnt) : Mask.lshr(ShiftAmnt);
82 };
84 uint64_t LoopRange = Max - Min;
85 APInt Mask = AOut;
86 APInt Shifted = AOut; // AOut | (AOut << 1) | ... | (AOut << (ShiftAmnt - 1)
87 for (unsigned ShiftAmnt = 1; ShiftAmnt <= LoopRange; ShiftAmnt <<= 1) {
88 if (LoopRange & ShiftAmnt) {
89 // Account for (LoopRange - ShiftAmnt, LoopRange]
90 Mask |= ShiftF(Shifted, LoopRange - ShiftAmnt + 1);
91 // Clears the low bit.
92 LoopRange -= ShiftAmnt;
93 }
94 // [0, ShiftAmnt) -> [0, ShiftAmnt * 2)
95 Shifted |= ShiftF(Shifted, ShiftAmnt);
96 }
97 AB = ShiftF(Mask, Min);
98 };
99
100 switch (UserI->getOpcode()) {
101 default: break;
102 case Instruction::Call:
103 case Instruction::Invoke:
104 if (const auto *II = dyn_cast<IntrinsicInst>(UserI)) {
105 switch (II->getIntrinsicID()) {
106 default: break;
107 case Intrinsic::bswap:
108 // The alive bits of the input are the swapped alive bits of
109 // the output.
110 AB = AOut.byteSwap();
111 break;
112 case Intrinsic::bitreverse:
113 // The alive bits of the input are the reversed alive bits of
114 // the output.
115 AB = AOut.reverseBits();
116 break;
117 case Intrinsic::ctlz:
118 if (OperandNo == 0) {
119 // We need some output bits, so we need all bits of the
120 // input to the left of, and including, the leftmost bit
121 // known to be one.
122 ComputeKnownBits(BitWidth, Val, nullptr);
124 std::min(BitWidth, Known.countMaxLeadingZeros()+1));
125 }
126 break;
127 case Intrinsic::cttz:
128 if (OperandNo == 0) {
129 // We need some output bits, so we need all bits of the
130 // input to the right of, and including, the rightmost bit
131 // known to be one.
132 ComputeKnownBits(BitWidth, Val, nullptr);
134 std::min(BitWidth, Known.countMaxTrailingZeros()+1));
135 }
136 break;
137 case Intrinsic::fshl:
138 case Intrinsic::fshr: {
139 const APInt *SA;
140 if (OperandNo == 2) {
141 // Shift amount is modulo the bitwidth. For powers of two we have
142 // SA % BW == SA & (BW - 1).
144 AB = BitWidth - 1;
145 } else if (match(II->getOperand(2), m_APInt(SA))) {
146 // Normalize to funnel shift left. APInt shifts of BitWidth are well-
147 // defined, so no need to special-case zero shifts here.
148 uint64_t ShiftAmt = SA->urem(BitWidth);
149 if (II->getIntrinsicID() == Intrinsic::fshr)
150 ShiftAmt = BitWidth - ShiftAmt;
151
152 if (OperandNo == 0)
153 AB = AOut.lshr(ShiftAmt);
154 else if (OperandNo == 1)
155 AB = AOut.shl(BitWidth - ShiftAmt);
156 }
157 break;
158 }
159 case Intrinsic::umax:
160 case Intrinsic::umin:
161 case Intrinsic::smax:
162 case Intrinsic::smin:
163 // If low bits of result are not demanded, they are also not demanded
164 // for the min/max operands.
166 break;
167 }
168 }
169 break;
170 case Instruction::Add:
171 if (AOut.isMask()) {
172 AB = AOut;
173 } else {
174 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
175 AB = determineLiveOperandBitsAdd(OperandNo, AOut, Known, Known2);
176 }
177 break;
178 case Instruction::Sub:
179 if (AOut.isMask()) {
180 AB = AOut;
181 } else {
182 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
183 AB = determineLiveOperandBitsSub(OperandNo, AOut, Known, Known2);
184 }
185 break;
186 case Instruction::Mul:
187 // Find the highest live output bit. We don't need any more input
188 // bits than that (adds, and thus subtracts, ripple only to the
189 // left).
191 break;
192 case Instruction::Shl:
193 if (OperandNo == 0) {
194 const APInt *ShiftAmtC;
195 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
196 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
197 AB = AOut.lshr(ShiftAmt);
198
199 // If the shift is nuw/nsw, then the high bits are not dead
200 // (because we've promised that they *must* be zero).
201 const auto *S = cast<ShlOperator>(UserI);
202 if (S->hasNoSignedWrap())
203 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
204 else if (S->hasNoUnsignedWrap())
205 AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
206 } else {
207 ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
208 uint64_t Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
209 uint64_t Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
210 // similar to Lshr case
211 GetShiftedRange(Min, Max, /*ShiftLeft=*/false);
212 const auto *S = cast<ShlOperator>(UserI);
213 if (S->hasNoSignedWrap())
214 AB |= APInt::getHighBitsSet(BitWidth, Max + 1);
215 else if (S->hasNoUnsignedWrap())
217 }
218 }
219 break;
220 case Instruction::LShr:
221 if (OperandNo == 0) {
222 const APInt *ShiftAmtC;
223 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
224 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
225 AB = AOut.shl(ShiftAmt);
226
227 // If the shift is exact, then the low bits are not dead
228 // (they must be zero).
229 if (cast<LShrOperator>(UserI)->isExact())
230 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
231 } else {
232 ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
233 uint64_t Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
234 uint64_t Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
235 // Suppose AOut == 0b0000 0001
236 // [min, max] = [1, 3]
237 // iteration 1 shift by 1 mask is 0b0000 0011
238 // iteration 2 shift by 2 mask is 0b0000 1111
239 // iteration 3, shiftAmnt = 4 > max - min, we stop.
240 //
241 // After the iterations we need one more shift by min,
242 // to move from 0b0000 1111 to --> 0b0001 1110.
243 // The loop populates the mask relative to (0,...,max-min),
244 // but we need coverage from (min, max).
245 // This is why the shift by min is needed.
246 GetShiftedRange(Min, Max, /*ShiftLeft=*/true);
247 if (cast<LShrOperator>(UserI)->isExact())
249 }
250 }
251 break;
252 case Instruction::AShr:
253 if (OperandNo == 0) {
254 const APInt *ShiftAmtC;
255 if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
256 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
257 AB = AOut.shl(ShiftAmt);
258 // Because the high input bit is replicated into the
259 // high-order bits of the result, if we need any of those
260 // bits, then we must keep the highest input bit.
261 if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
262 .getBoolValue())
263 AB.setSignBit();
264
265 // If the shift is exact, then the low bits are not dead
266 // (they must be zero).
267 if (cast<AShrOperator>(UserI)->isExact())
268 AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
269 } else {
270 ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
271 uint64_t Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
272 uint64_t Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
273 GetShiftedRange(Min, Max, /*ShiftLeft=*/true);
274 if (Max &&
275 (AOut & APInt::getHighBitsSet(BitWidth, Max)).getBoolValue()) {
276 // Suppose AOut = 0011 1100
277 // [min, max] = [1, 3]
278 // ShiftAmount = 1 : Mask is 1000 0000
279 // ShiftAmount = 2 : Mask is 1100 0000
280 // ShiftAmount = 3 : Mask is 1110 0000
281 // The Mask with Max covers every case in [min, max],
282 // so we are done
283 AB.setSignBit();
284 }
285 // If the shift is exact, then the low bits are not dead
286 // (they must be zero).
287 if (cast<AShrOperator>(UserI)->isExact())
289 }
290 }
291 break;
292 case Instruction::And:
293 AB = AOut;
294
295 // For bits that are known zero, the corresponding bits in the
296 // other operand are dead (unless they're both zero, in which
297 // case they can't both be dead, so just mark the LHS bits as
298 // dead).
299 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
300 if (OperandNo == 0)
301 AB &= ~Known2.Zero;
302 else
303 AB &= ~(Known.Zero & ~Known2.Zero);
304 break;
305 case Instruction::Or:
306 AB = AOut;
307
308 // For bits that are known one, the corresponding bits in the
309 // other operand are dead (unless they're both one, in which
310 // case they can't both be dead, so just mark the LHS bits as
311 // dead).
312 ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
313 if (OperandNo == 0)
314 AB &= ~Known2.One;
315 else
316 AB &= ~(Known.One & ~Known2.One);
317 break;
318 case Instruction::Xor:
319 case Instruction::PHI:
320 AB = AOut;
321 break;
322 case Instruction::Trunc:
323 AB = AOut.zext(BitWidth);
324 break;
325 case Instruction::ZExt:
326 AB = AOut.trunc(BitWidth);
327 break;
328 case Instruction::SExt:
329 AB = AOut.trunc(BitWidth);
330 // Because the high input bit is replicated into the
331 // high-order bits of the result, if we need any of those
332 // bits, then we must keep the highest input bit.
333 if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
334 AOut.getBitWidth() - BitWidth))
335 .getBoolValue())
336 AB.setSignBit();
337 break;
338 case Instruction::Select:
339 if (OperandNo != 0)
340 AB = AOut;
341 break;
342 case Instruction::ExtractElement:
343 if (OperandNo == 0)
344 AB = AOut;
345 break;
346 case Instruction::InsertElement:
347 case Instruction::ShuffleVector:
348 if (OperandNo == 0 || OperandNo == 1)
349 AB = AOut;
350 break;
351 }
352}
353
354void DemandedBits::performAnalysis() {
355 if (Analyzed)
356 // Analysis already completed for this function.
357 return;
358 Analyzed = true;
359
360 Visited.clear();
361 AliveBits.clear();
362 DeadUses.clear();
363
364 SmallSetVector<Instruction*, 16> Worklist;
365
366 // Collect the set of "root" instructions that are known live.
367 for (Instruction &I : instructions(F)) {
368 if (!isAlwaysLive(&I))
369 continue;
370
371 LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
372 // For integer-valued instructions, set up an initial empty set of alive
373 // bits and add the instruction to the work list. For other instructions
374 // add their operands to the work list (for integer values operands, mark
375 // all bits as live).
376 Type *T = I.getType();
377 if (T->isIntOrIntVectorTy()) {
378 if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
379 Worklist.insert(&I);
380
381 continue;
382 }
383
384 // Non-integer-typed instructions...
385 for (Use &OI : I.operands()) {
386 if (auto *J = dyn_cast<Instruction>(OI)) {
387 Type *T = J->getType();
388 if (T->isIntOrIntVectorTy())
389 AliveBits[J] = APInt::getAllOnes(T->getScalarSizeInBits());
390 else
391 Visited.insert(J);
392 Worklist.insert(J);
393 }
394 }
395 // To save memory, we don't add I to the Visited set here. Instead, we
396 // check isAlwaysLive on every instruction when searching for dead
397 // instructions later (we need to check isAlwaysLive for the
398 // integer-typed instructions anyway).
399 }
400
401 // Propagate liveness backwards to operands.
402 while (!Worklist.empty()) {
403 Instruction *UserI = Worklist.pop_back_val();
404
405 LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
406 APInt AOut;
407 bool InputIsKnownDead = false;
408 if (UserI->getType()->isIntOrIntVectorTy()) {
409 AOut = AliveBits[UserI];
410 LLVM_DEBUG(dbgs() << " Alive Out: 0x"
412
413 // If all bits of the output are dead, then all bits of the input
414 // are also dead.
415 InputIsKnownDead = !AOut && !isAlwaysLive(UserI);
416 }
417 LLVM_DEBUG(dbgs() << "\n");
418
419 KnownBits Known, Known2;
420 bool KnownBitsComputed = false;
421 // Compute the set of alive bits for each operand. These are anded into the
422 // existing set, if any, and if that changes the set of alive bits, the
423 // operand is added to the work-list.
424 for (Use &OI : UserI->operands()) {
425 // We also want to detect dead uses of arguments, but will only store
426 // demanded bits for instructions.
427 auto *I = dyn_cast<Instruction>(OI);
428 if (!I && !isa<Argument>(OI))
429 continue;
430
431 Type *T = OI->getType();
432 if (T->isIntOrIntVectorTy()) {
433 unsigned BitWidth = T->getScalarSizeInBits();
435 if (InputIsKnownDead) {
436 AB = APInt(BitWidth, 0);
437 } else {
438 // Bits of each operand that are used to compute alive bits of the
439 // output are alive, all others are dead.
440 determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
441 Known, Known2, KnownBitsComputed);
442
443 // Keep track of uses which have no demanded bits.
444 if (AB.isZero())
445 DeadUses.insert(&OI);
446 else
447 DeadUses.erase(&OI);
448 }
449
450 if (I) {
451 // If we've added to the set of alive bits (or the operand has not
452 // been previously visited), then re-queue the operand to be visited
453 // again.
454 auto Res = AliveBits.try_emplace(I);
455 if (Res.second || (AB |= Res.first->second) != Res.first->second) {
456 Res.first->second = std::move(AB);
457 Worklist.insert(I);
458 }
459 }
460 } else if (I && Visited.insert(I).second) {
461 Worklist.insert(I);
462 }
463 }
464 }
465}
466
468 performAnalysis();
469
470 auto Found = AliveBits.find(I);
471 if (Found != AliveBits.end())
472 return Found->second;
473
474 const DataLayout &DL = I->getDataLayout();
475 return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));
476}
477
479 Type *T = (*U)->getType();
480 auto *UserI = cast<Instruction>(U->getUser());
481 const DataLayout &DL = UserI->getDataLayout();
482 unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());
483
484 // We only track integer uses, everything else produces a mask with all bits
485 // set
486 if (!T->isIntOrIntVectorTy())
488
489 if (isUseDead(U))
490 return APInt(BitWidth, 0);
491
492 performAnalysis();
493
494 APInt AOut = getDemandedBits(UserI);
496 KnownBits Known, Known2;
497 bool KnownBitsComputed = false;
498
499 determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,
500 Known2, KnownBitsComputed);
501
502 return AB;
503}
504
506 performAnalysis();
507
508 return !Visited.count(I) && !AliveBits.contains(I) && !isAlwaysLive(I);
509}
510
512 // We only track integer uses, everything else is assumed live.
513 if (!(*U)->getType()->isIntOrIntVectorTy())
514 return false;
515
516 // Uses by always-live instructions are never dead.
517 auto *UserI = cast<Instruction>(U->getUser());
518 if (isAlwaysLive(UserI))
519 return false;
520
521 performAnalysis();
522 if (DeadUses.count(U))
523 return true;
524
525 // If no output bits are demanded, no input bits are demanded and the use
526 // is dead. These uses might not be explicitly present in the DeadUses map.
527 if (UserI->getType()->isIntOrIntVectorTy()) {
528 auto Found = AliveBits.find(UserI);
529 if (Found != AliveBits.end() && Found->second.isZero())
530 return true;
531 }
532
533 return false;
534}
535
537 auto PrintDB = [&](const Instruction *I, const APInt &A, Value *V = nullptr) {
538 OS << "DemandedBits: 0x" << Twine::utohexstr(A.getLimitedValue())
539 << " for ";
540 if (V) {
541 V->printAsOperand(OS, false);
542 OS << " in ";
543 }
544 OS << *I << '\n';
545 };
546
547 OS << "Printing analysis 'Demanded Bits Analysis' for function '" << F.getName() << "':\n";
548 performAnalysis();
549 for (auto &KV : AliveBits) {
550 Instruction *I = KV.first;
551 PrintDB(I, KV.second);
552
553 for (Use &OI : I->operands()) {
554 PrintDB(I, getDemandedBits(&OI), OI);
555 }
556 }
557}
558
559static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo,
560 const APInt &AOut,
561 const KnownBits &LHS,
562 const KnownBits &RHS,
563 bool CarryZero, bool CarryOne) {
564 assert(!(CarryZero && CarryOne) &&
565 "Carry can't be zero and one at the same time");
566
567 // The following check should be done by the caller, as it also indicates
568 // that LHS and RHS don't need to be computed.
569 //
570 // if (AOut.isMask())
571 // return AOut;
572
573 // Boundary bits' carry out is unaffected by their carry in.
574 APInt Bound = (LHS.Zero & RHS.Zero) | (LHS.One & RHS.One);
575
576 // First, the alive carry bits are determined from the alive output bits:
577 // Let demand ripple to the right but only up to any set bit in Bound.
578 // AOut = -1----
579 // Bound = ----1-
580 // ACarry&~AOut = --111-
581 APInt RBound = Bound.reverseBits();
582 APInt RAOut = AOut.reverseBits();
583 APInt RProp = RAOut + (RAOut | ~RBound);
584 APInt RACarry = RProp ^ ~RBound;
585 APInt ACarry = RACarry.reverseBits();
586
587 // Then, the alive input bits are determined from the alive carry bits:
588 APInt NeededToMaintainCarryZero;
589 APInt NeededToMaintainCarryOne;
590 if (OperandNo == 0) {
591 NeededToMaintainCarryZero = LHS.Zero | ~RHS.Zero;
592 NeededToMaintainCarryOne = LHS.One | ~RHS.One;
593 } else {
594 NeededToMaintainCarryZero = RHS.Zero | ~LHS.Zero;
595 NeededToMaintainCarryOne = RHS.One | ~LHS.One;
596 }
597
598 // As in computeForAddCarry
599 APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;
600 APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;
601
602 // The below is simplified from
603 //
604 // APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
605 // APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
606 // APInt CarryUnknown = ~(CarryKnownZero | CarryKnownOne);
607 //
608 // APInt NeededToMaintainCarry =
609 // (CarryKnownZero & NeededToMaintainCarryZero) |
610 // (CarryKnownOne & NeededToMaintainCarryOne) |
611 // CarryUnknown;
612
613 APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &
614 (PossibleSumOne | NeededToMaintainCarryOne);
615
616 APInt AB = AOut | (ACarry & NeededToMaintainCarry);
617 return AB;
618}
619
621 const APInt &AOut,
622 const KnownBits &LHS,
623 const KnownBits &RHS) {
624 return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, RHS, true,
625 false);
626}
627
629 const APInt &AOut,
630 const KnownBits &LHS,
631 const KnownBits &RHS) {
632 KnownBits NRHS;
633 NRHS.Zero = RHS.One;
634 NRHS.One = RHS.Zero;
635 return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, NRHS, false,
636 true);
637}
638
639AnalysisKey DemandedBitsAnalysis::Key;
640
647
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool isAlwaysLive(Instruction *I)
static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS, bool CarryZero, bool CarryOne)
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define T
uint64_t IntrinsicInst * II
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1512
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:475
bool isMask(unsigned numBits) const
Definition APInt.h:488
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
An analysis that produces DemandedBits for a function.
LLVM_ABI DemandedBits run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce demanded bits information.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI void print(raw_ostream &OS)
LLVM_ABI APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
static LLVM_ABI APInt determineLiveOperandBitsAdd(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one addition operand from alive output and known operand bits.
LLVM_ABI bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
static LLVM_ABI APInt determineLiveOperandBitsSub(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one subtraction operand from alive output and known operand bits.
LLVM_ABI bool isUseDead(Use *U)
Return whether this use is dead by means of not having any demanded bits.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:99
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:168
value_type pop_back_val()
Definition SetVector.h:296
static Twine utohexstr(const uint64_t &Val)
Definition Twine.h:392
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:292
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool match(Val *V, const Pattern &P)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:288
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:267
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:138
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:122
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:273