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 assert(MI.getNumExplicitDefs() == 1 &&
80 "expected single return generic instruction");
81 return getKnownBits(MI.getOperand(0).getReg());
82}
83
85 const LLT Ty = MRI.getType(R);
86 // Since the number of lanes in a scalable vector is unknown at compile time,
87 // we track one bit which is implicitly broadcast to all lanes. This means
88 // that all lanes in a scalable vector are considered demanded.
89 APInt DemandedElts =
90 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
91 return getKnownBits(R, DemandedElts);
92}
93
95 const APInt &DemandedElts,
96 unsigned Depth) {
98 computeKnownBitsImpl(R, Known, DemandedElts, Depth);
99 return Known;
100}
101
103 LLT Ty = MRI.getType(R);
104 unsigned BitWidth = Ty.getScalarSizeInBits();
106}
107
109 LLT Ty = MRI.getType(R);
110 const APInt ScalarDemandedElts(1, 1);
111 APInt DemandedElts = Ty.isFixedVector()
112 ? APInt::getAllOnes(Ty.getNumElements())
113 : ScalarDemandedElts;
114 return isKnownNeverZero(R, DemandedElts, Depth);
115}
116
118 unsigned Depth) {
119 if (Depth >= getMaxDepth())
120 return false;
121
122 const APInt ScalarDemandedElts(1, 1);
123 MachineInstr &MI = *MRI.getVRegDef(R);
124
125 switch (MI.getOpcode()) {
126 default:
127 break;
128
129 case TargetOpcode::G_BUILD_VECTOR: {
130 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
131 if (!DemandedElts[I])
132 continue;
133 if (!isKnownNeverZero(MO.getReg(), ScalarDemandedElts, Depth + 1))
134 return false;
135 }
136 return true;
137 }
138
139 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
141 Register InVec = Extract.getVectorReg();
142 LLT VecTy = MRI.getType(InVec);
143 if (VecTy.isScalableVector())
144 break;
145 unsigned NumSrcElts = VecTy.getNumElements();
146 // An out-of-range constant index produces poison. Keep all lanes demanded,
147 // which is poison-safe and matches SelectionDAG's conservative behavior.
148 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
149 if (auto Idx = getIConstantVRegVal(Extract.getIndexReg(), MRI)) {
150 if (Idx->ult(NumSrcElts))
151 DemandedSrcElts = APInt::getOneBitSet(NumSrcElts, Idx->getZExtValue());
152 }
153 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
154 }
155
156 case TargetOpcode::G_SHUFFLE_VECTOR: {
158 LLT SrcTy = MRI.getType(Shuf.getSrc1Reg());
159 if (SrcTy.isScalableVector())
160 break;
161 APInt DemandedLHS, DemandedRHS;
162 if (!getShuffleDemandedElts(SrcTy.getNumElements(), Shuf.getMask(),
163 DemandedElts, DemandedLHS, DemandedRHS))
164 break;
165 if (!DemandedLHS.isZero() &&
166 !isKnownNeverZero(Shuf.getSrc1Reg(), DemandedLHS, Depth + 1))
167 return false;
168 if (!DemandedRHS.isZero() &&
169 !isKnownNeverZero(Shuf.getSrc2Reg(), DemandedRHS, Depth + 1))
170 return false;
171 return true;
172 }
173
174 case TargetOpcode::G_OR:
175 return isKnownNeverZero(MI.getOperand(1).getReg(), DemandedElts,
176 Depth + 1) ||
177 isKnownNeverZero(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
178
179 case TargetOpcode::G_SELECT:
180 return isKnownNeverZero(MI.getOperand(2).getReg(), DemandedElts,
181 Depth + 1) &&
182 isKnownNeverZero(MI.getOperand(3).getReg(), DemandedElts, Depth + 1);
183
184 case TargetOpcode::G_SHL: {
185 Register LHSReg = MI.getOperand(1).getReg();
186 if (MI.getFlag(MachineInstr::NoSWrap) || MI.getFlag(MachineInstr::NoUWrap))
187 return isKnownNeverZero(LHSReg, DemandedElts, Depth + 1);
188 KnownBits ValKnown = getKnownBits(LHSReg, DemandedElts, Depth + 1);
189 if (ValKnown.One[0])
190 return true;
191 APInt MaxCnt =
192 getKnownBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1)
193 .getMaxValue();
194 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
195 !ValKnown.One.shl(MaxCnt).isZero())
196 return true;
197 break;
198 }
199 }
200
201 // Pass through this frame's Depth (not Depth+1) because we have not recursed
202 // into a child MI here: the fallback queries KnownBits for the same R.
203 return getKnownBits(R, DemandedElts, Depth).isNonZero();
204}
205
209
213
214[[maybe_unused]] static void
215dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) {
216 dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth
217 << "] Computed for: " << MI << "[" << Depth << "] Known: 0x"
218 << toString(Known.Zero | Known.One, 16, false) << "\n"
219 << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false)
220 << "\n"
221 << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false)
222 << "\n";
223}
224
225/// Compute known bits for the intersection of \p Src0 and \p Src1
226void GISelValueTracking::computeKnownBitsMin(Register Src0, Register Src1,
228 const APInt &DemandedElts,
229 unsigned Depth) {
230 // Test src1 first, since we canonicalize simpler expressions to the RHS.
231 computeKnownBitsImpl(Src1, Known, DemandedElts, Depth);
232
233 // If we don't know any bits, early out.
234 if (Known.isUnknown())
235 return;
236
237 KnownBits Known2;
238 computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
239
240 // Only known if known in both the LHS and RHS.
241 Known = Known.intersectWith(Known2);
242}
243
244// Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
245// created using Width. Use this function when the inputs are KnownBits
246// objects. TODO: Move this KnownBits.h if this is usable in more cases.
247static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown,
248 const KnownBits &OffsetKnown,
249 const KnownBits &WidthKnown) {
250 KnownBits Mask(BitWidth);
251 Mask.Zero = APInt::getBitsSetFrom(
253 Mask.One = APInt::getLowBitsSet(
255 return KnownBits::lshr(SrcOpKnown, OffsetKnown) & Mask;
256}
257
259 const APInt &DemandedElts,
260 unsigned Depth) {
261 MachineInstr &MI = *MRI.getVRegDef(R);
262 unsigned Opcode = MI.getOpcode();
263 LLT DstTy = MRI.getType(R);
264
265 // Handle the case where this is called on a register that does not have a
266 // type constraint. For example, it may be post-ISel or this target might not
267 // preserve the type when early-selecting instructions.
268 if (!DstTy.isValid()) {
269 Known = KnownBits();
270 return;
271 }
272
273#ifndef NDEBUG
274 if (DstTy.isFixedVector()) {
275 assert(
276 DstTy.getNumElements() == DemandedElts.getBitWidth() &&
277 "DemandedElt width should equal the fixed vector number of elements");
278 } else {
279 assert(DemandedElts.getBitWidth() == 1 && DemandedElts == APInt(1, 1) &&
280 "DemandedElt width should be 1 for scalars or scalable vectors");
281 }
282#endif
283
284 unsigned BitWidth = DstTy.getScalarSizeInBits();
285 Known = KnownBits(BitWidth); // Don't know anything
286
287 // Depth may get bigger than max depth if it gets passed to a different
288 // GISelValueTracking object.
289 // This may happen when say a generic part uses a GISelValueTracking object
290 // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr
291 // which creates a new GISelValueTracking object with a different and smaller
292 // depth. If we just check for equality, we would never exit if the depth
293 // that is passed down to the target specific GISelValueTracking object is
294 // already bigger than its max depth.
295 if (Depth >= getMaxDepth())
296 return;
297
298 if (!DemandedElts)
299 return; // No demanded elts, better to assume we don't know anything.
300
301 KnownBits Known2;
302
303 switch (Opcode) {
304 default:
305 TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI,
306 Depth);
307 break;
308 case TargetOpcode::G_BUILD_VECTOR: {
309 // Collect the known bits that are shared by every demanded vector element.
310 Known.Zero.setAllBits();
311 Known.One.setAllBits();
312 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
313 if (!DemandedElts[I])
314 continue;
315
316 computeKnownBitsImpl(MO.getReg(), Known2, APInt(1, 1), Depth + 1);
317
318 // Known bits are the values that are shared by every demanded element.
319 Known = Known.intersectWith(Known2);
320
321 // If we don't know any bits, early out.
322 if (Known.isUnknown())
323 break;
324 }
325 break;
326 }
327 case TargetOpcode::G_SPLAT_VECTOR: {
328 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, APInt(1, 1),
329 Depth + 1);
330 // Implicitly truncate the bits to match the official semantics of
331 // G_SPLAT_VECTOR.
332 Known = Known.trunc(BitWidth);
333 break;
334 }
335 case TargetOpcode::COPY:
336 case TargetOpcode::G_PHI:
337 case TargetOpcode::PHI: {
340 // Destination registers should not have subregisters at this
341 // point of the pipeline, otherwise the main live-range will be
342 // defined more than once, which is against SSA.
343 assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?");
344 // PHI's operand are a mix of registers and basic blocks interleaved.
345 // We only care about the register ones.
346 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
347 const MachineOperand &Src = MI.getOperand(Idx);
348 Register SrcReg = Src.getReg();
349 LLT SrcTy = MRI.getType(SrcReg);
350 // Look through trivial copies and phis but don't look through trivial
351 // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits
352 // analysis is currently unable to determine the bit width of a
353 // register class.
354 //
355 // We can't use NoSubRegister by name as it's defined by each target but
356 // it's always defined to be 0 by tablegen.
357 if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ &&
358 SrcTy.isValid()) {
359 APInt NowDemandedElts;
360 if (!SrcTy.isFixedVector()) {
361 NowDemandedElts = APInt(1, 1);
362 } else if (DstTy.isFixedVector() &&
363 SrcTy.getNumElements() == DstTy.getNumElements()) {
364 NowDemandedElts = DemandedElts;
365 } else {
366 NowDemandedElts = APInt::getAllOnes(SrcTy.getNumElements());
367 }
368
369 // For COPYs we don't do anything, don't increase the depth.
370 computeKnownBitsImpl(SrcReg, Known2, NowDemandedElts,
371 Depth + (Opcode != TargetOpcode::COPY));
372 Known2 = Known2.anyextOrTrunc(BitWidth);
373 Known = Known.intersectWith(Known2);
374 // If we reach a point where we don't know anything
375 // just stop looking through the operands.
376 if (Known.isUnknown())
377 break;
378 } else {
379 // We know nothing.
381 break;
382 }
383 }
384 break;
385 }
386 case TargetOpcode::G_STEP_VECTOR: {
387 APInt Step = MI.getOperand(1).getCImm()->getValue();
388
389 if (Step.isPowerOf2())
390 Known.Zero.setLowBits(Step.logBase2());
391
393 break;
394
395 const APInt MinNumElts =
398 bool Overflow;
399 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
401 .umul_ov(MinNumElts, Overflow);
402 if (Overflow)
403 break;
404 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
405 if (Overflow)
406 break;
407 Known.Zero.setHighBits(MaxValue.countl_zero());
408 break;
409 }
410 case TargetOpcode::G_VSCALE: {
412 const APInt &Multiplier = MI.getOperand(1).getCImm()->getValue();
414 break;
415 }
416 case TargetOpcode::G_CONSTANT: {
417 Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
418 break;
419 }
420 case TargetOpcode::G_FRAME_INDEX: {
421 int FrameIdx = MI.getOperand(1).getIndex();
422 TL.computeKnownBitsForStackObjectPointer(
423 Known, MF, MF.getFrameInfo().getObjectAlign(FrameIdx));
424 break;
425 }
426 case TargetOpcode::G_SUB: {
427 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
428 Depth + 1);
429 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
430 Depth + 1);
432 MI.getFlag(MachineInstr::NoUWrap));
433 break;
434 }
435 case TargetOpcode::G_XOR: {
436 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
437 Depth + 1);
438 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
439 Depth + 1);
440
441 Known ^= Known2;
442 break;
443 }
444 case TargetOpcode::G_PTR_ADD: {
445 if (DstTy.isVector())
446 break;
447 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets?
448 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
449 if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace()))
450 break;
451 [[fallthrough]];
452 }
453 case TargetOpcode::G_ADD: {
454 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
455 Depth + 1);
456 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
457 Depth + 1);
458 Known = KnownBits::add(Known, Known2);
459 break;
460 }
461 case TargetOpcode::G_AND: {
462 // If either the LHS or the RHS are Zero, the result is zero.
463 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
464 Depth + 1);
465 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
466 Depth + 1);
467
468 Known &= Known2;
469 break;
470 }
471 case TargetOpcode::G_OR: {
472 // If either the LHS or the RHS are Zero, the result is zero.
473 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
474 Depth + 1);
475 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
476 Depth + 1);
477
478 Known |= Known2;
479 break;
480 }
481 case TargetOpcode::G_MUL: {
482 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
483 Depth + 1);
484 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
485 Depth + 1);
486 Known = KnownBits::mul(Known, Known2);
487 break;
488 }
489 case TargetOpcode::G_UMULH: {
490 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
491 Depth + 1);
492 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
493 Depth + 1);
494 Known = KnownBits::mulhu(Known, Known2);
495 break;
496 }
497 case TargetOpcode::G_SMULH: {
498 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
499 Depth + 1);
500 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
501 Depth + 1);
502 Known = KnownBits::mulhs(Known, Known2);
503 break;
504 }
505 case TargetOpcode::G_UAVGFLOOR: {
506 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
507 Depth + 1);
508 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
509 Depth + 1);
511 break;
512 }
513 case TargetOpcode::G_UAVGCEIL: {
514 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
515 Depth + 1);
516 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
517 Depth + 1);
519 break;
520 }
521 case TargetOpcode::G_SAVGFLOOR: {
522 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
523 Depth + 1);
524 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
525 Depth + 1);
527 break;
528 }
529 case TargetOpcode::G_SAVGCEIL: {
530 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
531 Depth + 1);
532 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
533 Depth + 1);
535 break;
536 }
537 case TargetOpcode::G_ABDU: {
538 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
539 Depth + 1);
540 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
541 Depth + 1);
542 Known = KnownBits::abdu(Known, Known2);
543 break;
544 }
545 case TargetOpcode::G_ABDS: {
546 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
547 Depth + 1);
548 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
549 Depth + 1);
550 Known = KnownBits::abds(Known, Known2);
551
552 unsigned SignBits1 =
553 computeNumSignBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
554 if (SignBits1 == 1) {
555 break;
556 }
557 unsigned SignBits0 =
558 computeNumSignBits(MI.getOperand(1).getReg(), DemandedElts, Depth + 1);
559
560 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
561 break;
562 }
563 case TargetOpcode::G_SADDSAT: {
564 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
565 Depth + 1);
566 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
567 Depth + 1);
569 break;
570 }
571 case TargetOpcode::G_UADDSAT: {
572 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
573 Depth + 1);
574 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
575 Depth + 1);
577 break;
578 }
579 case TargetOpcode::G_SSUBSAT: {
580 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
581 Depth + 1);
582 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
583 Depth + 1);
585 break;
586 }
587 case TargetOpcode::G_USUBSAT: {
588 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
589 Depth + 1);
590 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
591 Depth + 1);
593 break;
594 }
595 case TargetOpcode::G_UDIV: {
596 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
597 Depth + 1);
598 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
599 Depth + 1);
600 Known = KnownBits::udiv(Known, Known2,
602 break;
603 }
604 case TargetOpcode::G_SDIV: {
605 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
606 Depth + 1);
607 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
608 Depth + 1);
609 Known = KnownBits::sdiv(Known, Known2,
611 break;
612 }
613 case TargetOpcode::G_UREM: {
614 KnownBits LHSKnown(Known.getBitWidth());
615 KnownBits RHSKnown(Known.getBitWidth());
616
617 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
618 Depth + 1);
619 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
620 Depth + 1);
621
622 Known = KnownBits::urem(LHSKnown, RHSKnown);
623 break;
624 }
625 case TargetOpcode::G_SREM: {
626 KnownBits LHSKnown(Known.getBitWidth());
627 KnownBits RHSKnown(Known.getBitWidth());
628
629 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
630 Depth + 1);
631 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
632 Depth + 1);
633
634 Known = KnownBits::srem(LHSKnown, RHSKnown);
635 break;
636 }
637 case TargetOpcode::G_SELECT: {
638 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
639 Known, DemandedElts, Depth + 1);
640 break;
641 }
642 case TargetOpcode::G_SMIN: {
643 // TODO: Handle clamp pattern with number of sign bits
644 KnownBits KnownRHS;
645 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
646 Depth + 1);
647 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
648 Depth + 1);
649 Known = KnownBits::smin(Known, KnownRHS);
650 break;
651 }
652 case TargetOpcode::G_SMAX: {
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::smax(Known, KnownRHS);
660 break;
661 }
662 case TargetOpcode::G_UMIN: {
663 KnownBits KnownRHS;
664 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
665 Depth + 1);
666 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
667 Depth + 1);
668 Known = KnownBits::umin(Known, KnownRHS);
669 break;
670 }
671 case TargetOpcode::G_UMAX: {
672 KnownBits KnownRHS;
673 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
674 Depth + 1);
675 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
676 Depth + 1);
677 Known = KnownBits::umax(Known, KnownRHS);
678 break;
679 }
680 case TargetOpcode::G_FCMP:
681 case TargetOpcode::G_ICMP: {
682 if (DstTy.isVector())
683 break;
684 if (TL.getBooleanContents(DstTy.isVector(),
685 Opcode == TargetOpcode::G_FCMP) ==
687 BitWidth > 1)
688 Known.Zero.setBitsFrom(1);
689 break;
690 }
691 case TargetOpcode::G_SEXT: {
692 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
693 Depth + 1);
694 // If the sign bit is known to be zero or one, then sext will extend
695 // it to the top bits, else it will just zext.
696 Known = Known.sext(BitWidth);
697 break;
698 }
699 case TargetOpcode::G_ASSERT_SEXT:
700 case TargetOpcode::G_SEXT_INREG: {
701 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
702 Depth + 1);
703 Known = Known.sextInReg(MI.getOperand(2).getImm());
704 break;
705 }
706 case TargetOpcode::G_ANYEXT: {
707 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
708 Depth + 1);
709 Known = Known.anyext(BitWidth);
710 break;
711 }
712 case TargetOpcode::G_LOAD: {
713 const MachineMemOperand *MMO = *MI.memoperands_begin();
714 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
715 if (const MDNode *Ranges = MMO->getRanges())
716 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
717 Known = KnownRange.anyext(Known.getBitWidth());
718 break;
719 }
720 case TargetOpcode::G_SEXTLOAD:
721 case TargetOpcode::G_ZEXTLOAD: {
722 if (DstTy.isVector())
723 break;
724 const MachineMemOperand *MMO = *MI.memoperands_begin();
725 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
726 if (const MDNode *Ranges = MMO->getRanges())
727 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
728 Known = Opcode == TargetOpcode::G_SEXTLOAD
729 ? KnownRange.sext(Known.getBitWidth())
730 : KnownRange.zext(Known.getBitWidth());
731 break;
732 }
733 case TargetOpcode::G_ASHR: {
734 KnownBits LHSKnown, RHSKnown;
735 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
736 Depth + 1);
737 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
738 Depth + 1);
739 Known = KnownBits::ashr(LHSKnown, RHSKnown);
740 break;
741 }
742 case TargetOpcode::G_LSHR: {
743 KnownBits LHSKnown, RHSKnown;
744 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
745 Depth + 1);
746 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
747 Depth + 1);
748 Known = KnownBits::lshr(LHSKnown, RHSKnown);
749 break;
750 }
751 case TargetOpcode::G_SHL: {
752 KnownBits LHSKnown, RHSKnown;
753 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
754 Depth + 1);
755 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
756 Depth + 1);
757 Known = KnownBits::shl(LHSKnown, RHSKnown);
758 break;
759 }
760 case TargetOpcode::G_ROTL:
761 case TargetOpcode::G_ROTR: {
762 auto MaybeAmtOp =
763 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
764 if (!MaybeAmtOp)
765 break;
766
767 Register SrcReg = MI.getOperand(1).getReg();
768 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
769
770 unsigned Amt = MaybeAmtOp->urem(BitWidth);
771
772 // Canonicalize to ROTR.
773 if (Opcode == TargetOpcode::G_ROTL)
774 Amt = BitWidth - Amt;
775
776 Known.Zero = Known.Zero.rotr(Amt);
777 Known.One = Known.One.rotr(Amt);
778 break;
779 }
780 case TargetOpcode::G_FSHL:
781 case TargetOpcode::G_FSHR: {
782 auto MaybeAmtOp =
783 isConstantOrConstantSplatVector(MI.getOperand(3).getReg(), MRI);
784 if (!MaybeAmtOp)
785 break;
786
787 const APInt Amt = *MaybeAmtOp;
788 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
789 Depth + 1);
790 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
791 Depth + 1);
792 Known = Opcode == TargetOpcode::G_FSHL
793 ? KnownBits::fshl(Known, Known2, Amt)
794 : KnownBits::fshr(Known, Known2, Amt);
795 break;
796 }
797 case TargetOpcode::G_INTTOPTR:
798 case TargetOpcode::G_PTRTOINT:
799 if (DstTy.isVector())
800 break;
801 // Fall through and handle them the same as zext/trunc.
802 [[fallthrough]];
803 case TargetOpcode::G_ZEXT:
804 case TargetOpcode::G_TRUNC: {
805 Register SrcReg = MI.getOperand(1).getReg();
806 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
807 Known = Known.zextOrTrunc(BitWidth);
808 break;
809 }
810 case TargetOpcode::G_ASSERT_ZEXT: {
811 Register SrcReg = MI.getOperand(1).getReg();
812 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
813
814 unsigned SrcBitWidth = MI.getOperand(2).getImm();
815 assert(SrcBitWidth && "SrcBitWidth can't be zero");
816 APInt InMask = APInt::getLowBitsSet(BitWidth, SrcBitWidth);
817 Known.Zero |= (~InMask);
818 Known.One &= (~Known.Zero);
819 break;
820 }
821 case TargetOpcode::G_ASSERT_ALIGN: {
822 int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
823
824 // TODO: Should use maximum with source
825 // If a node is guaranteed to be aligned, set low zero bits accordingly as
826 // well as clearing one bits.
827 Known.Zero.setLowBits(LogOfAlign);
828 Known.One.clearLowBits(LogOfAlign);
829 break;
830 }
831 case TargetOpcode::G_MERGE_VALUES: {
832 unsigned NumOps = MI.getNumOperands();
833 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
834
835 for (unsigned I = 0; I != NumOps - 1; ++I) {
836 KnownBits SrcOpKnown;
837 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
838 DemandedElts, Depth + 1);
839 Known.insertBits(SrcOpKnown, I * OpSize);
840 }
841 break;
842 }
843 case TargetOpcode::G_UNMERGE_VALUES: {
844 unsigned NumOps = MI.getNumOperands();
845 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
846 LLT SrcTy = MRI.getType(SrcReg);
847
848 if (SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType())
849 return; // TODO: Handle vector->subelement unmerges
850
851 // Figure out the result operand index
852 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
853
854 APInt SubDemandedElts = DemandedElts;
855 if (SrcTy.isVector()) {
856 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
857 SubDemandedElts =
858 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
859 }
860
861 KnownBits SrcOpKnown;
862 computeKnownBitsImpl(SrcReg, SrcOpKnown, SubDemandedElts, Depth + 1);
863
864 if (SrcTy.isVector())
865 Known = std::move(SrcOpKnown);
866 else
867 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
868 break;
869 }
870 case TargetOpcode::G_BSWAP: {
871 Register SrcReg = MI.getOperand(1).getReg();
872 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
873 Known = Known.byteSwap();
874 break;
875 }
876 case TargetOpcode::G_BITREVERSE: {
877 Register SrcReg = MI.getOperand(1).getReg();
878 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
879 Known = Known.reverseBits();
880 break;
881 }
882 case TargetOpcode::G_CTPOP: {
883 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
884 Depth + 1);
885 // We can bound the space the count needs. Also, bits known to be zero
886 // can't contribute to the population.
887 unsigned BitsPossiblySet = Known2.countMaxPopulation();
888 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
889 Known.Zero.setBitsFrom(LowBits);
890 // TODO: we could bound Known.One using the lower bound on the number of
891 // bits which might be set provided by popcnt KnownOne2.
892 break;
893 }
894 case TargetOpcode::G_UBFX: {
895 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
896 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
897 Depth + 1);
898 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
899 Depth + 1);
900 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
901 Depth + 1);
902 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
903 break;
904 }
905 case TargetOpcode::G_SBFX: {
906 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
907 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
908 Depth + 1);
909 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
910 Depth + 1);
911 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
912 Depth + 1);
913 OffsetKnown = OffsetKnown.sext(BitWidth);
914 WidthKnown = WidthKnown.sext(BitWidth);
915 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
916 // Sign extend the extracted value using shift left and arithmetic shift
917 // right.
919 KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
920 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
921 break;
922 }
923 case TargetOpcode::G_UADDO:
924 case TargetOpcode::G_UADDE:
925 case TargetOpcode::G_SADDO:
926 case TargetOpcode::G_SADDE: {
927 if (MI.getOperand(1).getReg() == R) {
928 // If we know the result of a compare has the top bits zero, use this
929 // info.
930 if (TL.getBooleanContents(DstTy.isVector(), false) ==
932 BitWidth > 1)
933 Known.Zero.setBitsFrom(1);
934 break;
935 }
936
937 assert(MI.getOperand(0).getReg() == R &&
938 "We only compute knownbits for the sum here.");
939 // With [US]ADDE, a carry bit may be added in.
940 KnownBits Carry(1);
941 if (Opcode == TargetOpcode::G_UADDE || Opcode == TargetOpcode::G_SADDE) {
942 computeKnownBitsImpl(MI.getOperand(4).getReg(), Carry, DemandedElts,
943 Depth + 1);
944 // Carry has bit width 1
945 Carry = Carry.trunc(1);
946 } else {
947 Carry.setAllZero();
948 }
949
950 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
951 Depth + 1);
952 computeKnownBitsImpl(MI.getOperand(3).getReg(), Known2, DemandedElts,
953 Depth + 1);
954 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
955 break;
956 }
957 case TargetOpcode::G_USUBO:
958 case TargetOpcode::G_USUBE:
959 case TargetOpcode::G_SSUBO:
960 case TargetOpcode::G_SSUBE:
961 case TargetOpcode::G_UMULO:
962 case TargetOpcode::G_SMULO: {
963 if (MI.getOperand(1).getReg() == R) {
964 // If we know the result of a compare has the top bits zero, use this
965 // info.
966 if (TL.getBooleanContents(DstTy.isVector(), false) ==
968 BitWidth > 1)
969 Known.Zero.setBitsFrom(1);
970 }
971 break;
972 }
973 case TargetOpcode::G_CTTZ:
974 case TargetOpcode::G_CTTZ_ZERO_POISON: {
975 KnownBits SrcOpKnown;
976 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
977 Depth + 1);
978 // If we have a known 1, its position is our upper bound
979 unsigned PossibleTZ = SrcOpKnown.countMaxTrailingZeros();
980 unsigned LowBits = llvm::bit_width(PossibleTZ);
981 Known.Zero.setBitsFrom(LowBits);
982 break;
983 }
984 case TargetOpcode::G_CTLZ:
985 case TargetOpcode::G_CTLZ_ZERO_POISON: {
986 KnownBits SrcOpKnown;
987 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
988 Depth + 1);
989 // If we have a known 1, its position is our upper bound.
990 unsigned PossibleLZ = SrcOpKnown.countMaxLeadingZeros();
991 unsigned LowBits = llvm::bit_width(PossibleLZ);
992 Known.Zero.setBitsFrom(LowBits);
993 break;
994 }
995 case TargetOpcode::G_CTLS: {
996 Register Reg = MI.getOperand(1).getReg();
997 unsigned MinRedundantSignBits = computeNumSignBits(Reg, Depth + 1) - 1;
998
999 unsigned MaxUpperRedundantSignBits = MRI.getType(Reg).getScalarSizeInBits();
1000
1001 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
1002 APInt(BitWidth, MaxUpperRedundantSignBits));
1003
1004 Known = Range.toKnownBits();
1005 break;
1006 }
1007 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1009 Register InVec = Extract.getVectorReg();
1010 Register EltNo = Extract.getIndexReg();
1011
1012 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1013
1014 LLT VecVT = MRI.getType(InVec);
1015 // computeKnownBits not yet implemented for scalable vectors.
1016 if (VecVT.isScalableVector())
1017 break;
1018
1019 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
1020 const unsigned NumSrcElts = VecVT.getNumElements();
1021 // A return type different from the vector's element type may lead to
1022 // issues with pattern selection. Bail out to avoid that.
1023 if (BitWidth > EltBitWidth)
1024 break;
1025
1026 Known.Zero.setAllBits();
1027 Known.One.setAllBits();
1028
1029 // If we know the element index, just demand that vector element, else for
1030 // an unknown element index, ignore DemandedElts and demand them all.
1031 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
1032 if (ConstEltNo && ConstEltNo->ult(NumSrcElts))
1033 DemandedSrcElts =
1034 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
1035
1036 computeKnownBitsImpl(InVec, Known, DemandedSrcElts, Depth + 1);
1037 break;
1038 }
1039 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1041 Register InVec = Insert.getVectorReg();
1042 Register InVal = Insert.getElementReg();
1043 Register EltNo = Insert.getIndexReg();
1044 LLT VecVT = MRI.getType(InVec);
1045
1046 if (VecVT.isScalableVector())
1047 break;
1048
1049 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1050 unsigned NumElts = VecVT.getNumElements();
1051
1052 bool DemandedVal = true;
1053 APInt DemandedVecElts = DemandedElts;
1054 if (ConstEltNo && ConstEltNo->ult(NumElts)) {
1055 unsigned EltIdx = ConstEltNo->getZExtValue();
1056 DemandedVal = !!DemandedElts[EltIdx];
1057 DemandedVecElts.clearBit(EltIdx);
1058 }
1059 Known.setAllConflict();
1060 if (DemandedVal) {
1061 computeKnownBitsImpl(InVal, Known2, APInt(1, 1), Depth + 1);
1062 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
1063 }
1064 if (!!DemandedVecElts) {
1065 computeKnownBitsImpl(InVec, Known2, DemandedVecElts, Depth + 1);
1066 Known = Known.intersectWith(Known2);
1067 }
1068 break;
1069 }
1070 case TargetOpcode::G_SHUFFLE_VECTOR: {
1071 APInt DemandedLHS, DemandedRHS;
1072 // Collect the known bits that are shared by every vector element referenced
1073 // by the shuffle.
1074 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1075 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
1076 DemandedElts, DemandedLHS, DemandedRHS))
1077 break;
1078
1079 // Known bits are the values that are shared by every demanded element.
1080 Known.Zero.setAllBits();
1081 Known.One.setAllBits();
1082 if (!!DemandedLHS) {
1083 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
1084 Depth + 1);
1085 Known = Known.intersectWith(Known2);
1086 }
1087 // If we don't know any bits, early out.
1088 if (Known.isUnknown())
1089 break;
1090 if (!!DemandedRHS) {
1091 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
1092 Depth + 1);
1093 Known = Known.intersectWith(Known2);
1094 }
1095 break;
1096 }
1097 case TargetOpcode::G_CONCAT_VECTORS: {
1098 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
1099 break;
1100 // Split DemandedElts and test each of the demanded subvectors.
1101 Known.Zero.setAllBits();
1102 Known.One.setAllBits();
1103 unsigned NumSubVectorElts =
1104 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1105
1106 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
1107 APInt DemandedSub =
1108 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
1109 if (!!DemandedSub) {
1110 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
1111
1112 Known = Known.intersectWith(Known2);
1113 }
1114 // If we don't know any bits, early out.
1115 if (Known.isUnknown())
1116 break;
1117 }
1118 break;
1119 }
1120 case TargetOpcode::G_ABS: {
1121 Register SrcReg = MI.getOperand(1).getReg();
1122 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
1123 Known = Known.abs();
1124 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
1125 1);
1126 break;
1127 }
1128 }
1129
1131}
1132
1133void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
1134 FPClassTest InterestedClasses,
1135 unsigned Depth) {
1136 LLT Ty = MRI.getType(R);
1137 APInt DemandedElts =
1138 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1139 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
1140}
1141
1142/// Return true if this value is known to be the fractional part x - floor(x),
1143/// which lies in [0, 1). This implies the value cannot introduce overflow in a
1144/// fmul when the other operand is known finite.
1146 using namespace MIPatternMatch;
1147 Register SubX;
1148 return mi_match(R, MRI, m_GFSub(m_Reg(SubX), m_GFFloor(m_DeferredReg(SubX))));
1149}
1150
1151void GISelValueTracking::computeKnownFPClassForFPTrunc(
1152 const MachineInstr &MI, const APInt &DemandedElts,
1153 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
1154 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
1155 fcNone)
1156 return;
1157
1158 Register Val = MI.getOperand(1).getReg();
1159 KnownFPClass KnownSrc;
1160 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1161 Depth + 1);
1162 Known = KnownFPClass::fptrunc(KnownSrc);
1163}
1164
1165void GISelValueTracking::computeKnownFPClass(Register R,
1166 const APInt &DemandedElts,
1167 FPClassTest InterestedClasses,
1169 unsigned Depth) {
1170 assert(Known.isUnknown() && "should not be called with known information");
1171
1172 if (!DemandedElts) {
1173 // No demanded elts, better to assume we don't know anything.
1174 Known.resetAll();
1175 return;
1176 }
1177
1178 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1179
1180 MachineInstr &MI = *MRI.getVRegDef(R);
1181 unsigned Opcode = MI.getOpcode();
1182 LLT DstTy = MRI.getType(R);
1183
1184 if (!DstTy.isValid()) {
1185 Known.resetAll();
1186 return;
1187 }
1188
1189 if (auto Cst = GFConstant::getConstant(R, MRI)) {
1190 switch (Cst->getKind()) {
1192 auto APF = Cst->getScalarValue();
1193 Known.KnownFPClasses = APF.classify();
1194 Known.SignBit = APF.isNegative();
1195 break;
1196 }
1198 Known.KnownFPClasses = fcNone;
1199 bool SignBitAllZero = true;
1200 bool SignBitAllOne = true;
1201
1202 for (auto C : *Cst) {
1203 Known.KnownFPClasses |= C.classify();
1204 if (C.isNegative())
1205 SignBitAllZero = false;
1206 else
1207 SignBitAllOne = false;
1208 }
1209
1210 if (SignBitAllOne != SignBitAllZero)
1211 Known.SignBit = SignBitAllOne;
1212
1213 break;
1214 }
1216 Known.resetAll();
1217 break;
1218 }
1219 }
1220
1221 return;
1222 }
1223
1224 FPClassTest KnownNotFromFlags = fcNone;
1226 KnownNotFromFlags |= fcNan;
1228 KnownNotFromFlags |= fcInf;
1229
1230 // We no longer need to find out about these bits from inputs if we can
1231 // assume this from flags/attributes.
1232 InterestedClasses &= ~KnownNotFromFlags;
1233
1234 llvm::scope_exit ClearClassesFromFlags(
1235 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1236
1237 // All recursive calls that increase depth must come after this.
1239 return;
1240
1241 const MachineFunction *MF = MI.getMF();
1242
1243 switch (Opcode) {
1244 default:
1245 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1246 Depth);
1247 break;
1248 case TargetOpcode::G_FNEG: {
1249 Register Val = MI.getOperand(1).getReg();
1250 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1251 Known.fneg();
1252 break;
1253 }
1254 case TargetOpcode::G_SELECT: {
1255 GSelect &SelMI = cast<GSelect>(MI);
1256 Register Cond = SelMI.getCondReg();
1257 Register LHS = SelMI.getTrueReg();
1258 Register RHS = SelMI.getFalseReg();
1259
1260 FPClassTest FilterLHS = fcAllFlags;
1261 FPClassTest FilterRHS = fcAllFlags;
1262
1263 Register TestedValue;
1264 FPClassTest MaskIfTrue = fcAllFlags;
1265 FPClassTest MaskIfFalse = fcAllFlags;
1266 FPClassTest ClassVal = fcNone;
1267
1268 CmpInst::Predicate Pred;
1269 Register CmpLHS, CmpRHS;
1270 if (mi_match(Cond, MRI,
1271 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1272 // If the select filters out a value based on the class, it no longer
1273 // participates in the class of the result
1274
1275 // TODO: In some degenerate cases we can infer something if we try again
1276 // without looking through sign operations.
1277 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1278 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1279 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1280 } else if (mi_match(
1281 Cond, MRI,
1282 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1283 FPClassTest TestedMask = ClassVal;
1284 MaskIfTrue = TestedMask;
1285 MaskIfFalse = ~TestedMask;
1286 }
1287
1288 if (TestedValue == LHS) {
1289 // match !isnan(x) ? x : y
1290 FilterLHS = MaskIfTrue;
1291 } else if (TestedValue == RHS) { // && IsExactClass
1292 // match !isnan(x) ? y : x
1293 FilterRHS = MaskIfFalse;
1294 }
1295
1296 KnownFPClass Known2;
1297 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1298 Depth + 1);
1299 Known.KnownFPClasses &= FilterLHS;
1300
1301 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1302 Known2, Depth + 1);
1303 Known2.KnownFPClasses &= FilterRHS;
1304
1305 Known |= Known2;
1306 break;
1307 }
1308 case TargetOpcode::G_FCOPYSIGN: {
1309 Register Magnitude = MI.getOperand(1).getReg();
1310 Register Sign = MI.getOperand(2).getReg();
1311
1312 KnownFPClass KnownSign;
1313
1314 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1315 Depth + 1);
1316 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1317 Depth + 1);
1318 Known.copysign(KnownSign);
1319 break;
1320 }
1321 case TargetOpcode::G_FMA:
1322 case TargetOpcode::G_STRICT_FMA:
1323 case TargetOpcode::G_FMAD: {
1324 if ((InterestedClasses & fcNegative) == fcNone)
1325 break;
1326
1327 Register A = MI.getOperand(1).getReg();
1328 Register B = MI.getOperand(2).getReg();
1329 Register C = MI.getOperand(3).getReg();
1330
1331 DenormalMode Mode =
1332 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1333
1334 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1335 // x * x + y
1336 KnownFPClass KnownSrc, KnownAddend;
1337 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
1338 Depth + 1);
1339 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc,
1340 Depth + 1);
1341 if (KnownNotFromFlags) {
1342 KnownSrc.knownNot(KnownNotFromFlags);
1343 KnownAddend.knownNot(KnownNotFromFlags);
1344 }
1345 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
1346 } else {
1347 KnownFPClass KnownSrc[3];
1348 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1349 Depth + 1);
1350 if (KnownSrc[0].isUnknown())
1351 break;
1352 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1353 Depth + 1);
1354 if (KnownSrc[1].isUnknown())
1355 break;
1356 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1357 Depth + 1);
1358 if (KnownSrc[2].isUnknown())
1359 break;
1360 if (KnownNotFromFlags) {
1361 KnownSrc[0].knownNot(KnownNotFromFlags);
1362 KnownSrc[1].knownNot(KnownNotFromFlags);
1363 KnownSrc[2].knownNot(KnownNotFromFlags);
1364 }
1365 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
1366 }
1367 break;
1368 }
1369 case TargetOpcode::G_FSQRT:
1370 case TargetOpcode::G_STRICT_FSQRT: {
1371 KnownFPClass KnownSrc;
1372 FPClassTest InterestedSrcs = InterestedClasses;
1373 if (InterestedClasses & fcNan)
1374 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1375
1376 Register Val = MI.getOperand(1).getReg();
1377 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1378
1379 DenormalMode Mode =
1380 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1381 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1382 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1383 Known.knownNot(fcNegZero);
1384 break;
1385 }
1386 case TargetOpcode::G_FABS: {
1387 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1388 Register Val = MI.getOperand(1).getReg();
1389 // If we only care about the sign bit we don't need to inspect the
1390 // operand.
1391 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1392 Depth + 1);
1393 }
1394 Known.fabs();
1395 break;
1396 }
1397 case TargetOpcode::G_FATAN2: {
1398 Register Y = MI.getOperand(1).getReg();
1399 Register X = MI.getOperand(2).getReg();
1400 KnownFPClass KnownY, KnownX;
1401 computeKnownFPClass(Y, DemandedElts, InterestedClasses, KnownY, Depth + 1);
1402 computeKnownFPClass(X, DemandedElts, InterestedClasses, KnownX, Depth + 1);
1403 Known = KnownFPClass::atan2(KnownY, KnownX);
1404 break;
1405 }
1406 case TargetOpcode::G_FSINH: {
1407 Register Val = MI.getOperand(1).getReg();
1408 KnownFPClass KnownSrc;
1409 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1410 Depth + 1);
1411 Known = KnownFPClass::sinh(KnownSrc);
1412 break;
1413 }
1414 case TargetOpcode::G_FCOSH: {
1415 Register Val = MI.getOperand(1).getReg();
1416 KnownFPClass KnownSrc;
1417 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1418 Depth + 1);
1419 Known = KnownFPClass::cosh(KnownSrc);
1420 break;
1421 }
1422 case TargetOpcode::G_FTANH: {
1423 Register Val = MI.getOperand(1).getReg();
1424 KnownFPClass KnownSrc;
1425 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1426 Depth + 1);
1427 Known = KnownFPClass::tanh(KnownSrc);
1428 break;
1429 }
1430 case TargetOpcode::G_FASIN: {
1431 Register Val = MI.getOperand(1).getReg();
1432 KnownFPClass KnownSrc;
1433 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1434 Depth + 1);
1435 Known = KnownFPClass::asin(KnownSrc);
1436 break;
1437 }
1438 case TargetOpcode::G_FACOS: {
1439 Register Val = MI.getOperand(1).getReg();
1440 KnownFPClass KnownSrc;
1441 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1442 Depth + 1);
1443 Known = KnownFPClass::acos(KnownSrc);
1444 break;
1445 }
1446 case TargetOpcode::G_FATAN: {
1447 Register Val = MI.getOperand(1).getReg();
1448 KnownFPClass KnownSrc;
1449 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1450 Depth + 1);
1451 Known = KnownFPClass::atan(KnownSrc);
1452 break;
1453 }
1454 case TargetOpcode::G_FTAN: {
1455 Register Val = MI.getOperand(1).getReg();
1456 KnownFPClass KnownSrc;
1457 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1458 Depth + 1);
1459 Known = KnownFPClass::tan(KnownSrc);
1460 break;
1461 }
1462 case TargetOpcode::G_FSIN:
1463 case TargetOpcode::G_FCOS: {
1464 // Return NaN on infinite inputs.
1465 Register Val = MI.getOperand(1).getReg();
1466 KnownFPClass KnownSrc;
1467 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1468 Depth + 1);
1469 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1470 : KnownFPClass::sin(KnownSrc);
1471 break;
1472 }
1473 case TargetOpcode::G_FSINCOS: {
1474 // Operand layout: (sin_dst, cos_dst, src)
1475 Register Src = MI.getOperand(2).getReg();
1476 KnownFPClass KnownSrc;
1477 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1478 Depth + 1);
1479 if (R == MI.getOperand(0).getReg())
1480 Known = KnownFPClass::sin(KnownSrc);
1481 else
1482 Known = KnownFPClass::cos(KnownSrc);
1483 break;
1484 }
1485 case TargetOpcode::G_FMAXNUM:
1486 case TargetOpcode::G_FMINNUM:
1487 case TargetOpcode::G_FMINNUM_IEEE:
1488 case TargetOpcode::G_FMAXIMUM:
1489 case TargetOpcode::G_FMINIMUM:
1490 case TargetOpcode::G_FMAXNUM_IEEE:
1491 case TargetOpcode::G_FMAXIMUMNUM:
1492 case TargetOpcode::G_FMINIMUMNUM: {
1493 Register LHS = MI.getOperand(1).getReg();
1494 Register RHS = MI.getOperand(2).getReg();
1495 KnownFPClass KnownLHS, KnownRHS;
1496
1497 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1498 Depth + 1);
1499 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1500 Depth + 1);
1501
1503 switch (Opcode) {
1504 case TargetOpcode::G_FMINIMUM:
1506 break;
1507 case TargetOpcode::G_FMAXIMUM:
1509 break;
1510 case TargetOpcode::G_FMINIMUMNUM:
1512 break;
1513 case TargetOpcode::G_FMAXIMUMNUM:
1515 break;
1516 case TargetOpcode::G_FMINNUM:
1517 case TargetOpcode::G_FMINNUM_IEEE:
1519 break;
1520 case TargetOpcode::G_FMAXNUM:
1521 case TargetOpcode::G_FMAXNUM_IEEE:
1523 break;
1524 default:
1525 llvm_unreachable("unhandled min/max opcode");
1526 }
1527
1528 DenormalMode Mode =
1529 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1530 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1531 break;
1532 }
1533 case TargetOpcode::G_FCANONICALIZE: {
1534 Register Val = MI.getOperand(1).getReg();
1535 KnownFPClass KnownSrc;
1536 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1537 Depth + 1);
1538
1539 LLT Ty = MRI.getType(Val).getScalarType();
1540 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1541 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1542 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
1543 break;
1544 }
1545 case TargetOpcode::G_VECREDUCE_FMAX:
1546 case TargetOpcode::G_VECREDUCE_FMIN:
1547 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1548 case TargetOpcode::G_VECREDUCE_FMINIMUM: {
1549 Register Val = MI.getOperand(1).getReg();
1550 // reduce min/max will choose an element from one of the vector elements,
1551 // so we can infer and class information that is common to all elements.
1552
1553 Known =
1554 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1555 // Can only propagate sign if output is never NaN.
1556 if (!Known.isKnownNeverNaN())
1557 Known.SignBit.reset();
1558 break;
1559 }
1560 case TargetOpcode::G_FFLOOR:
1561 case TargetOpcode::G_FCEIL:
1562 case TargetOpcode::G_FRINT:
1563 case TargetOpcode::G_FNEARBYINT:
1564 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1565 case TargetOpcode::G_INTRINSIC_ROUND:
1566 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1567 case TargetOpcode::G_INTRINSIC_TRUNC: {
1568 Register Val = MI.getOperand(1).getReg();
1569 KnownFPClass KnownSrc;
1570 FPClassTest InterestedSrcs = InterestedClasses;
1571 if (InterestedSrcs & fcPosFinite)
1572 InterestedSrcs |= fcPosFinite;
1573 if (InterestedSrcs & fcNegFinite)
1574 InterestedSrcs |= fcNegFinite;
1575 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1576
1577 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1578 bool IsTrunc = Opcode == TargetOpcode::G_INTRINSIC_TRUNC;
1579 Known = KnownFPClass::roundToIntegral(KnownSrc, IsTrunc,
1580 /*IsMultiUnitFPType=*/false);
1581 break;
1582 }
1583 case TargetOpcode::G_FEXP:
1584 case TargetOpcode::G_FEXP2:
1585 case TargetOpcode::G_FEXP10: {
1586 Register Val = MI.getOperand(1).getReg();
1587 KnownFPClass KnownSrc;
1588 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1589 Depth + 1);
1590 Known = KnownFPClass::exp(KnownSrc);
1591 break;
1592 }
1593 case TargetOpcode::G_FLOG:
1594 case TargetOpcode::G_FLOG2:
1595 case TargetOpcode::G_FLOG10: {
1596 // log(+inf) -> +inf
1597 // log([+-]0.0) -> -inf
1598 // log(-inf) -> nan
1599 // log(-x) -> nan
1600 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
1601 break;
1602
1603 FPClassTest InterestedSrcs = InterestedClasses;
1604 if ((InterestedClasses & fcNegInf) != fcNone)
1605 InterestedSrcs |= fcZero | fcSubnormal;
1606 if ((InterestedClasses & fcNan) != fcNone)
1607 InterestedSrcs |= fcNan | fcNegative;
1608
1609 Register Val = MI.getOperand(1).getReg();
1610 KnownFPClass KnownSrc;
1611 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1612
1613 LLT Ty = MRI.getType(Val).getScalarType();
1614 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1615 DenormalMode Mode = MF->getDenormalMode(FltSem);
1616 Known = KnownFPClass::log(KnownSrc, Mode);
1617 break;
1618 }
1619 case TargetOpcode::G_FPOWI: {
1620 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1621 break;
1622
1623 Register Exp = MI.getOperand(2).getReg();
1624 LLT ExpTy = MRI.getType(Exp);
1625 KnownBits ExponentKnownBits = getKnownBits(
1626 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1627
1628 FPClassTest InterestedSrcs = fcNone;
1629 if (InterestedClasses & fcNan)
1630 InterestedSrcs |= fcNan;
1631 if (!ExponentKnownBits.isZero()) {
1632 if (InterestedClasses & fcInf)
1633 InterestedSrcs |= fcFinite | fcInf;
1634 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1635 InterestedSrcs |= fcNegative;
1636 }
1637
1638 KnownFPClass KnownSrc;
1639 if (InterestedSrcs != fcNone) {
1640 Register Val = MI.getOperand(1).getReg();
1641 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1642 Depth + 1);
1643 }
1644
1645 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1646 break;
1647 }
1648 case TargetOpcode::G_FLDEXP:
1649 case TargetOpcode::G_STRICT_FLDEXP: {
1650 Register Val = MI.getOperand(1).getReg();
1651 KnownFPClass KnownSrc;
1652 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1653 Depth + 1);
1654
1655 // Can refine inf/zero handling based on the exponent operand.
1656 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1657 KnownBits ExpBits;
1658 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
1659 Register ExpReg = MI.getOperand(2).getReg();
1660 LLT ExpTy = MRI.getType(ExpReg);
1661 ExpBits = getKnownBits(
1662 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1663 }
1664
1665 LLT ScalarTy = DstTy.getScalarType();
1666 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1667 DenormalMode Mode = MF->getDenormalMode(Flt);
1668 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1669 break;
1670 }
1671 case TargetOpcode::G_FADD:
1672 case TargetOpcode::G_STRICT_FADD:
1673 case TargetOpcode::G_FSUB:
1674 case TargetOpcode::G_STRICT_FSUB: {
1675 Register LHS = MI.getOperand(1).getReg();
1676 Register RHS = MI.getOperand(2).getReg();
1677 bool IsAdd = (Opcode == TargetOpcode::G_FADD ||
1678 Opcode == TargetOpcode::G_STRICT_FADD);
1679 bool WantNegative =
1680 IsAdd &&
1681 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1682 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1683 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1684
1685 if (!WantNaN && !WantNegative && !WantNegZero) {
1686 break;
1687 }
1688
1689 DenormalMode Mode =
1690 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1691
1692 FPClassTest InterestedSrcs = InterestedClasses;
1693 if (WantNegative)
1694 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1695 if (InterestedClasses & fcNan)
1696 InterestedSrcs |= fcInf;
1697
1698 // Special case fadd x, x (canonical form of fmul x, 2).
1699 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1700 KnownFPClass KnownSelf;
1701 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1702 Depth + 1);
1703 Known = KnownFPClass::fadd_self(KnownSelf, Mode);
1704 break;
1705 }
1706
1707 KnownFPClass KnownLHS, KnownRHS;
1708 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1709
1710 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1711 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1712 WantNegZero || !IsAdd) {
1713 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1714 // there's no point.
1715 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1716 Depth + 1);
1717 }
1718
1719 if (IsAdd)
1720 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, Mode);
1721 else
1722 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
1723 break;
1724 }
1725 case TargetOpcode::G_FMUL:
1726 case TargetOpcode::G_STRICT_FMUL: {
1727 Register LHS = MI.getOperand(1).getReg();
1728 Register RHS = MI.getOperand(2).getReg();
1729 DenormalMode Mode =
1730 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1731
1732 // X * X is always non-negative or a NaN (use square() for precision).
1733 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1734 KnownFPClass KnownSrc;
1735 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
1736 Known = KnownFPClass::square(KnownSrc, Mode);
1737 } else {
1738 // If RHS is a scalar constant, use the more precise APFloat overload.
1739 auto RHSCst = GFConstant::getConstant(RHS, MRI);
1740 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
1741 KnownFPClass KnownLHS;
1742 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1743 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
1744 } else {
1745 KnownFPClass KnownLHS, KnownRHS;
1746 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1747 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1748 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
1749
1750 // If one operand is known |x| <= 1 and the other is finite, the
1751 // product cannot overflow to infinity.
1752 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
1753 Known.knownNot(fcInf);
1754 else if (KnownRHS.isKnownNever(fcInf) &&
1756 Known.knownNot(fcInf);
1757 }
1758 }
1759 break;
1760 }
1761 case TargetOpcode::G_FDIV:
1762 case TargetOpcode::G_FREM: {
1763 Register LHS = MI.getOperand(1).getReg();
1764 Register RHS = MI.getOperand(2).getReg();
1765
1766 if (Opcode == TargetOpcode::G_FREM)
1767 Known.knownNot(fcInf);
1768
1769 DenormalMode Mode =
1770 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1771
1772 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1773 if (Opcode == TargetOpcode::G_FDIV) {
1774 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1775 if (!WantNan) {
1776 // X / X is always exactly 1.0 or a NaN.
1777 Known.KnownFPClasses = fcPosNormal | fcNan;
1778 break;
1779 }
1780 KnownFPClass KnownSrc;
1781 computeKnownFPClass(LHS, DemandedElts,
1782 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1783 Depth + 1);
1784 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
1785 } else {
1786 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1787 if (!WantNan) {
1788 // X % X is always exactly [+-]0.0 or a NaN.
1789 Known.KnownFPClasses = fcZero | fcNan;
1790 break;
1791 }
1792 KnownFPClass KnownSrc;
1793 computeKnownFPClass(LHS, DemandedElts,
1794 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1795 Depth + 1);
1796 Known = KnownFPClass::frem_self(KnownSrc, Mode);
1797 }
1798 break;
1799 }
1800
1801 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1802 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1803 const bool WantPositive = Opcode == TargetOpcode::G_FREM &&
1804 (InterestedClasses & fcPositive) != fcNone;
1805 if (!WantNan && !WantNegative && !WantPositive) {
1806 break;
1807 }
1808
1809 KnownFPClass KnownLHS, KnownRHS;
1810
1811 computeKnownFPClass(RHS, DemandedElts, fcNan | fcInf | fcZero | fcNegative,
1812 KnownRHS, Depth + 1);
1813
1814 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
1815 KnownRHS.isKnownNever(fcNegative) ||
1816 KnownRHS.isKnownNever(fcPositive);
1817
1818 if (KnowSomethingUseful || WantPositive) {
1819 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1820 }
1821
1822 if (Opcode == TargetOpcode::G_FDIV) {
1823 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
1824 } else {
1825 // Inf REM x and x REM 0 produce NaN.
1826 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1827 KnownLHS.isKnownNeverInfinity() &&
1828 KnownRHS.isKnownNeverLogicalZero(Mode)) {
1829 Known.knownNot(fcNan);
1830 }
1831
1832 // The sign for frem is the same as the first operand.
1833 if (KnownLHS.cannotBeOrderedLessThanZero())
1835 if (KnownLHS.cannotBeOrderedGreaterThanZero())
1837
1838 // See if we can be more aggressive about the sign of 0.
1839 if (KnownLHS.isKnownNever(fcNegative))
1840 Known.knownNot(fcNegative);
1841 if (KnownLHS.isKnownNever(fcPositive))
1842 Known.knownNot(fcPositive);
1843 }
1844 break;
1845 }
1846 case TargetOpcode::G_FFREXP: {
1847 // Only handle the mantissa output (operand 0); the exponent is an integer.
1848 if (R != MI.getOperand(0).getReg())
1849 break;
1850 Register Src = MI.getOperand(2).getReg();
1851 KnownFPClass KnownSrc;
1852 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1853 Depth + 1);
1854 DenormalMode Mode =
1855 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1856 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
1857 break;
1858 }
1859 case TargetOpcode::G_FPEXT: {
1860 Register Src = MI.getOperand(1).getReg();
1861 KnownFPClass KnownSrc;
1862 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1863 Depth + 1);
1864
1865 LLT DstScalarTy = DstTy.getScalarType();
1866 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
1867 LLT SrcTy = MRI.getType(Src).getScalarType();
1868 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
1869
1870 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem);
1871 break;
1872 }
1873 case TargetOpcode::G_FPTRUNC: {
1874 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1875 Depth);
1876 break;
1877 }
1878 case TargetOpcode::G_SITOFP:
1879 case TargetOpcode::G_UITOFP: {
1880 // Cannot produce nan
1881 Known.knownNot(fcNan);
1882
1883 // Integers cannot be subnormal
1884 Known.knownNot(fcSubnormal);
1885
1886 // sitofp and uitofp turn into +0.0 for zero.
1887 Known.knownNot(fcNegZero);
1888
1889 // UIToFP is always non-negative regardless of known bits.
1890 if (Opcode == TargetOpcode::G_UITOFP)
1891 Known.signBitMustBeZero();
1892
1893 // Only compute known bits if we can learn something useful from them.
1894 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
1895 break;
1896
1897 Register Val = MI.getOperand(1).getReg();
1898 LLT Ty = MRI.getType(Val);
1899 KnownBits IntKnown = getKnownBits(
1900 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1901
1902 // If the integer is non-zero, the result cannot be +0.0.
1903 if (IntKnown.isNonZero())
1904 Known.knownNot(fcPosZero);
1905
1906 if (Opcode == TargetOpcode::G_SITOFP) {
1907 // If the signed integer is known non-negative, the result is
1908 // non-negative. If the signed integer is known negative, the result is
1909 // negative.
1910 if (IntKnown.isNonNegative())
1911 Known.signBitMustBeZero();
1912 else if (IntKnown.isNegative())
1913 Known.signBitMustBeOne();
1914 }
1915
1916 if (InterestedClasses & fcInf) {
1917 LLT FPTy = DstTy.getScalarType();
1918 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
1919
1920 // Compute the effective integer width after removing known-zero leading
1921 // bits, to check if the result can overflow to infinity.
1922 int IntSize = IntKnown.getBitWidth();
1923 if (Opcode == TargetOpcode::G_UITOFP)
1924 IntSize -= IntKnown.countMinLeadingZeros();
1925 else
1926 IntSize -= IntKnown.countMinSignBits();
1927
1928 // If the exponent of the largest finite FP value can hold the largest
1929 // integer, the result of the cast must be finite.
1930 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
1931 Known.knownNot(fcInf);
1932 }
1933
1934 break;
1935 }
1936 // case TargetOpcode::G_MERGE_VALUES:
1937 case TargetOpcode::G_BUILD_VECTOR:
1938 case TargetOpcode::G_CONCAT_VECTORS: {
1939 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
1940
1941 if (!DstTy.isFixedVector())
1942 break;
1943
1944 bool First = true;
1945 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
1946 // We know the index we are inserting to, so clear it from Vec check.
1947 bool NeedsElt = DemandedElts[Idx];
1948
1949 // Do we demand the inserted element?
1950 if (NeedsElt) {
1951 Register Src = Merge.getSourceReg(Idx);
1952 if (First) {
1953 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
1954 First = false;
1955 } else {
1956 KnownFPClass Known2;
1957 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
1958 Known |= Known2;
1959 }
1960
1961 // If we don't know any bits, early out.
1962 if (Known.isUnknown())
1963 break;
1964 }
1965 }
1966
1967 break;
1968 }
1969 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1970 // Look through extract element. If the index is non-constant or
1971 // out-of-range demand all elements, otherwise just the extracted
1972 // element.
1973 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1974 Register Vec = Extract.getVectorReg();
1975 Register Idx = Extract.getIndexReg();
1976
1977 auto CIdx = getIConstantVRegVal(Idx, MRI);
1978
1979 LLT VecTy = MRI.getType(Vec);
1980
1981 if (VecTy.isFixedVector()) {
1982 unsigned NumElts = VecTy.getNumElements();
1983 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1984 if (CIdx && CIdx->ult(NumElts))
1985 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1986 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
1987 Depth + 1);
1988 }
1989
1990 break;
1991 }
1992 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1993 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
1994 Register Vec = Insert.getVectorReg();
1995 Register Elt = Insert.getElementReg();
1996 Register Idx = Insert.getIndexReg();
1997
1998 LLT VecTy = MRI.getType(Vec);
1999
2000 if (VecTy.isScalableVector())
2001 return;
2002
2003 auto CIdx = getIConstantVRegVal(Idx, MRI);
2004
2005 unsigned NumElts = DemandedElts.getBitWidth();
2006 APInt DemandedVecElts = DemandedElts;
2007 bool NeedsElt = true;
2008 // If we know the index we are inserting to, clear it from Vec check.
2009 if (CIdx && CIdx->ult(NumElts)) {
2010 DemandedVecElts.clearBit(CIdx->getZExtValue());
2011 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2012 }
2013
2014 // Do we demand the inserted element?
2015 if (NeedsElt) {
2016 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
2017 // If we don't know any bits, early out.
2018 if (Known.isUnknown())
2019 break;
2020 } else {
2021 Known.KnownFPClasses = fcNone;
2022 }
2023
2024 // Do we need anymore elements from Vec?
2025 if (!DemandedVecElts.isZero()) {
2026 KnownFPClass Known2;
2027 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
2028 Depth + 1);
2029 Known |= Known2;
2030 }
2031
2032 break;
2033 }
2034 case TargetOpcode::G_SHUFFLE_VECTOR: {
2035 // For undef elements, we don't know anything about the common state of
2036 // the shuffle result.
2037 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
2038 APInt DemandedLHS, DemandedRHS;
2039 if (DstTy.isScalableVector()) {
2040 assert(DemandedElts == APInt(1, 1));
2041 DemandedLHS = DemandedRHS = DemandedElts;
2042 } else {
2043 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
2044 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
2045 DemandedLHS, DemandedRHS)) {
2046 Known.resetAll();
2047 return;
2048 }
2049 }
2050
2051 if (!!DemandedLHS) {
2052 Register LHS = Shuf.getSrc1Reg();
2053 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
2054 Depth + 1);
2055
2056 // If we don't know any bits, early out.
2057 if (Known.isUnknown())
2058 break;
2059 } else {
2060 Known.KnownFPClasses = fcNone;
2061 }
2062
2063 if (!!DemandedRHS) {
2064 KnownFPClass Known2;
2065 Register RHS = Shuf.getSrc2Reg();
2066 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
2067 Depth + 1);
2068 Known |= Known2;
2069 }
2070 break;
2071 }
2072 case TargetOpcode::G_PHI: {
2073 // Cap PHI recursion below the global limit to avoid spending the entire
2074 // budget chasing loop back-edges (matches ValueTracking's
2075 // PhiRecursionLimit).
2077 break;
2078 // PHI's operands are a mix of registers and basic blocks interleaved.
2079 // We only care about the register ones.
2080 bool First = true;
2081 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
2082 const MachineOperand &Src = MI.getOperand(Idx);
2083 Register SrcReg = Src.getReg();
2084 if (First) {
2085 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
2086 Depth + 1);
2087 First = false;
2088 } else {
2089 KnownFPClass Known2;
2090 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
2091 Depth + 1);
2092 Known = Known.intersectWith(Known2);
2093 }
2094 if (Known.isUnknown())
2095 break;
2096 }
2097 break;
2098 }
2099 case TargetOpcode::COPY: {
2100 Register Src = MI.getOperand(1).getReg();
2101
2102 if (!Src.isVirtual())
2103 return;
2104
2105 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
2106 break;
2107 }
2108 }
2109}
2110
2112GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
2113 FPClassTest InterestedClasses,
2114 unsigned Depth) {
2115 KnownFPClass KnownClasses;
2116 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
2117 return KnownClasses;
2118}
2119
2120KnownFPClass GISelValueTracking::computeKnownFPClass(
2121 Register R, FPClassTest InterestedClasses, unsigned Depth) {
2123 computeKnownFPClass(R, Known, InterestedClasses, Depth);
2124 return Known;
2125}
2126
2127KnownFPClass GISelValueTracking::computeKnownFPClass(
2128 Register R, const APInt &DemandedElts, uint32_t Flags,
2129 FPClassTest InterestedClasses, unsigned Depth) {
2131 InterestedClasses &= ~fcNan;
2133 InterestedClasses &= ~fcInf;
2134
2135 KnownFPClass Result =
2136 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
2137
2139 Result.KnownFPClasses &= ~fcNan;
2141 Result.KnownFPClasses &= ~fcInf;
2142 return Result;
2143}
2144
2145KnownFPClass GISelValueTracking::computeKnownFPClass(
2146 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
2147 LLT Ty = MRI.getType(R);
2148 APInt DemandedElts =
2149 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2150 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
2151}
2152
2154 const MachineInstr *DefMI = MRI.getVRegDef(Val);
2155 if (!DefMI)
2156 return false;
2157
2158 if (DefMI->getFlag(MachineInstr::FmNoNans))
2159 return true;
2160
2161 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
2162 // the value-tracking analysis for the SNaN-only case: if the defining op is
2163 // known to quiet sNaN, the output can never be an sNaN.
2164 if (SNaN) {
2165 switch (DefMI->getOpcode()) {
2166 default:
2167 break;
2168 case TargetOpcode::G_FADD:
2169 case TargetOpcode::G_STRICT_FADD:
2170 case TargetOpcode::G_FSUB:
2171 case TargetOpcode::G_STRICT_FSUB:
2172 case TargetOpcode::G_FMUL:
2173 case TargetOpcode::G_STRICT_FMUL:
2174 case TargetOpcode::G_FDIV:
2175 case TargetOpcode::G_FREM:
2176 case TargetOpcode::G_FMA:
2177 case TargetOpcode::G_STRICT_FMA:
2178 case TargetOpcode::G_FMAD:
2179 case TargetOpcode::G_FSQRT:
2180 case TargetOpcode::G_STRICT_FSQRT:
2181 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
2182 // exactly (LLVM LangRef: "never change anything except possibly the sign
2183 // bit"). They must NOT be listed here.
2184 case TargetOpcode::G_FSIN:
2185 case TargetOpcode::G_FCOS:
2186 case TargetOpcode::G_FSINCOS:
2187 case TargetOpcode::G_FTAN:
2188 case TargetOpcode::G_FASIN:
2189 case TargetOpcode::G_FACOS:
2190 case TargetOpcode::G_FATAN:
2191 case TargetOpcode::G_FATAN2:
2192 case TargetOpcode::G_FSINH:
2193 case TargetOpcode::G_FCOSH:
2194 case TargetOpcode::G_FTANH:
2195 case TargetOpcode::G_FEXP:
2196 case TargetOpcode::G_FEXP2:
2197 case TargetOpcode::G_FEXP10:
2198 case TargetOpcode::G_FLOG:
2199 case TargetOpcode::G_FLOG2:
2200 case TargetOpcode::G_FLOG10:
2201 case TargetOpcode::G_FPOWI:
2202 case TargetOpcode::G_FLDEXP:
2203 case TargetOpcode::G_STRICT_FLDEXP:
2204 case TargetOpcode::G_FFREXP:
2205 case TargetOpcode::G_INTRINSIC_TRUNC:
2206 case TargetOpcode::G_INTRINSIC_ROUND:
2207 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2208 case TargetOpcode::G_FFLOOR:
2209 case TargetOpcode::G_FCEIL:
2210 case TargetOpcode::G_FRINT:
2211 case TargetOpcode::G_FNEARBYINT:
2212 case TargetOpcode::G_FPEXT:
2213 case TargetOpcode::G_FPTRUNC:
2214 case TargetOpcode::G_FCANONICALIZE:
2215 case TargetOpcode::G_FMINNUM:
2216 case TargetOpcode::G_FMAXNUM:
2217 case TargetOpcode::G_FMINNUM_IEEE:
2218 case TargetOpcode::G_FMAXNUM_IEEE:
2219 case TargetOpcode::G_FMINIMUM:
2220 case TargetOpcode::G_FMAXIMUM:
2221 case TargetOpcode::G_FMINIMUMNUM:
2222 case TargetOpcode::G_FMAXIMUMNUM:
2223 return true;
2224 }
2225 }
2226
2227 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2228
2229 if (SNaN)
2230 return FPClass.isKnownNever(fcSNan);
2231
2232 return FPClass.isKnownNeverNaN();
2233}
2234
2235/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2236unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2237 const APInt &DemandedElts,
2238 unsigned Depth) {
2239 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2240 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2241 if (Src1SignBits == 1)
2242 return 1;
2243 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2244}
2245
2246/// Compute the known number of sign bits with attached range metadata in the
2247/// memory operand. If this is an extending load, accounts for the behavior of
2248/// the high bits.
2250 unsigned TyBits) {
2251 const MDNode *Ranges = Ld->getRanges();
2252 if (!Ranges)
2253 return 1;
2254
2256 if (TyBits > CR.getBitWidth()) {
2257 switch (Ld->getOpcode()) {
2258 case TargetOpcode::G_SEXTLOAD:
2259 CR = CR.signExtend(TyBits);
2260 break;
2261 case TargetOpcode::G_ZEXTLOAD:
2262 CR = CR.zeroExtend(TyBits);
2263 break;
2264 default:
2265 break;
2266 }
2267 }
2268
2269 return std::min(CR.getSignedMin().getNumSignBits(),
2271}
2272
2274 const APInt &DemandedElts,
2275 unsigned Depth) {
2276 MachineInstr &MI = *MRI.getVRegDef(R);
2277 unsigned Opcode = MI.getOpcode();
2278
2279 if (Opcode == TargetOpcode::G_CONSTANT)
2280 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2281
2282 if (Depth == getMaxDepth())
2283 return 1;
2284
2285 if (!DemandedElts)
2286 return 1; // No demanded elts, better to assume we don't know anything.
2287
2288 LLT DstTy = MRI.getType(R);
2289 const unsigned TyBits = DstTy.getScalarSizeInBits();
2290
2291 // Handle the case where this is called on a register that does not have a
2292 // type constraint. This is unlikely to occur except by looking through copies
2293 // but it is possible for the initial register being queried to be in this
2294 // state.
2295 if (!DstTy.isValid())
2296 return 1;
2297
2298 unsigned FirstAnswer = 1;
2299 switch (Opcode) {
2300 case TargetOpcode::COPY: {
2301 MachineOperand &Src = MI.getOperand(1);
2302 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2303 MRI.getType(Src.getReg()).isValid()) {
2304 // Don't increment Depth for this one since we didn't do any work.
2305 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2306 }
2307
2308 return 1;
2309 }
2310 case TargetOpcode::G_SEXT: {
2311 Register Src = MI.getOperand(1).getReg();
2312 LLT SrcTy = MRI.getType(Src);
2313 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
2314 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2315 }
2316 case TargetOpcode::G_ASSERT_SEXT:
2317 case TargetOpcode::G_SEXT_INREG: {
2318 // Max of the input and what this extends.
2319 Register Src = MI.getOperand(1).getReg();
2320 unsigned SrcBits = MI.getOperand(2).getImm();
2321 unsigned InRegBits = TyBits - SrcBits + 1;
2322 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2323 InRegBits);
2324 }
2325 case TargetOpcode::G_LOAD: {
2326 GLoad *Ld = cast<GLoad>(&MI);
2327 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2328 break;
2329
2330 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2331 }
2332 case TargetOpcode::G_SEXTLOAD: {
2334
2335 // FIXME: We need an in-memory type representation.
2336 if (DstTy.isVector())
2337 return 1;
2338
2339 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2340 if (NumBits != 1)
2341 return NumBits;
2342
2343 // e.g. i16->i32 = '17' bits known.
2344 const MachineMemOperand *MMO = *MI.memoperands_begin();
2345 return TyBits - MMO->getSizeInBits().getValue() + 1;
2346 }
2347 case TargetOpcode::G_ZEXTLOAD: {
2349
2350 // FIXME: We need an in-memory type representation.
2351 if (DstTy.isVector())
2352 return 1;
2353
2354 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2355 if (NumBits != 1)
2356 return NumBits;
2357
2358 // e.g. i16->i32 = '16' bits known.
2359 const MachineMemOperand *MMO = *MI.memoperands_begin();
2360 return TyBits - MMO->getSizeInBits().getValue();
2361 }
2362 case TargetOpcode::G_AND:
2363 case TargetOpcode::G_OR:
2364 case TargetOpcode::G_XOR: {
2365 Register Src1 = MI.getOperand(1).getReg();
2366 unsigned Src1NumSignBits =
2367 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2368 if (Src1NumSignBits != 1) {
2369 Register Src2 = MI.getOperand(2).getReg();
2370 unsigned Src2NumSignBits =
2371 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2372 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2373 }
2374 break;
2375 }
2376 case TargetOpcode::G_ASHR: {
2377 Register Src1 = MI.getOperand(1).getReg();
2378 Register Src2 = MI.getOperand(2).getReg();
2379 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2380 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2381 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2382 break;
2383 }
2384 case TargetOpcode::G_SHL: {
2385 Register Src1 = MI.getOperand(1).getReg();
2386 Register Src2 = MI.getOperand(2).getReg();
2387 if (std::optional<ConstantRange> ShAmtRange =
2388 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2389 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2390 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2391
2392 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2393 unsigned ExtOpc = ExtMI.getOpcode();
2394
2395 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2396 // shifted out, then we can compute the number of sign bits for the
2397 // operand being extended. A future improvement could be to pass along the
2398 // "shifted left by" information in the recursive calls to
2399 // ComputeKnownSignBits. Allowing us to handle this more generically.
2400 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2401 ExtOpc == TargetOpcode::G_ANYEXT) {
2402 LLT ExtTy = MRI.getType(Src1);
2403 Register Extendee = ExtMI.getOperand(1).getReg();
2404 LLT ExtendeeTy = MRI.getType(Extendee);
2405 uint64_t SizeDiff =
2406 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2407
2408 if (SizeDiff <= MinShAmt) {
2409 unsigned Tmp =
2410 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2411 if (MaxShAmt < Tmp)
2412 return Tmp - MaxShAmt;
2413 }
2414 }
2415 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2416 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2417 if (MaxShAmt < Tmp)
2418 return Tmp - MaxShAmt;
2419 }
2420 break;
2421 }
2422 case TargetOpcode::G_ROTL:
2423 case TargetOpcode::G_ROTR: {
2424 Register SrcReg = MI.getOperand(1).getReg();
2425 unsigned Tmp = computeNumSignBits(SrcReg, DemandedElts, Depth + 1);
2426 auto MaybeAmt =
2427 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
2428 FirstAnswer =
2429 SignBitsOps::rot(Tmp, TyBits, MaybeAmt, Opcode == TargetOpcode::G_ROTR);
2430 break;
2431 }
2432 case TargetOpcode::G_SAVGFLOOR:
2433 case TargetOpcode::G_SAVGCEIL: {
2434 Register Src1 = MI.getOperand(1).getReg();
2435 Register Src2 = MI.getOperand(2).getReg();
2436 FirstAnswer = computeNumSignBitsMin(Src1, Src2, DemandedElts, Depth + 1);
2437 break;
2438 }
2439 case TargetOpcode::G_SREM: {
2440 // The sign bit is the LHS's sign bit, except when the result of the
2441 // remainder is zero. The magnitude of the result should be less than or
2442 // equal to the magnitude of the LHS. Therefore, the result should have
2443 // at least as many sign bits as the left hand side.
2444 Register Src = MI.getOperand(1).getReg();
2445 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2446 }
2447 case TargetOpcode::G_TRUNC: {
2448 Register Src = MI.getOperand(1).getReg();
2449 LLT SrcTy = MRI.getType(Src);
2450
2451 // Check if the sign bits of source go down as far as the truncated value.
2452 unsigned DstTyBits = DstTy.getScalarSizeInBits();
2453 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2454 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2455 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
2456 return NumSrcSignBits - (NumSrcBits - DstTyBits);
2457 break;
2458 }
2459 case TargetOpcode::G_SELECT: {
2460 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2461 MI.getOperand(3).getReg(), DemandedElts,
2462 Depth + 1);
2463 }
2464 case TargetOpcode::G_SMIN:
2465 case TargetOpcode::G_SMAX:
2466 case TargetOpcode::G_UMIN:
2467 case TargetOpcode::G_UMAX:
2468 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2469 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2470 MI.getOperand(2).getReg(), DemandedElts,
2471 Depth + 1);
2472 case TargetOpcode::G_SADDO:
2473 case TargetOpcode::G_SADDE:
2474 case TargetOpcode::G_UADDO:
2475 case TargetOpcode::G_UADDE:
2476 case TargetOpcode::G_SSUBO:
2477 case TargetOpcode::G_SSUBE:
2478 case TargetOpcode::G_USUBO:
2479 case TargetOpcode::G_USUBE:
2480 case TargetOpcode::G_SMULO:
2481 case TargetOpcode::G_UMULO: {
2482 // If compares returns 0/-1, all bits are sign bits.
2483 // We know that we have an integer-based boolean since these operations
2484 // are only available for integer.
2485 if (MI.getOperand(1).getReg() == R) {
2486 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2488 return TyBits;
2489 }
2490
2491 break;
2492 }
2493 case TargetOpcode::G_SUB: {
2494 Register Src2 = MI.getOperand(2).getReg();
2495 unsigned Src2NumSignBits =
2496 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2497 if (Src2NumSignBits == 1)
2498 return 1; // Early out.
2499
2500 // Handle NEG.
2501 Register Src1 = MI.getOperand(1).getReg();
2502 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2503 if (Known1.isZero()) {
2504 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2505 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2506 // sign bits set.
2507 if ((Known2.Zero | 1).isAllOnes())
2508 return TyBits;
2509
2510 // If the input is known to be positive (the sign bit is known clear),
2511 // the output of the NEG has, at worst, the same number of sign bits as
2512 // the input.
2513 if (Known2.isNonNegative()) {
2514 FirstAnswer = Src2NumSignBits;
2515 break;
2516 }
2517
2518 // Otherwise, we treat this like a SUB.
2519 }
2520
2521 unsigned Src1NumSignBits =
2522 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2523 if (Src1NumSignBits == 1)
2524 return 1; // Early Out.
2525
2526 // Sub can have at most one carry bit. Thus we know that the output
2527 // is, at worst, one more bit than the inputs.
2528 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2529 break;
2530 }
2531 case TargetOpcode::G_ADD: {
2532 Register Src2 = MI.getOperand(2).getReg();
2533 unsigned Src2NumSignBits =
2534 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2535 if (Src2NumSignBits <= 2)
2536 return 1; // Early out.
2537
2538 Register Src1 = MI.getOperand(1).getReg();
2539 unsigned Src1NumSignBits =
2540 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2541 if (Src1NumSignBits == 1)
2542 return 1; // Early Out.
2543
2544 // Special case decrementing a value (ADD X, -1):
2545 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2546 if (Known2.isAllOnes()) {
2547 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2548 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2549 // sign bits set.
2550 if ((Known1.Zero | 1).isAllOnes())
2551 return TyBits;
2552
2553 // If we are subtracting one from a positive number, there is no carry
2554 // out of the result.
2555 if (Known1.isNonNegative()) {
2556 FirstAnswer = Src1NumSignBits;
2557 break;
2558 }
2559
2560 // Otherwise, we treat this like an ADD.
2561 }
2562
2563 // Add can have at most one carry bit. Thus we know that the output
2564 // is, at worst, one more bit than the inputs.
2565 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2566 break;
2567 }
2568 case TargetOpcode::G_FCMP:
2569 case TargetOpcode::G_ICMP: {
2570 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2571 if (TyBits == 1)
2572 break;
2573 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2575 return TyBits; // All bits are sign bits.
2577 return TyBits - 1; // Every always-zero bit is a sign bit.
2578 break;
2579 }
2580 case TargetOpcode::G_UNMERGE_VALUES: {
2581 unsigned NumOps = MI.getNumOperands();
2582 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
2583 LLT SrcTy = MRI.getType(SrcReg);
2584
2585 if ((SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType()) ||
2586 (SrcTy.isScalar() && DstTy.isVector()))
2587 break;
2588
2589 // Figure out the result operand index
2590 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
2591
2592 APInt SubDemandedElts = DemandedElts;
2593 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
2594 if (SrcTy.isVector()) {
2595 SubDemandedElts =
2596 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
2597 }
2598
2599 unsigned SrcOpKnown =
2600 computeNumSignBits(SrcReg, SubDemandedElts, Depth + 1);
2601 if (SrcTy.isVector()) {
2602 FirstAnswer = SrcOpKnown;
2603 } else if (SrcOpKnown >= (MI.getNumOperands() - DstIdx - 2) * TyBits) {
2604 FirstAnswer = SrcOpKnown >= (MI.getNumOperands() - DstIdx - 1) * TyBits
2605 ? TyBits
2606 : SrcOpKnown % TyBits;
2607 }
2608 break;
2609 }
2610 case TargetOpcode::G_BUILD_VECTOR: {
2611 // Collect the known bits that are shared by every demanded vector element.
2612 FirstAnswer = TyBits;
2613 APInt SingleDemandedElt(1, 1);
2614 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2615 if (!DemandedElts[I])
2616 continue;
2617
2618 unsigned Tmp2 =
2619 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2620 FirstAnswer = std::min(FirstAnswer, Tmp2);
2621
2622 // If we don't know any bits, early out.
2623 if (FirstAnswer == 1)
2624 break;
2625 }
2626 break;
2627 }
2628 case TargetOpcode::G_CONCAT_VECTORS: {
2629 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2630 break;
2631 FirstAnswer = TyBits;
2632 // Determine the minimum number of sign bits across all demanded
2633 // elts of the input vectors. Early out if the result is already 1.
2634 unsigned NumSubVectorElts =
2635 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2636 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2637 APInt DemandedSub =
2638 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2639 if (!DemandedSub)
2640 continue;
2641 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2642
2643 FirstAnswer = std::min(FirstAnswer, Tmp2);
2644
2645 // If we don't know any bits, early out.
2646 if (FirstAnswer == 1)
2647 break;
2648 }
2649 break;
2650 }
2651 case TargetOpcode::G_SHUFFLE_VECTOR: {
2652 // Collect the minimum number of sign bits that are shared by every vector
2653 // element referenced by the shuffle.
2654 APInt DemandedLHS, DemandedRHS;
2655 Register Src1 = MI.getOperand(1).getReg();
2656 unsigned NumElts = MRI.getType(Src1).getNumElements();
2657 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2658 DemandedElts, DemandedLHS, DemandedRHS))
2659 return 1;
2660
2661 if (!!DemandedLHS)
2662 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2663 // If we don't know anything, early out and try computeKnownBits fall-back.
2664 if (FirstAnswer == 1)
2665 break;
2666 if (!!DemandedRHS) {
2667 unsigned Tmp2 =
2668 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2669 FirstAnswer = std::min(FirstAnswer, Tmp2);
2670 }
2671 break;
2672 }
2673 case TargetOpcode::G_SPLAT_VECTOR: {
2674 // Check if the sign bits of source go down as far as the truncated value.
2675 Register Src = MI.getOperand(1).getReg();
2676 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2677 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2678 if (NumSrcSignBits > (NumSrcBits - TyBits))
2679 return NumSrcSignBits - (NumSrcBits - TyBits);
2680 break;
2681 }
2682 case TargetOpcode::G_INTRINSIC:
2683 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2684 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2685 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2686 default: {
2687 unsigned NumBits =
2688 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2689 if (NumBits > 1)
2690 FirstAnswer = std::max(FirstAnswer, NumBits);
2691 break;
2692 }
2693 }
2694
2695 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2696 // use this information.
2697 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2698 return std::max(FirstAnswer, Known.countMinSignBits());
2699}
2700
2702 LLT Ty = MRI.getType(R);
2703 APInt DemandedElts =
2704 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2705 return computeNumSignBits(R, DemandedElts, Depth);
2706}
2707
2709 Register R, const APInt &DemandedElts, unsigned Depth) {
2710 // Shifting more than the bitwidth is not valid.
2711 MachineInstr &MI = *MRI.getVRegDef(R);
2712 unsigned Opcode = MI.getOpcode();
2713
2714 LLT Ty = MRI.getType(R);
2715 unsigned BitWidth = Ty.getScalarSizeInBits();
2716
2717 if (Opcode == TargetOpcode::G_CONSTANT) {
2718 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2719 if (ShAmt.uge(BitWidth))
2720 return std::nullopt;
2721 return ConstantRange(ShAmt);
2722 }
2723
2724 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2725 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2726 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2727 if (!DemandedElts[I])
2728 continue;
2729 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2730 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2731 MinAmt = MaxAmt = nullptr;
2732 break;
2733 }
2734
2735 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2736 if (ShAmt.uge(BitWidth))
2737 return std::nullopt;
2738 if (!MinAmt || MinAmt->ugt(ShAmt))
2739 MinAmt = &ShAmt;
2740 if (!MaxAmt || MaxAmt->ult(ShAmt))
2741 MaxAmt = &ShAmt;
2742 }
2743 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2744 "Failed to find matching min/max shift amounts");
2745 if (MinAmt && MaxAmt)
2746 return ConstantRange(*MinAmt, *MaxAmt + 1);
2747 }
2748
2749 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2750 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2751 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2752 if (KnownAmt.getMaxValue().ult(BitWidth))
2753 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2754
2755 return std::nullopt;
2756}
2757
2759 Register R, const APInt &DemandedElts, unsigned Depth) {
2760 if (std::optional<ConstantRange> AmtRange =
2761 getValidShiftAmountRange(R, DemandedElts, Depth))
2762 return AmtRange->getUnsignedMin().getZExtValue();
2763 return std::nullopt;
2764}
2765
2771
2776
2778 if (!Info) {
2779 unsigned MaxDepth =
2781 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2782 }
2783 return *Info;
2784}
2785
2786AnalysisKey GISelValueTrackingAnalysis::Key;
2787
2791 unsigned MaxDepth =
2793 return Result(MF, MaxDepth);
2794}
2795
2799 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
2800 const auto &MRI = MF.getRegInfo();
2801 OS << "name: ";
2802 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
2803 OS << '\n';
2804
2805 for (MachineBasicBlock &BB : MF) {
2806 for (MachineInstr &MI : BB) {
2807 for (MachineOperand &MO : MI.defs()) {
2808 if (!MO.isReg() || MO.getReg().isPhysical())
2809 continue;
2810 Register Reg = MO.getReg();
2811 if (!MRI.getType(Reg).isValid())
2812 continue;
2813 KnownBits Known = VTA.getKnownBits(Reg);
2814 unsigned SignedBits = VTA.computeNumSignBits(Reg);
2815 bool IsKnownNeverZero = VTA.isKnownNeverZero(Reg);
2816 OS << " " << MO << " KnownBits:" << Known << " SignBits:" << SignedBits
2817 << " IsKnownNeverZero:" << IsKnownNeverZero << '\n';
2818 };
2819 }
2820 }
2821 return PreservedAnalyses::all();
2822}
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:856
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 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.
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 APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1234
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2006
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1431
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1055
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1191
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1513
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1120
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1653
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1623
unsigned logBase2() const
Definition APInt.h:1786
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:476
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:483
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1230
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.
const MDNode * getRanges() const
Returns the Ranges that describes the dereference.
Represents an extract vector element.
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2037
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)
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.
void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
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 bool isFixedVector() const
Returns true if the LLT is a fixed vector.
TypeSize getValue() const
Metadata node.
Definition Metadata.h:1069
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.
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:165
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
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:315
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:1517
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:2554
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:1684
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:338
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
DWARFExpression::Operation Op
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
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:165
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
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
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).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
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 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 fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
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.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:171
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.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedGreaterThanZeroMask
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 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.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
static LLVM_ABI KnownFPClass atan(const KnownFPClass &Src)
Report known values for atan.
static LLVM_ABI KnownFPClass atan2(const KnownFPClass &LHS, const KnownFPClass &RHS)
Report known values for atan2.
static LLVM_ABI KnownFPClass fdiv(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv.
static LLVM_ABI KnownFPClass roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
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())
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.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
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 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 frem_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass powi(const KnownFPClass &Src, const KnownBits &N)
Propagate known class for powi.
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.