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.One = *CstVal; 221 Known.Zero = ~Known.One; 222 break; 223 } 224 case TargetOpcode::G_FRAME_INDEX: { 225 int FrameIdx = MI.getOperand(1).getIndex(); 226 TL.computeKnownBitsForFrameIndex(FrameIdx, Known, MF); 227 break; 228 } 229 case TargetOpcode::G_SUB: { 230 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 231 Depth + 1); 232 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts, 233 Depth + 1); 234 Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false, Known, 235 Known2); 236 break; 237 } 238 case TargetOpcode::G_XOR: { 239 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 240 Depth + 1); 241 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 242 Depth + 1); 243 244 Known ^= Known2; 245 break; 246 } 247 case TargetOpcode::G_PTR_ADD: { 248 // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets? 249 LLT Ty = MRI.getType(MI.getOperand(1).getReg()); 250 if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace())) 251 break; 252 LLVM_FALLTHROUGH; 253 } 254 case TargetOpcode::G_ADD: { 255 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 256 Depth + 1); 257 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts, 258 Depth + 1); 259 Known = 260 KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false, Known, Known2); 261 break; 262 } 263 case TargetOpcode::G_AND: { 264 // If either the LHS or the RHS are Zero, the result is zero. 265 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 266 Depth + 1); 267 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 268 Depth + 1); 269 270 Known &= Known2; 271 break; 272 } 273 case TargetOpcode::G_OR: { 274 // If either the LHS or the RHS are Zero, the result is zero. 275 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 276 Depth + 1); 277 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 278 Depth + 1); 279 280 Known |= Known2; 281 break; 282 } 283 case TargetOpcode::G_MUL: { 284 computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts, 285 Depth + 1); 286 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts, 287 Depth + 1); 288 Known = KnownBits::computeForMul(Known, Known2); 289 break; 290 } 291 case TargetOpcode::G_SELECT: { 292 computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(), 293 Known, DemandedElts, Depth + 1); 294 break; 295 } 296 case TargetOpcode::G_SMIN: { 297 // TODO: Handle clamp pattern with number of sign bits 298 KnownBits KnownRHS; 299 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 300 Depth + 1); 301 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts, 302 Depth + 1); 303 Known = KnownBits::smin(Known, KnownRHS); 304 break; 305 } 306 case TargetOpcode::G_SMAX: { 307 // TODO: Handle clamp pattern with number of sign bits 308 KnownBits KnownRHS; 309 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 310 Depth + 1); 311 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts, 312 Depth + 1); 313 Known = KnownBits::smax(Known, KnownRHS); 314 break; 315 } 316 case TargetOpcode::G_UMIN: { 317 KnownBits KnownRHS; 318 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, 319 DemandedElts, Depth + 1); 320 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, 321 DemandedElts, Depth + 1); 322 Known = KnownBits::umin(Known, KnownRHS); 323 break; 324 } 325 case TargetOpcode::G_UMAX: { 326 KnownBits KnownRHS; 327 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, 328 DemandedElts, Depth + 1); 329 computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, 330 DemandedElts, Depth + 1); 331 Known = KnownBits::umax(Known, KnownRHS); 332 break; 333 } 334 case TargetOpcode::G_FCMP: 335 case TargetOpcode::G_ICMP: { 336 if (TL.getBooleanContents(DstTy.isVector(), 337 Opcode == TargetOpcode::G_FCMP) == 338 TargetLowering::ZeroOrOneBooleanContent && 339 BitWidth > 1) 340 Known.Zero.setBitsFrom(1); 341 break; 342 } 343 case TargetOpcode::G_SEXT: { 344 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 345 Depth + 1); 346 // If the sign bit is known to be zero or one, then sext will extend 347 // it to the top bits, else it will just zext. 348 Known = Known.sext(BitWidth); 349 break; 350 } 351 case TargetOpcode::G_ANYEXT: { 352 computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts, 353 Depth + 1); 354 Known = Known.anyext(BitWidth); 355 break; 356 } 357 case TargetOpcode::G_LOAD: { 358 const MachineMemOperand *MMO = *MI.memoperands_begin(); 359 if (const MDNode *Ranges = MMO->getRanges()) { 360 computeKnownBitsFromRangeMetadata(*Ranges, Known); 361 } 362 363 break; 364 } 365 case TargetOpcode::G_ZEXTLOAD: { 366 // Everything above the retrieved bits is zero 367 Known.Zero.setBitsFrom((*MI.memoperands_begin())->getSizeInBits()); 368 break; 369 } 370 case TargetOpcode::G_ASHR: { 371 KnownBits LHSKnown, RHSKnown; 372 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts, 373 Depth + 1); 374 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts, 375 Depth + 1); 376 Known = KnownBits::ashr(LHSKnown, RHSKnown); 377 break; 378 } 379 case TargetOpcode::G_LSHR: { 380 KnownBits LHSKnown, RHSKnown; 381 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts, 382 Depth + 1); 383 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts, 384 Depth + 1); 385 Known = KnownBits::lshr(LHSKnown, RHSKnown); 386 break; 387 } 388 case TargetOpcode::G_SHL: { 389 KnownBits LHSKnown, RHSKnown; 390 computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts, 391 Depth + 1); 392 computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts, 393 Depth + 1); 394 Known = KnownBits::shl(LHSKnown, RHSKnown); 395 break; 396 } 397 case TargetOpcode::G_INTTOPTR: 398 case TargetOpcode::G_PTRTOINT: 399 // Fall through and handle them the same as zext/trunc. 400 LLVM_FALLTHROUGH; 401 case TargetOpcode::G_ZEXT: 402 case TargetOpcode::G_TRUNC: { 403 Register SrcReg = MI.getOperand(1).getReg(); 404 LLT SrcTy = MRI.getType(SrcReg); 405 unsigned SrcBitWidth = SrcTy.isPointer() 406 ? DL.getIndexSizeInBits(SrcTy.getAddressSpace()) 407 : SrcTy.getSizeInBits(); 408 assert(SrcBitWidth && "SrcBitWidth can't be zero"); 409 Known = Known.zextOrTrunc(SrcBitWidth); 410 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 411 Known = Known.zextOrTrunc(BitWidth); 412 if (BitWidth > SrcBitWidth) 413 Known.Zero.setBitsFrom(SrcBitWidth); 414 break; 415 } 416 case TargetOpcode::G_MERGE_VALUES: { 417 unsigned NumOps = MI.getNumOperands(); 418 unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits(); 419 420 for (unsigned I = 0; I != NumOps - 1; ++I) { 421 KnownBits SrcOpKnown; 422 computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown, 423 DemandedElts, Depth + 1); 424 Known.insertBits(SrcOpKnown, I * OpSize); 425 } 426 break; 427 } 428 case TargetOpcode::G_UNMERGE_VALUES: { 429 unsigned NumOps = MI.getNumOperands(); 430 Register SrcReg = MI.getOperand(NumOps - 1).getReg(); 431 if (MRI.getType(SrcReg).isVector()) 432 return; // TODO: Handle vectors. 433 434 KnownBits SrcOpKnown; 435 computeKnownBitsImpl(SrcReg, SrcOpKnown, DemandedElts, Depth + 1); 436 437 // Figure out the result operand index 438 unsigned DstIdx = 0; 439 for (; DstIdx != NumOps - 1 && MI.getOperand(DstIdx).getReg() != R; 440 ++DstIdx) 441 ; 442 443 Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx); 444 break; 445 } 446 case TargetOpcode::G_BSWAP: { 447 Register SrcReg = MI.getOperand(1).getReg(); 448 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 449 Known.byteSwap(); 450 break; 451 } 452 case TargetOpcode::G_BITREVERSE: { 453 Register SrcReg = MI.getOperand(1).getReg(); 454 computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1); 455 Known.reverseBits(); 456 break; 457 } 458 } 459 460 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 461 LLVM_DEBUG(dumpResult(MI, Known, Depth)); 462 463 // Update the cache. 464 ComputeKnownBitsCache[R] = Known; 465 } 466 467 /// Compute number of sign bits for the intersection of \p Src0 and \p Src1 468 unsigned GISelKnownBits::computeNumSignBitsMin(Register Src0, Register Src1, 469 const APInt &DemandedElts, 470 unsigned Depth) { 471 // Test src1 first, since we canonicalize simpler expressions to the RHS. 472 unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth); 473 if (Src1SignBits == 1) 474 return 1; 475 return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits); 476 } 477 478 unsigned GISelKnownBits::computeNumSignBits(Register R, 479 const APInt &DemandedElts, 480 unsigned Depth) { 481 MachineInstr &MI = *MRI.getVRegDef(R); 482 unsigned Opcode = MI.getOpcode(); 483 484 if (Opcode == TargetOpcode::G_CONSTANT) 485 return MI.getOperand(1).getCImm()->getValue().getNumSignBits(); 486 487 if (Depth == getMaxDepth()) 488 return 1; 489 490 if (!DemandedElts) 491 return 1; // No demanded elts, better to assume we don't know anything. 492 493 LLT DstTy = MRI.getType(R); 494 const unsigned TyBits = DstTy.getScalarSizeInBits(); 495 496 // Handle the case where this is called on a register that does not have a 497 // type constraint. This is unlikely to occur except by looking through copies 498 // but it is possible for the initial register being queried to be in this 499 // state. 500 if (!DstTy.isValid()) 501 return 1; 502 503 unsigned FirstAnswer = 1; 504 switch (Opcode) { 505 case TargetOpcode::COPY: { 506 MachineOperand &Src = MI.getOperand(1); 507 if (Src.getReg().isVirtual() && Src.getSubReg() == 0 && 508 MRI.getType(Src.getReg()).isValid()) { 509 // Don't increment Depth for this one since we didn't do any work. 510 return computeNumSignBits(Src.getReg(), DemandedElts, Depth); 511 } 512 513 return 1; 514 } 515 case TargetOpcode::G_SEXT: { 516 Register Src = MI.getOperand(1).getReg(); 517 LLT SrcTy = MRI.getType(Src); 518 unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits(); 519 return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp; 520 } 521 case TargetOpcode::G_SEXT_INREG: { 522 // Max of the input and what this extends. 523 Register Src = MI.getOperand(1).getReg(); 524 unsigned SrcBits = MI.getOperand(2).getImm(); 525 unsigned InRegBits = TyBits - SrcBits + 1; 526 return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1), InRegBits); 527 } 528 case TargetOpcode::G_SEXTLOAD: { 529 // FIXME: We need an in-memory type representation. 530 if (DstTy.isVector()) 531 return 1; 532 533 // e.g. i16->i32 = '17' bits known. 534 const MachineMemOperand *MMO = *MI.memoperands_begin(); 535 return TyBits - MMO->getSizeInBits() + 1; 536 } 537 case TargetOpcode::G_ZEXTLOAD: { 538 // FIXME: We need an in-memory type representation. 539 if (DstTy.isVector()) 540 return 1; 541 542 // e.g. i16->i32 = '16' bits known. 543 const MachineMemOperand *MMO = *MI.memoperands_begin(); 544 return TyBits - MMO->getSizeInBits(); 545 } 546 case TargetOpcode::G_TRUNC: { 547 Register Src = MI.getOperand(1).getReg(); 548 LLT SrcTy = MRI.getType(Src); 549 550 // Check if the sign bits of source go down as far as the truncated value. 551 unsigned DstTyBits = DstTy.getScalarSizeInBits(); 552 unsigned NumSrcBits = SrcTy.getScalarSizeInBits(); 553 unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1); 554 if (NumSrcSignBits > (NumSrcBits - DstTyBits)) 555 return NumSrcSignBits - (NumSrcBits - DstTyBits); 556 break; 557 } 558 case TargetOpcode::G_SELECT: { 559 return computeNumSignBitsMin(MI.getOperand(2).getReg(), 560 MI.getOperand(3).getReg(), DemandedElts, 561 Depth + 1); 562 } 563 case TargetOpcode::G_INTRINSIC: 564 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: 565 default: { 566 unsigned NumBits = 567 TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth); 568 if (NumBits > 1) 569 FirstAnswer = std::max(FirstAnswer, NumBits); 570 break; 571 } 572 } 573 574 // Finally, if we can prove that the top bits of the result are 0's or 1's, 575 // use this information. 576 KnownBits Known = getKnownBits(R, DemandedElts, Depth); 577 APInt Mask; 578 if (Known.isNonNegative()) { // sign bit is 0 579 Mask = Known.Zero; 580 } else if (Known.isNegative()) { // sign bit is 1; 581 Mask = Known.One; 582 } else { 583 // Nothing known. 584 return FirstAnswer; 585 } 586 587 // Okay, we know that the sign bit in Mask is set. Use CLO to determine 588 // the number of identical bits in the top of the input value. 589 Mask <<= Mask.getBitWidth() - TyBits; 590 return std::max(FirstAnswer, Mask.countLeadingOnes()); 591 } 592 593 unsigned GISelKnownBits::computeNumSignBits(Register R, unsigned Depth) { 594 LLT Ty = MRI.getType(R); 595 APInt DemandedElts = Ty.isVector() 596 ? APInt::getAllOnesValue(Ty.getNumElements()) 597 : APInt(1, 1); 598 return computeNumSignBits(R, DemandedElts, Depth); 599 } 600 601 void GISelKnownBitsAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 602 AU.setPreservesAll(); 603 MachineFunctionPass::getAnalysisUsage(AU); 604 } 605 606 bool GISelKnownBitsAnalysis::runOnMachineFunction(MachineFunction &MF) { 607 return false; 608 } 609