LLVM 18.0.0git
GISelKnownBits.cpp
Go to the documentation of this file.
1//===- lib/CodeGen/GlobalISel/GISelKnownBits.cpp --------------*- C++ *-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// Provides analysis for querying information about KnownBits during GISel
10/// passes.
11//
12//===----------------------------------------------------------------------===//
21#include "llvm/IR/Module.h"
22
23#define DEBUG_TYPE "gisel-known-bits"
24
25using namespace llvm;
26
28
30 "Analysis for ComputingKnownBits", false, true)
31
33 : MF(MF), MRI(MF.getRegInfo()), TL(*MF.getSubtarget().getTargetLowering()),
34 DL(MF.getFunction().getParent()->getDataLayout()), MaxDepth(MaxDepth) {}
35
37 const MachineInstr *MI = MRI.getVRegDef(R);
38 switch (MI->getOpcode()) {
39 case TargetOpcode::COPY:
40 return computeKnownAlignment(MI->getOperand(1).getReg(), Depth);
41 case TargetOpcode::G_ASSERT_ALIGN: {
42 // TODO: Min with source
43 return Align(MI->getOperand(2).getImm());
44 }
45 case TargetOpcode::G_FRAME_INDEX: {
46 int FrameIdx = MI->getOperand(1).getIndex();
47 return MF.getFrameInfo().getObjectAlign(FrameIdx);
48 }
49 case TargetOpcode::G_INTRINSIC:
50 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
51 case TargetOpcode::G_INTRINSIC_CONVERGENT:
52 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
53 default:
54 return TL.computeKnownAlignForTargetInstr(*this, R, MRI, Depth + 1);
55 }
56}
57
59 assert(MI.getNumExplicitDefs() == 1 &&
60 "expected single return generic instruction");
61 return getKnownBits(MI.getOperand(0).getReg());
62}
63
65 const LLT Ty = MRI.getType(R);
66 APInt DemandedElts =
68 return getKnownBits(R, DemandedElts);
69}
70
72 unsigned Depth) {
73 // For now, we only maintain the cache during one request.
74 assert(ComputeKnownBitsCache.empty() && "Cache should have been cleared");
75
76 KnownBits Known;
77 computeKnownBitsImpl(R, Known, DemandedElts, Depth);
78 ComputeKnownBitsCache.clear();
79 return Known;
80}
81
83 LLT Ty = MRI.getType(R);
84 unsigned BitWidth = Ty.getScalarSizeInBits();
86}
87
89 return getKnownBits(R).Zero;
90}
91
93
94LLVM_ATTRIBUTE_UNUSED static void
95dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) {
96 dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth
97 << "] Computed for: " << MI << "[" << Depth << "] Known: 0x"
98 << toString(Known.Zero | Known.One, 16, false) << "\n"
99 << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false)
100 << "\n"
101 << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false)
102 << "\n";
103}
104
105/// Compute known bits for the intersection of \p Src0 and \p Src1
106void GISelKnownBits::computeKnownBitsMin(Register Src0, Register Src1,
107 KnownBits &Known,
108 const APInt &DemandedElts,
109 unsigned Depth) {
110 // Test src1 first, since we canonicalize simpler expressions to the RHS.
111 computeKnownBitsImpl(Src1, Known, DemandedElts, Depth);
112
113 // If we don't know any bits, early out.
114 if (Known.isUnknown())
115 return;
116
117 KnownBits Known2;
118 computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
119
120 // Only known if known in both the LHS and RHS.
121 Known = Known.intersectWith(Known2);
122}
123
124// Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
125// created using Width. Use this function when the inputs are KnownBits
126// objects. TODO: Move this KnownBits.h if this is usable in more cases.
127static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown,
128 const KnownBits &OffsetKnown,
129 const KnownBits &WidthKnown) {
130 KnownBits Mask(BitWidth);
131 Mask.Zero = APInt::getBitsSetFrom(
133 Mask.One = APInt::getLowBitsSet(
135 return KnownBits::lshr(SrcOpKnown, OffsetKnown) & Mask;
136}
137
139 const APInt &DemandedElts,
140 unsigned Depth) {
141 MachineInstr &MI = *MRI.getVRegDef(R);
142 unsigned Opcode = MI.getOpcode();
143 LLT DstTy = MRI.getType(R);
144
145 // Handle the case where this is called on a register that does not have a
146 // type constraint (i.e. it has a register class constraint instead). This is
147 // unlikely to occur except by looking through copies but it is possible for
148 // the initial register being queried to be in this state.
149 if (!DstTy.isValid()) {
150 Known = KnownBits();
151 return;
152 }
153
154 unsigned BitWidth = DstTy.getScalarSizeInBits();
155 auto CacheEntry = ComputeKnownBitsCache.find(R);
156 if (CacheEntry != ComputeKnownBitsCache.end()) {
157 Known = CacheEntry->second;
158 LLVM_DEBUG(dbgs() << "Cache hit at ");
159 LLVM_DEBUG(dumpResult(MI, Known, Depth));
160 assert(Known.getBitWidth() == BitWidth && "Cache entry size doesn't match");
161 return;
162 }
163 Known = KnownBits(BitWidth); // Don't know anything
164
165 // Depth may get bigger than max depth if it gets passed to a different
166 // GISelKnownBits object.
167 // This may happen when say a generic part uses a GISelKnownBits object
168 // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr
169 // which creates a new GISelKnownBits object with a different and smaller
170 // depth. If we just check for equality, we would never exit if the depth
171 // that is passed down to the target specific GISelKnownBits object is
172 // already bigger than its max depth.
173 if (Depth >= getMaxDepth())
174 return;
175
176 if (!DemandedElts)
177 return; // No demanded elts, better to assume we don't know anything.
178
179 KnownBits Known2;
180
181 switch (Opcode) {
182 default:
183 TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI,
184 Depth);
185 break;
186 case TargetOpcode::G_BUILD_VECTOR: {
187 // Collect the known bits that are shared by every demanded vector element.
188 Known.Zero.setAllBits(); Known.One.setAllBits();
189 for (unsigned i = 0, e = MI.getNumOperands() - 1; i < e; ++i) {
190 if (!DemandedElts[i])
191 continue;
192
193 computeKnownBitsImpl(MI.getOperand(i + 1).getReg(), Known2, DemandedElts,
194 Depth + 1);
195
196 // Known bits are the values that are shared by every demanded element.
197 Known = Known.intersectWith(Known2);
198
199 // If we don't know any bits, early out.
200 if (Known.isUnknown())
201 break;
202 }
203 break;
204 }
205 case TargetOpcode::COPY:
206 case TargetOpcode::G_PHI:
207 case TargetOpcode::PHI: {
210 // Destination registers should not have subregisters at this
211 // point of the pipeline, otherwise the main live-range will be
212 // defined more than once, which is against SSA.
213 assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?");
214 // Record in the cache that we know nothing for MI.
215 // This will get updated later and in the meantime, if we reach that
216 // phi again, because of a loop, we will cut the search thanks to this
217 // cache entry.
218 // We could actually build up more information on the phi by not cutting
219 // the search, but that additional information is more a side effect
220 // than an intended choice.
221 // Therefore, for now, save on compile time until we derive a proper way
222 // to derive known bits for PHIs within loops.
223 ComputeKnownBitsCache[R] = KnownBits(BitWidth);
224 // PHI's operand are a mix of registers and basic blocks interleaved.
225 // We only care about the register ones.
226 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
227 const MachineOperand &Src = MI.getOperand(Idx);
228 Register SrcReg = Src.getReg();
229 // Look through trivial copies and phis but don't look through trivial
230 // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits
231 // analysis is currently unable to determine the bit width of a
232 // register class.
233 //
234 // We can't use NoSubRegister by name as it's defined by each target but
235 // it's always defined to be 0 by tablegen.
236 if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ &&
237 MRI.getType(SrcReg).isValid()) {
238 // For COPYs we don't do anything, don't increase the depth.
239 computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
240 Depth + (Opcode != TargetOpcode::COPY));
241 Known = Known.intersectWith(Known2);
242 // If we reach a point where we don't know anything
243 // just stop looking through the operands.
244 if (Known.isUnknown())
245 break;
246 } else {
247 // We know nothing.
248 Known = KnownBits(BitWidth);
249 break;
250 }
251 }
252 break;
253 }
254 case TargetOpcode::G_CONSTANT: {
255 auto CstVal = getIConstantVRegVal(R, MRI);
256 if (!CstVal)
257 break;
258 Known = KnownBits::makeConstant(*CstVal);
259 break;
260 }
261 case TargetOpcode::G_FRAME_INDEX: {
262 int FrameIdx = MI.getOperand(1).getIndex();
263 TL.computeKnownBitsForFrameIndex(FrameIdx, Known, MF);
264 break;
265 }
266 case TargetOpcode::G_SUB: {
267 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
268 Depth + 1);
269 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
270 Depth + 1);
271 Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false, Known,
272 Known2);
273 break;
274 }
275 case TargetOpcode::G_XOR: {
276 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
277 Depth + 1);
278 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
279 Depth + 1);
280
281 Known ^= Known2;
282 break;
283 }
284 case TargetOpcode::G_PTR_ADD: {
285 if (DstTy.isVector())
286 break;
287 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets?
288 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
290 break;
291 [[fallthrough]];
292 }
293 case TargetOpcode::G_ADD: {
294 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
295 Depth + 1);
296 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
297 Depth + 1);
298 Known =
299 KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false, Known, Known2);
300 break;
301 }
302 case TargetOpcode::G_AND: {
303 // If either the LHS or the RHS are Zero, the result is zero.
304 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
305 Depth + 1);
306 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
307 Depth + 1);
308
309 Known &= Known2;
310 break;
311 }
312 case TargetOpcode::G_OR: {
313 // If either the LHS or the RHS are Zero, the result is zero.
314 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
315 Depth + 1);
316 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
317 Depth + 1);
318
319 Known |= Known2;
320 break;
321 }
322 case TargetOpcode::G_MUL: {
323 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
324 Depth + 1);
325 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
326 Depth + 1);
327 Known = KnownBits::mul(Known, Known2);
328 break;
329 }
330 case TargetOpcode::G_SELECT: {
331 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
332 Known, DemandedElts, Depth + 1);
333 break;
334 }
335 case TargetOpcode::G_SMIN: {
336 // TODO: Handle clamp pattern with number of sign bits
337 KnownBits KnownRHS;
338 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
339 Depth + 1);
340 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
341 Depth + 1);
342 Known = KnownBits::smin(Known, KnownRHS);
343 break;
344 }
345 case TargetOpcode::G_SMAX: {
346 // TODO: Handle clamp pattern with number of sign bits
347 KnownBits KnownRHS;
348 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
349 Depth + 1);
350 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
351 Depth + 1);
352 Known = KnownBits::smax(Known, KnownRHS);
353 break;
354 }
355 case TargetOpcode::G_UMIN: {
356 KnownBits KnownRHS;
357 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known,
358 DemandedElts, Depth + 1);
359 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS,
360 DemandedElts, Depth + 1);
361 Known = KnownBits::umin(Known, KnownRHS);
362 break;
363 }
364 case TargetOpcode::G_UMAX: {
365 KnownBits KnownRHS;
366 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known,
367 DemandedElts, Depth + 1);
368 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS,
369 DemandedElts, Depth + 1);
370 Known = KnownBits::umax(Known, KnownRHS);
371 break;
372 }
373 case TargetOpcode::G_FCMP:
374 case TargetOpcode::G_ICMP: {
375 if (DstTy.isVector())
376 break;
377 if (TL.getBooleanContents(DstTy.isVector(),
378 Opcode == TargetOpcode::G_FCMP) ==
380 BitWidth > 1)
381 Known.Zero.setBitsFrom(1);
382 break;
383 }
384 case TargetOpcode::G_SEXT: {
385 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
386 Depth + 1);
387 // If the sign bit is known to be zero or one, then sext will extend
388 // it to the top bits, else it will just zext.
389 Known = Known.sext(BitWidth);
390 break;
391 }
392 case TargetOpcode::G_ASSERT_SEXT:
393 case TargetOpcode::G_SEXT_INREG: {
394 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
395 Depth + 1);
396 Known = Known.sextInReg(MI.getOperand(2).getImm());
397 break;
398 }
399 case TargetOpcode::G_ANYEXT: {
400 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
401 Depth + 1);
402 Known = Known.anyext(BitWidth);
403 break;
404 }
405 case TargetOpcode::G_LOAD: {
406 const MachineMemOperand *MMO = *MI.memoperands_begin();
407 if (const MDNode *Ranges = MMO->getRanges()) {
408 computeKnownBitsFromRangeMetadata(*Ranges, Known);
409 }
410
411 break;
412 }
413 case TargetOpcode::G_ZEXTLOAD: {
414 if (DstTy.isVector())
415 break;
416 // Everything above the retrieved bits is zero
417 Known.Zero.setBitsFrom((*MI.memoperands_begin())->getSizeInBits());
418 break;
419 }
420 case TargetOpcode::G_ASHR: {
421 KnownBits LHSKnown, RHSKnown;
422 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
423 Depth + 1);
424 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
425 Depth + 1);
426 Known = KnownBits::ashr(LHSKnown, RHSKnown);
427 break;
428 }
429 case TargetOpcode::G_LSHR: {
430 KnownBits LHSKnown, RHSKnown;
431 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
432 Depth + 1);
433 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
434 Depth + 1);
435 Known = KnownBits::lshr(LHSKnown, RHSKnown);
436 break;
437 }
438 case TargetOpcode::G_SHL: {
439 KnownBits LHSKnown, RHSKnown;
440 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
441 Depth + 1);
442 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
443 Depth + 1);
444 Known = KnownBits::shl(LHSKnown, RHSKnown);
445 break;
446 }
447 case TargetOpcode::G_INTTOPTR:
448 case TargetOpcode::G_PTRTOINT:
449 if (DstTy.isVector())
450 break;
451 // Fall through and handle them the same as zext/trunc.
452 [[fallthrough]];
453 case TargetOpcode::G_ASSERT_ZEXT:
454 case TargetOpcode::G_ZEXT:
455 case TargetOpcode::G_TRUNC: {
456 Register SrcReg = MI.getOperand(1).getReg();
457 LLT SrcTy = MRI.getType(SrcReg);
458 unsigned SrcBitWidth;
459
460 // G_ASSERT_ZEXT stores the original bitwidth in the immediate operand.
461 if (Opcode == TargetOpcode::G_ASSERT_ZEXT)
462 SrcBitWidth = MI.getOperand(2).getImm();
463 else {
464 SrcBitWidth = SrcTy.isPointer()
466 : SrcTy.getSizeInBits();
467 }
468 assert(SrcBitWidth && "SrcBitWidth can't be zero");
469 Known = Known.zextOrTrunc(SrcBitWidth);
470 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
471 Known = Known.zextOrTrunc(BitWidth);
472 if (BitWidth > SrcBitWidth)
473 Known.Zero.setBitsFrom(SrcBitWidth);
474 break;
475 }
476 case TargetOpcode::G_ASSERT_ALIGN: {
477 int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
478
479 // TODO: Should use maximum with source
480 // If a node is guaranteed to be aligned, set low zero bits accordingly as
481 // well as clearing one bits.
482 Known.Zero.setLowBits(LogOfAlign);
483 Known.One.clearLowBits(LogOfAlign);
484 break;
485 }
486 case TargetOpcode::G_MERGE_VALUES: {
487 unsigned NumOps = MI.getNumOperands();
488 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
489
490 for (unsigned I = 0; I != NumOps - 1; ++I) {
491 KnownBits SrcOpKnown;
492 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
493 DemandedElts, Depth + 1);
494 Known.insertBits(SrcOpKnown, I * OpSize);
495 }
496 break;
497 }
498 case TargetOpcode::G_UNMERGE_VALUES: {
499 if (DstTy.isVector())
500 break;
501 unsigned NumOps = MI.getNumOperands();
502 Register SrcReg = MI.getOperand(NumOps - 1).getReg();
503 if (MRI.getType(SrcReg).isVector())
504 return; // TODO: Handle vectors.
505
506 KnownBits SrcOpKnown;
507 computeKnownBitsImpl(SrcReg, SrcOpKnown, DemandedElts, Depth + 1);
508
509 // Figure out the result operand index
510 unsigned DstIdx = 0;
511 for (; DstIdx != NumOps - 1 && MI.getOperand(DstIdx).getReg() != R;
512 ++DstIdx)
513 ;
514
515 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
516 break;
517 }
518 case TargetOpcode::G_BSWAP: {
519 Register SrcReg = MI.getOperand(1).getReg();
520 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
521 Known = Known.byteSwap();
522 break;
523 }
524 case TargetOpcode::G_BITREVERSE: {
525 Register SrcReg = MI.getOperand(1).getReg();
526 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
527 Known = Known.reverseBits();
528 break;
529 }
530 case TargetOpcode::G_CTPOP: {
531 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
532 Depth + 1);
533 // We can bound the space the count needs. Also, bits known to be zero can't
534 // contribute to the population.
535 unsigned BitsPossiblySet = Known2.countMaxPopulation();
536 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
537 Known.Zero.setBitsFrom(LowBits);
538 // TODO: we could bound Known.One using the lower bound on the number of
539 // bits which might be set provided by popcnt KnownOne2.
540 break;
541 }
542 case TargetOpcode::G_UBFX: {
543 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
544 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
545 Depth + 1);
546 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
547 Depth + 1);
548 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
549 Depth + 1);
550 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
551 break;
552 }
553 case TargetOpcode::G_SBFX: {
554 KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
555 computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
556 Depth + 1);
557 computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
558 Depth + 1);
559 computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
560 Depth + 1);
561 Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
562 // Sign extend the extracted value using shift left and arithmetic shift
563 // right.
566 /*Add*/ false, /*NSW*/ false, ExtKnown, WidthKnown);
567 Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
568 break;
569 }
570 case TargetOpcode::G_UADDO:
571 case TargetOpcode::G_UADDE:
572 case TargetOpcode::G_SADDO:
573 case TargetOpcode::G_SADDE:
574 case TargetOpcode::G_USUBO:
575 case TargetOpcode::G_USUBE:
576 case TargetOpcode::G_SSUBO:
577 case TargetOpcode::G_SSUBE:
578 case TargetOpcode::G_UMULO:
579 case TargetOpcode::G_SMULO: {
580 if (MI.getOperand(1).getReg() == R) {
581 // If we know the result of a compare has the top bits zero, use this
582 // info.
583 if (TL.getBooleanContents(DstTy.isVector(), false) ==
585 BitWidth > 1)
586 Known.Zero.setBitsFrom(1);
587 }
588 break;
589 }
590 }
591
592 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
593 LLVM_DEBUG(dumpResult(MI, Known, Depth));
594
595 // Update the cache.
596 ComputeKnownBitsCache[R] = Known;
597}
598
599/// Compute number of sign bits for the intersection of \p Src0 and \p Src1
600unsigned GISelKnownBits::computeNumSignBitsMin(Register Src0, Register Src1,
601 const APInt &DemandedElts,
602 unsigned Depth) {
603 // Test src1 first, since we canonicalize simpler expressions to the RHS.
604 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
605 if (Src1SignBits == 1)
606 return 1;
607 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
608}
609
611 const APInt &DemandedElts,
612 unsigned Depth) {
613 MachineInstr &MI = *MRI.getVRegDef(R);
614 unsigned Opcode = MI.getOpcode();
615
616 if (Opcode == TargetOpcode::G_CONSTANT)
617 return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
618
619 if (Depth == getMaxDepth())
620 return 1;
621
622 if (!DemandedElts)
623 return 1; // No demanded elts, better to assume we don't know anything.
624
625 LLT DstTy = MRI.getType(R);
626 const unsigned TyBits = DstTy.getScalarSizeInBits();
627
628 // Handle the case where this is called on a register that does not have a
629 // type constraint. This is unlikely to occur except by looking through copies
630 // but it is possible for the initial register being queried to be in this
631 // state.
632 if (!DstTy.isValid())
633 return 1;
634
635 unsigned FirstAnswer = 1;
636 switch (Opcode) {
637 case TargetOpcode::COPY: {
638 MachineOperand &Src = MI.getOperand(1);
639 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
640 MRI.getType(Src.getReg()).isValid()) {
641 // Don't increment Depth for this one since we didn't do any work.
642 return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
643 }
644
645 return 1;
646 }
647 case TargetOpcode::G_SEXT: {
648 Register Src = MI.getOperand(1).getReg();
649 LLT SrcTy = MRI.getType(Src);
650 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
651 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
652 }
653 case TargetOpcode::G_ASSERT_SEXT:
654 case TargetOpcode::G_SEXT_INREG: {
655 // Max of the input and what this extends.
656 Register Src = MI.getOperand(1).getReg();
657 unsigned SrcBits = MI.getOperand(2).getImm();
658 unsigned InRegBits = TyBits - SrcBits + 1;
659 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1), InRegBits);
660 }
661 case TargetOpcode::G_SEXTLOAD: {
662 // FIXME: We need an in-memory type representation.
663 if (DstTy.isVector())
664 return 1;
665
666 // e.g. i16->i32 = '17' bits known.
667 const MachineMemOperand *MMO = *MI.memoperands_begin();
668 return TyBits - MMO->getSizeInBits() + 1;
669 }
670 case TargetOpcode::G_ZEXTLOAD: {
671 // FIXME: We need an in-memory type representation.
672 if (DstTy.isVector())
673 return 1;
674
675 // e.g. i16->i32 = '16' bits known.
676 const MachineMemOperand *MMO = *MI.memoperands_begin();
677 return TyBits - MMO->getSizeInBits();
678 }
679 case TargetOpcode::G_TRUNC: {
680 Register Src = MI.getOperand(1).getReg();
681 LLT SrcTy = MRI.getType(Src);
682
683 // Check if the sign bits of source go down as far as the truncated value.
684 unsigned DstTyBits = DstTy.getScalarSizeInBits();
685 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
686 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
687 if (NumSrcSignBits > (NumSrcBits - DstTyBits))
688 return NumSrcSignBits - (NumSrcBits - DstTyBits);
689 break;
690 }
691 case TargetOpcode::G_SELECT: {
692 return computeNumSignBitsMin(MI.getOperand(2).getReg(),
693 MI.getOperand(3).getReg(), DemandedElts,
694 Depth + 1);
695 }
696 case TargetOpcode::G_SADDO:
697 case TargetOpcode::G_SADDE:
698 case TargetOpcode::G_UADDO:
699 case TargetOpcode::G_UADDE:
700 case TargetOpcode::G_SSUBO:
701 case TargetOpcode::G_SSUBE:
702 case TargetOpcode::G_USUBO:
703 case TargetOpcode::G_USUBE:
704 case TargetOpcode::G_SMULO:
705 case TargetOpcode::G_UMULO: {
706 // If compares returns 0/-1, all bits are sign bits.
707 // We know that we have an integer-based boolean since these operations
708 // are only available for integer.
709 if (MI.getOperand(1).getReg() == R) {
710 if (TL.getBooleanContents(DstTy.isVector(), false) ==
712 return TyBits;
713 }
714
715 break;
716 }
717 case TargetOpcode::G_FCMP:
718 case TargetOpcode::G_ICMP: {
719 bool IsFP = Opcode == TargetOpcode::G_FCMP;
720 if (TyBits == 1)
721 break;
722 auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
724 return TyBits; // All bits are sign bits.
726 return TyBits - 1; // Every always-zero bit is a sign bit.
727 break;
728 }
729 case TargetOpcode::G_INTRINSIC:
730 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
731 case TargetOpcode::G_INTRINSIC_CONVERGENT:
732 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS:
733 default: {
734 unsigned NumBits =
735 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
736 if (NumBits > 1)
737 FirstAnswer = std::max(FirstAnswer, NumBits);
738 break;
739 }
740 }
741
742 // Finally, if we can prove that the top bits of the result are 0's or 1's,
743 // use this information.
744 KnownBits Known = getKnownBits(R, DemandedElts, Depth);
745 APInt Mask;
746 if (Known.isNonNegative()) { // sign bit is 0
747 Mask = Known.Zero;
748 } else if (Known.isNegative()) { // sign bit is 1;
749 Mask = Known.One;
750 } else {
751 // Nothing known.
752 return FirstAnswer;
753 }
754
755 // Okay, we know that the sign bit in Mask is set. Use CLO to determine
756 // the number of identical bits in the top of the input value.
757 Mask <<= Mask.getBitWidth() - TyBits;
758 return std::max(FirstAnswer, Mask.countl_one());
759}
760
762 LLT Ty = MRI.getType(R);
763 APInt DemandedElts =
764 Ty.isVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
765 return computeNumSignBits(R, DemandedElts, Depth);
766}
767
769 AU.setPreservesAll();
771}
772
774 return false;
775}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:184
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:236
static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown, const KnownBits &OffsetKnown, const KnownBits &WidthKnown)
#define DEBUG_TYPE
Provides analysis for querying information about KnownBits during GISel passes.
static LLVM_ATTRIBUTE_UNUSED void dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth)
Provides analysis for querying information about KnownBits during GISel passes.
IRTranslator LLVM IR MI
static const unsigned MaxDepth
#define I(x, y, z)
Definition: MD5.cpp:58
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
This file describes how to lower LLVM code to machine code.
Class for arbitrary precision integers.
Definition: APInt.h:76
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:207
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1358
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1389
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:453
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1291
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1361
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool isNonIntegralAddressSpace(unsigned AddrSpace) const
Definition: DataLayout.h:393
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
Definition: DataLayout.h:420
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
Align computeKnownAlignment(Register R, unsigned Depth=0)
APInt getKnownOnes(Register R)
unsigned computeNumSignBits(Register R, const APInt &DemandedElts, unsigned Depth=0)
KnownBits getKnownBits(Register R)
bool maskedValueIsZero(Register Val, const APInt &Mask)
unsigned getMaxDepth() const
bool signBitIsZero(Register Op)
APInt getKnownZeroes(Register R)
constexpr unsigned getScalarSizeInBits() const
Definition: LowLevelType.h:249
constexpr bool isValid() const
Definition: LowLevelType.h:137
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
Definition: LowLevelType.h:149
constexpr bool isVector() const
Definition: LowLevelType.h:145
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
Definition: LowLevelType.h:175
constexpr bool isPointer() const
Definition: LowLevelType.h:141
constexpr unsigned getAddressSpace() const
Definition: LowLevelType.h:262
Metadata node.
Definition: Metadata.h:950
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Representation of each machine instruction.
Definition: MachineInstr.h:68
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
uint64_t getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
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:91
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
virtual unsigned computeNumSignBitsForTargetInstr(GISelKnownBits &Analysis, Register R, const APInt &DemandedElts, const MachineRegisterInfo &MRI, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
virtual Align computeKnownAlignForTargetInstr(GISelKnownBits &Analysis, Register R, const MachineRegisterInfo &MRI, unsigned Depth=0) const
Determine the known alignment for the pointer value R.
virtual void computeKnownBitsForTargetInstr(GISelKnownBits &Analysis, Register R, KnownBits &Known, const APInt &DemandedElts, const MachineRegisterInfo &MRI, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
Definition: Utils.cpp:292
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:281
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:319
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:184
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:292
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:88
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:141
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:99
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
KnownBits byteSwap() const
Definition: KnownBits.h:441
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:280
KnownBits reverseBits() const
Definition: KnownBits.h:445
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:117
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:216
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:302
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:171
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:187
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:136
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:259
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:154
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition: KnownBits.h:120
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:305
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:96
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:210
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:635
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:158
static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS, KnownBits RHS)
Compute known bits resulting from adding LHS and RHS.
Definition: KnownBits.cpp:57
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:174
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:135