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_SHUFFLE_VECTOR: {
874 APInt DemandedLHS, DemandedRHS;
875 // Collect the known bits that are shared by every vector element referenced
876 // by the shuffle.
877 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
878 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
879 DemandedElts, DemandedLHS, DemandedRHS))
880 break;
881
882 // Known bits are the values that are shared by every demanded element.
883 Known.Zero.setAllBits();
884 Known.One.setAllBits();
885 if (!!DemandedLHS) {
886 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
887 Depth + 1);
888 Known = Known.intersectWith(Known2);
889 }
890 // If we don't know any bits, early out.
891 if (Known.isUnknown())
892 break;
893 if (!!DemandedRHS) {
894 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
895 Depth + 1);
896 Known = Known.intersectWith(Known2);
897 }
898 break;
899 }
900 case TargetOpcode::G_CONCAT_VECTORS: {
901 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
902 break;
903 // Split DemandedElts and test each of the demanded subvectors.
904 Known.Zero.setAllBits();
905 Known.One.setAllBits();
906 unsigned NumSubVectorElts =
907 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
908
909 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
910 APInt DemandedSub =
911 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
912 if (!!DemandedSub) {
913 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
914
915 Known = Known.intersectWith(Known2);
916 }
917 // If we don't know any bits, early out.
918 if (Known.isUnknown())
919 break;
920 }
921 break;
922 }
923 case TargetOpcode::G_ABS: {
924 Register SrcReg = MI.getOperand(1).getReg();
925 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
926 Known = Known.abs();
927 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
928 1);
929 break;
930 }
931 }
932
933 LLVM_DEBUG(dumpResult(MI, Known, Depth));
934}
935
936void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
937 FPClassTest InterestedClasses,
938 unsigned Depth) {
939 LLT Ty = MRI.getType(R);
940 APInt DemandedElts =
941 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
942 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
943}
944
945/// Return true if this value is known to be the fractional part x - floor(x),
946/// which lies in [0, 1). This implies the value cannot introduce overflow in a
947/// fmul when the other operand is known finite.
949 using namespace MIPatternMatch;
950 Register SubX;
951 return mi_match(R, MRI, m_GFSub(m_Reg(SubX), m_GFFloor(m_DeferredReg(SubX))));
952}
953
954void GISelValueTracking::computeKnownFPClassForFPTrunc(
955 const MachineInstr &MI, const APInt &DemandedElts,
956 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
957 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
958 fcNone)
959 return;
960
961 Register Val = MI.getOperand(1).getReg();
962 KnownFPClass KnownSrc;
963 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
964 Depth + 1);
965 Known = KnownFPClass::fptrunc(KnownSrc);
966}
967
968void GISelValueTracking::computeKnownFPClass(Register R,
969 const APInt &DemandedElts,
970 FPClassTest InterestedClasses,
971 KnownFPClass &Known,
972 unsigned Depth) {
973 assert(Known.isUnknown() && "should not be called with known information");
974
975 if (!DemandedElts) {
976 // No demanded elts, better to assume we don't know anything.
977 Known.resetAll();
978 return;
979 }
980
981 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
982
983 MachineInstr &MI = *MRI.getVRegDef(R);
984 unsigned Opcode = MI.getOpcode();
985 LLT DstTy = MRI.getType(R);
986
987 if (!DstTy.isValid()) {
988 Known.resetAll();
989 return;
990 }
991
992 if (auto Cst = GFConstant::getConstant(R, MRI)) {
993 switch (Cst->getKind()) {
995 auto APF = Cst->getScalarValue();
996 Known.KnownFPClasses = APF.classify();
997 Known.SignBit = APF.isNegative();
998 break;
999 }
1001 Known.KnownFPClasses = fcNone;
1002 bool SignBitAllZero = true;
1003 bool SignBitAllOne = true;
1004
1005 for (auto C : *Cst) {
1006 Known.KnownFPClasses |= C.classify();
1007 if (C.isNegative())
1008 SignBitAllZero = false;
1009 else
1010 SignBitAllOne = false;
1011 }
1012
1013 if (SignBitAllOne != SignBitAllZero)
1014 Known.SignBit = SignBitAllOne;
1015
1016 break;
1017 }
1019 Known.resetAll();
1020 break;
1021 }
1022 }
1023
1024 return;
1025 }
1026
1027 FPClassTest KnownNotFromFlags = fcNone;
1029 KnownNotFromFlags |= fcNan;
1031 KnownNotFromFlags |= fcInf;
1032
1033 // We no longer need to find out about these bits from inputs if we can
1034 // assume this from flags/attributes.
1035 InterestedClasses &= ~KnownNotFromFlags;
1036
1037 llvm::scope_exit ClearClassesFromFlags(
1038 [=, &Known] { Known.knownNot(KnownNotFromFlags); });
1039
1040 // All recursive calls that increase depth must come after this.
1042 return;
1043
1044 const MachineFunction *MF = MI.getMF();
1045
1046 switch (Opcode) {
1047 default:
1048 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
1049 Depth);
1050 break;
1051 case TargetOpcode::G_FNEG: {
1052 Register Val = MI.getOperand(1).getReg();
1053 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
1054 Known.fneg();
1055 break;
1056 }
1057 case TargetOpcode::G_SELECT: {
1058 GSelect &SelMI = cast<GSelect>(MI);
1059 Register Cond = SelMI.getCondReg();
1060 Register LHS = SelMI.getTrueReg();
1061 Register RHS = SelMI.getFalseReg();
1062
1063 FPClassTest FilterLHS = fcAllFlags;
1064 FPClassTest FilterRHS = fcAllFlags;
1065
1066 Register TestedValue;
1067 FPClassTest MaskIfTrue = fcAllFlags;
1068 FPClassTest MaskIfFalse = fcAllFlags;
1069 FPClassTest ClassVal = fcNone;
1070
1071 CmpInst::Predicate Pred;
1072 Register CmpLHS, CmpRHS;
1073 if (mi_match(Cond, MRI,
1074 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
1075 // If the select filters out a value based on the class, it no longer
1076 // participates in the class of the result
1077
1078 // TODO: In some degenerate cases we can infer something if we try again
1079 // without looking through sign operations.
1080 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
1081 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
1082 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
1083 } else if (mi_match(
1084 Cond, MRI,
1085 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
1086 FPClassTest TestedMask = ClassVal;
1087 MaskIfTrue = TestedMask;
1088 MaskIfFalse = ~TestedMask;
1089 }
1090
1091 if (TestedValue == LHS) {
1092 // match !isnan(x) ? x : y
1093 FilterLHS = MaskIfTrue;
1094 } else if (TestedValue == RHS) { // && IsExactClass
1095 // match !isnan(x) ? y : x
1096 FilterRHS = MaskIfFalse;
1097 }
1098
1099 KnownFPClass Known2;
1100 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
1101 Depth + 1);
1102 Known.KnownFPClasses &= FilterLHS;
1103
1104 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
1105 Known2, Depth + 1);
1106 Known2.KnownFPClasses &= FilterRHS;
1107
1108 Known |= Known2;
1109 break;
1110 }
1111 case TargetOpcode::G_FCOPYSIGN: {
1112 Register Magnitude = MI.getOperand(1).getReg();
1113 Register Sign = MI.getOperand(2).getReg();
1114
1115 KnownFPClass KnownSign;
1116
1117 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
1118 Depth + 1);
1119 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
1120 Depth + 1);
1121 Known.copysign(KnownSign);
1122 break;
1123 }
1124 case TargetOpcode::G_FMA:
1125 case TargetOpcode::G_STRICT_FMA:
1126 case TargetOpcode::G_FMAD: {
1127 if ((InterestedClasses & fcNegative) == fcNone)
1128 break;
1129
1130 Register A = MI.getOperand(1).getReg();
1131 Register B = MI.getOperand(2).getReg();
1132 Register C = MI.getOperand(3).getReg();
1133
1134 DenormalMode Mode =
1135 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1136
1137 if (A == B && isGuaranteedNotToBeUndef(A, MRI, Depth + 1)) {
1138 // x * x + y
1139 KnownFPClass KnownSrc, KnownAddend;
1140 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
1141 Depth + 1);
1142 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc,
1143 Depth + 1);
1144 if (KnownNotFromFlags) {
1145 KnownSrc.knownNot(KnownNotFromFlags);
1146 KnownAddend.knownNot(KnownNotFromFlags);
1147 }
1148 Known = KnownFPClass::fma_square(KnownSrc, KnownAddend, Mode);
1149 } else {
1150 KnownFPClass KnownSrc[3];
1151 computeKnownFPClass(A, DemandedElts, InterestedClasses, KnownSrc[0],
1152 Depth + 1);
1153 if (KnownSrc[0].isUnknown())
1154 break;
1155 computeKnownFPClass(B, DemandedElts, InterestedClasses, KnownSrc[1],
1156 Depth + 1);
1157 if (KnownSrc[1].isUnknown())
1158 break;
1159 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownSrc[2],
1160 Depth + 1);
1161 if (KnownSrc[2].isUnknown())
1162 break;
1163 if (KnownNotFromFlags) {
1164 KnownSrc[0].knownNot(KnownNotFromFlags);
1165 KnownSrc[1].knownNot(KnownNotFromFlags);
1166 KnownSrc[2].knownNot(KnownNotFromFlags);
1167 }
1168 Known = KnownFPClass::fma(KnownSrc[0], KnownSrc[1], KnownSrc[2], Mode);
1169 }
1170 break;
1171 }
1172 case TargetOpcode::G_FSQRT:
1173 case TargetOpcode::G_STRICT_FSQRT: {
1174 KnownFPClass KnownSrc;
1175 FPClassTest InterestedSrcs = InterestedClasses;
1176 if (InterestedClasses & fcNan)
1177 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1178
1179 Register Val = MI.getOperand(1).getReg();
1180 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1181
1182 DenormalMode Mode =
1183 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1184 Known = KnownFPClass::sqrt(KnownSrc, Mode);
1185 if (MI.getFlag(MachineInstr::MIFlag::FmNsz))
1186 Known.knownNot(fcNegZero);
1187 break;
1188 }
1189 case TargetOpcode::G_FABS: {
1190 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
1191 Register Val = MI.getOperand(1).getReg();
1192 // If we only care about the sign bit we don't need to inspect the
1193 // operand.
1194 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
1195 Depth + 1);
1196 }
1197 Known.fabs();
1198 break;
1199 }
1200 case TargetOpcode::G_FATAN2: {
1201 Register Y = MI.getOperand(1).getReg();
1202 Register X = MI.getOperand(2).getReg();
1203 KnownFPClass KnownY, KnownX;
1204 computeKnownFPClass(Y, DemandedElts, InterestedClasses, KnownY, Depth + 1);
1205 computeKnownFPClass(X, DemandedElts, InterestedClasses, KnownX, Depth + 1);
1206 Known = KnownFPClass::atan2(KnownY, KnownX);
1207 break;
1208 }
1209 case TargetOpcode::G_FSINH: {
1210 Register Val = MI.getOperand(1).getReg();
1211 KnownFPClass KnownSrc;
1212 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1213 Depth + 1);
1214 Known = KnownFPClass::sinh(KnownSrc);
1215 break;
1216 }
1217 case TargetOpcode::G_FCOSH: {
1218 Register Val = MI.getOperand(1).getReg();
1219 KnownFPClass KnownSrc;
1220 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1221 Depth + 1);
1222 Known = KnownFPClass::cosh(KnownSrc);
1223 break;
1224 }
1225 case TargetOpcode::G_FTANH: {
1226 Register Val = MI.getOperand(1).getReg();
1227 KnownFPClass KnownSrc;
1228 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1229 Depth + 1);
1230 Known = KnownFPClass::tanh(KnownSrc);
1231 break;
1232 }
1233 case TargetOpcode::G_FASIN: {
1234 Register Val = MI.getOperand(1).getReg();
1235 KnownFPClass KnownSrc;
1236 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1237 Depth + 1);
1238 Known = KnownFPClass::asin(KnownSrc);
1239 break;
1240 }
1241 case TargetOpcode::G_FACOS: {
1242 Register Val = MI.getOperand(1).getReg();
1243 KnownFPClass KnownSrc;
1244 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1245 Depth + 1);
1246 Known = KnownFPClass::acos(KnownSrc);
1247 break;
1248 }
1249 case TargetOpcode::G_FATAN: {
1250 Register Val = MI.getOperand(1).getReg();
1251 KnownFPClass KnownSrc;
1252 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1253 Depth + 1);
1254 Known = KnownFPClass::atan(KnownSrc);
1255 break;
1256 }
1257 case TargetOpcode::G_FTAN: {
1258 Register Val = MI.getOperand(1).getReg();
1259 KnownFPClass KnownSrc;
1260 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1261 Depth + 1);
1262 Known = KnownFPClass::tan(KnownSrc);
1263 break;
1264 }
1265 case TargetOpcode::G_FSIN:
1266 case TargetOpcode::G_FCOS: {
1267 // Return NaN on infinite inputs.
1268 Register Val = MI.getOperand(1).getReg();
1269 KnownFPClass KnownSrc;
1270 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1271 Depth + 1);
1272 Known = Opcode == TargetOpcode::G_FCOS ? KnownFPClass::cos(KnownSrc)
1273 : KnownFPClass::sin(KnownSrc);
1274 break;
1275 }
1276 case TargetOpcode::G_FSINCOS: {
1277 // Operand layout: (sin_dst, cos_dst, src)
1278 Register Src = MI.getOperand(2).getReg();
1279 KnownFPClass KnownSrc;
1280 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1281 Depth + 1);
1282 if (R == MI.getOperand(0).getReg())
1283 Known = KnownFPClass::sin(KnownSrc);
1284 else
1285 Known = KnownFPClass::cos(KnownSrc);
1286 break;
1287 }
1288 case TargetOpcode::G_FMAXNUM:
1289 case TargetOpcode::G_FMINNUM:
1290 case TargetOpcode::G_FMINNUM_IEEE:
1291 case TargetOpcode::G_FMAXIMUM:
1292 case TargetOpcode::G_FMINIMUM:
1293 case TargetOpcode::G_FMAXNUM_IEEE:
1294 case TargetOpcode::G_FMAXIMUMNUM:
1295 case TargetOpcode::G_FMINIMUMNUM: {
1296 Register LHS = MI.getOperand(1).getReg();
1297 Register RHS = MI.getOperand(2).getReg();
1298 KnownFPClass KnownLHS, KnownRHS;
1299
1300 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1301 Depth + 1);
1302 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1303 Depth + 1);
1304
1306 switch (Opcode) {
1307 case TargetOpcode::G_FMINIMUM:
1309 break;
1310 case TargetOpcode::G_FMAXIMUM:
1312 break;
1313 case TargetOpcode::G_FMINIMUMNUM:
1315 break;
1316 case TargetOpcode::G_FMAXIMUMNUM:
1318 break;
1319 case TargetOpcode::G_FMINNUM:
1320 case TargetOpcode::G_FMINNUM_IEEE:
1322 break;
1323 case TargetOpcode::G_FMAXNUM:
1324 case TargetOpcode::G_FMAXNUM_IEEE:
1326 break;
1327 default:
1328 llvm_unreachable("unhandled min/max opcode");
1329 }
1330
1331 DenormalMode Mode =
1332 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1333 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, Kind, Mode);
1334 break;
1335 }
1336 case TargetOpcode::G_FCANONICALIZE: {
1337 Register Val = MI.getOperand(1).getReg();
1338 KnownFPClass KnownSrc;
1339 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1340 Depth + 1);
1341
1342 LLT Ty = MRI.getType(Val).getScalarType();
1343 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1344 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1345 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
1346 break;
1347 }
1348 case TargetOpcode::G_VECREDUCE_FMAX:
1349 case TargetOpcode::G_VECREDUCE_FMIN:
1350 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1351 case TargetOpcode::G_VECREDUCE_FMINIMUM: {
1352 Register Val = MI.getOperand(1).getReg();
1353 // reduce min/max will choose an element from one of the vector elements,
1354 // so we can infer and class information that is common to all elements.
1355
1356 Known =
1357 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1358 // Can only propagate sign if output is never NaN.
1359 if (!Known.isKnownNeverNaN())
1360 Known.SignBit.reset();
1361 break;
1362 }
1363 case TargetOpcode::G_FFLOOR:
1364 case TargetOpcode::G_FCEIL:
1365 case TargetOpcode::G_FRINT:
1366 case TargetOpcode::G_FNEARBYINT:
1367 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1368 case TargetOpcode::G_INTRINSIC_ROUND:
1369 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1370 case TargetOpcode::G_INTRINSIC_TRUNC: {
1371 Register Val = MI.getOperand(1).getReg();
1372 KnownFPClass KnownSrc;
1373 FPClassTest InterestedSrcs = InterestedClasses;
1374 if (InterestedSrcs & fcPosFinite)
1375 InterestedSrcs |= fcPosFinite;
1376 if (InterestedSrcs & fcNegFinite)
1377 InterestedSrcs |= fcNegFinite;
1378 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1379
1380 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1381 bool IsTrunc = Opcode == TargetOpcode::G_INTRINSIC_TRUNC;
1382 Known = KnownFPClass::roundToIntegral(KnownSrc, IsTrunc,
1383 /*IsMultiUnitFPType=*/false);
1384 break;
1385 }
1386 case TargetOpcode::G_FEXP:
1387 case TargetOpcode::G_FEXP2:
1388 case TargetOpcode::G_FEXP10: {
1389 Register Val = MI.getOperand(1).getReg();
1390 KnownFPClass KnownSrc;
1391 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1392 Depth + 1);
1393 Known = KnownFPClass::exp(KnownSrc);
1394 break;
1395 }
1396 case TargetOpcode::G_FLOG:
1397 case TargetOpcode::G_FLOG2:
1398 case TargetOpcode::G_FLOG10: {
1399 // log(+inf) -> +inf
1400 // log([+-]0.0) -> -inf
1401 // log(-inf) -> nan
1402 // log(-x) -> nan
1403 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
1404 break;
1405
1406 FPClassTest InterestedSrcs = InterestedClasses;
1407 if ((InterestedClasses & fcNegInf) != fcNone)
1408 InterestedSrcs |= fcZero | fcSubnormal;
1409 if ((InterestedClasses & fcNan) != fcNone)
1410 InterestedSrcs |= fcNan | fcNegative;
1411
1412 Register Val = MI.getOperand(1).getReg();
1413 KnownFPClass KnownSrc;
1414 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1415
1416 LLT Ty = MRI.getType(Val).getScalarType();
1417 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1418 DenormalMode Mode = MF->getDenormalMode(FltSem);
1419 Known = KnownFPClass::log(KnownSrc, Mode);
1420 break;
1421 }
1422 case TargetOpcode::G_FPOWI: {
1423 if ((InterestedClasses & (fcNan | fcInf | fcNegative)) == fcNone)
1424 break;
1425
1426 Register Exp = MI.getOperand(2).getReg();
1427 LLT ExpTy = MRI.getType(Exp);
1428 KnownBits ExponentKnownBits = getKnownBits(
1429 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1430
1431 FPClassTest InterestedSrcs = fcNone;
1432 if (InterestedClasses & fcNan)
1433 InterestedSrcs |= fcNan;
1434 if (!ExponentKnownBits.isZero()) {
1435 if (InterestedClasses & fcInf)
1436 InterestedSrcs |= fcFinite | fcInf;
1437 if ((InterestedClasses & fcNegative) && !ExponentKnownBits.isEven())
1438 InterestedSrcs |= fcNegative;
1439 }
1440
1441 KnownFPClass KnownSrc;
1442 if (InterestedSrcs != fcNone) {
1443 Register Val = MI.getOperand(1).getReg();
1444 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc,
1445 Depth + 1);
1446 }
1447
1448 Known = KnownFPClass::powi(KnownSrc, ExponentKnownBits);
1449 break;
1450 }
1451 case TargetOpcode::G_FLDEXP:
1452 case TargetOpcode::G_STRICT_FLDEXP: {
1453 Register Val = MI.getOperand(1).getReg();
1454 KnownFPClass KnownSrc;
1455 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1456 Depth + 1);
1457
1458 // Can refine inf/zero handling based on the exponent operand.
1459 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1460 KnownBits ExpBits;
1461 if ((KnownSrc.KnownFPClasses & ExpInfoMask) != fcNone) {
1462 Register ExpReg = MI.getOperand(2).getReg();
1463 LLT ExpTy = MRI.getType(ExpReg);
1464 ExpBits = getKnownBits(
1465 ExpReg, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1466 }
1467
1468 LLT ScalarTy = DstTy.getScalarType();
1469 const fltSemantics &Flt = getFltSemanticForLLT(ScalarTy);
1470 DenormalMode Mode = MF->getDenormalMode(Flt);
1471 Known = KnownFPClass::ldexp(KnownSrc, ExpBits, Flt, Mode);
1472 break;
1473 }
1474 case TargetOpcode::G_FADD:
1475 case TargetOpcode::G_STRICT_FADD:
1476 case TargetOpcode::G_FSUB:
1477 case TargetOpcode::G_STRICT_FSUB: {
1478 Register LHS = MI.getOperand(1).getReg();
1479 Register RHS = MI.getOperand(2).getReg();
1480 bool IsAdd = (Opcode == TargetOpcode::G_FADD ||
1481 Opcode == TargetOpcode::G_STRICT_FADD);
1482 bool WantNegative =
1483 IsAdd &&
1484 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1485 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1486 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1487
1488 if (!WantNaN && !WantNegative && !WantNegZero) {
1489 break;
1490 }
1491
1492 DenormalMode Mode =
1493 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1494
1495 FPClassTest InterestedSrcs = InterestedClasses;
1496 if (WantNegative)
1497 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1498 if (InterestedClasses & fcNan)
1499 InterestedSrcs |= fcInf;
1500
1501 // Special case fadd x, x (canonical form of fmul x, 2).
1502 if (IsAdd && LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1503 KnownFPClass KnownSelf;
1504 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownSelf,
1505 Depth + 1);
1506 Known = KnownFPClass::fadd_self(KnownSelf, Mode);
1507 break;
1508 }
1509
1510 KnownFPClass KnownLHS, KnownRHS;
1511 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1512
1513 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1514 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1515 WantNegZero || !IsAdd) {
1516 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1517 // there's no point.
1518 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1519 Depth + 1);
1520 }
1521
1522 if (IsAdd)
1523 Known = KnownFPClass::fadd(KnownLHS, KnownRHS, Mode);
1524 else
1525 Known = KnownFPClass::fsub(KnownLHS, KnownRHS, Mode);
1526 break;
1527 }
1528 case TargetOpcode::G_FMUL:
1529 case TargetOpcode::G_STRICT_FMUL: {
1530 Register LHS = MI.getOperand(1).getReg();
1531 Register RHS = MI.getOperand(2).getReg();
1532 DenormalMode Mode =
1533 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1534
1535 // X * X is always non-negative or a NaN (use square() for precision).
1536 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1537 KnownFPClass KnownSrc;
1538 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownSrc, Depth + 1);
1539 Known = KnownFPClass::square(KnownSrc, Mode);
1540 } else {
1541 // If RHS is a scalar constant, use the more precise APFloat overload.
1542 auto RHSCst = GFConstant::getConstant(RHS, MRI);
1543 if (RHSCst && RHSCst->getKind() == GFConstant::GFConstantKind::Scalar) {
1544 KnownFPClass KnownLHS;
1545 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1546 Known = KnownFPClass::fmul(KnownLHS, RHSCst->getScalarValue(), Mode);
1547 } else {
1548 KnownFPClass KnownLHS, KnownRHS;
1549 computeKnownFPClass(RHS, DemandedElts, fcAllFlags, KnownRHS, Depth + 1);
1550 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1551 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
1552
1553 // If one operand is known |x| <= 1 and the other is finite, the
1554 // product cannot overflow to infinity.
1555 if (KnownLHS.isKnownNever(fcInf) && isAbsoluteValueULEOne(RHS, MRI))
1556 Known.knownNot(fcInf);
1557 else if (KnownRHS.isKnownNever(fcInf) &&
1559 Known.knownNot(fcInf);
1560 }
1561 }
1562 break;
1563 }
1564 case TargetOpcode::G_FDIV:
1565 case TargetOpcode::G_FREM: {
1566 Register LHS = MI.getOperand(1).getReg();
1567 Register RHS = MI.getOperand(2).getReg();
1568
1569 if (Opcode == TargetOpcode::G_FREM)
1570 Known.knownNot(fcInf);
1571
1572 DenormalMode Mode =
1573 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1574
1575 if (LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1)) {
1576 if (Opcode == TargetOpcode::G_FDIV) {
1577 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1578 if (!WantNan) {
1579 // X / X is always exactly 1.0 or a NaN.
1581 break;
1582 }
1583 KnownFPClass KnownSrc;
1584 computeKnownFPClass(LHS, DemandedElts,
1585 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1586 Depth + 1);
1587 Known = KnownFPClass::fdiv_self(KnownSrc, Mode);
1588 } else {
1589 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1590 if (!WantNan) {
1591 // X % X is always exactly [+-]0.0 or a NaN.
1592 Known.KnownFPClasses = fcZero | fcNan;
1593 break;
1594 }
1595 KnownFPClass KnownSrc;
1596 computeKnownFPClass(LHS, DemandedElts,
1597 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc,
1598 Depth + 1);
1599 Known = KnownFPClass::frem_self(KnownSrc, Mode);
1600 }
1601 break;
1602 }
1603
1604 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1605 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1606 const bool WantPositive = Opcode == TargetOpcode::G_FREM &&
1607 (InterestedClasses & fcPositive) != fcNone;
1608 if (!WantNan && !WantNegative && !WantPositive) {
1609 break;
1610 }
1611
1612 KnownFPClass KnownLHS, KnownRHS;
1613
1614 computeKnownFPClass(RHS, DemandedElts, fcNan | fcInf | fcZero | fcNegative,
1615 KnownRHS, Depth + 1);
1616
1617 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
1618 KnownRHS.isKnownNever(fcNegative) ||
1619 KnownRHS.isKnownNever(fcPositive);
1620
1621 if (KnowSomethingUseful || WantPositive) {
1622 computeKnownFPClass(LHS, DemandedElts, fcAllFlags, KnownLHS, Depth + 1);
1623 }
1624
1625 if (Opcode == TargetOpcode::G_FDIV) {
1626 Known = KnownFPClass::fdiv(KnownLHS, KnownRHS, Mode);
1627 } else {
1628 // Inf REM x and x REM 0 produce NaN.
1629 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1630 KnownLHS.isKnownNeverInfinity() &&
1631 KnownRHS.isKnownNeverLogicalZero(Mode)) {
1632 Known.knownNot(fcNan);
1633 }
1634
1635 // The sign for frem is the same as the first operand.
1636 if (KnownLHS.cannotBeOrderedLessThanZero())
1638 if (KnownLHS.cannotBeOrderedGreaterThanZero())
1640
1641 // See if we can be more aggressive about the sign of 0.
1642 if (KnownLHS.isKnownNever(fcNegative))
1643 Known.knownNot(fcNegative);
1644 if (KnownLHS.isKnownNever(fcPositive))
1645 Known.knownNot(fcPositive);
1646 }
1647 break;
1648 }
1649 case TargetOpcode::G_FFREXP: {
1650 // Only handle the mantissa output (operand 0); the exponent is an integer.
1651 if (R != MI.getOperand(0).getReg())
1652 break;
1653 Register Src = MI.getOperand(2).getReg();
1654 KnownFPClass KnownSrc;
1655 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1656 Depth + 1);
1657 DenormalMode Mode =
1658 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1659 Known = KnownFPClass::frexp_mant(KnownSrc, Mode);
1660 break;
1661 }
1662 case TargetOpcode::G_FPEXT: {
1663 Register Src = MI.getOperand(1).getReg();
1664 KnownFPClass KnownSrc;
1665 computeKnownFPClass(Src, DemandedElts, InterestedClasses, KnownSrc,
1666 Depth + 1);
1667
1668 LLT DstScalarTy = DstTy.getScalarType();
1669 const fltSemantics &DstSem = getFltSemanticForLLT(DstScalarTy);
1670 LLT SrcTy = MRI.getType(Src).getScalarType();
1671 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
1672
1673 Known = KnownFPClass::fpext(KnownSrc, DstSem, SrcSem);
1674 break;
1675 }
1676 case TargetOpcode::G_FPTRUNC: {
1677 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1678 Depth);
1679 break;
1680 }
1681 case TargetOpcode::G_SITOFP:
1682 case TargetOpcode::G_UITOFP: {
1683 // Cannot produce nan
1684 Known.knownNot(fcNan);
1685
1686 // Integers cannot be subnormal
1687 Known.knownNot(fcSubnormal);
1688
1689 // sitofp and uitofp turn into +0.0 for zero.
1690 Known.knownNot(fcNegZero);
1691
1692 // UIToFP is always non-negative regardless of known bits.
1693 if (Opcode == TargetOpcode::G_UITOFP)
1694 Known.signBitMustBeZero();
1695
1696 // Only compute known bits if we can learn something useful from them.
1697 if (!(InterestedClasses & (fcPosZero | fcNormal | fcInf)))
1698 break;
1699
1700 Register Val = MI.getOperand(1).getReg();
1701 LLT Ty = MRI.getType(Val);
1702 KnownBits IntKnown = getKnownBits(
1703 Val, Ty.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1704
1705 // If the integer is non-zero, the result cannot be +0.0.
1706 if (IntKnown.isNonZero())
1707 Known.knownNot(fcPosZero);
1708
1709 if (Opcode == TargetOpcode::G_SITOFP) {
1710 // If the signed integer is known non-negative, the result is
1711 // non-negative. If the signed integer is known negative, the result is
1712 // negative.
1713 if (IntKnown.isNonNegative())
1714 Known.signBitMustBeZero();
1715 else if (IntKnown.isNegative())
1716 Known.signBitMustBeOne();
1717 }
1718
1719 if (InterestedClasses & fcInf) {
1720 LLT FPTy = DstTy.getScalarType();
1721 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
1722
1723 // Compute the effective integer width after removing known-zero leading
1724 // bits, to check if the result can overflow to infinity.
1725 int IntSize = IntKnown.getBitWidth();
1726 if (Opcode == TargetOpcode::G_UITOFP)
1727 IntSize -= IntKnown.countMinLeadingZeros();
1728 else
1729 IntSize -= IntKnown.countMinSignBits();
1730
1731 // If the exponent of the largest finite FP value can hold the largest
1732 // integer, the result of the cast must be finite.
1733 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
1734 Known.knownNot(fcInf);
1735 }
1736
1737 break;
1738 }
1739 // case TargetOpcode::G_MERGE_VALUES:
1740 case TargetOpcode::G_BUILD_VECTOR:
1741 case TargetOpcode::G_CONCAT_VECTORS: {
1742 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
1743
1744 if (!DstTy.isFixedVector())
1745 break;
1746
1747 bool First = true;
1748 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
1749 // We know the index we are inserting to, so clear it from Vec check.
1750 bool NeedsElt = DemandedElts[Idx];
1751
1752 // Do we demand the inserted element?
1753 if (NeedsElt) {
1754 Register Src = Merge.getSourceReg(Idx);
1755 if (First) {
1756 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
1757 First = false;
1758 } else {
1759 KnownFPClass Known2;
1760 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
1761 Known |= Known2;
1762 }
1763
1764 // If we don't know any bits, early out.
1765 if (Known.isUnknown())
1766 break;
1767 }
1768 }
1769
1770 break;
1771 }
1772 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1773 // Look through extract element. If the index is non-constant or
1774 // out-of-range demand all elements, otherwise just the extracted
1775 // element.
1776 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1777 Register Vec = Extract.getVectorReg();
1778 Register Idx = Extract.getIndexReg();
1779
1780 auto CIdx = getIConstantVRegVal(Idx, MRI);
1781
1782 LLT VecTy = MRI.getType(Vec);
1783
1784 if (VecTy.isFixedVector()) {
1785 unsigned NumElts = VecTy.getNumElements();
1786 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1787 if (CIdx && CIdx->ult(NumElts))
1788 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1789 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
1790 Depth + 1);
1791 }
1792
1793 break;
1794 }
1795 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1796 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
1797 Register Vec = Insert.getVectorReg();
1798 Register Elt = Insert.getElementReg();
1799 Register Idx = Insert.getIndexReg();
1800
1801 LLT VecTy = MRI.getType(Vec);
1802
1803 if (VecTy.isScalableVector())
1804 return;
1805
1806 auto CIdx = getIConstantVRegVal(Idx, MRI);
1807
1808 unsigned NumElts = DemandedElts.getBitWidth();
1809 APInt DemandedVecElts = DemandedElts;
1810 bool NeedsElt = true;
1811 // If we know the index we are inserting to, clear it from Vec check.
1812 if (CIdx && CIdx->ult(NumElts)) {
1813 DemandedVecElts.clearBit(CIdx->getZExtValue());
1814 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1815 }
1816
1817 // Do we demand the inserted element?
1818 if (NeedsElt) {
1819 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
1820 // If we don't know any bits, early out.
1821 if (Known.isUnknown())
1822 break;
1823 } else {
1824 Known.KnownFPClasses = fcNone;
1825 }
1826
1827 // Do we need anymore elements from Vec?
1828 if (!DemandedVecElts.isZero()) {
1829 KnownFPClass Known2;
1830 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
1831 Depth + 1);
1832 Known |= Known2;
1833 }
1834
1835 break;
1836 }
1837 case TargetOpcode::G_SHUFFLE_VECTOR: {
1838 // For undef elements, we don't know anything about the common state of
1839 // the shuffle result.
1840 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
1841 APInt DemandedLHS, DemandedRHS;
1842 if (DstTy.isScalableVector()) {
1843 assert(DemandedElts == APInt(1, 1));
1844 DemandedLHS = DemandedRHS = DemandedElts;
1845 } else {
1846 unsigned NumElts = MRI.getType(Shuf.getSrc1Reg()).getNumElements();
1847 if (!llvm::getShuffleDemandedElts(NumElts, Shuf.getMask(), DemandedElts,
1848 DemandedLHS, DemandedRHS)) {
1849 Known.resetAll();
1850 return;
1851 }
1852 }
1853
1854 if (!!DemandedLHS) {
1855 Register LHS = Shuf.getSrc1Reg();
1856 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
1857 Depth + 1);
1858
1859 // If we don't know any bits, early out.
1860 if (Known.isUnknown())
1861 break;
1862 } else {
1863 Known.KnownFPClasses = fcNone;
1864 }
1865
1866 if (!!DemandedRHS) {
1867 KnownFPClass Known2;
1868 Register RHS = Shuf.getSrc2Reg();
1869 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
1870 Depth + 1);
1871 Known |= Known2;
1872 }
1873 break;
1874 }
1875 case TargetOpcode::G_PHI: {
1876 // Cap PHI recursion below the global limit to avoid spending the entire
1877 // budget chasing loop back-edges (matches ValueTracking's
1878 // PhiRecursionLimit).
1880 break;
1881 // PHI's operands are a mix of registers and basic blocks interleaved.
1882 // We only care about the register ones.
1883 bool First = true;
1884 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
1885 const MachineOperand &Src = MI.getOperand(Idx);
1886 Register SrcReg = Src.getReg();
1887 if (First) {
1888 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known,
1889 Depth + 1);
1890 First = false;
1891 } else {
1892 KnownFPClass Known2;
1893 computeKnownFPClass(SrcReg, DemandedElts, InterestedClasses, Known2,
1894 Depth + 1);
1895 Known = Known.intersectWith(Known2);
1896 }
1897 if (Known.isUnknown())
1898 break;
1899 }
1900 break;
1901 }
1902 case TargetOpcode::COPY: {
1903 Register Src = MI.getOperand(1).getReg();
1904
1905 if (!Src.isVirtual())
1906 return;
1907
1908 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
1909 break;
1910 }
1911 }
1912}
1913
1915GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
1916 FPClassTest InterestedClasses,
1917 unsigned Depth) {
1918 KnownFPClass KnownClasses;
1919 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
1920 return KnownClasses;
1921}
1922
1923KnownFPClass GISelValueTracking::computeKnownFPClass(
1924 Register R, FPClassTest InterestedClasses, unsigned Depth) {
1925 KnownFPClass Known;
1926 computeKnownFPClass(R, Known, InterestedClasses, Depth);
1927 return Known;
1928}
1929
1930KnownFPClass GISelValueTracking::computeKnownFPClass(
1931 Register R, const APInt &DemandedElts, uint32_t Flags,
1932 FPClassTest InterestedClasses, unsigned Depth) {
1934 InterestedClasses &= ~fcNan;
1936 InterestedClasses &= ~fcInf;
1937
1938 KnownFPClass Result =
1939 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
1940
1942 Result.KnownFPClasses &= ~fcNan;
1944 Result.KnownFPClasses &= ~fcInf;
1945 return Result;
1946}
1947
1948KnownFPClass GISelValueTracking::computeKnownFPClass(
1949 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
1950 LLT Ty = MRI.getType(R);
1951 APInt DemandedElts =
1952 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1953 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
1954}
1955
1957 const MachineInstr *DefMI = MRI.getVRegDef(Val);
1958 if (!DefMI)
1959 return false;
1960
1961 if (DefMI->getFlag(MachineInstr::FmNoNans))
1962 return true;
1963
1964 // IEEE 754 arithmetic operations always quiet signaling NaNs. Short-circuit
1965 // the value-tracking analysis for the SNaN-only case: if the defining op is
1966 // known to quiet sNaN, the output can never be an sNaN.
1967 if (SNaN) {
1968 switch (DefMI->getOpcode()) {
1969 default:
1970 break;
1971 case TargetOpcode::G_FADD:
1972 case TargetOpcode::G_STRICT_FADD:
1973 case TargetOpcode::G_FSUB:
1974 case TargetOpcode::G_STRICT_FSUB:
1975 case TargetOpcode::G_FMUL:
1976 case TargetOpcode::G_STRICT_FMUL:
1977 case TargetOpcode::G_FDIV:
1978 case TargetOpcode::G_FREM:
1979 case TargetOpcode::G_FMA:
1980 case TargetOpcode::G_STRICT_FMA:
1981 case TargetOpcode::G_FMAD:
1982 case TargetOpcode::G_FSQRT:
1983 case TargetOpcode::G_STRICT_FSQRT:
1984 // Note: G_FABS and G_FNEG are bit-manipulation ops that preserve sNaN
1985 // exactly (LLVM LangRef: "never change anything except possibly the sign
1986 // bit"). They must NOT be listed here.
1987 case TargetOpcode::G_FSIN:
1988 case TargetOpcode::G_FCOS:
1989 case TargetOpcode::G_FSINCOS:
1990 case TargetOpcode::G_FTAN:
1991 case TargetOpcode::G_FASIN:
1992 case TargetOpcode::G_FACOS:
1993 case TargetOpcode::G_FATAN:
1994 case TargetOpcode::G_FATAN2:
1995 case TargetOpcode::G_FSINH:
1996 case TargetOpcode::G_FCOSH:
1997 case TargetOpcode::G_FTANH:
1998 case TargetOpcode::G_FEXP:
1999 case TargetOpcode::G_FEXP2:
2000 case TargetOpcode::G_FEXP10:
2001 case TargetOpcode::G_FLOG:
2002 case TargetOpcode::G_FLOG2:
2003 case TargetOpcode::G_FLOG10:
2004 case TargetOpcode::G_FPOWI:
2005 case TargetOpcode::G_FLDEXP:
2006 case TargetOpcode::G_STRICT_FLDEXP:
2007 case TargetOpcode::G_FFREXP:
2008 case TargetOpcode::G_INTRINSIC_TRUNC:
2009 case TargetOpcode::G_INTRINSIC_ROUND:
2010 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
2011 case TargetOpcode::G_FFLOOR:
2012 case TargetOpcode::G_FCEIL:
2013 case TargetOpcode::G_FRINT:
2014 case TargetOpcode::G_FNEARBYINT:
2015 case TargetOpcode::G_FPEXT:
2016 case TargetOpcode::G_FPTRUNC:
2017 case TargetOpcode::G_FCANONICALIZE:
2018 case TargetOpcode::G_FMINNUM:
2019 case TargetOpcode::G_FMAXNUM:
2020 case TargetOpcode::G_FMINNUM_IEEE:
2021 case TargetOpcode::G_FMAXNUM_IEEE:
2022 case TargetOpcode::G_FMINIMUM:
2023 case TargetOpcode::G_FMAXIMUM:
2024 case TargetOpcode::G_FMINIMUMNUM:
2025 case TargetOpcode::G_FMAXIMUMNUM:
2026 return true;
2027 }
2028 }
2029
2030 KnownFPClass FPClass = computeKnownFPClass(Val, SNaN ? fcSNan : fcNan);
2031
2032 if (SNaN)
2033 return FPClass.isKnownNever(fcSNan);
2034
2035 return FPClass.isKnownNeverNaN();
2036}
2037
2038/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
2039unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
2040 const APInt &DemandedElts,
2041 unsigned Depth) {
2042 // Test src1 first, since we canonicalize simpler expressions to the RHS.
2043 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
2044 if (Src1SignBits == 1)
2045 return 1;
2046 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
2047}
2048
2049/// Compute the known number of sign bits with attached range metadata in the
2050/// memory operand. If this is an extending load, accounts for the behavior of
2051/// the high bits.
2053 unsigned TyBits) {
2054 const MDNode *Ranges = Ld->getRanges();
2055 if (!Ranges)
2056 return 1;
2057
2059 if (TyBits > CR.getBitWidth()) {
2060 switch (Ld->getOpcode()) {
2061 case TargetOpcode::G_SEXTLOAD:
2062 CR = CR.signExtend(TyBits);
2063 break;
2064 case TargetOpcode::G_ZEXTLOAD:
2065 CR = CR.zeroExtend(TyBits);
2066 break;
2067 default:
2068 break;
2069 }
2070 }
2071
2072 return std::min(CR.getSignedMin().getNumSignBits(),
2074}
2075
2077 const APInt &DemandedElts,
2078 unsigned Depth) {
2079 MachineInstr &MI = *MRI.getVRegDef(R);
2080 unsigned Opcode = MI.getOpcode();
2081
2082 if (Opcode == TargetOpcode::G_CONSTANT)
2083 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
2084
2085 if (Depth == getMaxDepth())
2086 return 1;
2087
2088 if (!DemandedElts)
2089 return 1; // No demanded elts, better to assume we don't know anything.
2090
2091 LLT DstTy = MRI.getType(R);
2092 const unsigned TyBits = DstTy.getScalarSizeInBits();
2093
2094 // Handle the case where this is called on a register that does not have a
2095 // type constraint. This is unlikely to occur except by looking through copies
2096 // but it is possible for the initial register being queried to be in this
2097 // state.
2098 if (!DstTy.isValid())
2099 return 1;
2100
2101 unsigned FirstAnswer = 1;
2102 switch (Opcode) {
2103 case TargetOpcode::COPY: {
2104 MachineOperand &Src = MI.getOperand(1);
2105 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
2106 MRI.getType(Src.getReg()).isValid()) {
2107 // Don't increment Depth for this one since we didn't do any work.
2108 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
2109 }
2110
2111 return 1;
2112 }
2113 case TargetOpcode::G_SEXT: {
2114 Register Src = MI.getOperand(1).getReg();
2115 LLT SrcTy = MRI.getType(Src);
2116 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
2117 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
2118 }
2119 case TargetOpcode::G_ASSERT_SEXT:
2120 case TargetOpcode::G_SEXT_INREG: {
2121 // Max of the input and what this extends.
2122 Register Src = MI.getOperand(1).getReg();
2123 unsigned SrcBits = MI.getOperand(2).getImm();
2124 unsigned InRegBits = TyBits - SrcBits + 1;
2125 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
2126 InRegBits);
2127 }
2128 case TargetOpcode::G_LOAD: {
2129 GLoad *Ld = cast<GLoad>(&MI);
2130 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
2131 break;
2132
2133 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2134 }
2135 case TargetOpcode::G_SEXTLOAD: {
2137
2138 // FIXME: We need an in-memory type representation.
2139 if (DstTy.isVector())
2140 return 1;
2141
2142 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2143 if (NumBits != 1)
2144 return NumBits;
2145
2146 // e.g. i16->i32 = '17' bits known.
2147 const MachineMemOperand *MMO = *MI.memoperands_begin();
2148 return TyBits - MMO->getSizeInBits().getValue() + 1;
2149 }
2150 case TargetOpcode::G_ZEXTLOAD: {
2152
2153 // FIXME: We need an in-memory type representation.
2154 if (DstTy.isVector())
2155 return 1;
2156
2157 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
2158 if (NumBits != 1)
2159 return NumBits;
2160
2161 // e.g. i16->i32 = '16' bits known.
2162 const MachineMemOperand *MMO = *MI.memoperands_begin();
2163 return TyBits - MMO->getSizeInBits().getValue();
2164 }
2165 case TargetOpcode::G_AND:
2166 case TargetOpcode::G_OR:
2167 case TargetOpcode::G_XOR: {
2168 Register Src1 = MI.getOperand(1).getReg();
2169 unsigned Src1NumSignBits =
2170 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2171 if (Src1NumSignBits != 1) {
2172 Register Src2 = MI.getOperand(2).getReg();
2173 unsigned Src2NumSignBits =
2174 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2175 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
2176 }
2177 break;
2178 }
2179 case TargetOpcode::G_ASHR: {
2180 Register Src1 = MI.getOperand(1).getReg();
2181 Register Src2 = MI.getOperand(2).getReg();
2182 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2183 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
2184 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
2185 break;
2186 }
2187 case TargetOpcode::G_SHL: {
2188 Register Src1 = MI.getOperand(1).getReg();
2189 Register Src2 = MI.getOperand(2).getReg();
2190 if (std::optional<ConstantRange> ShAmtRange =
2191 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
2192 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
2193 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
2194
2195 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
2196 unsigned ExtOpc = ExtMI.getOpcode();
2197
2198 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
2199 // shifted out, then we can compute the number of sign bits for the
2200 // operand being extended. A future improvement could be to pass along the
2201 // "shifted left by" information in the recursive calls to
2202 // ComputeKnownSignBits. Allowing us to handle this more generically.
2203 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
2204 ExtOpc == TargetOpcode::G_ANYEXT) {
2205 LLT ExtTy = MRI.getType(Src1);
2206 Register Extendee = ExtMI.getOperand(1).getReg();
2207 LLT ExtendeeTy = MRI.getType(Extendee);
2208 uint64_t SizeDiff =
2209 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
2210
2211 if (SizeDiff <= MinShAmt) {
2212 unsigned Tmp =
2213 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
2214 if (MaxShAmt < Tmp)
2215 return Tmp - MaxShAmt;
2216 }
2217 }
2218 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
2219 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
2220 if (MaxShAmt < Tmp)
2221 return Tmp - MaxShAmt;
2222 }
2223 break;
2224 }
2225 case TargetOpcode::G_SREM: {
2226 // The sign bit is the LHS's sign bit, except when the result of the
2227 // remainder is zero. The magnitude of the result should be less than or
2228 // equal to the magnitude of the LHS. Therefore, the result should have
2229 // at least as many sign bits as the left hand side.
2230 Register Src = MI.getOperand(1).getReg();
2231 return computeNumSignBits(Src, DemandedElts, Depth + 1);
2232 }
2233 case TargetOpcode::G_TRUNC: {
2234 Register Src = MI.getOperand(1).getReg();
2235 LLT SrcTy = MRI.getType(Src);
2236
2237 // Check if the sign bits of source go down as far as the truncated value.
2238 unsigned DstTyBits = DstTy.getScalarSizeInBits();
2239 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
2240 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
2241 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
2242 return NumSrcSignBits - (NumSrcBits - DstTyBits);
2243 break;
2244 }
2245 case TargetOpcode::G_SELECT: {
2246 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
2247 MI.getOperand(3).getReg(), DemandedElts,
2248 Depth + 1);
2249 }
2250 case TargetOpcode::G_SMIN:
2251 case TargetOpcode::G_SMAX:
2252 case TargetOpcode::G_UMIN:
2253 case TargetOpcode::G_UMAX:
2254 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
2255 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
2256 MI.getOperand(2).getReg(), DemandedElts,
2257 Depth + 1);
2258 case TargetOpcode::G_SADDO:
2259 case TargetOpcode::G_SADDE:
2260 case TargetOpcode::G_UADDO:
2261 case TargetOpcode::G_UADDE:
2262 case TargetOpcode::G_SSUBO:
2263 case TargetOpcode::G_SSUBE:
2264 case TargetOpcode::G_USUBO:
2265 case TargetOpcode::G_USUBE:
2266 case TargetOpcode::G_SMULO:
2267 case TargetOpcode::G_UMULO: {
2268 // If compares returns 0/-1, all bits are sign bits.
2269 // We know that we have an integer-based boolean since these operations
2270 // are only available for integer.
2271 if (MI.getOperand(1).getReg() == R) {
2272 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2274 return TyBits;
2275 }
2276
2277 break;
2278 }
2279 case TargetOpcode::G_SUB: {
2280 Register Src2 = MI.getOperand(2).getReg();
2281 unsigned Src2NumSignBits =
2282 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2283 if (Src2NumSignBits == 1)
2284 return 1; // Early out.
2285
2286 // Handle NEG.
2287 Register Src1 = MI.getOperand(1).getReg();
2288 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2289 if (Known1.isZero()) {
2290 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2291 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2292 // sign bits set.
2293 if ((Known2.Zero | 1).isAllOnes())
2294 return TyBits;
2295
2296 // If the input is known to be positive (the sign bit is known clear),
2297 // the output of the NEG has, at worst, the same number of sign bits as
2298 // the input.
2299 if (Known2.isNonNegative()) {
2300 FirstAnswer = Src2NumSignBits;
2301 break;
2302 }
2303
2304 // Otherwise, we treat this like a SUB.
2305 }
2306
2307 unsigned Src1NumSignBits =
2308 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2309 if (Src1NumSignBits == 1)
2310 return 1; // Early Out.
2311
2312 // Sub can have at most one carry bit. Thus we know that the output
2313 // is, at worst, one more bit than the inputs.
2314 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2315 break;
2316 }
2317 case TargetOpcode::G_ADD: {
2318 Register Src2 = MI.getOperand(2).getReg();
2319 unsigned Src2NumSignBits =
2320 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2321 if (Src2NumSignBits <= 2)
2322 return 1; // Early out.
2323
2324 Register Src1 = MI.getOperand(1).getReg();
2325 unsigned Src1NumSignBits =
2326 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2327 if (Src1NumSignBits == 1)
2328 return 1; // Early Out.
2329
2330 // Special case decrementing a value (ADD X, -1):
2331 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2332 if (Known2.isAllOnes()) {
2333 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2334 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2335 // sign bits set.
2336 if ((Known1.Zero | 1).isAllOnes())
2337 return TyBits;
2338
2339 // If we are subtracting one from a positive number, there is no carry
2340 // out of the result.
2341 if (Known1.isNonNegative()) {
2342 FirstAnswer = Src1NumSignBits;
2343 break;
2344 }
2345
2346 // Otherwise, we treat this like an ADD.
2347 }
2348
2349 // Add can have at most one carry bit. Thus we know that the output
2350 // is, at worst, one more bit than the inputs.
2351 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2352 break;
2353 }
2354 case TargetOpcode::G_FCMP:
2355 case TargetOpcode::G_ICMP: {
2356 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2357 if (TyBits == 1)
2358 break;
2359 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2361 return TyBits; // All bits are sign bits.
2363 return TyBits - 1; // Every always-zero bit is a sign bit.
2364 break;
2365 }
2366 case TargetOpcode::G_BUILD_VECTOR: {
2367 // Collect the known bits that are shared by every demanded vector element.
2368 FirstAnswer = TyBits;
2369 APInt SingleDemandedElt(1, 1);
2370 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2371 if (!DemandedElts[I])
2372 continue;
2373
2374 unsigned Tmp2 =
2375 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2376 FirstAnswer = std::min(FirstAnswer, Tmp2);
2377
2378 // If we don't know any bits, early out.
2379 if (FirstAnswer == 1)
2380 break;
2381 }
2382 break;
2383 }
2384 case TargetOpcode::G_CONCAT_VECTORS: {
2385 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2386 break;
2387 FirstAnswer = TyBits;
2388 // Determine the minimum number of sign bits across all demanded
2389 // elts of the input vectors. Early out if the result is already 1.
2390 unsigned NumSubVectorElts =
2391 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2392 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2393 APInt DemandedSub =
2394 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2395 if (!DemandedSub)
2396 continue;
2397 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2398
2399 FirstAnswer = std::min(FirstAnswer, Tmp2);
2400
2401 // If we don't know any bits, early out.
2402 if (FirstAnswer == 1)
2403 break;
2404 }
2405 break;
2406 }
2407 case TargetOpcode::G_SHUFFLE_VECTOR: {
2408 // Collect the minimum number of sign bits that are shared by every vector
2409 // element referenced by the shuffle.
2410 APInt DemandedLHS, DemandedRHS;
2411 Register Src1 = MI.getOperand(1).getReg();
2412 unsigned NumElts = MRI.getType(Src1).getNumElements();
2413 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2414 DemandedElts, DemandedLHS, DemandedRHS))
2415 return 1;
2416
2417 if (!!DemandedLHS)
2418 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2419 // If we don't know anything, early out and try computeKnownBits fall-back.
2420 if (FirstAnswer == 1)
2421 break;
2422 if (!!DemandedRHS) {
2423 unsigned Tmp2 =
2424 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2425 FirstAnswer = std::min(FirstAnswer, Tmp2);
2426 }
2427 break;
2428 }
2429 case TargetOpcode::G_SPLAT_VECTOR: {
2430 // Check if the sign bits of source go down as far as the truncated value.
2431 Register Src = MI.getOperand(1).getReg();
2432 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2433 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2434 if (NumSrcSignBits > (NumSrcBits - TyBits))
2435 return NumSrcSignBits - (NumSrcBits - TyBits);
2436 break;
2437 }
2438 case TargetOpcode::G_INTRINSIC:
2439 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2440 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2441 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2442 default: {
2443 unsigned NumBits =
2444 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2445 if (NumBits > 1)
2446 FirstAnswer = std::max(FirstAnswer, NumBits);
2447 break;
2448 }
2449 }
2450
2451 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2452 // use this information.
2453 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2454 APInt Mask;
2455 if (Known.isNonNegative()) { // sign bit is 0
2456 Mask = Known.Zero;
2457 } else if (Known.isNegative()) { // sign bit is 1;
2458 Mask = Known.One;
2459 } else {
2460 // Nothing known.
2461 return FirstAnswer;
2462 }
2463
2464 // Okay, we know that the sign bit in Mask is set. Use CLO to determine
2465 // the number of identical bits in the top of the input value.
2466 Mask <<= Mask.getBitWidth() - TyBits;
2467 return std::max(FirstAnswer, Mask.countl_one());
2468}
2469
2471 LLT Ty = MRI.getType(R);
2472 APInt DemandedElts =
2473 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2474 return computeNumSignBits(R, DemandedElts, Depth);
2475}
2476
2478 Register R, const APInt &DemandedElts, unsigned Depth) {
2479 // Shifting more than the bitwidth is not valid.
2480 MachineInstr &MI = *MRI.getVRegDef(R);
2481 unsigned Opcode = MI.getOpcode();
2482
2483 LLT Ty = MRI.getType(R);
2484 unsigned BitWidth = Ty.getScalarSizeInBits();
2485
2486 if (Opcode == TargetOpcode::G_CONSTANT) {
2487 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2488 if (ShAmt.uge(BitWidth))
2489 return std::nullopt;
2490 return ConstantRange(ShAmt);
2491 }
2492
2493 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2494 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2495 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2496 if (!DemandedElts[I])
2497 continue;
2498 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2499 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2500 MinAmt = MaxAmt = nullptr;
2501 break;
2502 }
2503
2504 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2505 if (ShAmt.uge(BitWidth))
2506 return std::nullopt;
2507 if (!MinAmt || MinAmt->ugt(ShAmt))
2508 MinAmt = &ShAmt;
2509 if (!MaxAmt || MaxAmt->ult(ShAmt))
2510 MaxAmt = &ShAmt;
2511 }
2512 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2513 "Failed to find matching min/max shift amounts");
2514 if (MinAmt && MaxAmt)
2515 return ConstantRange(*MinAmt, *MaxAmt + 1);
2516 }
2517
2518 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2519 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2520 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2521 if (KnownAmt.getMaxValue().ult(BitWidth))
2522 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2523
2524 return std::nullopt;
2525}
2526
2528 Register R, const APInt &DemandedElts, unsigned Depth) {
2529 if (std::optional<ConstantRange> AmtRange =
2530 getValidShiftAmountRange(R, DemandedElts, Depth))
2531 return AmtRange->getUnsignedMin().getZExtValue();
2532 return std::nullopt;
2533}
2534
2540
2545
2547 if (!Info) {
2548 unsigned MaxDepth =
2550 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2551 }
2552 return *Info;
2553}
2554
2555AnalysisKey GISelValueTrackingAnalysis::Key;
2556
2562
2566 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
2567 const auto &MRI = MF.getRegInfo();
2568 OS << "name: ";
2569 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
2570 OS << '\n';
2571
2572 for (MachineBasicBlock &BB : MF) {
2573 for (MachineInstr &MI : BB) {
2574 for (MachineOperand &MO : MI.defs()) {
2575 if (!MO.isReg() || MO.getReg().isPhysical())
2576 continue;
2577 Register Reg = MO.getReg();
2578 if (!MRI.getType(Reg).isValid())
2579 continue;
2580 KnownBits Known = VTA.getKnownBits(Reg);
2581 unsigned SignedBits = VTA.computeNumSignBits(Reg);
2582 OS << " " << MO << " KnownBits:" << Known << " SignBits:" << SignedBits
2583 << '\n';
2584 };
2585 }
2586 }
2587 return PreservedAnalyses::all();
2588}
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:1197
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2023
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:2049
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
GISelValueTracking & get(MachineFunction &MF)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
KnownBits getKnownBits(Register R)
Align computeKnownAlignment(Register R, unsigned Depth=0)
std::optional< ConstantRange > getValidShiftAmountRange(Register R, const APInt &DemandedElts, unsigned Depth)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
bool maskedValueIsZero(Register Val, const APInt &Mask)
std::optional< uint64_t > getValidMinimumShiftAmount(Register R, const APInt &DemandedElts, unsigned Depth=0)
If a G_SHL/G_ASHR/G_LSHR node with shift operand R has shift amounts that are all less than the eleme...
const DataLayout & getDataLayout() const
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
const MachineFunction & getMachineFunction() const
bool isKnownNeverNaN(Register Val, bool SNaN=false)
Returns true if Val can be assumed to never be a NaN.
void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
Represents a G_LOAD.
Represents a G_SEXTLOAD.
Register getCondReg() const
Register getFalseReg() const
Register getTrueReg() const
ArrayRef< int > getMask() const
Represents a G_ZEXTLOAD.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
LLT getScalarType() const
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr ElementCount getElementCount() const
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
TypeSize getValue() const
Metadata node.
Definition Metadata.h:1080
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
LLT getMemoryType() const
Return the memory type of the memory reference.
const MDNode * getRanges() const
Return the range tag for the memory reference.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
operand_type_match m_Reg()
UnaryOp_match< SrcTy, TargetOpcode::G_FFLOOR > m_GFFloor(const SrcTy &Src)
operand_type_match m_Pred()
bind_ty< FPClassTest > m_FPClassTest(FPClassTest &T)
deferred_ty< Register > m_DeferredReg(Register &R)
Similar to m_SpecificReg/Type, but the specific value to match originated from an earlier sub-pattern...
BinaryOp_match< LHS, RHS, TargetOpcode::G_FSUB, false > m_GFSub(const LHS &L, const RHS &R)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
ClassifyOp_match< LHS, Test, TargetOpcode::G_IS_FPCLASS > m_GIsFPClass(const LHS &L, const Test &T)
Matches the register and immediate used in a fpclass test G_IS_FPCLASS val, 96.
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_FCMP > m_GFCmp(const Pred &P, const LHS &L, const RHS &R)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:294
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:1631
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:1527
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
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:165
KnownBits byteSwap() const
Definition KnownBits.h:553
static LLVM_ABI KnownBits fshl(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshl(LHS, RHS, Amt).
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:303
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:84
KnownBits reverseBits() const
Definition KnownBits.h:557
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:176
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false, bool SelfAdd=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:361
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:109
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:162
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:239
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:325
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:184
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:200
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:146
static LLVM_ABI KnownBits fshr(const KnownBits &LHS, const KnownBits &RHS, const APInt &Amt)
Compute known bits for fshr(LHS, RHS, Amt).
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits 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.