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_CLMUL: {
500 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
501 Depth + 1);
502 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
503 Depth + 1);
504 Known = KnownBits::clmul(Known, Known2);
505 break;
506 }
507 case TargetOpcode::G_UAVGFLOOR: {
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_UAVGCEIL: {
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_SAVGFLOOR: {
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_SAVGCEIL: {
532 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
533 Depth + 1);
534 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
535 Depth + 1);
537 break;
538 }
539 case TargetOpcode::G_ABDU: {
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::abdu(Known, Known2);
545 break;
546 }
547 case TargetOpcode::G_ABDS: {
548 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
549 Depth + 1);
550 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
551 Depth + 1);
552 Known = KnownBits::abds(Known, Known2);
553
554 unsigned SignBits1 =
555 computeNumSignBits(MI.getOperand(2).getReg(), DemandedElts, Depth + 1);
556 if (SignBits1 == 1) {
557 break;
558 }
559 unsigned SignBits0 =
560 computeNumSignBits(MI.getOperand(1).getReg(), DemandedElts, Depth + 1);
561
562 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
563 break;
564 }
565 case TargetOpcode::G_SADDSAT: {
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_UADDSAT: {
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_SSUBSAT: {
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_USUBSAT: {
590 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
591 Depth + 1);
592 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
593 Depth + 1);
595 break;
596 }
597 case TargetOpcode::G_UDIV: {
598 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
599 Depth + 1);
600 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
601 Depth + 1);
602 Known = KnownBits::udiv(Known, Known2,
604 break;
605 }
606 case TargetOpcode::G_SDIV: {
607 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
608 Depth + 1);
609 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
610 Depth + 1);
611 Known = KnownBits::sdiv(Known, Known2,
613 break;
614 }
615 case TargetOpcode::G_UREM: {
616 KnownBits LHSKnown(Known.getBitWidth());
617 KnownBits RHSKnown(Known.getBitWidth());
618
619 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
620 Depth + 1);
621 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
622 Depth + 1);
623
624 Known = KnownBits::urem(LHSKnown, RHSKnown);
625 break;
626 }
627 case TargetOpcode::G_SREM: {
628 KnownBits LHSKnown(Known.getBitWidth());
629 KnownBits RHSKnown(Known.getBitWidth());
630
631 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
632 Depth + 1);
633 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
634 Depth + 1);
635
636 Known = KnownBits::srem(LHSKnown, RHSKnown);
637 break;
638 }
639 case TargetOpcode::G_SELECT: {
640 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
641 Known, DemandedElts, Depth + 1);
642 break;
643 }
644 case TargetOpcode::G_SMIN: {
645 // TODO: Handle clamp pattern with number of sign bits
646 KnownBits KnownRHS;
647 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
648 Depth + 1);
649 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
650 Depth + 1);
651 Known = KnownBits::smin(Known, KnownRHS);
652 break;
653 }
654 case TargetOpcode::G_SMAX: {
655 // TODO: Handle clamp pattern with number of sign bits
656 KnownBits KnownRHS;
657 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
658 Depth + 1);
659 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
660 Depth + 1);
661 Known = KnownBits::smax(Known, KnownRHS);
662 break;
663 }
664 case TargetOpcode::G_UMIN: {
665 KnownBits KnownRHS;
666 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
667 Depth + 1);
668 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
669 Depth + 1);
670 Known = KnownBits::umin(Known, KnownRHS);
671 break;
672 }
673 case TargetOpcode::G_UMAX: {
674 KnownBits KnownRHS;
675 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
676 Depth + 1);
677 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
678 Depth + 1);
679 Known = KnownBits::umax(Known, KnownRHS);
680 break;
681 }
682 case TargetOpcode::G_FCMP:
683 case TargetOpcode::G_ICMP: {
684 if (DstTy.isVector())
685 break;
686 if (TL.getBooleanContents(DstTy.isVector(),
687 Opcode == TargetOpcode::G_FCMP) ==
689 BitWidth > 1)
690 Known.Zero.setBitsFrom(1);
691 break;
692 }
693 case TargetOpcode::G_SEXT: {
694 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
695 Depth + 1);
696 // If the sign bit is known to be zero or one, then sext will extend
697 // it to the top bits, else it will just zext.
698 Known = Known.sext(BitWidth);
699 break;
700 }
701 case TargetOpcode::G_ASSERT_SEXT:
702 case TargetOpcode::G_SEXT_INREG: {
703 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
704 Depth + 1);
705 Known = Known.sextInReg(MI.getOperand(2).getImm());
706 break;
707 }
708 case TargetOpcode::G_ANYEXT: {
709 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
710 Depth + 1);
711 Known = Known.anyext(BitWidth);
712 break;
713 }
714 case TargetOpcode::G_LOAD: {
715 const MachineMemOperand *MMO = *MI.memoperands_begin();
716 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
717 if (const MDNode *Ranges = MMO->getRanges())
718 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
719 Known = KnownRange.anyext(Known.getBitWidth());
720 break;
721 }
722 case TargetOpcode::G_SEXTLOAD:
723 case TargetOpcode::G_ZEXTLOAD: {
724 if (DstTy.isVector())
725 break;
726 const MachineMemOperand *MMO = *MI.memoperands_begin();
727 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
728 if (const MDNode *Ranges = MMO->getRanges())
729 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
730 Known = Opcode == TargetOpcode::G_SEXTLOAD
731 ? KnownRange.sext(Known.getBitWidth())
732 : KnownRange.zext(Known.getBitWidth());
733 break;
734 }
735 case TargetOpcode::G_ASHR: {
736 KnownBits LHSKnown, RHSKnown;
737 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
738 Depth + 1);
739 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
740 Depth + 1);
741 Known = KnownBits::ashr(LHSKnown, RHSKnown);
742 break;
743 }
744 case TargetOpcode::G_LSHR: {
745 KnownBits LHSKnown, RHSKnown;
746 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
747 Depth + 1);
748 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
749 Depth + 1);
750 Known = KnownBits::lshr(LHSKnown, RHSKnown);
751 break;
752 }
753 case TargetOpcode::G_SHL: {
754 KnownBits LHSKnown, RHSKnown;
755 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
756 Depth + 1);
757 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
758 Depth + 1);
759 Known = KnownBits::shl(LHSKnown, RHSKnown);
760 break;
761 }
762 case TargetOpcode::G_ROTL:
763 case TargetOpcode::G_ROTR: {
764 auto MaybeAmtOp =
765 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
766 if (!MaybeAmtOp)
767 break;
768
769 Register SrcReg = MI.getOperand(1).getReg();
770 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
771
772 unsigned Amt = MaybeAmtOp->urem(BitWidth);
773
774 // Canonicalize to ROTR.
775 if (Opcode == TargetOpcode::G_ROTL)
776 Amt = BitWidth - Amt;
777
778 Known.Zero = Known.Zero.rotr(Amt);
779 Known.One = Known.One.rotr(Amt);
780 break;
781 }
782 case TargetOpcode::G_FSHL:
783 case TargetOpcode::G_FSHR: {
784 auto MaybeAmtOp =
785 isConstantOrConstantSplatVector(MI.getOperand(3).getReg(), MRI);
786 if (!MaybeAmtOp)
787 break;
788
789 const APInt Amt = *MaybeAmtOp;
790 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
791 Depth + 1);
792 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
793 Depth + 1);
794 Known = Opcode == TargetOpcode::G_FSHL
795 ? KnownBits::fshl(Known, Known2, Amt)
796 : KnownBits::fshr(Known, Known2, Amt);
797 break;
798 }
799 case TargetOpcode::G_INTTOPTR:
800 case TargetOpcode::G_PTRTOINT:
801 if (DstTy.isVector())
802 break;
803 // Fall through and handle them the same as zext/trunc.
804 [[fallthrough]];
805 case TargetOpcode::G_ZEXT:
806 case TargetOpcode::G_TRUNC: {
807 Register SrcReg = MI.getOperand(1).getReg();
808 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
809 Known = Known.zextOrTrunc(BitWidth);
810 break;
811 }
812 case TargetOpcode::G_TRUNC_SSAT_S: {
813 Register SrcReg = MI.getOperand(1).getReg();
814 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
815 Known = Known.truncSSat(BitWidth);
816 break;
817 }
818 case TargetOpcode::G_TRUNC_SSAT_U: {
819 Register SrcReg = MI.getOperand(1).getReg();
820 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
821 Known = Known.truncSSatU(BitWidth);
822 break;
823 }
824 case TargetOpcode::G_TRUNC_USAT_U: {
825 Register SrcReg = MI.getOperand(1).getReg();
826 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
827 Known = Known.truncUSat(BitWidth);
828 break;
829 }
830 case TargetOpcode::G_ASSERT_ZEXT: {
831 Register SrcReg = MI.getOperand(1).getReg();
832 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
833
834 unsigned SrcBitWidth = MI.getOperand(2).getImm();
835 assert(SrcBitWidth && "SrcBitWidth can't be zero");
836 APInt InMask = APInt::getLowBitsSet(BitWidth, SrcBitWidth);
837 Known.Zero |= (~InMask);
838 Known.One &= (~Known.Zero);
839 break;
840 }
841 case TargetOpcode::G_ASSERT_ALIGN: {
842 int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
843
844 // TODO: Should use maximum with source
845 // If a node is guaranteed to be aligned, set low zero bits accordingly as
846 // well as clearing one bits.
847 Known.Zero.setLowBits(LogOfAlign);
848 Known.One.clearLowBits(LogOfAlign);
849 break;
850 }
851 case TargetOpcode::G_MERGE_VALUES: {
852 unsigned NumOps = MI.getNumOperands();
853 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
854
855 for (unsigned I = 0; I != NumOps - 1; ++I) {
856 KnownBits SrcOpKnown;
857 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
858 DemandedElts, Depth + 1);
859 Known.insertBits(SrcOpKnown, I * OpSize);
860 }
861 break;
862 }
863 case TargetOpcode::G_UNMERGE_VALUES: {
864 unsigned NumOps = MI.getNumOperands();
865 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
866 LLT SrcTy = MRI.getType(SrcReg);
867
868 if (SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType())
869 return; // TODO: Handle vector->subelement unmerges
870
871 // Figure out the result operand index
872 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
873
874 APInt SubDemandedElts = DemandedElts;
875 if (SrcTy.isVector()) {
876 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
877 SubDemandedElts =
878 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
879 }
880
881 KnownBits SrcOpKnown;
882 computeKnownBitsImpl(SrcReg, SrcOpKnown, SubDemandedElts, Depth + 1);
883
884 if (SrcTy.isVector())
885 Known = std::move(SrcOpKnown);
886 else
887 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
888 break;
889 }
890 case TargetOpcode::G_BSWAP: {
891 Register SrcReg = MI.getOperand(1).getReg();
892 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
893 Known = Known.byteSwap();
894 break;
895 }
896 case TargetOpcode::G_BITREVERSE: {
897 Register SrcReg = MI.getOperand(1).getReg();
898 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
899 Known = Known.reverseBits();
900 break;
901 }
902 case TargetOpcode::G_CTPOP: {
903 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
904 Depth + 1);
905 // We can bound the space the count needs. Also, bits known to be zero
906 // can't contribute to the population.
907 unsigned BitsPossiblySet = Known2.countMaxPopulation();
908 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
909 Known.Zero.setBitsFrom(LowBits);
910 // TODO: we could bound Known.One using the lower bound on the number of
911 // bits which might be set provided by popcnt KnownOne2.
912 break;
913 }
914 case TargetOpcode::G_UBFX: {
915 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
916 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
917 Depth + 1);
918 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
919 Depth + 1);
920 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
921 Depth + 1);
922 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
923 break;
924 }
925 case TargetOpcode::G_SBFX: {
926 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
927 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
928 Depth + 1);
929 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
930 Depth + 1);
931 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
932 Depth + 1);
933 OffsetKnown = OffsetKnown.sext(BitWidth);
934 WidthKnown = WidthKnown.sext(BitWidth);
935 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
936 // Sign extend the extracted value using shift left and arithmetic shift
937 // right.
939 KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
940 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
941 break;
942 }
943 case TargetOpcode::G_UADDO:
944 case TargetOpcode::G_UADDE:
945 case TargetOpcode::G_SADDO:
946 case TargetOpcode::G_SADDE: {
947 if (MI.getOperand(1).getReg() == R) {
948 // If we know the result of a compare has the top bits zero, use this
949 // info.
950 if (TL.getBooleanContents(DstTy.isVector(), false) ==
952 BitWidth > 1)
953 Known.Zero.setBitsFrom(1);
954 break;
955 }
956
957 assert(MI.getOperand(0).getReg() == R &&
958 "We only compute knownbits for the sum here.");
959 // With [US]ADDE, a carry bit may be added in.
960 KnownBits Carry(1);
961 if (Opcode == TargetOpcode::G_UADDE || Opcode == TargetOpcode::G_SADDE) {
962 computeKnownBitsImpl(MI.getOperand(4).getReg(), Carry, DemandedElts,
963 Depth + 1);
964 // Carry has bit width 1
965 Carry = Carry.trunc(1);
966 } else {
967 Carry.setAllZero();
968 }
969
970 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
971 Depth + 1);
972 computeKnownBitsImpl(MI.getOperand(3).getReg(), Known2, DemandedElts,
973 Depth + 1);
974 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
975 break;
976 }
977 case TargetOpcode::G_USUBO:
978 case TargetOpcode::G_USUBE:
979 case TargetOpcode::G_SSUBO:
980 case TargetOpcode::G_SSUBE:
981 case TargetOpcode::G_UMULO:
982 case TargetOpcode::G_SMULO: {
983 if (MI.getOperand(1).getReg() == R) {
984 // If we know the result of a compare has the top bits zero, use this
985 // info.
986 if (TL.getBooleanContents(DstTy.isVector(), false) ==
988 BitWidth > 1)
989 Known.Zero.setBitsFrom(1);
990 }
991 break;
992 }
993 case TargetOpcode::G_CTTZ:
994 case TargetOpcode::G_CTTZ_ZERO_POISON: {
995 KnownBits SrcOpKnown;
996 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
997 Depth + 1);
998 // If we have a known 1, its position is our upper bound
999 unsigned PossibleTZ = SrcOpKnown.countMaxTrailingZeros();
1000 unsigned LowBits = llvm::bit_width(PossibleTZ);
1001 Known.Zero.setBitsFrom(LowBits);
1002 break;
1003 }
1004 case TargetOpcode::G_CTLZ:
1005 case TargetOpcode::G_CTLZ_ZERO_POISON: {
1006 KnownBits SrcOpKnown;
1007 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
1008 Depth + 1);
1009 // If we have a known 1, its position is our upper bound.
1010 unsigned PossibleLZ = SrcOpKnown.countMaxLeadingZeros();
1011 unsigned LowBits = llvm::bit_width(PossibleLZ);
1012 Known.Zero.setBitsFrom(LowBits);
1013 break;
1014 }
1015 case TargetOpcode::G_CTLS: {
1016 Register Reg = MI.getOperand(1).getReg();
1017 unsigned MinRedundantSignBits = computeNumSignBits(Reg, Depth + 1) - 1;
1018
1019 unsigned MaxUpperRedundantSignBits = MRI.getType(Reg).getScalarSizeInBits();
1020
1021 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
1022 APInt(BitWidth, MaxUpperRedundantSignBits));
1023
1024 Known = Range.toKnownBits();
1025 break;
1026 }
1027 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1029 Register InVec = Extract.getVectorReg();
1030 Register EltNo = Extract.getIndexReg();
1031
1032 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1033
1034 LLT VecVT = MRI.getType(InVec);
1035 // computeKnownBits not yet implemented for scalable vectors.
1036 if (VecVT.isScalableVector())
1037 break;
1038
1039 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
1040 const unsigned NumSrcElts = VecVT.getNumElements();
1041 // A return type different from the vector's element type may lead to
1042 // issues with pattern selection. Bail out to avoid that.
1043 if (BitWidth > EltBitWidth)
1044 break;
1045
1046 Known.Zero.setAllBits();
1047 Known.One.setAllBits();
1048
1049 // If we know the element index, just demand that vector element, else for
1050 // an unknown element index, ignore DemandedElts and demand them all.
1051 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
1052 if (ConstEltNo && ConstEltNo->ult(NumSrcElts))
1053 DemandedSrcElts =
1054 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
1055
1056 computeKnownBitsImpl(InVec, Known, DemandedSrcElts, Depth + 1);
1057 break;
1058 }
1059 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1061 Register InVec = Insert.getVectorReg();
1062 Register InVal = Insert.getElementReg();
1063 Register EltNo = Insert.getIndexReg();
1064 LLT VecVT = MRI.getType(InVec);
1065
1066 if (VecVT.isScalableVector())
1067 break;
1068
1069 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
1070 unsigned NumElts = VecVT.getNumElements();
1071
1072 bool DemandedVal = true;
1073 APInt DemandedVecElts = DemandedElts;
1074 if (ConstEltNo && ConstEltNo->ult(NumElts)) {
1075 unsigned EltIdx = ConstEltNo->getZExtValue();
1076 DemandedVal = !!DemandedElts[EltIdx];
1077 DemandedVecElts.clearBit(EltIdx);
1078 }
1079 Known.setAllConflict();
1080 if (DemandedVal) {
1081 computeKnownBitsImpl(InVal, Known2, APInt(1, 1), Depth + 1);
1082 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
1083 }
1084 if (!!DemandedVecElts) {
1085 computeKnownBitsImpl(InVec, Known2, DemandedVecElts, Depth + 1);
1086 Known = Known.intersectWith(Known2);
1087 }
1088 break;
1089 }
1090 case TargetOpcode::G_INSERT_SUBVECTOR: {
1092 Register Src = Insert.getBigVec();
1093 Register Sub = Insert.getSubVec();
1094 uint64_t Idx = Insert.getIndexImm();
1095 LLT SrcTy = MRI.getType(Src);
1096 LLT SubTy = MRI.getType(Sub);
1097 APInt DemandedSubElts;
1098 APInt DemandedSrcElts;
1099
1100 if (SrcTy.isScalableVector()) {
1101 DemandedSubElts = SubTy.isScalableVector()
1102 ? APInt(1, 1)
1104 DemandedSrcElts = APInt(1, 1);
1105 } else {
1106 unsigned NumSubElts = SubTy.getNumElements();
1107 DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
1108 DemandedSrcElts = DemandedElts;
1109 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
1110 }
1111
1112 Known.setAllConflict();
1113 if (!!DemandedSubElts) {
1114 computeKnownBitsImpl(Sub, Known2, DemandedSubElts, Depth + 1);
1115 Known = Known.intersectWith(Known2);
1116 if (Known.isUnknown())
1117 break;
1118 }
1119
1120 if (!!DemandedSrcElts) {
1121 computeKnownBitsImpl(Src, Known2, DemandedSrcElts, Depth + 1);
1122 Known = Known.intersectWith(Known2);
1123 }
1124
1125 break;
1126 }
1127 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
1128 Register SrcReg = MI.getOperand(1).getReg();
1129 LLT SrcTy = MRI.getType(SrcReg);
1130 APInt DemandedSrcElts;
1131 if (SrcTy.isScalableVector()) {
1132 DemandedSrcElts = APInt(1, 1);
1133 } else {
1134 uint64_t Idx = MI.getOperand(2).getImm();
1135 unsigned NumSrcElts = SrcTy.getNumElements();
1136 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
1137 }
1138 computeKnownBitsImpl(SrcReg, Known, DemandedSrcElts, Depth + 1);
1139 break;
1140 }
1141 case TargetOpcode::G_SHUFFLE_VECTOR: {
1142 APInt DemandedLHS, DemandedRHS;
1143 // Collect the known bits that are shared by every vector element referenced
1144 // by the shuffle.
1145 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1146 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
1147 DemandedElts, DemandedLHS, DemandedRHS))
1148 break;
1149
1150 // Known bits are the values that are shared by every demanded element.
1151 Known.Zero.setAllBits();
1152 Known.One.setAllBits();
1153 if (!!DemandedLHS) {
1154 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
1155 Depth + 1);
1156 Known = Known.intersectWith(Known2);
1157 }
1158 // If we don't know any bits, early out.
1159 if (Known.isUnknown())
1160 break;
1161 if (!!DemandedRHS) {
1162 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
1163 Depth + 1);
1164 Known = Known.intersectWith(Known2);
1165 }
1166 break;
1167 }
1168 case TargetOpcode::G_CONCAT_VECTORS: {
1169 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
1170 break;
1171 // Split DemandedElts and test each of the demanded subvectors.
1172 Known.Zero.setAllBits();
1173 Known.One.setAllBits();
1174 unsigned NumSubVectorElts =
1175 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
1176
1177 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
1178 APInt DemandedSub =
1179 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
1180 if (!!DemandedSub) {
1181 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
1182
1183 Known = Known.intersectWith(Known2);
1184 }
1185 // If we don't know any bits, early out.
1186 if (Known.isUnknown())
1187 break;
1188 }
1189 break;
1190 }
1191 case TargetOpcode::G_VECTOR_COMPRESS: {
1192 // Each result lane is either a lane of the source vector or the passthru,
1193 // so the known bits are those shared by both.
1194 Register Vec = MI.getOperand(1).getReg();
1195 Register PassThru = MI.getOperand(3).getReg();
1196 computeKnownBitsImpl(PassThru, Known, DemandedElts, Depth + 1);
1197 // If we don't know any bits, early out.
1198 if (Known.isUnknown())
1199 break;
1200 // Compression can move any source lane to any result position, so all
1201 // source lanes are demanded.
1202 APInt DemandedSrcElts = APInt::getAllOnes(DemandedElts.getBitWidth());
1203 computeKnownBitsImpl(Vec, Known2, DemandedSrcElts, Depth + 1);
1204 Known = Known.intersectWith(Known2);
1205 break;
1206 }
1207 case TargetOpcode::G_ABS: {
1208 Register SrcReg = MI.getOperand(1).getReg();
1209 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
1210 Known = Known.abs();
1211 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
1212 1);
1213 break;
1214 }
1215 }
1216
1218}
1219
1220void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
1221 FPClassTest InterestedClasses,
1222 unsigned Depth) {
1223 LLT Ty = MRI.getType(R);
1224 APInt DemandedElts =
1225 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1226 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
1227}
1228
1229/// Return true if this value is known to be the fractional part x - floor(x),
1230/// which lies in [0, 1). This implies the value cannot introduce overflow in a
1231/// fmul when the other operand is known finite.
1233 using namespace MIPatternMatch;
1234 Register SubX;
1235 return mi_match(R, MRI, m_GFSub(m_Reg(SubX), m_GFFloor(m_DeferredReg(SubX))));
1236}
1237
1238void GISelValueTracking::computeKnownFPClassForFPTrunc(
1239 const MachineInstr &MI, const APInt &DemandedElts,
1240 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
1241 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
1242 fcNone)
1243 return;
1244
1245 Register Val = MI.getOperand(1).getReg();
1246 KnownFPClass KnownSrc;
1247 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1248 Depth + 1);
1249 Known = KnownFPClass::fptrunc(KnownSrc);
1250}
1251
1252void GISelValueTracking::computeKnownFPClass(Register R,
1253 const APInt &DemandedElts,
1254 FPClassTest InterestedClasses,
1256 unsigned Depth) {
1257 assert(Known.isUnknown() && "should not be called with known information");
1258
1259 if (!DemandedElts) {
1260 // No demanded elts, better to assume we don't know anything.
1261 Known.resetAll();
1262 return;
1263 }
1264
1265 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1266
1267 MachineInstr &MI = *MRI.getVRegDef(R);
1268 unsigned Opcode = MI.getOpcode();
1269 LLT DstTy = MRI.getType(R);
1270
1271 if (!DstTy.isValid()) {
1272 Known.resetAll();
1273 return;
1274 }
1275
1276 if (auto Cst = GFConstant::getConstant(R, MRI)) {
1277 switch (Cst->getKind()) {
1279 auto APF = Cst->getScalarValue();
1280 Known.setKnownFPClasses(APF.classify());
1281 Known.setSignBit(APF.isNegative());
1282 break;
1283 }
1285 Known.setKnownFPClasses(fcNone);
1286 bool SignBitAllZero = true;
1287 bool SignBitAllOne = true;
1288
1289 for (auto C : *Cst) {
1290 Known.setKnownFPClasses(Known.getKnownFPClasses() | C.classify());
1291 if (C.isNegative())
1292 SignBitAllZero = false;
1293 else
1294 SignBitAllOne = false;
1295 }
1296
1297 if (SignBitAllOne != SignBitAllZero)
1298 Known.setSignBit(SignBitAllOne);
1299
1300 break;
1301 }
1303 Known.resetAll();
1304 break;
1305 }
1306 }
1307
1308 return;
1309 }
1310
1311 FPClassTest KnownNotFromFlags = fcNone;
1313 KnownNotFromFlags |= fcNan;
1315 KnownNotFromFlags |= fcInf;
1316
1317 // We no longer need to find out about these bits from inputs if we can
1318 // assume this from flags/attributes.
1319 InterestedClasses &= ~KnownNotFromFlags;
1320
1321 llvm::scope_exit ClearClassesFromFlags(
1322 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1323
1324 // All recursive calls that increase depth must come after this.
1326 return;
1327
1328 const MachineFunction *MF = MI.getMF();
1329
1330 switch (Opcode) {
1331 default:
1332 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1333 Depth);
1334 break;
1335 case TargetOpcode::G_FNEG: {
1336 Register Val = MI.getOperand(1).getReg();
1337 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1338 Known.fneg();
1339 break;
1340 }
1341 case TargetOpcode::G_SELECT: {
1342 GSelect &SelMI = cast<GSelect>(MI);
1343 Register Cond = SelMI.getCondReg();
1344 Register LHS = SelMI.getTrueReg();
1345 Register RHS = SelMI.getFalseReg();
1346
1347 FPClassTest FilterLHS = fcAllFlags;
1348 FPClassTest FilterRHS = fcAllFlags;
1349
1350 Register TestedValue;
1351 FPClassTest MaskIfTrue = fcAllFlags;
1352 FPClassTest MaskIfFalse = fcAllFlags;
1353 FPClassTest ClassVal = fcNone;
1354
1355 CmpInst::Predicate Pred;
1356 Register CmpLHS, CmpRHS;
1357 if (mi_match(Cond, MRI,
1358 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1359 // If the select filters out a value based on the class, it no longer
1360 // participates in the class of the result
1361
1362 // TODO: In some degenerate cases we can infer something if we try again
1363 // without looking through sign operations.
1364 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1365 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1366 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1367 } else if (mi_match(
1368 Cond, MRI,
1369 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1370 FPClassTest TestedMask = ClassVal;
1371 MaskIfTrue = TestedMask;
1372 MaskIfFalse = ~TestedMask;
1373 }
1374
1375 if (TestedValue == LHS) {
1376 // match !isnan(x) ? x : y
1377 FilterLHS = MaskIfTrue;
1378 } else if (TestedValue == RHS) { // && IsExactClass
1379 // match !isnan(x) ? y : x
1380 FilterRHS = MaskIfFalse;
1381 }
1382
1383 KnownFPClass Known2;
1384 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1385 Depth + 1);
1386 Known.setKnownFPClasses(Known.getKnownFPClasses() & FilterLHS);
1387
1388 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1389 Known2, Depth + 1);
1390 Known2.setKnownFPClasses(Known2.getKnownFPClasses() & FilterRHS);
1391
1392 Known |= Known2;
1393 break;
1394 }
1395 case TargetOpcode::G_FCOPYSIGN: {
1396 Register Magnitude = MI.getOperand(1).getReg();
1397 Register Sign = MI.getOperand(2).getReg();
1398
1399 KnownFPClass KnownSign;
1400
1401 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1402 Depth + 1);
1403 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1404 Depth + 1);
1405 Known.copysign(KnownSign);
1406 break;
1407 }
1408 case TargetOpcode::G_FMA:
1409 case TargetOpcode::G_STRICT_FMA:
1410 case TargetOpcode::G_FMAD: {
1411 if ((InterestedClasses & fcNegative) == fcNone)
1412 break;
1413
1414 Register A = MI.getOperand(1).getReg();
1415 Register B = MI.getOperand(2).getReg();
1416 Register C = MI.getOperand(3).getReg();
1417
1418 DenormalMode Mode =
1419 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1420
1421 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1422 // x * x + y
1423 KnownFPClass KnownSrc, KnownAddend;
1424 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
1425 Depth + 1);
1426 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc,
1427 Depth + 1);
1428 if (KnownNotFromFlags) {
1429 KnownSrc.knownNot(KnownNotFromFlags);
1430 KnownAddend.knownNot(KnownNotFromFlags);
1431 }
1432 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
1433 } else {
1434 KnownFPClass KnownSrc[3];
1435 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1436 Depth + 1);
1437 if (KnownSrc[0].isUnknown())
1438 break;
1439 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1440 Depth + 1);
1441 if (KnownSrc[1].isUnknown())
1442 break;
1443 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1444 Depth + 1);
1445 if (KnownSrc[2].isUnknown())
1446 break;
1447 if (KnownNotFromFlags) {
1448 KnownSrc[0].knownNot(KnownNotFromFlags);
1449 KnownSrc[1].knownNot(KnownNotFromFlags);
1450 KnownSrc[2].knownNot(KnownNotFromFlags);
1451 }
1452 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
1453 }
1454 break;
1455 }
1456 case TargetOpcode::G_FSQRT:
1457 case TargetOpcode::G_STRICT_FSQRT: {
1458 KnownFPClass KnownSrc;
1459 FPClassTest InterestedSrcs = InterestedClasses;
1460 if (InterestedClasses & fcNan)
1461 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1462
1463 Register Val = MI.getOperand(1).getReg();
1464 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1465
1466 DenormalMode Mode =
1467 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1468 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1469 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1470 Known.knownNot(fcNegZero);
1471 break;
1472 }
1473 case TargetOpcode::G_FABS: {
1474 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1475 Register Val = MI.getOperand(1).getReg();
1476 // If we only care about the sign bit we don't need to inspect the
1477 // operand.
1478 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1479 Depth + 1);
1480 }
1481 Known.fabs();
1482 break;
1483 }
1484 case TargetOpcode::G_FATAN2: {
1485 FPClassTest InterestedY = InterestedClasses;
1486 FPClassTest InterestedX = InterestedClasses;
1487
1488 // We can rule out negative values if y cannot have a negative value.
1489 if ((InterestedClasses & fcNegFinite) != fcNone)
1490 InterestedY |= fcNegative;
1491
1492 // We can rule out positive values if y cannot have a positive value.
1493 if ((InterestedClasses & fcPosFinite) != fcNone)
1494 InterestedY |= fcPositive | fcNegSubnormal;
1495
1496 // We can rule out zero and subnormal if x cannot have a positive value.
1497 if ((InterestedClasses & (fcZero | fcSubnormal)) != fcNone)
1498 InterestedX |= fcPositive | fcNegSubnormal;
1499
1500 Register Y = MI.getOperand(1).getReg();
1501 Register X = MI.getOperand(2).getReg();
1502 KnownFPClass KnownY, KnownX;
1503 computeKnownFPClass(Y, DemandedElts, InterestedY, KnownY, Depth + 1);
1504 computeKnownFPClass(X, DemandedElts, InterestedX, KnownX, Depth + 1);
1505 DenormalMode Mode =
1506 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1507 Known = KnownFPClass::atan2(KnownY, KnownX, Mode);
1508 break;
1509 }
1510 case TargetOpcode::G_FSINH: {
1511 Register Val = MI.getOperand(1).getReg();
1512 KnownFPClass KnownSrc;
1513 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1514 Depth + 1);
1515 Known = KnownFPClass::sinh(KnownSrc);
1516 break;
1517 }
1518 case TargetOpcode::G_FCOSH: {
1519 Register Val = MI.getOperand(1).getReg();
1520 KnownFPClass KnownSrc;
1521 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1522 Depth + 1);
1523 Known = KnownFPClass::cosh(KnownSrc);
1524 break;
1525 }
1526 case TargetOpcode::G_FTANH: {
1527 Register Val = MI.getOperand(1).getReg();
1528 KnownFPClass KnownSrc;
1529 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1530 Depth + 1);
1531 Known = KnownFPClass::tanh(KnownSrc);
1532 break;
1533 }
1534 case TargetOpcode::G_FASIN: {
1535 Register Val = MI.getOperand(1).getReg();
1536 KnownFPClass KnownSrc;
1537 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1538 Depth + 1);
1539 Known = KnownFPClass::asin(KnownSrc);
1540 break;
1541 }
1542 case TargetOpcode::G_FACOS: {
1543 Register Val = MI.getOperand(1).getReg();
1544 KnownFPClass KnownSrc;
1545 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1546 Depth + 1);
1547 Known = KnownFPClass::acos(KnownSrc);
1548 break;
1549 }
1550 case TargetOpcode::G_FATAN: {
1551 Register Val = MI.getOperand(1).getReg();
1552 KnownFPClass KnownSrc;
1553 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1554 Depth + 1);
1555 Known = KnownFPClass::atan(KnownSrc);
1556 break;
1557 }
1558 case TargetOpcode::G_FTAN: {
1559 Register Val = MI.getOperand(1).getReg();
1560 KnownFPClass KnownSrc;
1561 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1562 Depth + 1);
1563 Known = KnownFPClass::tan(KnownSrc);
1564 break;
1565 }
1566 case TargetOpcode::G_FSIN:
1567 case TargetOpcode::G_FCOS: {
1568 // Return NaN on infinite inputs.
1569 Register Val = MI.getOperand(1).getReg();
1570 KnownFPClass KnownSrc;
1571 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1572 Depth + 1);
1573 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1574 : KnownFPClass::sin(KnownSrc);
1575 break;
1576 }
1577 case TargetOpcode::G_FSINCOS: {
1578 // Operand layout: (sin_dst, cos_dst, src)
1579 Register Src = MI.getOperand(2).getReg();
1580 KnownFPClass KnownSrc;
1581 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1582 Depth + 1);
1583 if (R == MI.getOperand(0).getReg())
1584 Known = KnownFPClass::sin(KnownSrc);
1585 else
1586 Known = KnownFPClass::cos(KnownSrc);
1587 break;
1588 }
1589 case TargetOpcode::G_FMAXNUM:
1590 case TargetOpcode::G_FMINNUM:
1591 case TargetOpcode::G_FMINNUM_IEEE:
1592 case TargetOpcode::G_FMAXIMUM:
1593 case TargetOpcode::G_FMINIMUM:
1594 case TargetOpcode::G_FMAXNUM_IEEE:
1595 case TargetOpcode::G_FMAXIMUMNUM:
1596 case TargetOpcode::G_FMINIMUMNUM: {
1597 Register LHS = MI.getOperand(1).getReg();
1598 Register RHS = MI.getOperand(2).getReg();
1599 KnownFPClass KnownLHS, KnownRHS;
1600
1601 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1602 Depth + 1);
1603 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1604 Depth + 1);
1605
1607 switch (Opcode) {
1608 case TargetOpcode::G_FMINIMUM:
1610 break;
1611 case TargetOpcode::G_FMAXIMUM:
1613 break;
1614 case TargetOpcode::G_FMINIMUMNUM:
1616 break;
1617 case TargetOpcode::G_FMAXIMUMNUM:
1619 break;
1620 case TargetOpcode::G_FMINNUM:
1621 case TargetOpcode::G_FMINNUM_IEEE:
1623 break;
1624 case TargetOpcode::G_FMAXNUM:
1625 case TargetOpcode::G_FMAXNUM_IEEE:
1627 break;
1628 default:
1629 llvm_unreachable("unhandled min/max opcode");
1630 }
1631
1632 DenormalMode Mode =
1633 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1634 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1635 break;
1636 }
1637 case TargetOpcode::G_FCANONICALIZE: {
1638 Register Val = MI.getOperand(1).getReg();
1639 KnownFPClass KnownSrc;
1640 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1641 Depth + 1);
1642
1643 LLT Ty = MRI.getType(Val).getScalarType();
1644 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1645 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1646 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
1647 break;
1648 }
1649 case TargetOpcode::G_VECREDUCE_FMAX:
1650 case TargetOpcode::G_VECREDUCE_FMIN:
1651 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1652 case TargetOpcode::G_VECREDUCE_FMINIMUM:
1653 case TargetOpcode::G_VECREDUCE_FMAXIMUMNUM:
1654 case TargetOpcode::G_VECREDUCE_FMINIMUMNUM: {
1655 Register Val = MI.getOperand(1).getReg();
1656 // reduce min/max will choose an element from one of the vector elements,
1657 // so we can infer and class information that is common to all elements.
1658
1659 Known =
1660 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1661 // Can only propagate sign if output is never NaN.
1662 if (!Known.isKnownNeverNaN())
1663 Known.setSignBit(std::nullopt);
1664 break;
1665 }
1666 case TargetOpcode::G_FFLOOR:
1667 case TargetOpcode::G_FCEIL:
1668 case TargetOpcode::G_FRINT:
1669 case TargetOpcode::G_FNEARBYINT:
1670 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1671 case TargetOpcode::G_INTRINSIC_ROUND:
1672 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1673 case TargetOpcode::G_INTRINSIC_TRUNC: {
1674 Register Val = MI.getOperand(1).getReg();
1675 KnownFPClass KnownSrc;
1676 FPClassTest InterestedSrcs = InterestedClasses;
1677 if (InterestedSrcs & fcPosFinite)
1678 InterestedSrcs |= fcPosFinite;
1679 if (InterestedSrcs & fcNegFinite)
1680 InterestedSrcs |= fcNegFinite;
1681 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1682
1683 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1684 bool IsTrunc = Opcode == TargetOpcode::G_INTRINSIC_TRUNC;
1685 Known = KnownFPClass::roundToIntegral(KnownSrc, IsTrunc,
1686 /*IsMultiUnitFPType=*/false);
1687 break;
1688 }
1689 case TargetOpcode::G_FEXP:
1690 case TargetOpcode::G_FEXP2:
1691 case TargetOpcode::G_FEXP10: {
1692 Register Val = MI.getOperand(1).getReg();
1693 KnownFPClass KnownSrc;
1694 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1695 Depth + 1);
1696 Known = KnownFPClass::exp(KnownSrc);
1697 break;
1698 }
1699 case TargetOpcode::G_FLOG:
1700 case TargetOpcode::G_FLOG2:
1701 case TargetOpcode::G_FLOG10: {
1702 FPClassTest InterestedSrcs = fcNone;
1703
1704 // log(negative) produces NaN.
1705 if ((InterestedClasses & fcNan) != fcNone)
1706 InterestedSrcs |= fcNan | fcNegative;
1707
1708 // log(logical-zero) produces negative infinity.
1709 if ((InterestedClasses & fcNegInf) != fcNone)
1710 InterestedSrcs |= fcZero | fcSubnormal;
1711
1712 // log(x) < -0.0 if x < +1.0
1713 if ((InterestedClasses & fcNegNormal) != fcNone)
1714 InterestedSrcs |= fcPosSubnormal | fcPosNormal;
1715
1716 // log(x) >= +0.0 if x >= +1.0
1717 if ((InterestedClasses & (fcPosZero | fcPosNormal)) != fcNone)
1718 InterestedSrcs |= fcPosNormal;
1719
1720 // log(x) is positive infinity iff x is positive infinity.
1721 if ((InterestedClasses & fcPosInf) != fcNone)
1722 InterestedSrcs |= fcPosInf;
1723
1724 Register Val = MI.getOperand(1).getReg();
1725 KnownFPClass KnownSrc;
1726 if (InterestedSrcs != fcNone)
1727 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1728 Depth + 1);
1729
1730 LLT Ty = MRI.getType(Val).getScalarType();
1731 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1732 DenormalMode Mode = MF->getDenormalMode(FltSem);
1733 Known = KnownFPClass::log(KnownSrc, Mode);
1734 break;
1735 }
1736 case TargetOpcode::G_FPOW: {
1737 const bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1738 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1739 if (!WantNaN && !WantNegative)
1740 break;
1741
1742 FPClassTest InterestedLHS = fcNone;
1743 FPClassTest InterestedRHS = fcNone;
1744 if (WantNaN) {
1745 // pow may return NaN if one of the arguments is NaN. NaN may be produced
1746 // from a non-zero-finite-negative base and a non-integer exponent.
1747 InterestedLHS |= fcNan | fcNegNormal | fcNegSubnormal;
1748 InterestedRHS |= fcNan;
1749 }
1750 if (WantNegative) {
1751 // A negative value is returned when a negative base is raised to an odd
1752 // integer power. Only normal values can be odd integers.
1753 InterestedLHS |= fcNegative;
1754 InterestedRHS |= fcNormal;
1755 }
1756
1757 KnownFPClass KnownLHS;
1758 computeKnownFPClass(MI.getOperand(1).getReg(), DemandedElts, InterestedLHS,
1759 KnownLHS, Depth + 1);
1760
1761 // If the LHS is unknown, then querying the RHS is only useful for rare edge
1762 // cases.
1763 if (KnownLHS.isUnknown())
1764 break;
1765
1766 KnownFPClass KnownRHS;
1767 computeKnownFPClass(MI.getOperand(2).getReg(), DemandedElts, InterestedRHS,
1768 KnownRHS, Depth + 1);
1769 Known = KnownFPClass::pow(KnownLHS, KnownRHS);
1770 break;
1771 }
1772 case TargetOpcode::G_FPOWI: {
1773 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1774 break;
1775
1776 Register Exp = MI.getOperand(2).getReg();
1777 LLT ExpTy = MRI.getType(Exp);
1778 KnownBits ExponentKnownBits = getKnownBits(
1779 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1780
1781 FPClassTest InterestedSrcs = fcNone;
1782 if (InterestedClasses & fcNan)
1783 InterestedSrcs |= fcNan;
1784 if (!ExponentKnownBits.isZero()) {
1785 if (InterestedClasses & fcInf)
1786 InterestedSrcs |= fcFinite | fcInf;
1787 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1788 InterestedSrcs |= fcNegative;
1789 }
1790
1791 KnownFPClass KnownSrc;
1792 if (InterestedSrcs != fcNone) {
1793 Register Val = MI.getOperand(1).getReg();
1794 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1795 Depth + 1);
1796 }
1797
1798 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1799 break;
1800 }
1801 case TargetOpcode::G_FLDEXP:
1802 case TargetOpcode::G_STRICT_FLDEXP: {
1803 Register Val = MI.getOperand(1).getReg();
1804 KnownFPClass KnownSrc;
1805 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1806 Depth + 1);
1807
1808 // Can refine inf/zero handling based on the exponent operand.
1809 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1810 KnownBits ExpBits;
1811 if ((KnownSrc.getKnownFPClasses() & ExpInfoMask) != fcNone) {
1812 Register ExpReg = MI.getOperand(2).getReg();
1813 LLT ExpTy = MRI.getType(ExpReg);
1814 ExpBits = getKnownBits(
1815 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1816 }
1817
1818 LLT ScalarTy = DstTy.getScalarType();
1819 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1820 DenormalMode Mode = MF->getDenormalMode(Flt);
1821 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1822 break;
1823 }
1824 case TargetOpcode::G_FADD:
1825 case TargetOpcode::G_STRICT_FADD:
1826 case TargetOpcode::G_FSUB:
1827 case TargetOpcode::G_STRICT_FSUB: {
1828 Register LHS = MI.getOperand(1).getReg();
1829 Register RHS = MI.getOperand(2).getReg();
1830 bool IsAdd = (Opcode == TargetOpcode::G_FADD ||
1831 Opcode == TargetOpcode::G_STRICT_FADD);
1832 bool WantNegative =
1833 IsAdd &&
1834 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1835 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1836 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1837
1838 if (!WantNaN && !WantNegative && !WantNegZero) {
1839 break;
1840 }
1841
1842 DenormalMode Mode =
1843 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1844
1845 FPClassTest InterestedSrcs = InterestedClasses;
1846 if (WantNegative)
1847 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1848 if (InterestedClasses & fcNan)
1849 InterestedSrcs |= fcInf;
1850
1851 // Special case fadd x, x (canonical form of fmul x, 2).
1852 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1853 KnownFPClass KnownSelf;
1854 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1855 Depth + 1);
1856 Known = KnownFPClass::fadd_self(KnownSelf, Mode);
1857 break;
1858 }
1859
1860 KnownFPClass KnownLHS, KnownRHS;
1861 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1862
1863 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1864 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1865 WantNegZero || !IsAdd) {
1866 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1867 // there's no point.
1868 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1869 Depth + 1);
1870 }
1871
1872 if (IsAdd)
1873 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, Mode);
1874 else
1875 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
1876 break;
1877 }
1878 case TargetOpcode::G_FMUL:
1879 case TargetOpcode::G_STRICT_FMUL: {
1880 Register LHS = MI.getOperand(1).getReg();
1881 Register RHS = MI.getOperand(2).getReg();
1882 DenormalMode Mode =
1883 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1884
1885 // X * X is always non-negative or a NaN (use square() for precision).
1886 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1887 KnownFPClass KnownSrc;
1888 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
1889 Known = KnownFPClass::square(KnownSrc, Mode);
1890 } else {
1891 // If RHS is a scalar constant, use the more precise APFloat overload.
1892 auto RHSCst = GFConstant::getConstant(RHS, MRI);
1893 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
1894 KnownFPClass KnownLHS;
1895 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1896 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
1897 } else {
1898 KnownFPClass KnownLHS, KnownRHS;
1899 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1900 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1901 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
1902
1903 // If one operand is known |x| <= 1 and the other is finite, the
1904 // product cannot overflow to infinity.
1905 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
1906 Known.knownNot(fcInf);
1907 else if (KnownRHS.isKnownNever(fcInf) &&
1909 Known.knownNot(fcInf);
1910 }
1911 }
1912 break;
1913 }
1914 case TargetOpcode::G_FDIV: {
1915 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1916
1917 Register LHS = MI.getOperand(1).getReg();
1918 Register RHS = MI.getOperand(2).getReg();
1919
1920 DenormalMode Mode =
1921 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1922
1923 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1924 // X / X is always exactly 1.0 or a NaN.
1925 Known.setKnownFPClasses(fcPosNormal | fcNan);
1926
1927 if (!WantNan)
1928 break;
1929
1930 KnownFPClass KnownSrc;
1931 computeKnownFPClass(LHS, DemandedElts,
1932 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1933 Depth + 1);
1934 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
1935 break;
1936 }
1937
1938 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1939 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
1940 if (!WantNan && !WantNegative && !WantPositive)
1941 break;
1942
1943 KnownFPClass KnownLHS, KnownRHS;
1944 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1945
1946 bool KnowSomethingUseful =
1947 KnownRHS.isKnownNeverNaN() ||
1950
1951 if (KnowSomethingUseful)
1952 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1953
1954 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
1955 break;
1956 }
1957 case TargetOpcode::G_FREM: {
1958 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1959
1960 Register LHS = MI.getOperand(1).getReg();
1961 Register RHS = MI.getOperand(2).getReg();
1962
1963 Known.knownNot(fcInf);
1964
1965 DenormalMode Mode =
1966 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1967
1968 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1969 // X % X is always exactly [+-]0.0 or a NaN.
1970 Known.setKnownFPClasses(fcZero | fcNan);
1971
1972 if (!WantNan)
1973 break;
1974
1975 KnownFPClass KnownSrc;
1976 computeKnownFPClass(LHS, DemandedElts,
1977 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1978 Depth + 1);
1979 Known = KnownFPClass::frem_self(KnownSrc, Mode);
1980 break;
1981 }
1982
1983 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1984 const bool WantPositive = (InterestedClasses & fcPositive) != fcNone;
1985 if (!WantNan && !WantNegative && !WantPositive)
1986 break;
1987
1988 KnownFPClass KnownLHS, KnownRHS;
1989 computeKnownFPClass(RHS, DemandedElts, fcNan | fcInf | fcZero | fcNegative,
1990 KnownRHS, Depth + 1);
1991
1992 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
1993 KnownRHS.isKnownNever(fcNegative) ||
1994 KnownRHS.isKnownNever(fcPositive);
1995
1996 if (KnowSomethingUseful || WantPositive)
1997 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1998
1999 Known = KnownFPClass::frem(KnownLHS, KnownRHS, Mode);
2000
2001 break;
2002 }
2003 case TargetOpcode::G_FFREXP: {
2004 // Only handle the mantissa output (operand 0); the exponent is an integer.
2005 if (R != MI.getOperand(0).getReg())
2006 break;
2007 Register Src = MI.getOperand(2).getReg();
2008 KnownFPClass KnownSrc;
2009 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
2010 Depth + 1);
2011 DenormalMode Mode =
2012 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
2013 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
2014 break;
2015 }
2016 case TargetOpcode::G_FPEXT: {
2017 Register Src = MI.getOperand(1).getReg();
2018 KnownFPClass KnownSrc;
2019 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
2020 Depth + 1);
2021
2022 LLT DstScalarTy = DstTy.getScalarType();
2023 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
2024 LLT SrcTy = MRI.getType(Src).getScalarType();
2025 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
2026
2027 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem);
2028 break;
2029 }
2030 case TargetOpcode::G_FPTRUNC: {
2031 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
2032 Depth);
2033 break;
2034 }
2035 case TargetOpcode::G_SITOFP:
2036 case TargetOpcode::G_UITOFP: {
2037 // Cannot produce nan
2038 Known.knownNot(fcNan);
2039
2040 // Integers cannot be subnormal
2041 Known.knownNot(fcSubnormal);
2042
2043 // sitofp and uitofp turn into +0.0 for zero.
2044 Known.knownNot(fcNegZero);
2045
2046 // UIToFP is always non-negative regardless of known bits.
2047 if (Opcode == TargetOpcode::G_UITOFP)
2048 Known.signBitMustBeZero();
2049
2050 // Only compute known bits if we can learn something useful from them.
2051 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
2052 break;
2053
2054 Register Val = MI.getOperand(1).getReg();
2055 LLT Ty = MRI.getType(Val);
2056 KnownBits IntKnown = getKnownBits(
2057 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
2058
2059 // If the integer is non-zero, the result cannot be +0.0.
2060 if (IntKnown.isNonZero())
2061 Known.knownNot(fcPosZero);
2062
2063 if (Opcode == TargetOpcode::G_SITOFP) {
2064 // If the signed integer is known non-negative, the result is
2065 // non-negative. If the signed integer is known negative, the result is
2066 // negative.
2067 if (IntKnown.isNonNegative())
2068 Known.signBitMustBeZero();
2069 else if (IntKnown.isNegative())
2070 Known.signBitMustBeOne();
2071 }
2072
2073 if (InterestedClasses & fcInf) {
2074 LLT FPTy = DstTy.getScalarType();
2075 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
2076
2077 // Compute the effective integer width after removing known-zero leading
2078 // bits, to check if the result can overflow to infinity.
2079 int IntSize = IntKnown.getBitWidth();
2080 if (Opcode == TargetOpcode::G_UITOFP)
2081 IntSize -= IntKnown.countMinLeadingZeros();
2082 else
2083 IntSize -= IntKnown.countMinSignBits();
2084
2085 // If the exponent of the largest finite FP value can hold the largest
2086 // integer, the result of the cast must be finite.
2087 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
2088 Known.knownNot(fcInf);
2089 }
2090
2091 break;
2092 }
2093 // case TargetOpcode::G_MERGE_VALUES:
2094 case TargetOpcode::G_BUILD_VECTOR:
2095 case TargetOpcode::G_CONCAT_VECTORS: {
2096 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
2097
2098 if (!DstTy.isFixedVector())
2099 break;
2100
2101 bool First = true;
2102 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
2103 // We know the index we are inserting to, so clear it from Vec check.
2104 bool NeedsElt = DemandedElts[Idx];
2105
2106 // Do we demand the inserted element?
2107 if (NeedsElt) {
2108 Register Src = Merge.getSourceReg(Idx);
2109 if (First) {
2110 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
2111 First = false;
2112 } else {
2113 KnownFPClass Known2;
2114 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
2115 Known |= Known2;
2116 }
2117
2118 // If we don't know any bits, early out.
2119 if (Known.isUnknown())
2120 break;
2121 }
2122 }
2123
2124 break;
2125 }
2126 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
2127 // Look through extract element. If the index is non-constant or
2128 // out-of-range demand all elements, otherwise just the extracted
2129 // element.
2130 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
2131 Register Vec = Extract.getVectorReg();
2132 Register Idx = Extract.getIndexReg();
2133
2134 auto CIdx = getIConstantVRegVal(Idx, MRI);
2135
2136 LLT VecTy = MRI.getType(Vec);
2137
2138 if (VecTy.isFixedVector()) {
2139 unsigned NumElts = VecTy.getNumElements();
2140 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2141 if (CIdx && CIdx->ult(NumElts))
2142 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2143 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
2144 Depth + 1);
2145 }
2146
2147 break;
2148 }
2149 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2150 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
2151 Register Vec = Insert.getVectorReg();
2152 Register Elt = Insert.getElementReg();
2153 Register Idx = Insert.getIndexReg();
2154
2155 LLT VecTy = MRI.getType(Vec);
2156
2157 if (VecTy.isScalableVector())
2158 return;
2159
2160 auto CIdx = getIConstantVRegVal(Idx, MRI);
2161
2162 unsigned NumElts = DemandedElts.getBitWidth();
2163 APInt DemandedVecElts = DemandedElts;
2164 bool NeedsElt = true;
2165 // If we know the index we are inserting to, clear it from Vec check.
2166 if (CIdx && CIdx->ult(NumElts)) {
2167 DemandedVecElts.clearBit(CIdx->getZExtValue());
2168 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2169 }
2170
2171 // Do we demand the inserted element?
2172 if (NeedsElt) {
2173 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
2174 // If we don't know any bits, early out.
2175 if (Known.isUnknown())
2176 break;
2177 } else {
2178 Known.setKnownFPClasses(fcNone);
2179 }
2180
2181 // Do we need anymore elements from Vec?
2182 if (!DemandedVecElts.isZero()) {
2183 KnownFPClass Known2;
2184 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
2185 Depth + 1);
2186 Known |= Known2;
2187 }
2188
2189 break;
2190 }
2191 case TargetOpcode::G_SHUFFLE_VECTOR: {
2192 // For undef elements, we don't know anything about the common state of
2193 // the shuffle result.
2194 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
2195 APInt DemandedLHS, DemandedRHS;
2196 if (DstTy.isScalableVector()) {
2197 assert(DemandedElts == APInt(1, 1));
2198 DemandedLHS = DemandedRHS = DemandedElts;
2199 } else {
2200 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
2201 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
2202 DemandedLHS, DemandedRHS)) {
2203 Known.resetAll();
2204 return;
2205 }
2206 }
2207
2208 if (!!DemandedLHS) {
2209 Register LHS = Shuf.getSrc1Reg();
2210 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
2211 Depth + 1);
2212
2213 // If we don't know any bits, early out.
2214 if (Known.isUnknown())
2215 break;
2216 } else {
2217 Known.setKnownFPClasses(fcNone);
2218 }
2219
2220 if (!!DemandedRHS) {
2221 KnownFPClass Known2;
2222 Register RHS = Shuf.getSrc2Reg();
2223 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
2224 Depth + 1);
2225 Known |= Known2;
2226 }
2227 break;
2228 }
2229 case TargetOpcode::G_PHI: {
2230 // Cap PHI recursion below the global limit to avoid spending the entire
2231 // budget chasing loop back-edges (matches ValueTracking's
2232 // PhiRecursionLimit).
2234 break;
2235 // PHI's operands are a mix of registers and basic blocks interleaved.
2236 // We only care about the register ones.
2237 bool First = true;
2238 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
2239 const MachineOperand &Src = MI.getOperand(Idx);
2240 Register SrcReg = Src.getReg();
2241 if (First) {
2242 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
2243 Depth + 1);
2244 First = false;
2245 } else {
2246 KnownFPClass Known2;
2247 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
2248 Depth + 1);
2249 Known = Known.intersectWith(Known2);
2250 }
2251 if (Known.isUnknown())
2252 break;
2253 }
2254 break;
2255 }
2256 case TargetOpcode::COPY: {
2257 Register Src = MI.getOperand(1).getReg();
2258
2259 if (!Src.isVirtual())
2260 return;
2261
2262 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
2263 break;
2264 }
2265 }
2266}
2267
2269GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
2270 FPClassTest InterestedClasses,
2271 unsigned Depth) {
2272 KnownFPClass KnownClasses;
2273 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
2274 return KnownClasses;
2275}
2276
2277KnownFPClass GISelValueTracking::computeKnownFPClass(
2278 Register R, FPClassTest InterestedClasses, unsigned Depth) {
2280 computeKnownFPClass(R, Known, InterestedClasses, Depth);
2281 return Known;
2282}
2283
2284KnownFPClass GISelValueTracking::computeKnownFPClass(
2285 Register R, const APInt &DemandedElts, uint32_t Flags,
2286 FPClassTest InterestedClasses, unsigned Depth) {
2288 InterestedClasses &= ~fcNan;
2290 InterestedClasses &= ~fcInf;
2291
2292 KnownFPClass Result =
2293 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
2294
2296 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcNan);
2298 Result.setKnownFPClasses(Result.getKnownFPClasses() & ~fcInf);
2299 return Result;
2300}
2301
2302KnownFPClass GISelValueTracking::computeKnownFPClass(
2303 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
2304 LLT Ty = MRI.getType(R);
2305 APInt DemandedElts =
2306 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2307 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
2308}
2309
2311 const MachineInstr *DefMI = MRI.getVRegDef(Val);
2312 if (!DefMI)
2313 return false;
2314
2315 if (DefMI->getFlag(MachineInstr::FmNoNans))
2316 return true;
2317
2318 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
2319 // the value-tracking analysis for the SNaN-only case: if the defining op is
2320 // known to quiet sNaN, the output can never be an sNaN.
2321 if (SNaN) {
2322 switch (DefMI->getOpcode()) {
2323 default:
2324 break;
2325 case TargetOpcode::G_FADD:
2326 case TargetOpcode::G_STRICT_FADD:
2327 case TargetOpcode::G_FSUB:
2328 case TargetOpcode::G_STRICT_FSUB:
2329 case TargetOpcode::G_FMUL:
2330 case TargetOpcode::G_STRICT_FMUL:
2331 case TargetOpcode::G_FDIV:
2332 case TargetOpcode::G_FREM:
2333 case TargetOpcode::G_FMA:
2334 case TargetOpcode::G_STRICT_FMA:
2335 case TargetOpcode::G_FMAD:
2336 case TargetOpcode::G_FSQRT:
2337 case TargetOpcode::G_STRICT_FSQRT:
2338 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
2339 // exactly (LLVM LangRef: "never change anything except possibly the sign
2340 // bit"). They must NOT be listed here.
2341 case TargetOpcode::G_FSIN:
2342 case TargetOpcode::G_FCOS:
2343 case TargetOpcode::G_FSINCOS:
2344 case TargetOpcode::G_FTAN:
2345 case TargetOpcode::G_FASIN:
2346 case TargetOpcode::G_FACOS:
2347 case TargetOpcode::G_FATAN:
2348 case TargetOpcode::G_FATAN2:
2349 case TargetOpcode::G_FSINH:
2350 case TargetOpcode::G_FCOSH:
2351 case TargetOpcode::G_FTANH:
2352 case TargetOpcode::G_FEXP:
2353 case TargetOpcode::G_FEXP2:
2354 case TargetOpcode::G_FEXP10:
2355 case TargetOpcode::G_FLOG:
2356 case TargetOpcode::G_FLOG2:
2357 case TargetOpcode::G_FLOG10:
2358 case TargetOpcode::G_FPOW:
2359 case TargetOpcode::G_FPOWI:
2360 case TargetOpcode::G_FLDEXP:
2361 case TargetOpcode::G_STRICT_FLDEXP:
2362 case TargetOpcode::G_FFREXP:
2363 case TargetOpcode::G_INTRINSIC_TRUNC:
2364 case TargetOpcode::G_INTRINSIC_ROUND:
2365 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2366 case TargetOpcode::G_FFLOOR:
2367 case TargetOpcode::G_FCEIL:
2368 case TargetOpcode::G_FRINT:
2369 case TargetOpcode::G_FNEARBYINT:
2370 case TargetOpcode::G_FPEXT:
2371 case TargetOpcode::G_FPTRUNC:
2372 case TargetOpcode::G_FCANONICALIZE:
2373 case TargetOpcode::G_FMINNUM:
2374 case TargetOpcode::G_FMAXNUM:
2375 case TargetOpcode::G_FMINNUM_IEEE:
2376 case TargetOpcode::G_FMAXNUM_IEEE:
2377 case TargetOpcode::G_FMINIMUM:
2378 case TargetOpcode::G_FMAXIMUM:
2379 case TargetOpcode::G_FMINIMUMNUM:
2380 case TargetOpcode::G_FMAXIMUMNUM:
2381 return true;
2382 }
2383 }
2384
2385 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2386
2387 if (SNaN)
2388 return FPClass.isKnownNever(fcSNan);
2389
2390 return FPClass.isKnownNeverNaN();
2391}
2392
2394 KnownFPClass Known = computeKnownFPClass(Val, fcZero | fcSubnormal, Depth);
2395 LLT Ty = MRI.getType(Val).getScalarType();
2396 return Known.isKnownNeverLogicalZero(
2397 MF.getDenormalMode(getFltSemanticForLLT(Ty)));
2398}
2399
2400/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2401unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2402 const APInt &DemandedElts,
2403 unsigned Depth) {
2404 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2405 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2406 if (Src1SignBits == 1)
2407 return 1;
2408 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2409}
2410
2411/// Compute the known number of sign bits with attached range metadata in the
2412/// memory operand. If this is an extending load, accounts for the behavior of
2413/// the high bits.
2415 unsigned TyBits) {
2416 const MDNode *Ranges = Ld->getRanges();
2417 if (!Ranges)
2418 return 1;
2419
2421 if (TyBits > CR.getBitWidth()) {
2422 switch (Ld->getOpcode()) {
2423 case TargetOpcode::G_SEXTLOAD:
2424 CR = CR.signExtend(TyBits);
2425 break;
2426 case TargetOpcode::G_ZEXTLOAD:
2427 CR = CR.zeroExtend(TyBits);
2428 break;
2429 default:
2430 break;
2431 }
2432 }
2433
2434 return std::min(CR.getSignedMin().getNumSignBits(),
2436}
2437
2439 const APInt &DemandedElts,
2440 unsigned Depth) {
2441 MachineInstr &MI = *MRI.getVRegDef(R);
2442 unsigned Opcode = MI.getOpcode();
2443
2444 if (Opcode == TargetOpcode::G_CONSTANT)
2445 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2446
2447 if (Depth == getMaxDepth())
2448 return 1;
2449
2450 if (!DemandedElts)
2451 return 1; // No demanded elts, better to assume we don't know anything.
2452
2453 LLT DstTy = MRI.getType(R);
2454 const unsigned TyBits = DstTy.getScalarSizeInBits();
2455
2456 // Handle the case where this is called on a register that does not have a
2457 // type constraint. This is unlikely to occur except by looking through copies
2458 // but it is possible for the initial register being queried to be in this
2459 // state.
2460 if (!DstTy.isValid())
2461 return 1;
2462
2463 unsigned FirstAnswer = 1;
2464 switch (Opcode) {
2465 case TargetOpcode::COPY: {
2466 MachineOperand &Src = MI.getOperand(1);
2467 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2468 MRI.getType(Src.getReg()).isValid()) {
2469 // Don't increment Depth for this one since we didn't do any work.
2470 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2471 }
2472
2473 return 1;
2474 }
2475 case TargetOpcode::G_SEXT: {
2476 Register Src = MI.getOperand(1).getReg();
2477 LLT SrcTy = MRI.getType(Src);
2478 unsigned Tmp = TyBits - SrcTy.getScalarSizeInBits();
2479 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2480 }
2481 case TargetOpcode::G_ASSERT_SEXT:
2482 case TargetOpcode::G_SEXT_INREG: {
2483 // Max of the input and what this extends.
2484 Register Src = MI.getOperand(1).getReg();
2485 unsigned SrcBits = MI.getOperand(2).getImm();
2486 unsigned InRegBits = TyBits - SrcBits + 1;
2487 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2488 InRegBits);
2489 }
2490 case TargetOpcode::G_LOAD: {
2491 GLoad *Ld = cast<GLoad>(&MI);
2492 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2493 break;
2494
2495 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2496 }
2497 case TargetOpcode::G_SEXTLOAD: {
2499
2500 // FIXME: We need an in-memory type representation.
2501 if (DstTy.isVector())
2502 return 1;
2503
2504 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2505 if (NumBits != 1)
2506 return NumBits;
2507
2508 // e.g. i16->i32 = '17' bits known.
2509 const MachineMemOperand *MMO = *MI.memoperands_begin();
2510 return TyBits - MMO->getSizeInBits().getValue() + 1;
2511 }
2512 case TargetOpcode::G_ZEXTLOAD: {
2514
2515 // FIXME: We need an in-memory type representation.
2516 if (DstTy.isVector())
2517 return 1;
2518
2519 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2520 if (NumBits != 1)
2521 return NumBits;
2522
2523 // e.g. i16->i32 = '16' bits known.
2524 const MachineMemOperand *MMO = *MI.memoperands_begin();
2525 return TyBits - MMO->getSizeInBits().getValue();
2526 }
2527 case TargetOpcode::G_AND:
2528 case TargetOpcode::G_OR:
2529 case TargetOpcode::G_XOR: {
2530 Register Src1 = MI.getOperand(1).getReg();
2531 unsigned Src1NumSignBits =
2532 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2533 if (Src1NumSignBits != 1) {
2534 Register Src2 = MI.getOperand(2).getReg();
2535 unsigned Src2NumSignBits =
2536 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2537 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2538 }
2539 break;
2540 }
2541 case TargetOpcode::G_ASHR: {
2542 Register Src1 = MI.getOperand(1).getReg();
2543 Register Src2 = MI.getOperand(2).getReg();
2544 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2545 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2546 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2547 break;
2548 }
2549 case TargetOpcode::G_SHL: {
2550 Register Src1 = MI.getOperand(1).getReg();
2551 Register Src2 = MI.getOperand(2).getReg();
2552 if (std::optional<ConstantRange> ShAmtRange =
2553 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2554 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2555 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2556
2557 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2558 unsigned ExtOpc = ExtMI.getOpcode();
2559
2560 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2561 // shifted out, then we can compute the number of sign bits for the
2562 // operand being extended. A future improvement could be to pass along the
2563 // "shifted left by" information in the recursive calls to
2564 // ComputeKnownSignBits. Allowing us to handle this more generically.
2565 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2566 ExtOpc == TargetOpcode::G_ANYEXT) {
2567 LLT ExtTy = MRI.getType(Src1);
2568 Register Extendee = ExtMI.getOperand(1).getReg();
2569 LLT ExtendeeTy = MRI.getType(Extendee);
2570 uint64_t SizeDiff =
2571 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2572
2573 if (SizeDiff <= MinShAmt) {
2574 unsigned Tmp =
2575 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2576 if (MaxShAmt < Tmp)
2577 return Tmp - MaxShAmt;
2578 }
2579 }
2580 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2581 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2582 if (MaxShAmt < Tmp)
2583 return Tmp - MaxShAmt;
2584 }
2585 break;
2586 }
2587 case TargetOpcode::G_ROTL:
2588 case TargetOpcode::G_ROTR: {
2589 Register SrcReg = MI.getOperand(1).getReg();
2590 unsigned Tmp = computeNumSignBits(SrcReg, DemandedElts, Depth + 1);
2591 auto MaybeAmt =
2592 isConstantOrConstantSplatVector(MI.getOperand(2).getReg(), MRI);
2593 FirstAnswer =
2594 SignBitsOps::rot(Tmp, TyBits, MaybeAmt, Opcode == TargetOpcode::G_ROTR);
2595 break;
2596 }
2597 case TargetOpcode::G_SAVGFLOOR:
2598 case TargetOpcode::G_SAVGCEIL: {
2599 Register Src1 = MI.getOperand(1).getReg();
2600 Register Src2 = MI.getOperand(2).getReg();
2601 FirstAnswer = computeNumSignBitsMin(Src1, Src2, DemandedElts, Depth + 1);
2602 break;
2603 }
2604 case TargetOpcode::G_SREM: {
2605 // The sign bit is the LHS's sign bit, except when the result of the
2606 // remainder is zero. The magnitude of the result should be less than or
2607 // equal to the magnitude of the LHS. Therefore, the result should have
2608 // at least as many sign bits as the left hand side.
2609 Register Src = MI.getOperand(1).getReg();
2610 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2611 }
2612 case TargetOpcode::G_TRUNC: {
2613 Register Src = MI.getOperand(1).getReg();
2614 LLT SrcTy = MRI.getType(Src);
2615
2616 // Check if the sign bits of source go down as far as the truncated value.
2617 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2618 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2619 if (NumSrcSignBits > (NumSrcBits - TyBits))
2620 return NumSrcSignBits - (NumSrcBits - TyBits);
2621 break;
2622 }
2623 case TargetOpcode::G_SELECT: {
2624 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2625 MI.getOperand(3).getReg(), DemandedElts,
2626 Depth + 1);
2627 }
2628 case TargetOpcode::G_SMIN:
2629 case TargetOpcode::G_SMAX:
2630 case TargetOpcode::G_UMIN:
2631 case TargetOpcode::G_UMAX:
2632 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2633 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2634 MI.getOperand(2).getReg(), DemandedElts,
2635 Depth + 1);
2636 case TargetOpcode::G_SADDO:
2637 case TargetOpcode::G_SADDE:
2638 case TargetOpcode::G_UADDO:
2639 case TargetOpcode::G_UADDE:
2640 case TargetOpcode::G_SSUBO:
2641 case TargetOpcode::G_SSUBE:
2642 case TargetOpcode::G_USUBO:
2643 case TargetOpcode::G_USUBE:
2644 case TargetOpcode::G_SMULO:
2645 case TargetOpcode::G_UMULO: {
2646 // If compares returns 0/-1, all bits are sign bits.
2647 // We know that we have an integer-based boolean since these operations
2648 // are only available for integer.
2649 if (MI.getOperand(1).getReg() == R) {
2650 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2652 return TyBits;
2653 }
2654
2655 break;
2656 }
2657 case TargetOpcode::G_SUB: {
2658 Register Src2 = MI.getOperand(2).getReg();
2659 unsigned Src2NumSignBits =
2660 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2661 if (Src2NumSignBits == 1)
2662 return 1; // Early out.
2663
2664 // Handle NEG.
2665 Register Src1 = MI.getOperand(1).getReg();
2666 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2667 if (Known1.isZero()) {
2668 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2669 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2670 // sign bits set.
2671 if ((Known2.Zero | 1).isAllOnes())
2672 return TyBits;
2673
2674 // If the input is known to be positive (the sign bit is known clear),
2675 // the output of the NEG has, at worst, the same number of sign bits as
2676 // the input.
2677 if (Known2.isNonNegative()) {
2678 FirstAnswer = Src2NumSignBits;
2679 break;
2680 }
2681
2682 // Otherwise, we treat this like a SUB.
2683 }
2684
2685 unsigned Src1NumSignBits =
2686 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2687 if (Src1NumSignBits == 1)
2688 return 1; // Early Out.
2689
2690 // Sub can have at most one carry bit. Thus we know that the output
2691 // is, at worst, one more bit than the inputs.
2692 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2693 break;
2694 }
2695 case TargetOpcode::G_ADD: {
2696 Register Src2 = MI.getOperand(2).getReg();
2697 unsigned Src2NumSignBits =
2698 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2699 if (Src2NumSignBits <= 2)
2700 return 1; // Early out.
2701
2702 Register Src1 = MI.getOperand(1).getReg();
2703 unsigned Src1NumSignBits =
2704 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2705 if (Src1NumSignBits == 1)
2706 return 1; // Early Out.
2707
2708 // Special case decrementing a value (ADD X, -1):
2709 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2710 if (Known2.isAllOnes()) {
2711 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2712 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2713 // sign bits set.
2714 if ((Known1.Zero | 1).isAllOnes())
2715 return TyBits;
2716
2717 // If we are subtracting one from a positive number, there is no carry
2718 // out of the result.
2719 if (Known1.isNonNegative()) {
2720 FirstAnswer = Src1NumSignBits;
2721 break;
2722 }
2723
2724 // Otherwise, we treat this like an ADD.
2725 }
2726
2727 // Add can have at most one carry bit. Thus we know that the output
2728 // is, at worst, one more bit than the inputs.
2729 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2730 break;
2731 }
2732 case TargetOpcode::G_FCMP:
2733 case TargetOpcode::G_ICMP: {
2734 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2735 if (TyBits == 1)
2736 break;
2737 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2739 return TyBits; // All bits are sign bits.
2741 return TyBits - 1; // Every always-zero bit is a sign bit.
2742 break;
2743 }
2744 case TargetOpcode::G_UNMERGE_VALUES: {
2745 unsigned NumOps = MI.getNumOperands();
2746 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
2747 LLT SrcTy = MRI.getType(SrcReg);
2748
2749 if ((SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType()) ||
2750 (SrcTy.isScalar() && DstTy.isVector()))
2751 break;
2752
2753 // Figure out the result operand index
2754 unsigned DstIdx = MI.findRegisterDefOperandIdx(R, nullptr);
2755
2756 APInt SubDemandedElts = DemandedElts;
2757 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
2758 if (SrcTy.isVector()) {
2759 SubDemandedElts =
2760 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
2761 }
2762
2763 unsigned SrcOpKnown =
2764 computeNumSignBits(SrcReg, SubDemandedElts, Depth + 1);
2765 if (SrcTy.isVector()) {
2766 FirstAnswer = SrcOpKnown;
2767 } else if (SrcOpKnown >= (MI.getNumOperands() - DstIdx - 2) * TyBits) {
2768 FirstAnswer = SrcOpKnown >= (MI.getNumOperands() - DstIdx - 1) * TyBits
2769 ? TyBits
2770 : SrcOpKnown % TyBits;
2771 }
2772 break;
2773 }
2774 case TargetOpcode::G_BUILD_VECTOR: {
2775 // Collect the known bits that are shared by every demanded vector element.
2776 FirstAnswer = TyBits;
2777 APInt SingleDemandedElt(1, 1);
2778 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2779 if (!DemandedElts[I])
2780 continue;
2781
2782 unsigned Tmp2 =
2783 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2784 FirstAnswer = std::min(FirstAnswer, Tmp2);
2785
2786 // If we don't know any bits, early out.
2787 if (FirstAnswer == 1)
2788 break;
2789 }
2790 break;
2791 }
2792 case TargetOpcode::G_CONCAT_VECTORS: {
2793 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2794 break;
2795 FirstAnswer = TyBits;
2796 // Determine the minimum number of sign bits across all demanded
2797 // elts of the input vectors. Early out if the result is already 1.
2798 unsigned NumSubVectorElts =
2799 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2800 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2801 APInt DemandedSub =
2802 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2803 if (!DemandedSub)
2804 continue;
2805 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2806
2807 FirstAnswer = std::min(FirstAnswer, Tmp2);
2808
2809 // If we don't know any bits, early out.
2810 if (FirstAnswer == 1)
2811 break;
2812 }
2813 break;
2814 }
2815 case TargetOpcode::G_VECTOR_COMPRESS: {
2816 // Each result lane is either a lane of the source vector or the passthru,
2817 // so the number of sign bits is the minimum of the two.
2818 Register Vec = MI.getOperand(1).getReg();
2819 Register PassThru = MI.getOperand(3).getReg();
2820 unsigned Tmp = computeNumSignBits(PassThru, DemandedElts, Depth + 1);
2821 // If passthru contributes nothing, fall back to the KnownBits refinement.
2822 if (Tmp == 1)
2823 break;
2824 // Compression can move any source lane to any result position, so all
2825 // source lanes are demanded.
2826 APInt DemandedSrcElts = APInt::getAllOnes(DemandedElts.getBitWidth());
2827 unsigned Tmp2 = computeNumSignBits(Vec, DemandedSrcElts, Depth + 1);
2828 FirstAnswer = std::min(Tmp, Tmp2);
2829 break;
2830 }
2831 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
2833 Register InVec = Extract.getVectorReg();
2834 Register EltNo = Extract.getIndexReg();
2835 LLT VecVT = MRI.getType(InVec);
2836 if (VecVT.isScalableVector())
2837 return computeNumSignBits(InVec, APInt(1, 1), Depth + 1);
2838 unsigned NumSrcElts = VecVT.getNumElements();
2839 std::optional<APInt> ConstEltNo = getIConstantVRegVal(EltNo, MRI);
2840 APInt DemandedSrcElts =
2841 ConstEltNo && ConstEltNo->ult(NumSrcElts)
2842 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
2843 : APInt::getAllOnes(NumSrcElts);
2844 return computeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
2845 }
2846 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
2847 // Offset the demanded elts by the subvector index.
2848 Register SrcReg = MI.getOperand(1).getReg();
2849 LLT SrcTy = MRI.getType(SrcReg);
2850 APInt DemandedSrcElts;
2851 if (SrcTy.isScalableVector()) {
2852 DemandedSrcElts = APInt(1, 1);
2853 } else {
2854 uint64_t Idx = MI.getOperand(2).getImm();
2855 unsigned NumSrcElts = SrcTy.getNumElements();
2856 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2857 }
2858 return computeNumSignBits(SrcReg, DemandedSrcElts, Depth + 1);
2859 }
2860 case TargetOpcode::G_SHUFFLE_VECTOR: {
2861 // Collect the minimum number of sign bits that are shared by every vector
2862 // element referenced by the shuffle.
2863 APInt DemandedLHS, DemandedRHS;
2864 Register Src1 = MI.getOperand(1).getReg();
2865 unsigned NumElts = MRI.getType(Src1).getNumElements();
2866 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2867 DemandedElts, DemandedLHS, DemandedRHS))
2868 return 1;
2869
2870 if (!!DemandedLHS)
2871 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2872 // If we don't know anything, early out and try computeKnownBits fall-back.
2873 if (FirstAnswer == 1)
2874 break;
2875 if (!!DemandedRHS) {
2876 unsigned Tmp2 =
2877 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2878 FirstAnswer = std::min(FirstAnswer, Tmp2);
2879 }
2880 break;
2881 }
2882 case TargetOpcode::G_SPLAT_VECTOR: {
2883 // Check if the sign bits of source go down as far as the truncated value.
2884 Register Src = MI.getOperand(1).getReg();
2885 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2886 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2887 if (NumSrcSignBits > (NumSrcBits - TyBits))
2888 return NumSrcSignBits - (NumSrcBits - TyBits);
2889 break;
2890 }
2891 case TargetOpcode::G_INTRINSIC:
2892 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2893 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2894 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2895 default: {
2896 unsigned NumBits =
2897 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2898 if (NumBits > 1)
2899 FirstAnswer = std::max(FirstAnswer, NumBits);
2900 break;
2901 }
2902 }
2903
2904 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2905 // use this information.
2906 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2907 return std::max(FirstAnswer, Known.countMinSignBits());
2908}
2909
2911 LLT Ty = MRI.getType(R);
2912 APInt DemandedElts =
2913 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2914 return computeNumSignBits(R, DemandedElts, Depth);
2915}
2916
2918 Register R, const APInt &DemandedElts, unsigned Depth) {
2919 // Shifting more than the bitwidth is not valid.
2920 MachineInstr &MI = *MRI.getVRegDef(R);
2921 unsigned Opcode = MI.getOpcode();
2922
2923 LLT Ty = MRI.getType(R);
2924 unsigned BitWidth = Ty.getScalarSizeInBits();
2925
2926 if (Opcode == TargetOpcode::G_CONSTANT) {
2927 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2928 if (ShAmt.uge(BitWidth))
2929 return std::nullopt;
2930 return ConstantRange(ShAmt);
2931 }
2932
2933 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2934 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2935 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2936 if (!DemandedElts[I])
2937 continue;
2938 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2939 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2940 MinAmt = MaxAmt = nullptr;
2941 break;
2942 }
2943
2944 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2945 if (ShAmt.uge(BitWidth))
2946 return std::nullopt;
2947 if (!MinAmt || MinAmt->ugt(ShAmt))
2948 MinAmt = &ShAmt;
2949 if (!MaxAmt || MaxAmt->ult(ShAmt))
2950 MaxAmt = &ShAmt;
2951 }
2952 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2953 "Failed to find matching min/max shift amounts");
2954 if (MinAmt && MaxAmt)
2955 return ConstantRange(*MinAmt, *MaxAmt + 1);
2956 }
2957
2958 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2959 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2960 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2961 if (KnownAmt.getMaxValue().ult(BitWidth))
2962 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2963
2964 return std::nullopt;
2965}
2966
2968 Register R, const APInt &DemandedElts, unsigned Depth) {
2969 if (std::optional<ConstantRange> AmtRange =
2970 getValidShiftAmountRange(R, DemandedElts, Depth))
2971 return AmtRange->getUnsignedMin().getZExtValue();
2972 return std::nullopt;
2973}
2974
2980
2985
2987 if (!Info) {
2988 unsigned MaxDepth =
2990 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2991 }
2992 return *Info;
2993}
2994
2995AnalysisKey GISelValueTrackingAnalysis::Key;
2996
3000 unsigned MaxDepth =
3002 return Result(MF, MaxDepth);
3003}
3004
3005static PreservedAnalyses
3008 bool PrintFPClass) {
3009 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
3010 const auto &MRI = MF.getRegInfo();
3011 OS << "name: ";
3012 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
3013 OS << '\n';
3014
3015 for (MachineBasicBlock &BB : MF) {
3016 for (MachineInstr &MI : BB) {
3017 for (MachineOperand &MO : MI.defs()) {
3018 if (!MO.isReg() || MO.getReg().isPhysical())
3019 continue;
3020 Register Reg = MO.getReg();
3021 if (!MRI.getType(Reg).isValid())
3022 continue;
3023 if (PrintFPClass) {
3024 KnownFPClass FPKnown = VTA.computeKnownFPClass(Reg);
3025 OS << " " << MO << " FPClasses:" << FPKnown.getKnownFPClasses()
3026 << " SignBitKnown:";
3027 if (FPKnown.getSignBit())
3028 OS << (*FPKnown.getSignBit() ? '1' : '0');
3029 else
3030 OS << '?';
3031 OS << '\n';
3032 } else {
3033 KnownBits Known = VTA.getKnownBits(Reg);
3034 unsigned SignedBits = VTA.computeNumSignBits(Reg);
3035 bool IsKnownNeverZero = VTA.isKnownNeverZero(Reg);
3036 OS << " " << MO << " KnownBits:" << Known
3037 << " SignBits:" << SignedBits
3038 << " IsKnownNeverZero:" << IsKnownNeverZero << '\n';
3039 }
3040 };
3041 }
3042 }
3043 return PreservedAnalyses::all();
3044}
3045
3051
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 PreservedAnalyses printGISelValueTracking(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM, raw_ostream &OS, bool PrintFPClass)
static unsigned computeNumSignBitsFromRangeMetadata(const GAnyLoad *Ld, unsigned TyBits)
Compute the known number of sign bits with attached range metadata in the memory operand.
Provides analysis for querying information about KnownBits during GISel passes.
#define DEBUG_TYPE
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
static bool isAbsoluteValueULEOne(const Value *V)
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Value * RHS
Value * LHS
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1242
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2009
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:230
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1426
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1057
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:225
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1186
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1115
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1648
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1618
unsigned logBase2() const
Definition APInt.h:1781
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:471
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:875
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:436
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:302
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1437
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:282
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:235
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1225
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
This class represents a range of values.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI ConstantRange multiply(const ConstantRange &Other, unsigned NoWrapKind=0) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
Represents any generic load, including sign/zero extending variants.
Represents an extract vector element.
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp: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)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
KnownBits getKnownBits(Register R)
Align computeKnownAlignment(Register R, unsigned Depth=0)
std::optional< ConstantRange > getValidShiftAmountRange(Register R, const APInt &DemandedElts, unsigned Depth)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
bool maskedValueIsZero(Register Val, const APInt &Mask)
std::optional< uint64_t > getValidMinimumShiftAmount(Register R, const APInt &DemandedElts, unsigned Depth=0)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
const DataLayout & getDataLayout() const
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
const MachineFunction & getMachineFunction() const
bool isKnownNeverNaN(Register Val, bool SNaN=false)
Returns true if Val can be assumed to never be a NaN.
bool isKnownNeverLogicalZero(Register Val, unsigned Depth=0)
Returns true if Val can be assumed to never be a zero, accounting for denormal flushing of the contai...
void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
bool isKnownNeverZero(Register R, unsigned Depth=0)
Return true if the value defined by R is provably never zero.
Represents a insert subvector.
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:1081
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
LLT getMemoryType() const
Return the memory type of the memory reference.
const MDNode * getRanges() const
Return the range tag for the memory reference.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
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
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:316
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp: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:2570
LLVM_ABI const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
scope_exit(Callable) -> scope_exit< Callable >
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1692
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
constexpr unsigned MaxAnalysisRecursionDepth
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
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 clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
bool isAllOnes() const
Returns true if value is all one bits.
Definition KnownBits.h:81
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
void setKnownFPClasses(FPClassTest Classes)
static LLVM_ABI KnownFPClass sin(const KnownFPClass &Src)
Report known values for sin.
static LLVM_ABI KnownFPClass frem(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for frem.
static LLVM_ABI KnownFPClass fdiv_self(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fdiv x, x.
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
static LLVM_ABI KnownFPClass 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.
std::optional< bool > getSignBit() const
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
FPClassTest getKnownFPClasses() const
Floating-point classes the value could be one of.
static LLVM_ABI KnownFPClass 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.