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 OffsetKnown = OffsetKnown.sext(BitWidth);
942 WidthKnown = WidthKnown.sext(BitWidth);
943 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
944 // Sign extend the extracted value using shift left and arithmetic shift
945 // right.
946 KnownBits ExtKnown = KnownBits::makeConstant(APInt(BitWidth, BitWidth));
947 KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
948 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
949 break;
950 }
951 case TargetOpcode::G_UADDO:
952 case TargetOpcode::G_UADDE:
953 case TargetOpcode::G_SADDO:
954 case TargetOpcode::G_SADDE: {
955 if (MI.getOperand(1).getReg() == R) {
956 // If we know the result of a compare has the top bits zero, use this
957 // info.
958 if (TL.getBooleanContents(DstTy.isVector(), false) ==
960 BitWidth > 1)
961 Known.Zero.setBitsFrom(1);
962 break;
963 }
964
965 assert(MI.getOperand(0).getReg() == R &&
966 "We only compute knownbits for the sum here.");
967 // With [US]ADDE, a carry bit may be added in.
968 KnownBits Carry(1);
969 if (Opcode == TargetOpcode::G_UADDE || Opcode == TargetOpcode::G_SADDE) {
970 computeKnownBitsImpl(MI.getOperand(4).getReg(), Carry, DemandedElts,
971 Depth + 1);
972 // Carry has bit width 1
973 Carry = Carry.trunc(1);
974 } else {
975 Carry.setAllZero();
976 }
977
978 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
979 Depth + 1);
980 computeKnownBitsImpl(MI.getOperand(3).getReg(), Known2, DemandedElts,
981 Depth + 1);
982 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
983 break;
984 }
985 case TargetOpcode::G_USUBO:
986 case TargetOpcode::G_USUBE:
987 case TargetOpcode::G_SSUBO:
988 case TargetOpcode::G_SSUBE:
989 case TargetOpcode::G_UMULO:
990 case TargetOpcode::G_SMULO: {
991 if (MI.getOperand(1).getReg() == R) {
992 // If we know the result of a compare has the top bits zero, use this
993 // info.
994 if (TL.getBooleanContents(DstTy.isVector(), false) ==
996 BitWidth > 1)
997 Known.Zero.setBitsFrom(1);
998 }
999 break;
1000 }
1001 case TargetOpcode::G_CTTZ:
1002 case TargetOpcode::G_CTTZ_ZERO_POISON: {
1003 KnownBits SrcOpKnown;
1004 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
1005 Depth + 1);
1006 // If we have a known 1, its position is our upper bound
1007 unsigned PossibleTZ = SrcOpKnown.countMaxTrailingZeros();
1008 unsigned LowBits = llvm::bit_width(PossibleTZ);
1009 Known.Zero.setBitsFrom(LowBits);
1010 break;
1011 }
1012 case TargetOpcode::G_CTLZ:
1013 case TargetOpcode::G_CTLZ_ZERO_POISON: {
1014 KnownBits SrcOpKnown;
1015 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
1016 Depth + 1);
1017 // If we have a known 1, its position is our upper bound.
1018 unsigned PossibleLZ = SrcOpKnown.countMaxLeadingZeros();
1019 unsigned LowBits = llvm::bit_width(PossibleLZ);
1020 Known.Zero.setBitsFrom(LowBits);
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 Known = KnownFPClass::fptrunc(KnownSrc);
1328}
1329
1330void GISelValueTracking::computeKnownFPClass(Register R,
1331 const APInt &DemandedElts,
1332 FPClassTest InterestedClasses,
1334 unsigned Depth) {
1335 assert(Known.isUnknown() && "should not be called with known information");
1336
1337 if (!DemandedElts) {
1338 // No demanded elts, better to assume we don't know anything.
1339 Known.resetAll();
1340 return;
1341 }
1342
1343 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1344
1345 MachineInstr &MI = *MRI.getVRegDef(R);
1346 unsigned Opcode = MI.getOpcode();
1347 LLT DstTy = MRI.getType(R);
1348
1349 if (!DstTy.isValid()) {
1350 Known.resetAll();
1351 return;
1352 }
1353
1354 if (auto Cst = GFConstant::getConstant(R, MRI)) {
1355 switch (Cst->getKind()) {
1357 auto APF = Cst->getScalarValue();
1358 Known.setKnownFPClasses(APF.classify());
1359 Known.setSignBit(APF.isNegative());
1360 break;
1361 }
1363 Known.setKnownFPClasses(fcNone);
1364 bool SignBitAllZero = true;
1365 bool SignBitAllOne = true;
1366
1367 for (auto C : *Cst) {
1368 Known.setKnownFPClasses(Known.getKnownFPClasses() | C.classify());
1369 if (C.isNegative())
1370 SignBitAllZero = false;
1371 else
1372 SignBitAllOne = false;
1373 }
1374
1375 if (SignBitAllOne != SignBitAllZero)
1376 Known.setSignBit(SignBitAllOne);
1377
1378 break;
1379 }
1381 Known.resetAll();
1382 break;
1383 }
1384 }
1385
1386 return;
1387 }
1388
1389 FPClassTest KnownNotFromFlags = fcNone;
1391 KnownNotFromFlags |= fcNan;
1393 KnownNotFromFlags |= fcInf;
1394
1395 // We no longer need to find out about these bits from inputs if we can
1396 // assume this from flags/attributes.
1397 InterestedClasses &= ~KnownNotFromFlags;
1398
1399 llvm::scope_exit ClearClassesFromFlags(
1400 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1401
1402 // All recursive calls that increase depth must come after this.
1404 return;
1405
1406 const MachineFunction *MF = MI.getMF();
1407
1408 switch (Opcode) {
1409 default:
1410 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1411 Depth);
1412 break;
1413 case TargetOpcode::G_FNEG: {
1414 Register Val = MI.getOperand(1).getReg();
1415 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1416 Known.fneg();
1417 break;
1418 }
1419 case TargetOpcode::G_SELECT: {
1420 GSelect &SelMI = cast<GSelect>(MI);
1421 Register Cond = SelMI.getCondReg();
1422 Register LHS = SelMI.getTrueReg();
1423 Register RHS = SelMI.getFalseReg();
1424
1425 FPClassTest FilterLHS = fcAllFlags;
1426 FPClassTest FilterRHS = fcAllFlags;
1427
1428 Register TestedValue;
1429 FPClassTest MaskIfTrue = fcAllFlags;
1430 FPClassTest MaskIfFalse = fcAllFlags;
1431 FPClassTest ClassVal = fcNone;
1432
1433 CmpInst::Predicate Pred;
1434 Register CmpLHS, CmpRHS;
1435 if (mi_match(Cond, MRI,
1436 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1437 // If the select filters out a value based on the class, it no longer
1438 // participates in the class of the result
1439
1440 // TODO: In some degenerate cases we can infer something if we try again
1441 // without looking through sign operations.
1442 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1443 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1444 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1445 } else if (mi_match(
1446 Cond, MRI,
1447 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1448 FPClassTest TestedMask = ClassVal;
1449 MaskIfTrue = TestedMask;
1450 MaskIfFalse = ~TestedMask;
1451 }
1452
1453 if (TestedValue == LHS) {
1454 // match !isnan(x) ? x : y
1455 FilterLHS = MaskIfTrue;
1456 } else if (TestedValue == RHS) { // && IsExactClass
1457 // match !isnan(x) ? y : x
1458 FilterRHS = MaskIfFalse;
1459 }
1460
1461 KnownFPClass Known2;
1462 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1463 Depth + 1);
1464 Known.setKnownFPClasses(Known.getKnownFPClasses() & FilterLHS);
1465
1466 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1467 Known2, Depth + 1);
1468 Known2.setKnownFPClasses(Known2.getKnownFPClasses() & FilterRHS);
1469
1470 Known |= Known2;
1471 break;
1472 }
1473 case TargetOpcode::G_FCOPYSIGN: {
1474 Register Magnitude = MI.getOperand(1).getReg();
1475 Register Sign = MI.getOperand(2).getReg();
1476
1477 KnownFPClass KnownSign;
1478
1479 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1480 Depth + 1);
1481 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1482 Depth + 1);
1483 Known.copysign(KnownSign);
1484 break;
1485 }
1486 case TargetOpcode::G_FMA:
1487 case TargetOpcode::G_FMAD: {
1488 if ((InterestedClasses & fcNegative) == fcNone)
1489 break;
1490
1491 const bool IsFMA = Opcode == TargetOpcode::G_FMA;
1492
1493 Register A = MI.getOperand(1).getReg();
1494 Register B = MI.getOperand(2).getReg();
1495 Register C = MI.getOperand(3).getReg();
1496
1497 DenormalMode Mode =
1498 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1499
1500 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1501 // x * x + y
1502 KnownFPClass KnownSrc, KnownAddend;
1503 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
1504 Depth + 1);
1505 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc,
1506 Depth + 1);
1507 if (KnownNotFromFlags) {
1508 KnownSrc.knownNot(KnownNotFromFlags);
1509 KnownAddend.knownNot(KnownNotFromFlags);
1510 }
1511 Known = IsFMA ? KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode)
1512 : KnownFPClass::fmuladd_square(KnownSrc, KnownAddend, Mode);
1513 } else {
1514 KnownFPClass KnownSrc[3];
1515 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1516 Depth + 1);
1517 if (KnownSrc[0].isUnknown())
1518 break;
1519 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1520 Depth + 1);
1521 if (KnownSrc[1].isUnknown())
1522 break;
1523 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1524 Depth + 1);
1525 if (KnownSrc[2].isUnknown())
1526 break;
1527 if (KnownNotFromFlags) {
1528 KnownSrc[0].knownNot(KnownNotFromFlags);
1529 KnownSrc[1].knownNot(KnownNotFromFlags);
1530 KnownSrc[2].knownNot(KnownNotFromFlags);
1531 }
1532 Known =
1533 IsFMA ? KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode)
1534 : KnownFPClass::fmuladd(KnownSrc[0], KnownSrc[1], KnownSrc[2],
1535 Mode);
1536 }
1537 break;
1538 }
1539 case TargetOpcode::G_FSQRT:
1540 case TargetOpcode::G_STRICT_FSQRT: {
1541 KnownFPClass KnownSrc;
1542 FPClassTest InterestedSrcs = InterestedClasses;
1543 if (InterestedClasses & fcNan)
1544 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1545
1546 Register Val = MI.getOperand(1).getReg();
1547 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1548
1549 DenormalMode Mode =
1550 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1551 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1552 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1553 Known.knownNot(fcNegZero);
1554 break;
1555 }
1556 case TargetOpcode::G_FABS: {
1557 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1558 Register Val = MI.getOperand(1).getReg();
1559 // If we only care about the sign bit we don't need to inspect the
1560 // operand.
1561 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1562 Depth + 1);
1563 }
1564 Known.fabs();
1565 break;
1566 }
1567 case TargetOpcode::G_FATAN2: {
1568 FPClassTest InterestedY = InterestedClasses;
1569 FPClassTest InterestedX = InterestedClasses;
1570
1571 // We can rule out negative values if y cannot have a negative value.
1572 if ((InterestedClasses & fcNegFinite) != fcNone)
1573 InterestedY |= fcNegative;
1574
1575 // We can rule out positive values if y cannot have a positive value.
1576 if ((InterestedClasses & fcPosFinite) != fcNone)
1577 InterestedY |= fcPositive | fcNegSubnormal;
1578
1579 // We can rule out zero and subnormal if x cannot have a positive value.
1580 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
1581 InterestedX |= fcPositive | fcNegSubnormal;
1582
1583 Register Y = MI.getOperand(1).getReg();
1584 Register X = MI.getOperand(2).getReg();
1585 KnownFPClass KnownY, KnownX;
1586 computeKnownFPClass(Y, DemandedElts, InterestedY, KnownY, Depth + 1);
1587 computeKnownFPClass(X, DemandedElts, InterestedX, KnownX, Depth + 1);
1588 DenormalMode Mode =
1589 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1590 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
1591 break;
1592 }
1593 case TargetOpcode::G_FSINH: {
1594 Register Val = MI.getOperand(1).getReg();
1595 KnownFPClass KnownSrc;
1596 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1597 Depth + 1);
1598 Known = KnownFPClass::sinh(KnownSrc);
1599 break;
1600 }
1601 case TargetOpcode::G_FCOSH: {
1602 Register Val = MI.getOperand(1).getReg();
1603 KnownFPClass KnownSrc;
1604 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1605 Depth + 1);
1606 Known = KnownFPClass::cosh(KnownSrc);
1607 break;
1608 }
1609 case TargetOpcode::G_FTANH: {
1610 Register Val = MI.getOperand(1).getReg();
1611 KnownFPClass KnownSrc;
1612 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1613 Depth + 1);
1614 Known = KnownFPClass::tanh(KnownSrc);
1615 break;
1616 }
1617 case TargetOpcode::G_FASIN: {
1618 Register Val = MI.getOperand(1).getReg();
1619 KnownFPClass KnownSrc;
1620 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1621 Depth + 1);
1622 Known = KnownFPClass::asin(KnownSrc);
1623 break;
1624 }
1625 case TargetOpcode::G_FACOS: {
1626 Register Val = MI.getOperand(1).getReg();
1627 KnownFPClass KnownSrc;
1628 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1629 Depth + 1);
1630 Known = KnownFPClass::acos(KnownSrc);
1631 break;
1632 }
1633 case TargetOpcode::G_FATAN: {
1634 Register Val = MI.getOperand(1).getReg();
1635 KnownFPClass KnownSrc;
1636 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1637 Depth + 1);
1638 Known = KnownFPClass::atan(KnownSrc);
1639 break;
1640 }
1641 case TargetOpcode::G_FTAN: {
1642 Register Val = MI.getOperand(1).getReg();
1643 KnownFPClass KnownSrc;
1644 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1645 Depth + 1);
1646 Known = KnownFPClass::tan(KnownSrc);
1647 break;
1648 }
1649 case TargetOpcode::G_FSIN:
1650 case TargetOpcode::G_FCOS: {
1651 // Return NaN on infinite inputs.
1652 Register Val = MI.getOperand(1).getReg();
1653 KnownFPClass KnownSrc;
1654 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1655 Depth + 1);
1656 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1657 : KnownFPClass::sin(KnownSrc);
1658 break;
1659 }
1660 case TargetOpcode::G_FSINCOS: {
1661 // Operand layout: (sin_dst, cos_dst, src)
1662 Register Src = MI.getOperand(2).getReg();
1663 KnownFPClass KnownSrc;
1664 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1665 Depth + 1);
1666 if (R == MI.getOperand(0).getReg())
1667 Known = KnownFPClass::sin(KnownSrc);
1668 else
1669 Known = KnownFPClass::cos(KnownSrc);
1670 break;
1671 }
1672 case TargetOpcode::G_FMAXNUM:
1673 case TargetOpcode::G_FMINNUM:
1674 case TargetOpcode::G_FMINNUM_IEEE:
1675 case TargetOpcode::G_FMAXIMUM:
1676 case TargetOpcode::G_FMINIMUM:
1677 case TargetOpcode::G_FMAXNUM_IEEE:
1678 case TargetOpcode::G_FMAXIMUMNUM:
1679 case TargetOpcode::G_FMINIMUMNUM: {
1680 Register LHS = MI.getOperand(1).getReg();
1681 Register RHS = MI.getOperand(2).getReg();
1682 KnownFPClass KnownLHS, KnownRHS;
1683
1684 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1685 Depth + 1);
1686 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1687 Depth + 1);
1688
1690 switch (Opcode) {
1691 case TargetOpcode::G_FMINIMUM:
1693 break;
1694 case TargetOpcode::G_FMAXIMUM:
1696 break;
1697 case TargetOpcode::G_FMINIMUMNUM:
1699 break;
1700 case TargetOpcode::G_FMAXIMUMNUM:
1702 break;
1703 case TargetOpcode::G_FMINNUM:
1704 case TargetOpcode::G_FMINNUM_IEEE:
1706 break;
1707 case TargetOpcode::G_FMAXNUM:
1708 case TargetOpcode::G_FMAXNUM_IEEE:
1710 break;
1711 default:
1712 llvm_unreachable("unhandled min/max opcode");
1713 }
1714
1715 DenormalMode Mode =
1716 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1717 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1718 break;
1719 }
1720 case TargetOpcode::G_FCANONICALIZE: {
1721 Register Val = MI.getOperand(1).getReg();
1722 KnownFPClass KnownSrc;
1723 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1724 Depth + 1);
1725
1726 LLT Ty = MRI.getType(Val).getScalarType();
1727 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1728 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1729 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
1730 break;
1731 }
1732 case TargetOpcode::G_VECREDUCE_FMAX:
1733 case TargetOpcode::G_VECREDUCE_FMIN:
1734 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1735 case TargetOpcode::G_VECREDUCE_FMINIMUM:
1736 case TargetOpcode::G_VECREDUCE_FMAXIMUMNUM:
1737 case TargetOpcode::G_VECREDUCE_FMINIMUMNUM: {
1738 Register Val = MI.getOperand(1).getReg();
1739 // reduce min/max will choose an element from one of the vector elements,
1740 // so we can infer and class information that is common to all elements.
1741
1742 Known =
1743 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1744 // Can only propagate sign if output is never NaN.
1745 if (!Known.isKnownNeverNaN())
1746 Known.setSignBit(std::nullopt);
1747 break;
1748 }
1749 case TargetOpcode::G_FFLOOR:
1750 case TargetOpcode::G_FCEIL:
1751 case TargetOpcode::G_FRINT:
1752 case TargetOpcode::G_FNEARBYINT:
1753 case TargetOpcode::G_INTRINSIC_ROUND:
1754 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1755 case TargetOpcode::G_INTRINSIC_TRUNC: {
1756 Register Val = MI.getOperand(1).getReg();
1757 KnownFPClass KnownSrc;
1758 FPClassTest InterestedSrcs = InterestedClasses;
1759
1760 // Negative round ups towards zero produce negative zero.
1761 if (InterestedSrcs & fcNegFinite)
1762 InterestedSrcs |= fcNegFinite;
1763
1764 // Negative subnormals may flush to positive zero.
1765 if (InterestedSrcs & fcPosFinite)
1766 InterestedSrcs |= fcPosFinite | fcNegSubnormal;
1767
1768 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1769
1770 LLT Ty = MRI.getType(Val).getScalarType();
1771 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1772 DenormalMode Mode = MF->getDenormalMode(FltSem);
1773 const bool IsMultiUnitFPType = &FltSem == &APFloat::PPCDoubleDouble();
1774
1775 switch (Opcode) {
1776 case TargetOpcode::G_INTRINSIC_TRUNC:
1777 Known = KnownFPClass::trunc(KnownSrc, Mode);
1778 break;
1779 case TargetOpcode::G_FFLOOR:
1780 Known = KnownFPClass::floor(KnownSrc, IsMultiUnitFPType, Mode);
1781 break;
1782 case TargetOpcode::G_FCEIL:
1783 Known = KnownFPClass::ceil(KnownSrc, IsMultiUnitFPType, Mode);
1784 break;
1785 default:
1786 Known = KnownFPClass::roundToIntegral(KnownSrc, IsMultiUnitFPType, Mode);
1787 break;
1788 }
1789 break;
1790 }
1791 case TargetOpcode::G_FEXP:
1792 case TargetOpcode::G_FEXP2:
1793 case TargetOpcode::G_FEXP10: {
1794 Register Val = MI.getOperand(1).getReg();
1795 KnownFPClass KnownSrc;
1796 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1797 Depth + 1);
1798 Known = KnownFPClass::exp(KnownSrc);
1799 break;
1800 }
1801 case TargetOpcode::G_FLOG:
1802 case TargetOpcode::G_FLOG2:
1803 case TargetOpcode::G_FLOG10: {
1804 FPClassTest InterestedSrcs = fcNone;
1805
1806 // log(negative) produces NaN.
1807 if ((InterestedClasses & fcNan) != fcNone)
1808 InterestedSrcs |= fcNan | fcNegative;
1809
1810 // log(logical-zero) produces negative infinity.
1811 if ((InterestedClasses & fcNegInf) != fcNone)
1812 InterestedSrcs |= fcZero | fcSubnormal;
1813
1814 // log(x) < -0.0 if x < +1.0
1815 if ((InterestedClasses & fcNegNormal) != fcNone)
1816 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
1817
1818 // log(x) >= +0.0 if x >= +1.0
1819 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
1820 InterestedSrcs |= fcPosNormal;
1821
1822 // log(x) is positive infinity iff x is positive infinity.
1823 if ((InterestedClasses & fcPosInf) != fcNone)
1824 InterestedSrcs |= fcPosInf;
1825
1826 Register Val = MI.getOperand(1).getReg();
1827 KnownFPClass KnownSrc;
1828 if (InterestedSrcs != fcNone)
1829 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1830 Depth + 1);
1831
1832 LLT Ty = MRI.getType(Val).getScalarType();
1833 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1834 DenormalMode Mode = MF->getDenormalMode(FltSem);
1835 bool IsMultiUnitFPType = &FltSem == &APFloat::PPCDoubleDouble();
1836 Known = KnownFPClass::log(KnownSrc, Mode, IsMultiUnitFPType);
1837 break;
1838 }
1839 case TargetOpcode::G_FPOW: {
1840 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1841 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1842 if (!WantNaN && !WantNegative)
1843 break;
1844
1845 FPClassTest InterestedLHS = fcNone;
1846 FPClassTest InterestedRHS = fcNone;
1847 if (WantNaN) {
1848 // pow may return NaN if one of the arguments is NaN. NaN may be produced
1849 // from a non-zero-finite-negative base and a non-integer exponent.
1850 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
1851 InterestedRHS |= fcNan;
1852 }
1853 if (WantNegative) {
1854 // A negative value is returned when a negative base is raised to an odd
1855 // integer power. Only normal values can be odd integers.
1856 InterestedLHS |= fcNegative;
1857 InterestedRHS |= fcNormal;
1858 }
1859
1860 KnownFPClass KnownLHS;
1861 computeKnownFPClass(MI.getOperand(1).getReg(), DemandedElts, InterestedLHS,
1862 KnownLHS, Depth + 1);
1863
1864 // If the LHS is unknown, then querying the RHS is only useful for rare edge
1865 // cases.
1866 if (KnownLHS.isUnknown())
1867 break;
1868
1869 KnownFPClass KnownRHS;
1870 computeKnownFPClass(MI.getOperand(2).getReg(), DemandedElts, InterestedRHS,
1871 KnownRHS, Depth + 1);
1872 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
1873 break;
1874 }
1875 case TargetOpcode::G_FPOWI: {
1876 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1877 break;
1878
1879 Register Exp = MI.getOperand(2).getReg();
1880 LLT ExpTy = MRI.getType(Exp);
1881 KnownBits ExponentKnownBits = getKnownBits(
1882 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1883
1884 FPClassTest InterestedSrcs = fcNone;
1885 if (InterestedClasses & fcNan)
1886 InterestedSrcs |= fcNan;
1887 if (!ExponentKnownBits.isZero()) {
1888 if (InterestedClasses & fcInf)
1889 InterestedSrcs |= fcFinite | fcInf;
1890 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1891 InterestedSrcs |= fcNegative;
1892 }
1893
1894 KnownFPClass KnownSrc;
1895 if (InterestedSrcs != fcNone) {
1896 Register Val = MI.getOperand(1).getReg();
1897 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1898 Depth + 1);
1899 }
1900
1901 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1902 break;
1903 }
1904 case TargetOpcode::G_FLDEXP: {
1905 Register Val = MI.getOperand(1).getReg();
1906 KnownFPClass KnownSrc;
1907 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1908 Depth + 1);
1909
1910 // Can refine inf/zero handling based on the exponent operand.
1911 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1912 KnownBits ExpBits;
1913 if ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone) {
1914 Register ExpReg = MI.getOperand(2).getReg();
1915 LLT ExpTy = MRI.getType(ExpReg);
1916 ExpBits = getKnownBits(
1917 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1918 }
1919
1920 LLT ScalarTy = DstTy.getScalarType();
1921 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1922 DenormalMode Mode = MF->getDenormalMode(Flt);
1923 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1924 break;
1925 }
1926 case TargetOpcode::G_FADD:
1927 case TargetOpcode::G_FSUB: {
1928 Register LHS = MI.getOperand(1).getReg();
1929 Register RHS = MI.getOperand(2).getReg();
1930 bool IsAdd = Opcode == TargetOpcode::G_FADD;
1931 bool WantNegative =
1932 IsAdd &&
1933 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1934 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1935 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1936
1937 if (!WantNaN && !WantNegative && !WantNegZero) {
1938 break;
1939 }
1940
1941 DenormalMode Mode =
1942 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1943
1944 FPClassTest InterestedSrcs = InterestedClasses;
1945 if (WantNegative)
1946 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1947 if (InterestedClasses & fcNan)
1948 InterestedSrcs |= fcInf;
1949
1950 // Special case fadd x, x (canonical form of fmul x, 2).
1951 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1952 KnownFPClass KnownSelf;
1953 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1954 Depth + 1);
1955 Known = KnownFPClass::fadd_self(KnownSelf, Mode);
1956 break;
1957 }
1958
1959 KnownFPClass KnownLHS, KnownRHS;
1960 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1961
1962 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1963 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1964 WantNegZero || !IsAdd) {
1965 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1966 // there's no point.
1967 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1968 Depth + 1);
1969 }
1970
1971 if (IsAdd)
1972 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, Mode);
1973 else
1974 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
1975 break;
1976 }
1977 case TargetOpcode::G_FMUL: {
1978 Register LHS = MI.getOperand(1).getReg();
1979 Register RHS = MI.getOperand(2).getReg();
1980 DenormalMode Mode =
1981 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1982
1983 // X * X is always non-negative or a NaN (use square() for precision).
1984 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1985 KnownFPClass KnownSrc;
1986 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
1987 Known = KnownFPClass::square(KnownSrc, Mode);
1988 } else {
1989 // If RHS is a scalar constant, use the more precise APFloat overload.
1990 auto RHSCst = GFConstant::getConstant(RHS, MRI);
1991 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
1992 KnownFPClass KnownLHS;
1993 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1994 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
1995 } else {
1996 KnownFPClass KnownLHS, KnownRHS;
1997 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1998 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1999 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
2000
2001 // If one operand is known |x| <= 1 and the other is finite, the
2002 // product cannot overflow to infinity.
2003 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
2004 Known.knownNot(fcInf);
2005 else if (KnownRHS.isKnownNever(fcInf) &&
2007 Known.knownNot(fcInf);
2008 }
2009 }
2010 break;
2011 }
2012 case TargetOpcode::G_FDIV: {
2013 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
2014
2015 Register LHS = MI.getOperand(1).getReg();
2016 Register RHS = MI.getOperand(2).getReg();
2017
2018 DenormalMode Mode =
2019 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2020
2021 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
2022 // X / X is always exactly 1.0 or a NaN.
2023 Known.setKnownFPClasses(fcPosNormal | fcNan);
2024
2025 if (!WantNan)
2026 break;
2027
2028 KnownFPClass KnownSrc;
2029 computeKnownFPClass(LHS, DemandedElts,
2030 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
2031 Depth + 1);
2032 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
2033 break;
2034 }
2035
2036 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
2037 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
2038 if (!WantNan && !WantNegative && !WantPositive)
2039 break;
2040
2041 KnownFPClass KnownLHS, KnownRHS;
2042 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
2043
2044 bool KnowSomethingUseful =
2045 KnownRHS.isKnownNeverNaN() ||
2048
2049 if (KnowSomethingUseful)
2050 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
2051
2052 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
2053 break;
2054 }
2055 case TargetOpcode::G_FREM: {
2056 FPClassTest InterestedLHS = fcNone;
2057 FPClassTest InterestedRHS = fcNone;
2058
2059 // NaN is also generated for frem(Inf, x) and frem(x, 0.0).
2060 if (InterestedClasses & fcNan) {
2061 InterestedLHS |= fcNan | fcInf;
2062 InterestedRHS |= fcNan | fcZero | fcSubnormal;
2063 }
2064
2065 // The sign for frem is the same as the first operand.
2066 if (InterestedClasses & (fcPosNormal | fcPosSubnormal))
2067 InterestedLHS |= fcPosNormal | fcPosSubnormal;
2068 if (InterestedClasses & (fcNegNormal | fcNegSubnormal))
2069 InterestedLHS |= fcNegNormal | fcNegSubnormal;
2070
2071 // A negative zero result requires a negative finite first operand.
2072 if (InterestedClasses & fcNegZero)
2073 InterestedLHS |= fcNegFinite;
2074
2075 // A positive zero result can additionally come from a negative finite
2076 // result being flushed to positive zero.
2077 if (InterestedClasses & fcPosZero)
2078 InterestedLHS |= fcPosFinite | fcNegNormal | fcNegSubnormal;
2079
2080 Register LHS = MI.getOperand(1).getReg();
2081 Register RHS = MI.getOperand(2).getReg();
2082
2083 DenormalMode Mode =
2084 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2085
2086 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
2087 // X % X is always exactly [+-]0.0 or a NaN.
2088 FPClassTest InterestedSrcs = InterestedLHS | InterestedRHS;
2089 KnownFPClass KnownSrc;
2090 if (InterestedSrcs != fcNone)
2091 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSrc,
2092 Depth + 1);
2093 Known = KnownFPClass::frem_self(KnownSrc, Mode);
2094 break;
2095 }
2096
2097 KnownFPClass KnownLHS;
2098 if (InterestedLHS != fcNone)
2099 computeKnownFPClass(LHS, DemandedElts, InterestedLHS, KnownLHS,
2100 Depth + 1);
2101
2102 KnownFPClass KnownRHS;
2103 // RHS is only useful for refining NaN classes.
2104 if (InterestedRHS != fcNone && KnownLHS.isKnownNever(fcSNan))
2105 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS,
2106 Depth + 1);
2107
2108 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
2109
2110 break;
2111 }
2112 case TargetOpcode::G_FFREXP: {
2113 // Only handle the mantissa output (operand 0); the exponent is an integer.
2114 if (R != MI.getOperand(0).getReg())
2115 break;
2116 Register Src = MI.getOperand(2).getReg();
2117 FPClassTest InterestedSrcs = InterestedClasses;
2118
2119 // Positive subnormals and negative subnormals could become positive zero.
2120 if (InterestedClasses & fcPosZero)
2121 InterestedSrcs |= fcSubnormal;
2122
2123 // Negative subnormals could become negative zero.
2124 if (InterestedClasses & fcNegZero)
2125 InterestedSrcs |= fcNegSubnormal;
2126
2127 if (InterestedClasses & fcPosNormal)
2128 InterestedSrcs |= fcPosSubnormal;
2129
2130 if (InterestedClasses & fcNegNormal)
2131 InterestedSrcs |= fcNegSubnormal;
2132
2133 KnownFPClass KnownSrc;
2134 computeKnownFPClass(Src, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
2135 DenormalMode Mode =
2136 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2137 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
2138 break;
2139 }
2140 case TargetOpcode::G_FPEXT: {
2141 Register Src = MI.getOperand(1).getReg();
2142 KnownFPClass KnownSrc;
2143 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
2144 Depth + 1);
2145
2146 LLT DstScalarTy = DstTy.getScalarType();
2147 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
2148 LLT SrcTy = MRI.getType(Src).getScalarType();
2149 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
2150
2151 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem);
2152 break;
2153 }
2154 case TargetOpcode::G_FPTRUNC:
2155 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND: {
2156 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
2157 Depth);
2158 break;
2159 }
2160 case TargetOpcode::G_SITOFP:
2161 case TargetOpcode::G_UITOFP: {
2162 // Cannot produce nan
2163 Known.knownNot(fcNan);
2164
2165 // Integers cannot be subnormal
2166 Known.knownNot(fcSubnormal);
2167
2168 // sitofp and uitofp turn into +0.0 for zero.
2169 Known.knownNot(fcNegZero);
2170
2171 // UIToFP is always non-negative regardless of known bits.
2172 if (Opcode == TargetOpcode::G_UITOFP)
2173 Known.signBitMustBeZero();
2174
2175 // Only compute known bits if we can learn something useful from them.
2176 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
2177 break;
2178
2179 Register Val = MI.getOperand(1).getReg();
2180 LLT Ty = MRI.getType(Val);
2181 KnownBits IntKnown = getKnownBits(
2182 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
2183
2184 // If the integer is non-zero, the result cannot be +0.0.
2185 if (IntKnown.isNonZero())
2186 Known.knownNot(fcPosZero);
2187
2188 if (Opcode == TargetOpcode::G_SITOFP) {
2189 // If the signed integer is known non-negative, the result is
2190 // non-negative. If the signed integer is known negative, the result is
2191 // negative.
2192 if (IntKnown.isNonNegative())
2193 Known.signBitMustBeZero();
2194 else if (IntKnown.isNegative())
2195 Known.signBitMustBeOne();
2196 }
2197
2198 if (InterestedClasses & fcInf) {
2199 LLT FPTy = DstTy.getScalarType();
2200 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
2201
2202 // Compute the effective integer width after removing known-zero leading
2203 // bits, to check if the result can overflow to infinity.
2204 int IntSize = IntKnown.getBitWidth();
2205 if (Opcode == TargetOpcode::G_UITOFP)
2206 IntSize -= IntKnown.countMinLeadingZeros();
2207 else
2208 IntSize -= IntKnown.countMinSignBits();
2209
2210 // If the exponent of the largest finite FP value can hold the largest
2211 // integer, the result of the cast must be finite.
2212 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
2213 Known.knownNot(fcInf);
2214 }
2215
2216 break;
2217 }
2218 // case TargetOpcode::G_MERGE_VALUES:
2219 case TargetOpcode::G_BUILD_VECTOR:
2220 case TargetOpcode::G_CONCAT_VECTORS: {
2221 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
2222
2223 if (!DstTy.isFixedVector())
2224 break;
2225
2226 bool First = true;
2227 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
2228 // We know the index we are inserting to, so clear it from Vec check.
2229 bool NeedsElt = DemandedElts[Idx];
2230
2231 // Do we demand the inserted element?
2232 if (NeedsElt) {
2233 Register Src = Merge.getSourceReg(Idx);
2234 if (First) {
2235 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
2236 First = false;
2237 } else {
2238 KnownFPClass Known2;
2239 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
2240 Known |= Known2;
2241 }
2242
2243 // If we don't know any bits, early out.
2244 if (Known.isUnknown())
2245 break;
2246 }
2247 }
2248
2249 break;
2250 }
2251 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
2252 // Look through extract element. If the index is non-constant or
2253 // out-of-range demand all elements, otherwise just the extracted
2254 // element.
2255 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
2256 Register Vec = Extract.getVectorReg();
2257 Register Idx = Extract.getIndexReg();
2258
2259 auto CIdx = getIConstantVRegVal(Idx, MRI);
2260
2261 LLT VecTy = MRI.getType(Vec);
2262
2263 if (VecTy.isFixedVector()) {
2264 unsigned NumElts = VecTy.getNumElements();
2265 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2266 if (CIdx && CIdx->ult(NumElts))
2267 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2268 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
2269 Depth + 1);
2270 }
2271
2272 break;
2273 }
2274 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2275 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
2276 Register Vec = Insert.getVectorReg();
2277 Register Elt = Insert.getElementReg();
2278 Register Idx = Insert.getIndexReg();
2279
2280 LLT VecTy = MRI.getType(Vec);
2281
2282 if (VecTy.isScalableVector())
2283 return;
2284
2285 auto CIdx = getIConstantVRegVal(Idx, MRI);
2286
2287 unsigned NumElts = DemandedElts.getBitWidth();
2288 APInt DemandedVecElts = DemandedElts;
2289 bool NeedsElt = true;
2290 // If we know the index we are inserting to, clear it from Vec check.
2291 if (CIdx && CIdx->ult(NumElts)) {
2292 DemandedVecElts.clearBit(CIdx->getZExtValue());
2293 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2294 }
2295
2296 // Do we demand the inserted element?
2297 if (NeedsElt) {
2298 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
2299 // If we don't know any bits, early out.
2300 if (Known.isUnknown())
2301 break;
2302 } else {
2303 Known.setKnownFPClasses(fcNone);
2304 }
2305
2306 // Do we need anymore elements from Vec?
2307 if (!DemandedVecElts.isZero()) {
2308 KnownFPClass Known2;
2309 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
2310 Depth + 1);
2311 Known |= Known2;
2312 }
2313
2314 break;
2315 }
2316 case TargetOpcode::G_SHUFFLE_VECTOR: {
2317 // For undef elements, we don't know anything about the common state of
2318 // the shuffle result.
2319 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
2320 APInt DemandedLHS, DemandedRHS;
2321 if (DstTy.isScalableVector()) {
2322 assert(DemandedElts == APInt(1, 1));
2323 DemandedLHS = DemandedRHS = DemandedElts;
2324 } else {
2325 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
2326 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
2327 DemandedLHS, DemandedRHS)) {
2328 Known.resetAll();
2329 return;
2330 }
2331 }
2332
2333 if (!!DemandedLHS) {
2334 Register LHS = Shuf.getSrc1Reg();
2335 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
2336 Depth + 1);
2337
2338 // If we don't know any bits, early out.
2339 if (Known.isUnknown())
2340 break;
2341 } else {
2342 Known.setKnownFPClasses(fcNone);
2343 }
2344
2345 if (!!DemandedRHS) {
2346 KnownFPClass Known2;
2347 Register RHS = Shuf.getSrc2Reg();
2348 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
2349 Depth + 1);
2350 Known |= Known2;
2351 }
2352 break;
2353 }
2354 case TargetOpcode::G_PHI: {
2355 // Cap PHI recursion below the global limit to avoid spending the entire
2356 // budget chasing loop back-edges (matches ValueTracking's
2357 // PhiRecursionLimit).
2359 break;
2360 // PHI's operands are a mix of registers and basic blocks interleaved.
2361 // We only care about the register ones.
2362 bool First = true;
2363 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
2364 const MachineOperand &Src = MI.getOperand(Idx);
2365 Register SrcReg = Src.getReg();
2366 if (First) {
2367 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
2368 Depth + 1);
2369 First = false;
2370 } else {
2371 KnownFPClass Known2;
2372 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
2373 Depth + 1);
2374 Known = Known.intersectWith(Known2);
2375 }
2376 if (Known.isUnknown())
2377 break;
2378 }
2379 break;
2380 }
2381 case TargetOpcode::G_FREEZE: {
2382 Register Src = MI.getOperand(1).getReg();
2383 if (isGuaranteedNotToBeUndefOrPoison(Src, MRI, Depth + 1)) {
2384 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known,
2385 Depth + 1);
2386 }
2387 break;
2388 }
2389 case TargetOpcode::COPY: {
2390 Register Src = MI.getOperand(1).getReg();
2391
2392 if (!Src.isVirtual())
2393 return;
2394
2395 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
2396 break;
2397 }
2398 }
2399}
2400
2402GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
2403 FPClassTest InterestedClasses,
2404 unsigned Depth) {
2405 KnownFPClass KnownClasses;
2406 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
2407 return KnownClasses;
2408}
2409
2410KnownFPClass GISelValueTracking::computeKnownFPClass(
2411 Register R, FPClassTest InterestedClasses, unsigned Depth) {
2413 computeKnownFPClass(R, Known, InterestedClasses, Depth);
2414 return Known;
2415}
2416
2417KnownFPClass GISelValueTracking::computeKnownFPClass(
2418 Register R, const APInt &DemandedElts, uint32_t Flags,
2419 FPClassTest InterestedClasses, unsigned Depth) {
2421 InterestedClasses &= ~fcNan;
2423 InterestedClasses &= ~fcInf;
2424
2425 KnownFPClass Result =
2426 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
2427
2429 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
2431 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
2432 return Result;
2433}
2434
2435KnownFPClass GISelValueTracking::computeKnownFPClass(
2436 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
2437 LLT Ty = MRI.getType(R);
2438 APInt DemandedElts =
2439 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2440 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
2441}
2442
2444 const MachineInstr *DefMI = MRI.getVRegDef(Val);
2445 if (!DefMI)
2446 return false;
2447
2448 if (DefMI->getFlag(MachineInstr::FmNoNans))
2449 return true;
2450
2451 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
2452 // the value-tracking analysis for the SNaN-only case: if the defining op is
2453 // known to quiet sNaN, the output can never be an sNaN.
2454 if (SNaN) {
2455 switch (DefMI->getOpcode()) {
2456 default:
2457 break;
2458 case TargetOpcode::G_FADD:
2459 case TargetOpcode::G_STRICT_FADD:
2460 case TargetOpcode::G_FSUB:
2461 case TargetOpcode::G_STRICT_FSUB:
2462 case TargetOpcode::G_FMUL:
2463 case TargetOpcode::G_STRICT_FMUL:
2464 case TargetOpcode::G_FDIV:
2465 case TargetOpcode::G_FREM:
2466 case TargetOpcode::G_FMA:
2467 case TargetOpcode::G_STRICT_FMA:
2468 case TargetOpcode::G_FMAD:
2469 case TargetOpcode::G_FSQRT:
2470 case TargetOpcode::G_STRICT_FSQRT:
2471 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
2472 // exactly (LLVM LangRef: "never change anything except possibly the sign
2473 // bit"). They must NOT be listed here.
2474 case TargetOpcode::G_FSIN:
2475 case TargetOpcode::G_FCOS:
2476 case TargetOpcode::G_FSINCOS:
2477 case TargetOpcode::G_FTAN:
2478 case TargetOpcode::G_FASIN:
2479 case TargetOpcode::G_FACOS:
2480 case TargetOpcode::G_FATAN:
2481 case TargetOpcode::G_FATAN2:
2482 case TargetOpcode::G_FSINH:
2483 case TargetOpcode::G_FCOSH:
2484 case TargetOpcode::G_FTANH:
2485 case TargetOpcode::G_FEXP:
2486 case TargetOpcode::G_FEXP2:
2487 case TargetOpcode::G_FEXP10:
2488 case TargetOpcode::G_FLOG:
2489 case TargetOpcode::G_FLOG2:
2490 case TargetOpcode::G_FLOG10:
2491 case TargetOpcode::G_FPOW:
2492 case TargetOpcode::G_FPOWI:
2493 case TargetOpcode::G_FLDEXP:
2494 case TargetOpcode::G_STRICT_FLDEXP:
2495 case TargetOpcode::G_FFREXP:
2496 case TargetOpcode::G_INTRINSIC_TRUNC:
2497 case TargetOpcode::G_INTRINSIC_ROUND:
2498 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2499 case TargetOpcode::G_FFLOOR:
2500 case TargetOpcode::G_FCEIL:
2501 case TargetOpcode::G_FRINT:
2502 case TargetOpcode::G_FNEARBYINT:
2503 case TargetOpcode::G_FPEXT:
2504 case TargetOpcode::G_FPTRUNC:
2505 case TargetOpcode::G_FCANONICALIZE:
2506 case TargetOpcode::G_FMINNUM:
2507 case TargetOpcode::G_FMAXNUM:
2508 case TargetOpcode::G_FMINNUM_IEEE:
2509 case TargetOpcode::G_FMAXNUM_IEEE:
2510 case TargetOpcode::G_FMINIMUM:
2511 case TargetOpcode::G_FMAXIMUM:
2512 case TargetOpcode::G_FMINIMUMNUM:
2513 case TargetOpcode::G_FMAXIMUMNUM:
2514 return true;
2515 }
2516 }
2517
2518 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2519
2520 if (SNaN)
2521 return FPClass.isKnownNever(fcSNan);
2522
2523 return FPClass.isKnownNeverNaN();
2524}
2525
2527 KnownFPClass Known = computeKnownFPClass(Val, fcZero | fcSubnormal, Depth);
2528 LLT Ty = MRI.getType(Val).getScalarType();
2529 return Known.isKnownNeverLogicalZero(
2530 MF.getDenormalMode(getFltSemanticForLLT(Ty)));
2531}
2532
2533/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2534unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2535 const APInt &DemandedElts,
2536 unsigned Depth) {
2537 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2538 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2539 if (Src1SignBits == 1)
2540 return 1;
2541 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2542}
2543
2544/// Compute the known number of sign bits with attached range metadata in the
2545/// memory operand. If this is an extending load, accounts for the behavior of
2546/// the high bits.
2548 unsigned TyBits) {
2549 const MDNode *Ranges = Ld->getRanges();
2550 if (!Ranges)
2551 return 1;
2552
2554 if (TyBits > CR.getBitWidth()) {
2555 switch (Ld->getOpcode()) {
2556 case TargetOpcode::G_SEXTLOAD:
2557 CR = CR.signExtend(TyBits);
2558 break;
2559 case TargetOpcode::G_ZEXTLOAD:
2560 CR = CR.zeroExtend(TyBits);
2561 break;
2562 default:
2563 break;
2564 }
2565 }
2566
2567 return std::min(CR.getSignedMin().getNumSignBits(),
2569}
2570
2572 const APInt &DemandedElts,
2573 unsigned Depth) {
2574 MachineInstr &MI = *MRI.getVRegDef(R);
2575 unsigned Opcode = MI.getOpcode();
2576
2577 if (Opcode == TargetOpcode::G_CONSTANT)
2578 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2579
2580 if (Depth == getMaxDepth())
2581 return 1;
2582
2583 if (!DemandedElts)
2584 return 1; // No demanded elts, better to assume we don't know anything.
2585
2586 LLT DstTy = MRI.getType(R);
2587 const unsigned TyBits = DstTy.getScalarSizeInBits();
2588
2589 // Handle the case where this is called on a register that does not have a
2590 // type constraint. This is unlikely to occur except by looking through copies
2591 // but it is possible for the initial register being queried to be in this
2592 // state.
2593 if (!DstTy.isValid())
2594 return 1;
2595
2596 unsigned FirstAnswer = 1;
2597 switch (Opcode) {
2598 case TargetOpcode::COPY: {
2599 MachineOperand &Src = MI.getOperand(1);
2600 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2601 MRI.getType(Src.getReg()).isValid()) {
2602 // Don't increment Depth for this one since we didn't do any work.
2603 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2604 }
2605
2606 return 1;
2607 }
2608 case TargetOpcode::G_FREEZE: {
2609 Register Src = MI.getOperand(1).getReg();
2610 if (isGuaranteedNotToBeUndefOrPoison(Src, MRI, Depth + 1))
2611 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2612 break;
2613 }
2614 case TargetOpcode::G_SEXT: {
2615 Register Src = MI.getOperand(1).getReg();
2616 LLT SrcTy = MRI.getType(Src);
2617 unsigned Tmp = TyBits - SrcTy.getScalarSizeInBits();
2618 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2619 }
2620 case TargetOpcode::G_ASSERT_SEXT:
2621 case TargetOpcode::G_SEXT_INREG: {
2622 // Max of the input and what this extends.
2623 Register Src = MI.getOperand(1).getReg();
2624 unsigned SrcBits = MI.getOperand(2).getImm();
2625 unsigned InRegBits = TyBits - SrcBits + 1;
2626 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2627 InRegBits);
2628 }
2629 case TargetOpcode::G_LOAD: {
2630 GLoad *Ld = cast<GLoad>(&MI);
2631 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2632 break;
2633
2634 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2635 }
2636 case TargetOpcode::G_SEXTLOAD: {
2638
2639 // FIXME: We need an in-memory type representation.
2640 if (DstTy.isVector())
2641 return 1;
2642
2643 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2644 if (NumBits != 1)
2645 return NumBits;
2646
2647 // e.g. i16->i32 = '17' bits known.
2648 const MachineMemOperand *MMO = *MI.memoperands_begin();
2649 return TyBits - MMO->getSizeInBits().getValue() + 1;
2650 }
2651 case TargetOpcode::G_ZEXTLOAD: {
2653
2654 // FIXME: We need an in-memory type representation.
2655 if (DstTy.isVector())
2656 return 1;
2657
2658 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2659 if (NumBits != 1)
2660 return NumBits;
2661
2662 // e.g. i16->i32 = '16' bits known.
2663 const MachineMemOperand *MMO = *MI.memoperands_begin();
2664 return TyBits - MMO->getSizeInBits().getValue();
2665 }
2666 case TargetOpcode::G_AND:
2667 case TargetOpcode::G_OR:
2668 case TargetOpcode::G_XOR: {
2669 Register Src1 = MI.getOperand(1).getReg();
2670 unsigned Src1NumSignBits =
2671 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2672 if (Src1NumSignBits != 1) {
2673 Register Src2 = MI.getOperand(2).getReg();
2674 unsigned Src2NumSignBits =
2675 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2676 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2677 }
2678 break;
2679 }
2680 case TargetOpcode::G_ASHR: {
2681 Register Src1 = MI.getOperand(1).getReg();
2682 Register Src2 = MI.getOperand(2).getReg();
2683 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2684 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2685 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2686 break;
2687 }
2688 case TargetOpcode::G_SHL: {
2689 Register Src1 = MI.getOperand(1).getReg();
2690 Register Src2 = MI.getOperand(2).getReg();
2691 if (std::optional<ConstantRange> ShAmtRange =
2692 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2693 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2694 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2695
2696 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2697 unsigned ExtOpc = ExtMI.getOpcode();
2698
2699 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2700 // shifted out, then we can compute the number of sign bits for the
2701 // operand being extended. A future improvement could be to pass along the
2702 // "shifted left by" information in the recursive calls to
2703 // ComputeKnownSignBits. Allowing us to handle this more generically.
2704 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2705 ExtOpc == TargetOpcode::G_ANYEXT) {
2706 LLT ExtTy = MRI.getType(Src1);
2707 Register Extendee = ExtMI.getOperand(1).getReg();
2708 LLT ExtendeeTy = MRI.getType(Extendee);
2709 uint64_t SizeDiff =
2710 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2711
2712 if (SizeDiff <= MinShAmt) {
2713 unsigned Tmp =
2714 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2715 if (MaxShAmt < Tmp)
2716 return Tmp - MaxShAmt;
2717 }
2718 }
2719 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2720 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2721 if (MaxShAmt < Tmp)
2722 return Tmp - MaxShAmt;
2723 }
2724 break;
2725 }
2726 case TargetOpcode::G_ROTL:
2727 case TargetOpcode::G_ROTR: {
2728 Register SrcReg = MI.getOperand(1).getReg();
2729 unsigned Tmp = computeNumSignBits(SrcReg, DemandedElts, Depth + 1);
2730 auto MaybeAmt =
2731 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
2732 FirstAnswer =
2733 SignBitsOps::rot(Tmp, TyBits, MaybeAmt, Opcode == TargetOpcode::G_ROTR);
2734 break;
2735 }
2736 case TargetOpcode::G_SAVGFLOOR:
2737 case TargetOpcode::G_SAVGCEIL: {
2738 Register Src1 = MI.getOperand(1).getReg();
2739 Register Src2 = MI.getOperand(2).getReg();
2740 FirstAnswer = computeNumSignBitsMin(Src1, Src2, DemandedElts, Depth + 1);
2741 break;
2742 }
2743 case TargetOpcode::G_SREM: {
2744 // The sign bit is the LHS's sign bit, except when the result of the
2745 // remainder is zero. The magnitude of the result should be less than or
2746 // equal to the magnitude of the LHS. Therefore, the result should have
2747 // at least as many sign bits as the left hand side.
2748 Register Src = MI.getOperand(1).getReg();
2749 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2750 }
2751 case TargetOpcode::G_TRUNC: {
2752 Register Src = MI.getOperand(1).getReg();
2753 LLT SrcTy = MRI.getType(Src);
2754
2755 // Check if the sign bits of source go down as far as the truncated value.
2756 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2757 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2758 if (NumSrcSignBits > (NumSrcBits - TyBits))
2759 return NumSrcSignBits - (NumSrcBits - TyBits);
2760 break;
2761 }
2762 case TargetOpcode::G_SELECT: {
2763 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2764 MI.getOperand(3).getReg(), DemandedElts,
2765 Depth + 1);
2766 }
2767 case TargetOpcode::G_SMIN:
2768 case TargetOpcode::G_SMAX:
2769 case TargetOpcode::G_UMIN:
2770 case TargetOpcode::G_UMAX:
2771 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2772 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2773 MI.getOperand(2).getReg(), DemandedElts,
2774 Depth + 1);
2775 case TargetOpcode::G_SADDO:
2776 case TargetOpcode::G_SADDE:
2777 case TargetOpcode::G_UADDO:
2778 case TargetOpcode::G_UADDE:
2779 case TargetOpcode::G_SSUBO:
2780 case TargetOpcode::G_SSUBE:
2781 case TargetOpcode::G_USUBO:
2782 case TargetOpcode::G_USUBE:
2783 case TargetOpcode::G_SMULO:
2784 case TargetOpcode::G_UMULO: {
2785 // If compares returns 0/-1, all bits are sign bits.
2786 // We know that we have an integer-based boolean since these operations
2787 // are only available for integer.
2788 if (MI.getOperand(1).getReg() == R) {
2789 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2791 return TyBits;
2792 }
2793
2794 break;
2795 }
2796 case TargetOpcode::G_SUB: {
2797 Register Src2 = MI.getOperand(2).getReg();
2798 unsigned Src2NumSignBits =
2799 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2800 if (Src2NumSignBits == 1)
2801 return 1; // Early out.
2802
2803 // Handle NEG.
2804 Register Src1 = MI.getOperand(1).getReg();
2805 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2806 if (Known1.isZero()) {
2807 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2808 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2809 // sign bits set.
2810 if ((Known2.Zero | 1).isAllOnes())
2811 return TyBits;
2812
2813 // If the input is known to be positive (the sign bit is known clear),
2814 // the output of the NEG has, at worst, the same number of sign bits as
2815 // the input.
2816 if (Known2.isNonNegative()) {
2817 FirstAnswer = Src2NumSignBits;
2818 break;
2819 }
2820
2821 // Otherwise, we treat this like a SUB.
2822 }
2823
2824 unsigned Src1NumSignBits =
2825 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2826 if (Src1NumSignBits == 1)
2827 return 1; // Early Out.
2828
2829 // Sub can have at most one carry bit. Thus we know that the output
2830 // is, at worst, one more bit than the inputs.
2831 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2832 break;
2833 }
2834 case TargetOpcode::G_ADD: {
2835 Register Src2 = MI.getOperand(2).getReg();
2836 unsigned Src2NumSignBits =
2837 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2838 if (Src2NumSignBits <= 2)
2839 return 1; // Early out.
2840
2841 Register Src1 = MI.getOperand(1).getReg();
2842 unsigned Src1NumSignBits =
2843 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2844 if (Src1NumSignBits == 1)
2845 return 1; // Early Out.
2846
2847 // Special case decrementing a value (ADD X, -1):
2848 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2849 if (Known2.isAllOnes()) {
2850 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2851 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2852 // sign bits set.
2853 if ((Known1.Zero | 1).isAllOnes())
2854 return TyBits;
2855
2856 // If we are subtracting one from a positive number, there is no carry
2857 // out of the result.
2858 if (Known1.isNonNegative()) {
2859 FirstAnswer = Src1NumSignBits;
2860 break;
2861 }
2862
2863 // Otherwise, we treat this like an ADD.
2864 }
2865
2866 // Add can have at most one carry bit. Thus we know that the output
2867 // is, at worst, one more bit than the inputs.
2868 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2869 break;
2870 }
2871 case TargetOpcode::G_MUL: {
2872 unsigned Src2NumSignBits =
2873 computeNumSignBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
2874 if (Src2NumSignBits == 1)
2875 break;
2876 unsigned Src1NumSignBits =
2877 computeNumSignBits(MI.getOperand(1).getReg(), DemandedElts, Depth + 1);
2878 if (Src1NumSignBits == 1)
2879 break;
2880
2881 // The product needs at most the sum of the operands' signed widths.
2882 unsigned OutValidBits =
2883 (TyBits - Src1NumSignBits + 1) + (TyBits - Src2NumSignBits + 1);
2884 if (OutValidBits <= TyBits)
2885 FirstAnswer = TyBits - OutValidBits + 1;
2886 break;
2887 }
2888 case TargetOpcode::G_FCMP:
2889 case TargetOpcode::G_ICMP: {
2890 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2891 if (TyBits == 1)
2892 break;
2893 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2895 return TyBits; // All bits are sign bits.
2897 return TyBits - 1; // Every always-zero bit is a sign bit.
2898 break;
2899 }
2900 case TargetOpcode::G_UNMERGE_VALUES: {
2901 unsigned NumOps = MI.getNumOperands();
2902 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
2903 LLT SrcTy = MRI.getType(SrcReg);
2904
2905 if ((SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType()) ||
2906 (SrcTy.isScalar() && DstTy.isVector()))
2907 break;
2908
2909 // Figure out the result operand index
2910 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
2911
2912 APInt SubDemandedElts = DemandedElts;
2913 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
2914 if (SrcTy.isVector()) {
2915 SubDemandedElts =
2916 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
2917 }
2918
2919 unsigned SrcOpKnown =
2920 computeNumSignBits(SrcReg, SubDemandedElts, Depth + 1);
2921 if (SrcTy.isVector()) {
2922 FirstAnswer = SrcOpKnown;
2923 } else if (SrcOpKnown >= (MI.getNumOperands() - DstIdx - 2) * TyBits) {
2924 FirstAnswer = SrcOpKnown >= (MI.getNumOperands() - DstIdx - 1) * TyBits
2925 ? TyBits
2926 : SrcOpKnown % TyBits;
2927 }
2928 break;
2929 }
2930 case TargetOpcode::G_BUILD_VECTOR: {
2931 // Collect the known bits that are shared by every demanded vector element.
2932 FirstAnswer = TyBits;
2933 APInt SingleDemandedElt(1, 1);
2934 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2935 if (!DemandedElts[I])
2936 continue;
2937
2938 unsigned Tmp2 =
2939 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2940 FirstAnswer = std::min(FirstAnswer, Tmp2);
2941
2942 // If we don't know any bits, early out.
2943 if (FirstAnswer == 1)
2944 break;
2945 }
2946 break;
2947 }
2948 case TargetOpcode::G_CONCAT_VECTORS: {
2949 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2950 break;
2951 FirstAnswer = TyBits;
2952 // Determine the minimum number of sign bits across all demanded
2953 // elts of the input vectors. Early out if the result is already 1.
2954 unsigned NumSubVectorElts =
2955 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2956 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2957 APInt DemandedSub =
2958 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2959 if (!DemandedSub)
2960 continue;
2961 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2962
2963 FirstAnswer = std::min(FirstAnswer, Tmp2);
2964
2965 // If we don't know any bits, early out.
2966 if (FirstAnswer == 1)
2967 break;
2968 }
2969 break;
2970 }
2971 case TargetOpcode::G_VECTOR_COMPRESS: {
2972 // Each result lane is either a lane of the source vector or the passthru,
2973 // so the number of sign bits is the minimum of the two.
2974 Register Vec = MI.getOperand(1).getReg();
2975 Register PassThru = MI.getOperand(3).getReg();
2976 unsigned Tmp = computeNumSignBits(PassThru, DemandedElts, Depth + 1);
2977 // If passthru contributes nothing, fall back to the KnownBits refinement.
2978 if (Tmp == 1)
2979 break;
2980 // Compression can move any source lane to any result position, so all
2981 // source lanes are demanded.
2982 APInt DemandedSrcElts = APInt::getAllOnes(DemandedElts.getBitWidth());
2983 unsigned Tmp2 = computeNumSignBits(Vec, DemandedSrcElts, Depth + 1);
2984 FirstAnswer = std::min(Tmp, Tmp2);
2985 break;
2986 }
2987 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2989 Register InVec = Insert.getVectorReg();
2990 Register InVal = Insert.getElementReg();
2991 LLT VecVT = MRI.getType(InVec);
2992
2993 // If we know the element index, split the demand between the inserted
2994 // value and the source vector, otherwise assume we need both. Scalable
2995 // vectors carry no per-lane demand, so they always take the minimum of the
2996 // whole vector and the inserted value.
2997 bool DemandedVal = true;
2998 APInt DemandedVecElts = DemandedElts;
2999 if (!VecVT.isScalableVector()) {
3000 unsigned NumElts = VecVT.getNumElements();
3001 auto ConstEltNo = getIConstantVRegVal(Insert.getIndexReg(), MRI);
3002 if (ConstEltNo && ConstEltNo->ult(NumElts)) {
3003 unsigned EltIdx = ConstEltNo->getZExtValue();
3004 DemandedVal = !!DemandedElts[EltIdx];
3005 DemandedVecElts.clearBit(EltIdx);
3006 }
3007 }
3008
3009 unsigned Tmp = TyBits;
3010 if (DemandedVal) {
3011 // TODO: Handle implicit truncation of inserted elements.
3012 if (MRI.getType(InVal).getSizeInBits() != TyBits)
3013 break;
3014 unsigned ValSignBits = computeNumSignBits(InVal, APInt(1, 1), Depth + 1);
3015 Tmp = std::min(Tmp, ValSignBits);
3016 }
3017 if (!!DemandedVecElts) {
3018 unsigned VecSignBits =
3019 computeNumSignBits(InVec, DemandedVecElts, Depth + 1);
3020 Tmp = std::min(Tmp, VecSignBits);
3021 }
3022 return Tmp;
3023 }
3024 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
3026 Register InVec = Extract.getVectorReg();
3027 Register EltNo = Extract.getIndexReg();
3028 LLT VecVT = MRI.getType(InVec);
3029 if (VecVT.isScalableVector())
3030 return computeNumSignBits(InVec, APInt(1, 1), Depth + 1);
3031 unsigned NumSrcElts = VecVT.getNumElements();
3032 std::optional<APInt> ConstEltNo = getIConstantVRegVal(EltNo, MRI);
3033 APInt DemandedSrcElts =
3034 ConstEltNo && ConstEltNo->ult(NumSrcElts)
3035 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
3036 : APInt::getAllOnes(NumSrcElts);
3037 return computeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
3038 }
3039 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
3040 // Offset the demanded elts by the subvector index.
3041 Register SrcReg = MI.getOperand(1).getReg();
3042 LLT SrcTy = MRI.getType(SrcReg);
3043 APInt DemandedSrcElts;
3044 if (SrcTy.isScalableVector()) {
3045 DemandedSrcElts = APInt(1, 1);
3046 } else {
3047 uint64_t Idx = MI.getOperand(2).getImm();
3048 unsigned NumSrcElts = SrcTy.getNumElements();
3049 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3050 }
3051 return computeNumSignBits(SrcReg, DemandedSrcElts, Depth + 1);
3052 }
3053 case TargetOpcode::G_SHUFFLE_VECTOR: {
3054 // Collect the minimum number of sign bits that are shared by every vector
3055 // element referenced by the shuffle.
3056 APInt DemandedLHS, DemandedRHS;
3057 Register Src1 = MI.getOperand(1).getReg();
3058 unsigned NumElts = MRI.getType(Src1).getNumElements();
3059 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
3060 DemandedElts, DemandedLHS, DemandedRHS))
3061 return 1;
3062
3063 if (!!DemandedLHS)
3064 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
3065 // If we don't know anything, early out and try computeKnownBits fall-back.
3066 if (FirstAnswer == 1)
3067 break;
3068 if (!!DemandedRHS) {
3069 unsigned Tmp2 =
3070 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
3071 FirstAnswer = std::min(FirstAnswer, Tmp2);
3072 }
3073 break;
3074 }
3075 case TargetOpcode::G_SPLAT_VECTOR: {
3076 // Check if the sign bits of source go down as far as the truncated value.
3077 Register Src = MI.getOperand(1).getReg();
3078 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
3079 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
3080 if (NumSrcSignBits > (NumSrcBits - TyBits))
3081 return NumSrcSignBits - (NumSrcBits - TyBits);
3082 break;
3083 }
3084 case TargetOpcode::G_INTRINSIC:
3085 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
3086 case TargetOpcode::G_INTRINSIC_CONVERGENT:
3087 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
3088 default: {
3089 unsigned NumBits =
3090 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
3091 if (NumBits > 1)
3092 FirstAnswer = std::max(FirstAnswer, NumBits);
3093 break;
3094 }
3095 }
3096
3097 // Finally, if we can prove that the top bits of the result are 0's or 1's,
3098 // use this information.
3099 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
3100 return std::max(FirstAnswer, Known.countMinSignBits());
3101}
3102
3104 LLT Ty = MRI.getType(R);
3105 APInt DemandedElts =
3106 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
3107 return computeNumSignBits(R, DemandedElts, Depth);
3108}
3109
3111 Register R, const APInt &DemandedElts, unsigned Depth) {
3112 // Shifting more than the bitwidth is not valid.
3113 MachineInstr &MI = *MRI.getVRegDef(R);
3114 unsigned Opcode = MI.getOpcode();
3115
3116 LLT Ty = MRI.getType(R);
3117 unsigned BitWidth = Ty.getScalarSizeInBits();
3118
3119 if (Opcode == TargetOpcode::G_CONSTANT) {
3120 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
3121 if (ShAmt.uge(BitWidth))
3122 return std::nullopt;
3123 return ConstantRange(ShAmt);
3124 }
3125
3126 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
3127 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3128 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
3129 if (!DemandedElts[I])
3130 continue;
3131 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
3132 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
3133 MinAmt = MaxAmt = nullptr;
3134 break;
3135 }
3136
3137 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
3138 if (ShAmt.uge(BitWidth))
3139 return std::nullopt;
3140 if (!MinAmt || MinAmt->ugt(ShAmt))
3141 MinAmt = &ShAmt;
3142 if (!MaxAmt || MaxAmt->ult(ShAmt))
3143 MaxAmt = &ShAmt;
3144 }
3145 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3146 "Failed to find matching min/max shift amounts");
3147 if (MinAmt && MaxAmt)
3148 return ConstantRange(*MinAmt, *MaxAmt + 1);
3149 }
3150
3151 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3152 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3153 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
3154 if (KnownAmt.getMaxValue().ult(BitWidth))
3155 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3156
3157 return std::nullopt;
3158}
3159
3161 Register R, const APInt &DemandedElts, unsigned Depth) {
3162 if (std::optional<ConstantRange> AmtRange =
3163 getValidShiftAmountRange(R, DemandedElts, Depth))
3164 return AmtRange->getUnsignedMin().getZExtValue();
3165 return std::nullopt;
3166}
3167
3173
3178
3180 if (!Info) {
3181 unsigned MaxDepth =
3183 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
3184 }
3185 return *Info;
3186}
3187
3188AnalysisKey GISelValueTrackingAnalysis::Key;
3189
3193 unsigned MaxDepth =
3195 return Result(MF, MaxDepth);
3196}
3197
3198static PreservedAnalyses
3201 bool PrintFPClass) {
3202 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
3203 const auto &MRI = MF.getRegInfo();
3204 OS << "name: ";
3205 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
3206 OS << '\n';
3207
3208 for (MachineBasicBlock &BB : MF) {
3209 for (MachineInstr &MI : BB) {
3210 for (MachineOperand &MO : MI.defs()) {
3211 if (!MO.isReg() || MO.getReg().isPhysical())
3212 continue;
3213 Register Reg = MO.getReg();
3214 if (!MRI.getType(Reg).isValid())
3215 continue;
3216 if (PrintFPClass) {
3217 KnownFPClass FPKnown = VTA.computeKnownFPClass(Reg);
3218 OS << " " << MO << " FPClasses:" << FPKnown.getKnownFPClasses()
3219 << " SignBitKnown:";
3220 if (FPKnown.getSignBit())
3221 OS << (*FPKnown.getSignBit() ? '1' : '0');
3222 else
3223 OS << '?';
3224 OS << '\n';
3225 } else {
3226 KnownBits Known = VTA.getKnownBits(Reg);
3227 unsigned SignedBits = VTA.computeNumSignBits(Reg);
3228 bool IsKnownNeverZero = VTA.isKnownNeverZero(Reg);
3229 OS << " " << MO << " KnownBits:" << Known
3230 << " SignBits:" << SignedBits
3231 << " IsKnownNeverZero:" << IsKnownNeverZero << '\n';
3232 }
3233 };
3234 }
3235 }
3236 return PreservedAnalyses::all();
3237}
3238
3244
MachineInstrBuilder MachineInstrBuilder & DefMI
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:857
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
const SmallVectorImpl< MachineOperand > & Cond
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 const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:310
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1242
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2009
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:2044
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.
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:1081
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,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
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:3226
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:110
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:1518
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:297
@ 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:2570
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:1692
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
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
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
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 sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:184
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:54
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
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:294
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.
void setKnownFPClasses(FPClassTest Classes)
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass frem(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass ceil(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ceil.
static LLVM_ABI KnownFPClass fadd_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd x, x.
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
static LLVM_ABI KnownFPClass fsub(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fsub.
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for atan2.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
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.
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
static LLVM_ABI KnownFPClass frexp_mant(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for mantissa component of frexp.
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.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
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 fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
FPClassTest getKnownFPClasses() const
Floating-point classes the value could be one of.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for rounding intrinsics (rint, nearbyint, round, roundeven).
static LLVM_ABI KnownFPClass fma(const KnownFPClass &LHS, const KnownFPClass &RHS, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma.
static LLVM_ABI KnownFPClass tan(const KnownFPClass &Src)
Report known values for tan.
static LLVM_ABI KnownFPClass fptrunc(const KnownFPClass &KnownSrc)
Propagate known class for fptrunc.
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 sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
static LLVM_ABI KnownFPClass fadd(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fadd.
static LLVM_ABI KnownFPClass trunc(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for trunc.
static LLVM_ABI KnownFPClass fma_square(const KnownFPClass &Squared, const KnownFPClass &Addend, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fma squared, squared, addend.
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=DenormalMode::getDynamic())
Report known values for frem x, x.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
static LLVM_ABI KnownFPClass floor(const KnownFPClass &Src, bool IsMultiUnitFPType, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for floor.
static LLVM_ABI KnownFPClass pow(const KnownFPClass &LHS, const KnownFPClass &RHS)
Propagate known class for pow.
static LLVM_ABI KnownFPClass ldexp(const KnownFPClass &Src, const APInt &ConstantRangeMin, const APInt &ConstantRangeMax, const fltSemantics &Flt, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for ldexp, assuming the exponent is known to be within [ConstantRangeMin,...
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.