1 //===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===// 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 /// The pass tries to use the 32-bit encoding for instructions when possible. 8 //===----------------------------------------------------------------------===// 9 // 10 11 #include "AMDGPU.h" 12 #include "GCNSubtarget.h" 13 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 14 #include "llvm/ADT/Statistic.h" 15 #include "llvm/CodeGen/MachineFunctionPass.h" 16 17 #define DEBUG_TYPE "si-shrink-instructions" 18 19 STATISTIC(NumInstructionsShrunk, 20 "Number of 64-bit instruction reduced to 32-bit."); 21 STATISTIC(NumLiteralConstantsFolded, 22 "Number of literal constants folded into 32-bit instructions."); 23 24 using namespace llvm; 25 26 namespace { 27 28 class SIShrinkInstructions : public MachineFunctionPass { 29 public: 30 static char ID; 31 32 void shrinkMIMG(MachineInstr &MI); 33 34 public: 35 SIShrinkInstructions() : MachineFunctionPass(ID) { 36 } 37 38 bool runOnMachineFunction(MachineFunction &MF) override; 39 40 StringRef getPassName() const override { return "SI Shrink Instructions"; } 41 42 void getAnalysisUsage(AnalysisUsage &AU) const override { 43 AU.setPreservesCFG(); 44 MachineFunctionPass::getAnalysisUsage(AU); 45 } 46 }; 47 48 } // End anonymous namespace. 49 50 INITIALIZE_PASS(SIShrinkInstructions, DEBUG_TYPE, 51 "SI Shrink Instructions", false, false) 52 53 char SIShrinkInstructions::ID = 0; 54 55 FunctionPass *llvm::createSIShrinkInstructionsPass() { 56 return new SIShrinkInstructions(); 57 } 58 59 /// This function checks \p MI for operands defined by a move immediate 60 /// instruction and then folds the literal constant into the instruction if it 61 /// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instructions. 62 static bool foldImmediates(MachineInstr &MI, const SIInstrInfo *TII, 63 MachineRegisterInfo &MRI, bool TryToCommute = true) { 64 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI)); 65 66 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0); 67 68 // Try to fold Src0 69 MachineOperand &Src0 = MI.getOperand(Src0Idx); 70 if (Src0.isReg()) { 71 Register Reg = Src0.getReg(); 72 if (Reg.isVirtual() && MRI.hasOneUse(Reg)) { 73 MachineInstr *Def = MRI.getUniqueVRegDef(Reg); 74 if (Def && Def->isMoveImmediate()) { 75 MachineOperand &MovSrc = Def->getOperand(1); 76 bool ConstantFolded = false; 77 78 if (MovSrc.isImm() && (isInt<32>(MovSrc.getImm()) || 79 isUInt<32>(MovSrc.getImm()))) { 80 Src0.ChangeToImmediate(MovSrc.getImm()); 81 ConstantFolded = true; 82 } else if (MovSrc.isFI()) { 83 Src0.ChangeToFrameIndex(MovSrc.getIndex()); 84 ConstantFolded = true; 85 } else if (MovSrc.isGlobal()) { 86 Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(), 87 MovSrc.getTargetFlags()); 88 ConstantFolded = true; 89 } 90 91 if (ConstantFolded) { 92 assert(MRI.use_empty(Reg)); 93 Def->eraseFromParent(); 94 ++NumLiteralConstantsFolded; 95 return true; 96 } 97 } 98 } 99 } 100 101 // We have failed to fold src0, so commute the instruction and try again. 102 if (TryToCommute && MI.isCommutable()) { 103 if (TII->commuteInstruction(MI)) { 104 if (foldImmediates(MI, TII, MRI, false)) 105 return true; 106 107 // Commute back. 108 TII->commuteInstruction(MI); 109 } 110 } 111 112 return false; 113 } 114 115 static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) { 116 return isInt<16>(Src.getImm()) && 117 !TII->isInlineConstant(*Src.getParent(), 118 Src.getParent()->getOperandNo(&Src)); 119 } 120 121 static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src) { 122 return isUInt<16>(Src.getImm()) && 123 !TII->isInlineConstant(*Src.getParent(), 124 Src.getParent()->getOperandNo(&Src)); 125 } 126 127 static bool isKImmOrKUImmOperand(const SIInstrInfo *TII, 128 const MachineOperand &Src, 129 bool &IsUnsigned) { 130 if (isInt<16>(Src.getImm())) { 131 IsUnsigned = false; 132 return !TII->isInlineConstant(Src); 133 } 134 135 if (isUInt<16>(Src.getImm())) { 136 IsUnsigned = true; 137 return !TII->isInlineConstant(Src); 138 } 139 140 return false; 141 } 142 143 /// \returns true if the constant in \p Src should be replaced with a bitreverse 144 /// of an inline immediate. 145 static bool isReverseInlineImm(const SIInstrInfo *TII, 146 const MachineOperand &Src, 147 int32_t &ReverseImm) { 148 if (!isInt<32>(Src.getImm()) || TII->isInlineConstant(Src)) 149 return false; 150 151 ReverseImm = reverseBits<int32_t>(static_cast<int32_t>(Src.getImm())); 152 return ReverseImm >= -16 && ReverseImm <= 64; 153 } 154 155 /// Copy implicit register operands from specified instruction to this 156 /// instruction that are not part of the instruction definition. 157 static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF, 158 const MachineInstr &MI) { 159 for (unsigned i = MI.getDesc().getNumOperands() + 160 MI.getDesc().getNumImplicitUses() + 161 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands(); 162 i != e; ++i) { 163 const MachineOperand &MO = MI.getOperand(i); 164 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask()) 165 NewMI.addOperand(MF, MO); 166 } 167 } 168 169 static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI) { 170 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to 171 // get constants on the RHS. 172 if (!MI.getOperand(0).isReg()) 173 TII->commuteInstruction(MI, false, 0, 1); 174 175 // cmpk requires src0 to be a register 176 const MachineOperand &Src0 = MI.getOperand(0); 177 if (!Src0.isReg()) 178 return; 179 180 const MachineOperand &Src1 = MI.getOperand(1); 181 if (!Src1.isImm()) 182 return; 183 184 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode()); 185 if (SOPKOpc == -1) 186 return; 187 188 // eq/ne is special because the imm16 can be treated as signed or unsigned, 189 // and initially selectd to the unsigned versions. 190 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) { 191 bool HasUImm; 192 if (isKImmOrKUImmOperand(TII, Src1, HasUImm)) { 193 if (!HasUImm) { 194 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ? 195 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32; 196 } 197 198 MI.setDesc(TII->get(SOPKOpc)); 199 } 200 201 return; 202 } 203 204 const MCInstrDesc &NewDesc = TII->get(SOPKOpc); 205 206 if ((TII->sopkIsZext(SOPKOpc) && isKUImmOperand(TII, Src1)) || 207 (!TII->sopkIsZext(SOPKOpc) && isKImmOperand(TII, Src1))) { 208 MI.setDesc(NewDesc); 209 } 210 } 211 212 // Shrink NSA encoded instructions with contiguous VGPRs to non-NSA encoding. 213 void SIShrinkInstructions::shrinkMIMG(MachineInstr &MI) { 214 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode()); 215 if (!Info || Info->MIMGEncoding != AMDGPU::MIMGEncGfx10NSA) 216 return; 217 218 MachineFunction *MF = MI.getParent()->getParent(); 219 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 220 const SIInstrInfo *TII = ST.getInstrInfo(); 221 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 222 int VAddr0Idx = 223 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0); 224 unsigned NewAddrDwords = Info->VAddrDwords; 225 const TargetRegisterClass *RC; 226 227 if (Info->VAddrDwords == 2) { 228 RC = &AMDGPU::VReg_64RegClass; 229 } else if (Info->VAddrDwords == 3) { 230 RC = &AMDGPU::VReg_96RegClass; 231 } else if (Info->VAddrDwords == 4) { 232 RC = &AMDGPU::VReg_128RegClass; 233 } else if (Info->VAddrDwords <= 8) { 234 RC = &AMDGPU::VReg_256RegClass; 235 NewAddrDwords = 8; 236 } else { 237 RC = &AMDGPU::VReg_512RegClass; 238 NewAddrDwords = 16; 239 } 240 241 unsigned VgprBase = 0; 242 bool IsUndef = true; 243 bool IsKill = NewAddrDwords == Info->VAddrDwords; 244 for (unsigned i = 0; i < Info->VAddrDwords; ++i) { 245 const MachineOperand &Op = MI.getOperand(VAddr0Idx + i); 246 unsigned Vgpr = TRI.getHWRegIndex(Op.getReg()); 247 248 if (i == 0) { 249 VgprBase = Vgpr; 250 } else if (VgprBase + i != Vgpr) 251 return; 252 253 if (!Op.isUndef()) 254 IsUndef = false; 255 if (!Op.isKill()) 256 IsKill = false; 257 } 258 259 if (VgprBase + NewAddrDwords > 256) 260 return; 261 262 // Further check for implicit tied operands - this may be present if TFE is 263 // enabled 264 int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe); 265 int LWEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::lwe); 266 unsigned TFEVal = (TFEIdx == -1) ? 0 : MI.getOperand(TFEIdx).getImm(); 267 unsigned LWEVal = (LWEIdx == -1) ? 0 : MI.getOperand(LWEIdx).getImm(); 268 int ToUntie = -1; 269 if (TFEVal || LWEVal) { 270 // TFE/LWE is enabled so we need to deal with an implicit tied operand 271 for (unsigned i = LWEIdx + 1, e = MI.getNumOperands(); i != e; ++i) { 272 if (MI.getOperand(i).isReg() && MI.getOperand(i).isTied() && 273 MI.getOperand(i).isImplicit()) { 274 // This is the tied operand 275 assert( 276 ToUntie == -1 && 277 "found more than one tied implicit operand when expecting only 1"); 278 ToUntie = i; 279 MI.untieRegOperand(ToUntie); 280 } 281 } 282 } 283 284 unsigned NewOpcode = 285 AMDGPU::getMIMGOpcode(Info->BaseOpcode, AMDGPU::MIMGEncGfx10Default, 286 Info->VDataDwords, NewAddrDwords); 287 MI.setDesc(TII->get(NewOpcode)); 288 MI.getOperand(VAddr0Idx).setReg(RC->getRegister(VgprBase)); 289 MI.getOperand(VAddr0Idx).setIsUndef(IsUndef); 290 MI.getOperand(VAddr0Idx).setIsKill(IsKill); 291 292 for (unsigned i = 1; i < Info->VAddrDwords; ++i) 293 MI.RemoveOperand(VAddr0Idx + 1); 294 295 if (ToUntie >= 0) { 296 MI.tieOperands( 297 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata), 298 ToUntie - (Info->VAddrDwords - 1)); 299 } 300 } 301 302 /// Attempt to shink AND/OR/XOR operations requiring non-inlineable literals. 303 /// For AND or OR, try using S_BITSET{0,1} to clear or set bits. 304 /// If the inverse of the immediate is legal, use ANDN2, ORN2 or 305 /// XNOR (as a ^ b == ~(a ^ ~b)). 306 /// \returns true if the caller should continue the machine function iterator 307 static bool shrinkScalarLogicOp(const GCNSubtarget &ST, 308 MachineRegisterInfo &MRI, 309 const SIInstrInfo *TII, 310 MachineInstr &MI) { 311 unsigned Opc = MI.getOpcode(); 312 const MachineOperand *Dest = &MI.getOperand(0); 313 MachineOperand *Src0 = &MI.getOperand(1); 314 MachineOperand *Src1 = &MI.getOperand(2); 315 MachineOperand *SrcReg = Src0; 316 MachineOperand *SrcImm = Src1; 317 318 if (!SrcImm->isImm() || 319 AMDGPU::isInlinableLiteral32(SrcImm->getImm(), ST.hasInv2PiInlineImm())) 320 return false; 321 322 uint32_t Imm = static_cast<uint32_t>(SrcImm->getImm()); 323 uint32_t NewImm = 0; 324 325 if (Opc == AMDGPU::S_AND_B32) { 326 if (isPowerOf2_32(~Imm)) { 327 NewImm = countTrailingOnes(Imm); 328 Opc = AMDGPU::S_BITSET0_B32; 329 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST.hasInv2PiInlineImm())) { 330 NewImm = ~Imm; 331 Opc = AMDGPU::S_ANDN2_B32; 332 } 333 } else if (Opc == AMDGPU::S_OR_B32) { 334 if (isPowerOf2_32(Imm)) { 335 NewImm = countTrailingZeros(Imm); 336 Opc = AMDGPU::S_BITSET1_B32; 337 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST.hasInv2PiInlineImm())) { 338 NewImm = ~Imm; 339 Opc = AMDGPU::S_ORN2_B32; 340 } 341 } else if (Opc == AMDGPU::S_XOR_B32) { 342 if (AMDGPU::isInlinableLiteral32(~Imm, ST.hasInv2PiInlineImm())) { 343 NewImm = ~Imm; 344 Opc = AMDGPU::S_XNOR_B32; 345 } 346 } else { 347 llvm_unreachable("unexpected opcode"); 348 } 349 350 if ((Opc == AMDGPU::S_ANDN2_B32 || Opc == AMDGPU::S_ORN2_B32) && 351 SrcImm == Src0) { 352 if (!TII->commuteInstruction(MI, false, 1, 2)) 353 NewImm = 0; 354 } 355 356 if (NewImm != 0) { 357 if (Dest->getReg().isVirtual() && SrcReg->isReg()) { 358 MRI.setRegAllocationHint(Dest->getReg(), 0, SrcReg->getReg()); 359 MRI.setRegAllocationHint(SrcReg->getReg(), 0, Dest->getReg()); 360 return true; 361 } 362 363 if (SrcReg->isReg() && SrcReg->getReg() == Dest->getReg()) { 364 const bool IsUndef = SrcReg->isUndef(); 365 const bool IsKill = SrcReg->isKill(); 366 MI.setDesc(TII->get(Opc)); 367 if (Opc == AMDGPU::S_BITSET0_B32 || 368 Opc == AMDGPU::S_BITSET1_B32) { 369 Src0->ChangeToImmediate(NewImm); 370 // Remove the immediate and add the tied input. 371 MI.getOperand(2).ChangeToRegister(Dest->getReg(), /*IsDef*/ false, 372 /*isImp*/ false, IsKill, 373 /*isDead*/ false, IsUndef); 374 MI.tieOperands(0, 2); 375 } else { 376 SrcImm->setImm(NewImm); 377 } 378 } 379 } 380 381 return false; 382 } 383 384 // This is the same as MachineInstr::readsRegister/modifiesRegister except 385 // it takes subregs into account. 386 static bool instAccessReg(iterator_range<MachineInstr::const_mop_iterator> &&R, 387 Register Reg, unsigned SubReg, 388 const SIRegisterInfo &TRI) { 389 for (const MachineOperand &MO : R) { 390 if (!MO.isReg()) 391 continue; 392 393 if (Reg.isPhysical() && MO.getReg().isPhysical()) { 394 if (TRI.regsOverlap(Reg, MO.getReg())) 395 return true; 396 } else if (MO.getReg() == Reg && Reg.isVirtual()) { 397 LaneBitmask Overlap = TRI.getSubRegIndexLaneMask(SubReg) & 398 TRI.getSubRegIndexLaneMask(MO.getSubReg()); 399 if (Overlap.any()) 400 return true; 401 } 402 } 403 return false; 404 } 405 406 static bool instReadsReg(const MachineInstr *MI, 407 unsigned Reg, unsigned SubReg, 408 const SIRegisterInfo &TRI) { 409 return instAccessReg(MI->uses(), Reg, SubReg, TRI); 410 } 411 412 static bool instModifiesReg(const MachineInstr *MI, 413 unsigned Reg, unsigned SubReg, 414 const SIRegisterInfo &TRI) { 415 return instAccessReg(MI->defs(), Reg, SubReg, TRI); 416 } 417 418 static TargetInstrInfo::RegSubRegPair 419 getSubRegForIndex(Register Reg, unsigned Sub, unsigned I, 420 const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI) { 421 if (TRI.getRegSizeInBits(Reg, MRI) != 32) { 422 if (Reg.isPhysical()) { 423 Reg = TRI.getSubReg(Reg, TRI.getSubRegFromChannel(I)); 424 } else { 425 Sub = TRI.getSubRegFromChannel(I + TRI.getChannelFromSubReg(Sub)); 426 } 427 } 428 return TargetInstrInfo::RegSubRegPair(Reg, Sub); 429 } 430 431 static void dropInstructionKeepingImpDefs(MachineInstr &MI, 432 const SIInstrInfo *TII) { 433 for (unsigned i = MI.getDesc().getNumOperands() + 434 MI.getDesc().getNumImplicitUses() + 435 MI.getDesc().getNumImplicitDefs(), e = MI.getNumOperands(); 436 i != e; ++i) { 437 const MachineOperand &Op = MI.getOperand(i); 438 if (!Op.isDef()) 439 continue; 440 BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(), 441 TII->get(AMDGPU::IMPLICIT_DEF), Op.getReg()); 442 } 443 444 MI.eraseFromParent(); 445 } 446 447 // Match: 448 // mov t, x 449 // mov x, y 450 // mov y, t 451 // 452 // => 453 // 454 // mov t, x (t is potentially dead and move eliminated) 455 // v_swap_b32 x, y 456 // 457 // Returns next valid instruction pointer if was able to create v_swap_b32. 458 // 459 // This shall not be done too early not to prevent possible folding which may 460 // remove matched moves, and this should prefereably be done before RA to 461 // release saved registers and also possibly after RA which can insert copies 462 // too. 463 // 464 // This is really just a generic peephole that is not a canocical shrinking, 465 // although requirements match the pass placement and it reduces code size too. 466 static MachineInstr* matchSwap(MachineInstr &MovT, MachineRegisterInfo &MRI, 467 const SIInstrInfo *TII) { 468 assert(MovT.getOpcode() == AMDGPU::V_MOV_B32_e32 || 469 MovT.getOpcode() == AMDGPU::COPY); 470 471 Register T = MovT.getOperand(0).getReg(); 472 unsigned Tsub = MovT.getOperand(0).getSubReg(); 473 MachineOperand &Xop = MovT.getOperand(1); 474 475 if (!Xop.isReg()) 476 return nullptr; 477 Register X = Xop.getReg(); 478 unsigned Xsub = Xop.getSubReg(); 479 480 unsigned Size = TII->getOpSize(MovT, 0) / 4; 481 482 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 483 if (!TRI.isVGPR(MRI, X)) 484 return nullptr; 485 486 if (MovT.hasRegisterImplicitUseOperand(AMDGPU::M0)) 487 return nullptr; 488 489 const unsigned SearchLimit = 16; 490 unsigned Count = 0; 491 bool KilledT = false; 492 for (auto Iter = std::next(MovT.getIterator()), 493 E = MovT.getParent()->instr_end(); 494 Iter != E && Count < SearchLimit && !KilledT; ++Iter, ++Count) { 495 496 MachineInstr *MovY = &*Iter; 497 KilledT = MovY->killsRegister(T, &TRI); 498 499 if ((MovY->getOpcode() != AMDGPU::V_MOV_B32_e32 && 500 MovY->getOpcode() != AMDGPU::COPY) || 501 !MovY->getOperand(1).isReg() || 502 MovY->getOperand(1).getReg() != T || 503 MovY->getOperand(1).getSubReg() != Tsub || 504 MovY->hasRegisterImplicitUseOperand(AMDGPU::M0)) 505 continue; 506 507 Register Y = MovY->getOperand(0).getReg(); 508 unsigned Ysub = MovY->getOperand(0).getSubReg(); 509 510 if (!TRI.isVGPR(MRI, Y)) 511 continue; 512 513 MachineInstr *MovX = nullptr; 514 for (auto IY = MovY->getIterator(), I = std::next(MovT.getIterator()); 515 I != IY; ++I) { 516 if (instReadsReg(&*I, X, Xsub, TRI) || 517 instModifiesReg(&*I, Y, Ysub, TRI) || 518 instModifiesReg(&*I, T, Tsub, TRI) || 519 (MovX && instModifiesReg(&*I, X, Xsub, TRI))) { 520 MovX = nullptr; 521 break; 522 } 523 if (!instReadsReg(&*I, Y, Ysub, TRI)) { 524 if (!MovX && instModifiesReg(&*I, X, Xsub, TRI)) { 525 MovX = nullptr; 526 break; 527 } 528 continue; 529 } 530 if (MovX || 531 (I->getOpcode() != AMDGPU::V_MOV_B32_e32 && 532 I->getOpcode() != AMDGPU::COPY) || 533 I->getOperand(0).getReg() != X || 534 I->getOperand(0).getSubReg() != Xsub) { 535 MovX = nullptr; 536 break; 537 } 538 // Implicit use of M0 is an indirect move. 539 if (I->hasRegisterImplicitUseOperand(AMDGPU::M0)) 540 continue; 541 542 if (Size > 1 && (I->getNumImplicitOperands() > (I->isCopy() ? 0U : 1U))) 543 continue; 544 545 MovX = &*I; 546 } 547 548 if (!MovX) 549 continue; 550 551 LLVM_DEBUG(dbgs() << "Matched v_swap_b32:\n" << MovT << *MovX << *MovY); 552 553 for (unsigned I = 0; I < Size; ++I) { 554 TargetInstrInfo::RegSubRegPair X1, Y1; 555 X1 = getSubRegForIndex(X, Xsub, I, TRI, MRI); 556 Y1 = getSubRegForIndex(Y, Ysub, I, TRI, MRI); 557 MachineBasicBlock &MBB = *MovT.getParent(); 558 auto MIB = BuildMI(MBB, MovX->getIterator(), MovT.getDebugLoc(), 559 TII->get(AMDGPU::V_SWAP_B32)) 560 .addDef(X1.Reg, 0, X1.SubReg) 561 .addDef(Y1.Reg, 0, Y1.SubReg) 562 .addReg(Y1.Reg, 0, Y1.SubReg) 563 .addReg(X1.Reg, 0, X1.SubReg).getInstr(); 564 if (MovX->hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 565 // Drop implicit EXEC. 566 MIB->RemoveOperand(MIB->getNumExplicitOperands()); 567 MIB->copyImplicitOps(*MBB.getParent(), *MovX); 568 } 569 } 570 MovX->eraseFromParent(); 571 dropInstructionKeepingImpDefs(*MovY, TII); 572 MachineInstr *Next = &*std::next(MovT.getIterator()); 573 574 if (MRI.use_nodbg_empty(T)) { 575 dropInstructionKeepingImpDefs(MovT, TII); 576 } else { 577 Xop.setIsKill(false); 578 for (int I = MovT.getNumImplicitOperands() - 1; I >= 0; --I ) { 579 unsigned OpNo = MovT.getNumExplicitOperands() + I; 580 const MachineOperand &Op = MovT.getOperand(OpNo); 581 if (Op.isKill() && TRI.regsOverlap(X, Op.getReg())) 582 MovT.RemoveOperand(OpNo); 583 } 584 } 585 586 return Next; 587 } 588 589 return nullptr; 590 } 591 592 bool SIShrinkInstructions::runOnMachineFunction(MachineFunction &MF) { 593 if (skipFunction(MF.getFunction())) 594 return false; 595 596 MachineRegisterInfo &MRI = MF.getRegInfo(); 597 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 598 const SIInstrInfo *TII = ST.getInstrInfo(); 599 unsigned VCCReg = ST.isWave32() ? AMDGPU::VCC_LO : AMDGPU::VCC; 600 601 std::vector<unsigned> I1Defs; 602 603 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); 604 BI != BE; ++BI) { 605 606 MachineBasicBlock &MBB = *BI; 607 MachineBasicBlock::iterator I, Next; 608 for (I = MBB.begin(); I != MBB.end(); I = Next) { 609 Next = std::next(I); 610 MachineInstr &MI = *I; 611 612 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) { 613 // If this has a literal constant source that is the same as the 614 // reversed bits of an inline immediate, replace with a bitreverse of 615 // that constant. This saves 4 bytes in the common case of materializing 616 // sign bits. 617 618 // Test if we are after regalloc. We only want to do this after any 619 // optimizations happen because this will confuse them. 620 // XXX - not exactly a check for post-regalloc run. 621 MachineOperand &Src = MI.getOperand(1); 622 if (Src.isImm() && MI.getOperand(0).getReg().isPhysical()) { 623 int32_t ReverseImm; 624 if (isReverseInlineImm(TII, Src, ReverseImm)) { 625 MI.setDesc(TII->get(AMDGPU::V_BFREV_B32_e32)); 626 Src.setImm(ReverseImm); 627 continue; 628 } 629 } 630 } 631 632 if (ST.hasSwap() && (MI.getOpcode() == AMDGPU::V_MOV_B32_e32 || 633 MI.getOpcode() == AMDGPU::COPY)) { 634 if (auto *NextMI = matchSwap(MI, MRI, TII)) { 635 Next = NextMI->getIterator(); 636 continue; 637 } 638 } 639 640 // FIXME: We also need to consider movs of constant operands since 641 // immediate operands are not folded if they have more than one use, and 642 // the operand folding pass is unaware if the immediate will be free since 643 // it won't know if the src == dest constraint will end up being 644 // satisfied. 645 if (MI.getOpcode() == AMDGPU::S_ADD_I32 || 646 MI.getOpcode() == AMDGPU::S_MUL_I32) { 647 const MachineOperand *Dest = &MI.getOperand(0); 648 MachineOperand *Src0 = &MI.getOperand(1); 649 MachineOperand *Src1 = &MI.getOperand(2); 650 651 if (!Src0->isReg() && Src1->isReg()) { 652 if (TII->commuteInstruction(MI, false, 1, 2)) 653 std::swap(Src0, Src1); 654 } 655 656 // FIXME: This could work better if hints worked with subregisters. If 657 // we have a vector add of a constant, we usually don't get the correct 658 // allocation due to the subregister usage. 659 if (Dest->getReg().isVirtual() && Src0->isReg()) { 660 MRI.setRegAllocationHint(Dest->getReg(), 0, Src0->getReg()); 661 MRI.setRegAllocationHint(Src0->getReg(), 0, Dest->getReg()); 662 continue; 663 } 664 665 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) { 666 if (Src1->isImm() && isKImmOperand(TII, *Src1)) { 667 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ? 668 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32; 669 670 MI.setDesc(TII->get(Opc)); 671 MI.tieOperands(0, 1); 672 } 673 } 674 } 675 676 // Try to use s_cmpk_* 677 if (MI.isCompare() && TII->isSOPC(MI)) { 678 shrinkScalarCompare(TII, MI); 679 continue; 680 } 681 682 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates. 683 if (MI.getOpcode() == AMDGPU::S_MOV_B32) { 684 const MachineOperand &Dst = MI.getOperand(0); 685 MachineOperand &Src = MI.getOperand(1); 686 687 if (Src.isImm() && Dst.getReg().isPhysical()) { 688 int32_t ReverseImm; 689 if (isKImmOperand(TII, Src)) 690 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32)); 691 else if (isReverseInlineImm(TII, Src, ReverseImm)) { 692 MI.setDesc(TII->get(AMDGPU::S_BREV_B32)); 693 Src.setImm(ReverseImm); 694 } 695 } 696 697 continue; 698 } 699 700 // Shrink scalar logic operations. 701 if (MI.getOpcode() == AMDGPU::S_AND_B32 || 702 MI.getOpcode() == AMDGPU::S_OR_B32 || 703 MI.getOpcode() == AMDGPU::S_XOR_B32) { 704 if (shrinkScalarLogicOp(ST, MRI, TII, MI)) 705 continue; 706 } 707 708 if (TII->isMIMG(MI.getOpcode()) && 709 ST.getGeneration() >= AMDGPUSubtarget::GFX10 && 710 MF.getProperties().hasProperty( 711 MachineFunctionProperties::Property::NoVRegs)) { 712 shrinkMIMG(MI); 713 continue; 714 } 715 716 if (!TII->hasVALU32BitEncoding(MI.getOpcode())) 717 continue; 718 719 if (!TII->canShrink(MI, MRI)) { 720 // Try commuting the instruction and see if that enables us to shrink 721 // it. 722 if (!MI.isCommutable() || !TII->commuteInstruction(MI) || 723 !TII->canShrink(MI, MRI)) 724 continue; 725 } 726 727 // getVOPe32 could be -1 here if we started with an instruction that had 728 // a 32-bit encoding and then commuted it to an instruction that did not. 729 if (!TII->hasVALU32BitEncoding(MI.getOpcode())) 730 continue; 731 732 int Op32 = AMDGPU::getVOPe32(MI.getOpcode()); 733 734 if (TII->isVOPC(Op32)) { 735 Register DstReg = MI.getOperand(0).getReg(); 736 if (DstReg.isVirtual()) { 737 // VOPC instructions can only write to the VCC register. We can't 738 // force them to use VCC here, because this is only one register and 739 // cannot deal with sequences which would require multiple copies of 740 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...) 741 // 742 // So, instead of forcing the instruction to write to VCC, we provide 743 // a hint to the register allocator to use VCC and then we will run 744 // this pass again after RA and shrink it if it outputs to VCC. 745 MRI.setRegAllocationHint(MI.getOperand(0).getReg(), 0, VCCReg); 746 continue; 747 } 748 if (DstReg != VCCReg) 749 continue; 750 } 751 752 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) { 753 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC 754 // instructions. 755 const MachineOperand *Src2 = 756 TII->getNamedOperand(MI, AMDGPU::OpName::src2); 757 if (!Src2->isReg()) 758 continue; 759 Register SReg = Src2->getReg(); 760 if (SReg.isVirtual()) { 761 MRI.setRegAllocationHint(SReg, 0, VCCReg); 762 continue; 763 } 764 if (SReg != VCCReg) 765 continue; 766 } 767 768 // Check for the bool flag output for instructions like V_ADD_I32_e64. 769 const MachineOperand *SDst = TII->getNamedOperand(MI, 770 AMDGPU::OpName::sdst); 771 772 // Check the carry-in operand for v_addc_u32_e64. 773 const MachineOperand *Src2 = TII->getNamedOperand(MI, 774 AMDGPU::OpName::src2); 775 776 if (SDst) { 777 bool Next = false; 778 779 if (SDst->getReg() != VCCReg) { 780 if (SDst->getReg().isVirtual()) 781 MRI.setRegAllocationHint(SDst->getReg(), 0, VCCReg); 782 Next = true; 783 } 784 785 // All of the instructions with carry outs also have an SGPR input in 786 // src2. 787 if (Src2 && Src2->getReg() != VCCReg) { 788 if (Src2->getReg().isVirtual()) 789 MRI.setRegAllocationHint(Src2->getReg(), 0, VCCReg); 790 Next = true; 791 } 792 793 if (Next) 794 continue; 795 } 796 797 // We can shrink this instruction 798 LLVM_DEBUG(dbgs() << "Shrinking " << MI); 799 800 MachineInstr *Inst32 = TII->buildShrunkInst(MI, Op32); 801 ++NumInstructionsShrunk; 802 803 // Copy extra operands not present in the instruction definition. 804 copyExtraImplicitOps(*Inst32, MF, MI); 805 806 MI.eraseFromParent(); 807 foldImmediates(*Inst32, TII, MRI); 808 809 LLVM_DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n'); 810 } 811 } 812 return false; 813 } 814