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_EXTRACT_SUBVECTOR: {
1071 Register SrcReg = MI.getOperand(1).getReg();
1072 LLT SrcTy = MRI.getType(SrcReg);
1073 APInt DemandedSrcElts;
1074 if (SrcTy.isScalableVector()) {
1075 DemandedSrcElts = APInt(1, 1);
1076 } else {
1077 uint64_t Idx = MI.getOperand(2).getImm();
1078 unsigned NumSrcElts = SrcTy.getNumElements();
1079 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
1080 }
1081 computeKnownBitsImpl(SrcReg, Known, DemandedSrcElts, Depth + 1);
1082 break;
1083 }
1084 case TargetOpcode::G_SHUFFLE_VECTOR: {
1085 APInt DemandedLHS, DemandedRHS;
1086 // Collect the known bits that are shared by every vector element referenced
1087 // by the shuffle.
1088 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1089 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
1090 DemandedElts, DemandedLHS, DemandedRHS))
1091 break;
1092
1093 // Known bits are the values that are shared by every demanded element.
1094 Known.Zero.setAllBits();
1095 Known.One.setAllBits();
1096 if (!!DemandedLHS) {
1097 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
1098 Depth + 1);
1099 Known = Known.intersectWith(Known2);
1100 }
1101 // If we don't know any bits, early out.
1102 if (Known.isUnknown())
1103 break;
1104 if (!!DemandedRHS) {
1105 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
1106 Depth + 1);
1107 Known = Known.intersectWith(Known2);
1108 }
1109 break;
1110 }
1111 case TargetOpcode::G_CONCAT_VECTORS: {
1112 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
1113 break;
1114 // Split DemandedElts and test each of the demanded subvectors.
1115 Known.Zero.setAllBits();
1116 Known.One.setAllBits();
1117 unsigned NumSubVectorElts =
1118 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1119
1120 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
1121 APInt DemandedSub =
1122 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
1123 if (!!DemandedSub) {
1124 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
1125
1126 Known = Known.intersectWith(Known2);
1127 }
1128 // If we don't know any bits, early out.
1129 if (Known.isUnknown())
1130 break;
1131 }
1132 break;
1133 }
1134 case TargetOpcode::G_ABS: {
1135 Register SrcReg = MI.getOperand(1).getReg();
1136 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
1137 Known = Known.abs();
1138 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
1139 1);
1140 break;
1141 }
1142 }
1143
1145}
1146
1147void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
1148 FPClassTest InterestedClasses,
1149 unsigned Depth) {
1150 LLT Ty = MRI.getType(R);
1151 APInt DemandedElts =
1152 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1153 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
1154}
1155
1156/// Return true if this value is known to be the fractional part x - floor(x),
1157/// which lies in [0, 1). This implies the value cannot introduce overflow in a
1158/// fmul when the other operand is known finite.
1160 using namespace MIPatternMatch;
1161 Register SubX;
1162 return mi_match(R, MRI, m_GFSub(m_Reg(SubX), m_GFFloor(m_DeferredReg(SubX))));
1163}
1164
1165void GISelValueTracking::computeKnownFPClassForFPTrunc(
1166 const MachineInstr &MI, const APInt &DemandedElts,
1167 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
1168 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
1169 fcNone)
1170 return;
1171
1172 Register Val = MI.getOperand(1).getReg();
1173 KnownFPClass KnownSrc;
1174 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1175 Depth + 1);
1176 Known = KnownFPClass::fptrunc(KnownSrc);
1177}
1178
1179void GISelValueTracking::computeKnownFPClass(Register R,
1180 const APInt &DemandedElts,
1181 FPClassTest InterestedClasses,
1183 unsigned Depth) {
1184 assert(Known.isUnknown() && "should not be called with known information");
1185
1186 if (!DemandedElts) {
1187 // No demanded elts, better to assume we don't know anything.
1188 Known.resetAll();
1189 return;
1190 }
1191
1192 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1193
1194 MachineInstr &MI = *MRI.getVRegDef(R);
1195 unsigned Opcode = MI.getOpcode();
1196 LLT DstTy = MRI.getType(R);
1197
1198 if (!DstTy.isValid()) {
1199 Known.resetAll();
1200 return;
1201 }
1202
1203 if (auto Cst = GFConstant::getConstant(R, MRI)) {
1204 switch (Cst->getKind()) {
1206 auto APF = Cst->getScalarValue();
1207 Known.KnownFPClasses = APF.classify();
1208 Known.SignBit = APF.isNegative();
1209 break;
1210 }
1212 Known.KnownFPClasses = fcNone;
1213 bool SignBitAllZero = true;
1214 bool SignBitAllOne = true;
1215
1216 for (auto C : *Cst) {
1217 Known.KnownFPClasses |= C.classify();
1218 if (C.isNegative())
1219 SignBitAllZero = false;
1220 else
1221 SignBitAllOne = false;
1222 }
1223
1224 if (SignBitAllOne != SignBitAllZero)
1225 Known.SignBit = SignBitAllOne;
1226
1227 break;
1228 }
1230 Known.resetAll();
1231 break;
1232 }
1233 }
1234
1235 return;
1236 }
1237
1238 FPClassTest KnownNotFromFlags = fcNone;
1240 KnownNotFromFlags |= fcNan;
1242 KnownNotFromFlags |= fcInf;
1243
1244 // We no longer need to find out about these bits from inputs if we can
1245 // assume this from flags/attributes.
1246 InterestedClasses &= ~KnownNotFromFlags;
1247
1248 llvm::scope_exit ClearClassesFromFlags(
1249 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1250
1251 // All recursive calls that increase depth must come after this.
1253 return;
1254
1255 const MachineFunction *MF = MI.getMF();
1256
1257 switch (Opcode) {
1258 default:
1259 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1260 Depth);
1261 break;
1262 case TargetOpcode::G_FNEG: {
1263 Register Val = MI.getOperand(1).getReg();
1264 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1265 Known.fneg();
1266 break;
1267 }
1268 case TargetOpcode::G_SELECT: {
1269 GSelect &SelMI = cast<GSelect>(MI);
1270 Register Cond = SelMI.getCondReg();
1271 Register LHS = SelMI.getTrueReg();
1272 Register RHS = SelMI.getFalseReg();
1273
1274 FPClassTest FilterLHS = fcAllFlags;
1275 FPClassTest FilterRHS = fcAllFlags;
1276
1277 Register TestedValue;
1278 FPClassTest MaskIfTrue = fcAllFlags;
1279 FPClassTest MaskIfFalse = fcAllFlags;
1280 FPClassTest ClassVal = fcNone;
1281
1282 CmpInst::Predicate Pred;
1283 Register CmpLHS, CmpRHS;
1284 if (mi_match(Cond, MRI,
1285 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1286 // If the select filters out a value based on the class, it no longer
1287 // participates in the class of the result
1288
1289 // TODO: In some degenerate cases we can infer something if we try again
1290 // without looking through sign operations.
1291 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1292 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1293 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1294 } else if (mi_match(
1295 Cond, MRI,
1296 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1297 FPClassTest TestedMask = ClassVal;
1298 MaskIfTrue = TestedMask;
1299 MaskIfFalse = ~TestedMask;
1300 }
1301
1302 if (TestedValue == LHS) {
1303 // match !isnan(x) ? x : y
1304 FilterLHS = MaskIfTrue;
1305 } else if (TestedValue == RHS) { // && IsExactClass
1306 // match !isnan(x) ? y : x
1307 FilterRHS = MaskIfFalse;
1308 }
1309
1310 KnownFPClass Known2;
1311 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1312 Depth + 1);
1313 Known.KnownFPClasses &= FilterLHS;
1314
1315 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1316 Known2, Depth + 1);
1317 Known2.KnownFPClasses &= FilterRHS;
1318
1319 Known |= Known2;
1320 break;
1321 }
1322 case TargetOpcode::G_FCOPYSIGN: {
1323 Register Magnitude = MI.getOperand(1).getReg();
1324 Register Sign = MI.getOperand(2).getReg();
1325
1326 KnownFPClass KnownSign;
1327
1328 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1329 Depth + 1);
1330 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1331 Depth + 1);
1332 Known.copysign(KnownSign);
1333 break;
1334 }
1335 case TargetOpcode::G_FMA:
1336 case TargetOpcode::G_STRICT_FMA:
1337 case TargetOpcode::G_FMAD: {
1338 if ((InterestedClasses & fcNegative) == fcNone)
1339 break;
1340
1341 Register A = MI.getOperand(1).getReg();
1342 Register B = MI.getOperand(2).getReg();
1343 Register C = MI.getOperand(3).getReg();
1344
1345 DenormalMode Mode =
1346 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1347
1348 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1349 // x * x + y
1350 KnownFPClass KnownSrc, KnownAddend;
1351 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
1352 Depth + 1);
1353 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc,
1354 Depth + 1);
1355 if (KnownNotFromFlags) {
1356 KnownSrc.knownNot(KnownNotFromFlags);
1357 KnownAddend.knownNot(KnownNotFromFlags);
1358 }
1359 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
1360 } else {
1361 KnownFPClass KnownSrc[3];
1362 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1363 Depth + 1);
1364 if (KnownSrc[0].isUnknown())
1365 break;
1366 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1367 Depth + 1);
1368 if (KnownSrc[1].isUnknown())
1369 break;
1370 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1371 Depth + 1);
1372 if (KnownSrc[2].isUnknown())
1373 break;
1374 if (KnownNotFromFlags) {
1375 KnownSrc[0].knownNot(KnownNotFromFlags);
1376 KnownSrc[1].knownNot(KnownNotFromFlags);
1377 KnownSrc[2].knownNot(KnownNotFromFlags);
1378 }
1379 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
1380 }
1381 break;
1382 }
1383 case TargetOpcode::G_FSQRT:
1384 case TargetOpcode::G_STRICT_FSQRT: {
1385 KnownFPClass KnownSrc;
1386 FPClassTest InterestedSrcs = InterestedClasses;
1387 if (InterestedClasses & fcNan)
1388 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1389
1390 Register Val = MI.getOperand(1).getReg();
1391 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1392
1393 DenormalMode Mode =
1394 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1395 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1396 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1397 Known.knownNot(fcNegZero);
1398 break;
1399 }
1400 case TargetOpcode::G_FABS: {
1401 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1402 Register Val = MI.getOperand(1).getReg();
1403 // If we only care about the sign bit we don't need to inspect the
1404 // operand.
1405 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1406 Depth + 1);
1407 }
1408 Known.fabs();
1409 break;
1410 }
1411 case TargetOpcode::G_FATAN2: {
1412 Register Y = MI.getOperand(1).getReg();
1413 Register X = MI.getOperand(2).getReg();
1414 KnownFPClass KnownY, KnownX;
1415 computeKnownFPClass(Y, DemandedElts, InterestedClasses, KnownY, Depth + 1);
1416 computeKnownFPClass(X, DemandedElts, InterestedClasses, KnownX, Depth + 1);
1417 Known = KnownFPClass::atan2(KnownY, KnownX);
1418 break;
1419 }
1420 case TargetOpcode::G_FSINH: {
1421 Register Val = MI.getOperand(1).getReg();
1422 KnownFPClass KnownSrc;
1423 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1424 Depth + 1);
1425 Known = KnownFPClass::sinh(KnownSrc);
1426 break;
1427 }
1428 case TargetOpcode::G_FCOSH: {
1429 Register Val = MI.getOperand(1).getReg();
1430 KnownFPClass KnownSrc;
1431 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1432 Depth + 1);
1433 Known = KnownFPClass::cosh(KnownSrc);
1434 break;
1435 }
1436 case TargetOpcode::G_FTANH: {
1437 Register Val = MI.getOperand(1).getReg();
1438 KnownFPClass KnownSrc;
1439 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1440 Depth + 1);
1441 Known = KnownFPClass::tanh(KnownSrc);
1442 break;
1443 }
1444 case TargetOpcode::G_FASIN: {
1445 Register Val = MI.getOperand(1).getReg();
1446 KnownFPClass KnownSrc;
1447 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1448 Depth + 1);
1449 Known = KnownFPClass::asin(KnownSrc);
1450 break;
1451 }
1452 case TargetOpcode::G_FACOS: {
1453 Register Val = MI.getOperand(1).getReg();
1454 KnownFPClass KnownSrc;
1455 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1456 Depth + 1);
1457 Known = KnownFPClass::acos(KnownSrc);
1458 break;
1459 }
1460 case TargetOpcode::G_FATAN: {
1461 Register Val = MI.getOperand(1).getReg();
1462 KnownFPClass KnownSrc;
1463 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1464 Depth + 1);
1465 Known = KnownFPClass::atan(KnownSrc);
1466 break;
1467 }
1468 case TargetOpcode::G_FTAN: {
1469 Register Val = MI.getOperand(1).getReg();
1470 KnownFPClass KnownSrc;
1471 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1472 Depth + 1);
1473 Known = KnownFPClass::tan(KnownSrc);
1474 break;
1475 }
1476 case TargetOpcode::G_FSIN:
1477 case TargetOpcode::G_FCOS: {
1478 // Return NaN on infinite inputs.
1479 Register Val = MI.getOperand(1).getReg();
1480 KnownFPClass KnownSrc;
1481 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1482 Depth + 1);
1483 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1484 : KnownFPClass::sin(KnownSrc);
1485 break;
1486 }
1487 case TargetOpcode::G_FSINCOS: {
1488 // Operand layout: (sin_dst, cos_dst, src)
1489 Register Src = MI.getOperand(2).getReg();
1490 KnownFPClass KnownSrc;
1491 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1492 Depth + 1);
1493 if (R == MI.getOperand(0).getReg())
1494 Known = KnownFPClass::sin(KnownSrc);
1495 else
1496 Known = KnownFPClass::cos(KnownSrc);
1497 break;
1498 }
1499 case TargetOpcode::G_FMAXNUM:
1500 case TargetOpcode::G_FMINNUM:
1501 case TargetOpcode::G_FMINNUM_IEEE:
1502 case TargetOpcode::G_FMAXIMUM:
1503 case TargetOpcode::G_FMINIMUM:
1504 case TargetOpcode::G_FMAXNUM_IEEE:
1505 case TargetOpcode::G_FMAXIMUMNUM:
1506 case TargetOpcode::G_FMINIMUMNUM: {
1507 Register LHS = MI.getOperand(1).getReg();
1508 Register RHS = MI.getOperand(2).getReg();
1509 KnownFPClass KnownLHS, KnownRHS;
1510
1511 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1512 Depth + 1);
1513 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1514 Depth + 1);
1515
1517 switch (Opcode) {
1518 case TargetOpcode::G_FMINIMUM:
1520 break;
1521 case TargetOpcode::G_FMAXIMUM:
1523 break;
1524 case TargetOpcode::G_FMINIMUMNUM:
1526 break;
1527 case TargetOpcode::G_FMAXIMUMNUM:
1529 break;
1530 case TargetOpcode::G_FMINNUM:
1531 case TargetOpcode::G_FMINNUM_IEEE:
1533 break;
1534 case TargetOpcode::G_FMAXNUM:
1535 case TargetOpcode::G_FMAXNUM_IEEE:
1537 break;
1538 default:
1539 llvm_unreachable("unhandled min/max opcode");
1540 }
1541
1542 DenormalMode Mode =
1543 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1544 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1545 break;
1546 }
1547 case TargetOpcode::G_FCANONICALIZE: {
1548 Register Val = MI.getOperand(1).getReg();
1549 KnownFPClass KnownSrc;
1550 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1551 Depth + 1);
1552
1553 LLT Ty = MRI.getType(Val).getScalarType();
1554 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1555 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1556 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
1557 break;
1558 }
1559 case TargetOpcode::G_VECREDUCE_FMAX:
1560 case TargetOpcode::G_VECREDUCE_FMIN:
1561 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1562 case TargetOpcode::G_VECREDUCE_FMINIMUM: {
1563 Register Val = MI.getOperand(1).getReg();
1564 // reduce min/max will choose an element from one of the vector elements,
1565 // so we can infer and class information that is common to all elements.
1566
1567 Known =
1568 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1569 // Can only propagate sign if output is never NaN.
1570 if (!Known.isKnownNeverNaN())
1571 Known.SignBit.reset();
1572 break;
1573 }
1574 case TargetOpcode::G_FFLOOR:
1575 case TargetOpcode::G_FCEIL:
1576 case TargetOpcode::G_FRINT:
1577 case TargetOpcode::G_FNEARBYINT:
1578 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1579 case TargetOpcode::G_INTRINSIC_ROUND:
1580 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1581 case TargetOpcode::G_INTRINSIC_TRUNC: {
1582 Register Val = MI.getOperand(1).getReg();
1583 KnownFPClass KnownSrc;
1584 FPClassTest InterestedSrcs = InterestedClasses;
1585 if (InterestedSrcs & fcPosFinite)
1586 InterestedSrcs |= fcPosFinite;
1587 if (InterestedSrcs & fcNegFinite)
1588 InterestedSrcs |= fcNegFinite;
1589 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1590
1591 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1592 bool IsTrunc = Opcode == TargetOpcode::G_INTRINSIC_TRUNC;
1593 Known = KnownFPClass::roundToIntegral(KnownSrc, IsTrunc,
1594 /*IsMultiUnitFPType=*/false);
1595 break;
1596 }
1597 case TargetOpcode::G_FEXP:
1598 case TargetOpcode::G_FEXP2:
1599 case TargetOpcode::G_FEXP10: {
1600 Register Val = MI.getOperand(1).getReg();
1601 KnownFPClass KnownSrc;
1602 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1603 Depth + 1);
1604 Known = KnownFPClass::exp(KnownSrc);
1605 break;
1606 }
1607 case TargetOpcode::G_FLOG:
1608 case TargetOpcode::G_FLOG2:
1609 case TargetOpcode::G_FLOG10: {
1610 // log(+inf) -> +inf
1611 // log([+-]0.0) -> -inf
1612 // log(-inf) -> nan
1613 // log(-x) -> nan
1614 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
1615 break;
1616
1617 FPClassTest InterestedSrcs = InterestedClasses;
1618 if ((InterestedClasses & fcNegInf) != fcNone)
1619 InterestedSrcs |= fcZero | fcSubnormal;
1620 if ((InterestedClasses & fcNan) != fcNone)
1621 InterestedSrcs |= fcNan | fcNegative;
1622
1623 Register Val = MI.getOperand(1).getReg();
1624 KnownFPClass KnownSrc;
1625 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1626
1627 LLT Ty = MRI.getType(Val).getScalarType();
1628 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1629 DenormalMode Mode = MF->getDenormalMode(FltSem);
1630 Known = KnownFPClass::log(KnownSrc, Mode);
1631 break;
1632 }
1633 case TargetOpcode::G_FPOWI: {
1634 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1635 break;
1636
1637 Register Exp = MI.getOperand(2).getReg();
1638 LLT ExpTy = MRI.getType(Exp);
1639 KnownBits ExponentKnownBits = getKnownBits(
1640 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1641
1642 FPClassTest InterestedSrcs = fcNone;
1643 if (InterestedClasses & fcNan)
1644 InterestedSrcs |= fcNan;
1645 if (!ExponentKnownBits.isZero()) {
1646 if (InterestedClasses & fcInf)
1647 InterestedSrcs |= fcFinite | fcInf;
1648 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1649 InterestedSrcs |= fcNegative;
1650 }
1651
1652 KnownFPClass KnownSrc;
1653 if (InterestedSrcs != fcNone) {
1654 Register Val = MI.getOperand(1).getReg();
1655 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1656 Depth + 1);
1657 }
1658
1659 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1660 break;
1661 }
1662 case TargetOpcode::G_FLDEXP:
1663 case TargetOpcode::G_STRICT_FLDEXP: {
1664 Register Val = MI.getOperand(1).getReg();
1665 KnownFPClass KnownSrc;
1666 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1667 Depth + 1);
1668
1669 // Can refine inf/zero handling based on the exponent operand.
1670 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1671 KnownBits ExpBits;
1672 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
1673 Register ExpReg = MI.getOperand(2).getReg();
1674 LLT ExpTy = MRI.getType(ExpReg);
1675 ExpBits = getKnownBits(
1676 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1677 }
1678
1679 LLT ScalarTy = DstTy.getScalarType();
1680 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1681 DenormalMode Mode = MF->getDenormalMode(Flt);
1682 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1683 break;
1684 }
1685 case TargetOpcode::G_FADD:
1686 case TargetOpcode::G_STRICT_FADD:
1687 case TargetOpcode::G_FSUB:
1688 case TargetOpcode::G_STRICT_FSUB: {
1689 Register LHS = MI.getOperand(1).getReg();
1690 Register RHS = MI.getOperand(2).getReg();
1691 bool IsAdd = (Opcode == TargetOpcode::G_FADD ||
1692 Opcode == TargetOpcode::G_STRICT_FADD);
1693 bool WantNegative =
1694 IsAdd &&
1695 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1696 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1697 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1698
1699 if (!WantNaN && !WantNegative && !WantNegZero) {
1700 break;
1701 }
1702
1703 DenormalMode Mode =
1704 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1705
1706 FPClassTest InterestedSrcs = InterestedClasses;
1707 if (WantNegative)
1708 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1709 if (InterestedClasses & fcNan)
1710 InterestedSrcs |= fcInf;
1711
1712 // Special case fadd x, x (canonical form of fmul x, 2).
1713 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1714 KnownFPClass KnownSelf;
1715 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1716 Depth + 1);
1717 Known = KnownFPClass::fadd_self(KnownSelf, Mode);
1718 break;
1719 }
1720
1721 KnownFPClass KnownLHS, KnownRHS;
1722 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1723
1724 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1725 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1726 WantNegZero || !IsAdd) {
1727 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1728 // there's no point.
1729 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1730 Depth + 1);
1731 }
1732
1733 if (IsAdd)
1734 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, Mode);
1735 else
1736 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
1737 break;
1738 }
1739 case TargetOpcode::G_FMUL:
1740 case TargetOpcode::G_STRICT_FMUL: {
1741 Register LHS = MI.getOperand(1).getReg();
1742 Register RHS = MI.getOperand(2).getReg();
1743 DenormalMode Mode =
1744 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1745
1746 // X * X is always non-negative or a NaN (use square() for precision).
1747 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1748 KnownFPClass KnownSrc;
1749 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
1750 Known = KnownFPClass::square(KnownSrc, Mode);
1751 } else {
1752 // If RHS is a scalar constant, use the more precise APFloat overload.
1753 auto RHSCst = GFConstant::getConstant(RHS, MRI);
1754 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
1755 KnownFPClass KnownLHS;
1756 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1757 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
1758 } else {
1759 KnownFPClass KnownLHS, KnownRHS;
1760 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1761 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1762 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
1763
1764 // If one operand is known |x| <= 1 and the other is finite, the
1765 // product cannot overflow to infinity.
1766 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
1767 Known.knownNot(fcInf);
1768 else if (KnownRHS.isKnownNever(fcInf) &&
1770 Known.knownNot(fcInf);
1771 }
1772 }
1773 break;
1774 }
1775 case TargetOpcode::G_FDIV:
1776 case TargetOpcode::G_FREM: {
1777 Register LHS = MI.getOperand(1).getReg();
1778 Register RHS = MI.getOperand(2).getReg();
1779
1780 if (Opcode == TargetOpcode::G_FREM)
1781 Known.knownNot(fcInf);
1782
1783 DenormalMode Mode =
1784 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1785
1786 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1787 if (Opcode == TargetOpcode::G_FDIV) {
1788 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1789 if (!WantNan) {
1790 // X / X is always exactly 1.0 or a NaN.
1791 Known.KnownFPClasses = fcPosNormal | fcNan;
1792 break;
1793 }
1794 KnownFPClass KnownSrc;
1795 computeKnownFPClass(LHS, DemandedElts,
1796 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1797 Depth + 1);
1798 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
1799 } else {
1800 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1801 if (!WantNan) {
1802 // X % X is always exactly [+-]0.0 or a NaN.
1803 Known.KnownFPClasses = fcZero | fcNan;
1804 break;
1805 }
1806 KnownFPClass KnownSrc;
1807 computeKnownFPClass(LHS, DemandedElts,
1808 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1809 Depth + 1);
1810 Known = KnownFPClass::frem_self(KnownSrc, Mode);
1811 }
1812 break;
1813 }
1814
1815 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1816 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1817 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
1818 if (!WantNan && !WantNegative && !WantPositive) {
1819 break;
1820 }
1821
1822 KnownFPClass KnownLHS, KnownRHS;
1823 const bool IsFDiv = Opcode == TargetOpcode::G_FDIV;
1824 FPClassTest InterestedRHS =
1825 IsFDiv ? fcAllFlags : fcNan | fcInf | fcZero | fcNegative;
1826
1827 computeKnownFPClass(RHS, DemandedElts, InterestedRHS, KnownRHS, Depth + 1);
1828
1829 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN();
1830 if (IsFDiv) {
1831 KnowSomethingUseful |=
1834 } else {
1835 KnowSomethingUseful |= KnownRHS.isKnownNever(fcNegative) ||
1836 KnownRHS.isKnownNever(fcPositive);
1837 }
1838
1839 if (KnowSomethingUseful || (!IsFDiv && WantPositive)) {
1840 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1841 }
1842
1843 if (IsFDiv) {
1844 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
1845 } else {
1846 // Inf REM x and x REM 0 produce NaN.
1847 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1848 KnownLHS.isKnownNeverInfinity() &&
1849 KnownRHS.isKnownNeverLogicalZero(Mode)) {
1850 Known.knownNot(fcNan);
1851 }
1852
1853 // The sign for frem is the same as the first operand.
1854 if (KnownLHS.cannotBeOrderedLessThanZero())
1856 if (KnownLHS.cannotBeOrderedGreaterThanZero())
1858
1859 // See if we can be more aggressive about the sign of 0.
1860 if (KnownLHS.isKnownNever(fcNegative))
1861 Known.knownNot(fcNegative);
1862 if (KnownLHS.isKnownNever(fcPositive))
1863 Known.knownNot(fcPositive);
1864 }
1865 break;
1866 }
1867 case TargetOpcode::G_FFREXP: {
1868 // Only handle the mantissa output (operand 0); the exponent is an integer.
1869 if (R != MI.getOperand(0).getReg())
1870 break;
1871 Register Src = MI.getOperand(2).getReg();
1872 KnownFPClass KnownSrc;
1873 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1874 Depth + 1);
1875 DenormalMode Mode =
1876 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1877 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
1878 break;
1879 }
1880 case TargetOpcode::G_FPEXT: {
1881 Register Src = MI.getOperand(1).getReg();
1882 KnownFPClass KnownSrc;
1883 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1884 Depth + 1);
1885
1886 LLT DstScalarTy = DstTy.getScalarType();
1887 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
1888 LLT SrcTy = MRI.getType(Src).getScalarType();
1889 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
1890
1891 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem);
1892 break;
1893 }
1894 case TargetOpcode::G_FPTRUNC: {
1895 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1896 Depth);
1897 break;
1898 }
1899 case TargetOpcode::G_SITOFP:
1900 case TargetOpcode::G_UITOFP: {
1901 // Cannot produce nan
1902 Known.knownNot(fcNan);
1903
1904 // Integers cannot be subnormal
1905 Known.knownNot(fcSubnormal);
1906
1907 // sitofp and uitofp turn into +0.0 for zero.
1908 Known.knownNot(fcNegZero);
1909
1910 // UIToFP is always non-negative regardless of known bits.
1911 if (Opcode == TargetOpcode::G_UITOFP)
1912 Known.signBitMustBeZero();
1913
1914 // Only compute known bits if we can learn something useful from them.
1915 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
1916 break;
1917
1918 Register Val = MI.getOperand(1).getReg();
1919 LLT Ty = MRI.getType(Val);
1920 KnownBits IntKnown = getKnownBits(
1921 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1922
1923 // If the integer is non-zero, the result cannot be +0.0.
1924 if (IntKnown.isNonZero())
1925 Known.knownNot(fcPosZero);
1926
1927 if (Opcode == TargetOpcode::G_SITOFP) {
1928 // If the signed integer is known non-negative, the result is
1929 // non-negative. If the signed integer is known negative, the result is
1930 // negative.
1931 if (IntKnown.isNonNegative())
1932 Known.signBitMustBeZero();
1933 else if (IntKnown.isNegative())
1934 Known.signBitMustBeOne();
1935 }
1936
1937 if (InterestedClasses & fcInf) {
1938 LLT FPTy = DstTy.getScalarType();
1939 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
1940
1941 // Compute the effective integer width after removing known-zero leading
1942 // bits, to check if the result can overflow to infinity.
1943 int IntSize = IntKnown.getBitWidth();
1944 if (Opcode == TargetOpcode::G_UITOFP)
1945 IntSize -= IntKnown.countMinLeadingZeros();
1946 else
1947 IntSize -= IntKnown.countMinSignBits();
1948
1949 // If the exponent of the largest finite FP value can hold the largest
1950 // integer, the result of the cast must be finite.
1951 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
1952 Known.knownNot(fcInf);
1953 }
1954
1955 break;
1956 }
1957 // case TargetOpcode::G_MERGE_VALUES:
1958 case TargetOpcode::G_BUILD_VECTOR:
1959 case TargetOpcode::G_CONCAT_VECTORS: {
1960 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
1961
1962 if (!DstTy.isFixedVector())
1963 break;
1964
1965 bool First = true;
1966 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
1967 // We know the index we are inserting to, so clear it from Vec check.
1968 bool NeedsElt = DemandedElts[Idx];
1969
1970 // Do we demand the inserted element?
1971 if (NeedsElt) {
1972 Register Src = Merge.getSourceReg(Idx);
1973 if (First) {
1974 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
1975 First = false;
1976 } else {
1977 KnownFPClass Known2;
1978 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
1979 Known |= Known2;
1980 }
1981
1982 // If we don't know any bits, early out.
1983 if (Known.isUnknown())
1984 break;
1985 }
1986 }
1987
1988 break;
1989 }
1990 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1991 // Look through extract element. If the index is non-constant or
1992 // out-of-range demand all elements, otherwise just the extracted
1993 // element.
1994 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1995 Register Vec = Extract.getVectorReg();
1996 Register Idx = Extract.getIndexReg();
1997
1998 auto CIdx = getIConstantVRegVal(Idx, MRI);
1999
2000 LLT VecTy = MRI.getType(Vec);
2001
2002 if (VecTy.isFixedVector()) {
2003 unsigned NumElts = VecTy.getNumElements();
2004 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2005 if (CIdx && CIdx->ult(NumElts))
2006 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2007 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
2008 Depth + 1);
2009 }
2010
2011 break;
2012 }
2013 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2014 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
2015 Register Vec = Insert.getVectorReg();
2016 Register Elt = Insert.getElementReg();
2017 Register Idx = Insert.getIndexReg();
2018
2019 LLT VecTy = MRI.getType(Vec);
2020
2021 if (VecTy.isScalableVector())
2022 return;
2023
2024 auto CIdx = getIConstantVRegVal(Idx, MRI);
2025
2026 unsigned NumElts = DemandedElts.getBitWidth();
2027 APInt DemandedVecElts = DemandedElts;
2028 bool NeedsElt = true;
2029 // If we know the index we are inserting to, clear it from Vec check.
2030 if (CIdx && CIdx->ult(NumElts)) {
2031 DemandedVecElts.clearBit(CIdx->getZExtValue());
2032 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2033 }
2034
2035 // Do we demand the inserted element?
2036 if (NeedsElt) {
2037 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
2038 // If we don't know any bits, early out.
2039 if (Known.isUnknown())
2040 break;
2041 } else {
2042 Known.KnownFPClasses = fcNone;
2043 }
2044
2045 // Do we need anymore elements from Vec?
2046 if (!DemandedVecElts.isZero()) {
2047 KnownFPClass Known2;
2048 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
2049 Depth + 1);
2050 Known |= Known2;
2051 }
2052
2053 break;
2054 }
2055 case TargetOpcode::G_SHUFFLE_VECTOR: {
2056 // For undef elements, we don't know anything about the common state of
2057 // the shuffle result.
2058 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
2059 APInt DemandedLHS, DemandedRHS;
2060 if (DstTy.isScalableVector()) {
2061 assert(DemandedElts == APInt(1, 1));
2062 DemandedLHS = DemandedRHS = DemandedElts;
2063 } else {
2064 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
2065 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
2066 DemandedLHS, DemandedRHS)) {
2067 Known.resetAll();
2068 return;
2069 }
2070 }
2071
2072 if (!!DemandedLHS) {
2073 Register LHS = Shuf.getSrc1Reg();
2074 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
2075 Depth + 1);
2076
2077 // If we don't know any bits, early out.
2078 if (Known.isUnknown())
2079 break;
2080 } else {
2081 Known.KnownFPClasses = fcNone;
2082 }
2083
2084 if (!!DemandedRHS) {
2085 KnownFPClass Known2;
2086 Register RHS = Shuf.getSrc2Reg();
2087 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
2088 Depth + 1);
2089 Known |= Known2;
2090 }
2091 break;
2092 }
2093 case TargetOpcode::G_PHI: {
2094 // Cap PHI recursion below the global limit to avoid spending the entire
2095 // budget chasing loop back-edges (matches ValueTracking's
2096 // PhiRecursionLimit).
2098 break;
2099 // PHI's operands are a mix of registers and basic blocks interleaved.
2100 // We only care about the register ones.
2101 bool First = true;
2102 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
2103 const MachineOperand &Src = MI.getOperand(Idx);
2104 Register SrcReg = Src.getReg();
2105 if (First) {
2106 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
2107 Depth + 1);
2108 First = false;
2109 } else {
2110 KnownFPClass Known2;
2111 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
2112 Depth + 1);
2113 Known = Known.intersectWith(Known2);
2114 }
2115 if (Known.isUnknown())
2116 break;
2117 }
2118 break;
2119 }
2120 case TargetOpcode::COPY: {
2121 Register Src = MI.getOperand(1).getReg();
2122
2123 if (!Src.isVirtual())
2124 return;
2125
2126 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
2127 break;
2128 }
2129 }
2130}
2131
2133GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
2134 FPClassTest InterestedClasses,
2135 unsigned Depth) {
2136 KnownFPClass KnownClasses;
2137 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
2138 return KnownClasses;
2139}
2140
2141KnownFPClass GISelValueTracking::computeKnownFPClass(
2142 Register R, FPClassTest InterestedClasses, unsigned Depth) {
2144 computeKnownFPClass(R, Known, InterestedClasses, Depth);
2145 return Known;
2146}
2147
2148KnownFPClass GISelValueTracking::computeKnownFPClass(
2149 Register R, const APInt &DemandedElts, uint32_t Flags,
2150 FPClassTest InterestedClasses, unsigned Depth) {
2152 InterestedClasses &= ~fcNan;
2154 InterestedClasses &= ~fcInf;
2155
2156 KnownFPClass Result =
2157 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
2158
2160 Result.KnownFPClasses &= ~fcNan;
2162 Result.KnownFPClasses &= ~fcInf;
2163 return Result;
2164}
2165
2166KnownFPClass GISelValueTracking::computeKnownFPClass(
2167 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
2168 LLT Ty = MRI.getType(R);
2169 APInt DemandedElts =
2170 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2171 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
2172}
2173
2175 const MachineInstr *DefMI = MRI.getVRegDef(Val);
2176 if (!DefMI)
2177 return false;
2178
2179 if (DefMI->getFlag(MachineInstr::FmNoNans))
2180 return true;
2181
2182 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
2183 // the value-tracking analysis for the SNaN-only case: if the defining op is
2184 // known to quiet sNaN, the output can never be an sNaN.
2185 if (SNaN) {
2186 switch (DefMI->getOpcode()) {
2187 default:
2188 break;
2189 case TargetOpcode::G_FADD:
2190 case TargetOpcode::G_STRICT_FADD:
2191 case TargetOpcode::G_FSUB:
2192 case TargetOpcode::G_STRICT_FSUB:
2193 case TargetOpcode::G_FMUL:
2194 case TargetOpcode::G_STRICT_FMUL:
2195 case TargetOpcode::G_FDIV:
2196 case TargetOpcode::G_FREM:
2197 case TargetOpcode::G_FMA:
2198 case TargetOpcode::G_STRICT_FMA:
2199 case TargetOpcode::G_FMAD:
2200 case TargetOpcode::G_FSQRT:
2201 case TargetOpcode::G_STRICT_FSQRT:
2202 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
2203 // exactly (LLVM LangRef: "never change anything except possibly the sign
2204 // bit"). They must NOT be listed here.
2205 case TargetOpcode::G_FSIN:
2206 case TargetOpcode::G_FCOS:
2207 case TargetOpcode::G_FSINCOS:
2208 case TargetOpcode::G_FTAN:
2209 case TargetOpcode::G_FASIN:
2210 case TargetOpcode::G_FACOS:
2211 case TargetOpcode::G_FATAN:
2212 case TargetOpcode::G_FATAN2:
2213 case TargetOpcode::G_FSINH:
2214 case TargetOpcode::G_FCOSH:
2215 case TargetOpcode::G_FTANH:
2216 case TargetOpcode::G_FEXP:
2217 case TargetOpcode::G_FEXP2:
2218 case TargetOpcode::G_FEXP10:
2219 case TargetOpcode::G_FLOG:
2220 case TargetOpcode::G_FLOG2:
2221 case TargetOpcode::G_FLOG10:
2222 case TargetOpcode::G_FPOWI:
2223 case TargetOpcode::G_FLDEXP:
2224 case TargetOpcode::G_STRICT_FLDEXP:
2225 case TargetOpcode::G_FFREXP:
2226 case TargetOpcode::G_INTRINSIC_TRUNC:
2227 case TargetOpcode::G_INTRINSIC_ROUND:
2228 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2229 case TargetOpcode::G_FFLOOR:
2230 case TargetOpcode::G_FCEIL:
2231 case TargetOpcode::G_FRINT:
2232 case TargetOpcode::G_FNEARBYINT:
2233 case TargetOpcode::G_FPEXT:
2234 case TargetOpcode::G_FPTRUNC:
2235 case TargetOpcode::G_FCANONICALIZE:
2236 case TargetOpcode::G_FMINNUM:
2237 case TargetOpcode::G_FMAXNUM:
2238 case TargetOpcode::G_FMINNUM_IEEE:
2239 case TargetOpcode::G_FMAXNUM_IEEE:
2240 case TargetOpcode::G_FMINIMUM:
2241 case TargetOpcode::G_FMAXIMUM:
2242 case TargetOpcode::G_FMINIMUMNUM:
2243 case TargetOpcode::G_FMAXIMUMNUM:
2244 return true;
2245 }
2246 }
2247
2248 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2249
2250 if (SNaN)
2251 return FPClass.isKnownNever(fcSNan);
2252
2253 return FPClass.isKnownNeverNaN();
2254}
2255
2256/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2257unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2258 const APInt &DemandedElts,
2259 unsigned Depth) {
2260 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2261 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2262 if (Src1SignBits == 1)
2263 return 1;
2264 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2265}
2266
2267/// Compute the known number of sign bits with attached range metadata in the
2268/// memory operand. If this is an extending load, accounts for the behavior of
2269/// the high bits.
2271 unsigned TyBits) {
2272 const MDNode *Ranges = Ld->getRanges();
2273 if (!Ranges)
2274 return 1;
2275
2277 if (TyBits > CR.getBitWidth()) {
2278 switch (Ld->getOpcode()) {
2279 case TargetOpcode::G_SEXTLOAD:
2280 CR = CR.signExtend(TyBits);
2281 break;
2282 case TargetOpcode::G_ZEXTLOAD:
2283 CR = CR.zeroExtend(TyBits);
2284 break;
2285 default:
2286 break;
2287 }
2288 }
2289
2290 return std::min(CR.getSignedMin().getNumSignBits(),
2292}
2293
2295 const APInt &DemandedElts,
2296 unsigned Depth) {
2297 MachineInstr &MI = *MRI.getVRegDef(R);
2298 unsigned Opcode = MI.getOpcode();
2299
2300 if (Opcode == TargetOpcode::G_CONSTANT)
2301 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2302
2303 if (Depth == getMaxDepth())
2304 return 1;
2305
2306 if (!DemandedElts)
2307 return 1; // No demanded elts, better to assume we don't know anything.
2308
2309 LLT DstTy = MRI.getType(R);
2310 const unsigned TyBits = DstTy.getScalarSizeInBits();
2311
2312 // Handle the case where this is called on a register that does not have a
2313 // type constraint. This is unlikely to occur except by looking through copies
2314 // but it is possible for the initial register being queried to be in this
2315 // state.
2316 if (!DstTy.isValid())
2317 return 1;
2318
2319 unsigned FirstAnswer = 1;
2320 switch (Opcode) {
2321 case TargetOpcode::COPY: {
2322 MachineOperand &Src = MI.getOperand(1);
2323 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2324 MRI.getType(Src.getReg()).isValid()) {
2325 // Don't increment Depth for this one since we didn't do any work.
2326 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2327 }
2328
2329 return 1;
2330 }
2331 case TargetOpcode::G_SEXT: {
2332 Register Src = MI.getOperand(1).getReg();
2333 LLT SrcTy = MRI.getType(Src);
2334 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
2335 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2336 }
2337 case TargetOpcode::G_ASSERT_SEXT:
2338 case TargetOpcode::G_SEXT_INREG: {
2339 // Max of the input and what this extends.
2340 Register Src = MI.getOperand(1).getReg();
2341 unsigned SrcBits = MI.getOperand(2).getImm();
2342 unsigned InRegBits = TyBits - SrcBits + 1;
2343 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2344 InRegBits);
2345 }
2346 case TargetOpcode::G_LOAD: {
2347 GLoad *Ld = cast<GLoad>(&MI);
2348 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2349 break;
2350
2351 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2352 }
2353 case TargetOpcode::G_SEXTLOAD: {
2355
2356 // FIXME: We need an in-memory type representation.
2357 if (DstTy.isVector())
2358 return 1;
2359
2360 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2361 if (NumBits != 1)
2362 return NumBits;
2363
2364 // e.g. i16->i32 = '17' bits known.
2365 const MachineMemOperand *MMO = *MI.memoperands_begin();
2366 return TyBits - MMO->getSizeInBits().getValue() + 1;
2367 }
2368 case TargetOpcode::G_ZEXTLOAD: {
2370
2371 // FIXME: We need an in-memory type representation.
2372 if (DstTy.isVector())
2373 return 1;
2374
2375 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2376 if (NumBits != 1)
2377 return NumBits;
2378
2379 // e.g. i16->i32 = '16' bits known.
2380 const MachineMemOperand *MMO = *MI.memoperands_begin();
2381 return TyBits - MMO->getSizeInBits().getValue();
2382 }
2383 case TargetOpcode::G_AND:
2384 case TargetOpcode::G_OR:
2385 case TargetOpcode::G_XOR: {
2386 Register Src1 = MI.getOperand(1).getReg();
2387 unsigned Src1NumSignBits =
2388 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2389 if (Src1NumSignBits != 1) {
2390 Register Src2 = MI.getOperand(2).getReg();
2391 unsigned Src2NumSignBits =
2392 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2393 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2394 }
2395 break;
2396 }
2397 case TargetOpcode::G_ASHR: {
2398 Register Src1 = MI.getOperand(1).getReg();
2399 Register Src2 = MI.getOperand(2).getReg();
2400 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2401 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2402 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2403 break;
2404 }
2405 case TargetOpcode::G_SHL: {
2406 Register Src1 = MI.getOperand(1).getReg();
2407 Register Src2 = MI.getOperand(2).getReg();
2408 if (std::optional<ConstantRange> ShAmtRange =
2409 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2410 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2411 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2412
2413 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2414 unsigned ExtOpc = ExtMI.getOpcode();
2415
2416 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2417 // shifted out, then we can compute the number of sign bits for the
2418 // operand being extended. A future improvement could be to pass along the
2419 // "shifted left by" information in the recursive calls to
2420 // ComputeKnownSignBits. Allowing us to handle this more generically.
2421 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2422 ExtOpc == TargetOpcode::G_ANYEXT) {
2423 LLT ExtTy = MRI.getType(Src1);
2424 Register Extendee = ExtMI.getOperand(1).getReg();
2425 LLT ExtendeeTy = MRI.getType(Extendee);
2426 uint64_t SizeDiff =
2427 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2428
2429 if (SizeDiff <= MinShAmt) {
2430 unsigned Tmp =
2431 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2432 if (MaxShAmt < Tmp)
2433 return Tmp - MaxShAmt;
2434 }
2435 }
2436 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2437 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2438 if (MaxShAmt < Tmp)
2439 return Tmp - MaxShAmt;
2440 }
2441 break;
2442 }
2443 case TargetOpcode::G_ROTL:
2444 case TargetOpcode::G_ROTR: {
2445 Register SrcReg = MI.getOperand(1).getReg();
2446 unsigned Tmp = computeNumSignBits(SrcReg, DemandedElts, Depth + 1);
2447 auto MaybeAmt =
2448 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
2449 FirstAnswer =
2450 SignBitsOps::rot(Tmp, TyBits, MaybeAmt, Opcode == TargetOpcode::G_ROTR);
2451 break;
2452 }
2453 case TargetOpcode::G_SAVGFLOOR:
2454 case TargetOpcode::G_SAVGCEIL: {
2455 Register Src1 = MI.getOperand(1).getReg();
2456 Register Src2 = MI.getOperand(2).getReg();
2457 FirstAnswer = computeNumSignBitsMin(Src1, Src2, DemandedElts, Depth + 1);
2458 break;
2459 }
2460 case TargetOpcode::G_SREM: {
2461 // The sign bit is the LHS's sign bit, except when the result of the
2462 // remainder is zero. The magnitude of the result should be less than or
2463 // equal to the magnitude of the LHS. Therefore, the result should have
2464 // at least as many sign bits as the left hand side.
2465 Register Src = MI.getOperand(1).getReg();
2466 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2467 }
2468 case TargetOpcode::G_TRUNC: {
2469 Register Src = MI.getOperand(1).getReg();
2470 LLT SrcTy = MRI.getType(Src);
2471
2472 // Check if the sign bits of source go down as far as the truncated value.
2473 unsigned DstTyBits = DstTy.getScalarSizeInBits();
2474 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2475 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2476 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
2477 return NumSrcSignBits - (NumSrcBits - DstTyBits);
2478 break;
2479 }
2480 case TargetOpcode::G_SELECT: {
2481 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2482 MI.getOperand(3).getReg(), DemandedElts,
2483 Depth + 1);
2484 }
2485 case TargetOpcode::G_SMIN:
2486 case TargetOpcode::G_SMAX:
2487 case TargetOpcode::G_UMIN:
2488 case TargetOpcode::G_UMAX:
2489 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2490 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2491 MI.getOperand(2).getReg(), DemandedElts,
2492 Depth + 1);
2493 case TargetOpcode::G_SADDO:
2494 case TargetOpcode::G_SADDE:
2495 case TargetOpcode::G_UADDO:
2496 case TargetOpcode::G_UADDE:
2497 case TargetOpcode::G_SSUBO:
2498 case TargetOpcode::G_SSUBE:
2499 case TargetOpcode::G_USUBO:
2500 case TargetOpcode::G_USUBE:
2501 case TargetOpcode::G_SMULO:
2502 case TargetOpcode::G_UMULO: {
2503 // If compares returns 0/-1, all bits are sign bits.
2504 // We know that we have an integer-based boolean since these operations
2505 // are only available for integer.
2506 if (MI.getOperand(1).getReg() == R) {
2507 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2509 return TyBits;
2510 }
2511
2512 break;
2513 }
2514 case TargetOpcode::G_SUB: {
2515 Register Src2 = MI.getOperand(2).getReg();
2516 unsigned Src2NumSignBits =
2517 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2518 if (Src2NumSignBits == 1)
2519 return 1; // Early out.
2520
2521 // Handle NEG.
2522 Register Src1 = MI.getOperand(1).getReg();
2523 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2524 if (Known1.isZero()) {
2525 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2526 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2527 // sign bits set.
2528 if ((Known2.Zero | 1).isAllOnes())
2529 return TyBits;
2530
2531 // If the input is known to be positive (the sign bit is known clear),
2532 // the output of the NEG has, at worst, the same number of sign bits as
2533 // the input.
2534 if (Known2.isNonNegative()) {
2535 FirstAnswer = Src2NumSignBits;
2536 break;
2537 }
2538
2539 // Otherwise, we treat this like a SUB.
2540 }
2541
2542 unsigned Src1NumSignBits =
2543 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2544 if (Src1NumSignBits == 1)
2545 return 1; // Early Out.
2546
2547 // Sub can have at most one carry bit. Thus we know that the output
2548 // is, at worst, one more bit than the inputs.
2549 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2550 break;
2551 }
2552 case TargetOpcode::G_ADD: {
2553 Register Src2 = MI.getOperand(2).getReg();
2554 unsigned Src2NumSignBits =
2555 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2556 if (Src2NumSignBits <= 2)
2557 return 1; // Early out.
2558
2559 Register Src1 = MI.getOperand(1).getReg();
2560 unsigned Src1NumSignBits =
2561 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2562 if (Src1NumSignBits == 1)
2563 return 1; // Early Out.
2564
2565 // Special case decrementing a value (ADD X, -1):
2566 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2567 if (Known2.isAllOnes()) {
2568 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2569 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2570 // sign bits set.
2571 if ((Known1.Zero | 1).isAllOnes())
2572 return TyBits;
2573
2574 // If we are subtracting one from a positive number, there is no carry
2575 // out of the result.
2576 if (Known1.isNonNegative()) {
2577 FirstAnswer = Src1NumSignBits;
2578 break;
2579 }
2580
2581 // Otherwise, we treat this like an ADD.
2582 }
2583
2584 // Add can have at most one carry bit. Thus we know that the output
2585 // is, at worst, one more bit than the inputs.
2586 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2587 break;
2588 }
2589 case TargetOpcode::G_FCMP:
2590 case TargetOpcode::G_ICMP: {
2591 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2592 if (TyBits == 1)
2593 break;
2594 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2596 return TyBits; // All bits are sign bits.
2598 return TyBits - 1; // Every always-zero bit is a sign bit.
2599 break;
2600 }
2601 case TargetOpcode::G_UNMERGE_VALUES: {
2602 unsigned NumOps = MI.getNumOperands();
2603 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
2604 LLT SrcTy = MRI.getType(SrcReg);
2605
2606 if ((SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType()) ||
2607 (SrcTy.isScalar() && DstTy.isVector()))
2608 break;
2609
2610 // Figure out the result operand index
2611 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
2612
2613 APInt SubDemandedElts = DemandedElts;
2614 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
2615 if (SrcTy.isVector()) {
2616 SubDemandedElts =
2617 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
2618 }
2619
2620 unsigned SrcOpKnown =
2621 computeNumSignBits(SrcReg, SubDemandedElts, Depth + 1);
2622 if (SrcTy.isVector()) {
2623 FirstAnswer = SrcOpKnown;
2624 } else if (SrcOpKnown >= (MI.getNumOperands() - DstIdx - 2) * TyBits) {
2625 FirstAnswer = SrcOpKnown >= (MI.getNumOperands() - DstIdx - 1) * TyBits
2626 ? TyBits
2627 : SrcOpKnown % TyBits;
2628 }
2629 break;
2630 }
2631 case TargetOpcode::G_BUILD_VECTOR: {
2632 // Collect the known bits that are shared by every demanded vector element.
2633 FirstAnswer = TyBits;
2634 APInt SingleDemandedElt(1, 1);
2635 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2636 if (!DemandedElts[I])
2637 continue;
2638
2639 unsigned Tmp2 =
2640 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2641 FirstAnswer = std::min(FirstAnswer, Tmp2);
2642
2643 // If we don't know any bits, early out.
2644 if (FirstAnswer == 1)
2645 break;
2646 }
2647 break;
2648 }
2649 case TargetOpcode::G_CONCAT_VECTORS: {
2650 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2651 break;
2652 FirstAnswer = TyBits;
2653 // Determine the minimum number of sign bits across all demanded
2654 // elts of the input vectors. Early out if the result is already 1.
2655 unsigned NumSubVectorElts =
2656 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2657 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2658 APInt DemandedSub =
2659 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2660 if (!DemandedSub)
2661 continue;
2662 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2663
2664 FirstAnswer = std::min(FirstAnswer, Tmp2);
2665
2666 // If we don't know any bits, early out.
2667 if (FirstAnswer == 1)
2668 break;
2669 }
2670 break;
2671 }
2672 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
2673 // Offset the demanded elts by the subvector index.
2674 Register SrcReg = MI.getOperand(1).getReg();
2675 LLT SrcTy = MRI.getType(SrcReg);
2676 APInt DemandedSrcElts;
2677 if (SrcTy.isScalableVector()) {
2678 DemandedSrcElts = APInt(1, 1);
2679 } else {
2680 uint64_t Idx = MI.getOperand(2).getImm();
2681 unsigned NumSrcElts = SrcTy.getNumElements();
2682 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2683 }
2684 return computeNumSignBits(SrcReg, DemandedSrcElts, Depth + 1);
2685 }
2686 case TargetOpcode::G_SHUFFLE_VECTOR: {
2687 // Collect the minimum number of sign bits that are shared by every vector
2688 // element referenced by the shuffle.
2689 APInt DemandedLHS, DemandedRHS;
2690 Register Src1 = MI.getOperand(1).getReg();
2691 unsigned NumElts = MRI.getType(Src1).getNumElements();
2692 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2693 DemandedElts, DemandedLHS, DemandedRHS))
2694 return 1;
2695
2696 if (!!DemandedLHS)
2697 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2698 // If we don't know anything, early out and try computeKnownBits fall-back.
2699 if (FirstAnswer == 1)
2700 break;
2701 if (!!DemandedRHS) {
2702 unsigned Tmp2 =
2703 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2704 FirstAnswer = std::min(FirstAnswer, Tmp2);
2705 }
2706 break;
2707 }
2708 case TargetOpcode::G_SPLAT_VECTOR: {
2709 // Check if the sign bits of source go down as far as the truncated value.
2710 Register Src = MI.getOperand(1).getReg();
2711 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2712 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2713 if (NumSrcSignBits > (NumSrcBits - TyBits))
2714 return NumSrcSignBits - (NumSrcBits - TyBits);
2715 break;
2716 }
2717 case TargetOpcode::G_INTRINSIC:
2718 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2719 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2720 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2721 default: {
2722 unsigned NumBits =
2723 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2724 if (NumBits > 1)
2725 FirstAnswer = std::max(FirstAnswer, NumBits);
2726 break;
2727 }
2728 }
2729
2730 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2731 // use this information.
2732 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2733 return std::max(FirstAnswer, Known.countMinSignBits());
2734}
2735
2737 LLT Ty = MRI.getType(R);
2738 APInt DemandedElts =
2739 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2740 return computeNumSignBits(R, DemandedElts, Depth);
2741}
2742
2744 Register R, const APInt &DemandedElts, unsigned Depth) {
2745 // Shifting more than the bitwidth is not valid.
2746 MachineInstr &MI = *MRI.getVRegDef(R);
2747 unsigned Opcode = MI.getOpcode();
2748
2749 LLT Ty = MRI.getType(R);
2750 unsigned BitWidth = Ty.getScalarSizeInBits();
2751
2752 if (Opcode == TargetOpcode::G_CONSTANT) {
2753 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2754 if (ShAmt.uge(BitWidth))
2755 return std::nullopt;
2756 return ConstantRange(ShAmt);
2757 }
2758
2759 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2760 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2761 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2762 if (!DemandedElts[I])
2763 continue;
2764 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2765 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2766 MinAmt = MaxAmt = nullptr;
2767 break;
2768 }
2769
2770 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2771 if (ShAmt.uge(BitWidth))
2772 return std::nullopt;
2773 if (!MinAmt || MinAmt->ugt(ShAmt))
2774 MinAmt = &ShAmt;
2775 if (!MaxAmt || MaxAmt->ult(ShAmt))
2776 MaxAmt = &ShAmt;
2777 }
2778 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2779 "Failed to find matching min/max shift amounts");
2780 if (MinAmt && MaxAmt)
2781 return ConstantRange(*MinAmt, *MaxAmt + 1);
2782 }
2783
2784 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2785 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2786 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2787 if (KnownAmt.getMaxValue().ult(BitWidth))
2788 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2789
2790 return std::nullopt;
2791}
2792
2794 Register R, const APInt &DemandedElts, unsigned Depth) {
2795 if (std::optional<ConstantRange> AmtRange =
2796 getValidShiftAmountRange(R, DemandedElts, Depth))
2797 return AmtRange->getUnsignedMin().getZExtValue();
2798 return std::nullopt;
2799}
2800
2806
2811
2813 if (!Info) {
2814 unsigned MaxDepth =
2816 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2817 }
2818 return *Info;
2819}
2820
2821AnalysisKey GISelValueTrackingAnalysis::Key;
2822
2826 unsigned MaxDepth =
2828 return Result(MF, MaxDepth);
2829}
2830
2834 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
2835 const auto &MRI = MF.getRegInfo();
2836 OS << "name: ";
2837 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
2838 OS << '\n';
2839
2840 for (MachineBasicBlock &BB : MF) {
2841 for (MachineInstr &MI : BB) {
2842 for (MachineOperand &MO : MI.defs()) {
2843 if (!MO.isReg() || MO.getReg().isPhysical())
2844 continue;
2845 Register Reg = MO.getReg();
2846 if (!MRI.getType(Reg).isValid())
2847 continue;
2848 KnownBits Known = VTA.getKnownBits(Reg);
2849 unsigned SignedBits = VTA.computeNumSignBits(Reg);
2850 bool IsKnownNeverZero = VTA.isKnownNeverZero(Reg);
2851 OS << " " << MO << " KnownBits:" << Known << " SignBits:" << SignedBits
2852 << " IsKnownNeverZero:" << IsKnownNeverZero << '\n';
2853 };
2854 }
2855 }
2856 return PreservedAnalyses::all();
2857}
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:2001
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:231
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1427
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1050
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:226
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1187
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:377
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1509
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1116
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1649
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1619
unsigned logBase2() const
Definition APInt.h:1782
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:472
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:876
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:437
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:303
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:283
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:236
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1226
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.