Thanks to visit codestin.com
Credit goes to llvm.org

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