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 //===------------------ 13 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h" 14 #include "llvm/Analysis/ValueTracking.h" 15 #include "llvm/CodeGen/GlobalISel/Utils.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/CodeGen/MachineRegisterInfo.h" 18 #include "llvm/CodeGen/TargetLowering.h" 19 #include "llvm/CodeGen/TargetOpcodes.h" 20 21 #define DEBUG_TYPE "gisel-known-bits" 22 23 using namespace llvm; 24 25 char llvm::GISelKnownBitsAnalysis::ID = 0; 26 27 INITIALIZE_PASS(GISelKnownBitsAnalysis, DEBUG_TYPE, 28 "Analysis for ComputingKnownBits", false, true) 29 30 GISelKnownBits::GISelKnownBits(MachineFunction &MF, unsigned MaxDepth) 31 : MF(MF), MRI(MF.getRegInfo()), TL(*MF.getSubtarget().getTargetLowering()), 32 DL(MF.getFunction().getParent()->getDataLayout()), MaxDepth(MaxDepth) {} 33 34 Align GISelKnownBits::computeKnownAlignment(Register R, unsigned Depth) { 35 const MachineInstr *MI = MRI.getVRegDef(R); 36 switch (MI->getOpcode()) { 37 case TargetOpcode::COPY: 38 return computeKnownAlignment(MI->getOperand(1).getReg(), Depth); 39 case TargetOpcode::G_FRAME_INDEX: { 40 int FrameIdx = MI->getOperand(1).getIndex(); 41 return MF.getFrameInfo().getObjectAlign(FrameIdx); 42 } 43 case TargetOpcode::G_INTRINSIC: 44 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: 45 default: 46 return TL.computeKnownAlignForTargetInstr(*this, R, MRI, Depth + 1); 47 } 48 } 49 50 KnownBits GISelKnownBits::getKnownBits(MachineInstr &MI) { 51 assert(MI.getNumExplicitDefs() == 1 && 52 "expected single return generic instruction"); 53 return getKnownBits(MI.getOperand(0).getReg()); 54 } 55 56 KnownBits GISelKnownBits::getKnownBits(Register R) { 57 const LLT Ty = MRI.getType(R); 58 APInt DemandedElts = 59 Ty.isVector() ? APInt::getAllOnesValue(Ty.getNumElements()) : APInt(1, 1); 60 return getKnownBits(R, DemandedElts); 61 } 62 63 KnownBits GISelKnownBits::getKnownBits(Register R, const APInt &DemandedElts, 64 unsigned Depth) { 65 // For now, we only maintain the cache during one request. 66 assert(ComputeKnownBitsCache.empty() && "Cache should have been cleared"); 67 68 KnownBits Known; 69 computeKnownBitsImpl(R, Known, DemandedElts); 70 ComputeKnownBitsCache.clear(); 71 return Known; 72 } 73 74 bool GISelKnownBits::signBitIsZero(Register R) { 75 LLT Ty = MRI.getType(R); 76 unsigned BitWidth = Ty.getScalarSizeInBits(); 77 return maskedValueIsZero(R, APInt::getSignMask(BitWidth)); 78 } 79 80 APInt GISelKnownBits::getKnownZeroes(Register R) { 81 return getKnownBits(R).Zero; 82 } 83 84 APInt GISelKnownBits::getKnownOnes(Register R) { return getKnownBits(R).One; } 85 86 LLVM_ATTRIBUTE_UNUSED static void 87 dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) { 88 dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth 89 << "] Computed for: " << MI << "[" << Depth << "] Known: 0x" 90 << (Known.Zero | Known.One).toString(16, false) << "\n" 91 << "[" << Depth << "] Zero: 0x" << Known.Zero.toString(16, false) 92 << "\n" 93 << "[" << Depth << "] One: 0x" << Known.One.toString(16, false) 94 << "\n"; 95 } 96 97 /// Compute known bits for the intersection of \p Src0 and \p Src1 98 void GISelKnownBits::computeKnownBitsMin(Register Src0, Register Src1, 99 KnownBits &Known, 100 const APInt &DemandedElts, 101 unsigned Depth) { 102 // Test src1 first, since we canonicalize simpler expressions to the RHS. 103 computeKnownBitsImpl(Src1, Known, DemandedElts, Depth); 104 105 // If we don't know any bits, early out. 106 if (Known.isUnknown()) 107 return; 108 109 KnownBits Known2; 110 computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth); 111 112 // Only known if known in both the LHS and RHS. 113 Known.Zero &= Known2.Zero; 114 Known.One &= Known2.One; 115 } 116 117 void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known, 118 const APInt &DemandedElts, 119 unsigned Depth) { 120 MachineInstr &MI = *MRI.getVRegDef(R); 121 unsigned Opcode = MI.getOpcode(); 122 LLT DstTy = MRI.getType(R); 123 124 // Handle the case where this is called on a register that does not have a 125 // type constraint (i.e. it has a register class constraint instead). This is 126 // unlikely to occur except by looking through copies but it is possible for 127 // the initial register being queried to be in this state. 128 if (!DstTy.isValid()) { 129 Known = KnownBits(); 130 return; 131 } 132 133 unsigned BitWidth = DstTy.getSizeInBits(); 134 auto CacheEntry = ComputeKnownBitsCache.find(R); 135 if (CacheEntry != ComputeKnownBitsCache.end()) { 136 Known = CacheEntry->second; 137 LLVM_DEBUG(dbgs() << "Cache hit at "); 138 LLVM_DEBUG(dumpResult(MI, Known, Depth)); 139 assert(Known.getBitWidth() == BitWidth && "Cache entry size doesn't match"); 140 return; 141 } 142 Known = KnownBits(BitWidth); // Don't know anything 143 144 if (DstTy.isVector()) 145 return; // TODO: Handle vectors. 146 147 // Depth may get bigger than max depth if it gets passed to a different 148 // GISelKnownBits object. 149 // This may happen when say a generic part uses a GISelKnownBits object 150 // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr 151 // which creates a new GISelKnownBits object with a different and smaller 152 // depth. If we just check for equality, we would never exit if the depth 153 // that is passed down to the target specific GISelKnownBits object is 154 // already bigger than its max depth. 155 if (Depth >= getMaxDepth()) 156 return; 157 158 if (!DemandedElts) 159 return; // No demanded elts, better to assume we don't know anything. 160 161 KnownBits Known2; 162 163 switch (Opcode) { 164 default: 165 TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI, 166 Depth); 167 break; 168 case TargetOpcode::COPY: 169 case TargetOpcode::G_PHI: 170 case TargetOpcode::PHI: { 171 Known.One = APInt::getAllOnesValue(BitWidth); 172 Known.Zero = APInt::getAllOnesValue(BitWidth); 173 // Destination registers should not have subregisters at this 174 // point of the pipeline, otherwise the main live-range will be 175 // defined more than once, which is against SSA. 176 assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?"); 177 // Record in the cache that we know nothing for MI. 178 // This will get updated later and in the meantime, if we reach that 179 // phi again, because of a loop, we will cut the search thanks to this 180 // cache entry. 181 // We could actually build up more information on the phi by not cutting 182 // the search, but that additional information is more a side effect 183 // than an intended choice. 184 // Therefore, for now, save on compile time until we derive a proper way 185 // to derive known bits for PHIs within loops. 186 ComputeKnownBitsCache[R] = KnownBits(BitWidth); 187 // PHI's operand are a mix of registers and basic blocks interleaved. 188 // We only care about the register ones. 189 for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) { 190 const MachineOperand &Src = MI.getOperand(Idx); 191 Register SrcReg = Src.getReg(); 192 // Look through trivial copies and phis but don't look through trivial 193 // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits 194 // analysis is currently unable to determine the bit width of a 195 // register class. 196 // 197 // We can't use NoSubRegister by name as it's defined by each target but 198 // it's always defined to be 0 by tablegen. 199 if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ && 200 MRI.getType(SrcReg).isValid()) { 201 // For COPYs we don't do anything, don't increase the depth. 202 computeKnownBitsImpl(SrcReg, Known2, DemandedElts, 203 Depth + (Opcode != TargetOpcode::COPY)); 204 Known.One &= Known2.One; 205 Known.Zero &= Known2.Zero; 206 // If we reach a point where we don't know anything 207 // just stop looking through the operands. 208 if (Known.One == 0 && Known.Zero == 0) 209 break; 210 } else { 211 // We know nothing. 212 Known = KnownBits(BitWidth); 213 break; 214 } 215 } 216 break; 217 } 218 case TargetOpcode::G_CONSTANT: { 219 auto CstVal = getConstantVRegVal(R, MRI); 220 if (!CstVal) 221 break; 222 Known.One = *CstVal; 223 Known.Zero = ~Known.One; 224 break; 225 } 226 case TargetOpcode::G_FRAME_INDEX: { 227 int FrameIdx = MI.getOperand(1).getIndex(); 228 TL.computeKnownBitsForFrameIndex(FrameIdx, Known, MF); 229 break; 230 } 231 case TargetOpcode::G_SUB: { 232 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 233 Depth + 1); 234 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts, 235 Depth + 1); 236 Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false, Known, 237 Known2); 238 break; 239 } 240 case TargetOpcode::G_XOR: { 241 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 242 Depth + 1); 243 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 244 Depth + 1); 245 246 Known ^= Known2; 247 break; 248 } 249 case TargetOpcode::G_PTR_ADD: { 250 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets? 251 LLT Ty = MRI.getType(MI.getOperand(1).getReg()); 252 if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace())) 253 break; 254 LLVM_FALLTHROUGH; 255 } 256 case TargetOpcode::G_ADD: { 257 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 258 Depth + 1); 259 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts, 260 Depth + 1); 261 Known = 262 KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false, Known, Known2); 263 break; 264 } 265 case TargetOpcode::G_AND: { 266 // If either the LHS or the RHS are Zero, the result is zero. 267 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 268 Depth + 1); 269 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 270 Depth + 1); 271 272 Known &= Known2; 273 break; 274 } 275 case TargetOpcode::G_OR: { 276 // If either the LHS or the RHS are Zero, the result is zero. 277 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 278 Depth + 1); 279 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 280 Depth + 1); 281 282 Known |= Known2; 283 break; 284 } 285 case TargetOpcode::G_MUL: { 286 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 287 Depth + 1); 288 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 289 Depth + 1); 290 // If low bits are zero in either operand, output low known-0 bits. 291 // Also compute a conservative estimate for high known-0 bits. 292 // More trickiness is possible, but this is sufficient for the 293 // interesting case of alignment computation. 294 unsigned TrailZ = 295 Known.countMinTrailingZeros() + Known2.countMinTrailingZeros(); 296 unsigned LeadZ = 297 std::max(Known.countMinLeadingZeros() + Known2.countMinLeadingZeros(), 298 BitWidth) - 299 BitWidth; 300 301 Known.resetAll(); 302 Known.Zero.setLowBits(std::min(TrailZ, BitWidth)); 303 Known.Zero.setHighBits(std::min(LeadZ, BitWidth)); 304 break; 305 } 306 case TargetOpcode::G_SELECT: { 307 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(), 308 Known, DemandedElts, Depth + 1); 309 break; 310 } 311 case TargetOpcode::G_SMIN: { 312 // TODO: Handle clamp pattern with number of sign bits 313 KnownBits KnownRHS; 314 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 315 Depth + 1); 316 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts, 317 Depth + 1); 318 Known = KnownBits::smin(Known, KnownRHS); 319 break; 320 } 321 case TargetOpcode::G_SMAX: { 322 // TODO: Handle clamp pattern with number of sign bits 323 KnownBits KnownRHS; 324 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 325 Depth + 1); 326 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts, 327 Depth + 1); 328 Known = KnownBits::smax(Known, KnownRHS); 329 break; 330 } 331 case TargetOpcode::G_UMIN: { 332 KnownBits KnownRHS; 333 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, 334 DemandedElts, Depth + 1); 335 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, 336 DemandedElts, Depth + 1); 337 Known = KnownBits::umin(Known, KnownRHS); 338 break; 339 } 340 case TargetOpcode::G_UMAX: { 341 KnownBits KnownRHS; 342 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, 343 DemandedElts, Depth + 1); 344 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, 345 DemandedElts, Depth + 1); 346 Known = KnownBits::umax(Known, KnownRHS); 347 break; 348 } 349 case TargetOpcode::G_FCMP: 350 case TargetOpcode::G_ICMP: { 351 if (TL.getBooleanContents(DstTy.isVector(), 352 Opcode == TargetOpcode::G_FCMP) == 353 TargetLowering::ZeroOrOneBooleanContent && 354 BitWidth > 1) 355 Known.Zero.setBitsFrom(1); 356 break; 357 } 358 case TargetOpcode::G_SEXT: { 359 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 360 Depth + 1); 361 // If the sign bit is known to be zero or one, then sext will extend 362 // it to the top bits, else it will just zext. 363 Known = Known.sext(BitWidth); 364 break; 365 } 366 case TargetOpcode::G_ANYEXT: { 367 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 368 Depth + 1); 369 Known = Known.anyext(BitWidth); 370 break; 371 } 372 case TargetOpcode::G_LOAD: { 373 const MachineMemOperand *MMO = *MI.memoperands_begin(); 374 if (const MDNode *Ranges = MMO->getRanges()) { 375 computeKnownBitsFromRangeMetadata(*Ranges, Known); 376 } 377 378 break; 379 } 380 case TargetOpcode::G_ZEXTLOAD: { 381 // Everything above the retrieved bits is zero 382 Known.Zero.setBitsFrom((*MI.memoperands_begin())->getSizeInBits()); 383 break; 384 } 385 case TargetOpcode::G_ASHR: 386 case TargetOpcode::G_LSHR: 387 case TargetOpcode::G_SHL: { 388 KnownBits RHSKnown; 389 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts, 390 Depth + 1); 391 if (!RHSKnown.isConstant()) { 392 LLVM_DEBUG( 393 MachineInstr *RHSMI = MRI.getVRegDef(MI.getOperand(2).getReg()); 394 dbgs() << '[' << Depth << "] Shift not known constant: " << *RHSMI); 395 break; 396 } 397 uint64_t Shift = RHSKnown.getConstant().getZExtValue(); 398 LLVM_DEBUG(dbgs() << '[' << Depth << "] Shift is " << Shift << '\n'); 399 400 // Guard against oversized shift amounts 401 if (Shift >= MRI.getType(MI.getOperand(1).getReg()).getScalarSizeInBits()) 402 break; 403 404 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 405 Depth + 1); 406 407 switch (Opcode) { 408 case TargetOpcode::G_ASHR: 409 Known.Zero = Known.Zero.ashr(Shift); 410 Known.One = Known.One.ashr(Shift); 411 break; 412 case TargetOpcode::G_LSHR: 413 Known.Zero = Known.Zero.lshr(Shift); 414 Known.One = Known.One.lshr(Shift); 415 Known.Zero.setBitsFrom(Known.Zero.getBitWidth() - Shift); 416 break; 417 case TargetOpcode::G_SHL: 418 Known.Zero = Known.Zero.shl(Shift); 419 Known.One = Known.One.shl(Shift); 420 Known.Zero.setBits(0, Shift); 421 break; 422 } 423 break; 424 } 425 case TargetOpcode::G_INTTOPTR: 426 case TargetOpcode::G_PTRTOINT: 427 // Fall through and handle them the same as zext/trunc. 428 LLVM_FALLTHROUGH; 429 case TargetOpcode::G_ZEXT: 430 case TargetOpcode::G_TRUNC: { 431 Register SrcReg = MI.getOperand(1).getReg(); 432 LLT SrcTy = MRI.getType(SrcReg); 433 unsigned SrcBitWidth = SrcTy.isPointer() 434 ? DL.getIndexSizeInBits(SrcTy.getAddressSpace()) 435 : SrcTy.getSizeInBits(); 436 assert(SrcBitWidth && "SrcBitWidth can't be zero"); 437 Known = Known.zextOrTrunc(SrcBitWidth); 438 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 439 Known = Known.zextOrTrunc(BitWidth); 440 if (BitWidth > SrcBitWidth) 441 Known.Zero.setBitsFrom(SrcBitWidth); 442 break; 443 } 444 case TargetOpcode::G_MERGE_VALUES: { 445 Register NumOps = MI.getNumOperands(); 446 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 447 448 for (unsigned I = 0; I != NumOps - 1; ++I) { 449 KnownBits SrcOpKnown; 450 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown, 451 DemandedElts, Depth + 1); 452 Known.insertBits(SrcOpKnown, I * OpSize); 453 } 454 break; 455 } 456 case TargetOpcode::G_UNMERGE_VALUES: { 457 Register NumOps = MI.getNumOperands(); 458 Register SrcReg = MI.getOperand(NumOps - 1).getReg(); 459 if (MRI.getType(SrcReg).isVector()) 460 return; // TODO: Handle vectors. 461 462 KnownBits SrcOpKnown; 463 computeKnownBitsImpl(SrcReg, SrcOpKnown, DemandedElts, Depth + 1); 464 465 // Figure out the result operand index 466 unsigned DstIdx = 0; 467 for (; DstIdx != NumOps - 1 && MI.getOperand(DstIdx).getReg() != R; 468 ++DstIdx) 469 ; 470 471 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx); 472 break; 473 } 474 case TargetOpcode::G_BSWAP: { 475 Register SrcReg = MI.getOperand(1).getReg(); 476 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 477 Known.byteSwap(); 478 break; 479 } 480 case TargetOpcode::G_BITREVERSE: { 481 Register SrcReg = MI.getOperand(1).getReg(); 482 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 483 Known.reverseBits(); 484 break; 485 } 486 } 487 488 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 489 LLVM_DEBUG(dumpResult(MI, Known, Depth)); 490 491 // Update the cache. 492 ComputeKnownBitsCache[R] = Known; 493 } 494 495 /// Compute number of sign bits for the intersection of \p Src0 and \p Src1 496 unsigned GISelKnownBits::computeNumSignBitsMin(Register Src0, Register Src1, 497 const APInt &DemandedElts, 498 unsigned Depth) { 499 // Test src1 first, since we canonicalize simpler expressions to the RHS. 500 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth); 501 if (Src1SignBits == 1) 502 return 1; 503 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits); 504 } 505 506 unsigned GISelKnownBits::computeNumSignBits(Register R, 507 const APInt &DemandedElts, 508 unsigned Depth) { 509 MachineInstr &MI = *MRI.getVRegDef(R); 510 unsigned Opcode = MI.getOpcode(); 511 512 if (Opcode == TargetOpcode::G_CONSTANT) 513 return MI.getOperand(1).getCImm()->getValue().getNumSignBits(); 514 515 if (Depth == getMaxDepth()) 516 return 1; 517 518 if (!DemandedElts) 519 return 1; // No demanded elts, better to assume we don't know anything. 520 521 LLT DstTy = MRI.getType(R); 522 const unsigned TyBits = DstTy.getScalarSizeInBits(); 523 524 // Handle the case where this is called on a register that does not have a 525 // type constraint. This is unlikely to occur except by looking through copies 526 // but it is possible for the initial register being queried to be in this 527 // state. 528 if (!DstTy.isValid()) 529 return 1; 530 531 unsigned FirstAnswer = 1; 532 switch (Opcode) { 533 case TargetOpcode::COPY: { 534 MachineOperand &Src = MI.getOperand(1); 535 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 && 536 MRI.getType(Src.getReg()).isValid()) { 537 // Don't increment Depth for this one since we didn't do any work. 538 return computeNumSignBits(Src.getReg(), DemandedElts, Depth); 539 } 540 541 return 1; 542 } 543 case TargetOpcode::G_SEXT: { 544 Register Src = MI.getOperand(1).getReg(); 545 LLT SrcTy = MRI.getType(Src); 546 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits(); 547 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp; 548 } 549 case TargetOpcode::G_SEXT_INREG: { 550 // Max of the input and what this extends. 551 Register Src = MI.getOperand(1).getReg(); 552 unsigned SrcBits = MI.getOperand(2).getImm(); 553 unsigned InRegBits = TyBits - SrcBits + 1; 554 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1), InRegBits); 555 } 556 case TargetOpcode::G_SEXTLOAD: { 557 // FIXME: We need an in-memory type representation. 558 if (DstTy.isVector()) 559 return 1; 560 561 // e.g. i16->i32 = '17' bits known. 562 const MachineMemOperand *MMO = *MI.memoperands_begin(); 563 return TyBits - MMO->getSizeInBits() + 1; 564 } 565 case TargetOpcode::G_ZEXTLOAD: { 566 // FIXME: We need an in-memory type representation. 567 if (DstTy.isVector()) 568 return 1; 569 570 // e.g. i16->i32 = '16' bits known. 571 const MachineMemOperand *MMO = *MI.memoperands_begin(); 572 return TyBits - MMO->getSizeInBits(); 573 } 574 case TargetOpcode::G_TRUNC: { 575 Register Src = MI.getOperand(1).getReg(); 576 LLT SrcTy = MRI.getType(Src); 577 578 // Check if the sign bits of source go down as far as the truncated value. 579 unsigned DstTyBits = DstTy.getScalarSizeInBits(); 580 unsigned NumSrcBits = SrcTy.getScalarSizeInBits(); 581 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1); 582 if (NumSrcSignBits > (NumSrcBits - DstTyBits)) 583 return NumSrcSignBits - (NumSrcBits - DstTyBits); 584 break; 585 } 586 case TargetOpcode::G_SELECT: { 587 return computeNumSignBitsMin(MI.getOperand(2).getReg(), 588 MI.getOperand(3).getReg(), DemandedElts, 589 Depth + 1); 590 } 591 case TargetOpcode::G_INTRINSIC: 592 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: 593 default: { 594 unsigned NumBits = 595 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth); 596 if (NumBits > 1) 597 FirstAnswer = std::max(FirstAnswer, NumBits); 598 break; 599 } 600 } 601 602 // Finally, if we can prove that the top bits of the result are 0's or 1's, 603 // use this information. 604 KnownBits Known = getKnownBits(R, DemandedElts, Depth); 605 APInt Mask; 606 if (Known.isNonNegative()) { // sign bit is 0 607 Mask = Known.Zero; 608 } else if (Known.isNegative()) { // sign bit is 1; 609 Mask = Known.One; 610 } else { 611 // Nothing known. 612 return FirstAnswer; 613 } 614 615 // Okay, we know that the sign bit in Mask is set. Use CLO to determine 616 // the number of identical bits in the top of the input value. 617 Mask <<= Mask.getBitWidth() - TyBits; 618 return std::max(FirstAnswer, Mask.countLeadingOnes()); 619 } 620 621 unsigned GISelKnownBits::computeNumSignBits(Register R, unsigned Depth) { 622 LLT Ty = MRI.getType(R); 623 APInt DemandedElts = Ty.isVector() 624 ? APInt::getAllOnesValue(Ty.getNumElements()) 625 : APInt(1, 1); 626 return computeNumSignBits(R, DemandedElts, Depth); 627 } 628 629 void GISelKnownBitsAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 630 AU.setPreservesAll(); 631 MachineFunctionPass::getAnalysisUsage(AU); 632 } 633 634 bool GISelKnownBitsAnalysis::runOnMachineFunction(MachineFunction &MF) { 635 return false; 636 } 637