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