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