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_SHUFFLE_VECTOR: {
647 APInt DemandedLHS, DemandedRHS;
648 // Collect the known bits that are shared by every vector element referenced
649 // by the shuffle.
650 unsigned NumElts = MRI.getType(MI.getOperand(1).getReg()).getNumElements();
651 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
652 DemandedElts, DemandedLHS, DemandedRHS))
653 break;
654
655 // Known bits are the values that are shared by every demanded element.
656 Known.Zero.setAllBits();
657 Known.One.setAllBits();
658 if (!!DemandedLHS) {
659 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedLHS,
660 Depth + 1);
661 Known = Known.intersectWith(Known2);
662 }
663 // If we don't know any bits, early out.
664 if (Known.isUnknown())
665 break;
666 if (!!DemandedRHS) {
667 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedRHS,
668 Depth + 1);
669 Known = Known.intersectWith(Known2);
670 }
671 break;
672 }
673 case TargetOpcode::G_CONCAT_VECTORS: {
674 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
675 break;
676 // Split DemandedElts and test each of the demanded subvectors.
677 Known.Zero.setAllBits();
678 Known.One.setAllBits();
679 unsigned NumSubVectorElts =
680 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
681
682 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
683 APInt DemandedSub =
684 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
685 if (!!DemandedSub) {
686 computeKnownBitsImpl(MO.getReg(), Known2, DemandedSub, Depth + 1);
687
688 Known = Known.intersectWith(Known2);
689 }
690 // If we don't know any bits, early out.
691 if (Known.isUnknown())
692 break;
693 }
694 break;
695 }
696 case TargetOpcode::G_ABS: {
697 Register SrcReg = MI.getOperand(1).getReg();
698 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
699 Known = Known.abs();
700 Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
701 1);
702 break;
703 }
704 }
705
706 LLVM_DEBUG(dumpResult(MI, Known, Depth));
707}
708
710 Ty = Ty.getScalarType();
712 return Mode.Output == DenormalMode::IEEE ||
714}
715
716void GISelValueTracking::computeKnownFPClass(Register R, KnownFPClass &Known,
717 FPClassTest InterestedClasses,
718 unsigned Depth) {
719 LLT Ty = MRI.getType(R);
720 APInt DemandedElts =
721 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
722 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth);
723}
724
725void GISelValueTracking::computeKnownFPClassForFPTrunc(
726 const MachineInstr &MI, const APInt &DemandedElts,
727 FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth) {
728 if ((InterestedClasses & (KnownFPClass::OrderedLessThanZeroMask | fcNan)) ==
729 fcNone)
730 return;
731
732 Register Val = MI.getOperand(1).getReg();
733 KnownFPClass KnownSrc;
734 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
735 Depth + 1);
736
737 // Sign should be preserved
738 // TODO: Handle cannot be ordered greater than zero
739 if (KnownSrc.cannotBeOrderedLessThanZero())
741
742 Known.propagateNaN(KnownSrc, true);
743
744 // Infinity needs a range check.
745}
746
747void GISelValueTracking::computeKnownFPClass(Register R,
748 const APInt &DemandedElts,
749 FPClassTest InterestedClasses,
750 KnownFPClass &Known,
751 unsigned Depth) {
752 assert(Known.isUnknown() && "should not be called with known information");
753
754 if (!DemandedElts) {
755 // No demanded elts, better to assume we don't know anything.
756 Known.resetAll();
757 return;
758 }
759
760 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
761
762 MachineInstr &MI = *MRI.getVRegDef(R);
763 unsigned Opcode = MI.getOpcode();
764 LLT DstTy = MRI.getType(R);
765
766 if (!DstTy.isValid()) {
767 Known.resetAll();
768 return;
769 }
770
771 if (auto Cst = GFConstant::getConstant(R, MRI)) {
772 switch (Cst->getKind()) {
774 auto APF = Cst->getScalarValue();
775 Known.KnownFPClasses = APF.classify();
776 Known.SignBit = APF.isNegative();
777 break;
778 }
780 Known.KnownFPClasses = fcNone;
781 bool SignBitAllZero = true;
782 bool SignBitAllOne = true;
783
784 for (auto C : *Cst) {
785 Known.KnownFPClasses |= C.classify();
786 if (C.isNegative())
787 SignBitAllZero = false;
788 else
789 SignBitAllOne = false;
790 }
791
792 if (SignBitAllOne != SignBitAllZero)
793 Known.SignBit = SignBitAllOne;
794
795 break;
796 }
798 Known.resetAll();
799 break;
800 }
801 }
802
803 return;
804 }
805
806 FPClassTest KnownNotFromFlags = fcNone;
808 KnownNotFromFlags |= fcNan;
810 KnownNotFromFlags |= fcInf;
811
812 // We no longer need to find out about these bits from inputs if we can
813 // assume this from flags/attributes.
814 InterestedClasses &= ~KnownNotFromFlags;
815
816 auto ClearClassesFromFlags =
817 make_scope_exit([=, &Known] { Known.knownNot(KnownNotFromFlags); });
818
819 // All recursive calls that increase depth must come after this.
821 return;
822
823 const MachineFunction *MF = MI.getMF();
824
825 switch (Opcode) {
826 default:
827 TL.computeKnownFPClassForTargetInstr(*this, R, Known, DemandedElts, MRI,
828 Depth);
829 break;
830 case TargetOpcode::G_FNEG: {
831 Register Val = MI.getOperand(1).getReg();
832 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known, Depth + 1);
833 Known.fneg();
834 break;
835 }
836 case TargetOpcode::G_SELECT: {
837 GSelect &SelMI = cast<GSelect>(MI);
838 Register Cond = SelMI.getCondReg();
839 Register LHS = SelMI.getTrueReg();
840 Register RHS = SelMI.getFalseReg();
841
842 FPClassTest FilterLHS = fcAllFlags;
843 FPClassTest FilterRHS = fcAllFlags;
844
845 Register TestedValue;
846 FPClassTest MaskIfTrue = fcAllFlags;
847 FPClassTest MaskIfFalse = fcAllFlags;
848 FPClassTest ClassVal = fcNone;
849
851 Register CmpLHS, CmpRHS;
852 if (mi_match(Cond, MRI,
853 m_GFCmp(m_Pred(Pred), m_Reg(CmpLHS), m_Reg(CmpRHS)))) {
854 // If the select filters out a value based on the class, it no longer
855 // participates in the class of the result
856
857 // TODO: In some degenerate cases we can infer something if we try again
858 // without looking through sign operations.
859 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
860 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
861 fcmpImpliesClass(Pred, *MF, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
862 } else if (mi_match(
863 Cond, MRI,
864 m_GIsFPClass(m_Reg(TestedValue), m_FPClassTest(ClassVal)))) {
865 FPClassTest TestedMask = ClassVal;
866 MaskIfTrue = TestedMask;
867 MaskIfFalse = ~TestedMask;
868 }
869
870 if (TestedValue == LHS) {
871 // match !isnan(x) ? x : y
872 FilterLHS = MaskIfTrue;
873 } else if (TestedValue == RHS) { // && IsExactClass
874 // match !isnan(x) ? y : x
875 FilterRHS = MaskIfFalse;
876 }
877
878 KnownFPClass Known2;
879 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
880 Depth + 1);
881 Known.KnownFPClasses &= FilterLHS;
882
883 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
884 Known2, Depth + 1);
885 Known2.KnownFPClasses &= FilterRHS;
886
887 Known |= Known2;
888 break;
889 }
890 case TargetOpcode::G_FCOPYSIGN: {
891 Register Magnitude = MI.getOperand(1).getReg();
892 Register Sign = MI.getOperand(2).getReg();
893
894 KnownFPClass KnownSign;
895
896 computeKnownFPClass(Magnitude, DemandedElts, InterestedClasses, Known,
897 Depth + 1);
898 computeKnownFPClass(Sign, DemandedElts, InterestedClasses, KnownSign,
899 Depth + 1);
900 Known.copysign(KnownSign);
901 break;
902 }
903 case TargetOpcode::G_FMA:
904 case TargetOpcode::G_STRICT_FMA:
905 case TargetOpcode::G_FMAD: {
906 if ((InterestedClasses & fcNegative) == fcNone)
907 break;
908
909 Register A = MI.getOperand(1).getReg();
910 Register B = MI.getOperand(2).getReg();
911 Register C = MI.getOperand(3).getReg();
912
913 if (A != B)
914 break;
915
916 // The multiply cannot be -0 and therefore the add can't be -0
917 Known.knownNot(fcNegZero);
918
919 // x * x + y is non-negative if y is non-negative.
920 KnownFPClass KnownAddend;
921 computeKnownFPClass(C, DemandedElts, InterestedClasses, KnownAddend,
922 Depth + 1);
923
924 if (KnownAddend.cannotBeOrderedLessThanZero())
925 Known.knownNot(fcNegative);
926 break;
927 }
928 case TargetOpcode::G_FSQRT:
929 case TargetOpcode::G_STRICT_FSQRT: {
930 KnownFPClass KnownSrc;
931 FPClassTest InterestedSrcs = InterestedClasses;
932 if (InterestedClasses & fcNan)
934
935 Register Val = MI.getOperand(1).getReg();
936
937 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
938
939 if (KnownSrc.isKnownNeverPosInfinity())
940 Known.knownNot(fcPosInf);
941 if (KnownSrc.isKnownNever(fcSNan))
942 Known.knownNot(fcSNan);
943
944 // Any negative value besides -0 returns a nan.
945 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
946 Known.knownNot(fcNan);
947
948 // The only negative value that can be returned is -0 for -0 inputs.
950 break;
951 }
952 case TargetOpcode::G_FABS: {
953 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
954 Register Val = MI.getOperand(1).getReg();
955 // If we only care about the sign bit we don't need to inspect the
956 // operand.
957 computeKnownFPClass(Val, DemandedElts, InterestedClasses, Known,
958 Depth + 1);
959 }
960 Known.fabs();
961 break;
962 }
963 case TargetOpcode::G_FSIN:
964 case TargetOpcode::G_FCOS:
965 case TargetOpcode::G_FSINCOS: {
966 // Return NaN on infinite inputs.
967 Register Val = MI.getOperand(1).getReg();
968 KnownFPClass KnownSrc;
969
970 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
971 Depth + 1);
972 Known.knownNot(fcInf);
973
974 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
975 Known.knownNot(fcNan);
976 break;
977 }
978 case TargetOpcode::G_FMAXNUM:
979 case TargetOpcode::G_FMINNUM:
980 case TargetOpcode::G_FMINNUM_IEEE:
981 case TargetOpcode::G_FMAXIMUM:
982 case TargetOpcode::G_FMINIMUM:
983 case TargetOpcode::G_FMAXNUM_IEEE:
984 case TargetOpcode::G_FMAXIMUMNUM:
985 case TargetOpcode::G_FMINIMUMNUM: {
986 Register LHS = MI.getOperand(1).getReg();
987 Register RHS = MI.getOperand(2).getReg();
988 KnownFPClass KnownLHS, KnownRHS;
989
990 computeKnownFPClass(LHS, DemandedElts, InterestedClasses, KnownLHS,
991 Depth + 1);
992 computeKnownFPClass(RHS, DemandedElts, InterestedClasses, KnownRHS,
993 Depth + 1);
994
995 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
996 Known = KnownLHS | KnownRHS;
997
998 // If either operand is not NaN, the result is not NaN.
999 if (NeverNaN && (Opcode == TargetOpcode::G_FMINNUM ||
1000 Opcode == TargetOpcode::G_FMAXNUM ||
1001 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1002 Opcode == TargetOpcode::G_FMAXIMUMNUM))
1003 Known.knownNot(fcNan);
1004
1005 if (Opcode == TargetOpcode::G_FMAXNUM ||
1006 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1007 Opcode == TargetOpcode::G_FMAXNUM_IEEE) {
1008 // If at least one operand is known to be positive, the result must be
1009 // positive.
1010 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
1011 KnownLHS.isKnownNeverNaN()) ||
1012 (KnownRHS.cannotBeOrderedLessThanZero() &&
1013 KnownRHS.isKnownNeverNaN()))
1015 } else if (Opcode == TargetOpcode::G_FMAXIMUM) {
1016 // If at least one operand is known to be positive, the result must be
1017 // positive.
1018 if (KnownLHS.cannotBeOrderedLessThanZero() ||
1019 KnownRHS.cannotBeOrderedLessThanZero())
1021 } else if (Opcode == TargetOpcode::G_FMINNUM ||
1022 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1023 Opcode == TargetOpcode::G_FMINNUM_IEEE) {
1024 // If at least one operand is known to be negative, the result must be
1025 // negative.
1026 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
1027 KnownLHS.isKnownNeverNaN()) ||
1028 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
1029 KnownRHS.isKnownNeverNaN()))
1031 } else if (Opcode == TargetOpcode::G_FMINIMUM) {
1032 // If at least one operand is known to be negative, the result must be
1033 // negative.
1034 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
1037 } else {
1038 llvm_unreachable("unhandled intrinsic");
1039 }
1040
1041 // Fixup zero handling if denormals could be returned as a zero.
1042 //
1043 // As there's no spec for denormal flushing, be conservative with the
1044 // treatment of denormals that could be flushed to zero. For older
1045 // subtargets on AMDGPU the min/max instructions would not flush the
1046 // output and return the original value.
1047 //
1048 if ((Known.KnownFPClasses & fcZero) != fcNone &&
1049 !Known.isKnownNeverSubnormal()) {
1050 DenormalMode Mode =
1051 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType()));
1052 if (Mode != DenormalMode::getIEEE())
1053 Known.KnownFPClasses |= fcZero;
1054 }
1055
1056 if (Known.isKnownNeverNaN()) {
1057 if (KnownLHS.SignBit && KnownRHS.SignBit &&
1058 *KnownLHS.SignBit == *KnownRHS.SignBit) {
1059 if (*KnownLHS.SignBit)
1060 Known.signBitMustBeOne();
1061 else
1062 Known.signBitMustBeZero();
1063 } else if ((Opcode == TargetOpcode::G_FMAXIMUM ||
1064 Opcode == TargetOpcode::G_FMINIMUM) ||
1065 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1066 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1067 Opcode == TargetOpcode::G_FMAXNUM_IEEE ||
1068 Opcode == TargetOpcode::G_FMINNUM_IEEE ||
1069 // FIXME: Should be using logical zero versions
1070 ((KnownLHS.isKnownNeverNegZero() ||
1071 KnownRHS.isKnownNeverPosZero()) &&
1072 (KnownLHS.isKnownNeverPosZero() ||
1073 KnownRHS.isKnownNeverNegZero()))) {
1074 if ((Opcode == TargetOpcode::G_FMAXIMUM ||
1075 Opcode == TargetOpcode::G_FMAXNUM ||
1076 Opcode == TargetOpcode::G_FMAXIMUMNUM ||
1077 Opcode == TargetOpcode::G_FMAXNUM_IEEE) &&
1078 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
1079 Known.signBitMustBeZero();
1080 else if ((Opcode == TargetOpcode::G_FMINIMUM ||
1081 Opcode == TargetOpcode::G_FMINNUM ||
1082 Opcode == TargetOpcode::G_FMINIMUMNUM ||
1083 Opcode == TargetOpcode::G_FMINNUM_IEEE) &&
1084 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
1085 Known.signBitMustBeOne();
1086 }
1087 }
1088 break;
1089 }
1090 case TargetOpcode::G_FCANONICALIZE: {
1091 Register Val = MI.getOperand(1).getReg();
1092 KnownFPClass KnownSrc;
1093 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1094 Depth + 1);
1095
1096 // This is essentially a stronger form of
1097 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
1098 // actually have an IR canonicalization guarantee.
1099
1100 // Canonicalize may flush denormals to zero, so we have to consider the
1101 // denormal mode to preserve known-not-0 knowledge.
1102 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
1103
1104 // Stronger version of propagateNaN
1105 // Canonicalize is guaranteed to quiet signaling nans.
1106 if (KnownSrc.isKnownNeverNaN())
1107 Known.knownNot(fcNan);
1108 else
1109 Known.knownNot(fcSNan);
1110
1111 // If the parent function flushes denormals, the canonical output cannot
1112 // be a denormal.
1113 LLT Ty = MRI.getType(Val).getScalarType();
1114 const fltSemantics &FPType = getFltSemanticForLLT(Ty);
1115 DenormalMode DenormMode = MF->getDenormalMode(FPType);
1116 if (DenormMode == DenormalMode::getIEEE()) {
1117 if (KnownSrc.isKnownNever(fcPosZero))
1118 Known.knownNot(fcPosZero);
1119 if (KnownSrc.isKnownNever(fcNegZero))
1120 Known.knownNot(fcNegZero);
1121 break;
1122 }
1123
1124 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
1125 Known.knownNot(fcSubnormal);
1126
1127 if (DenormMode.Input == DenormalMode::PositiveZero ||
1128 (DenormMode.Output == DenormalMode::PositiveZero &&
1129 DenormMode.Input == DenormalMode::IEEE))
1130 Known.knownNot(fcNegZero);
1131
1132 break;
1133 }
1134 case TargetOpcode::G_VECREDUCE_FMAX:
1135 case TargetOpcode::G_VECREDUCE_FMIN:
1136 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
1137 case TargetOpcode::G_VECREDUCE_FMINIMUM: {
1138 Register Val = MI.getOperand(1).getReg();
1139 // reduce min/max will choose an element from one of the vector elements,
1140 // so we can infer and class information that is common to all elements.
1141
1142 Known =
1143 computeKnownFPClass(Val, MI.getFlags(), InterestedClasses, Depth + 1);
1144 // Can only propagate sign if output is never NaN.
1145 if (!Known.isKnownNeverNaN())
1146 Known.SignBit.reset();
1147 break;
1148 }
1149 case TargetOpcode::G_TRUNC:
1150 case TargetOpcode::G_FFLOOR:
1151 case TargetOpcode::G_FCEIL:
1152 case TargetOpcode::G_FRINT:
1153 case TargetOpcode::G_FNEARBYINT:
1154 case TargetOpcode::G_INTRINSIC_FPTRUNC_ROUND:
1155 case TargetOpcode::G_INTRINSIC_ROUND: {
1156 Register Val = MI.getOperand(1).getReg();
1157 KnownFPClass KnownSrc;
1158 FPClassTest InterestedSrcs = InterestedClasses;
1159 if (InterestedSrcs & fcPosFinite)
1160 InterestedSrcs |= fcPosFinite;
1161 if (InterestedSrcs & fcNegFinite)
1162 InterestedSrcs |= fcNegFinite;
1163 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1164
1165 // Integer results cannot be subnormal.
1166 Known.knownNot(fcSubnormal);
1167
1168 Known.propagateNaN(KnownSrc, true);
1169
1170 // TODO: handle multi unit FPTypes once LLT FPInfo lands
1171
1172 // Negative round ups to 0 produce -0
1173 if (KnownSrc.isKnownNever(fcPosFinite))
1174 Known.knownNot(fcPosFinite);
1175 if (KnownSrc.isKnownNever(fcNegFinite))
1176 Known.knownNot(fcNegFinite);
1177
1178 break;
1179 }
1180 case TargetOpcode::G_FEXP:
1181 case TargetOpcode::G_FEXP2:
1182 case TargetOpcode::G_FEXP10: {
1183 Known.knownNot(fcNegative);
1184 if ((InterestedClasses & fcNan) == fcNone)
1185 break;
1186
1187 Register Val = MI.getOperand(1).getReg();
1188 KnownFPClass KnownSrc;
1189 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1190 Depth + 1);
1191 if (KnownSrc.isKnownNeverNaN()) {
1192 Known.knownNot(fcNan);
1193 Known.signBitMustBeZero();
1194 }
1195
1196 break;
1197 }
1198 case TargetOpcode::G_FLOG:
1199 case TargetOpcode::G_FLOG2:
1200 case TargetOpcode::G_FLOG10: {
1201 // log(+inf) -> +inf
1202 // log([+-]0.0) -> -inf
1203 // log(-inf) -> nan
1204 // log(-x) -> nan
1205 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
1206 break;
1207
1208 FPClassTest InterestedSrcs = InterestedClasses;
1209 if ((InterestedClasses & fcNegInf) != fcNone)
1210 InterestedSrcs |= fcZero | fcSubnormal;
1211 if ((InterestedClasses & fcNan) != fcNone)
1212 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
1213
1214 Register Val = MI.getOperand(1).getReg();
1215 KnownFPClass KnownSrc;
1216 computeKnownFPClass(Val, DemandedElts, InterestedSrcs, KnownSrc, Depth + 1);
1217
1218 if (KnownSrc.isKnownNeverPosInfinity())
1219 Known.knownNot(fcPosInf);
1220
1221 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
1222 Known.knownNot(fcNan);
1223
1224 LLT Ty = MRI.getType(Val).getScalarType();
1225 const fltSemantics &FltSem = getFltSemanticForLLT(Ty);
1226 DenormalMode Mode = MF->getDenormalMode(FltSem);
1227
1228 if (KnownSrc.isKnownNeverLogicalZero(Mode))
1229 Known.knownNot(fcNegInf);
1230
1231 break;
1232 }
1233 case TargetOpcode::G_FPOWI: {
1234 if ((InterestedClasses & fcNegative) == fcNone)
1235 break;
1236
1237 Register Exp = MI.getOperand(2).getReg();
1238 LLT ExpTy = MRI.getType(Exp);
1239 KnownBits ExponentKnownBits = getKnownBits(
1240 Exp, ExpTy.isVector() ? DemandedElts : APInt(1, 1), Depth + 1);
1241
1242 if (ExponentKnownBits.Zero[0]) { // Is even
1243 Known.knownNot(fcNegative);
1244 break;
1245 }
1246
1247 // Given that exp is an integer, here are the
1248 // ways that pow can return a negative value:
1249 //
1250 // pow(-x, exp) --> negative if exp is odd and x is negative.
1251 // pow(-0, exp) --> -inf if exp is negative odd.
1252 // pow(-0, exp) --> -0 if exp is positive odd.
1253 // pow(-inf, exp) --> -0 if exp is negative odd.
1254 // pow(-inf, exp) --> -inf if exp is positive odd.
1255 Register Val = MI.getOperand(1).getReg();
1256 KnownFPClass KnownSrc;
1257 computeKnownFPClass(Val, DemandedElts, fcNegative, KnownSrc, Depth + 1);
1258 if (KnownSrc.isKnownNever(fcNegative))
1259 Known.knownNot(fcNegative);
1260 break;
1261 }
1262 case TargetOpcode::G_FLDEXP:
1263 case TargetOpcode::G_STRICT_FLDEXP: {
1264 Register Val = MI.getOperand(1).getReg();
1265 KnownFPClass KnownSrc;
1266 computeKnownFPClass(Val, DemandedElts, InterestedClasses, KnownSrc,
1267 Depth + 1);
1268 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
1269
1270 // Sign is preserved, but underflows may produce zeroes.
1271 if (KnownSrc.isKnownNever(fcNegative))
1272 Known.knownNot(fcNegative);
1273 else if (KnownSrc.cannotBeOrderedLessThanZero())
1275
1276 if (KnownSrc.isKnownNever(fcPositive))
1277 Known.knownNot(fcPositive);
1278 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
1280
1281 // Can refine inf/zero handling based on the exponent operand.
1282 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
1283 if ((InterestedClasses & ExpInfoMask) == fcNone)
1284 break;
1285 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
1286 break;
1287
1288 // TODO: Handle constant range of Exp
1289
1290 break;
1291 }
1292 case TargetOpcode::G_INTRINSIC_ROUNDEVEN: {
1293 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1294 Depth);
1295 break;
1296 }
1297 case TargetOpcode::G_FADD:
1298 case TargetOpcode::G_STRICT_FADD:
1299 case TargetOpcode::G_FSUB:
1300 case TargetOpcode::G_STRICT_FSUB: {
1301 Register LHS = MI.getOperand(1).getReg();
1302 Register RHS = MI.getOperand(2).getReg();
1303 KnownFPClass KnownLHS, KnownRHS;
1304 bool WantNegative =
1305 (Opcode == TargetOpcode::G_FADD ||
1306 Opcode == TargetOpcode::G_STRICT_FADD) &&
1307 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
1308 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
1309 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
1310
1311 if (!WantNaN && !WantNegative && !WantNegZero)
1312 break;
1313
1314 FPClassTest InterestedSrcs = InterestedClasses;
1315 if (WantNegative)
1316 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
1317 if (InterestedClasses & fcNan)
1318 InterestedSrcs |= fcInf;
1319 computeKnownFPClass(RHS, DemandedElts, InterestedSrcs, KnownRHS, Depth + 1);
1320
1321 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
1322 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
1323 WantNegZero ||
1324 (Opcode == TargetOpcode::G_FSUB ||
1325 Opcode == TargetOpcode::G_STRICT_FSUB)) {
1326
1327 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
1328 // there's no point.
1329 computeKnownFPClass(LHS, DemandedElts, InterestedSrcs, KnownLHS,
1330 Depth + 1);
1331 // Adding positive and negative infinity produces NaN.
1332 // TODO: Check sign of infinities.
1333 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1334 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
1335 Known.knownNot(fcNan);
1336
1337 if (Opcode == Instruction::FAdd) {
1338 if (KnownLHS.cannotBeOrderedLessThanZero() &&
1339 KnownRHS.cannotBeOrderedLessThanZero())
1341
1342 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
1343 if ((KnownLHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1345 KnownRHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1346 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1347 // Make sure output negative denormal can't flush to -0
1349 Known.knownNot(fcNegZero);
1350 } else {
1351 // Only fsub -0, +0 can return -0
1352 if ((KnownLHS.isKnownNeverLogicalNegZero(MF->getDenormalMode(
1354 KnownRHS.isKnownNeverLogicalPosZero(MF->getDenormalMode(
1355 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1356 // Make sure output negative denormal can't flush to -0
1358 Known.knownNot(fcNegZero);
1359 }
1360 }
1361
1362 break;
1363 }
1364 case TargetOpcode::G_FMUL:
1365 case TargetOpcode::G_STRICT_FMUL: {
1366 Register LHS = MI.getOperand(1).getReg();
1367 Register RHS = MI.getOperand(2).getReg();
1368 // X * X is always non-negative or a NaN.
1369 if (LHS == RHS)
1370 Known.knownNot(fcNegative);
1371
1372 if ((InterestedClasses & fcNan) != fcNan)
1373 break;
1374
1375 // fcSubnormal is only needed in case of DAZ.
1376 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
1377
1378 KnownFPClass KnownLHS, KnownRHS;
1379 computeKnownFPClass(RHS, DemandedElts, NeedForNan, KnownRHS, Depth + 1);
1380 if (!KnownRHS.isKnownNeverNaN())
1381 break;
1382
1383 computeKnownFPClass(LHS, DemandedElts, NeedForNan, KnownLHS, Depth + 1);
1384 if (!KnownLHS.isKnownNeverNaN())
1385 break;
1386
1387 if (KnownLHS.SignBit && KnownRHS.SignBit) {
1388 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
1389 Known.signBitMustBeZero();
1390 else
1391 Known.signBitMustBeOne();
1392 }
1393
1394 // If 0 * +/-inf produces NaN.
1395 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
1396 Known.knownNot(fcNan);
1397 break;
1398 }
1399
1400 if ((KnownRHS.isKnownNeverInfinity() ||
1401 KnownLHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1402 getFltSemanticForLLT(DstTy.getScalarType())))) &&
1403 (KnownLHS.isKnownNeverInfinity() ||
1404 KnownRHS.isKnownNeverLogicalZero(
1405 MF->getDenormalMode(getFltSemanticForLLT(DstTy.getScalarType())))))
1406 Known.knownNot(fcNan);
1407
1408 break;
1409 }
1410 case TargetOpcode::G_FDIV:
1411 case TargetOpcode::G_FREM: {
1412 Register LHS = MI.getOperand(1).getReg();
1413 Register RHS = MI.getOperand(2).getReg();
1414
1415 if (LHS == RHS) {
1416 // TODO: Could filter out snan if we inspect the operand
1417 if (Opcode == TargetOpcode::G_FDIV) {
1418 // X / X is always exactly 1.0 or a NaN.
1420 } else {
1421 // X % X is always exactly [+-]0.0 or a NaN.
1422 Known.KnownFPClasses = fcNan | fcZero;
1423 }
1424
1425 break;
1426 }
1427
1428 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
1429 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
1430 const bool WantPositive = Opcode == TargetOpcode::G_FREM &&
1431 (InterestedClasses & fcPositive) != fcNone;
1432 if (!WantNan && !WantNegative && !WantPositive)
1433 break;
1434
1435 KnownFPClass KnownLHS, KnownRHS;
1436
1437 computeKnownFPClass(RHS, DemandedElts, fcNan | fcInf | fcZero | fcNegative,
1438 KnownRHS, Depth + 1);
1439
1440 bool KnowSomethingUseful =
1441 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
1442
1443 if (KnowSomethingUseful || WantPositive) {
1444 const FPClassTest InterestedLHS =
1445 WantPositive ? fcAllFlags
1447
1448 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & InterestedLHS,
1449 KnownLHS, Depth + 1);
1450 }
1451
1452 if (Opcode == Instruction::FDiv) {
1453 // Only 0/0, Inf/Inf produce NaN.
1454 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1455 (KnownLHS.isKnownNeverInfinity() ||
1456 KnownRHS.isKnownNeverInfinity()) &&
1457 ((KnownLHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1458 getFltSemanticForLLT(DstTy.getScalarType())))) ||
1459 (KnownRHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1460 getFltSemanticForLLT(DstTy.getScalarType())))))) {
1461 Known.knownNot(fcNan);
1462 }
1463
1464 // X / -0.0 is -Inf (or NaN).
1465 // +X / +X is +X
1466 if (KnownLHS.isKnownNever(fcNegative) &&
1467 KnownRHS.isKnownNever(fcNegative))
1468 Known.knownNot(fcNegative);
1469 } else {
1470 // Inf REM x and x REM 0 produce NaN.
1471 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
1472 KnownLHS.isKnownNeverInfinity() &&
1473 KnownRHS.isKnownNeverLogicalZero(MF->getDenormalMode(
1475 Known.knownNot(fcNan);
1476 }
1477
1478 // The sign for frem is the same as the first operand.
1479 if (KnownLHS.cannotBeOrderedLessThanZero())
1481 if (KnownLHS.cannotBeOrderedGreaterThanZero())
1483
1484 // See if we can be more aggressive about the sign of 0.
1485 if (KnownLHS.isKnownNever(fcNegative))
1486 Known.knownNot(fcNegative);
1487 if (KnownLHS.isKnownNever(fcPositive))
1488 Known.knownNot(fcPositive);
1489 }
1490
1491 break;
1492 }
1493 case TargetOpcode::G_FPEXT: {
1494 Register Dst = MI.getOperand(0).getReg();
1495 Register Src = MI.getOperand(1).getReg();
1496 // Infinity, nan and zero propagate from source.
1497 computeKnownFPClass(R, DemandedElts, InterestedClasses, Known, Depth + 1);
1498
1499 LLT DstTy = MRI.getType(Dst).getScalarType();
1500 const fltSemantics &DstSem = getFltSemanticForLLT(DstTy);
1501 LLT SrcTy = MRI.getType(Src).getScalarType();
1502 const fltSemantics &SrcSem = getFltSemanticForLLT(SrcTy);
1503
1504 // All subnormal inputs should be in the normal range in the result type.
1505 if (APFloat::isRepresentableAsNormalIn(SrcSem, DstSem)) {
1506 if (Known.KnownFPClasses & fcPosSubnormal)
1507 Known.KnownFPClasses |= fcPosNormal;
1508 if (Known.KnownFPClasses & fcNegSubnormal)
1509 Known.KnownFPClasses |= fcNegNormal;
1510 Known.knownNot(fcSubnormal);
1511 }
1512
1513 // Sign bit of a nan isn't guaranteed.
1514 if (!Known.isKnownNeverNaN())
1515 Known.SignBit = std::nullopt;
1516 break;
1517 }
1518 case TargetOpcode::G_FPTRUNC: {
1519 computeKnownFPClassForFPTrunc(MI, DemandedElts, InterestedClasses, Known,
1520 Depth);
1521 break;
1522 }
1523 case TargetOpcode::G_SITOFP:
1524 case TargetOpcode::G_UITOFP: {
1525 // Cannot produce nan
1526 Known.knownNot(fcNan);
1527
1528 // Integers cannot be subnormal
1529 Known.knownNot(fcSubnormal);
1530
1531 // sitofp and uitofp turn into +0.0 for zero.
1532 Known.knownNot(fcNegZero);
1533 if (Opcode == TargetOpcode::G_UITOFP)
1534 Known.signBitMustBeZero();
1535
1536 Register Val = MI.getOperand(1).getReg();
1537 LLT Ty = MRI.getType(Val);
1538
1539 if (InterestedClasses & fcInf) {
1540 // Get width of largest magnitude integer (remove a bit if signed).
1541 // This still works for a signed minimum value because the largest FP
1542 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).;
1543 int IntSize = Ty.getScalarSizeInBits();
1544 if (Opcode == TargetOpcode::G_SITOFP)
1545 --IntSize;
1546
1547 // If the exponent of the largest finite FP value can hold the largest
1548 // integer, the result of the cast must be finite.
1549 LLT FPTy = DstTy.getScalarType();
1550 const fltSemantics &FltSem = getFltSemanticForLLT(FPTy);
1551 if (ilogb(APFloat::getLargest(FltSem)) >= IntSize)
1552 Known.knownNot(fcInf);
1553 }
1554
1555 break;
1556 }
1557 // case TargetOpcode::G_MERGE_VALUES:
1558 case TargetOpcode::G_BUILD_VECTOR:
1559 case TargetOpcode::G_CONCAT_VECTORS: {
1560 GMergeLikeInstr &Merge = cast<GMergeLikeInstr>(MI);
1561
1562 if (!DstTy.isFixedVector())
1563 break;
1564
1565 bool First = true;
1566 for (unsigned Idx = 0; Idx < Merge.getNumSources(); ++Idx) {
1567 // We know the index we are inserting to, so clear it from Vec check.
1568 bool NeedsElt = DemandedElts[Idx];
1569
1570 // Do we demand the inserted element?
1571 if (NeedsElt) {
1572 Register Src = Merge.getSourceReg(Idx);
1573 if (First) {
1574 computeKnownFPClass(Src, Known, InterestedClasses, Depth + 1);
1575 First = false;
1576 } else {
1577 KnownFPClass Known2;
1578 computeKnownFPClass(Src, Known2, InterestedClasses, Depth + 1);
1579 Known |= Known2;
1580 }
1581
1582 // If we don't know any bits, early out.
1583 if (Known.isUnknown())
1584 break;
1585 }
1586 }
1587
1588 break;
1589 }
1590 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1591 // Look through extract element. If the index is non-constant or
1592 // out-of-range demand all elements, otherwise just the extracted
1593 // element.
1594 GExtractVectorElement &Extract = cast<GExtractVectorElement>(MI);
1595 Register Vec = Extract.getVectorReg();
1596 Register Idx = Extract.getIndexReg();
1597
1598 auto CIdx = getIConstantVRegVal(Idx, MRI);
1599
1600 LLT VecTy = MRI.getType(Vec);
1601
1602 if (VecTy.isFixedVector()) {
1603 unsigned NumElts = VecTy.getNumElements();
1604 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1605 if (CIdx && CIdx->ult(NumElts))
1606 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1607 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
1608 Depth + 1);
1609 }
1610
1611 break;
1612 }
1613 case TargetOpcode::G_INSERT_VECTOR_ELT: {
1614 GInsertVectorElement &Insert = cast<GInsertVectorElement>(MI);
1615 Register Vec = Insert.getVectorReg();
1616 Register Elt = Insert.getElementReg();
1617 Register Idx = Insert.getIndexReg();
1618
1619 LLT VecTy = MRI.getType(Vec);
1620
1621 if (VecTy.isScalableVector())
1622 return;
1623
1624 auto CIdx = getIConstantVRegVal(Idx, MRI);
1625
1626 unsigned NumElts = DemandedElts.getBitWidth();
1627 APInt DemandedVecElts = DemandedElts;
1628 bool NeedsElt = true;
1629 // If we know the index we are inserting to, clear it from Vec check.
1630 if (CIdx && CIdx->ult(NumElts)) {
1631 DemandedVecElts.clearBit(CIdx->getZExtValue());
1632 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1633 }
1634
1635 // Do we demand the inserted element?
1636 if (NeedsElt) {
1637 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1);
1638 // If we don't know any bits, early out.
1639 if (Known.isUnknown())
1640 break;
1641 } else {
1642 Known.KnownFPClasses = fcNone;
1643 }
1644
1645 // Do we need anymore elements from Vec?
1646 if (!DemandedVecElts.isZero()) {
1647 KnownFPClass Known2;
1648 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
1649 Depth + 1);
1650 Known |= Known2;
1651 }
1652
1653 break;
1654 }
1655 case TargetOpcode::G_SHUFFLE_VECTOR: {
1656 // For undef elements, we don't know anything about the common state of
1657 // the shuffle result.
1658 GShuffleVector &Shuf = cast<GShuffleVector>(MI);
1659 APInt DemandedLHS, DemandedRHS;
1660 if (DstTy.isScalableVector()) {
1661 assert(DemandedElts == APInt(1, 1));
1662 DemandedLHS = DemandedRHS = DemandedElts;
1663 } else {
1665 DemandedElts, DemandedLHS,
1666 DemandedRHS)) {
1667 Known.resetAll();
1668 return;
1669 }
1670 }
1671
1672 if (!!DemandedLHS) {
1673 Register LHS = Shuf.getSrc1Reg();
1674 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
1675 Depth + 1);
1676
1677 // If we don't know any bits, early out.
1678 if (Known.isUnknown())
1679 break;
1680 } else {
1681 Known.KnownFPClasses = fcNone;
1682 }
1683
1684 if (!!DemandedRHS) {
1685 KnownFPClass Known2;
1686 Register RHS = Shuf.getSrc2Reg();
1687 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
1688 Depth + 1);
1689 Known |= Known2;
1690 }
1691 break;
1692 }
1693 case TargetOpcode::COPY: {
1694 Register Src = MI.getOperand(1).getReg();
1695
1696 if (!Src.isVirtual())
1697 return;
1698
1699 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1);
1700 break;
1701 }
1702 }
1703}
1704
1706GISelValueTracking::computeKnownFPClass(Register R, const APInt &DemandedElts,
1707 FPClassTest InterestedClasses,
1708 unsigned Depth) {
1709 KnownFPClass KnownClasses;
1710 computeKnownFPClass(R, DemandedElts, InterestedClasses, KnownClasses, Depth);
1711 return KnownClasses;
1712}
1713
1714KnownFPClass GISelValueTracking::computeKnownFPClass(
1715 Register R, FPClassTest InterestedClasses, unsigned Depth) {
1716 KnownFPClass Known;
1717 computeKnownFPClass(R, Known, InterestedClasses, Depth);
1718 return Known;
1719}
1720
1721KnownFPClass GISelValueTracking::computeKnownFPClass(
1722 Register R, const APInt &DemandedElts, uint32_t Flags,
1723 FPClassTest InterestedClasses, unsigned Depth) {
1725 InterestedClasses &= ~fcNan;
1727 InterestedClasses &= ~fcInf;
1728
1729 KnownFPClass Result =
1730 computeKnownFPClass(R, DemandedElts, InterestedClasses, Depth);
1731
1733 Result.KnownFPClasses &= ~fcNan;
1735 Result.KnownFPClasses &= ~fcInf;
1736 return Result;
1737}
1738
1739KnownFPClass GISelValueTracking::computeKnownFPClass(
1740 Register R, uint32_t Flags, FPClassTest InterestedClasses, unsigned Depth) {
1741 LLT Ty = MRI.getType(R);
1742 APInt DemandedElts =
1743 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
1744 return computeKnownFPClass(R, DemandedElts, Flags, InterestedClasses, Depth);
1745}
1746
1747/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
1748unsigned GISelValueTracking::computeNumSignBitsMin(Register Src0, Register Src1,
1749 const APInt &DemandedElts,
1750 unsigned Depth) {
1751 // Test src1 first, since we canonicalize simpler expressions to the RHS.
1752 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
1753 if (Src1SignBits == 1)
1754 return 1;
1755 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
1756}
1757
1758/// Compute the known number of sign bits with attached range metadata in the
1759/// memory operand. If this is an extending load, accounts for the behavior of
1760/// the high bits.
1762 unsigned TyBits) {
1763 const MDNode *Ranges = Ld->getRanges();
1764 if (!Ranges)
1765 return 1;
1766
1768 if (TyBits > CR.getBitWidth()) {
1769 switch (Ld->getOpcode()) {
1770 case TargetOpcode::G_SEXTLOAD:
1771 CR = CR.signExtend(TyBits);
1772 break;
1773 case TargetOpcode::G_ZEXTLOAD:
1774 CR = CR.zeroExtend(TyBits);
1775 break;
1776 default:
1777 break;
1778 }
1779 }
1780
1781 return std::min(CR.getSignedMin().getNumSignBits(),
1783}
1784
1786 const APInt &DemandedElts,
1787 unsigned Depth) {
1788 MachineInstr &MI = *MRI.getVRegDef(R);
1789 unsigned Opcode = MI.getOpcode();
1790
1791 if (Opcode == TargetOpcode::G_CONSTANT)
1792 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
1793
1794 if (Depth == getMaxDepth())
1795 return 1;
1796
1797 if (!DemandedElts)
1798 return 1; // No demanded elts, better to assume we don't know anything.
1799
1800 LLT DstTy = MRI.getType(R);
1801 const unsigned TyBits = DstTy.getScalarSizeInBits();
1802
1803 // Handle the case where this is called on a register that does not have a
1804 // type constraint. This is unlikely to occur except by looking through copies
1805 // but it is possible for the initial register being queried to be in this
1806 // state.
1807 if (!DstTy.isValid())
1808 return 1;
1809
1810 unsigned FirstAnswer = 1;
1811 switch (Opcode) {
1812 case TargetOpcode::COPY: {
1813 MachineOperand &Src = MI.getOperand(1);
1814 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
1815 MRI.getType(Src.getReg()).isValid()) {
1816 // Don't increment Depth for this one since we didn't do any work.
1817 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
1818 }
1819
1820 return 1;
1821 }
1822 case TargetOpcode::G_SEXT: {
1823 Register Src = MI.getOperand(1).getReg();
1824 LLT SrcTy = MRI.getType(Src);
1825 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
1826 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
1827 }
1828 case TargetOpcode::G_ASSERT_SEXT:
1829 case TargetOpcode::G_SEXT_INREG: {
1830 // Max of the input and what this extends.
1831 Register Src = MI.getOperand(1).getReg();
1832 unsigned SrcBits = MI.getOperand(2).getImm();
1833 unsigned InRegBits = TyBits - SrcBits + 1;
1834 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1),
1835 InRegBits);
1836 }
1837 case TargetOpcode::G_LOAD: {
1838 GLoad *Ld = cast<GLoad>(&MI);
1839 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
1840 break;
1841
1842 return computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1843 }
1844 case TargetOpcode::G_SEXTLOAD: {
1846
1847 // FIXME: We need an in-memory type representation.
1848 if (DstTy.isVector())
1849 return 1;
1850
1851 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1852 if (NumBits != 1)
1853 return NumBits;
1854
1855 // e.g. i16->i32 = '17' bits known.
1856 const MachineMemOperand *MMO = *MI.memoperands_begin();
1857 return TyBits - MMO->getSizeInBits().getValue() + 1;
1858 }
1859 case TargetOpcode::G_ZEXTLOAD: {
1861
1862 // FIXME: We need an in-memory type representation.
1863 if (DstTy.isVector())
1864 return 1;
1865
1866 unsigned NumBits = computeNumSignBitsFromRangeMetadata(Ld, TyBits);
1867 if (NumBits != 1)
1868 return NumBits;
1869
1870 // e.g. i16->i32 = '16' bits known.
1871 const MachineMemOperand *MMO = *MI.memoperands_begin();
1872 return TyBits - MMO->getSizeInBits().getValue();
1873 }
1874 case TargetOpcode::G_AND:
1875 case TargetOpcode::G_OR:
1876 case TargetOpcode::G_XOR: {
1877 Register Src1 = MI.getOperand(1).getReg();
1878 unsigned Src1NumSignBits =
1879 computeNumSignBits(Src1, DemandedElts, Depth + 1);
1880 if (Src1NumSignBits != 1) {
1881 Register Src2 = MI.getOperand(2).getReg();
1882 unsigned Src2NumSignBits =
1883 computeNumSignBits(Src2, DemandedElts, Depth + 1);
1884 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits);
1885 }
1886 break;
1887 }
1888 case TargetOpcode::G_ASHR: {
1889 Register Src1 = MI.getOperand(1).getReg();
1890 Register Src2 = MI.getOperand(2).getReg();
1891 FirstAnswer = computeNumSignBits(Src1, DemandedElts, Depth + 1);
1892 if (auto C = getValidMinimumShiftAmount(Src2, DemandedElts, Depth + 1))
1893 FirstAnswer = std::min<uint64_t>(FirstAnswer + *C, TyBits);
1894 break;
1895 }
1896 case TargetOpcode::G_SHL: {
1897 Register Src1 = MI.getOperand(1).getReg();
1898 Register Src2 = MI.getOperand(2).getReg();
1899 if (std::optional<ConstantRange> ShAmtRange =
1900 getValidShiftAmountRange(Src2, DemandedElts, Depth + 1)) {
1901 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
1902 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
1903
1904 MachineInstr &ExtMI = *MRI.getVRegDef(Src1);
1905 unsigned ExtOpc = ExtMI.getOpcode();
1906
1907 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
1908 // shifted out, then we can compute the number of sign bits for the
1909 // operand being extended. A future improvement could be to pass along the
1910 // "shifted left by" information in the recursive calls to
1911 // ComputeKnownSignBits. Allowing us to handle this more generically.
1912 if (ExtOpc == TargetOpcode::G_SEXT || ExtOpc == TargetOpcode::G_ZEXT ||
1913 ExtOpc == TargetOpcode::G_ANYEXT) {
1914 LLT ExtTy = MRI.getType(Src1);
1915 Register Extendee = ExtMI.getOperand(1).getReg();
1916 LLT ExtendeeTy = MRI.getType(Extendee);
1917 uint64_t SizeDiff =
1918 ExtTy.getScalarSizeInBits() - ExtendeeTy.getScalarSizeInBits();
1919
1920 if (SizeDiff <= MinShAmt) {
1921 unsigned Tmp =
1922 SizeDiff + computeNumSignBits(Extendee, DemandedElts, Depth + 1);
1923 if (MaxShAmt < Tmp)
1924 return Tmp - MaxShAmt;
1925 }
1926 }
1927 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
1928 unsigned Tmp = computeNumSignBits(Src1, DemandedElts, Depth + 1);
1929 if (MaxShAmt < Tmp)
1930 return Tmp - MaxShAmt;
1931 }
1932 break;
1933 }
1934 case TargetOpcode::G_TRUNC: {
1935 Register Src = MI.getOperand(1).getReg();
1936 LLT SrcTy = MRI.getType(Src);
1937
1938 // Check if the sign bits of source go down as far as the truncated value.
1939 unsigned DstTyBits = DstTy.getScalarSizeInBits();
1940 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
1941 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
1942 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
1943 return NumSrcSignBits - (NumSrcBits - DstTyBits);
1944 break;
1945 }
1946 case TargetOpcode::G_SELECT: {
1947 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
1948 MI.getOperand(3).getReg(), DemandedElts,
1949 Depth + 1);
1950 }
1951 case TargetOpcode::G_SMIN:
1952 case TargetOpcode::G_SMAX:
1953 case TargetOpcode::G_UMIN:
1954 case TargetOpcode::G_UMAX:
1955 // TODO: Handle clamp pattern with number of sign bits for SMIN/SMAX.
1956 return computeNumSignBitsMin(MI.getOperand(1).getReg(),
1957 MI.getOperand(2).getReg(), DemandedElts,
1958 Depth + 1);
1959 case TargetOpcode::G_SADDO:
1960 case TargetOpcode::G_SADDE:
1961 case TargetOpcode::G_UADDO:
1962 case TargetOpcode::G_UADDE:
1963 case TargetOpcode::G_SSUBO:
1964 case TargetOpcode::G_SSUBE:
1965 case TargetOpcode::G_USUBO:
1966 case TargetOpcode::G_USUBE:
1967 case TargetOpcode::G_SMULO:
1968 case TargetOpcode::G_UMULO: {
1969 // If compares returns 0/-1, all bits are sign bits.
1970 // We know that we have an integer-based boolean since these operations
1971 // are only available for integer.
1972 if (MI.getOperand(1).getReg() == R) {
1973 if (TL.getBooleanContents(DstTy.isVector(), false) ==
1975 return TyBits;
1976 }
1977
1978 break;
1979 }
1980 case TargetOpcode::G_SUB: {
1981 Register Src2 = MI.getOperand(2).getReg();
1982 unsigned Src2NumSignBits =
1983 computeNumSignBits(Src2, DemandedElts, Depth + 1);
1984 if (Src2NumSignBits == 1)
1985 return 1; // Early out.
1986
1987 // Handle NEG.
1988 Register Src1 = MI.getOperand(1).getReg();
1989 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
1990 if (Known1.isZero()) {
1991 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
1992 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1993 // sign bits set.
1994 if ((Known2.Zero | 1).isAllOnes())
1995 return TyBits;
1996
1997 // If the input is known to be positive (the sign bit is known clear),
1998 // the output of the NEG has, at worst, the same number of sign bits as
1999 // the input.
2000 if (Known2.isNonNegative()) {
2001 FirstAnswer = Src2NumSignBits;
2002 break;
2003 }
2004
2005 // Otherwise, we treat this like a SUB.
2006 }
2007
2008 unsigned Src1NumSignBits =
2009 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2010 if (Src1NumSignBits == 1)
2011 return 1; // Early Out.
2012
2013 // Sub can have at most one carry bit. Thus we know that the output
2014 // is, at worst, one more bit than the inputs.
2015 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2016 break;
2017 }
2018 case TargetOpcode::G_ADD: {
2019 Register Src2 = MI.getOperand(2).getReg();
2020 unsigned Src2NumSignBits =
2021 computeNumSignBits(Src2, DemandedElts, Depth + 1);
2022 if (Src2NumSignBits <= 2)
2023 return 1; // Early out.
2024
2025 Register Src1 = MI.getOperand(1).getReg();
2026 unsigned Src1NumSignBits =
2027 computeNumSignBits(Src1, DemandedElts, Depth + 1);
2028 if (Src1NumSignBits == 1)
2029 return 1; // Early Out.
2030
2031 // Special case decrementing a value (ADD X, -1):
2032 KnownBits Known2 = getKnownBits(Src2, DemandedElts, Depth);
2033 if (Known2.isAllOnes()) {
2034 KnownBits Known1 = getKnownBits(Src1, DemandedElts, Depth);
2035 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2036 // sign bits set.
2037 if ((Known1.Zero | 1).isAllOnes())
2038 return TyBits;
2039
2040 // If we are subtracting one from a positive number, there is no carry
2041 // out of the result.
2042 if (Known1.isNonNegative()) {
2043 FirstAnswer = Src1NumSignBits;
2044 break;
2045 }
2046
2047 // Otherwise, we treat this like an ADD.
2048 }
2049
2050 // Add can have at most one carry bit. Thus we know that the output
2051 // is, at worst, one more bit than the inputs.
2052 FirstAnswer = std::min(Src1NumSignBits, Src2NumSignBits) - 1;
2053 break;
2054 }
2055 case TargetOpcode::G_FCMP:
2056 case TargetOpcode::G_ICMP: {
2057 bool IsFP = Opcode == TargetOpcode::G_FCMP;
2058 if (TyBits == 1)
2059 break;
2060 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
2062 return TyBits; // All bits are sign bits.
2064 return TyBits - 1; // Every always-zero bit is a sign bit.
2065 break;
2066 }
2067 case TargetOpcode::G_BUILD_VECTOR: {
2068 // Collect the known bits that are shared by every demanded vector element.
2069 FirstAnswer = TyBits;
2070 APInt SingleDemandedElt(1, 1);
2071 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2072 if (!DemandedElts[I])
2073 continue;
2074
2075 unsigned Tmp2 =
2076 computeNumSignBits(MO.getReg(), SingleDemandedElt, Depth + 1);
2077 FirstAnswer = std::min(FirstAnswer, Tmp2);
2078
2079 // If we don't know any bits, early out.
2080 if (FirstAnswer == 1)
2081 break;
2082 }
2083 break;
2084 }
2085 case TargetOpcode::G_CONCAT_VECTORS: {
2086 if (MRI.getType(MI.getOperand(0).getReg()).isScalableVector())
2087 break;
2088 FirstAnswer = TyBits;
2089 // Determine the minimum number of sign bits across all demanded
2090 // elts of the input vectors. Early out if the result is already 1.
2091 unsigned NumSubVectorElts =
2092 MRI.getType(MI.getOperand(1).getReg()).getNumElements();
2093 for (const auto &[I, MO] : enumerate(drop_begin(MI.operands()))) {
2094 APInt DemandedSub =
2095 DemandedElts.extractBits(NumSubVectorElts, I * NumSubVectorElts);
2096 if (!DemandedSub)
2097 continue;
2098 unsigned Tmp2 = computeNumSignBits(MO.getReg(), DemandedSub, Depth + 1);
2099
2100 FirstAnswer = std::min(FirstAnswer, Tmp2);
2101
2102 // If we don't know any bits, early out.
2103 if (FirstAnswer == 1)
2104 break;
2105 }
2106 break;
2107 }
2108 case TargetOpcode::G_SHUFFLE_VECTOR: {
2109 // Collect the minimum number of sign bits that are shared by every vector
2110 // element referenced by the shuffle.
2111 APInt DemandedLHS, DemandedRHS;
2112 Register Src1 = MI.getOperand(1).getReg();
2113 unsigned NumElts = MRI.getType(Src1).getNumElements();
2114 if (!getShuffleDemandedElts(NumElts, MI.getOperand(3).getShuffleMask(),
2115 DemandedElts, DemandedLHS, DemandedRHS))
2116 return 1;
2117
2118 if (!!DemandedLHS)
2119 FirstAnswer = computeNumSignBits(Src1, DemandedLHS, Depth + 1);
2120 // If we don't know anything, early out and try computeKnownBits fall-back.
2121 if (FirstAnswer == 1)
2122 break;
2123 if (!!DemandedRHS) {
2124 unsigned Tmp2 =
2125 computeNumSignBits(MI.getOperand(2).getReg(), DemandedRHS, Depth + 1);
2126 FirstAnswer = std::min(FirstAnswer, Tmp2);
2127 }
2128 break;
2129 }
2130 case TargetOpcode::G_SPLAT_VECTOR: {
2131 // Check if the sign bits of source go down as far as the truncated value.
2132 Register Src = MI.getOperand(1).getReg();
2133 unsigned NumSrcSignBits = computeNumSignBits(Src, APInt(1, 1), Depth + 1);
2134 unsigned NumSrcBits = MRI.getType(Src).getSizeInBits();
2135 if (NumSrcSignBits > (NumSrcBits - TyBits))
2136 return NumSrcSignBits - (NumSrcBits - TyBits);
2137 break;
2138 }
2139 case TargetOpcode::G_INTRINSIC:
2140 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
2141 case TargetOpcode::G_INTRINSIC_CONVERGENT:
2142 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
2143 default: {
2144 unsigned NumBits =
2145 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
2146 if (NumBits > 1)
2147 FirstAnswer = std::max(FirstAnswer, NumBits);
2148 break;
2149 }
2150 }
2151
2152 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2153 // use this information.
2154 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
2155 APInt Mask;
2156 if (Known.isNonNegative()) { // sign bit is 0
2157 Mask = Known.Zero;
2158 } else if (Known.isNegative()) { // sign bit is 1;
2159 Mask = Known.One;
2160 } else {
2161 // Nothing known.
2162 return FirstAnswer;
2163 }
2164
2165 // Okay, we know that the sign bit in Mask is set. Use CLO to determine
2166 // the number of identical bits in the top of the input value.
2167 Mask <<= Mask.getBitWidth() - TyBits;
2168 return std::max(FirstAnswer, Mask.countl_one());
2169}
2170
2172 LLT Ty = MRI.getType(R);
2173 APInt DemandedElts =
2174 Ty.isFixedVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
2175 return computeNumSignBits(R, DemandedElts, Depth);
2176}
2177
2179 Register R, const APInt &DemandedElts, unsigned Depth) {
2180 // Shifting more than the bitwidth is not valid.
2181 MachineInstr &MI = *MRI.getVRegDef(R);
2182 unsigned Opcode = MI.getOpcode();
2183
2184 LLT Ty = MRI.getType(R);
2185 unsigned BitWidth = Ty.getScalarSizeInBits();
2186
2187 if (Opcode == TargetOpcode::G_CONSTANT) {
2188 const APInt &ShAmt = MI.getOperand(1).getCImm()->getValue();
2189 if (ShAmt.uge(BitWidth))
2190 return std::nullopt;
2191 return ConstantRange(ShAmt);
2192 }
2193
2194 if (Opcode == TargetOpcode::G_BUILD_VECTOR) {
2195 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
2196 for (unsigned I = 0, E = MI.getNumOperands() - 1; I != E; ++I) {
2197 if (!DemandedElts[I])
2198 continue;
2199 MachineInstr *Op = MRI.getVRegDef(MI.getOperand(I + 1).getReg());
2200 if (Op->getOpcode() != TargetOpcode::G_CONSTANT) {
2201 MinAmt = MaxAmt = nullptr;
2202 break;
2203 }
2204
2205 const APInt &ShAmt = Op->getOperand(1).getCImm()->getValue();
2206 if (ShAmt.uge(BitWidth))
2207 return std::nullopt;
2208 if (!MinAmt || MinAmt->ugt(ShAmt))
2209 MinAmt = &ShAmt;
2210 if (!MaxAmt || MaxAmt->ult(ShAmt))
2211 MaxAmt = &ShAmt;
2212 }
2213 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
2214 "Failed to find matching min/max shift amounts");
2215 if (MinAmt && MaxAmt)
2216 return ConstantRange(*MinAmt, *MaxAmt + 1);
2217 }
2218
2219 // Use computeKnownBits to find a hidden constant/knownbits (usually type
2220 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
2221 KnownBits KnownAmt = getKnownBits(R, DemandedElts, Depth);
2222 if (KnownAmt.getMaxValue().ult(BitWidth))
2223 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
2224
2225 return std::nullopt;
2226}
2227
2229 Register R, const APInt &DemandedElts, unsigned Depth) {
2230 if (std::optional<ConstantRange> AmtRange =
2231 getValidShiftAmountRange(R, DemandedElts, Depth))
2232 return AmtRange->getUnsignedMin().getZExtValue();
2233 return std::nullopt;
2234}
2235
2241
2246
2248 if (!Info) {
2249 unsigned MaxDepth =
2251 Info = std::make_unique<GISelValueTracking>(MF, MaxDepth);
2252 }
2253 return *Info;
2254}
2255
2256AnalysisKey GISelValueTrackingAnalysis::Key;
2257
2263
2267 auto &VTA = MFAM.getResult<GISelValueTrackingAnalysis>(MF);
2268 const auto &MRI = MF.getRegInfo();
2269 OS << "name: ";
2270 MF.getFunction().printAsOperand(OS, /*PrintType=*/false);
2271 OS << '\n';
2272
2273 for (MachineBasicBlock &BB : MF) {
2274 for (MachineInstr &MI : BB) {
2275 for (MachineOperand &MO : MI.defs()) {
2276 if (!MO.isReg() || MO.getReg().isPhysical())
2277 continue;
2278 Register Reg = MO.getReg();
2279 if (!MRI.getType(Reg).isValid())
2280 continue;
2281 KnownBits Known = VTA.getKnownBits(Reg);
2282 unsigned SignedBits = VTA.computeNumSignBits(Reg);
2283 OS << " " << MO << " KnownBits:" << Known << " SignBits:" << SignedBits
2284 << '\n';
2285 };
2286 }
2287 }
2288 return PreservedAnalyses::all();
2289}
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:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1406
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:229
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1628
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1435
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:475
void setAllBits()
Set every bit to 1.
Definition APInt.h:1319
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1388
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:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
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.
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:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
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.