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