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