LLVM 22.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"
40
41#define DEBUG_TYPE "gisel-known-bits"
42
43using namespace llvm;
44using namespace MIPatternMatch;
45
47
49 "Analysis for ComputingKnownBits", false, true)
50
52 : MF(MF), MRI(MF.getRegInfo()), TL(*MF.getSubtarget().getTargetLowering()),
53 DL(MF.getFunction().getDataLayout()), MaxDepth(MaxDepth) {}
54
56 const MachineInstr *MI = MRI.getVRegDef(R);
57 switch (MI->getOpcode()) {
58 case TargetOpcode::COPY:
59 return computeKnownAlignment(MI->getOperand(1).getReg(), Depth);
60 case TargetOpcode::G_ASSERT_ALIGN: {
61 // TODO: Min with source
62 return Align(MI->getOperand(2).getImm());
63 }
64 case TargetOpcode::G_FRAME_INDEX: {
65 int FrameIdx = MI->getOperand(1).getIndex();
66 return MF.getFrameInfo().getObjectAlign(FrameIdx);
67 }
68 case TargetOpcode::G_INTRINSIC:
69 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
70 case TargetOpcode::G_INTRINSIC_CONVERGENT:
71 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
72 default:
73 return TL.computeKnownAlignForTargetInstr(*this, R, MRI, Depth + 1);
74 }
75}
76
78 assert(MI.getNumExplicitDefs() == 1 &&
79 "expected single return generic instruction");
80 return getKnownBits(MI.getOperand(0).getReg());
81}
82
84 const LLT Ty = MRI.getType(R);
85 // Since the number of lanes in a scalable vector is unknown at compile time,
86 // we track one bit which is implicitly broadcast to all lanes. This means
87 // that all lanes in a scalable vector are considered demanded.
88 APInt DemandedElts =
89 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
90 return getKnownBits(R, DemandedElts);
91}
92
94 const APInt &DemandedElts,
95 unsigned Depth) {
96 KnownBits Known;
97 computeKnownBitsImpl(R, Known, DemandedElts, Depth);
98 return Known;
99}
100
102 LLT Ty = MRI.getType(R);
103 unsigned BitWidth = Ty.getScalarSizeInBits();
105}
106
110
114
115[[maybe_unused]] static void
116dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) {
117 dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth
118 << "] Computed for: " << MI << "[" << Depth << "] Known: 0x"
119 << toString(Known.Zero | Known.One, 16, false) << "\n"
120 << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false)
121 << "\n"
122 << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false)
123 << "\n";
124}
125
126/// Compute known bits for the intersection of \p Src0 and \p Src1
127void GISelValueTracking::computeKnownBitsMin(Register Src0, Register Src1,
128 KnownBits &Known,
129 const APInt &DemandedElts,
130 unsigned Depth) {
131 // Test src1 first, since we canonicalize simpler expressions to the RHS.
132 computeKnownBitsImpl(Src1, Known, DemandedElts, Depth);
133
134 // If we don't know any bits, early out.
135 if (Known.isUnknown())
136 return;
137
138 KnownBits Known2;
139 computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
140
141 // Only known if known in both the LHS and RHS.
142 Known = Known.intersectWith(Known2);
143}
144
145// Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
146// created using Width. Use this function when the inputs are KnownBits
147// objects. TODO: Move this KnownBits.h if this is usable in more cases.
148static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown,
149 const KnownBits &OffsetKnown,
150 const KnownBits &WidthKnown) {
151 KnownBits Mask(BitWidth);
152 Mask.Zero = APInt::getBitsSetFrom(
154 Mask.One = APInt::getLowBitsSet(
156 return KnownBits::lshr(SrcOpKnown, OffsetKnown) & Mask;
157}
158
160 const APInt &DemandedElts,
161 unsigned Depth) {
162 MachineInstr &MI = *MRI.getVRegDef(R);
163 unsigned Opcode = MI.getOpcode();
164 LLT DstTy = MRI.getType(R);
165
166 // Handle the case where this is called on a register that does not have a
167 // type constraint. For example, it may be post-ISel or this target might not
168 // preserve the type when early-selecting instructions.
169 if (!DstTy.isValid()) {
170 Known = KnownBits();
171 return;
172 }
173
174#ifndef NDEBUG
175 if (DstTy.isFixedVector()) {
176 assert(
177 DstTy.getNumElements() == DemandedElts.getBitWidth() &&
178 "DemandedElt width should equal the fixed vector number of elements");
179 } else {
180 assert(DemandedElts.getBitWidth() == 1 && DemandedElts == APInt(1, 1) &&
181 "DemandedElt width should be 1 for scalars or scalable vectors");
182 }
183#endif
184
185 unsigned BitWidth = DstTy.getScalarSizeInBits();
186 Known = KnownBits(BitWidth); // Don't know anything
187
188 // Depth may get bigger than max depth if it gets passed to a different
189 // GISelValueTracking object.
190 // This may happen when say a generic part uses a GISelValueTracking object
191 // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr
192 // which creates a new GISelValueTracking object with a different and smaller
193 // depth. If we just check for equality, we would never exit if the depth
194 // that is passed down to the target specific GISelValueTracking object is
195 // already bigger than its max depth.
196 if (Depth >= getMaxDepth())
197 return;
198
199 if (!DemandedElts)
200 return; // No demanded elts, better to assume we don't know anything.
201
202 KnownBits Known2;
203
204 switch (Opcode) {
205 default:
206 TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI,
207 Depth);
208 break;
209 case TargetOpcode::G_BUILD_VECTOR: {
210 // Collect the known bits that are shared by every demanded vector element.
211 Known.Zero.setAllBits();
212 Known.One.setAllBits();
213 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
214 if (!DemandedElts[I])
215 continue;
216
217 computeKnownBitsImpl(MO.getReg(), Known2, APInt(1, 1), Depth + 1);
218
219 // Known bits are the values that are shared by every demanded element.
220 Known = Known.intersectWith(Known2);
221
222 // If we don't know any bits, early out.
223 if (Known.isUnknown())
224 break;
225 }
226 break;
227 }
228 case TargetOpcode::G_SPLAT_VECTOR: {
229 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, APInt(1, 1),
230 Depth + 1);
231 // Implicitly truncate the bits to match the official semantics of
232 // G_SPLAT_VECTOR.
233 Known = Known.trunc(BitWidth);
234 break;
235 }
236 case TargetOpcode::COPY:
237 case TargetOpcode::G_PHI:
238 case TargetOpcode::PHI: {
241 // Destination registers should not have subregisters at this
242 // point of the pipeline, otherwise the main live-range will be
243 // defined more than once, which is against SSA.
244 assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?");
245 // PHI's operand are a mix of registers and basic blocks interleaved.
246 // We only care about the register ones.
247 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
248 const MachineOperand &Src = MI.getOperand(Idx);
249 Register SrcReg = Src.getReg();
250 // Look through trivial copies and phis but don't look through trivial
251 // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits
252 // analysis is currently unable to determine the bit width of a
253 // register class.
254 //
255 // We can't use NoSubRegister by name as it's defined by each target but
256 // it's always defined to be 0 by tablegen.
257 if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ &&
258 MRI.getType(SrcReg).isValid()) {
259 // For COPYs we don't do anything, don't increase the depth.
260 computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
261 Depth + (Opcode != TargetOpcode::COPY));
262 Known2 = Known2.anyextOrTrunc(BitWidth);
263 Known = Known.intersectWith(Known2);
264 // If we reach a point where we don't know anything
265 // just stop looking through the operands.
266 if (Known.isUnknown())
267 break;
268 } else {
269 // We know nothing.
270 Known = KnownBits(BitWidth);
271 break;
272 }
273 }
274 break;
275 }
276 case TargetOpcode::G_CONSTANT: {
277 Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
278 break;
279 }
280 case TargetOpcode::G_FRAME_INDEX: {
281 int FrameIdx = MI.getOperand(1).getIndex();
282 TL.computeKnownBitsForFrameIndex(FrameIdx, Known, MF);
283 break;
284 }
285 case TargetOpcode::G_SUB: {
286 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
287 Depth + 1);
288 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
289 Depth + 1);
290 Known = KnownBits::sub(Known, Known2);
291 break;
292 }
293 case TargetOpcode::G_XOR: {
294 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
295 Depth + 1);
296 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
297 Depth + 1);
298
299 Known ^= Known2;
300 break;
301 }
302 case TargetOpcode::G_PTR_ADD: {
303 if (DstTy.isVector())
304 break;
305 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets?
306 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
307 if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace()))
308 break;
309 [[fallthrough]];
310 }
311 case TargetOpcode::G_ADD: {
312 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
313 Depth + 1);
314 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
315 Depth + 1);
316 Known = KnownBits::add(Known, Known2);
317 break;
318 }
319 case TargetOpcode::G_AND: {
320 // If either the LHS or the RHS are Zero, the result is zero.
321 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
322 Depth + 1);
323 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
324 Depth + 1);
325
326 Known &= Known2;
327 break;
328 }
329 case TargetOpcode::G_OR: {
330 // If either the LHS or the RHS are Zero, the result is zero.
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_MUL: {
340 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
341 Depth + 1);
342 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
343 Depth + 1);
344 Known = KnownBits::mul(Known, Known2);
345 break;
346 }
347 case TargetOpcode::G_UMULH: {
348 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
349 Depth + 1);
350 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
351 Depth + 1);
352 Known = KnownBits::mulhu(Known, Known2);
353 break;
354 }
355 case TargetOpcode::G_SMULH: {
356 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
357 Depth + 1);
358 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
359 Depth + 1);
360 Known = KnownBits::mulhs(Known, Known2);
361 break;
362 }
363 case TargetOpcode::G_SELECT: {
364 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
365 Known, DemandedElts, Depth + 1);
366 break;
367 }
368 case TargetOpcode::G_SMIN: {
369 // TODO: Handle clamp pattern with number of sign bits
370 KnownBits KnownRHS;
371 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
372 Depth + 1);
373 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
374 Depth + 1);
375 Known = KnownBits::smin(Known, KnownRHS);
376 break;
377 }
378 case TargetOpcode::G_SMAX: {
379 // TODO: Handle clamp pattern with number of sign bits
380 KnownBits KnownRHS;
381 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
382 Depth + 1);
383 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
384 Depth + 1);
385 Known = KnownBits::smax(Known, KnownRHS);
386 break;
387 }
388 case TargetOpcode::G_UMIN: {
389 KnownBits KnownRHS;
390 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
391 Depth + 1);
392 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
393 Depth + 1);
394 Known = KnownBits::umin(Known, KnownRHS);
395 break;
396 }
397 case TargetOpcode::G_UMAX: {
398 KnownBits KnownRHS;
399 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
400 Depth + 1);
401 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
402 Depth + 1);
403 Known = KnownBits::umax(Known, KnownRHS);
404 break;
405 }
406 case TargetOpcode::G_FCMP:
407 case TargetOpcode::G_ICMP: {
408 if (DstTy.isVector())
409 break;
410 if (TL.getBooleanContents(DstTy.isVector(),
411 Opcode == TargetOpcode::G_FCMP) ==
413 BitWidth > 1)
414 Known.Zero.setBitsFrom(1);
415 break;
416 }
417 case TargetOpcode::G_SEXT: {
418 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
419 Depth + 1);
420 // If the sign bit is known to be zero or one, then sext will extend
421 // it to the top bits, else it will just zext.
422 Known = Known.sext(BitWidth);
423 break;
424 }
425 case TargetOpcode::G_ASSERT_SEXT:
426 case TargetOpcode::G_SEXT_INREG: {
427 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
428 Depth + 1);
429 Known = Known.sextInReg(MI.getOperand(2).getImm());
430 break;
431 }
432 case TargetOpcode::G_ANYEXT: {
433 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
434 Depth + 1);
435 Known = Known.anyext(BitWidth);
436 break;
437 }
438 case TargetOpcode::G_LOAD: {
439 const MachineMemOperand *MMO = *MI.memoperands_begin();
440 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
441 if (const MDNode *Ranges = MMO->getRanges())
442 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
443 Known = KnownRange.anyext(Known.getBitWidth());
444 break;
445 }
446 case TargetOpcode::G_SEXTLOAD:
447 case TargetOpcode::G_ZEXTLOAD: {
448 if (DstTy.isVector())
449 break;
450 const MachineMemOperand *MMO = *MI.memoperands_begin();
451 KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
452 if (const MDNode *Ranges = MMO->getRanges())
453 computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
454 Known = Opcode == TargetOpcode::G_SEXTLOAD
455 ? KnownRange.sext(Known.getBitWidth())
456 : KnownRange.zext(Known.getBitWidth());
457 break;
458 }
459 case TargetOpcode::G_ASHR: {
460 KnownBits LHSKnown, RHSKnown;
461 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
462 Depth + 1);
463 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
464 Depth + 1);
465 Known = KnownBits::ashr(LHSKnown, RHSKnown);
466 break;
467 }
468 case TargetOpcode::G_LSHR: {
469 KnownBits LHSKnown, RHSKnown;
470 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
471 Depth + 1);
472 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
473 Depth + 1);
474 Known = KnownBits::lshr(LHSKnown, RHSKnown);
475 break;
476 }
477 case TargetOpcode::G_SHL: {
478 KnownBits LHSKnown, RHSKnown;
479 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
480 Depth + 1);
481 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
482 Depth + 1);
483 Known = KnownBits::shl(LHSKnown, RHSKnown);
484 break;
485 }
486 case TargetOpcode::G_INTTOPTR:
487 case TargetOpcode::G_PTRTOINT:
488 if (DstTy.isVector())
489 break;
490 // Fall through and handle them the same as zext/trunc.
491 [[fallthrough]];
492 case TargetOpcode::G_ZEXT:
493 case TargetOpcode::G_TRUNC: {
494 Register SrcReg = MI.getOperand(1).getReg();
495 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
496 Known = Known.zextOrTrunc(BitWidth);
497 break;
498 }
499 case TargetOpcode::G_ASSERT_ZEXT: {
500 Register SrcReg = MI.getOperand(1).getReg();
501 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
502
503 unsigned SrcBitWidth = MI.getOperand(2).getImm();
504 assert(SrcBitWidth && "SrcBitWidth can't be zero");
505 APInt InMask = APInt::getLowBitsSet(BitWidth, SrcBitWidth);
506 Known.Zero |= (~InMask);
507 Known.One &= (~Known.Zero);
508 break;
509 }
510 case TargetOpcode::G_ASSERT_ALIGN: {
511 int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
512
513 // TODO: Should use maximum with source
514 // If a node is guaranteed to be aligned, set low zero bits accordingly as
515 // well as clearing one bits.
516 Known.Zero.setLowBits(LogOfAlign);
517 Known.One.clearLowBits(LogOfAlign);
518 break;
519 }
520 case TargetOpcode::G_MERGE_VALUES: {
521 unsigned NumOps = MI.getNumOperands();
522 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
523
524 for (unsigned I = 0; I != NumOps - 1; ++I) {
525 KnownBits SrcOpKnown;
526 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
527 DemandedElts, Depth + 1);
528 Known.insertBits(SrcOpKnown, I * OpSize);
529 }
530 break;
531 }
532 case TargetOpcode::G_UNMERGE_VALUES: {
533 unsigned NumOps = MI.getNumOperands();
534 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
535 LLT SrcTy = MRI.getType(SrcReg);
536
537 if (SrcTy.isVector() && SrcTy.getScalarType() != DstTy.getScalarType())
538 return; // TODO: Handle vector->subelement unmerges
539
540 // Figure out the result operand index
541 unsigned DstIdx = 0;
542 for (; DstIdx != NumOps - 1 && MI.getOperand(DstIdx).getReg() != R;
543 ++DstIdx)
544 ;
545
546 APInt SubDemandedElts = DemandedElts;
547 if (SrcTy.isVector()) {
548 unsigned DstLanes = DstTy.isVector() ? DstTy.getNumElements() : 1;
549 SubDemandedElts =
550 DemandedElts.zext(SrcTy.getNumElements()).shl(DstIdx * DstLanes);
551 }
552
553 KnownBits SrcOpKnown;
554 computeKnownBitsImpl(SrcReg, SrcOpKnown, SubDemandedElts, Depth + 1);
555
556 if (SrcTy.isVector())
557 Known = std::move(SrcOpKnown);
558 else
559 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
560 break;
561 }
562 case TargetOpcode::G_BSWAP: {
563 Register SrcReg = MI.getOperand(1).getReg();
564 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
565 Known = Known.byteSwap();
566 break;
567 }
568 case TargetOpcode::G_BITREVERSE: {
569 Register SrcReg = MI.getOperand(1).getReg();
570 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
571 Known = Known.reverseBits();
572 break;
573 }
574 case TargetOpcode::G_CTPOP: {
575 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
576 Depth + 1);
577 // We can bound the space the count needs. Also, bits known to be zero
578 // can't contribute to the population.
579 unsigned BitsPossiblySet = Known2.countMaxPopulation();
580 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
581 Known.Zero.setBitsFrom(LowBits);
582 // TODO: we could bound Known.One using the lower bound on the number of
583 // bits which might be set provided by popcnt KnownOne2.
584 break;
585 }
586 case TargetOpcode::G_UBFX: {
587 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
588 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
589 Depth + 1);
590 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
591 Depth + 1);
592 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
593 Depth + 1);
594 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
595 break;
596 }
597 case TargetOpcode::G_SBFX: {
598 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
599 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
600 Depth + 1);
601 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
602 Depth + 1);
603 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
604 Depth + 1);
605 OffsetKnown = OffsetKnown.sext(BitWidth);
606 WidthKnown = WidthKnown.sext(BitWidth);
607 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
608 // Sign extend the extracted value using shift left and arithmetic shift
609 // right.
611 KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
612 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
613 break;
614 }
615 case TargetOpcode::G_UADDO:
616 case TargetOpcode::G_UADDE:
617 case TargetOpcode::G_SADDO:
618 case TargetOpcode::G_SADDE:
619 case TargetOpcode::G_USUBO:
620 case TargetOpcode::G_USUBE:
621 case TargetOpcode::G_SSUBO:
622 case TargetOpcode::G_SSUBE:
623 case TargetOpcode::G_UMULO:
624 case TargetOpcode::G_SMULO: {
625 if (MI.getOperand(1).getReg() == R) {
626 // If we know the result of a compare has the top bits zero, use this
627 // info.
628 if (TL.getBooleanContents(DstTy.isVector(), false) ==
630 BitWidth > 1)
631 Known.Zero.setBitsFrom(1);
632 }
633 break;
634 }
635 case TargetOpcode::G_CTLZ:
636 case TargetOpcode::G_CTLZ_ZERO_UNDEF: {
637 KnownBits SrcOpKnown;
638 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
639 Depth + 1);
640 // If we have a known 1, its position is our upper bound.
641 unsigned PossibleLZ = SrcOpKnown.countMaxLeadingZeros();
642 unsigned LowBits = llvm::bit_width(PossibleLZ);
643 Known.Zero.setBitsFrom(LowBits);
644 break;
645 }
646 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
648 Register InVec = Extract.getVectorReg();
649 Register EltNo = Extract.getIndexReg();
650
651 auto ConstEltNo = getIConstantVRegVal(EltNo, MRI);
652
653 LLT VecVT = MRI.getType(InVec);
654 // computeKnownBits not yet implemented for scalable vectors.
655 if (VecVT.isScalableVector())
656 break;
657
658 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
659 const unsigned NumSrcElts = VecVT.getNumElements();
660 // A return type different from the vector's element type may lead to
661 // issues with pattern selection. Bail out to avoid that.
662 if (BitWidth > EltBitWidth)
663 break;
664
665 Known.Zero.setAllBits();
666 Known.One.setAllBits();
667
668 // If we know the element index, just demand that vector element, else for
669 // an unknown element index, ignore DemandedElts and demand them all.
670 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
671 if (ConstEltNo && ConstEltNo->ult(NumSrcElts))
672 DemandedSrcElts =
673 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
674
675 computeKnownBitsImpl(InVec, Known, DemandedSrcElts, Depth + 1);
676 break;
677 }
678 case TargetOpcode::G_SHUFFLE_VECTOR: {
679 APInt DemandedLHS, DemandedRHS;
680 // Collect the known bits that are shared by every vector element referenced
681 // by the shuffle.
682 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
683 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
684 DemandedElts, DemandedLHS, DemandedRHS))
685 break;
686
687 // Known bits are the values that are shared by every demanded element.
688 Known.Zero.setAllBits();
689 Known.One.setAllBits();
690 if (!!DemandedLHS) {
691 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
692 Depth + 1);
693 Known = Known.intersectWith(Known2);
694 }
695 // If we don't know any bits, early out.
696 if (Known.isUnknown())
697 break;
698 if (!!DemandedRHS) {
699 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
700 Depth + 1);
701 Known = Known.intersectWith(Known2);
702 }
703 break;
704 }
705 case TargetOpcode::G_CONCAT_VECTORS: {
706 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
707 break;
708 // Split DemandedElts and test each of the demanded subvectors.
709 Known.Zero.setAllBits();
710 Known.One.setAllBits();
711 unsigned NumSubVectorElts =
712 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
713
714 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
715 APInt DemandedSub =
716 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
717 if (!!DemandedSub) {
718 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
719
720 Known = Known.intersectWith(Known2);
721 }
722 // If we don't know any bits, early out.
723 if (Known.isUnknown())
724 break;
725 }
726 break;
727 }
728 case TargetOpcode::G_ABS: {
729 Register SrcReg = MI.getOperand(1).getReg();
730 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
731 Known = Known.abs();
732 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
733 1);
734 break;
735 }
736 }
737
738 LLVM_DEBUG(dumpResult(MI, Known, Depth));
739}
740
742 Ty = Ty.getScalarType();
744 return Mode.Output == DenormalMode::IEEE ||
746}
747
748void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
749 FPClassTest InterestedClasses,
750 unsigned Depth) {
751 LLT Ty = MRI.getType(R);
752 APInt DemandedElts =
753 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
754 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
755}
756
757void GISelValueTracking::computeKnownFPClassForFPTrunc(
758 const MachineInstr &MI, const APInt &DemandedElts,
759 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
760 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
761 fcNone)
762 return;
763
764 Register Val = MI.getOperand(1).getReg();
765 KnownFPClass KnownSrc;
766 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
767 Depth + 1);
768
769 // Sign should be preserved
770 // TODO: Handle cannot be ordered greater than zero
771 if (KnownSrc.cannotBeOrderedLessThanZero())
773
774 Known.propagateNaN(KnownSrc, true);
775
776 // Infinity needs a range check.
777}
778
779void GISelValueTracking::computeKnownFPClass(Register R,
780 const APInt &DemandedElts,
781 FPClassTest InterestedClasses,
782 KnownFPClass &Known,
783 unsigned Depth) {
784 assert(Known.isUnknown() && "should not be called with known information");
785
786 if (!DemandedElts) {
787 // No demanded elts, better to assume we don't know anything.
788 Known.resetAll();
789 return;
790 }
791
792 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
793
794 MachineInstr &MI = *MRI.getVRegDef(R);
795 unsigned Opcode = MI.getOpcode();
796 LLT DstTy = MRI.getType(R);
797
798 if (!DstTy.isValid()) {
799 Known.resetAll();
800 return;
801 }
802
803 if (auto Cst = GFConstant::getConstant(R, MRI)) {
804 switch (Cst->getKind()) {
806 auto APF = Cst->getScalarValue();
807 Known.KnownFPClasses = APF.classify();
808 Known.SignBit = APF.isNegative();
809 break;
810 }
812 Known.KnownFPClasses = fcNone;
813 bool SignBitAllZero = true;
814 bool SignBitAllOne = true;
815
816 for (auto C : *Cst) {
817 Known.KnownFPClasses |= C.classify();
818 if (C.isNegative())
819 SignBitAllZero = false;
820 else
821 SignBitAllOne = false;
822 }
823
824 if (SignBitAllOne != SignBitAllZero)
825 Known.SignBit = SignBitAllOne;
826
827 break;
828 }
830 Known.resetAll();
831 break;
832 }
833 }
834
835 return;
836 }
837
838 FPClassTest KnownNotFromFlags = fcNone;
840 KnownNotFromFlags |= fcNan;
842 KnownNotFromFlags |= fcInf;
843
844 // We no longer need to find out about these bits from inputs if we can
845 // assume this from flags/attributes.
846 InterestedClasses &= ~KnownNotFromFlags;
847
848 auto ClearClassesFromFlags =
849 make_scope_exit([=, &Known] { Known.knownNot(KnownNotFromFlags); });
850
851 // All recursive calls that increase depth must come after this.
853 return;
854
855 const MachineFunction *MF = MI.getMF();
856
857 switch (Opcode) {
858 default:
859 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
860 Depth);
861 break;
862 case TargetOpcode::G_FNEG: {
863 Register Val = MI.getOperand(1).getReg();
864 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
865 Known.fneg();
866 break;
867 }
868 case TargetOpcode::G_SELECT: {
869 GSelect &SelMI = cast<GSelect>(MI);
870 Register Cond = SelMI.getCondReg();
871 Register LHS = SelMI.getTrueReg();
872 Register RHS = SelMI.getFalseReg();
873
874 FPClassTest FilterLHS = fcAllFlags;
875 FPClassTest FilterRHS = fcAllFlags;
876
877 Register TestedValue;
878 FPClassTest MaskIfTrue = fcAllFlags;
879 FPClassTest MaskIfFalse = fcAllFlags;
880 FPClassTest ClassVal = fcNone;
881
883 Register CmpLHS, CmpRHS;
884 if (mi_match(Cond, MRI,
885 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
886 // If the select filters out a value based on the class, it no longer
887 // participates in the class of the result
888
889 // TODO: In some degenerate cases we can infer something if we try again
890 // without looking through sign operations.
891 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
892 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
893 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
894 } else if (mi_match(
895 Cond, MRI,
896 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
897 FPClassTest TestedMask = ClassVal;
898 MaskIfTrue = TestedMask;
899 MaskIfFalse = ~TestedMask;
900 }
901
902 if (TestedValue == LHS) {
903 // match !isnan(x) ? x : y
904 FilterLHS = MaskIfTrue;
905 } else if (TestedValue == RHS) { // && IsExactClass
906 // match !isnan(x) ? y : x
907 FilterRHS = MaskIfFalse;
908 }
909
910 KnownFPClass Known2;
911 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
912 Depth + 1);
913 Known.KnownFPClasses &= FilterLHS;
914
915 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
916 Known2, Depth + 1);
917 Known2.KnownFPClasses &= FilterRHS;
918
919 Known |= Known2;
920 break;
921 }
922 case TargetOpcode::G_FCOPYSIGN: {
923 Register Magnitude = MI.getOperand(1).getReg();
924 Register Sign = MI.getOperand(2).getReg();
925
926 KnownFPClass KnownSign;
927
928 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
929 Depth + 1);
930 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
931 Depth + 1);
932 Known.copysign(KnownSign);
933 break;
934 }
935 case TargetOpcode::G_FMA:
936 case TargetOpcode::G_STRICT_FMA:
937 case TargetOpcode::G_FMAD: {
938 if ((InterestedClasses & fcNegative) == fcNone)
939 break;
940
941 Register A = MI.getOperand(1).getReg();
942 Register B = MI.getOperand(2).getReg();
943 Register C = MI.getOperand(3).getReg();
944
945 if (A != B)
946 break;
947
948 // The multiply cannot be -0 and therefore the add can't be -0
949 Known.knownNot(fcNegZero);
950
951 // x * x + y is non-negative if y is non-negative.
952 KnownFPClass KnownAddend;
953 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
954 Depth + 1);
955
956 if (KnownAddend.cannotBeOrderedLessThanZero())
957 Known.knownNot(fcNegative);
958 break;
959 }
960 case TargetOpcode::G_FSQRT:
961 case TargetOpcode::G_STRICT_FSQRT: {
962 KnownFPClass KnownSrc;
963 FPClassTest InterestedSrcs = InterestedClasses;
964 if (InterestedClasses & fcNan)
966
967 Register Val = MI.getOperand(1).getReg();
968
969 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
970
971 if (KnownSrc.isKnownNeverPosInfinity())
972 Known.knownNot(fcPosInf);
973 if (KnownSrc.isKnownNever(fcSNan))
974 Known.knownNot(fcSNan);
975
976 // Any negative value besides -0 returns a nan.
977 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
978 Known.knownNot(fcNan);
979
980 // The only negative value that can be returned is -0 for -0 inputs.
982 break;
983 }
984 case TargetOpcode::G_FABS: {
985 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
986 Register Val = MI.getOperand(1).getReg();
987 // If we only care about the sign bit we don't need to inspect the
988 // operand.
989 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
990 Depth + 1);
991 }
992 Known.fabs();
993 break;
994 }
995 case TargetOpcode::G_FSIN:
996 case TargetOpcode::G_FCOS:
997 case TargetOpcode::G_FSINCOS: {
998 // Return NaN on infinite inputs.
999 Register Val = MI.getOperand(1).getReg();
1000 KnownFPClass KnownSrc;
1001
1002 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1003 Depth + 1);
1004 Known.knownNot(fcInf);
1005
1006 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
1007 Known.knownNot(fcNan);
1008 break;
1009 }
1010 case TargetOpcode::G_FMAXNUM:
1011 case TargetOpcode::G_FMINNUM:
1012 case TargetOpcode::G_FMINNUM_IEEE:
1013 case TargetOpcode::G_FMAXIMUM:
1014 case TargetOpcode::G_FMINIMUM:
1015 case TargetOpcode::G_FMAXNUM_IEEE:
1016 case TargetOpcode::G_FMAXIMUMNUM:
1017 case TargetOpcode::G_FMINIMUMNUM: {
1018 Register LHS = MI.getOperand(1).getReg();
1019 Register RHS = MI.getOperand(2).getReg();
1020 KnownFPClass KnownLHS, KnownRHS;
1021
1022 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
1023 Depth + 1);
1024 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
1025 Depth + 1);
1026
1027 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
1028 Known = KnownLHS | KnownRHS;
1029
1030 // If either operand is not NaN, the result is not NaN.
1031 if (NeverNaN && (Opcode == TargetOpcode::G_FMINNUM ||
1032 Opcode == TargetOpcode::G_FMAXNUM ||
1033 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1034 Opcode == TargetOpcode::G_FMAXIMUMNUM))
1035 Known.knownNot(fcNan);
1036
1037 if (Opcode == TargetOpcode::G_FMAXNUM ||
1038 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1039 Opcode == TargetOpcode::G_FMAXNUM_IEEE) {
1040 // If at least one operand is known to be positive, the result must be
1041 // positive.
1042 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
1043 KnownLHS.isKnownNeverNaN()) ||
1044 (KnownRHS.cannotBeOrderedLessThanZero() &&
1045 KnownRHS.isKnownNeverNaN()))
1047 } else if (Opcode == TargetOpcode::G_FMAXIMUM) {
1048 // If at least one operand is known to be positive, the result must be
1049 // positive.
1050 if (KnownLHS.cannotBeOrderedLessThanZero() ||
1051 KnownRHS.cannotBeOrderedLessThanZero())
1053 } else if (Opcode == TargetOpcode::G_FMINNUM ||
1054 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1055 Opcode == TargetOpcode::G_FMINNUM_IEEE) {
1056 // If at least one operand is known to be negative, the result must be
1057 // negative.
1058 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
1059 KnownLHS.isKnownNeverNaN()) ||
1060 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
1061 KnownRHS.isKnownNeverNaN()))
1063 } else if (Opcode == TargetOpcode::G_FMINIMUM) {
1064 // If at least one operand is known to be negative, the result must be
1065 // negative.
1066 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
1069 } else {
1070 llvm_unreachable("unhandled intrinsic");
1071 }
1072
1073 // Fixup zero handling if denormals could be returned as a zero.
1074 //
1075 // As there's no spec for denormal flushing, be conservative with the
1076 // treatment of denormals that could be flushed to zero. For older
1077 // subtargets on AMDGPU the min/max instructions would not flush the
1078 // output and return the original value.
1079 //
1080 if ((Known.KnownFPClasses & fcZero) != fcNone &&
1081 !Known.isKnownNeverSubnormal()) {
1082 DenormalMode Mode =
1083 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1084 if (Mode != DenormalMode::getIEEE())
1085 Known.KnownFPClasses |= fcZero;
1086 }
1087
1088 if (Known.isKnownNeverNaN()) {
1089 if (KnownLHS.SignBit && KnownRHS.SignBit &&
1090 *KnownLHS.SignBit == *KnownRHS.SignBit) {
1091 if (*KnownLHS.SignBit)
1092 Known.signBitMustBeOne();
1093 else
1094 Known.signBitMustBeZero();
1095 } else if ((Opcode == TargetOpcode::G_FMAXIMUM ||
1096 Opcode == TargetOpcode::G_FMINIMUM) ||
1097 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1098 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1099 Opcode == TargetOpcode::G_FMAXNUM_IEEE ||
1100 Opcode == TargetOpcode::G_FMINNUM_IEEE ||
1101 // FIXME: Should be using logical zero versions
1102 ((KnownLHS.isKnownNeverNegZero() ||
1103 KnownRHS.isKnownNeverPosZero()) &&
1104 (KnownLHS.isKnownNeverPosZero() ||
1105 KnownRHS.isKnownNeverNegZero()))) {
1106 if ((Opcode == TargetOpcode::G_FMAXIMUM ||
1107 Opcode == TargetOpcode::G_FMAXNUM ||
1108 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1109 Opcode == TargetOpcode::G_FMAXNUM_IEEE) &&
1110 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
1111 Known.signBitMustBeZero();
1112 else if ((Opcode == TargetOpcode::G_FMINIMUM ||
1113 Opcode == TargetOpcode::G_FMINNUM ||
1114 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1115 Opcode == TargetOpcode::G_FMINNUM_IEEE) &&
1116 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
1117 Known.signBitMustBeOne();
1118 }
1119 }
1120 break;
1121 }
1122 case TargetOpcode::G_FCANONICALIZE: {
1123 Register Val = MI.getOperand(1).getReg();
1124 KnownFPClass KnownSrc;
1125 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1126 Depth + 1);
1127
1128 // This is essentially a stronger form of
1129 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
1130 // actually have an IR canonicalization guarantee.
1131
1132 // Canonicalize may flush denormals to zero, so we have to consider the
1133 // denormal mode to preserve known-not-0 knowledge.
1134 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
1135
1136 // Stronger version of propagateNaN
1137 // Canonicalize is guaranteed to quiet signaling nans.
1138 if (KnownSrc.isKnownNeverNaN())
1139 Known.knownNot(fcNan);
1140 else
1141 Known.knownNot(fcSNan);
1142
1143 // If the parent function flushes denormals, the canonical output cannot
1144 // be a denormal.
1145 LLT Ty = MRI.getType(Val).getScalarType();
1146 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1147 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1148 if (DenormMode == DenormalMode::getIEEE()) {
1149 if (KnownSrc.isKnownNever(fcPosZero))
1150 Known.knownNot(fcPosZero);
1151 if (KnownSrc.isKnownNever(fcNegZero))
1152 Known.knownNot(fcNegZero);
1153 break;
1154 }
1155
1156 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
1157 Known.knownNot(fcSubnormal);
1158
1159 if (DenormMode.Input == DenormalMode::PositiveZero ||
1160 (DenormMode.Output == DenormalMode::PositiveZero &&
1161 DenormMode.Input == DenormalMode::IEEE))
1162 Known.knownNot(fcNegZero);
1163
1164 break;
1165 }
1166 case TargetOpcode::G_VECREDUCE_FMAX:
1167 case TargetOpcode::G_VECREDUCE_FMIN:
1168 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1169 case TargetOpcode::G_VECREDUCE_FMINIMUM: {
1170 Register Val = MI.getOperand(1).getReg();
1171 // reduce min/max will choose an element from one of the vector elements,
1172 // so we can infer and class information that is common to all elements.
1173
1174 Known =
1175 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1176 // Can only propagate sign if output is never NaN.
1177 if (!Known.isKnownNeverNaN())
1178 Known.SignBit.reset();
1179 break;
1180 }
1181 case TargetOpcode::G_TRUNC:
1182 case TargetOpcode::G_FFLOOR:
1183 case TargetOpcode::G_FCEIL:
1184 case TargetOpcode::G_FRINT:
1185 case TargetOpcode::G_FNEARBYINT:
1186 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1187 case TargetOpcode::G_INTRINSIC_ROUND: {
1188 Register Val = MI.getOperand(1).getReg();
1189 KnownFPClass KnownSrc;
1190 FPClassTest InterestedSrcs = InterestedClasses;
1191 if (InterestedSrcs & fcPosFinite)
1192 InterestedSrcs |= fcPosFinite;
1193 if (InterestedSrcs & fcNegFinite)
1194 InterestedSrcs |= fcNegFinite;
1195 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1196
1197 // Integer results cannot be subnormal.
1198 Known.knownNot(fcSubnormal);
1199
1200 Known.propagateNaN(KnownSrc, true);
1201
1202 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1203
1204 // Negative round ups to 0 produce -0
1205 if (KnownSrc.isKnownNever(fcPosFinite))
1206 Known.knownNot(fcPosFinite);
1207 if (KnownSrc.isKnownNever(fcNegFinite))
1208 Known.knownNot(fcNegFinite);
1209
1210 break;
1211 }
1212 case TargetOpcode::G_FEXP:
1213 case TargetOpcode::G_FEXP2:
1214 case TargetOpcode::G_FEXP10: {
1215 Known.knownNot(fcNegative);
1216 if ((InterestedClasses & fcNan) == fcNone)
1217 break;
1218
1219 Register Val = MI.getOperand(1).getReg();
1220 KnownFPClass KnownSrc;
1221 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1222 Depth + 1);
1223 if (KnownSrc.isKnownNeverNaN()) {
1224 Known.knownNot(fcNan);
1225 Known.signBitMustBeZero();
1226 }
1227
1228 break;
1229 }
1230 case TargetOpcode::G_FLOG:
1231 case TargetOpcode::G_FLOG2:
1232 case TargetOpcode::G_FLOG10: {
1233 // log(+inf) -> +inf
1234 // log([+-]0.0) -> -inf
1235 // log(-inf) -> nan
1236 // log(-x) -> nan
1237 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
1238 break;
1239
1240 FPClassTest InterestedSrcs = InterestedClasses;
1241 if ((InterestedClasses & fcNegInf) != fcNone)
1242 InterestedSrcs |= fcZero | fcSubnormal;
1243 if ((InterestedClasses & fcNan) != fcNone)
1244 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
1245
1246 Register Val = MI.getOperand(1).getReg();
1247 KnownFPClass KnownSrc;
1248 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1249
1250 if (KnownSrc.isKnownNeverPosInfinity())
1251 Known.knownNot(fcPosInf);
1252
1253 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
1254 Known.knownNot(fcNan);
1255
1256 LLT Ty = MRI.getType(Val).getScalarType();
1257 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1258 DenormalMode Mode = MF->getDenormalMode(FltSem);
1259
1260 if (KnownSrc.isKnownNeverLogicalZero(Mode))
1261 Known.knownNot(fcNegInf);
1262
1263 break;
1264 }
1265 case TargetOpcode::G_FPOWI: {
1266 if ((InterestedClasses & fcNegative) == fcNone)
1267 break;
1268
1269 Register Exp = MI.getOperand(2).getReg();
1270 LLT ExpTy = MRI.getType(Exp);
1271 KnownBits ExponentKnownBits = getKnownBits(
1272 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1273
1274 if (ExponentKnownBits.Zero[0]) { // Is even
1275 Known.knownNot(fcNegative);
1276 break;
1277 }
1278
1279 // Given that exp is an integer, here are the
1280 // ways that pow can return a negative value:
1281 //
1282 // pow(-x, exp) --> negative if exp is odd and x is negative.
1283 // pow(-0, exp) --> -inf if exp is negative odd.
1284 // pow(-0, exp) --> -0 if exp is positive odd.
1285 // pow(-inf, exp) --> -0 if exp is negative odd.
1286 // pow(-inf, exp) --> -inf if exp is positive odd.
1287 Register Val = MI.getOperand(1).getReg();
1288 KnownFPClass KnownSrc;
1289 computeKnownFPClass(Val, DemandedElts, fcNegative, KnownSrc, Depth + 1);
1290 if (KnownSrc.isKnownNever(fcNegative))
1291 Known.knownNot(fcNegative);
1292 break;
1293 }
1294 case TargetOpcode::G_FLDEXP:
1295 case TargetOpcode::G_STRICT_FLDEXP: {
1296 Register Val = MI.getOperand(1).getReg();
1297 KnownFPClass KnownSrc;
1298 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1299 Depth + 1);
1300 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
1301
1302 // Sign is preserved, but underflows may produce zeroes.
1303 if (KnownSrc.isKnownNever(fcNegative))
1304 Known.knownNot(fcNegative);
1305 else if (KnownSrc.cannotBeOrderedLessThanZero())
1307
1308 if (KnownSrc.isKnownNever(fcPositive))
1309 Known.knownNot(fcPositive);
1310 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
1312
1313 // Can refine inf/zero handling based on the exponent operand.
1314 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1315 if ((InterestedClasses & ExpInfoMask) == fcNone)
1316 break;
1317 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
1318 break;
1319
1320 // TODO: Handle constant range of Exp
1321
1322 break;
1323 }
1324 case TargetOpcode::G_INTRINSIC_ROUNDEVEN: {
1325 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1326 Depth);
1327 break;
1328 }
1329 case TargetOpcode::G_FADD:
1330 case TargetOpcode::G_STRICT_FADD:
1331 case TargetOpcode::G_FSUB:
1332 case TargetOpcode::G_STRICT_FSUB: {
1333 Register LHS = MI.getOperand(1).getReg();
1334 Register RHS = MI.getOperand(2).getReg();
1335 KnownFPClass KnownLHS, KnownRHS;
1336 bool WantNegative =
1337 (Opcode == TargetOpcode::G_FADD ||
1338 Opcode == TargetOpcode::G_STRICT_FADD) &&
1339 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1340 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1341 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1342
1343 if (!WantNaN && !WantNegative && !WantNegZero)
1344 break;
1345
1346 FPClassTest InterestedSrcs = InterestedClasses;
1347 if (WantNegative)
1348 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1349 if (InterestedClasses & fcNan)
1350 InterestedSrcs |= fcInf;
1351 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1352
1353 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1354 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1355 WantNegZero ||
1356 (Opcode == TargetOpcode::G_FSUB ||
1357 Opcode == TargetOpcode::G_STRICT_FSUB)) {
1358
1359 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1360 // there's no point.
1361 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1362 Depth + 1);
1363 // Adding positive and negative infinity produces NaN.
1364 // TODO: Check sign of infinities.
1365 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1366 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
1367 Known.knownNot(fcNan);
1368
1369 if (Opcode == Instruction::FAdd) {
1370 if (KnownLHS.cannotBeOrderedLessThanZero() &&
1371 KnownRHS.cannotBeOrderedLessThanZero())
1373
1374 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
1375 if ((KnownLHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1377 KnownRHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1378 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1379 // Make sure output negative denormal can't flush to -0
1381 Known.knownNot(fcNegZero);
1382 } else {
1383 // Only fsub -0, +0 can return -0
1384 if ((KnownLHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1386 KnownRHS.isKnownNeverLogicalPosZero(MF->getDenormalMode(
1387 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1388 // Make sure output negative denormal can't flush to -0
1390 Known.knownNot(fcNegZero);
1391 }
1392 }
1393
1394 break;
1395 }
1396 case TargetOpcode::G_FMUL:
1397 case TargetOpcode::G_STRICT_FMUL: {
1398 Register LHS = MI.getOperand(1).getReg();
1399 Register RHS = MI.getOperand(2).getReg();
1400 // X * X is always non-negative or a NaN.
1401 if (LHS == RHS)
1402 Known.knownNot(fcNegative);
1403
1404 if ((InterestedClasses & fcNan) != fcNan)
1405 break;
1406
1407 // fcSubnormal is only needed in case of DAZ.
1408 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
1409
1410 KnownFPClass KnownLHS, KnownRHS;
1411 computeKnownFPClass(RHS, DemandedElts, NeedForNan, KnownRHS, Depth + 1);
1412 if (!KnownRHS.isKnownNeverNaN())
1413 break;
1414
1415 computeKnownFPClass(LHS, DemandedElts, NeedForNan, KnownLHS, Depth + 1);
1416 if (!KnownLHS.isKnownNeverNaN())
1417 break;
1418
1419 if (KnownLHS.SignBit && KnownRHS.SignBit) {
1420 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
1421 Known.signBitMustBeZero();
1422 else
1423 Known.signBitMustBeOne();
1424 }
1425
1426 // If 0 * +/-inf produces NaN.
1427 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
1428 Known.knownNot(fcNan);
1429 break;
1430 }
1431
1432 if ((KnownRHS.isKnownNeverInfinity() ||
1433 KnownLHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1434 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1435 (KnownLHS.isKnownNeverInfinity() ||
1436 KnownRHS.isKnownNeverLogicalZero(
1437 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType())))))
1438 Known.knownNot(fcNan);
1439
1440 break;
1441 }
1442 case TargetOpcode::G_FDIV:
1443 case TargetOpcode::G_FREM: {
1444 Register LHS = MI.getOperand(1).getReg();
1445 Register RHS = MI.getOperand(2).getReg();
1446
1447 if (LHS == RHS) {
1448 // TODO: Could filter out snan if we inspect the operand
1449 if (Opcode == TargetOpcode::G_FDIV) {
1450 // X / X is always exactly 1.0 or a NaN.
1452 } else {
1453 // X % X is always exactly [+-]0.0 or a NaN.
1454 Known.KnownFPClasses = fcNan | fcZero;
1455 }
1456
1457 break;
1458 }
1459
1460 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1461 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1462 const bool WantPositive = Opcode == TargetOpcode::G_FREM &&
1463 (InterestedClasses & fcPositive) != fcNone;
1464 if (!WantNan && !WantNegative && !WantPositive)
1465 break;
1466
1467 KnownFPClass KnownLHS, KnownRHS;
1468
1469 computeKnownFPClass(RHS, DemandedElts, fcNan | fcInf | fcZero | fcNegative,
1470 KnownRHS, Depth + 1);
1471
1472 bool KnowSomethingUseful =
1473 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
1474
1475 if (KnowSomethingUseful || WantPositive) {
1476 const FPClassTest InterestedLHS =
1477 WantPositive ? fcAllFlags
1479
1480 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & InterestedLHS,
1481 KnownLHS, Depth + 1);
1482 }
1483
1484 if (Opcode == Instruction::FDiv) {
1485 // Only 0/0, Inf/Inf produce NaN.
1486 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1487 (KnownLHS.isKnownNeverInfinity() ||
1488 KnownRHS.isKnownNeverInfinity()) &&
1489 ((KnownLHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1490 getFltSemanticForLLT(DstTy.getScalarType())))) ||
1491 (KnownRHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1492 getFltSemanticForLLT(DstTy.getScalarType())))))) {
1493 Known.knownNot(fcNan);
1494 }
1495
1496 // X / -0.0 is -Inf (or NaN).
1497 // +X / +X is +X
1498 if (KnownLHS.isKnownNever(fcNegative) &&
1499 KnownRHS.isKnownNever(fcNegative))
1500 Known.knownNot(fcNegative);
1501 } else {
1502 // Inf REM x and x REM 0 produce NaN.
1503 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1504 KnownLHS.isKnownNeverInfinity() &&
1505 KnownRHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1507 Known.knownNot(fcNan);
1508 }
1509
1510 // The sign for frem is the same as the first operand.
1511 if (KnownLHS.cannotBeOrderedLessThanZero())
1513 if (KnownLHS.cannotBeOrderedGreaterThanZero())
1515
1516 // See if we can be more aggressive about the sign of 0.
1517 if (KnownLHS.isKnownNever(fcNegative))
1518 Known.knownNot(fcNegative);
1519 if (KnownLHS.isKnownNever(fcPositive))
1520 Known.knownNot(fcPositive);
1521 }
1522
1523 break;
1524 }
1525 case TargetOpcode::G_FPEXT: {
1526 Register Dst = MI.getOperand(0).getReg();
1527 Register Src = MI.getOperand(1).getReg();
1528 // Infinity, nan and zero propagate from source.
1529 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth + 1);
1530
1531 LLT DstTy = MRI.getType(Dst).getScalarType();
1532 const fltSemantics &DstSem = getFltSemanticForLLT(DstTy);
1533 LLT SrcTy = MRI.getType(Src).getScalarType();
1534 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
1535
1536 // All subnormal inputs should be in the normal range in the result type.
1537 if (APFloat::isRepresentableAsNormalIn(SrcSem, DstSem)) {
1538 if (Known.KnownFPClasses & fcPosSubnormal)
1539 Known.KnownFPClasses |= fcPosNormal;
1540 if (Known.KnownFPClasses & fcNegSubnormal)
1541 Known.KnownFPClasses |= fcNegNormal;
1542 Known.knownNot(fcSubnormal);
1543 }
1544
1545 // Sign bit of a nan isn't guaranteed.
1546 if (!Known.isKnownNeverNaN())
1547 Known.SignBit = std::nullopt;
1548 break;
1549 }
1550 case TargetOpcode::G_FPTRUNC: {
1551 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1552 Depth);
1553 break;
1554 }
1555 case TargetOpcode::G_SITOFP:
1556 case TargetOpcode::G_UITOFP: {
1557 // Cannot produce nan
1558 Known.knownNot(fcNan);
1559
1560 // Integers cannot be subnormal
1561 Known.knownNot(fcSubnormal);
1562
1563 // sitofp and uitofp turn into +0.0 for zero.
1564 Known.knownNot(fcNegZero);
1565 if (Opcode == TargetOpcode::G_UITOFP)
1566 Known.signBitMustBeZero();
1567
1568 Register Val = MI.getOperand(1).getReg();
1569 LLT Ty = MRI.getType(Val);
1570
1571 if (InterestedClasses & fcInf) {
1572 // Get width of largest magnitude integer (remove a bit if signed).
1573 // This still works for a signed minimum value because the largest FP
1574 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).;
1575 int IntSize = Ty.getScalarSizeInBits();
1576 if (Opcode == TargetOpcode::G_SITOFP)
1577 --IntSize;
1578
1579 // If the exponent of the largest finite FP value can hold the largest
1580 // integer, the result of the cast must be finite.
1581 LLT FPTy = DstTy.getScalarType();
1582 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
1583 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
1584 Known.knownNot(fcInf);
1585 }
1586
1587 break;
1588 }
1589 // case TargetOpcode::G_MERGE_VALUES:
1590 case TargetOpcode::G_BUILD_VECTOR:
1591 case TargetOpcode::G_CONCAT_VECTORS: {
1592 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
1593
1594 if (!DstTy.isFixedVector())
1595 break;
1596
1597 bool First = true;
1598 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
1599 // We know the index we are inserting to, so clear it from Vec check.
1600 bool NeedsElt = DemandedElts[Idx];
1601
1602 // Do we demand the inserted element?
1603 if (NeedsElt) {
1604 Register Src = Merge.getSourceReg(Idx);
1605 if (First) {
1606 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
1607 First = false;
1608 } else {
1609 KnownFPClass Known2;
1610 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
1611 Known |= Known2;
1612 }
1613
1614 // If we don't know any bits, early out.
1615 if (Known.isUnknown())
1616 break;
1617 }
1618 }
1619
1620 break;
1621 }
1622 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1623 // Look through extract element. If the index is non-constant or
1624 // out-of-range demand all elements, otherwise just the extracted
1625 // element.
1626 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1627 Register Vec = Extract.getVectorReg();
1628 Register Idx = Extract.getIndexReg();
1629
1630 auto CIdx = getIConstantVRegVal(Idx, MRI);
1631
1632 LLT VecTy = MRI.getType(Vec);
1633
1634 if (VecTy.isFixedVector()) {
1635 unsigned NumElts = VecTy.getNumElements();
1636 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1637 if (CIdx && CIdx->ult(NumElts))
1638 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1639 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
1640 Depth + 1);
1641 }
1642
1643 break;
1644 }
1645 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1646 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
1647 Register Vec = Insert.getVectorReg();
1648 Register Elt = Insert.getElementReg();
1649 Register Idx = Insert.getIndexReg();
1650
1651 LLT VecTy = MRI.getType(Vec);
1652
1653 if (VecTy.isScalableVector())
1654 return;
1655
1656 auto CIdx = getIConstantVRegVal(Idx, MRI);
1657
1658 unsigned NumElts = DemandedElts.getBitWidth();
1659 APInt DemandedVecElts = DemandedElts;
1660 bool NeedsElt = true;
1661 // If we know the index we are inserting to, clear it from Vec check.
1662 if (CIdx && CIdx->ult(NumElts)) {
1663 DemandedVecElts.clearBit(CIdx->getZExtValue());
1664 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1665 }
1666
1667 // Do we demand the inserted element?
1668 if (NeedsElt) {
1669 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
1670 // If we don't know any bits, early out.
1671 if (Known.isUnknown())
1672 break;
1673 } else {
1674 Known.KnownFPClasses = fcNone;
1675 }
1676
1677 // Do we need anymore elements from Vec?
1678 if (!DemandedVecElts.isZero()) {
1679 KnownFPClass Known2;
1680 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
1681 Depth + 1);
1682 Known |= Known2;
1683 }
1684
1685 break;
1686 }
1687 case TargetOpcode::G_SHUFFLE_VECTOR: {
1688 // For undef elements, we don't know anything about the common state of
1689 // the shuffle result.
1690 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
1691 APInt DemandedLHS, DemandedRHS;
1692 if (DstTy.isScalableVector()) {
1693 assert(DemandedElts == APInt(1, 1));
1694 DemandedLHS = DemandedRHS = DemandedElts;
1695 } else {
1697 DemandedElts, DemandedLHS,
1698 DemandedRHS)) {
1699 Known.resetAll();
1700 return;
1701 }
1702 }
1703
1704 if (!!DemandedLHS) {
1705 Register LHS = Shuf.getSrc1Reg();
1706 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
1707 Depth + 1);
1708
1709 // If we don't know any bits, early out.
1710 if (Known.isUnknown())
1711 break;
1712 } else {
1713 Known.KnownFPClasses = fcNone;
1714 }
1715
1716 if (!!DemandedRHS) {
1717 KnownFPClass Known2;
1718 Register RHS = Shuf.getSrc2Reg();
1719 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
1720 Depth + 1);
1721 Known |= Known2;
1722 }
1723 break;
1724 }
1725 case TargetOpcode::COPY: {
1726 Register Src = MI.getOperand(1).getReg();
1727
1728 if (!Src.isVirtual())
1729 return;
1730
1731 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
1732 break;
1733 }
1734 }
1735}
1736
1738GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
1739 FPClassTest InterestedClasses,
1740 unsigned Depth) {
1741 KnownFPClass KnownClasses;
1742 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
1743 return KnownClasses;
1744}
1745
1746KnownFPClass GISelValueTracking::computeKnownFPClass(
1747 Register R, FPClassTest InterestedClasses, unsigned Depth) {
1748 KnownFPClass Known;
1749 computeKnownFPClass(R, Known, InterestedClasses, Depth);
1750 return Known;
1751}
1752
1753KnownFPClass GISelValueTracking::computeKnownFPClass(
1754 Register R, const APInt &DemandedElts, uint32_t Flags,
1755 FPClassTest InterestedClasses, unsigned Depth) {
1757 InterestedClasses &= ~fcNan;
1759 InterestedClasses &= ~fcInf;
1760
1761 KnownFPClass Result =
1762 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
1763
1765 Result.KnownFPClasses &= ~fcNan;
1767 Result.KnownFPClasses &= ~fcInf;
1768 return Result;
1769}
1770
1771KnownFPClass GISelValueTracking::computeKnownFPClass(
1772 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
1773 LLT Ty = MRI.getType(R);
1774 APInt DemandedElts =
1775 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1776 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
1777}
1778
1779/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
1780unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
1781 const APInt &DemandedElts,
1782 unsigned Depth) {
1783 // Test src1 first, since we canonicalize simpler expressions to the RHS.
1784 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
1785 if (Src1SignBits == 1)
1786 return 1;
1787 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
1788}
1789
1790/// Compute the known number of sign bits with attached range metadata in the
1791/// memory operand. If this is an extending load, accounts for the behavior of
1792/// the high bits.
1794 unsigned TyBits) {
1795 const MDNode *Ranges = Ld->getRanges();
1796 if (!Ranges)
1797 return 1;
1798
1800 if (TyBits > CR.getBitWidth()) {
1801 switch (Ld->getOpcode()) {
1802 case TargetOpcode::G_SEXTLOAD:
1803 CR = CR.signExtend(TyBits);
1804 break;
1805 case TargetOpcode::G_ZEXTLOAD:
1806 CR = CR.zeroExtend(TyBits);
1807 break;
1808 default:
1809 break;
1810 }
1811 }
1812
1813 return std::min(CR.getSignedMin().getNumSignBits(),
1815}
1816
1818 const APInt &DemandedElts,
1819 unsigned Depth) {
1820 MachineInstr &MI = *MRI.getVRegDef(R);
1821 unsigned Opcode = MI.getOpcode();
1822
1823 if (Opcode == TargetOpcode::G_CONSTANT)
1824 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
1825
1826 if (Depth == getMaxDepth())
1827 return 1;
1828
1829 if (!DemandedElts)
1830 return 1; // No demanded elts, better to assume we don't know anything.
1831
1832 LLT DstTy = MRI.getType(R);
1833 const unsigned TyBits = DstTy.getScalarSizeInBits();
1834
1835 // Handle the case where this is called on a register that does not have a
1836 // type constraint. This is unlikely to occur except by looking through copies
1837 // but it is possible for the initial register being queried to be in this
1838 // state.
1839 if (!DstTy.isValid())
1840 return 1;
1841
1842 unsigned FirstAnswer = 1;
1843 switch (Opcode) {
1844 case TargetOpcode::COPY: {
1845 MachineOperand &Src = MI.getOperand(1);
1846 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
1847 MRI.getType(Src.getReg()).isValid()) {
1848 // Don't increment Depth for this one since we didn't do any work.
1849 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
1850 }
1851
1852 return 1;
1853 }
1854 case TargetOpcode::G_SEXT: {
1855 Register Src = MI.getOperand(1).getReg();
1856 LLT SrcTy = MRI.getType(Src);
1857 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
1858 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
1859 }
1860 case TargetOpcode::G_ASSERT_SEXT:
1861 case TargetOpcode::G_SEXT_INREG: {
1862 // Max of the input and what this extends.
1863 Register Src = MI.getOperand(1).getReg();
1864 unsigned SrcBits = MI.getOperand(2).getImm();
1865 unsigned InRegBits = TyBits - SrcBits + 1;
1866 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
1867 InRegBits);
1868 }
1869 case TargetOpcode::G_LOAD: {
1870 GLoad *Ld = cast<GLoad>(&MI);
1871 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
1872 break;
1873
1874 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1875 }
1876 case TargetOpcode::G_SEXTLOAD: {
1878
1879 // FIXME: We need an in-memory type representation.
1880 if (DstTy.isVector())
1881 return 1;
1882
1883 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1884 if (NumBits != 1)
1885 return NumBits;
1886
1887 // e.g. i16->i32 = '17' bits known.
1888 const MachineMemOperand *MMO = *MI.memoperands_begin();
1889 return TyBits - MMO->getSizeInBits().getValue() + 1;
1890 }
1891 case TargetOpcode::G_ZEXTLOAD: {
1893
1894 // FIXME: We need an in-memory type representation.
1895 if (DstTy.isVector())
1896 return 1;
1897
1898 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1899 if (NumBits != 1)
1900 return NumBits;
1901
1902 // e.g. i16->i32 = '16' bits known.
1903 const MachineMemOperand *MMO = *MI.memoperands_begin();
1904 return TyBits - MMO->getSizeInBits().getValue();
1905 }
1906 case TargetOpcode::G_AND:
1907 case TargetOpcode::G_OR:
1908 case TargetOpcode::G_XOR: {
1909 Register Src1 = MI.getOperand(1).getReg();
1910 unsigned Src1NumSignBits =
1911 computeNumSignBits(Src1, DemandedElts, Depth + 1);
1912 if (Src1NumSignBits != 1) {
1913 Register Src2 = MI.getOperand(2).getReg();
1914 unsigned Src2NumSignBits =
1915 computeNumSignBits(Src2, DemandedElts, Depth + 1);
1916 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
1917 }
1918 break;
1919 }
1920 case TargetOpcode::G_ASHR: {
1921 Register Src1 = MI.getOperand(1).getReg();
1922 Register Src2 = MI.getOperand(2).getReg();
1923 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
1924 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
1925 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
1926 break;
1927 }
1928 case TargetOpcode::G_SHL: {
1929 Register Src1 = MI.getOperand(1).getReg();
1930 Register Src2 = MI.getOperand(2).getReg();
1931 if (std::optional<ConstantRange> ShAmtRange =
1932 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
1933 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
1934 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
1935
1936 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
1937 unsigned ExtOpc = ExtMI.getOpcode();
1938
1939 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
1940 // shifted out, then we can compute the number of sign bits for the
1941 // operand being extended. A future improvement could be to pass along the
1942 // "shifted left by" information in the recursive calls to
1943 // ComputeKnownSignBits. Allowing us to handle this more generically.
1944 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
1945 ExtOpc == TargetOpcode::G_ANYEXT) {
1946 LLT ExtTy = MRI.getType(Src1);
1947 Register Extendee = ExtMI.getOperand(1).getReg();
1948 LLT ExtendeeTy = MRI.getType(Extendee);
1949 uint64_t SizeDiff =
1950 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
1951
1952 if (SizeDiff <= MinShAmt) {
1953 unsigned Tmp =
1954 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
1955 if (MaxShAmt < Tmp)
1956 return Tmp - MaxShAmt;
1957 }
1958 }
1959 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
1960 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
1961 if (MaxShAmt < Tmp)
1962 return Tmp - MaxShAmt;
1963 }
1964 break;
1965 }
1966 case TargetOpcode::G_TRUNC: {
1967 Register Src = MI.getOperand(1).getReg();
1968 LLT SrcTy = MRI.getType(Src);
1969
1970 // Check if the sign bits of source go down as far as the truncated value.
1971 unsigned DstTyBits = DstTy.getScalarSizeInBits();
1972 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
1973 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
1974 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
1975 return NumSrcSignBits - (NumSrcBits - DstTyBits);
1976 break;
1977 }
1978 case TargetOpcode::G_SELECT: {
1979 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
1980 MI.getOperand(3).getReg(), DemandedElts,
1981 Depth + 1);
1982 }
1983 case TargetOpcode::G_SMIN:
1984 case TargetOpcode::G_SMAX:
1985 case TargetOpcode::G_UMIN:
1986 case TargetOpcode::G_UMAX:
1987 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
1988 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
1989 MI.getOperand(2).getReg(), DemandedElts,
1990 Depth + 1);
1991 case TargetOpcode::G_SADDO:
1992 case TargetOpcode::G_SADDE:
1993 case TargetOpcode::G_UADDO:
1994 case TargetOpcode::G_UADDE:
1995 case TargetOpcode::G_SSUBO:
1996 case TargetOpcode::G_SSUBE:
1997 case TargetOpcode::G_USUBO:
1998 case TargetOpcode::G_USUBE:
1999 case TargetOpcode::G_SMULO:
2000 case TargetOpcode::G_UMULO: {
2001 // If compares returns 0/-1, all bits are sign bits.
2002 // We know that we have an integer-based boolean since these operations
2003 // are only available for integer.
2004 if (MI.getOperand(1).getReg() == R) {
2005 if (TL.getBooleanContents(DstTy.isVector(), false) ==
2007 return TyBits;
2008 }
2009
2010 break;
2011 }
2012 case TargetOpcode::G_SUB: {
2013 Register Src2 = MI.getOperand(2).getReg();
2014 unsigned Src2NumSignBits =
2015 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2016 if (Src2NumSignBits == 1)
2017 return 1; // Early out.
2018
2019 // Handle NEG.
2020 Register Src1 = MI.getOperand(1).getReg();
2021 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2022 if (Known1.isZero()) {
2023 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2024 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2025 // sign bits set.
2026 if ((Known2.Zero | 1).isAllOnes())
2027 return TyBits;
2028
2029 // If the input is known to be positive (the sign bit is known clear),
2030 // the output of the NEG has, at worst, the same number of sign bits as
2031 // the input.
2032 if (Known2.isNonNegative()) {
2033 FirstAnswer = Src2NumSignBits;
2034 break;
2035 }
2036
2037 // Otherwise, we treat this like a SUB.
2038 }
2039
2040 unsigned Src1NumSignBits =
2041 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2042 if (Src1NumSignBits == 1)
2043 return 1; // Early Out.
2044
2045 // Sub can have at most one carry bit. Thus we know that the output
2046 // is, at worst, one more bit than the inputs.
2047 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2048 break;
2049 }
2050 case TargetOpcode::G_ADD: {
2051 Register Src2 = MI.getOperand(2).getReg();
2052 unsigned Src2NumSignBits =
2053 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2054 if (Src2NumSignBits <= 2)
2055 return 1; // Early out.
2056
2057 Register Src1 = MI.getOperand(1).getReg();
2058 unsigned Src1NumSignBits =
2059 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2060 if (Src1NumSignBits == 1)
2061 return 1; // Early Out.
2062
2063 // Special case decrementing a value (ADD X, -1):
2064 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2065 if (Known2.isAllOnes()) {
2066 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2067 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2068 // sign bits set.
2069 if ((Known1.Zero | 1).isAllOnes())
2070 return TyBits;
2071
2072 // If we are subtracting one from a positive number, there is no carry
2073 // out of the result.
2074 if (Known1.isNonNegative()) {
2075 FirstAnswer = Src1NumSignBits;
2076 break;
2077 }
2078
2079 // Otherwise, we treat this like an ADD.
2080 }
2081
2082 // Add can have at most one carry bit. Thus we know that the output
2083 // is, at worst, one more bit than the inputs.
2084 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2085 break;
2086 }
2087 case TargetOpcode::G_FCMP:
2088 case TargetOpcode::G_ICMP: {
2089 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2090 if (TyBits == 1)
2091 break;
2092 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2094 return TyBits; // All bits are sign bits.
2096 return TyBits - 1; // Every always-zero bit is a sign bit.
2097 break;
2098 }
2099 case TargetOpcode::G_BUILD_VECTOR: {
2100 // Collect the known bits that are shared by every demanded vector element.
2101 FirstAnswer = TyBits;
2102 APInt SingleDemandedElt(1, 1);
2103 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2104 if (!DemandedElts[I])
2105 continue;
2106
2107 unsigned Tmp2 =
2108 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2109 FirstAnswer = std::min(FirstAnswer, Tmp2);
2110
2111 // If we don't know any bits, early out.
2112 if (FirstAnswer == 1)
2113 break;
2114 }
2115 break;
2116 }
2117 case TargetOpcode::G_CONCAT_VECTORS: {
2118 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2119 break;
2120 FirstAnswer = TyBits;
2121 // Determine the minimum number of sign bits across all demanded
2122 // elts of the input vectors. Early out if the result is already 1.
2123 unsigned NumSubVectorElts =
2124 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2125 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2126 APInt DemandedSub =
2127 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2128 if (!DemandedSub)
2129 continue;
2130 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2131
2132 FirstAnswer = std::min(FirstAnswer, Tmp2);
2133
2134 // If we don't know any bits, early out.
2135 if (FirstAnswer == 1)
2136 break;
2137 }
2138 break;
2139 }
2140 case TargetOpcode::G_SHUFFLE_VECTOR: {
2141 // Collect the minimum number of sign bits that are shared by every vector
2142 // element referenced by the shuffle.
2143 APInt DemandedLHS, DemandedRHS;
2144 Register Src1 = MI.getOperand(1).getReg();
2145 unsigned NumElts = MRI.getType(Src1).getNumElements();
2146 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2147 DemandedElts, DemandedLHS, DemandedRHS))
2148 return 1;
2149
2150 if (!!DemandedLHS)
2151 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2152 // If we don't know anything, early out and try computeKnownBits fall-back.
2153 if (FirstAnswer == 1)
2154 break;
2155 if (!!DemandedRHS) {
2156 unsigned Tmp2 =
2157 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2158 FirstAnswer = std::min(FirstAnswer, Tmp2);
2159 }
2160 break;
2161 }
2162 case TargetOpcode::G_SPLAT_VECTOR: {
2163 // Check if the sign bits of source go down as far as the truncated value.
2164 Register Src = MI.getOperand(1).getReg();
2165 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2166 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2167 if (NumSrcSignBits > (NumSrcBits - TyBits))
2168 return NumSrcSignBits - (NumSrcBits - TyBits);
2169 break;
2170 }
2171 case TargetOpcode::G_INTRINSIC:
2172 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2173 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2174 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2175 default: {
2176 unsigned NumBits =
2177 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2178 if (NumBits > 1)
2179 FirstAnswer = std::max(FirstAnswer, NumBits);
2180 break;
2181 }
2182 }
2183
2184 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2185 // use this information.
2186 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2187 APInt Mask;
2188 if (Known.isNonNegative()) { // sign bit is 0
2189 Mask = Known.Zero;
2190 } else if (Known.isNegative()) { // sign bit is 1;
2191 Mask = Known.One;
2192 } else {
2193 // Nothing known.
2194 return FirstAnswer;
2195 }
2196
2197 // Okay, we know that the sign bit in Mask is set. Use CLO to determine
2198 // the number of identical bits in the top of the input value.
2199 Mask <<= Mask.getBitWidth() - TyBits;
2200 return std::max(FirstAnswer, Mask.countl_one());
2201}
2202
2204 LLT Ty = MRI.getType(R);
2205 APInt DemandedElts =
2206 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2207 return computeNumSignBits(R, DemandedElts, Depth);
2208}
2209
2211 Register R, const APInt &DemandedElts, unsigned Depth) {
2212 // Shifting more than the bitwidth is not valid.
2213 MachineInstr &MI = *MRI.getVRegDef(R);
2214 unsigned Opcode = MI.getOpcode();
2215
2216 LLT Ty = MRI.getType(R);
2217 unsigned BitWidth = Ty.getScalarSizeInBits();
2218
2219 if (Opcode == TargetOpcode::G_CONSTANT) {
2220 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2221 if (ShAmt.uge(BitWidth))
2222 return std::nullopt;
2223 return ConstantRange(ShAmt);
2224 }
2225
2226 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2227 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2228 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2229 if (!DemandedElts[I])
2230 continue;
2231 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2232 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2233 MinAmt = MaxAmt = nullptr;
2234 break;
2235 }
2236
2237 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2238 if (ShAmt.uge(BitWidth))
2239 return std::nullopt;
2240 if (!MinAmt || MinAmt->ugt(ShAmt))
2241 MinAmt = &ShAmt;
2242 if (!MaxAmt || MaxAmt->ult(ShAmt))
2243 MaxAmt = &ShAmt;
2244 }
2245 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2246 "Failed to find matching min/max shift amounts");
2247 if (MinAmt && MaxAmt)
2248 return ConstantRange(*MinAmt, *MaxAmt + 1);
2249 }
2250
2251 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2252 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2253 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2254 if (KnownAmt.getMaxValue().ult(BitWidth))
2255 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2256
2257 return std::nullopt;
2258}
2259
2261 Register R, const APInt &DemandedElts, unsigned Depth) {
2262 if (std::optional<ConstantRange> AmtRange =
2263 getValidShiftAmountRange(R, DemandedElts, Depth))
2264 return AmtRange->getUnsignedMin().getZExtValue();
2265 return std::nullopt;
2266}
2267
2273
2278
2280 if (!Info) {
2281 unsigned MaxDepth =
2283 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2284 }
2285 return *Info;
2286}
2287
2288AnalysisKey GISelValueTrackingAnalysis::Key;
2289
2295
2299 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
2300 const auto &MRI = MF.getRegInfo();
2301 OS << "name: ";
2302 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
2303 OS << '\n';
2304
2305 for (MachineBasicBlock &BB : MF) {
2306 for (MachineInstr &MI : BB) {
2307 for (MachineOperand &MO : MI.defs()) {
2308 if (!MO.isReg() || MO.getReg().isPhysical())
2309 continue;
2310 Register Reg = MO.getReg();
2311 if (!MRI.getType(Reg).isValid())
2312 continue;
2313 KnownBits Known = VTA.getKnownBits(Reg);
2314 unsigned SignedBits = VTA.computeNumSignBits(Reg);
2315 OS << " " << MO << " KnownBits:" << Known << " SignBits:" << SignedBits
2316 << '\n';
2317 };
2318 }
2319 }
2320 return PreservedAnalyses::all();
2321}
unsigned const MachineRegisterInfo * MRI
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
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 I(x, y, z)
Definition MD5.cpp:58
Contains matchers for matching SSA Machine Instructions.
Promote Memory to Register
Definition Mem2Reg.cpp:110
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#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")))
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
This file describes how to lower LLVM code to machine code.
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Value * RHS
Value * LHS
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:340
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
Class for arbitrary precision integers.
Definition APInt.h:78
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:1407
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
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:1392
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
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:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1436
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:1320
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
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:1389
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:1222
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 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:2099
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)
KnownBits getKnownBits(MachineInstr &MI)
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
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
constexpr LLT getScalarType() const
TypeSize getValue() const
Metadata node.
Definition Metadata.h:1078
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
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.
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.
#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()
operand_type_match m_Pred()
bind_ty< FPClassTest > m_FPClassTest(FPClassTest &T)
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 std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:294
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
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:2472
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.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1516
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 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
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
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
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:186
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.
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:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
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:66
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
KnownBits reverseBits() const
Definition KnownBits.h:518
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:172
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
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.
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:129
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
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:353
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
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:167
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:83
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 constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
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 ...
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.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
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.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.