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