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