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