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