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