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