LLVM 24.0.0git
GISelValueTracking.cpp
Go to the documentation of this file.
1//===- lib/CodeGen/GlobalISel/GISelValueTracking.cpp --------------*- C++
2//*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10/// Provides analysis for querying information about KnownBits during GISel
11/// passes.
12//
13//===----------------------------------------------------------------------===//
15#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/ScopeExit.h"
35#include "llvm/IR/FMF.h"
41
42#define DEBUG_TYPE "gisel-known-bits"
43
44using namespace llvm;
45using namespace MIPatternMatch;
46
48
50 "Analysis for ComputingKnownBits", false, true)
51
53 : MF(MF), MRI(MF.getRegInfo()), TL(*MF.getSubtarget().getTargetLowering()),
54 DL(MF.getFunction().getDataLayout()), MaxDepth(MaxDepth) {}
55
57 const MachineInstr *MI = MRI.getVRegDef(R);
58 switch (MI->getOpcode()) {
59 case TargetOpcode::COPY:
60 return computeKnownAlignment(MI->getOperand(1).getReg(), Depth);
61 case TargetOpcode::G_ASSERT_ALIGN: {
62 // TODO: Min with source
63 return Align(MI->getOperand(2).getImm());
64 }
65 case TargetOpcode::G_FRAME_INDEX: {
66 int FrameIdx = MI->getOperand(1).getIndex();
67 return MF.getFrameInfo().getObjectAlign(FrameIdx);
68 }
69 case TargetOpcode::G_INTRINSIC:
70 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
71 case TargetOpcode::G_INTRINSIC_CONVERGENT:
72 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
73 default:
74 return TL.computeKnownAlignForTargetInstr(*this, R, MRI, Depth + 1);
75 }
76}
77
79 const LLT Ty = MRI.getType(R);
80 // Since the number of lanes in a scalable vector is unknown at compile time,
81 // we track one bit which is implicitly broadcast to all lanes. This means
82 // that all lanes in a scalable vector are considered demanded.
83 APInt DemandedElts =
84 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
85 return getKnownBits(R, DemandedElts);
86}
87
89 const APInt &DemandedElts,
90 unsigned Depth) {
92 computeKnownBitsImpl(R, Known, DemandedElts, Depth);
93 return Known;
94}
95
97 LLT Ty = MRI.getType(R);
98 unsigned BitWidth = Ty.getScalarSizeInBits();
100}
101
103 LLT Ty = MRI.getType(R);
104 const APInt ScalarDemandedElts(1, 1);
105 APInt DemandedElts = Ty.isFixedVector()
106 ? APInt::getAllOnes(Ty.getNumElements())
107 : ScalarDemandedElts;
108 return isKnownNeverZero(R, DemandedElts, Depth);
109}
110
112 unsigned Depth) {
113 if (Depth >= getMaxDepth())
114 return false;
115
116 const APInt ScalarDemandedElts(1, 1);
117 MachineInstr &MI = *MRI.getVRegDef(R);
118
119 switch (MI.getOpcode()) {
120 default:
121 break;
122
123 case TargetOpcode::G_BUILD_VECTOR: {
124 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
125 if (!DemandedElts[I])
126 continue;
127 if (!isKnownNeverZero(MO.getReg(), ScalarDemandedElts, Depth + 1))
128 return false;
129 }
130 return true;
131 }
132
133 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
135 Register InVec = Extract.getVectorReg();
136 LLT VecTy = MRI.getType(InVec);
137 if (VecTy.isScalableVector())
138 break;
139 unsigned NumSrcElts = VecTy.getNumElements();
140 // An out-of-range constant index produces poison. Keep all lanes demanded,
141 // which is poison-safe and matches SelectionDAG's conservative behavior.
142 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
143 if (auto Idx = getIConstantVRegVal(Extract.getIndexReg(), MRI)) {
144 if (Idx->ult(NumSrcElts))
145 DemandedSrcElts = APInt::getOneBitSet(NumSrcElts, Idx->getZExtValue());
146 }
147 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
148 }
149
150 case TargetOpcode::G_SHUFFLE_VECTOR: {
152 LLT SrcTy = MRI.getType(Shuf.getSrc1Reg());
153 if (SrcTy.isScalableVector())
154 break;
155 APInt DemandedLHS, DemandedRHS;
156 if (!getShuffleDemandedElts(SrcTy.getNumElements(), Shuf.getMask(),
157 DemandedElts, DemandedLHS, DemandedRHS))
158 break;
159 if (!DemandedLHS.isZero() &&
160 !isKnownNeverZero(Shuf.getSrc1Reg(), DemandedLHS, Depth + 1))
161 return false;
162 if (!DemandedRHS.isZero() &&
163 !isKnownNeverZero(Shuf.getSrc2Reg(), DemandedRHS, Depth + 1))
164 return false;
165 return true;
166 }
167
168 case TargetOpcode::G_OR:
169 return isKnownNeverZero(MI.getOperand(1).getReg(), DemandedElts,
170 Depth + 1) ||
171 isKnownNeverZero(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
172
173 case TargetOpcode::G_SELECT:
174 return isKnownNeverZero(MI.getOperand(2).getReg(), DemandedElts,
175 Depth + 1) &&
176 isKnownNeverZero(MI.getOperand(3).getReg(), DemandedElts, Depth + 1);
177
178 case TargetOpcode::G_SHL: {
179 Register LHSReg = MI.getOperand(1).getReg();
180 if (MI.getFlag(MachineInstr::NoSWrap) || MI.getFlag(MachineInstr::NoUWrap))
181 return isKnownNeverZero(LHSReg, DemandedElts, Depth + 1);
182 KnownBits ValKnown = getKnownBits(LHSReg, DemandedElts, Depth + 1);
183 if (ValKnown.One[0])
184 return true;
185 APInt MaxCnt =
186 getKnownBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1)
187 .getMaxValue();
188 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
189 !ValKnown.One.shl(MaxCnt).isZero())
190 return true;
191 break;
192 }
193 }
194
195 // Pass through this frame's Depth (not Depth+1) because we have not recursed
196 // into a child MI here: the fallback queries KnownBits for the same R.
197 return getKnownBits(R, DemandedElts, Depth).isNonZero();
198}
199
203
207
208[[maybe_unused]] static void
209dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) {
210 dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth
211 << "] Computed for: " << MI << "[" << Depth << "] Known: 0x"
212 << toString(Known.Zero | Known.One, 16, false) << "\n"
213 << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false)
214 << "\n"
215 << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false)
216 << "\n";
217}
218
219/// Compute known bits for the intersection of \p Src0 and \p Src1
220void GISelValueTracking::computeKnownBitsMin(Register Src0, Register Src1,
222 const APInt &DemandedElts,
223 unsigned Depth) {
224 // Test src1 first, since we canonicalize simpler expressions to the RHS.
225 computeKnownBitsImpl(Src1, Known, DemandedElts, Depth);
226
227 // If we don't know any bits, early out.
228 if (Known.isUnknown())
229 return;
230
231 KnownBits Known2;
232 computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
233
234 // Only known if known in both the LHS and RHS.
235 Known = Known.intersectWith(Known2);
236}
237
238// Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
239// created using Width. Use this function when the inputs are KnownBits
240// objects. TODO: Move this KnownBits.h if this is usable in more cases.
241static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown,
242 const KnownBits &OffsetKnown,
243 const KnownBits &WidthKnown) {
244 KnownBits Mask(BitWidth);
245 Mask.Zero = APInt::getBitsSetFrom(
247 Mask.One = APInt::getLowBitsSet(
249 return KnownBits::lshr(SrcOpKnown, OffsetKnown) & Mask;
250}
251
252void GISelValueTracking::computeKnownBits(Register R, KnownBits &Known,
253 const APInt &DemandedElts,
254 unsigned Depth) {
255 MachineInstr &MI = *MRI.getVRegDef(R);
256 unsigned Opcode = MI.getOpcode();
257 LLT DstTy = MRI.getType(R);
258
259 // Handle the case where this is called on a register that does not have a
260 // type constraint. For example, it may be post-ISel or this target might not
261 // preserve the type when early-selecting instructions.
262 if (!DstTy.isValid()) {
263 Known = KnownBits();
264 return;
265 }
266
267#ifndef NDEBUG
268 if (DstTy.isFixedVector()) {
269 assert(
270 DstTy.getNumElements() == DemandedElts.getBitWidth() &&
271 "DemandedElt width should equal the fixed vector number of elements");
272 } else {
273 assert(DemandedElts.getBitWidth() == 1 && DemandedElts == APInt(1, 1) &&
274 "DemandedElt width should be 1 for scalars or scalable vectors");
275 }
276#endif
277
278 unsigned BitWidth = DstTy.getScalarSizeInBits();
279 Known = KnownBits(BitWidth); // Don't know anything
280
281 // Depth may get bigger than max depth if it gets passed to a different
282 // GISelValueTracking object.
283 // This may happen when say a generic part uses a GISelValueTracking object
284 // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr
285 // which creates a new GISelValueTracking object with a different and smaller
286 // depth. If we just check for equality, we would never exit if the depth
287 // that is passed down to the target specific GISelValueTracking object is
288 // already bigger than its max depth.
289 if (Depth >= getMaxDepth())
290 return;
291
292 if (!DemandedElts)
293 return; // No demanded elts, better to assume we don't know anything.
294
295 KnownBits Known2;
296
297 switch (Opcode) {
298 default:
299 TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI,
300 Depth);
301 break;
302 case TargetOpcode::G_BUILD_VECTOR: {
303 // Collect the known bits that are shared by every demanded vector element.
304 Known.Zero.setAllBits();
305 Known.One.setAllBits();
306 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
307 if (!DemandedElts[I])
308 continue;
309
310 computeKnownBitsImpl(MO.getReg(), Known2, APInt(1, 1), Depth + 1);
311
312 // Known bits are the values that are shared by every demanded element.
313 Known = Known.intersectWith(Known2);
314
315 // If we don't know any bits, early out.
316 if (Known.isUnknown())
317 break;
318 }
319 break;
320 }
321 case TargetOpcode::G_SPLAT_VECTOR: {
322 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, APInt(1, 1),
323 Depth + 1);
324 // Implicitly truncate the bits to match the official semantics of
325 // G_SPLAT_VECTOR.
326 Known = Known.trunc(BitWidth);
327 break;
328 }
329 case TargetOpcode::G_FREEZE: {
330 Register Src = MI.getOperand(1).getReg();
331 // freeze of undef/poison is an arbitrary noundef bit pattern, so the known
332 // bits of the source only carry over when it cannot be undef or poison.
333 if (isGuaranteedNotToBeUndefOrPoison(Src, MRI, Depth + 1))
334 computeKnownBitsImpl(Src, Known, DemandedElts, Depth + 1);
335 break;
336 }
337 case TargetOpcode::COPY:
338 case TargetOpcode::G_PHI:
339 case TargetOpcode::PHI: {
342 // Destination registers should not have subregisters at this
343 // point of the pipeline, otherwise the main live-range will be
344 // defined more than once, which is against SSA.
345 assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?");
346 // PHI's operand are a mix of registers and basic blocks interleaved.
347 // We only care about the register ones.
348 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
349 const MachineOperand &Src = MI.getOperand(Idx);
350 Register SrcReg = Src.getReg();
351 LLT SrcTy = MRI.getType(SrcReg);
352 // Look through trivial copies and phis but don't look through trivial
353 // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits
354 // analysis is currently unable to determine the bit width of a
355 // register class.
356 //
357 // We can't use NoSubRegister by name as it's defined by each target but
358 // it's always defined to be 0 by tablegen.
359 if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ &&
360 SrcTy.isValid()) {
361 APInt NowDemandedElts;
362 if (!SrcTy.isFixedVector()) {
363 NowDemandedElts = APInt(1, 1);
364 } else if (DstTy.isFixedVector() &&
365 SrcTy.getNumElements() == DstTy.getNumElements()) {
366 NowDemandedElts = DemandedElts;
367 } else {
368 NowDemandedElts = APInt::getAllOnes(SrcTy.getNumElements());
369 }
370
371 // For COPYs we don't do anything, don't increase the depth.
372 computeKnownBitsImpl(SrcReg, Known2, NowDemandedElts,
373 Depth + (Opcode != TargetOpcode::COPY));
374 Known2 = Known2.anyextOrTrunc(BitWidth);
375 Known = Known.intersectWith(Known2);
376 // If we reach a point where we don't know anything
377 // just stop looking through the operands.
378 if (Known.isUnknown())
379 break;
380 } else {
381 // We know nothing.
382 Known = KnownBits(BitWidth);
383 break;
384 }
385 }
386 break;
387 }
388 case TargetOpcode::G_STEP_VECTOR: {
389 APInt Step = MI.getOperand(1).getCImm()->getValue();
390
391 if (Step.isPowerOf2())
392 Known.Zero.setLowBits(Step.logBase2());
393
395 break;
396
397 const APInt MinNumElts =
398 APInt(BitWidth, DstTy.getElementCount().getKnownMinValue());
400 bool Overflow;
401 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
403 .umul_ov(MinNumElts, Overflow);
404 if (Overflow)
405 break;
406 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
407 if (Overflow)
408 break;
409 Known.Zero.setHighBits(MaxValue.countl_zero());
410 break;
411 }
412 case TargetOpcode::G_VSCALE: {
414 const APInt &Multiplier = MI.getOperand(1).getCImm()->getValue();
416 break;
417 }
418 case TargetOpcode::G_CONSTANT: {
419 Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
420 break;
421 }
422 case TargetOpcode::G_FRAME_INDEX: {
423 int FrameIdx = MI.getOperand(1).getIndex();
424 TL.computeKnownBitsForStackObjectPointer(
425 Known, MF, MF.getFrameInfo().getObjectAlign(FrameIdx));
426 break;
427 }
428 case TargetOpcode::G_SUB: {
429 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
430 Depth + 1);
431 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
432 Depth + 1);
434 MI.getFlag(MachineInstr::NoUWrap));
435 break;
436 }
437 case TargetOpcode::G_XOR: {
438 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
439 Depth + 1);
440 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
441 Depth + 1);
442
443 Known ^= Known2;
444 break;
445 }
446 case TargetOpcode::G_PTR_ADD: {
447 if (DstTy.isVector())
448 break;
449 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets?
450 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
451 if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace()))
452 break;
453 [[fallthrough]];
454 }
455 case TargetOpcode::G_ADD: {
456 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
457 Depth + 1);
458 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
459 Depth + 1);
460 Known = KnownBits::add(Known, Known2);
461 break;
462 }
463 case TargetOpcode::G_AND: {
464 // If either the LHS or the RHS are Zero, the result is zero.
465 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
466 Depth + 1);
467 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
468 Depth + 1);
469
470 Known &= Known2;
471 break;
472 }
473 case TargetOpcode::G_OR: {
474 // If either the LHS or the RHS are Zero, the result is zero.
475 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
476 Depth + 1);
477 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
478 Depth + 1);
479
480 Known |= Known2;
481 break;
482 }
483 case TargetOpcode::G_MUL: {
484 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
485 Depth + 1);
486 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
487 Depth + 1);
488 Known = KnownBits::mul(Known, Known2);
489 break;
490 }
491 case TargetOpcode::G_UMULH: {
492 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
493 Depth + 1);
494 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
495 Depth + 1);
496 Known = KnownBits::mulhu(Known, Known2);
497 break;
498 }
499 case TargetOpcode::G_SMULH: {
500 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
501 Depth + 1);
502 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
503 Depth + 1);
504 Known = KnownBits::mulhs(Known, Known2);
505 break;
506 }
507 case TargetOpcode::G_CLMUL: {
508 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
509 Depth + 1);
510 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
511 Depth + 1);
512 Known = KnownBits::clmul(Known, Known2);
513 break;
514 }
515 case TargetOpcode::G_UAVGFLOOR: {
516 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
517 Depth + 1);
518 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
519 Depth + 1);
521 break;
522 }
523 case TargetOpcode::G_UAVGCEIL: {
524 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
525 Depth + 1);
526 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
527 Depth + 1);
529 break;
530 }
531 case TargetOpcode::G_SAVGFLOOR: {
532 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
533 Depth + 1);
534 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
535 Depth + 1);
537 break;
538 }
539 case TargetOpcode::G_SAVGCEIL: {
540 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
541 Depth + 1);
542 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
543 Depth + 1);
545 break;
546 }
547 case TargetOpcode::G_ABDU: {
548 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
549 Depth + 1);
550 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
551 Depth + 1);
552 Known = KnownBits::abdu(Known, Known2);
553 break;
554 }
555 case TargetOpcode::G_ABDS: {
556 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
557 Depth + 1);
558 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
559 Depth + 1);
560 Known = KnownBits::abds(Known, Known2);
561
562 unsigned SignBits1 =
563 computeNumSignBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
564 if (SignBits1 == 1) {
565 break;
566 }
567 unsigned SignBits0 =
568 computeNumSignBits(MI.getOperand(1).getReg(), DemandedElts, Depth + 1);
569
570 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
571 break;
572 }
573 case TargetOpcode::G_SADDSAT: {
574 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
575 Depth + 1);
576 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
577 Depth + 1);
579 break;
580 }
581 case TargetOpcode::G_UADDSAT: {
582 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
583 Depth + 1);
584 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
585 Depth + 1);
587 break;
588 }
589 case TargetOpcode::G_SSUBSAT: {
590 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
591 Depth + 1);
592 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
593 Depth + 1);
595 break;
596 }
597 case TargetOpcode::G_USUBSAT: {
598 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
599 Depth + 1);
600 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
601 Depth + 1);
603 break;
604 }
605 case TargetOpcode::G_UDIV: {
606 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
607 Depth + 1);
608 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
609 Depth + 1);
610 Known = KnownBits::udiv(Known, Known2,
612 break;
613 }
614 case TargetOpcode::G_SDIV: {
615 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
616 Depth + 1);
617 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
618 Depth + 1);
619 Known = KnownBits::sdiv(Known, Known2,
621 break;
622 }
623 case TargetOpcode::G_UREM: {
624 KnownBits LHSKnown(Known.getBitWidth());
625 KnownBits RHSKnown(Known.getBitWidth());
626
627 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
628 Depth + 1);
629 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
630 Depth + 1);
631
632 Known = KnownBits::urem(LHSKnown, RHSKnown);
633 break;
634 }
635 case TargetOpcode::G_SREM: {
636 KnownBits LHSKnown(Known.getBitWidth());
637 KnownBits RHSKnown(Known.getBitWidth());
638
639 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
640 Depth + 1);
641 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
642 Depth + 1);
643
644 Known = KnownBits::srem(LHSKnown, RHSKnown);
645 break;
646 }
647 case TargetOpcode::G_SELECT: {
648 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
649 Known, DemandedElts, Depth + 1);
650 break;
651 }
652 case TargetOpcode::G_SMIN: {
653 // TODO: Handle clamp pattern with number of sign bits
654 KnownBits KnownRHS;
655 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
656 Depth + 1);
657 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
658 Depth + 1);
659 Known = KnownBits::smin(Known, KnownRHS);
660 break;
661 }
662 case TargetOpcode::G_SMAX: {
663 // TODO: Handle clamp pattern with number of sign bits
664 KnownBits KnownRHS;
665 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
666 Depth + 1);
667 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
668 Depth + 1);
669 Known = KnownBits::smax(Known, KnownRHS);
670 break;
671 }
672 case TargetOpcode::G_UMIN: {
673 KnownBits KnownRHS;
674 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
675 Depth + 1);
676 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
677 Depth + 1);
678 Known = KnownBits::umin(Known, KnownRHS);
679 break;
680 }
681 case TargetOpcode::G_UMAX: {
682 KnownBits KnownRHS;
683 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
684 Depth + 1);
685 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
686 Depth + 1);
687 Known = KnownBits::umax(Known, KnownRHS);
688 break;
689 }
690 case TargetOpcode::G_FCMP:
691 case TargetOpcode::G_ICMP: {
692 if (DstTy.isVector())
693 break;
694 if (TL.getBooleanContents(DstTy.isVector(),
695 Opcode == TargetOpcode::G_FCMP) ==
697 BitWidth > 1)
698 Known.Zero.setBitsFrom(1);
699 break;
700 }
701 case TargetOpcode::G_SEXT: {
702 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
703 Depth + 1);
704 // If the sign bit is known to be zero or one, then sext will extend
705 // it to the top bits, else it will just zext.
706 Known = Known.sext(BitWidth);
707 break;
708 }
709 case TargetOpcode::G_ASSERT_SEXT:
710 case TargetOpcode::G_SEXT_INREG: {
711 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
712 Depth + 1);
713 Known = Known.sextInReg(MI.getOperand(2).getImm());
714 break;
715 }
716 case TargetOpcode::G_ANYEXT: {
717 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
718 Depth + 1);
719 Known = Known.anyext(BitWidth);
720 break;
721 }
722 case TargetOpcode::G_LOAD: {
723 const MachineMemOperand *MMO = *MI.memoperands_begin();
724 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
725 if (const MDNode *Ranges = MMO->getRanges())
726 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
727 Known = KnownRange.anyext(Known.getBitWidth());
728 break;
729 }
730 case TargetOpcode::G_SEXTLOAD:
731 case TargetOpcode::G_ZEXTLOAD: {
732 if (DstTy.isVector())
733 break;
734 const MachineMemOperand *MMO = *MI.memoperands_begin();
735 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
736 if (const MDNode *Ranges = MMO->getRanges())
737 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
738 Known = Opcode == TargetOpcode::G_SEXTLOAD
739 ? KnownRange.sext(Known.getBitWidth())
740 : KnownRange.zext(Known.getBitWidth());
741 break;
742 }
743 case TargetOpcode::G_ASHR: {
744 KnownBits LHSKnown, RHSKnown;
745 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
746 Depth + 1);
747 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
748 Depth + 1);
749 Known = KnownBits::ashr(LHSKnown, RHSKnown);
750 break;
751 }
752 case TargetOpcode::G_LSHR: {
753 KnownBits LHSKnown, RHSKnown;
754 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
755 Depth + 1);
756 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
757 Depth + 1);
758 Known = KnownBits::lshr(LHSKnown, RHSKnown);
759 break;
760 }
761 case TargetOpcode::G_SHL: {
762 KnownBits LHSKnown, RHSKnown;
763 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
764 Depth + 1);
765 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
766 Depth + 1);
767 Known = KnownBits::shl(LHSKnown, RHSKnown);
768 break;
769 }
770 case TargetOpcode::G_ROTL:
771 case TargetOpcode::G_ROTR: {
772 auto MaybeAmtOp =
773 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
774 if (!MaybeAmtOp)
775 break;
776
777 Register SrcReg = MI.getOperand(1).getReg();
778 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
779
780 unsigned Amt = MaybeAmtOp->urem(BitWidth);
781
782 // Canonicalize to ROTR.
783 if (Opcode == TargetOpcode::G_ROTL)
784 Amt = BitWidth - Amt;
785
786 Known.Zero = Known.Zero.rotr(Amt);
787 Known.One = Known.One.rotr(Amt);
788 break;
789 }
790 case TargetOpcode::G_FSHL:
791 case TargetOpcode::G_FSHR: {
792 auto MaybeAmtOp =
793 isConstantOrConstantSplatVector(MI.getOperand(3).getReg(), MRI);
794 if (!MaybeAmtOp)
795 break;
796
797 const APInt Amt = *MaybeAmtOp;
798 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
799 Depth + 1);
800 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
801 Depth + 1);
802 Known = Opcode == TargetOpcode::G_FSHL
803 ? KnownBits::fshl(Known, Known2, Amt)
804 : KnownBits::fshr(Known, Known2, Amt);
805 break;
806 }
807 case TargetOpcode::G_INTTOPTR:
808 case TargetOpcode::G_PTRTOINT:
809 if (DstTy.isVector())
810 break;
811 // Fall through and handle them the same as zext/trunc.
812 [[fallthrough]];
813 case TargetOpcode::G_ZEXT:
814 case TargetOpcode::G_TRUNC: {
815 Register SrcReg = MI.getOperand(1).getReg();
816 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
817 Known = Known.zextOrTrunc(BitWidth);
818 break;
819 }
820 case TargetOpcode::G_TRUNC_SSAT_S: {
821 Register SrcReg = MI.getOperand(1).getReg();
822 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
823 Known = Known.truncSSat(BitWidth);
824 break;
825 }
826 case TargetOpcode::G_TRUNC_SSAT_U: {
827 Register SrcReg = MI.getOperand(1).getReg();
828 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
829 Known = Known.truncSSatU(BitWidth);
830 break;
831 }
832 case TargetOpcode::G_TRUNC_USAT_U: {
833 Register SrcReg = MI.getOperand(1).getReg();
834 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
835 Known = Known.truncUSat(BitWidth);
836 break;
837 }
838 case TargetOpcode::G_ASSERT_ZEXT: {
839 Register SrcReg = MI.getOperand(1).getReg();
840 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
841
842 unsigned SrcBitWidth = MI.getOperand(2).getImm();
843 assert(SrcBitWidth && "SrcBitWidth can't be zero");
844 APInt InMask = APInt::getLowBitsSet(BitWidth, SrcBitWidth);
845 Known.Zero |= (~InMask);
846 Known.One &= (~Known.Zero);
847 break;
848 }
849 case TargetOpcode::G_ASSERT_ALIGN: {
850 int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
851
852 // TODO: Should use maximum with source
853 // If a node is guaranteed to be aligned, set low zero bits accordingly as
854 // well as clearing one bits.
855 Known.Zero.setLowBits(LogOfAlign);
856 Known.One.clearLowBits(LogOfAlign);
857 break;
858 }
859 case TargetOpcode::G_MERGE_VALUES: {
860 unsigned NumOps = MI.getNumOperands();
861 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
862
863 for (unsigned I = 0; I != NumOps - 1; ++I) {
864 KnownBits SrcOpKnown;
865 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
866 DemandedElts, Depth + 1);
867 Known.insertBits(SrcOpKnown, I * OpSize);
868 }
869 break;
870 }
871 case TargetOpcode::G_UNMERGE_VALUES: {
872 unsigned NumOps = MI.getNumOperands();
873 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
874 LLT SrcTy = MRI.getType(SrcReg);
875
876 if (SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType())
877 return; // TODO: Handle vector->subelement unmerges
878
879 // Figure out the result operand index
880 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
881
882 APInt SubDemandedElts = DemandedElts;
883 if (SrcTy.isVector()) {
884 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
885 SubDemandedElts =
886 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
887 }
888
889 KnownBits SrcOpKnown;
890 computeKnownBitsImpl(SrcReg, SrcOpKnown, SubDemandedElts, Depth + 1);
891
892 if (SrcTy.isVector())
893 Known = std::move(SrcOpKnown);
894 else
895 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
896 break;
897 }
898 case TargetOpcode::G_BSWAP: {
899 Register SrcReg = MI.getOperand(1).getReg();
900 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
901 Known = Known.byteSwap();
902 break;
903 }
904 case TargetOpcode::G_BITREVERSE: {
905 Register SrcReg = MI.getOperand(1).getReg();
906 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
907 Known = Known.reverseBits();
908 break;
909 }
910 case TargetOpcode::G_CTPOP: {
911 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
912 Depth + 1);
913 // We can bound the space the count needs. Also, bits known to be zero
914 // can't contribute to the population.
915 unsigned BitsPossiblySet = Known2.countMaxPopulation();
916 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
917 Known.Zero.setBitsFrom(LowBits);
918 // TODO: we could bound Known.One using the lower bound on the number of
919 // bits which might be set provided by popcnt KnownOne2.
920 break;
921 }
922 case TargetOpcode::G_UBFX: {
923 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
924 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
925 Depth + 1);
926 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
927 Depth + 1);
928 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
929 Depth + 1);
930 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
931 break;
932 }
933 case TargetOpcode::G_SBFX: {
934 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
935 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
936 Depth + 1);
937 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
938 Depth + 1);
939 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
940 Depth + 1);
941 // The offset and width are unsigned and can have a different type than the
942 // source. extractBits reads both as unsigned values of any width. The
943 // width is also used below in a subtraction of BitWidth type. A valid width
944 // is at most BitWidth, so it fits in BitWidth bits.
945 WidthKnown = WidthKnown.zextOrTrunc(BitWidth);
946 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
947 // Sign extend the extracted value using shift left and arithmetic shift
948 // right.
949 KnownBits ExtKnown = KnownBits::makeConstant(APInt(BitWidth, BitWidth));
950 KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
951 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
952 break;
953 }
954 case TargetOpcode::G_UADDO:
955 case TargetOpcode::G_UADDE:
956 case TargetOpcode::G_SADDO:
957 case TargetOpcode::G_SADDE: {
958 if (MI.getOperand(1).getReg() == R) {
959 // If we know the result of a compare has the top bits zero, use this
960 // info.
961 if (TL.getBooleanContents(DstTy.isVector(), false) ==
963 BitWidth > 1)
964 Known.Zero.setBitsFrom(1);
965 break;
966 }
967
968 assert(MI.getOperand(0).getReg() == R &&
969 "We only compute knownbits for the sum here.");
970 // With [US]ADDE, a carry bit may be added in.
971 KnownBits Carry(1);
972 if (Opcode == TargetOpcode::G_UADDE || Opcode == TargetOpcode::G_SADDE) {
973 computeKnownBitsImpl(MI.getOperand(4).getReg(), Carry, DemandedElts,
974 Depth + 1);
975 // Carry has bit width 1
976 Carry = Carry.trunc(1);
977 } else {
978 Carry.setAllZero();
979 }
980
981 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
982 Depth + 1);
983 computeKnownBitsImpl(MI.getOperand(3).getReg(), Known2, DemandedElts,
984 Depth + 1);
985 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
986 break;
987 }
988 case TargetOpcode::G_USUBO:
989 case TargetOpcode::G_USUBE:
990 case TargetOpcode::G_SSUBO:
991 case TargetOpcode::G_SSUBE:
992 case TargetOpcode::G_UMULO:
993 case TargetOpcode::G_SMULO: {
994 if (MI.getOperand(1).getReg() == R) {
995 // If we know the result of a compare has the top bits zero, use this
996 // info.
997 if (TL.getBooleanContents(DstTy.isVector(), false) ==
999 BitWidth > 1)
1000 Known.Zero.setBitsFrom(1);
1001 }
1002 break;
1003 }
1004 case TargetOpcode::G_CTTZ:
1005 case TargetOpcode::G_CTTZ_ZERO_POISON: {
1006 KnownBits SrcOpKnown;
1007 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
1008 Depth + 1);
1009 // If we have a known 1, its position is our upper bound
1010 unsigned PossibleTZ = SrcOpKnown.countMaxTrailingZeros();
1011 unsigned LowBits = llvm::bit_width(PossibleTZ);
1012 Known.Zero.setBitsFrom(LowBits);
1013 break;
1014 }
1015 case TargetOpcode::G_CTLZ:
1016 case TargetOpcode::G_CTLZ_ZERO_POISON: {
1017 KnownBits SrcOpKnown;
1018 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
1019 Depth + 1);
1020 Known = SrcOpKnown.ctlz(/*ZeroIsPoison=*/false).zextOrTrunc(BitWidth);
1021 break;
1022 }
1023 case TargetOpcode::G_CTLS: {
1024 Register Reg = MI.getOperand(1).getReg();
1025 unsigned MinRedundantSignBits =
1026 computeNumSignBits(Reg, DemandedElts, Depth + 1) - 1;
1027
1028 unsigned MaxUpperRedundantSignBits = MRI.getType(Reg).getScalarSizeInBits();
1029
1030 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
1031 APInt(BitWidth, MaxUpperRedundantSignBits));
1032
1034 break;
1035 }
1036 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1037 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1038 Register InVec = Extract.getVectorReg();
1039 Register EltNo = Extract.getIndexReg();
1040
1041 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1042
1043 LLT VecVT = MRI.getType(InVec);
1044 // computeKnownBits not yet implemented for scalable vectors.
1045 if (VecVT.isScalableVector())
1046 break;
1047
1048 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
1049 const unsigned NumSrcElts = VecVT.getNumElements();
1050 // A return type different from the vector's element type may lead to
1051 // issues with pattern selection. Bail out to avoid that.
1052 if (BitWidth > EltBitWidth)
1053 break;
1054
1055 Known.Zero.setAllBits();
1056 Known.One.setAllBits();
1057
1058 // If we know the element index, just demand that vector element, else for
1059 // an unknown element index, ignore DemandedElts and demand them all.
1060 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
1061 if (ConstEltNo && ConstEltNo->ult(NumSrcElts))
1062 DemandedSrcElts =
1063 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
1064
1065 computeKnownBitsImpl(InVec, Known, DemandedSrcElts, Depth + 1);
1066 break;
1067 }
1068 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1069 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
1070 Register InVec = Insert.getVectorReg();
1071 Register InVal = Insert.getElementReg();
1072 Register EltNo = Insert.getIndexReg();
1073 LLT VecVT = MRI.getType(InVec);
1074
1075 if (VecVT.isScalableVector())
1076 break;
1077
1078 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1079 unsigned NumElts = VecVT.getNumElements();
1080
1081 bool DemandedVal = true;
1082 APInt DemandedVecElts = DemandedElts;
1083 if (ConstEltNo && ConstEltNo->ult(NumElts)) {
1084 unsigned EltIdx = ConstEltNo->getZExtValue();
1085 DemandedVal = !!DemandedElts[EltIdx];
1086 DemandedVecElts.clearBit(EltIdx);
1087 }
1088 Known.setAllConflict();
1089 if (DemandedVal) {
1090 computeKnownBitsImpl(InVal, Known2, APInt(1, 1), Depth + 1);
1091 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
1092 }
1093 if (!!DemandedVecElts) {
1094 computeKnownBitsImpl(InVec, Known2, DemandedVecElts, Depth + 1);
1095 Known = Known.intersectWith(Known2);
1096 }
1097 break;
1098 }
1099 case TargetOpcode::G_INSERT_SUBVECTOR: {
1100 GInsertSubvector &Insert = cast<GInsertSubvector>(MI);
1101 Register Src = Insert.getBigVec();
1102 Register Sub = Insert.getSubVec();
1103 uint64_t Idx = Insert.getIndexImm();
1104 LLT SrcTy = MRI.getType(Src);
1105 LLT SubTy = MRI.getType(Sub);
1106 APInt DemandedSubElts;
1107 APInt DemandedSrcElts;
1108
1109 if (SrcTy.isScalableVector()) {
1110 DemandedSubElts = SubTy.isScalableVector()
1111 ? APInt(1, 1)
1112 : APInt::getAllOnes(SubTy.getNumElements());
1113 DemandedSrcElts = APInt(1, 1);
1114 } else {
1115 unsigned NumSubElts = SubTy.getNumElements();
1116 DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
1117 DemandedSrcElts = DemandedElts;
1118 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
1119 }
1120
1121 Known.setAllConflict();
1122 if (!!DemandedSubElts) {
1123 computeKnownBitsImpl(Sub, Known2, DemandedSubElts, Depth + 1);
1124 Known = Known.intersectWith(Known2);
1125 if (Known.isUnknown())
1126 break;
1127 }
1128
1129 if (!!DemandedSrcElts) {
1130 computeKnownBitsImpl(Src, Known2, DemandedSrcElts, Depth + 1);
1131 Known = Known.intersectWith(Known2);
1132 }
1133
1134 break;
1135 }
1136 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
1137 Register SrcReg = MI.getOperand(1).getReg();
1138 LLT SrcTy = MRI.getType(SrcReg);
1139 APInt DemandedSrcElts;
1140 if (SrcTy.isScalableVector()) {
1141 DemandedSrcElts = APInt(1, 1);
1142 } else {
1143 uint64_t Idx = MI.getOperand(2).getImm();
1144 unsigned NumSrcElts = SrcTy.getNumElements();
1145 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
1146 }
1147 computeKnownBitsImpl(SrcReg, Known, DemandedSrcElts, Depth + 1);
1148 break;
1149 }
1150 case TargetOpcode::G_SHUFFLE_VECTOR: {
1151 APInt DemandedLHS, DemandedRHS;
1152 // Collect the known bits that are shared by every vector element referenced
1153 // by the shuffle.
1154 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1155 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
1156 DemandedElts, DemandedLHS, DemandedRHS))
1157 break;
1158
1159 // Known bits are the values that are shared by every demanded element.
1160 Known.Zero.setAllBits();
1161 Known.One.setAllBits();
1162 if (!!DemandedLHS) {
1163 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
1164 Depth + 1);
1165 Known = Known.intersectWith(Known2);
1166 }
1167 // If we don't know any bits, early out.
1168 if (Known.isUnknown())
1169 break;
1170 if (!!DemandedRHS) {
1171 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
1172 Depth + 1);
1173 Known = Known.intersectWith(Known2);
1174 }
1175 break;
1176 }
1177 case TargetOpcode::G_CONCAT_VECTORS: {
1178 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
1179 break;
1180 // Split DemandedElts and test each of the demanded subvectors.
1181 Known.Zero.setAllBits();
1182 Known.One.setAllBits();
1183 unsigned NumSubVectorElts =
1184 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1185
1186 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
1187 APInt DemandedSub =
1188 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
1189 if (!!DemandedSub) {
1190 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
1191
1192 Known = Known.intersectWith(Known2);
1193 }
1194 // If we don't know any bits, early out.
1195 if (Known.isUnknown())
1196 break;
1197 }
1198 break;
1199 }
1200 case TargetOpcode::G_VECTOR_COMPRESS: {
1201 // Each result lane is either a lane of the source vector or the passthru,
1202 // so the known bits are those shared by both.
1203 Register Vec = MI.getOperand(1).getReg();
1204 Register PassThru = MI.getOperand(3).getReg();
1205 computeKnownBitsImpl(PassThru, Known, DemandedElts, Depth + 1);
1206 // If we don't know any bits, early out.
1207 if (Known.isUnknown())
1208 break;
1209 // Compression can move any source lane to any result position, so all
1210 // source lanes are demanded.
1211 APInt DemandedSrcElts = APInt::getAllOnes(DemandedElts.getBitWidth());
1212 computeKnownBitsImpl(Vec, Known2, DemandedSrcElts, Depth + 1);
1213 Known = Known.intersectWith(Known2);
1214 break;
1215 }
1216 case TargetOpcode::G_ABS: {
1217 Register SrcReg = MI.getOperand(1).getReg();
1218 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
1219 Known = Known.abs();
1220 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
1221 1);
1222 break;
1223 }
1224 }
1225
1227}
1228
1230 KnownBits &Known) {
1231 LLT Ty = MRI.getType(Reg);
1232 if (!Ty.isValid()) {
1233 Known = KnownBits();
1234 return;
1235 }
1236 unsigned BitWidth = Ty.getScalarSizeInBits();
1238}
1239
1240/// Evaluate a known-bits query with an explicit worklist instead of recursive
1241/// descent.
1243 const APInt &DemandedElts,
1244 unsigned Depth) {
1245 // Nested queries only consult the per-query cache. If the result is not
1246 // available yet, enqueue the request and return an unknown placeholder.
1247 if (!Stack.empty()) {
1248 if (!getKnownBitsResult(R, DemandedElts, Depth, Known)) {
1249 Stack.push_back({R, DemandedElts, Depth});
1250 genUnknown(MRI, R, Known);
1251 }
1252 return;
1253 }
1254
1255 // Top-level queries drive evaluation iteratively until every queued item has
1256 // either been computed or found in the cache.
1257 Stack.push_back({R, DemandedElts, Depth});
1258 while (!Stack.empty()) {
1259 WorkItem Item = Stack.back();
1260 size_t StackSize = Stack.size();
1261 Register ItemReg = std::get<0>(Item);
1262 const APInt &ItemDemandedElts = std::get<1>(Item);
1263 const unsigned ItemDepth = std::get<2>(Item);
1264 KnownBits ItemKnown;
1265
1266 if (getKnownBitsResult(ItemReg, ItemDemandedElts, ItemDepth, ItemKnown)) {
1267 Stack.pop_back();
1268 continue;
1269 }
1270
1271 // Evaluate this item with the per-instruction known-bits logic. Dependent
1272 // queries issued from there re-enter this worklist driver and take the
1273 // nested-query path to enqueue more work.
1274 computeKnownBits(ItemReg, ItemKnown, ItemDemandedElts, ItemDepth);
1275
1276 // If evaluating this item did not queue more work, its dependencies are
1277 // resolved and the result can be memoized immediately.
1278 if (Stack.size() == StackSize) {
1279 assert((std::get<0>(Stack.back()) == ItemReg &&
1280 std::get<1>(Stack.back()) == ItemDemandedElts &&
1281 std::get<2>(Stack.back()) == ItemDepth) &&
1282 "The item we just evaluated must still be the top one.");
1283
1284 setKnownBitsResult(ItemReg, ItemDemandedElts, ItemDepth, ItemKnown);
1285 Stack.pop_back();
1286 }
1287 }
1288
1289 // The original query must have been computed by the time the worklist is
1290 // drained.
1291 if (!getKnownBitsResult(R, DemandedElts, Depth, Known))
1293 "Top level query must be in `results` after iteration is complete.");
1294
1295 Results.clear();
1296}
1297
1298void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
1299 FPClassTest InterestedClasses,
1300 unsigned Depth) {
1301 LLT Ty = MRI.getType(R);
1302 APInt DemandedElts =
1303 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1304 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
1305}
1306
1307/// Return true if this value is known to be the fractional part x - floor(x),
1308/// which lies in [0, 1). This implies the value cannot introduce overflow in a
1309/// fmul when the other operand is known finite.
1311 using namespace MIPatternMatch;
1312 Register SubX;
1313 return mi_match(R, MRI, m_GFSub(m_Reg(SubX), m_GFFloor(m_DeferredReg(SubX))));
1314}
1315
1316void GISelValueTracking::computeKnownFPClassForFPTrunc(
1317 const MachineInstr &MI, const APInt &DemandedElts,
1318 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
1319 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
1320 fcNone)
1321 return;
1322
1323 Register Val = MI.getOperand(1).getReg();
1324 KnownFPClass KnownSrc;
1325 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1326 Depth + 1);
1327
1328 const fltSemantics &DstSem = getFltSemanticForLLT(
1329 MRI.getType(MI.getOperand(0).getReg()).getScalarType());
1330 const fltSemantics &SrcSem =
1331 getFltSemanticForLLT(MRI.getType(Val).getScalarType());
1332
1333 const RoundingMode RM =
1334 MI.getOpcode() == TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND
1335 ? static_cast<RoundingMode>(MI.getOperand(2).getImm())
1337
1338 Known = KnownFPClass::fptrunc(KnownSrc, DstSem, SrcSem, RM,
1339 MF.getDenormalMode(DstSem),
1340 MF.getDenormalMode(SrcSem));
1341}
1342
1343void GISelValueTracking::computeKnownFPClass(Register R,
1344 const APInt &DemandedElts,
1345 FPClassTest InterestedClasses,
1347 unsigned Depth) {
1348 assert(Known.isUnknown() && "should not be called with known information");
1349
1350 if (!DemandedElts) {
1351 // No demanded elts, better to assume we don't know anything.
1352 Known.resetAll();
1353 return;
1354 }
1355
1356 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1357
1358 MachineInstr &MI = *MRI.getVRegDef(R);
1359 unsigned Opcode = MI.getOpcode();
1360 LLT DstTy = MRI.getType(R);
1361
1362 if (!DstTy.isValid()) {
1363 Known.resetAll();
1364 return;
1365 }
1366
1367 if (auto Cst = GFConstant::getConstant(R, MRI)) {
1368 switch (Cst->getKind()) {
1370 Known = KnownFPClass(Cst->getScalarValue());
1371 break;
1372 }
1375
1376 for (const APFloat &C : *Cst)
1377 Known |= KnownFPClass(C);
1378
1379 break;
1380 }
1382 Known.resetAll();
1383 break;
1384 }
1385 }
1386
1387 return;
1388 }
1389
1390 FPClassTest KnownNotFromFlags = fcNone;
1392 KnownNotFromFlags |= fcNan;
1394 KnownNotFromFlags |= fcInf;
1395
1396 // We no longer need to find out about these bits from inputs if we can
1397 // assume this from flags/attributes.
1398 InterestedClasses &= ~KnownNotFromFlags;
1399
1400 llvm::scope_exit ClearClassesFromFlags(
1401 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1402
1403 // All recursive calls that increase depth must come after this.
1405 return;
1406
1407 const MachineFunction *MF = MI.getMF();
1408
1409 switch (Opcode) {
1410 default:
1411 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1412 Depth);
1413 break;
1414 case TargetOpcode::G_FNEG: {
1415 Register Val = MI.getOperand(1).getReg();
1416 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1417 Known.fneg();
1418 break;
1419 }
1420 case TargetOpcode::G_SELECT: {
1421 GSelect &SelMI = cast<GSelect>(MI);
1422 Register Cond = SelMI.getCondReg();
1423 Register LHS = SelMI.getTrueReg();
1424 Register RHS = SelMI.getFalseReg();
1425
1426 FPClassTest FilterLHS = fcAllFlags;
1427 FPClassTest FilterRHS = fcAllFlags;
1428
1429 Register TestedValue;
1430 FPClassTest MaskIfTrue = fcAllFlags;
1431 FPClassTest MaskIfFalse = fcAllFlags;
1432 FPClassTest ClassVal = fcNone;
1433
1435 Register CmpLHS, CmpRHS;
1436 if (mi_match(Cond, MRI,
1437 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1438 // If the select filters out a value based on the class, it no longer
1439 // participates in the class of the result
1440
1441 // TODO: In some degenerate cases we can infer something if we try again
1442 // without looking through sign operations.
1443 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1444 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1445 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1446 } else if (mi_match(
1447 Cond, MRI,
1448 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1449 FPClassTest TestedMask = ClassVal;
1450 MaskIfTrue = TestedMask;
1451 MaskIfFalse = ~TestedMask;
1452 }
1453
1454 if (TestedValue == LHS) {
1455 // match !isnan(x) ? x : y
1456 FilterLHS = MaskIfTrue;
1457 } else if (TestedValue == RHS) { // && IsExactClass
1458 // match !isnan(x) ? y : x
1459 FilterRHS = MaskIfFalse;
1460 }
1461
1462 KnownFPClass Known2;
1463 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1464 Depth + 1);
1465 Known.setKnownFPClasses(Known.getKnownFPClasses() & FilterLHS);
1466
1467 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1468 Known2, Depth + 1);
1469 Known2.setKnownFPClasses(Known2.getKnownFPClasses() & FilterRHS);
1470
1471 Known |= Known2;
1472 break;
1473 }
1474 case TargetOpcode::G_FCOPYSIGN: {
1475 Register Magnitude = MI.getOperand(1).getReg();
1476 Register Sign = MI.getOperand(2).getReg();
1477
1478 KnownFPClass KnownSign;
1479
1480 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1481 Depth + 1);
1482 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1483 Depth + 1);
1484 Known.copysign(KnownSign);
1485 break;
1486 }
1487 case TargetOpcode::G_FMA:
1488 case TargetOpcode::G_FMAD: {
1489 if ((InterestedClasses & (fcNegative | fcNan)) == fcNone)
1490 break;
1491
1492 const bool IsFMA = Opcode == TargetOpcode::G_FMA;
1493
1494 Register A = MI.getOperand(1).getReg();
1495 Register B = MI.getOperand(2).getReg();
1496 Register C = MI.getOperand(3).getReg();
1497
1498 DenormalMode Mode =
1499 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1500
1501 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1502 FPClassTest InterestedSquared = InterestedClasses;
1503 FPClassTest InterestedAddend = InterestedClasses;
1504 // Only fma(+inf, +inf, -inf) and fma(-inf, -inf, -inf) produce NaN when
1505 // squaring.
1506 if (InterestedClasses & fcQNan) {
1507 InterestedSquared |= fcSNan | fcInf;
1508 InterestedAddend |= fcSNan | fcNegInf;
1509 }
1510
1511 KnownFPClass KnownSrc, KnownAddend;
1512 computeKnownFPClass(C, DemandedElts, InterestedAddend, KnownAddend,
1513 Depth + 1);
1514 computeKnownFPClass(A, DemandedElts, InterestedSquared, KnownSrc,
1515 Depth + 1);
1516 if (KnownNotFromFlags) {
1517 KnownSrc.knownNot(KnownNotFromFlags);
1518 KnownAddend.knownNot(KnownNotFromFlags);
1519 }
1520 Known = IsFMA ? KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode)
1521 : KnownFPClass::fmuladd_square(KnownSrc, KnownAddend, Mode);
1522 } else {
1523 KnownFPClass KnownSrc[3];
1524 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1525 Depth + 1);
1526 if (KnownSrc[0].isUnknown())
1527 break;
1528 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1529 Depth + 1);
1530 if (KnownSrc[1].isUnknown())
1531 break;
1532 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1533 Depth + 1);
1534 if (KnownSrc[2].isUnknown())
1535 break;
1536 if (KnownNotFromFlags) {
1537 KnownSrc[0].knownNot(KnownNotFromFlags);
1538 KnownSrc[1].knownNot(KnownNotFromFlags);
1539 KnownSrc[2].knownNot(KnownNotFromFlags);
1540 }
1541 Known =
1542 IsFMA ? KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode)
1543 : KnownFPClass::fmuladd(KnownSrc[0], KnownSrc[1], KnownSrc[2],
1544 Mode);
1545 }
1546 break;
1547 }
1548 case TargetOpcode::G_FSQRT:
1549 case TargetOpcode::G_STRICT_FSQRT: {
1550 KnownFPClass KnownSrc;
1551 FPClassTest InterestedSrcs = InterestedClasses;
1552 if (InterestedClasses & fcQNan)
1554 // Subnormals may flush to zero.
1555 if (InterestedClasses & fcZero)
1556 InterestedSrcs |= fcSubnormal;
1557
1558 Register Val = MI.getOperand(1).getReg();
1559 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1560
1561 DenormalMode Mode =
1562 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1563 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1564 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1565 Known.knownNot(fcNegZero);
1566 break;
1567 }
1568 case TargetOpcode::G_FABS: {
1569 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1570 Register Val = MI.getOperand(1).getReg();
1571 // If we only care about the sign bit we don't need to inspect the
1572 // operand.
1573 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1574 Depth + 1);
1575 }
1576 Known.fabs();
1577 break;
1578 }
1579 case TargetOpcode::G_FATAN2: {
1580 FPClassTest InterestedY = InterestedClasses;
1581 FPClassTest InterestedX = InterestedClasses;
1582
1583 // We can rule out negative values if y cannot have a negative value.
1584 if ((InterestedClasses & fcNegFinite) != fcNone)
1585 InterestedY |= fcNegative;
1586
1587 // We can rule out positive values if y cannot have a positive value.
1588 if ((InterestedClasses & fcPosFinite) != fcNone)
1589 InterestedY |= fcPositive | fcNegSubnormal;
1590
1591 // We can rule out zero and subnormal if x cannot have a positive value.
1592 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
1593 InterestedX |= fcPositive | fcNegSubnormal;
1594
1595 Register Y = MI.getOperand(1).getReg();
1596 Register X = MI.getOperand(2).getReg();
1597 KnownFPClass KnownY, KnownX;
1598 computeKnownFPClass(Y, DemandedElts, InterestedY, KnownY, Depth + 1);
1599 computeKnownFPClass(X, DemandedElts, InterestedX, KnownX, Depth + 1);
1600 DenormalMode Mode =
1601 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1602 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
1603 break;
1604 }
1605 case TargetOpcode::G_FSINH: {
1606 Register Val = MI.getOperand(1).getReg();
1607 KnownFPClass KnownSrc;
1608 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1609 Depth + 1);
1610 Known = KnownFPClass::sinh(KnownSrc);
1611 break;
1612 }
1613 case TargetOpcode::G_FCOSH: {
1614 Register Val = MI.getOperand(1).getReg();
1615 KnownFPClass KnownSrc;
1616 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1617 Depth + 1);
1618 Known = KnownFPClass::cosh(KnownSrc);
1619 break;
1620 }
1621 case TargetOpcode::G_FTANH: {
1622 Register Val = MI.getOperand(1).getReg();
1623 KnownFPClass KnownSrc;
1624 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1625 Depth + 1);
1626 Known = KnownFPClass::tanh(KnownSrc);
1627 break;
1628 }
1629 case TargetOpcode::G_FASIN: {
1630 Register Val = MI.getOperand(1).getReg();
1631 KnownFPClass KnownSrc;
1632 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1633 Depth + 1);
1634 Known = KnownFPClass::asin(KnownSrc);
1635 break;
1636 }
1637 case TargetOpcode::G_FACOS: {
1638 Register Val = MI.getOperand(1).getReg();
1639 KnownFPClass KnownSrc;
1640 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1641 Depth + 1);
1642 Known = KnownFPClass::acos(KnownSrc);
1643 break;
1644 }
1645 case TargetOpcode::G_FATAN: {
1646 Register Val = MI.getOperand(1).getReg();
1647 KnownFPClass KnownSrc;
1648 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1649 Depth + 1);
1650 Known = KnownFPClass::atan(KnownSrc);
1651 break;
1652 }
1653 case TargetOpcode::G_FTAN: {
1654 Register Val = MI.getOperand(1).getReg();
1655 KnownFPClass KnownSrc;
1656 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1657 Depth + 1);
1658 Known = KnownFPClass::tan(KnownSrc);
1659 break;
1660 }
1661 case TargetOpcode::G_FSIN:
1662 case TargetOpcode::G_FCOS: {
1663 // Return NaN on infinite inputs.
1664 Register Val = MI.getOperand(1).getReg();
1665 KnownFPClass KnownSrc;
1666 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1667 Depth + 1);
1668 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1669 : KnownFPClass::sin(KnownSrc);
1670 break;
1671 }
1672 case TargetOpcode::G_FSINCOS: {
1673 // Operand layout: (sin_dst, cos_dst, src)
1674 Register Src = MI.getOperand(2).getReg();
1675 KnownFPClass KnownSrc;
1676 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1677 Depth + 1);
1678 if (R == MI.getOperand(0).getReg())
1679 Known = KnownFPClass::sin(KnownSrc);
1680 else
1681 Known = KnownFPClass::cos(KnownSrc);
1682 break;
1683 }
1684 case TargetOpcode::G_FMAXNUM:
1685 case TargetOpcode::G_FMINNUM:
1686 case TargetOpcode::G_FMINNUM_IEEE:
1687 case TargetOpcode::G_FMAXIMUM:
1688 case TargetOpcode::G_FMINIMUM:
1689 case TargetOpcode::G_FMAXNUM_IEEE:
1690 case TargetOpcode::G_FMAXIMUMNUM:
1691 case TargetOpcode::G_FMINIMUMNUM: {
1692 Register LHS = MI.getOperand(1).getReg();
1693 Register RHS = MI.getOperand(2).getReg();
1694 KnownFPClass KnownLHS, KnownRHS;
1695
1696 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1697 Depth + 1);
1698 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1699 Depth + 1);
1700
1702 switch (Opcode) {
1703 case TargetOpcode::G_FMINIMUM:
1705 break;
1706 case TargetOpcode::G_FMAXIMUM:
1708 break;
1709 case TargetOpcode::G_FMINIMUMNUM:
1711 break;
1712 case TargetOpcode::G_FMAXIMUMNUM:
1714 break;
1715 case TargetOpcode::G_FMINNUM:
1716 case TargetOpcode::G_FMINNUM_IEEE:
1718 break;
1719 case TargetOpcode::G_FMAXNUM:
1720 case TargetOpcode::G_FMAXNUM_IEEE:
1722 break;
1723 default:
1724 llvm_unreachable("unhandled min/max opcode");
1725 }
1726
1727 DenormalMode Mode =
1728 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1729 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1730 break;
1731 }
1732 case TargetOpcode::G_FCANONICALIZE: {
1733 Register Val = MI.getOperand(1).getReg();
1734 KnownFPClass KnownSrc;
1735 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1736 Depth + 1);
1737
1738 LLT Ty = MRI.getType(Val).getScalarType();
1739 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1740 DenormalMode Mode = MF->getDenormalMode(FPType);
1741 Known = KnownFPClass::canonicalize(KnownSrc, FPType, Mode);
1742 break;
1743 }
1744 case TargetOpcode::G_VECREDUCE_FMAX:
1745 case TargetOpcode::G_VECREDUCE_FMIN:
1746 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1747 case TargetOpcode::G_VECREDUCE_FMINIMUM:
1748 case TargetOpcode::G_VECREDUCE_FMAXIMUMNUM:
1749 case TargetOpcode::G_VECREDUCE_FMINIMUMNUM: {
1750 Register Val = MI.getOperand(1).getReg();
1751 // reduce min/max will choose an element from one of the vector elements,
1752 // so we can infer and class information that is common to all elements.
1753
1754 Known =
1755 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1756 // Can only propagate sign if output is never NaN.
1757 if (!Known.isKnownNeverNaN())
1758 Known.setSignBit(std::nullopt);
1759 break;
1760 }
1761 case TargetOpcode::G_FFLOOR:
1762 case TargetOpcode::G_FCEIL:
1763 case TargetOpcode::G_FRINT:
1764 case TargetOpcode::G_FNEARBYINT:
1765 case TargetOpcode::G_INTRINSIC_ROUND:
1766 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1767 case TargetOpcode::G_INTRINSIC_TRUNC: {
1768 Register Val = MI.getOperand(1).getReg();
1769 KnownFPClass KnownSrc;
1770 FPClassTest InterestedSrcs = InterestedClasses;
1771
1772 // Negative round ups towards zero produce negative zero.
1773 if (InterestedSrcs & fcNegFinite)
1774 InterestedSrcs |= fcNegFinite;
1775
1776 // Negative subnormals may flush to positive zero.
1777 if (InterestedSrcs & fcPosFinite)
1778 InterestedSrcs |= fcPosFinite | fcNegSubnormal;
1779
1780 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1781
1782 LLT Ty = MRI.getType(Val).getScalarType();
1783 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1784 DenormalMode Mode = MF->getDenormalMode(FltSem);
1785 const bool IsMultiUnitFPType = &FltSem == &APFloat::PPCDoubleDouble();
1786
1787 switch (Opcode) {
1788 case TargetOpcode::G_INTRINSIC_TRUNC:
1789 Known = KnownFPClass::trunc(KnownSrc, Mode);
1790 break;
1791 case TargetOpcode::G_FFLOOR:
1792 Known = KnownFPClass::floor(KnownSrc, IsMultiUnitFPType, Mode);
1793 break;
1794 case TargetOpcode::G_FCEIL:
1795 Known = KnownFPClass::ceil(KnownSrc, IsMultiUnitFPType, Mode);
1796 break;
1797 default:
1798 Known = KnownFPClass::roundToIntegral(KnownSrc, IsMultiUnitFPType, Mode);
1799 break;
1800 }
1801 break;
1802 }
1803 case TargetOpcode::G_FEXP:
1804 case TargetOpcode::G_FEXP2:
1805 case TargetOpcode::G_FEXP10: {
1806 Register Val = MI.getOperand(1).getReg();
1807 KnownFPClass KnownSrc;
1808 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1809 Depth + 1);
1810 Known = KnownFPClass::exp(KnownSrc);
1811 break;
1812 }
1813 case TargetOpcode::G_FLOG:
1814 case TargetOpcode::G_FLOG2:
1815 case TargetOpcode::G_FLOG10: {
1816 FPClassTest InterestedSrcs = fcNone;
1817
1818 // log(negative) produces NaN.
1819 if ((InterestedClasses & fcNan) != fcNone)
1820 InterestedSrcs |= fcNan | fcNegative;
1821
1822 // log(logical-zero) produces negative infinity.
1823 if ((InterestedClasses & fcNegInf) != fcNone)
1824 InterestedSrcs |= fcZero | fcSubnormal;
1825
1826 // log(x) < -0.0 if x < +1.0
1827 if ((InterestedClasses & fcNegNormal) != fcNone)
1828 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
1829
1830 // log(x) >= +0.0 if x >= +1.0
1831 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
1832 InterestedSrcs |= fcPosNormal;
1833
1834 // log(x) is positive infinity iff x is positive infinity.
1835 if ((InterestedClasses & fcPosInf) != fcNone)
1836 InterestedSrcs |= fcPosInf;
1837
1838 Register Val = MI.getOperand(1).getReg();
1839 KnownFPClass KnownSrc;
1840 if (InterestedSrcs != fcNone)
1841 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1842 Depth + 1);
1843
1844 LLT Ty = MRI.getType(Val).getScalarType();
1845 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1846 DenormalMode Mode = MF->getDenormalMode(FltSem);
1847 bool IsMultiUnitFPType = &FltSem == &APFloat::PPCDoubleDouble();
1848 Known = KnownFPClass::log(KnownSrc, Mode, IsMultiUnitFPType);
1849 break;
1850 }
1851 case TargetOpcode::G_FPOW: {
1852 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1853 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1854 if (!WantNaN && !WantNegative)
1855 break;
1856
1857 FPClassTest InterestedLHS = fcNone;
1858 FPClassTest InterestedRHS = fcNone;
1859 if (WantNaN) {
1860 // pow may return NaN if one of the arguments is NaN. NaN may be produced
1861 // from a non-zero-finite-negative base and a non-integer exponent.
1862 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
1863 InterestedRHS |= fcNan;
1864 }
1865 if (WantNegative) {
1866 // A negative value is returned when a negative base is raised to an odd
1867 // integer power. Only normal values can be odd integers.
1868 InterestedLHS |= fcNegative;
1869 InterestedRHS |= fcNormal;
1870 }
1871
1872 KnownFPClass KnownLHS;
1873 computeKnownFPClass(MI.getOperand(1).getReg(), DemandedElts, InterestedLHS,
1874 KnownLHS, Depth + 1);
1875
1876 // If the LHS is unknown, then querying the RHS is only useful for rare edge
1877 // cases.
1878 if (KnownLHS.isUnknown())
1879 break;
1880
1881 KnownFPClass KnownRHS;
1882 computeKnownFPClass(MI.getOperand(2).getReg(), DemandedElts, InterestedRHS,
1883 KnownRHS, Depth + 1);
1884 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
1885 break;
1886 }
1887 case TargetOpcode::G_FPOWI: {
1888 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1889 break;
1890
1891 Register Exp = MI.getOperand(2).getReg();
1892 LLT ExpTy = MRI.getType(Exp);
1893 KnownBits ExponentKnownBits = getKnownBits(
1894 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1895
1896 FPClassTest InterestedSrcs = fcNone;
1897 if (InterestedClasses & fcNan)
1898 InterestedSrcs |= fcNan;
1899 if (!ExponentKnownBits.isZero()) {
1900 if (InterestedClasses & fcInf)
1901 InterestedSrcs |= fcFinite | fcInf;
1902 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1903 InterestedSrcs |= fcNegative;
1904 }
1905
1906 KnownFPClass KnownSrc;
1907 if (InterestedSrcs != fcNone) {
1908 Register Val = MI.getOperand(1).getReg();
1909 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1910 Depth + 1);
1911 }
1912
1913 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1914 break;
1915 }
1916 case TargetOpcode::G_FLDEXP: {
1917 Register Val = MI.getOperand(1).getReg();
1918 KnownFPClass KnownSrc;
1919 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1920 Depth + 1);
1921
1922 // Can refine inf/zero handling based on the exponent operand.
1923 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1924 KnownBits ExpBits;
1925 if ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone) {
1926 Register ExpReg = MI.getOperand(2).getReg();
1927 LLT ExpTy = MRI.getType(ExpReg);
1928 ExpBits = getKnownBits(
1929 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1930 }
1931
1932 LLT ScalarTy = DstTy.getScalarType();
1933 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1934 DenormalMode Mode = MF->getDenormalMode(Flt);
1935 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1936 break;
1937 }
1938 case TargetOpcode::G_FADD:
1939 case TargetOpcode::G_STRICT_FADD:
1940 case TargetOpcode::G_FSUB:
1941 case TargetOpcode::G_STRICT_FSUB: {
1942 Register LHS = MI.getOperand(1).getReg();
1943 Register RHS = MI.getOperand(2).getReg();
1944
1945 const bool IsAdd =
1946 Opcode == TargetOpcode::G_FADD || Opcode == TargetOpcode::G_STRICT_FADD;
1947 const bool IsStrict = Opcode == TargetOpcode::G_STRICT_FADD ||
1948 Opcode == TargetOpcode::G_STRICT_FSUB;
1949 // TODO: Obtain rounding mode for strict fadd/fsub.
1950 const RoundingMode RM =
1952
1953 bool WantNegative =
1954 IsAdd &&
1955 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1956 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1957 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1958
1959 if (!WantNaN && !WantNegative && !WantNegZero) {
1960 break;
1961 }
1962
1963 DenormalMode Mode =
1964 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1965
1966 FPClassTest InterestedSrcs = InterestedClasses;
1967 if (WantNegative)
1968 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1969 if (InterestedClasses & fcNan)
1970 InterestedSrcs |= fcInf;
1971
1972 // Special case fadd x, x (canonical form of fmul x, 2).
1973 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1974 KnownFPClass KnownSelf;
1975 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1976 Depth + 1);
1977 Known = KnownFPClass::fadd_self(KnownSelf, RM, Mode);
1978 break;
1979 }
1980
1981 KnownFPClass KnownLHS, KnownRHS;
1982 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1983
1984 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1985 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1986 WantNegZero || !IsAdd) {
1987 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1988 // there's no point.
1989 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1990 Depth + 1);
1991 }
1992
1993 if (IsAdd)
1994 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, RM, Mode);
1995 else
1996 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, RM, Mode);
1997 break;
1998 }
1999 case TargetOpcode::G_FMUL: {
2000 Register LHS = MI.getOperand(1).getReg();
2001 Register RHS = MI.getOperand(2).getReg();
2002 DenormalMode Mode =
2003 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2004
2005 // X * X is always non-negative or a NaN (use square() for precision).
2006 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
2007 KnownFPClass KnownSrc;
2008 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
2009 Known = KnownFPClass::square(KnownSrc, Mode);
2010 } else {
2011 // If RHS is a scalar constant, use the more precise APFloat overload.
2012 auto RHSCst = GFConstant::getConstant(RHS, MRI);
2013 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
2014 KnownFPClass KnownLHS;
2015 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
2016 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
2017 } else {
2018 KnownFPClass KnownLHS, KnownRHS;
2019 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
2020 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
2021 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
2022
2023 // If one operand is known |x| <= 1 and the other is finite, the
2024 // product cannot overflow to infinity.
2025 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
2026 Known.knownNot(fcInf);
2027 else if (KnownRHS.isKnownNever(fcInf) &&
2029 Known.knownNot(fcInf);
2030 }
2031 }
2032 break;
2033 }
2034 case TargetOpcode::G_FDIV: {
2035 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
2036
2037 Register LHS = MI.getOperand(1).getReg();
2038 Register RHS = MI.getOperand(2).getReg();
2039
2040 DenormalMode Mode =
2041 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2042
2043 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
2044 // X / X is always exactly 1.0 or a NaN.
2045 Known.setKnownFPClasses(fcPosNormal | fcNan);
2046
2047 if (!WantNan)
2048 break;
2049
2050 KnownFPClass KnownSrc;
2051 computeKnownFPClass(LHS, DemandedElts,
2052 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
2053 Depth + 1);
2054 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
2055 break;
2056 }
2057
2058 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
2059 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
2060 if (!WantNan && !WantNegative && !WantPositive)
2061 break;
2062
2063 KnownFPClass KnownLHS, KnownRHS;
2064 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
2065
2066 bool KnowSomethingUseful =
2067 KnownRHS.isKnownNever(fcSNan) ||
2070
2071 if (KnowSomethingUseful)
2072 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
2073
2074 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
2075 break;
2076 }
2077 case TargetOpcode::G_FREM:
2078 case TargetOpcode::G_STRICT_FREM: {
2079 FPClassTest InterestedLHS = fcNone;
2080 FPClassTest InterestedRHS = fcNone;
2081
2082 // NaN is also generated for frem(Inf, x) and frem(x, 0.0).
2083 if (InterestedClasses & fcNan) {
2084 InterestedLHS |= fcNan | fcInf;
2085 InterestedRHS |= fcNan | fcZero | fcSubnormal;
2086 }
2087
2088 // The sign for frem is the same as the first operand.
2089 if (InterestedClasses & (fcPosNormal | fcPosSubnormal))
2090 InterestedLHS |= fcPosNormal | fcPosSubnormal;
2091 if (InterestedClasses & (fcNegNormal | fcNegSubnormal))
2092 InterestedLHS |= fcNegNormal | fcNegSubnormal;
2093
2094 // A negative zero result requires a negative finite first operand.
2095 if (InterestedClasses & fcNegZero)
2096 InterestedLHS |= fcNegFinite;
2097
2098 // A positive zero result can additionally come from a negative finite
2099 // result being flushed to positive zero.
2100 if (InterestedClasses & fcPosZero)
2101 InterestedLHS |= fcPosFinite | fcNegNormal | fcNegSubnormal;
2102
2103 Register LHS = MI.getOperand(1).getReg();
2104 Register RHS = MI.getOperand(2).getReg();
2105
2106 DenormalMode Mode =
2107 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2108
2109 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
2110 // X % X is always exactly [+-]0.0 or a NaN.
2111 FPClassTest InterestedSrcs = InterestedLHS | InterestedRHS;
2112 KnownFPClass KnownSrc;
2113 if (InterestedSrcs != fcNone)
2114 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSrc,
2115 Depth + 1);
2116 Known = KnownFPClass::frem_self(KnownSrc, Mode);
2117 break;
2118 }
2119
2120 KnownFPClass KnownLHS;
2121 if (InterestedLHS != fcNone)
2122 computeKnownFPClass(LHS, DemandedElts, InterestedLHS, KnownLHS,
2123 Depth + 1);
2124
2125 KnownFPClass KnownRHS;
2126 // RHS is only useful for refining NaN classes.
2127 if (InterestedRHS != fcNone && KnownLHS.isKnownNever(fcSNan))
2128 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS,
2129 Depth + 1);
2130
2131 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
2132
2133 break;
2134 }
2135 case TargetOpcode::G_FFREXP: {
2136 // Only handle the mantissa output (operand 0); the exponent is an integer.
2137 if (R != MI.getOperand(0).getReg())
2138 break;
2139 Register Src = MI.getOperand(2).getReg();
2140 FPClassTest InterestedSrcs = InterestedClasses;
2141
2142 // Positive subnormals and negative subnormals could become positive zero.
2143 if (InterestedClasses & fcPosZero)
2144 InterestedSrcs |= fcSubnormal;
2145
2146 // Negative subnormals could become negative zero.
2147 if (InterestedClasses & fcNegZero)
2148 InterestedSrcs |= fcNegSubnormal;
2149
2150 if (InterestedClasses & fcPosNormal)
2151 InterestedSrcs |= fcPosSubnormal;
2152
2153 if (InterestedClasses & fcNegNormal)
2154 InterestedSrcs |= fcNegSubnormal;
2155
2156 KnownFPClass KnownSrc;
2157 computeKnownFPClass(Src, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
2158 DenormalMode Mode =
2159 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2160 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
2161 break;
2162 }
2163 case TargetOpcode::G_FPEXT: {
2164 Register Src = MI.getOperand(1).getReg();
2165 KnownFPClass KnownSrc;
2166 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
2167 Depth + 1);
2168
2169 LLT DstScalarTy = DstTy.getScalarType();
2170 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
2171 LLT SrcTy = MRI.getType(Src).getScalarType();
2172 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
2173
2174 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem,
2175 MF->getDenormalMode(DstSem),
2176 MF->getDenormalMode(SrcSem));
2177 break;
2178 }
2179 case TargetOpcode::G_FPTRUNC:
2180 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND: {
2181 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
2182 Depth);
2183 break;
2184 }
2185 case TargetOpcode::G_SITOFP:
2186 case TargetOpcode::G_UITOFP: {
2187 // Cannot produce nan
2188 Known.knownNot(fcNan);
2189
2190 // Integers cannot be subnormal
2191 Known.knownNot(fcSubnormal);
2192
2193 // sitofp and uitofp turn into +0.0 for zero.
2194 Known.knownNot(fcNegZero);
2195
2196 // UIToFP is always non-negative regardless of known bits.
2197 if (Opcode == TargetOpcode::G_UITOFP)
2198 Known.signBitMustBeZero();
2199
2200 // Only compute known bits if we can learn something useful from them.
2201 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
2202 break;
2203
2204 Register Val = MI.getOperand(1).getReg();
2205 LLT Ty = MRI.getType(Val);
2206 KnownBits IntKnown = getKnownBits(
2207 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
2208
2209 // If the integer is non-zero, the result cannot be +0.0.
2210 if (IntKnown.isNonZero())
2211 Known.knownNot(fcPosZero);
2212
2213 if (Opcode == TargetOpcode::G_SITOFP) {
2214 // If the signed integer is known non-negative, the result is
2215 // non-negative. If the signed integer is known negative, the result is
2216 // negative.
2217 if (IntKnown.isNonNegative())
2218 Known.signBitMustBeZero();
2219 else if (IntKnown.isNegative())
2220 Known.signBitMustBeOne();
2221 }
2222
2223 if (InterestedClasses & fcInf) {
2224 LLT FPTy = DstTy.getScalarType();
2225 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
2226
2227 // Compute the effective integer width after removing known-zero leading
2228 // bits, to check if the result can overflow to infinity.
2229 int IntSize = IntKnown.getBitWidth();
2230 if (Opcode == TargetOpcode::G_UITOFP)
2231 IntSize -= IntKnown.countMinLeadingZeros();
2232 else
2233 IntSize -= IntKnown.countMinSignBits();
2234
2235 // If the exponent of the largest finite FP value can hold the largest
2236 // integer, the result of the cast must be finite.
2237 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
2238 Known.knownNot(fcInf);
2239 }
2240
2241 break;
2242 }
2243 // case TargetOpcode::G_MERGE_VALUES:
2244 case TargetOpcode::G_BUILD_VECTOR:
2245 case TargetOpcode::G_CONCAT_VECTORS: {
2246 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
2247
2248 if (!DstTy.isFixedVector())
2249 break;
2250
2251 bool First = true;
2252 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
2253 // We know the index we are inserting to, so clear it from Vec check.
2254 bool NeedsElt = DemandedElts[Idx];
2255
2256 // Do we demand the inserted element?
2257 if (NeedsElt) {
2258 Register Src = Merge.getSourceReg(Idx);
2259 if (First) {
2260 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
2261 First = false;
2262 } else {
2263 KnownFPClass Known2;
2264 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
2265 Known |= Known2;
2266 }
2267
2268 // If we don't know any bits, early out.
2269 if (Known.isUnknown())
2270 break;
2271 }
2272 }
2273
2274 break;
2275 }
2276 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
2277 // Look through extract element. If the index is non-constant or
2278 // out-of-range demand all elements, otherwise just the extracted
2279 // element.
2280 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
2281 Register Vec = Extract.getVectorReg();
2282 Register Idx = Extract.getIndexReg();
2283
2284 auto CIdx = getIConstantVRegVal(Idx, MRI);
2285
2286 LLT VecTy = MRI.getType(Vec);
2287
2288 if (VecTy.isFixedVector()) {
2289 unsigned NumElts = VecTy.getNumElements();
2290 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2291 if (CIdx && CIdx->ult(NumElts))
2292 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2293 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
2294 Depth + 1);
2295 }
2296
2297 break;
2298 }
2299 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2300 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
2301 Register Vec = Insert.getVectorReg();
2302 Register Elt = Insert.getElementReg();
2303 Register Idx = Insert.getIndexReg();
2304
2305 LLT VecTy = MRI.getType(Vec);
2306
2307 if (VecTy.isScalableVector())
2308 return;
2309
2310 auto CIdx = getIConstantVRegVal(Idx, MRI);
2311
2312 unsigned NumElts = DemandedElts.getBitWidth();
2313 APInt DemandedVecElts = DemandedElts;
2314 bool NeedsElt = true;
2315 // If we know the index we are inserting to, clear it from Vec check.
2316 if (CIdx && CIdx->ult(NumElts)) {
2317 DemandedVecElts.clearBit(CIdx->getZExtValue());
2318 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2319 }
2320
2321 // Do we demand the inserted element?
2322 if (NeedsElt) {
2323 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
2324 // If we don't know any bits, early out.
2325 if (Known.isUnknown())
2326 break;
2327 } else {
2329 }
2330
2331 // Do we need anymore elements from Vec?
2332 if (!DemandedVecElts.isZero()) {
2333 KnownFPClass Known2;
2334 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
2335 Depth + 1);
2336 Known |= Known2;
2337 }
2338
2339 break;
2340 }
2341 case TargetOpcode::G_SHUFFLE_VECTOR: {
2342 // For undef elements, we don't know anything about the common state of
2343 // the shuffle result.
2344 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
2345 APInt DemandedLHS, DemandedRHS;
2346 if (DstTy.isScalableVector()) {
2347 assert(DemandedElts == APInt(1, 1));
2348 DemandedLHS = DemandedRHS = DemandedElts;
2349 } else {
2350 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
2351 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
2352 DemandedLHS, DemandedRHS)) {
2353 Known.resetAll();
2354 return;
2355 }
2356 }
2357
2358 if (!!DemandedLHS) {
2359 Register LHS = Shuf.getSrc1Reg();
2360 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
2361 Depth + 1);
2362
2363 // If we don't know any bits, early out.
2364 if (Known.isUnknown())
2365 break;
2366 } else {
2368 }
2369
2370 if (!!DemandedRHS) {
2371 KnownFPClass Known2;
2372 Register RHS = Shuf.getSrc2Reg();
2373 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
2374 Depth + 1);
2375 Known |= Known2;
2376 }
2377 break;
2378 }
2379 case TargetOpcode::G_PHI: {
2380 // Cap PHI recursion below the global limit to avoid spending the entire
2381 // budget chasing loop back-edges (matches ValueTracking's
2382 // PhiRecursionLimit).
2384 break;
2385 // PHI's operands are a mix of registers and basic blocks interleaved.
2386 // We only care about the register ones.
2387 bool First = true;
2388 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
2389 const MachineOperand &Src = MI.getOperand(Idx);
2390 Register SrcReg = Src.getReg();
2391 if (First) {
2392 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
2393 Depth + 1);
2394 First = false;
2395 } else {
2396 KnownFPClass Known2;
2397 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
2398 Depth + 1);
2399 Known = Known.intersectWith(Known2);
2400 }
2401 if (Known.isUnknown())
2402 break;
2403 }
2404 break;
2405 }
2406 case TargetOpcode::G_FREEZE: {
2407 Register Src = MI.getOperand(1).getReg();
2408 if (isGuaranteedNotToBeUndefOrPoison(Src, MRI, Depth + 1)) {
2409 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known,
2410 Depth + 1);
2411 }
2412 break;
2413 }
2414 case TargetOpcode::COPY: {
2415 Register Src = MI.getOperand(1).getReg();
2416
2417 if (!Src.isVirtual())
2418 return;
2419
2420 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
2421 break;
2422 }
2423 }
2424}
2425
2427GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
2428 FPClassTest InterestedClasses,
2429 unsigned Depth) {
2430 KnownFPClass KnownClasses;
2431 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
2432 return KnownClasses;
2433}
2434
2435KnownFPClass GISelValueTracking::computeKnownFPClass(
2436 Register R, FPClassTest InterestedClasses, unsigned Depth) {
2438 computeKnownFPClass(R, Known, InterestedClasses, Depth);
2439 return Known;
2440}
2441
2442KnownFPClass GISelValueTracking::computeKnownFPClass(
2443 Register R, const APInt &DemandedElts, uint32_t Flags,
2444 FPClassTest InterestedClasses, unsigned Depth) {
2446 InterestedClasses &= ~fcNan;
2448 InterestedClasses &= ~fcInf;
2449
2450 KnownFPClass Result =
2451 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
2452
2454 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
2456 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
2457 return Result;
2458}
2459
2460KnownFPClass GISelValueTracking::computeKnownFPClass(
2461 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
2462 LLT Ty = MRI.getType(R);
2463 APInt DemandedElts =
2464 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2465 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
2466}
2467
2469 const MachineInstr *DefMI = MRI.getVRegDef(Val);
2470 if (!DefMI)
2471 return false;
2472
2473 if (DefMI->getFlag(MachineInstr::FmNoNans))
2474 return true;
2475
2476 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
2477 // the value-tracking analysis for the SNaN-only case: if the defining op is
2478 // known to quiet sNaN, the output can never be an sNaN.
2479 if (SNaN) {
2480 switch (DefMI->getOpcode()) {
2481 default:
2482 break;
2483 case TargetOpcode::G_FADD:
2484 case TargetOpcode::G_STRICT_FADD:
2485 case TargetOpcode::G_FSUB:
2486 case TargetOpcode::G_STRICT_FSUB:
2487 case TargetOpcode::G_FMUL:
2488 case TargetOpcode::G_STRICT_FMUL:
2489 case TargetOpcode::G_FDIV:
2490 case TargetOpcode::G_STRICT_FDIV:
2491 case TargetOpcode::G_FREM:
2492 case TargetOpcode::G_STRICT_FREM:
2493 case TargetOpcode::G_FMA:
2494 case TargetOpcode::G_STRICT_FMA:
2495 case TargetOpcode::G_FMAD:
2496 case TargetOpcode::G_FSQRT:
2497 case TargetOpcode::G_STRICT_FSQRT:
2498 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
2499 // exactly (LLVM LangRef: "never change anything except possibly the sign
2500 // bit"). They must NOT be listed here.
2501 case TargetOpcode::G_FSIN:
2502 case TargetOpcode::G_FCOS:
2503 case TargetOpcode::G_FSINCOS:
2504 case TargetOpcode::G_FTAN:
2505 case TargetOpcode::G_FASIN:
2506 case TargetOpcode::G_FACOS:
2507 case TargetOpcode::G_FATAN:
2508 case TargetOpcode::G_FATAN2:
2509 case TargetOpcode::G_FSINH:
2510 case TargetOpcode::G_FCOSH:
2511 case TargetOpcode::G_FTANH:
2512 case TargetOpcode::G_FEXP:
2513 case TargetOpcode::G_FEXP2:
2514 case TargetOpcode::G_FEXP10:
2515 case TargetOpcode::G_FLOG:
2516 case TargetOpcode::G_FLOG2:
2517 case TargetOpcode::G_FLOG10:
2518 case TargetOpcode::G_FPOW:
2519 case TargetOpcode::G_FPOWI:
2520 case TargetOpcode::G_FLDEXP:
2521 case TargetOpcode::G_STRICT_FLDEXP:
2522 case TargetOpcode::G_FFREXP:
2523 case TargetOpcode::G_INTRINSIC_TRUNC:
2524 case TargetOpcode::G_INTRINSIC_ROUND:
2525 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2526 case TargetOpcode::G_FFLOOR:
2527 case TargetOpcode::G_FCEIL:
2528 case TargetOpcode::G_FRINT:
2529 case TargetOpcode::G_FNEARBYINT:
2530 case TargetOpcode::G_FPEXT:
2531 case TargetOpcode::G_FPTRUNC:
2532 case TargetOpcode::G_FCANONICALIZE:
2533 case TargetOpcode::G_FMINNUM:
2534 case TargetOpcode::G_FMAXNUM:
2535 case TargetOpcode::G_FMINNUM_IEEE:
2536 case TargetOpcode::G_FMAXNUM_IEEE:
2537 case TargetOpcode::G_FMINIMUM:
2538 case TargetOpcode::G_FMAXIMUM:
2539 case TargetOpcode::G_FMINIMUMNUM:
2540 case TargetOpcode::G_FMAXIMUMNUM:
2541 return true;
2542 }
2543 }
2544
2545 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2546
2547 if (SNaN)
2548 return FPClass.isKnownNever(fcSNan);
2549
2550 return FPClass.isKnownNeverNaN();
2551}
2552
2554 KnownFPClass Known = computeKnownFPClass(Val, fcZero | fcSubnormal, Depth);
2555 LLT Ty = MRI.getType(Val).getScalarType();
2556 return Known.isKnownNeverLogicalZero(
2557 MF.getDenormalMode(getFltSemanticForLLT(Ty)));
2558}
2559
2560/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2561unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2562 const APInt &DemandedElts,
2563 unsigned Depth) {
2564 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2565 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2566 if (Src1SignBits == 1)
2567 return 1;
2568 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2569}
2570
2571/// Compute the known number of sign bits with attached range metadata in the
2572/// memory operand. If this is an extending load, accounts for the behavior of
2573/// the high bits.
2575 unsigned TyBits) {
2576 const MDNode *Ranges = Ld->getRanges();
2577 if (!Ranges)
2578 return 1;
2579
2581 if (TyBits > CR.getBitWidth()) {
2582 switch (Ld->getOpcode()) {
2583 case TargetOpcode::G_SEXTLOAD:
2584 CR = CR.signExtend(TyBits);
2585 break;
2586 case TargetOpcode::G_ZEXTLOAD:
2587 CR = CR.zeroExtend(TyBits);
2588 break;
2589 default:
2590 break;
2591 }
2592 }
2593
2594 return std::min(CR.getSignedMin().getNumSignBits(),
2596}
2597
2599 const APInt &DemandedElts,
2600 unsigned Depth) {
2601 MachineInstr &MI = *MRI.getVRegDef(R);
2602 unsigned Opcode = MI.getOpcode();
2603
2604 if (Opcode == TargetOpcode::G_CONSTANT)
2605 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2606
2607 if (Depth == getMaxDepth())
2608 return 1;
2609
2610 if (!DemandedElts)
2611 return 1; // No demanded elts, better to assume we don't know anything.
2612
2613 LLT DstTy = MRI.getType(R);
2614 const unsigned TyBits = DstTy.getScalarSizeInBits();
2615
2616 // Handle the case where this is called on a register that does not have a
2617 // type constraint. This is unlikely to occur except by looking through copies
2618 // but it is possible for the initial register being queried to be in this
2619 // state.
2620 if (!DstTy.isValid())
2621 return 1;
2622
2623 unsigned FirstAnswer = 1;
2624 switch (Opcode) {
2625 case TargetOpcode::COPY: {
2626 MachineOperand &Src = MI.getOperand(1);
2627 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2628 MRI.getType(Src.getReg()).isValid()) {
2629 // Don't increment Depth for this one since we didn't do any work.
2630 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2631 }
2632
2633 return 1;
2634 }
2635 case TargetOpcode::G_FREEZE: {
2636 Register Src = MI.getOperand(1).getReg();
2637 if (isGuaranteedNotToBeUndefOrPoison(Src, MRI, Depth + 1))
2638 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2639 break;
2640 }
2641 case TargetOpcode::G_SEXT: {
2642 Register Src = MI.getOperand(1).getReg();
2643 LLT SrcTy = MRI.getType(Src);
2644 unsigned Tmp = TyBits - SrcTy.getScalarSizeInBits();
2645 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2646 }
2647 case TargetOpcode::G_ASSERT_SEXT:
2648 case TargetOpcode::G_SEXT_INREG: {
2649 // Max of the input and what this extends.
2650 Register Src = MI.getOperand(1).getReg();
2651 unsigned SrcBits = MI.getOperand(2).getImm();
2652 unsigned InRegBits = TyBits - SrcBits + 1;
2653 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2654 InRegBits);
2655 }
2656 case TargetOpcode::G_LOAD: {
2657 GLoad *Ld = cast<GLoad>(&MI);
2658 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2659 break;
2660
2661 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2662 }
2663 case TargetOpcode::G_SEXTLOAD: {
2665
2666 // FIXME: We need an in-memory type representation.
2667 if (DstTy.isVector())
2668 return 1;
2669
2670 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2671 if (NumBits != 1)
2672 return NumBits;
2673
2674 // e.g. i16->i32 = '17' bits known.
2675 const MachineMemOperand *MMO = *MI.memoperands_begin();
2676 return TyBits - MMO->getSizeInBits().getValue() + 1;
2677 }
2678 case TargetOpcode::G_ZEXTLOAD: {
2680
2681 // FIXME: We need an in-memory type representation.
2682 if (DstTy.isVector())
2683 return 1;
2684
2685 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2686 if (NumBits != 1)
2687 return NumBits;
2688
2689 // e.g. i16->i32 = '16' bits known.
2690 const MachineMemOperand *MMO = *MI.memoperands_begin();
2691 return TyBits - MMO->getSizeInBits().getValue();
2692 }
2693 case TargetOpcode::G_AND:
2694 case TargetOpcode::G_OR:
2695 case TargetOpcode::G_XOR: {
2696 Register Src1 = MI.getOperand(1).getReg();
2697 unsigned Src1NumSignBits =
2698 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2699 if (Src1NumSignBits != 1) {
2700 Register Src2 = MI.getOperand(2).getReg();
2701 unsigned Src2NumSignBits =
2702 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2703 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2704 }
2705 break;
2706 }
2707 case TargetOpcode::G_ASHR: {
2708 Register Src1 = MI.getOperand(1).getReg();
2709 Register Src2 = MI.getOperand(2).getReg();
2710 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2711 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2712 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2713 break;
2714 }
2715 case TargetOpcode::G_SHL: {
2716 Register Src1 = MI.getOperand(1).getReg();
2717 Register Src2 = MI.getOperand(2).getReg();
2718 if (std::optional<ConstantRange> ShAmtRange =
2719 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2720 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2721 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2722
2723 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2724 unsigned ExtOpc = ExtMI.getOpcode();
2725
2726 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2727 // shifted out, then we can compute the number of sign bits for the
2728 // operand being extended. A future improvement could be to pass along the
2729 // "shifted left by" information in the recursive calls to
2730 // ComputeKnownSignBits. Allowing us to handle this more generically.
2731 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2732 ExtOpc == TargetOpcode::G_ANYEXT) {
2733 LLT ExtTy = MRI.getType(Src1);
2734 Register Extendee = ExtMI.getOperand(1).getReg();
2735 LLT ExtendeeTy = MRI.getType(Extendee);
2736 uint64_t SizeDiff =
2737 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2738
2739 if (SizeDiff <= MinShAmt) {
2740 unsigned Tmp =
2741 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2742 if (MaxShAmt < Tmp)
2743 return Tmp - MaxShAmt;
2744 }
2745 }
2746 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2747 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2748 if (MaxShAmt < Tmp)
2749 return Tmp - MaxShAmt;
2750 }
2751 break;
2752 }
2753 case TargetOpcode::G_ROTL:
2754 case TargetOpcode::G_ROTR: {
2755 Register SrcReg = MI.getOperand(1).getReg();
2756 unsigned Tmp = computeNumSignBits(SrcReg, DemandedElts, Depth + 1);
2757 auto MaybeAmt =
2758 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
2759 FirstAnswer =
2760 SignBitsOps::rot(Tmp, TyBits, MaybeAmt, Opcode == TargetOpcode::G_ROTR);
2761 break;
2762 }
2763 case TargetOpcode::G_SAVGFLOOR:
2764 case TargetOpcode::G_SAVGCEIL: {
2765 Register Src1 = MI.getOperand(1).getReg();
2766 Register Src2 = MI.getOperand(2).getReg();
2767 FirstAnswer = computeNumSignBitsMin(Src1, Src2, DemandedElts, Depth + 1);
2768 break;
2769 }
2770 case TargetOpcode::G_SREM: {
2771 // The sign bit is the LHS's sign bit, except when the result of the
2772 // remainder is zero. The magnitude of the result should be less than or
2773 // equal to the magnitude of the LHS. Therefore, the result should have
2774 // at least as many sign bits as the left hand side.
2775 Register Src = MI.getOperand(1).getReg();
2776 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2777 }
2778 case TargetOpcode::G_TRUNC: {
2779 Register Src = MI.getOperand(1).getReg();
2780 LLT SrcTy = MRI.getType(Src);
2781
2782 // Check if the sign bits of source go down as far as the truncated value.
2783 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2784 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2785 if (NumSrcSignBits > (NumSrcBits - TyBits))
2786 return NumSrcSignBits - (NumSrcBits - TyBits);
2787 break;
2788 }
2789 case TargetOpcode::G_SELECT: {
2790 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2791 MI.getOperand(3).getReg(), DemandedElts,
2792 Depth + 1);
2793 }
2794 case TargetOpcode::G_SMIN:
2795 case TargetOpcode::G_SMAX:
2796 case TargetOpcode::G_UMIN:
2797 case TargetOpcode::G_UMAX:
2798 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2799 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2800 MI.getOperand(2).getReg(), DemandedElts,
2801 Depth + 1);
2802 case TargetOpcode::G_SADDO:
2803 case TargetOpcode::G_SADDE:
2804 case TargetOpcode::G_UADDO:
2805 case TargetOpcode::G_UADDE:
2806 case TargetOpcode::G_SSUBO:
2807 case TargetOpcode::G_SSUBE:
2808 case TargetOpcode::G_USUBO:
2809 case TargetOpcode::G_USUBE:
2810 case TargetOpcode::G_SMULO:
2811 case TargetOpcode::G_UMULO: {
2812 // If compares returns 0/-1, all bits are sign bits.
2813 // We know that we have an integer-based boolean since these operations
2814 // are only available for integer.
2815 if (MI.getOperand(1).getReg() == R) {
2816 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2818 return TyBits;
2819 }
2820
2821 break;
2822 }
2823 case TargetOpcode::G_SUB: {
2824 Register Src2 = MI.getOperand(2).getReg();
2825 unsigned Src2NumSignBits =
2826 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2827 if (Src2NumSignBits == 1)
2828 return 1; // Early out.
2829
2830 // Handle NEG.
2831 Register Src1 = MI.getOperand(1).getReg();
2832 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2833 if (Known1.isZero()) {
2834 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2835 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2836 // sign bits set.
2837 if ((Known2.Zero | 1).isAllOnes())
2838 return TyBits;
2839
2840 // If the input is known to be positive (the sign bit is known clear),
2841 // the output of the NEG has, at worst, the same number of sign bits as
2842 // the input.
2843 if (Known2.isNonNegative()) {
2844 FirstAnswer = Src2NumSignBits;
2845 break;
2846 }
2847
2848 // Otherwise, we treat this like a SUB.
2849 }
2850
2851 unsigned Src1NumSignBits =
2852 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2853 if (Src1NumSignBits == 1)
2854 return 1; // Early Out.
2855
2856 // Sub can have at most one carry bit. Thus we know that the output
2857 // is, at worst, one more bit than the inputs.
2858 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2859 break;
2860 }
2861 case TargetOpcode::G_ADD: {
2862 Register Src2 = MI.getOperand(2).getReg();
2863 unsigned Src2NumSignBits =
2864 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2865 if (Src2NumSignBits <= 2)
2866 return 1; // Early out.
2867
2868 Register Src1 = MI.getOperand(1).getReg();
2869 unsigned Src1NumSignBits =
2870 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2871 if (Src1NumSignBits == 1)
2872 return 1; // Early Out.
2873
2874 // Special case decrementing a value (ADD X, -1):
2875 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2876 if (Known2.isAllOnes()) {
2877 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2878 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2879 // sign bits set.
2880 if ((Known1.Zero | 1).isAllOnes())
2881 return TyBits;
2882
2883 // If we are subtracting one from a positive number, there is no carry
2884 // out of the result.
2885 if (Known1.isNonNegative()) {
2886 FirstAnswer = Src1NumSignBits;
2887 break;
2888 }
2889
2890 // Otherwise, we treat this like an ADD.
2891 }
2892
2893 // Add can have at most one carry bit. Thus we know that the output
2894 // is, at worst, one more bit than the inputs.
2895 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2896 break;
2897 }
2898 case TargetOpcode::G_MUL: {
2899 unsigned Src2NumSignBits =
2900 computeNumSignBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
2901 if (Src2NumSignBits == 1)
2902 break;
2903 unsigned Src1NumSignBits =
2904 computeNumSignBits(MI.getOperand(1).getReg(), DemandedElts, Depth + 1);
2905 if (Src1NumSignBits == 1)
2906 break;
2907
2908 // The product needs at most the sum of the operands' signed widths.
2909 unsigned OutValidBits =
2910 (TyBits - Src1NumSignBits + 1) + (TyBits - Src2NumSignBits + 1);
2911 if (OutValidBits <= TyBits)
2912 FirstAnswer = TyBits - OutValidBits + 1;
2913 break;
2914 }
2915 case TargetOpcode::G_FCMP:
2916 case TargetOpcode::G_ICMP: {
2917 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2918 if (TyBits == 1)
2919 break;
2920 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2922 return TyBits; // All bits are sign bits.
2924 return TyBits - 1; // Every always-zero bit is a sign bit.
2925 break;
2926 }
2927 case TargetOpcode::G_UNMERGE_VALUES: {
2928 unsigned NumOps = MI.getNumOperands();
2929 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
2930 LLT SrcTy = MRI.getType(SrcReg);
2931
2932 if ((SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType()) ||
2933 (SrcTy.isScalar() && DstTy.isVector()))
2934 break;
2935
2936 // Figure out the result operand index
2937 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
2938
2939 APInt SubDemandedElts = DemandedElts;
2940 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
2941 if (SrcTy.isVector()) {
2942 SubDemandedElts =
2943 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
2944 }
2945
2946 unsigned SrcOpKnown =
2947 computeNumSignBits(SrcReg, SubDemandedElts, Depth + 1);
2948 if (SrcTy.isVector()) {
2949 FirstAnswer = SrcOpKnown;
2950 } else if (SrcOpKnown >= (MI.getNumOperands() - DstIdx - 2) * TyBits) {
2951 FirstAnswer = SrcOpKnown >= (MI.getNumOperands() - DstIdx - 1) * TyBits
2952 ? TyBits
2953 : SrcOpKnown % TyBits;
2954 }
2955 break;
2956 }
2957 case TargetOpcode::G_BUILD_VECTOR: {
2958 // Collect the known bits that are shared by every demanded vector element.
2959 FirstAnswer = TyBits;
2960 APInt SingleDemandedElt(1, 1);
2961 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2962 if (!DemandedElts[I])
2963 continue;
2964
2965 unsigned Tmp2 =
2966 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2967 FirstAnswer = std::min(FirstAnswer, Tmp2);
2968
2969 // If we don't know any bits, early out.
2970 if (FirstAnswer == 1)
2971 break;
2972 }
2973 break;
2974 }
2975 case TargetOpcode::G_CONCAT_VECTORS: {
2976 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2977 break;
2978 FirstAnswer = TyBits;
2979 // Determine the minimum number of sign bits across all demanded
2980 // elts of the input vectors. Early out if the result is already 1.
2981 unsigned NumSubVectorElts =
2982 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2983 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2984 APInt DemandedSub =
2985 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2986 if (!DemandedSub)
2987 continue;
2988 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2989
2990 FirstAnswer = std::min(FirstAnswer, Tmp2);
2991
2992 // If we don't know any bits, early out.
2993 if (FirstAnswer == 1)
2994 break;
2995 }
2996 break;
2997 }
2998 case TargetOpcode::G_VECTOR_COMPRESS: {
2999 // Each result lane is either a lane of the source vector or the passthru,
3000 // so the number of sign bits is the minimum of the two.
3001 Register Vec = MI.getOperand(1).getReg();
3002 Register PassThru = MI.getOperand(3).getReg();
3003 unsigned Tmp = computeNumSignBits(PassThru, DemandedElts, Depth + 1);
3004 // If passthru contributes nothing, fall back to the KnownBits refinement.
3005 if (Tmp == 1)
3006 break;
3007 // Compression can move any source lane to any result position, so all
3008 // source lanes are demanded.
3009 APInt DemandedSrcElts = APInt::getAllOnes(DemandedElts.getBitWidth());
3010 unsigned Tmp2 = computeNumSignBits(Vec, DemandedSrcElts, Depth + 1);
3011 FirstAnswer = std::min(Tmp, Tmp2);
3012 break;
3013 }
3014 case TargetOpcode::G_INSERT_VECTOR_ELT: {
3016 Register InVec = Insert.getVectorReg();
3017 Register InVal = Insert.getElementReg();
3018 LLT VecVT = MRI.getType(InVec);
3019
3020 // If we know the element index, split the demand between the inserted
3021 // value and the source vector, otherwise assume we need both. Scalable
3022 // vectors carry no per-lane demand, so they always take the minimum of the
3023 // whole vector and the inserted value.
3024 bool DemandedVal = true;
3025 APInt DemandedVecElts = DemandedElts;
3026 if (!VecVT.isScalableVector()) {
3027 unsigned NumElts = VecVT.getNumElements();
3028 auto ConstEltNo = getIConstantVRegVal(Insert.getIndexReg(), MRI);
3029 if (ConstEltNo && ConstEltNo->ult(NumElts)) {
3030 unsigned EltIdx = ConstEltNo->getZExtValue();
3031 DemandedVal = !!DemandedElts[EltIdx];
3032 DemandedVecElts.clearBit(EltIdx);
3033 }
3034 }
3035
3036 unsigned Tmp = TyBits;
3037 if (DemandedVal) {
3038 // TODO: Handle implicit truncation of inserted elements.
3039 if (MRI.getType(InVal).getSizeInBits() != TyBits)
3040 break;
3041 unsigned ValSignBits = computeNumSignBits(InVal, APInt(1, 1), Depth + 1);
3042 Tmp = std::min(Tmp, ValSignBits);
3043 }
3044 if (!!DemandedVecElts) {
3045 unsigned VecSignBits =
3046 computeNumSignBits(InVec, DemandedVecElts, Depth + 1);
3047 Tmp = std::min(Tmp, VecSignBits);
3048 }
3049 return Tmp;
3050 }
3051 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
3053 Register InVec = Extract.getVectorReg();
3054 Register EltNo = Extract.getIndexReg();
3055 LLT VecVT = MRI.getType(InVec);
3056 if (VecVT.isScalableVector())
3057 return computeNumSignBits(InVec, APInt(1, 1), Depth + 1);
3058 unsigned NumSrcElts = VecVT.getNumElements();
3059 std::optional<APInt> ConstEltNo = getIConstantVRegVal(EltNo, MRI);
3060 APInt DemandedSrcElts =
3061 ConstEltNo && ConstEltNo->ult(NumSrcElts)
3062 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
3063 : APInt::getAllOnes(NumSrcElts);
3064 return computeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
3065 }
3066 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
3067 // Offset the demanded elts by the subvector index.
3068 Register SrcReg = MI.getOperand(1).getReg();
3069 LLT SrcTy = MRI.getType(SrcReg);
3070 APInt DemandedSrcElts;
3071 if (SrcTy.isScalableVector()) {
3072 DemandedSrcElts = APInt(1, 1);
3073 } else {
3074 uint64_t Idx = MI.getOperand(2).getImm();
3075 unsigned NumSrcElts = SrcTy.getNumElements();
3076 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3077 }
3078 return computeNumSignBits(SrcReg, DemandedSrcElts, Depth + 1);
3079 }
3080 case TargetOpcode::G_SHUFFLE_VECTOR: {
3081 // Collect the minimum number of sign bits that are shared by every vector
3082 // element referenced by the shuffle.
3083 APInt DemandedLHS, DemandedRHS;
3084 Register Src1 = MI.getOperand(1).getReg();
3085 unsigned NumElts = MRI.getType(Src1).getNumElements();
3086 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
3087 DemandedElts, DemandedLHS, DemandedRHS))
3088 return 1;
3089
3090 if (!!DemandedLHS)
3091 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
3092 // If we don't know anything, early out and try computeKnownBits fall-back.
3093 if (FirstAnswer == 1)
3094 break;
3095 if (!!DemandedRHS) {
3096 unsigned Tmp2 =
3097 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
3098 FirstAnswer = std::min(FirstAnswer, Tmp2);
3099 }
3100 break;
3101 }
3102 case TargetOpcode::G_SPLAT_VECTOR: {
3103 // Check if the sign bits of source go down as far as the truncated value.
3104 Register Src = MI.getOperand(1).getReg();
3105 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
3106 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
3107 if (NumSrcSignBits > (NumSrcBits - TyBits))
3108 return NumSrcSignBits - (NumSrcBits - TyBits);
3109 break;
3110 }
3111 case TargetOpcode::G_INTRINSIC:
3112 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
3113 case TargetOpcode::G_INTRINSIC_CONVERGENT:
3114 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
3115 default: {
3116 unsigned NumBits =
3117 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
3118 if (NumBits > 1)
3119 FirstAnswer = std::max(FirstAnswer, NumBits);
3120 break;
3121 }
3122 }
3123
3124 // Finally, if we can prove that the top bits of the result are 0's or 1's,
3125 // use this information.
3126 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
3127 return std::max(FirstAnswer, Known.countMinSignBits());
3128}
3129
3131 LLT Ty = MRI.getType(R);
3132 APInt DemandedElts =
3133 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
3134 return computeNumSignBits(R, DemandedElts, Depth);
3135}
3136
3138 Register R, const APInt &DemandedElts, unsigned Depth) {
3139 // Shifting more than the bitwidth is not valid.
3140 MachineInstr &MI = *MRI.getVRegDef(R);
3141 unsigned Opcode = MI.getOpcode();
3142
3143 LLT Ty = MRI.getType(R);
3144 unsigned BitWidth = Ty.getScalarSizeInBits();
3145
3146 if (Opcode == TargetOpcode::G_CONSTANT) {
3147 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
3148 if (ShAmt.uge(BitWidth))
3149 return std::nullopt;
3150 return ConstantRange(ShAmt);
3151 }
3152
3153 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
3154 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3155 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
3156 if (!DemandedElts[I])
3157 continue;
3158 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
3159 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
3160 MinAmt = MaxAmt = nullptr;
3161 break;
3162 }
3163
3164 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
3165 if (ShAmt.uge(BitWidth))
3166 return std::nullopt;
3167 if (!MinAmt || MinAmt->ugt(ShAmt))
3168 MinAmt = &ShAmt;
3169 if (!MaxAmt || MaxAmt->ult(ShAmt))
3170 MaxAmt = &ShAmt;
3171 }
3172 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3173 "Failed to find matching min/max shift amounts");
3174 if (MinAmt && MaxAmt)
3175 return ConstantRange(*MinAmt, *MaxAmt + 1);
3176 }
3177
3178 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3179 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3180 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
3181 if (KnownAmt.getMaxValue().ult(BitWidth))
3182 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3183
3184 return std::nullopt;
3185}
3186
3188 Register R, const APInt &DemandedElts, unsigned Depth) {
3189 if (std::optional<ConstantRange> AmtRange =
3190 getValidShiftAmountRange(R, DemandedElts, Depth))
3191 return AmtRange->getUnsignedMin().getZExtValue();
3192 return std::nullopt;
3193}
3194
3200
3205
3207 if (!Info) {
3208 unsigned MaxDepth =
3210 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
3211 }
3212 return *Info;
3213}
3214
3215AnalysisKey GISelValueTrackingAnalysis::Key;
3216
3220 unsigned MaxDepth =
3222 return Result(MF, MaxDepth);
3223}
3224
3225static PreservedAnalyses
3228 bool PrintFPClass) {
3229 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
3230 const auto &MRI = MF.getRegInfo();
3231 OS << "name: ";
3232 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
3233 OS << '\n';
3234
3235 for (MachineBasicBlock &BB : MF) {
3236 for (MachineInstr &MI : BB) {
3237 for (MachineOperand &MO : MI.defs()) {
3238 if (!MO.isReg() || MO.getReg().isPhysical())
3239 continue;
3240 Register Reg = MO.getReg();
3241 if (!MRI.getType(Reg).isValid())
3242 continue;
3243 if (PrintFPClass) {
3244 KnownFPClass FPKnown = VTA.computeKnownFPClass(Reg);
3245 OS << " " << MO << " FPClasses:" << FPKnown.getKnownFPClasses()
3246 << " SignBitKnown:";
3247 if (FPKnown.getSignBit())
3248 OS << (*FPKnown.getSignBit() ? '1' : '0');
3249 else
3250 OS << '?';
3251 OS << '\n';
3252 } else {
3253 KnownBits Known = VTA.getKnownBits(Reg);
3254 unsigned SignedBits = VTA.computeNumSignBits(Reg);
3255 bool IsKnownNeverZero = VTA.isKnownNeverZero(Reg);
3256 OS << " " << MO << " KnownBits:" << Known
3257 << " SignBits:" << SignedBits
3258 << " IsKnownNeverZero:" << IsKnownNeverZero << '\n';
3259 }
3260 };
3261 }
3262 }
3263 return PreservedAnalyses::all();
3264}
3265
3271
MachineInstrBuilder MachineInstrBuilder & DefMI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Utilities for dealing with flags related to floating point properties and mode controls.
static void dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth)
static void genUnknown(MachineRegisterInfo &MRI, Register Reg, KnownBits &Known)
static PreservedAnalyses printGISelValueTracking(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM, raw_ostream &OS, bool PrintFPClass)
static unsigned computeNumSignBitsFromRangeMetadata(const GAnyLoad *Ld, unsigned TyBits)
Compute the known number of sign bits with attached range metadata in the memory operand.
Provides analysis for querying information about KnownBits during GISel passes.
#define DEBUG_TYPE
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
R600 Clause Merge
SmallVector< MachineOperand, 4 > Cond
MachineRegisterInfo & MRI
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
static bool isAbsoluteValueULEOne(const Value *V)
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Value * RHS
Value * LHS
static constexpr const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:311
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1271
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2018
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:230
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1426
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1057
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:225
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1186
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1115
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1648
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1618
unsigned logBase2() const
Definition APInt.h:1781
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:471
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:875
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:436
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:302
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1437
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:282
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:235
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1225
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
This class represents a range of values.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI ConstantRange multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
Represents any generic load, including sign/zero extending variants.
Represents an extract vector element.
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2205
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
GISelValueTracking & get(MachineFunction &MF)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
KnownBits getKnownBits(Register R)
Align computeKnownAlignment(Register R, unsigned Depth=0)
std::optional< ConstantRange > getValidShiftAmountRange(Register R, const APInt &DemandedElts, unsigned Depth)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
bool maskedValueIsZero(Register Val, const APInt &Mask)
std::optional< uint64_t > getValidMinimumShiftAmount(Register R, const APInt &DemandedElts, unsigned Depth=0)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
const DataLayout & getDataLayout() const
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
const MachineFunction & getMachineFunction() const
bool isKnownNeverNaN(Register Val, bool SNaN=false)
Returns true if Val can be assumed to never be a NaN.
bool isKnownNeverLogicalZero(Register Val, unsigned Depth=0)
Returns true if Val can be assumed to never be a zero, accounting for denormal flushing of the contai...
void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
Evaluate a known-bits query with an explicit worklist instead of recursive descent.
bool isKnownNeverZero(Register R, unsigned Depth=0)
Return true if the value defined by R is provably never zero.
Represents an insert vector element.
Represents a G_LOAD.
Represents a G_SEXTLOAD.
Register getCondReg() const
Register getFalseReg() const
Register getTrueReg() const
Represents a G_SHUFFLE_VECTOR.
ArrayRef< int > getMask() const
Represents a G_ZEXTLOAD.
void setKnownFPClasses(FPClassTest Classes)
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for atan2.
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma.
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, RoundingMode RM, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fptrunc and fptrunc_round.
static constexpr FPClassTest OrderedLessThanZeroMask
static LLVM_ABI KnownFPClass trunc(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for trunc.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fadd.
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, RoundingMode RM, DenormalMode Mode)
Report known values for fsub.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode)
Report known values for fma squared, squared, addend.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, RoundingMode RM, DenormalMode Mode)
Report known values for fadd x, x.
static LLVM_ABI KnownFPClass frem(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for frem.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const APInt &ConstantRangeMin, const APInt &ConstantRangeMax, const fltSemantics &Flt, DenormalMode Mode)
Propagate known class for ldexp, assuming the exponent is known to be within [ConstantRangeMin,...
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode)
Propagate known class for mantissa component of frexp.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for rounding intrinsics (rint, nearbyint, round, roundeven).
static LLVM_ABI KnownFPClass ceil(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for ceil.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy, DenormalMode DstMode, DenormalMode SrcMode)
Propagate known class for fpext.
static LLVM_ABI KnownFPClass cos(const KnownFPClass &Src)
Report known values for cos.
static LLVM_ABI KnownFPClass cosh(const KnownFPClass &Src)
Report known values for cosh.
bool isUnknown() const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass asin(const KnownFPClass &Src)
Report known values for asin.
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode Mode)
void knownNot(FPClassMask RuleOut)
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, const fltSemantics &Flt, DenormalMode Mode)
Apply the canonicalize intrinsic to this value.
std::optional< bool > getSignBit() const
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
static LLVM_ABI KnownFPClass floor(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode)
Propagate known class for floor.
FPClassTest getKnownFPClasses() const
Floating-point classes the value could be one of.
static KnownFPClass getEmpty()
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for fdiv x, x.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
bool isKnownNever(FPClassMask Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode)
static LLVM_ABI KnownFPClass acos(const KnownFPClass &Src)
Report known values for acos.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode, bool IsMultiUnitFPType)
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass frem_self(const KnownFPClass &Src, DenormalMode Mode)
Report known values for frem x, x. Valid for all rounding modes.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fdiv.
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode)
Report known values for fmul.
static LLVM_ABI KnownFPClass pow(const KnownFPClass &LHS, const KnownFPClass &RHS)
Propagate known class for pow.
static LLVM_ABI KnownFPClass sinh(const KnownFPClass &Src)
Report known values for sinh.
static LLVM_ABI KnownFPClass tanh(const KnownFPClass &Src)
Report known values for tanh.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
LLT getScalarType() const
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr ElementCount getElementCount() const
constexpr unsigned getAddressSpace() const
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
TypeSize getValue() const
Metadata node.
Definition Metadata.h:1087
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
LLT getMemoryType() const
Return the memory type of the memory reference.
const MDNode * getRanges() const
Return the range tag for the memory reference.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
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
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3235
operand_type_match m_Reg()
UnaryOp_match< SrcTy, TargetOpcode::G_FFLOOR > m_GFFloor(const SrcTy &Src)
operand_type_match m_Pred()
bind_ty< FPClassTest > m_FPClassTest(FPClassTest &T)
deferred_ty< Register > m_DeferredReg(Register &R)
Similar to m_SpecificReg/Type, but the specific value to match originated from an earlier sub-pattern...
BinaryOp_match< LHS, RHS, TargetOpcode::G_FSUB, false > m_GFSub(const LHS &L, const RHS &R)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
ClassifyOp_match< LHS, Test, TargetOpcode::G_IS_FPCLASS > m_GIsFPClass(const LHS &L, const Test &T)
Matches the register and immediate used in a fpclass test G_IS_FPCLASS val, 96.
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_FCMP > m_GFCmp(const Pred &P, const LHS &L, const RHS &R)
LLVM_ABI unsigned rot(unsigned SrcSignBits, unsigned BitWidth, std::optional< APInt > RotAmt, bool IsRotateRight)
Compute the number of sign bits after rotating a value.
unsigned getNumElements(Type *Ty)
Definition SLPUtils.cpp:117
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp:1677
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:301
@ Known
Known to have no common set bits.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2575
LLVM_ABI const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
scope_exit(Callable) -> scope_exit< Callable >
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1736
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
constexpr unsigned MaxAnalysisRecursionDepth
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
RoundingMode
Rounding mode.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static uint32_t extractBits(uint64_t Val, uint32_t Hi, uint32_t Lo)
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:190
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:269
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:106
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:78
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:288
LLVM_ABI KnownBits ctlz(bool ZeroIsPoison=false) const
Compute known bits for the count of leading zeros.
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:200
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:130
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:103
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:55
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:376
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
bool isAllOnes() const
Returns true if value is all one bits.
Definition KnownBits.h:81
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.