1 //===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===// 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 // This is an extremely simple MachineInstr-level copy propagation pass. 10 // 11 // This pass forwards the source of COPYs to the users of their destinations 12 // when doing so is legal. For example: 13 // 14 // %reg1 = COPY %reg0 15 // ... 16 // ... = OP %reg1 17 // 18 // If 19 // - %reg0 has not been clobbered by the time of the use of %reg1 20 // - the register class constraints are satisfied 21 // - the COPY def is the only value that reaches OP 22 // then this pass replaces the above with: 23 // 24 // %reg1 = COPY %reg0 25 // ... 26 // ... = OP %reg0 27 // 28 // This pass also removes some redundant COPYs. For example: 29 // 30 // %R1 = COPY %R0 31 // ... // No clobber of %R1 32 // %R0 = COPY %R1 <<< Removed 33 // 34 // or 35 // 36 // %R1 = COPY %R0 37 // ... // No clobber of %R0 38 // %R1 = COPY %R0 <<< Removed 39 // 40 // or 41 // 42 // $R0 = OP ... 43 // ... // No read/clobber of $R0 and $R1 44 // $R1 = COPY $R0 // $R0 is killed 45 // Replace $R0 with $R1 and remove the COPY 46 // $R1 = OP ... 47 // ... 48 // 49 //===----------------------------------------------------------------------===// 50 51 #include "llvm/ADT/DenseMap.h" 52 #include "llvm/ADT/STLExtras.h" 53 #include "llvm/ADT/SetVector.h" 54 #include "llvm/ADT/SmallSet.h" 55 #include "llvm/ADT/SmallVector.h" 56 #include "llvm/ADT/Statistic.h" 57 #include "llvm/ADT/iterator_range.h" 58 #include "llvm/CodeGen/MachineBasicBlock.h" 59 #include "llvm/CodeGen/MachineFunction.h" 60 #include "llvm/CodeGen/MachineFunctionPass.h" 61 #include "llvm/CodeGen/MachineInstr.h" 62 #include "llvm/CodeGen/MachineOperand.h" 63 #include "llvm/CodeGen/MachineRegisterInfo.h" 64 #include "llvm/CodeGen/TargetInstrInfo.h" 65 #include "llvm/CodeGen/TargetRegisterInfo.h" 66 #include "llvm/CodeGen/TargetSubtargetInfo.h" 67 #include "llvm/InitializePasses.h" 68 #include "llvm/MC/MCRegisterInfo.h" 69 #include "llvm/Pass.h" 70 #include "llvm/Support/Debug.h" 71 #include "llvm/Support/DebugCounter.h" 72 #include "llvm/Support/raw_ostream.h" 73 #include <cassert> 74 #include <iterator> 75 76 using namespace llvm; 77 78 #define DEBUG_TYPE "machine-cp" 79 80 STATISTIC(NumDeletes, "Number of dead copies deleted"); 81 STATISTIC(NumCopyForwards, "Number of copy uses forwarded"); 82 STATISTIC(NumCopyBackwardPropagated, "Number of copy defs backward propagated"); 83 STATISTIC(SpillageChainsLength, "Length of spillage chains"); 84 STATISTIC(NumSpillageChains, "Number of spillage chains"); 85 DEBUG_COUNTER(FwdCounter, "machine-cp-fwd", 86 "Controls which register COPYs are forwarded"); 87 88 static cl::opt<bool> MCPUseCopyInstr("mcp-use-is-copy-instr", cl::init(false), 89 cl::Hidden); 90 static cl::opt<cl::boolOrDefault> 91 EnableSpillageCopyElimination("enable-spill-copy-elim", cl::Hidden); 92 93 namespace { 94 95 static std::optional<DestSourcePair> isCopyInstr(const MachineInstr &MI, 96 const TargetInstrInfo &TII, 97 bool UseCopyInstr) { 98 if (UseCopyInstr) 99 return TII.isCopyInstr(MI); 100 101 if (MI.isCopy()) 102 return std::optional<DestSourcePair>( 103 DestSourcePair{MI.getOperand(0), MI.getOperand(1)}); 104 105 return std::nullopt; 106 } 107 108 class CopyTracker { 109 struct CopyInfo { 110 MachineInstr *MI, *LastSeenUseInCopy; 111 SmallVector<MCRegister, 4> DefRegs; 112 bool Avail; 113 }; 114 115 DenseMap<MCRegister, CopyInfo> Copies; 116 117 public: 118 /// Mark all of the given registers and their subregisters as unavailable for 119 /// copying. 120 void markRegsUnavailable(ArrayRef<MCRegister> Regs, 121 const TargetRegisterInfo &TRI) { 122 for (MCRegister Reg : Regs) { 123 // Source of copy is no longer available for propagation. 124 for (MCRegUnit Unit : TRI.regunits(Reg)) { 125 auto CI = Copies.find(Unit); 126 if (CI != Copies.end()) 127 CI->second.Avail = false; 128 } 129 } 130 } 131 132 /// Remove register from copy maps. 133 void invalidateRegister(MCRegister Reg, const TargetRegisterInfo &TRI, 134 const TargetInstrInfo &TII, bool UseCopyInstr) { 135 // Since Reg might be a subreg of some registers, only invalidate Reg is not 136 // enough. We have to find the COPY defines Reg or registers defined by Reg 137 // and invalidate all of them. Similarly, we must invalidate all of the 138 // the subregisters used in the source of the COPY. 139 SmallSet<MCRegUnit, 8> RegUnitsToInvalidate; 140 auto InvalidateCopy = [&](MachineInstr *MI) { 141 std::optional<DestSourcePair> CopyOperands = 142 isCopyInstr(*MI, TII, UseCopyInstr); 143 assert(CopyOperands && "Expect copy"); 144 145 auto Dest = TRI.regunits(CopyOperands->Destination->getReg().asMCReg()); 146 auto Src = TRI.regunits(CopyOperands->Source->getReg().asMCReg()); 147 RegUnitsToInvalidate.insert(Dest.begin(), Dest.end()); 148 RegUnitsToInvalidate.insert(Src.begin(), Src.end()); 149 }; 150 151 for (MCRegUnit Unit : TRI.regunits(Reg)) { 152 auto I = Copies.find(Unit); 153 if (I != Copies.end()) { 154 if (MachineInstr *MI = I->second.MI) 155 InvalidateCopy(MI); 156 if (MachineInstr *MI = I->second.LastSeenUseInCopy) 157 InvalidateCopy(MI); 158 } 159 } 160 for (MCRegUnit Unit : RegUnitsToInvalidate) 161 Copies.erase(Unit); 162 } 163 164 /// Clobber a single register, removing it from the tracker's copy maps. 165 void clobberRegister(MCRegister Reg, const TargetRegisterInfo &TRI, 166 const TargetInstrInfo &TII, bool UseCopyInstr) { 167 for (MCRegUnit Unit : TRI.regunits(Reg)) { 168 auto I = Copies.find(Unit); 169 if (I != Copies.end()) { 170 // When we clobber the source of a copy, we need to clobber everything 171 // it defined. 172 markRegsUnavailable(I->second.DefRegs, TRI); 173 // When we clobber the destination of a copy, we need to clobber the 174 // whole register it defined. 175 if (MachineInstr *MI = I->second.MI) { 176 std::optional<DestSourcePair> CopyOperands = 177 isCopyInstr(*MI, TII, UseCopyInstr); 178 179 MCRegister Def = CopyOperands->Destination->getReg().asMCReg(); 180 MCRegister Src = CopyOperands->Source->getReg().asMCReg(); 181 182 markRegsUnavailable(Def, TRI); 183 184 // Since we clobber the destination of a copy, the semantic of Src's 185 // "DefRegs" to contain Def is no longer effectual. We will also need 186 // to remove the record from the copy maps that indicates Src defined 187 // Def. Failing to do so might cause the target to miss some 188 // opportunities to further eliminate redundant copy instructions. 189 // Consider the following sequence during the 190 // ForwardCopyPropagateBlock procedure: 191 // L1: r0 = COPY r9 <- TrackMI 192 // L2: r0 = COPY r8 <- TrackMI (Remove r9 defined r0 from tracker) 193 // L3: use r0 <- Remove L2 from MaybeDeadCopies 194 // L4: early-clobber r9 <- Clobber r9 (L2 is still valid in tracker) 195 // L5: r0 = COPY r8 <- Remove NopCopy 196 for (MCRegUnit SrcUnit : TRI.regunits(Src)) { 197 auto SrcCopy = Copies.find(SrcUnit); 198 if (SrcCopy != Copies.end() && SrcCopy->second.LastSeenUseInCopy) { 199 // If SrcCopy defines multiple values, we only need 200 // to erase the record for Def in DefRegs. 201 for (auto itr = SrcCopy->second.DefRegs.begin(); 202 itr != SrcCopy->second.DefRegs.end(); itr++) { 203 if (*itr == Def) { 204 SrcCopy->second.DefRegs.erase(itr); 205 // If DefReg becomes empty after removal, we can directly 206 // remove SrcCopy from the tracker's copy maps. 207 if (SrcCopy->second.DefRegs.empty()) { 208 Copies.erase(SrcCopy); 209 } 210 break; 211 } 212 } 213 } 214 } 215 } 216 // Now we can erase the copy. 217 Copies.erase(I); 218 } 219 } 220 } 221 222 /// Add this copy's registers into the tracker's copy maps. 223 void trackCopy(MachineInstr *MI, const TargetRegisterInfo &TRI, 224 const TargetInstrInfo &TII, bool UseCopyInstr) { 225 std::optional<DestSourcePair> CopyOperands = 226 isCopyInstr(*MI, TII, UseCopyInstr); 227 assert(CopyOperands && "Tracking non-copy?"); 228 229 MCRegister Src = CopyOperands->Source->getReg().asMCReg(); 230 MCRegister Def = CopyOperands->Destination->getReg().asMCReg(); 231 232 // Remember Def is defined by the copy. 233 for (MCRegUnit Unit : TRI.regunits(Def)) 234 Copies[Unit] = {MI, nullptr, {}, true}; 235 236 // Remember source that's copied to Def. Once it's clobbered, then 237 // it's no longer available for copy propagation. 238 for (MCRegUnit Unit : TRI.regunits(Src)) { 239 auto I = Copies.insert({Unit, {nullptr, nullptr, {}, false}}); 240 auto &Copy = I.first->second; 241 if (!is_contained(Copy.DefRegs, Def)) 242 Copy.DefRegs.push_back(Def); 243 Copy.LastSeenUseInCopy = MI; 244 } 245 } 246 247 bool hasAnyCopies() { 248 return !Copies.empty(); 249 } 250 251 MachineInstr *findCopyForUnit(MCRegister RegUnit, 252 const TargetRegisterInfo &TRI, 253 bool MustBeAvailable = false) { 254 auto CI = Copies.find(RegUnit); 255 if (CI == Copies.end()) 256 return nullptr; 257 if (MustBeAvailable && !CI->second.Avail) 258 return nullptr; 259 return CI->second.MI; 260 } 261 262 MachineInstr *findCopyDefViaUnit(MCRegister RegUnit, 263 const TargetRegisterInfo &TRI) { 264 auto CI = Copies.find(RegUnit); 265 if (CI == Copies.end()) 266 return nullptr; 267 if (CI->second.DefRegs.size() != 1) 268 return nullptr; 269 MCRegUnit RU = *TRI.regunits(CI->second.DefRegs[0]).begin(); 270 return findCopyForUnit(RU, TRI, true); 271 } 272 273 MachineInstr *findAvailBackwardCopy(MachineInstr &I, MCRegister Reg, 274 const TargetRegisterInfo &TRI, 275 const TargetInstrInfo &TII, 276 bool UseCopyInstr) { 277 MCRegUnit RU = *TRI.regunits(Reg).begin(); 278 MachineInstr *AvailCopy = findCopyDefViaUnit(RU, TRI); 279 280 if (!AvailCopy) 281 return nullptr; 282 283 std::optional<DestSourcePair> CopyOperands = 284 isCopyInstr(*AvailCopy, TII, UseCopyInstr); 285 Register AvailSrc = CopyOperands->Source->getReg(); 286 Register AvailDef = CopyOperands->Destination->getReg(); 287 if (!TRI.isSubRegisterEq(AvailSrc, Reg)) 288 return nullptr; 289 290 for (const MachineInstr &MI : 291 make_range(AvailCopy->getReverseIterator(), I.getReverseIterator())) 292 for (const MachineOperand &MO : MI.operands()) 293 if (MO.isRegMask()) 294 // FIXME: Shall we simultaneously invalidate AvailSrc or AvailDef? 295 if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 296 return nullptr; 297 298 return AvailCopy; 299 } 300 301 MachineInstr *findAvailCopy(MachineInstr &DestCopy, MCRegister Reg, 302 const TargetRegisterInfo &TRI, 303 const TargetInstrInfo &TII, bool UseCopyInstr) { 304 // We check the first RegUnit here, since we'll only be interested in the 305 // copy if it copies the entire register anyway. 306 MCRegUnit RU = *TRI.regunits(Reg).begin(); 307 MachineInstr *AvailCopy = 308 findCopyForUnit(RU, TRI, /*MustBeAvailable=*/true); 309 310 if (!AvailCopy) 311 return nullptr; 312 313 std::optional<DestSourcePair> CopyOperands = 314 isCopyInstr(*AvailCopy, TII, UseCopyInstr); 315 Register AvailSrc = CopyOperands->Source->getReg(); 316 Register AvailDef = CopyOperands->Destination->getReg(); 317 if (!TRI.isSubRegisterEq(AvailDef, Reg)) 318 return nullptr; 319 320 // Check that the available copy isn't clobbered by any regmasks between 321 // itself and the destination. 322 for (const MachineInstr &MI : 323 make_range(AvailCopy->getIterator(), DestCopy.getIterator())) 324 for (const MachineOperand &MO : MI.operands()) 325 if (MO.isRegMask()) 326 if (MO.clobbersPhysReg(AvailSrc) || MO.clobbersPhysReg(AvailDef)) 327 return nullptr; 328 329 return AvailCopy; 330 } 331 332 // Find last COPY that defines Reg before Current MachineInstr. 333 MachineInstr *findLastSeenDefInCopy(const MachineInstr &Current, 334 MCRegister Reg, 335 const TargetRegisterInfo &TRI, 336 const TargetInstrInfo &TII, 337 bool UseCopyInstr) { 338 MCRegUnit RU = *TRI.regunits(Reg).begin(); 339 auto CI = Copies.find(RU); 340 if (CI == Copies.end() || !CI->second.Avail) 341 return nullptr; 342 343 MachineInstr *DefCopy = CI->second.MI; 344 std::optional<DestSourcePair> CopyOperands = 345 isCopyInstr(*DefCopy, TII, UseCopyInstr); 346 Register Def = CopyOperands->Destination->getReg(); 347 if (!TRI.isSubRegisterEq(Def, Reg)) 348 return nullptr; 349 350 for (const MachineInstr &MI : 351 make_range(static_cast<const MachineInstr *>(DefCopy)->getIterator(), 352 Current.getIterator())) 353 for (const MachineOperand &MO : MI.operands()) 354 if (MO.isRegMask()) 355 if (MO.clobbersPhysReg(Def)) { 356 LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " 357 << printReg(Def, &TRI) << "\n"); 358 return nullptr; 359 } 360 361 return DefCopy; 362 } 363 364 // Find last COPY that uses Reg. 365 MachineInstr *findLastSeenUseInCopy(MCRegister Reg, 366 const TargetRegisterInfo &TRI) { 367 MCRegUnit RU = *TRI.regunits(Reg).begin(); 368 auto CI = Copies.find(RU); 369 if (CI == Copies.end()) 370 return nullptr; 371 return CI->second.LastSeenUseInCopy; 372 } 373 374 void clear() { 375 Copies.clear(); 376 } 377 }; 378 379 class MachineCopyPropagation : public MachineFunctionPass { 380 const TargetRegisterInfo *TRI = nullptr; 381 const TargetInstrInfo *TII = nullptr; 382 const MachineRegisterInfo *MRI = nullptr; 383 384 // Return true if this is a copy instruction and false otherwise. 385 bool UseCopyInstr; 386 387 public: 388 static char ID; // Pass identification, replacement for typeid 389 390 MachineCopyPropagation(bool CopyInstr = false) 391 : MachineFunctionPass(ID), UseCopyInstr(CopyInstr || MCPUseCopyInstr) { 392 initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry()); 393 } 394 395 void getAnalysisUsage(AnalysisUsage &AU) const override { 396 AU.setPreservesCFG(); 397 MachineFunctionPass::getAnalysisUsage(AU); 398 } 399 400 bool runOnMachineFunction(MachineFunction &MF) override; 401 402 MachineFunctionProperties getRequiredProperties() const override { 403 return MachineFunctionProperties().set( 404 MachineFunctionProperties::Property::NoVRegs); 405 } 406 407 private: 408 typedef enum { DebugUse = false, RegularUse = true } DebugType; 409 410 void ReadRegister(MCRegister Reg, MachineInstr &Reader, DebugType DT); 411 void ForwardCopyPropagateBlock(MachineBasicBlock &MBB); 412 void BackwardCopyPropagateBlock(MachineBasicBlock &MBB); 413 void EliminateSpillageCopies(MachineBasicBlock &MBB); 414 bool eraseIfRedundant(MachineInstr &Copy, MCRegister Src, MCRegister Def); 415 void forwardUses(MachineInstr &MI); 416 void propagateDefs(MachineInstr &MI); 417 bool isForwardableRegClassCopy(const MachineInstr &Copy, 418 const MachineInstr &UseI, unsigned UseIdx); 419 bool isBackwardPropagatableRegClassCopy(const MachineInstr &Copy, 420 const MachineInstr &UseI, 421 unsigned UseIdx); 422 bool hasImplicitOverlap(const MachineInstr &MI, const MachineOperand &Use); 423 bool hasOverlappingMultipleDef(const MachineInstr &MI, 424 const MachineOperand &MODef, Register Def); 425 426 /// Candidates for deletion. 427 SmallSetVector<MachineInstr *, 8> MaybeDeadCopies; 428 429 /// Multimap tracking debug users in current BB 430 DenseMap<MachineInstr *, SmallSet<MachineInstr *, 2>> CopyDbgUsers; 431 432 CopyTracker Tracker; 433 434 bool Changed = false; 435 }; 436 437 } // end anonymous namespace 438 439 char MachineCopyPropagation::ID = 0; 440 441 char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID; 442 443 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE, 444 "Machine Copy Propagation Pass", false, false) 445 446 void MachineCopyPropagation::ReadRegister(MCRegister Reg, MachineInstr &Reader, 447 DebugType DT) { 448 // If 'Reg' is defined by a copy, the copy is no longer a candidate 449 // for elimination. If a copy is "read" by a debug user, record the user 450 // for propagation. 451 for (MCRegUnit Unit : TRI->regunits(Reg)) { 452 if (MachineInstr *Copy = Tracker.findCopyForUnit(Unit, *TRI)) { 453 if (DT == RegularUse) { 454 LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump()); 455 MaybeDeadCopies.remove(Copy); 456 } else { 457 CopyDbgUsers[Copy].insert(&Reader); 458 } 459 } 460 } 461 } 462 463 /// Return true if \p PreviousCopy did copy register \p Src to register \p Def. 464 /// This fact may have been obscured by sub register usage or may not be true at 465 /// all even though Src and Def are subregisters of the registers used in 466 /// PreviousCopy. e.g. 467 /// isNopCopy("ecx = COPY eax", AX, CX) == true 468 /// isNopCopy("ecx = COPY eax", AH, CL) == false 469 static bool isNopCopy(const MachineInstr &PreviousCopy, MCRegister Src, 470 MCRegister Def, const TargetRegisterInfo *TRI, 471 const TargetInstrInfo *TII, bool UseCopyInstr) { 472 473 std::optional<DestSourcePair> CopyOperands = 474 isCopyInstr(PreviousCopy, *TII, UseCopyInstr); 475 MCRegister PreviousSrc = CopyOperands->Source->getReg().asMCReg(); 476 MCRegister PreviousDef = CopyOperands->Destination->getReg().asMCReg(); 477 if (Src == PreviousSrc && Def == PreviousDef) 478 return true; 479 if (!TRI->isSubRegister(PreviousSrc, Src)) 480 return false; 481 unsigned SubIdx = TRI->getSubRegIndex(PreviousSrc, Src); 482 return SubIdx == TRI->getSubRegIndex(PreviousDef, Def); 483 } 484 485 /// Remove instruction \p Copy if there exists a previous copy that copies the 486 /// register \p Src to the register \p Def; This may happen indirectly by 487 /// copying the super registers. 488 bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, 489 MCRegister Src, MCRegister Def) { 490 // Avoid eliminating a copy from/to a reserved registers as we cannot predict 491 // the value (Example: The sparc zero register is writable but stays zero). 492 if (MRI->isReserved(Src) || MRI->isReserved(Def)) 493 return false; 494 495 // Search for an existing copy. 496 MachineInstr *PrevCopy = 497 Tracker.findAvailCopy(Copy, Def, *TRI, *TII, UseCopyInstr); 498 if (!PrevCopy) 499 return false; 500 501 auto PrevCopyOperands = isCopyInstr(*PrevCopy, *TII, UseCopyInstr); 502 // Check that the existing copy uses the correct sub registers. 503 if (PrevCopyOperands->Destination->isDead()) 504 return false; 505 if (!isNopCopy(*PrevCopy, Src, Def, TRI, TII, UseCopyInstr)) 506 return false; 507 508 LLVM_DEBUG(dbgs() << "MCP: copy is a NOP, removing: "; Copy.dump()); 509 510 // Copy was redundantly redefining either Src or Def. Remove earlier kill 511 // flags between Copy and PrevCopy because the value will be reused now. 512 std::optional<DestSourcePair> CopyOperands = 513 isCopyInstr(Copy, *TII, UseCopyInstr); 514 assert(CopyOperands); 515 516 Register CopyDef = CopyOperands->Destination->getReg(); 517 assert(CopyDef == Src || CopyDef == Def); 518 for (MachineInstr &MI : 519 make_range(PrevCopy->getIterator(), Copy.getIterator())) 520 MI.clearRegisterKills(CopyDef, TRI); 521 522 // Clear undef flag from remaining copy if needed. 523 if (!CopyOperands->Source->isUndef()) { 524 PrevCopy->getOperand(PrevCopyOperands->Source->getOperandNo()) 525 .setIsUndef(false); 526 } 527 528 Copy.eraseFromParent(); 529 Changed = true; 530 ++NumDeletes; 531 return true; 532 } 533 534 bool MachineCopyPropagation::isBackwardPropagatableRegClassCopy( 535 const MachineInstr &Copy, const MachineInstr &UseI, unsigned UseIdx) { 536 std::optional<DestSourcePair> CopyOperands = 537 isCopyInstr(Copy, *TII, UseCopyInstr); 538 Register Def = CopyOperands->Destination->getReg(); 539 540 if (const TargetRegisterClass *URC = 541 UseI.getRegClassConstraint(UseIdx, TII, TRI)) 542 return URC->contains(Def); 543 544 // We don't process further if UseI is a COPY, since forward copy propagation 545 // should handle that. 546 return false; 547 } 548 549 /// Decide whether we should forward the source of \param Copy to its use in 550 /// \param UseI based on the physical register class constraints of the opcode 551 /// and avoiding introducing more cross-class COPYs. 552 bool MachineCopyPropagation::isForwardableRegClassCopy(const MachineInstr &Copy, 553 const MachineInstr &UseI, 554 unsigned UseIdx) { 555 std::optional<DestSourcePair> CopyOperands = 556 isCopyInstr(Copy, *TII, UseCopyInstr); 557 Register CopySrcReg = CopyOperands->Source->getReg(); 558 559 // If the new register meets the opcode register constraints, then allow 560 // forwarding. 561 if (const TargetRegisterClass *URC = 562 UseI.getRegClassConstraint(UseIdx, TII, TRI)) 563 return URC->contains(CopySrcReg); 564 565 auto UseICopyOperands = isCopyInstr(UseI, *TII, UseCopyInstr); 566 if (!UseICopyOperands) 567 return false; 568 569 /// COPYs don't have register class constraints, so if the user instruction 570 /// is a COPY, we just try to avoid introducing additional cross-class 571 /// COPYs. For example: 572 /// 573 /// RegClassA = COPY RegClassB // Copy parameter 574 /// ... 575 /// RegClassB = COPY RegClassA // UseI parameter 576 /// 577 /// which after forwarding becomes 578 /// 579 /// RegClassA = COPY RegClassB 580 /// ... 581 /// RegClassB = COPY RegClassB 582 /// 583 /// so we have reduced the number of cross-class COPYs and potentially 584 /// introduced a nop COPY that can be removed. 585 586 // Allow forwarding if src and dst belong to any common class, so long as they 587 // don't belong to any (possibly smaller) common class that requires copies to 588 // go via a different class. 589 Register UseDstReg = UseICopyOperands->Destination->getReg(); 590 bool Found = false; 591 bool IsCrossClass = false; 592 for (const TargetRegisterClass *RC : TRI->regclasses()) { 593 if (RC->contains(CopySrcReg) && RC->contains(UseDstReg)) { 594 Found = true; 595 if (TRI->getCrossCopyRegClass(RC) != RC) { 596 IsCrossClass = true; 597 break; 598 } 599 } 600 } 601 if (!Found) 602 return false; 603 if (!IsCrossClass) 604 return true; 605 // The forwarded copy would be cross-class. Only do this if the original copy 606 // was also cross-class. 607 Register CopyDstReg = CopyOperands->Destination->getReg(); 608 for (const TargetRegisterClass *RC : TRI->regclasses()) { 609 if (RC->contains(CopySrcReg) && RC->contains(CopyDstReg) && 610 TRI->getCrossCopyRegClass(RC) != RC) 611 return true; 612 } 613 return false; 614 } 615 616 /// Check that \p MI does not have implicit uses that overlap with it's \p Use 617 /// operand (the register being replaced), since these can sometimes be 618 /// implicitly tied to other operands. For example, on AMDGPU: 619 /// 620 /// V_MOVRELS_B32_e32 %VGPR2, %M0<imp-use>, %EXEC<imp-use>, %VGPR2_VGPR3_VGPR4_VGPR5<imp-use> 621 /// 622 /// the %VGPR2 is implicitly tied to the larger reg operand, but we have no 623 /// way of knowing we need to update the latter when updating the former. 624 bool MachineCopyPropagation::hasImplicitOverlap(const MachineInstr &MI, 625 const MachineOperand &Use) { 626 for (const MachineOperand &MIUse : MI.uses()) 627 if (&MIUse != &Use && MIUse.isReg() && MIUse.isImplicit() && 628 MIUse.isUse() && TRI->regsOverlap(Use.getReg(), MIUse.getReg())) 629 return true; 630 631 return false; 632 } 633 634 /// For an MI that has multiple definitions, check whether \p MI has 635 /// a definition that overlaps with another of its definitions. 636 /// For example, on ARM: umull r9, r9, lr, r0 637 /// The umull instruction is unpredictable unless RdHi and RdLo are different. 638 bool MachineCopyPropagation::hasOverlappingMultipleDef( 639 const MachineInstr &MI, const MachineOperand &MODef, Register Def) { 640 for (const MachineOperand &MIDef : MI.defs()) { 641 if ((&MIDef != &MODef) && MIDef.isReg() && 642 TRI->regsOverlap(Def, MIDef.getReg())) 643 return true; 644 } 645 646 return false; 647 } 648 649 /// Look for available copies whose destination register is used by \p MI and 650 /// replace the use in \p MI with the copy's source register. 651 void MachineCopyPropagation::forwardUses(MachineInstr &MI) { 652 if (!Tracker.hasAnyCopies()) 653 return; 654 655 // Look for non-tied explicit vreg uses that have an active COPY 656 // instruction that defines the physical register allocated to them. 657 // Replace the vreg with the source of the active COPY. 658 for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx < OpEnd; 659 ++OpIdx) { 660 MachineOperand &MOUse = MI.getOperand(OpIdx); 661 // Don't forward into undef use operands since doing so can cause problems 662 // with the machine verifier, since it doesn't treat undef reads as reads, 663 // so we can end up with a live range that ends on an undef read, leading to 664 // an error that the live range doesn't end on a read of the live range 665 // register. 666 if (!MOUse.isReg() || MOUse.isTied() || MOUse.isUndef() || MOUse.isDef() || 667 MOUse.isImplicit()) 668 continue; 669 670 if (!MOUse.getReg()) 671 continue; 672 673 // Check that the register is marked 'renamable' so we know it is safe to 674 // rename it without violating any constraints that aren't expressed in the 675 // IR (e.g. ABI or opcode requirements). 676 if (!MOUse.isRenamable()) 677 continue; 678 679 MachineInstr *Copy = Tracker.findAvailCopy(MI, MOUse.getReg().asMCReg(), 680 *TRI, *TII, UseCopyInstr); 681 if (!Copy) 682 continue; 683 684 std::optional<DestSourcePair> CopyOperands = 685 isCopyInstr(*Copy, *TII, UseCopyInstr); 686 Register CopyDstReg = CopyOperands->Destination->getReg(); 687 const MachineOperand &CopySrc = *CopyOperands->Source; 688 Register CopySrcReg = CopySrc.getReg(); 689 690 Register ForwardedReg = CopySrcReg; 691 // MI might use a sub-register of the Copy destination, in which case the 692 // forwarded register is the matching sub-register of the Copy source. 693 if (MOUse.getReg() != CopyDstReg) { 694 unsigned SubRegIdx = TRI->getSubRegIndex(CopyDstReg, MOUse.getReg()); 695 assert(SubRegIdx && 696 "MI source is not a sub-register of Copy destination"); 697 ForwardedReg = TRI->getSubReg(CopySrcReg, SubRegIdx); 698 if (!ForwardedReg) { 699 LLVM_DEBUG(dbgs() << "MCP: Copy source does not have sub-register " 700 << TRI->getSubRegIndexName(SubRegIdx) << '\n'); 701 continue; 702 } 703 } 704 705 // Don't forward COPYs of reserved regs unless they are constant. 706 if (MRI->isReserved(CopySrcReg) && !MRI->isConstantPhysReg(CopySrcReg)) 707 continue; 708 709 if (!isForwardableRegClassCopy(*Copy, MI, OpIdx)) 710 continue; 711 712 if (hasImplicitOverlap(MI, MOUse)) 713 continue; 714 715 // Check that the instruction is not a copy that partially overwrites the 716 // original copy source that we are about to use. The tracker mechanism 717 // cannot cope with that. 718 if (isCopyInstr(MI, *TII, UseCopyInstr) && 719 MI.modifiesRegister(CopySrcReg, TRI) && 720 !MI.definesRegister(CopySrcReg)) { 721 LLVM_DEBUG(dbgs() << "MCP: Copy source overlap with dest in " << MI); 722 continue; 723 } 724 725 if (!DebugCounter::shouldExecute(FwdCounter)) { 726 LLVM_DEBUG(dbgs() << "MCP: Skipping forwarding due to debug counter:\n " 727 << MI); 728 continue; 729 } 730 731 LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MOUse.getReg(), TRI) 732 << "\n with " << printReg(ForwardedReg, TRI) 733 << "\n in " << MI << " from " << *Copy); 734 735 MOUse.setReg(ForwardedReg); 736 737 if (!CopySrc.isRenamable()) 738 MOUse.setIsRenamable(false); 739 MOUse.setIsUndef(CopySrc.isUndef()); 740 741 LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 742 743 // Clear kill markers that may have been invalidated. 744 for (MachineInstr &KMI : 745 make_range(Copy->getIterator(), std::next(MI.getIterator()))) 746 KMI.clearRegisterKills(CopySrcReg, TRI); 747 748 ++NumCopyForwards; 749 Changed = true; 750 } 751 } 752 753 void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) { 754 LLVM_DEBUG(dbgs() << "MCP: ForwardCopyPropagateBlock " << MBB.getName() 755 << "\n"); 756 757 for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) { 758 // Analyze copies (which don't overlap themselves). 759 std::optional<DestSourcePair> CopyOperands = 760 isCopyInstr(MI, *TII, UseCopyInstr); 761 if (CopyOperands) { 762 763 Register RegSrc = CopyOperands->Source->getReg(); 764 Register RegDef = CopyOperands->Destination->getReg(); 765 766 if (!TRI->regsOverlap(RegDef, RegSrc)) { 767 assert(RegDef.isPhysical() && RegSrc.isPhysical() && 768 "MachineCopyPropagation should be run after register allocation!"); 769 770 MCRegister Def = RegDef.asMCReg(); 771 MCRegister Src = RegSrc.asMCReg(); 772 773 // The two copies cancel out and the source of the first copy 774 // hasn't been overridden, eliminate the second one. e.g. 775 // %ecx = COPY %eax 776 // ... nothing clobbered eax. 777 // %eax = COPY %ecx 778 // => 779 // %ecx = COPY %eax 780 // 781 // or 782 // 783 // %ecx = COPY %eax 784 // ... nothing clobbered eax. 785 // %ecx = COPY %eax 786 // => 787 // %ecx = COPY %eax 788 if (eraseIfRedundant(MI, Def, Src) || eraseIfRedundant(MI, Src, Def)) 789 continue; 790 791 forwardUses(MI); 792 793 // Src may have been changed by forwardUses() 794 CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr); 795 Src = CopyOperands->Source->getReg().asMCReg(); 796 797 // If Src is defined by a previous copy, the previous copy cannot be 798 // eliminated. 799 ReadRegister(Src, MI, RegularUse); 800 for (const MachineOperand &MO : MI.implicit_operands()) { 801 if (!MO.isReg() || !MO.readsReg()) 802 continue; 803 MCRegister Reg = MO.getReg().asMCReg(); 804 if (!Reg) 805 continue; 806 ReadRegister(Reg, MI, RegularUse); 807 } 808 809 LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI.dump()); 810 811 // Copy is now a candidate for deletion. 812 if (!MRI->isReserved(Def)) 813 MaybeDeadCopies.insert(&MI); 814 815 // If 'Def' is previously source of another copy, then this earlier copy's 816 // source is no longer available. e.g. 817 // %xmm9 = copy %xmm2 818 // ... 819 // %xmm2 = copy %xmm0 820 // ... 821 // %xmm2 = copy %xmm9 822 Tracker.clobberRegister(Def, *TRI, *TII, UseCopyInstr); 823 824 for (const MachineOperand &MO : MI.implicit_operands()) { 825 if (!MO.isReg() || !MO.isDef()) 826 continue; 827 MCRegister Reg = MO.getReg().asMCReg(); 828 if (!Reg) 829 continue; 830 Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 831 } 832 833 Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 834 continue; 835 } 836 } 837 838 // Clobber any earlyclobber regs first. 839 for (const MachineOperand &MO : MI.operands()) 840 if (MO.isReg() && MO.isEarlyClobber()) { 841 MCRegister Reg = MO.getReg().asMCReg(); 842 // If we have a tied earlyclobber, that means it is also read by this 843 // instruction, so we need to make sure we don't remove it as dead 844 // later. 845 if (MO.isTied()) 846 ReadRegister(Reg, MI, RegularUse); 847 Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 848 } 849 850 forwardUses(MI); 851 852 // Not a copy. 853 SmallVector<Register, 2> Defs; 854 const MachineOperand *RegMask = nullptr; 855 for (const MachineOperand &MO : MI.operands()) { 856 if (MO.isRegMask()) 857 RegMask = &MO; 858 if (!MO.isReg()) 859 continue; 860 Register Reg = MO.getReg(); 861 if (!Reg) 862 continue; 863 864 assert(!Reg.isVirtual() && 865 "MachineCopyPropagation should be run after register allocation!"); 866 867 if (MO.isDef() && !MO.isEarlyClobber()) { 868 Defs.push_back(Reg.asMCReg()); 869 continue; 870 } else if (MO.readsReg()) 871 ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse); 872 } 873 874 // The instruction has a register mask operand which means that it clobbers 875 // a large set of registers. Treat clobbered registers the same way as 876 // defined registers. 877 if (RegMask) { 878 // Erase any MaybeDeadCopies whose destination register is clobbered. 879 for (SmallSetVector<MachineInstr *, 8>::iterator DI = 880 MaybeDeadCopies.begin(); 881 DI != MaybeDeadCopies.end();) { 882 MachineInstr *MaybeDead = *DI; 883 std::optional<DestSourcePair> CopyOperands = 884 isCopyInstr(*MaybeDead, *TII, UseCopyInstr); 885 MCRegister Reg = CopyOperands->Destination->getReg().asMCReg(); 886 assert(!MRI->isReserved(Reg)); 887 888 if (!RegMask->clobbersPhysReg(Reg)) { 889 ++DI; 890 continue; 891 } 892 893 LLVM_DEBUG(dbgs() << "MCP: Removing copy due to regmask clobbering: "; 894 MaybeDead->dump()); 895 896 // Make sure we invalidate any entries in the copy maps before erasing 897 // the instruction. 898 Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 899 900 // erase() will return the next valid iterator pointing to the next 901 // element after the erased one. 902 DI = MaybeDeadCopies.erase(DI); 903 MaybeDead->eraseFromParent(); 904 Changed = true; 905 ++NumDeletes; 906 } 907 } 908 909 // Any previous copy definition or reading the Defs is no longer available. 910 for (MCRegister Reg : Defs) 911 Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 912 } 913 914 // If MBB doesn't have successors, delete the copies whose defs are not used. 915 // If MBB does have successors, then conservative assume the defs are live-out 916 // since we don't want to trust live-in lists. 917 if (MBB.succ_empty()) { 918 for (MachineInstr *MaybeDead : MaybeDeadCopies) { 919 LLVM_DEBUG(dbgs() << "MCP: Removing copy due to no live-out succ: "; 920 MaybeDead->dump()); 921 922 std::optional<DestSourcePair> CopyOperands = 923 isCopyInstr(*MaybeDead, *TII, UseCopyInstr); 924 assert(CopyOperands); 925 926 Register SrcReg = CopyOperands->Source->getReg(); 927 Register DestReg = CopyOperands->Destination->getReg(); 928 assert(!MRI->isReserved(DestReg)); 929 930 // Update matching debug values, if any. 931 SmallVector<MachineInstr *> MaybeDeadDbgUsers( 932 CopyDbgUsers[MaybeDead].begin(), CopyDbgUsers[MaybeDead].end()); 933 MRI->updateDbgUsersToReg(DestReg.asMCReg(), SrcReg.asMCReg(), 934 MaybeDeadDbgUsers); 935 936 MaybeDead->eraseFromParent(); 937 Changed = true; 938 ++NumDeletes; 939 } 940 } 941 942 MaybeDeadCopies.clear(); 943 CopyDbgUsers.clear(); 944 Tracker.clear(); 945 } 946 947 static bool isBackwardPropagatableCopy(const DestSourcePair &CopyOperands, 948 const MachineRegisterInfo &MRI, 949 const TargetInstrInfo &TII) { 950 Register Def = CopyOperands.Destination->getReg(); 951 Register Src = CopyOperands.Source->getReg(); 952 953 if (!Def || !Src) 954 return false; 955 956 if (MRI.isReserved(Def) || MRI.isReserved(Src)) 957 return false; 958 959 return CopyOperands.Source->isRenamable() && CopyOperands.Source->isKill(); 960 } 961 962 void MachineCopyPropagation::propagateDefs(MachineInstr &MI) { 963 if (!Tracker.hasAnyCopies()) 964 return; 965 966 for (unsigned OpIdx = 0, OpEnd = MI.getNumOperands(); OpIdx != OpEnd; 967 ++OpIdx) { 968 MachineOperand &MODef = MI.getOperand(OpIdx); 969 970 if (!MODef.isReg() || MODef.isUse()) 971 continue; 972 973 // Ignore non-trivial cases. 974 if (MODef.isTied() || MODef.isUndef() || MODef.isImplicit()) 975 continue; 976 977 if (!MODef.getReg()) 978 continue; 979 980 // We only handle if the register comes from a vreg. 981 if (!MODef.isRenamable()) 982 continue; 983 984 MachineInstr *Copy = Tracker.findAvailBackwardCopy( 985 MI, MODef.getReg().asMCReg(), *TRI, *TII, UseCopyInstr); 986 if (!Copy) 987 continue; 988 989 std::optional<DestSourcePair> CopyOperands = 990 isCopyInstr(*Copy, *TII, UseCopyInstr); 991 Register Def = CopyOperands->Destination->getReg(); 992 Register Src = CopyOperands->Source->getReg(); 993 994 if (MODef.getReg() != Src) 995 continue; 996 997 if (!isBackwardPropagatableRegClassCopy(*Copy, MI, OpIdx)) 998 continue; 999 1000 if (hasImplicitOverlap(MI, MODef)) 1001 continue; 1002 1003 if (hasOverlappingMultipleDef(MI, MODef, Def)) 1004 continue; 1005 1006 LLVM_DEBUG(dbgs() << "MCP: Replacing " << printReg(MODef.getReg(), TRI) 1007 << "\n with " << printReg(Def, TRI) << "\n in " 1008 << MI << " from " << *Copy); 1009 1010 MODef.setReg(Def); 1011 MODef.setIsRenamable(CopyOperands->Destination->isRenamable()); 1012 1013 LLVM_DEBUG(dbgs() << "MCP: After replacement: " << MI << "\n"); 1014 MaybeDeadCopies.insert(Copy); 1015 Changed = true; 1016 ++NumCopyBackwardPropagated; 1017 } 1018 } 1019 1020 void MachineCopyPropagation::BackwardCopyPropagateBlock( 1021 MachineBasicBlock &MBB) { 1022 LLVM_DEBUG(dbgs() << "MCP: BackwardCopyPropagateBlock " << MBB.getName() 1023 << "\n"); 1024 1025 for (MachineInstr &MI : llvm::make_early_inc_range(llvm::reverse(MBB))) { 1026 // Ignore non-trivial COPYs. 1027 std::optional<DestSourcePair> CopyOperands = 1028 isCopyInstr(MI, *TII, UseCopyInstr); 1029 if (CopyOperands && MI.getNumOperands() == 2) { 1030 Register DefReg = CopyOperands->Destination->getReg(); 1031 Register SrcReg = CopyOperands->Source->getReg(); 1032 1033 if (!TRI->regsOverlap(DefReg, SrcReg)) { 1034 // Unlike forward cp, we don't invoke propagateDefs here, 1035 // just let forward cp do COPY-to-COPY propagation. 1036 if (isBackwardPropagatableCopy(*CopyOperands, *MRI, *TII)) { 1037 Tracker.invalidateRegister(SrcReg.asMCReg(), *TRI, *TII, 1038 UseCopyInstr); 1039 Tracker.invalidateRegister(DefReg.asMCReg(), *TRI, *TII, 1040 UseCopyInstr); 1041 Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 1042 continue; 1043 } 1044 } 1045 } 1046 1047 // Invalidate any earlyclobber regs first. 1048 for (const MachineOperand &MO : MI.operands()) 1049 if (MO.isReg() && MO.isEarlyClobber()) { 1050 MCRegister Reg = MO.getReg().asMCReg(); 1051 if (!Reg) 1052 continue; 1053 Tracker.invalidateRegister(Reg, *TRI, *TII, UseCopyInstr); 1054 } 1055 1056 propagateDefs(MI); 1057 for (const MachineOperand &MO : MI.operands()) { 1058 if (!MO.isReg()) 1059 continue; 1060 1061 if (!MO.getReg()) 1062 continue; 1063 1064 if (MO.isDef()) 1065 Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII, 1066 UseCopyInstr); 1067 1068 if (MO.readsReg()) { 1069 if (MO.isDebug()) { 1070 // Check if the register in the debug instruction is utilized 1071 // in a copy instruction, so we can update the debug info if the 1072 // register is changed. 1073 for (MCRegUnit Unit : TRI->regunits(MO.getReg().asMCReg())) { 1074 if (auto *Copy = Tracker.findCopyDefViaUnit(Unit, *TRI)) { 1075 CopyDbgUsers[Copy].insert(&MI); 1076 } 1077 } 1078 } else { 1079 Tracker.invalidateRegister(MO.getReg().asMCReg(), *TRI, *TII, 1080 UseCopyInstr); 1081 } 1082 } 1083 } 1084 } 1085 1086 for (auto *Copy : MaybeDeadCopies) { 1087 std::optional<DestSourcePair> CopyOperands = 1088 isCopyInstr(*Copy, *TII, UseCopyInstr); 1089 Register Src = CopyOperands->Source->getReg(); 1090 Register Def = CopyOperands->Destination->getReg(); 1091 SmallVector<MachineInstr *> MaybeDeadDbgUsers(CopyDbgUsers[Copy].begin(), 1092 CopyDbgUsers[Copy].end()); 1093 1094 MRI->updateDbgUsersToReg(Src.asMCReg(), Def.asMCReg(), MaybeDeadDbgUsers); 1095 Copy->eraseFromParent(); 1096 ++NumDeletes; 1097 } 1098 1099 MaybeDeadCopies.clear(); 1100 CopyDbgUsers.clear(); 1101 Tracker.clear(); 1102 } 1103 1104 static void LLVM_ATTRIBUTE_UNUSED printSpillReloadChain( 1105 DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &SpillChain, 1106 DenseMap<MachineInstr *, SmallVector<MachineInstr *>> &ReloadChain, 1107 MachineInstr *Leader) { 1108 auto &SC = SpillChain[Leader]; 1109 auto &RC = ReloadChain[Leader]; 1110 for (auto I = SC.rbegin(), E = SC.rend(); I != E; ++I) 1111 (*I)->dump(); 1112 for (MachineInstr *MI : RC) 1113 MI->dump(); 1114 } 1115 1116 // Remove spill-reload like copy chains. For example 1117 // r0 = COPY r1 1118 // r1 = COPY r2 1119 // r2 = COPY r3 1120 // r3 = COPY r4 1121 // <def-use r4> 1122 // r4 = COPY r3 1123 // r3 = COPY r2 1124 // r2 = COPY r1 1125 // r1 = COPY r0 1126 // will be folded into 1127 // r0 = COPY r1 1128 // r1 = COPY r4 1129 // <def-use r4> 1130 // r4 = COPY r1 1131 // r1 = COPY r0 1132 // TODO: Currently we don't track usage of r0 outside the chain, so we 1133 // conservatively keep its value as it was before the rewrite. 1134 // 1135 // The algorithm is trying to keep 1136 // property#1: No Def of spill COPY in the chain is used or defined until the 1137 // paired reload COPY in the chain uses the Def. 1138 // 1139 // property#2: NO Source of COPY in the chain is used or defined until the next 1140 // COPY in the chain defines the Source, except the innermost spill-reload 1141 // pair. 1142 // 1143 // The algorithm is conducted by checking every COPY inside the MBB, assuming 1144 // the COPY is a reload COPY, then try to find paired spill COPY by searching 1145 // the COPY defines the Src of the reload COPY backward. If such pair is found, 1146 // it either belongs to an existing chain or a new chain depends on 1147 // last available COPY uses the Def of the reload COPY. 1148 // Implementation notes, we use CopyTracker::findLastDefCopy(Reg, ...) to find 1149 // out last COPY that defines Reg; we use CopyTracker::findLastUseCopy(Reg, ...) 1150 // to find out last COPY that uses Reg. When we are encountered with a Non-COPY 1151 // instruction, we check registers in the operands of this instruction. If this 1152 // Reg is defined by a COPY, we untrack this Reg via 1153 // CopyTracker::clobberRegister(Reg, ...). 1154 void MachineCopyPropagation::EliminateSpillageCopies(MachineBasicBlock &MBB) { 1155 // ChainLeader maps MI inside a spill-reload chain to its innermost reload COPY. 1156 // Thus we can track if a MI belongs to an existing spill-reload chain. 1157 DenseMap<MachineInstr *, MachineInstr *> ChainLeader; 1158 // SpillChain maps innermost reload COPY of a spill-reload chain to a sequence 1159 // of COPYs that forms spills of a spill-reload chain. 1160 // ReloadChain maps innermost reload COPY of a spill-reload chain to a 1161 // sequence of COPYs that forms reloads of a spill-reload chain. 1162 DenseMap<MachineInstr *, SmallVector<MachineInstr *>> SpillChain, ReloadChain; 1163 // If a COPY's Source has use or def until next COPY defines the Source, 1164 // we put the COPY in this set to keep property#2. 1165 DenseSet<const MachineInstr *> CopySourceInvalid; 1166 1167 auto TryFoldSpillageCopies = 1168 [&, this](const SmallVectorImpl<MachineInstr *> &SC, 1169 const SmallVectorImpl<MachineInstr *> &RC) { 1170 assert(SC.size() == RC.size() && "Spill-reload should be paired"); 1171 1172 // We need at least 3 pairs of copies for the transformation to apply, 1173 // because the first outermost pair cannot be removed since we don't 1174 // recolor outside of the chain and that we need at least one temporary 1175 // spill slot to shorten the chain. If we only have a chain of two 1176 // pairs, we already have the shortest sequence this code can handle: 1177 // the outermost pair for the temporary spill slot, and the pair that 1178 // use that temporary spill slot for the other end of the chain. 1179 // TODO: We might be able to simplify to one spill-reload pair if collecting 1180 // more infomation about the outermost COPY. 1181 if (SC.size() <= 2) 1182 return; 1183 1184 // If violate property#2, we don't fold the chain. 1185 for (const MachineInstr *Spill : drop_begin(SC)) 1186 if (CopySourceInvalid.count(Spill)) 1187 return; 1188 1189 for (const MachineInstr *Reload : drop_end(RC)) 1190 if (CopySourceInvalid.count(Reload)) 1191 return; 1192 1193 auto CheckCopyConstraint = [this](Register Def, Register Src) { 1194 for (const TargetRegisterClass *RC : TRI->regclasses()) { 1195 if (RC->contains(Def) && RC->contains(Src)) 1196 return true; 1197 } 1198 return false; 1199 }; 1200 1201 auto UpdateReg = [](MachineInstr *MI, const MachineOperand *Old, 1202 const MachineOperand *New) { 1203 for (MachineOperand &MO : MI->operands()) { 1204 if (&MO == Old) 1205 MO.setReg(New->getReg()); 1206 } 1207 }; 1208 1209 std::optional<DestSourcePair> InnerMostSpillCopy = 1210 isCopyInstr(*SC[0], *TII, UseCopyInstr); 1211 std::optional<DestSourcePair> OuterMostSpillCopy = 1212 isCopyInstr(*SC.back(), *TII, UseCopyInstr); 1213 std::optional<DestSourcePair> InnerMostReloadCopy = 1214 isCopyInstr(*RC[0], *TII, UseCopyInstr); 1215 std::optional<DestSourcePair> OuterMostReloadCopy = 1216 isCopyInstr(*RC.back(), *TII, UseCopyInstr); 1217 if (!CheckCopyConstraint(OuterMostSpillCopy->Source->getReg(), 1218 InnerMostSpillCopy->Source->getReg()) || 1219 !CheckCopyConstraint(InnerMostReloadCopy->Destination->getReg(), 1220 OuterMostReloadCopy->Destination->getReg())) 1221 return; 1222 1223 SpillageChainsLength += SC.size() + RC.size(); 1224 NumSpillageChains += 1; 1225 UpdateReg(SC[0], InnerMostSpillCopy->Destination, 1226 OuterMostSpillCopy->Source); 1227 UpdateReg(RC[0], InnerMostReloadCopy->Source, 1228 OuterMostReloadCopy->Destination); 1229 1230 for (size_t I = 1; I < SC.size() - 1; ++I) { 1231 SC[I]->eraseFromParent(); 1232 RC[I]->eraseFromParent(); 1233 NumDeletes += 2; 1234 } 1235 }; 1236 1237 auto IsFoldableCopy = [this](const MachineInstr &MaybeCopy) { 1238 if (MaybeCopy.getNumImplicitOperands() > 0) 1239 return false; 1240 std::optional<DestSourcePair> CopyOperands = 1241 isCopyInstr(MaybeCopy, *TII, UseCopyInstr); 1242 if (!CopyOperands) 1243 return false; 1244 Register Src = CopyOperands->Source->getReg(); 1245 Register Def = CopyOperands->Destination->getReg(); 1246 return Src && Def && !TRI->regsOverlap(Src, Def) && 1247 CopyOperands->Source->isRenamable() && 1248 CopyOperands->Destination->isRenamable(); 1249 }; 1250 1251 auto IsSpillReloadPair = [&, this](const MachineInstr &Spill, 1252 const MachineInstr &Reload) { 1253 if (!IsFoldableCopy(Spill) || !IsFoldableCopy(Reload)) 1254 return false; 1255 std::optional<DestSourcePair> SpillCopy = 1256 isCopyInstr(Spill, *TII, UseCopyInstr); 1257 std::optional<DestSourcePair> ReloadCopy = 1258 isCopyInstr(Reload, *TII, UseCopyInstr); 1259 if (!SpillCopy || !ReloadCopy) 1260 return false; 1261 return SpillCopy->Source->getReg() == ReloadCopy->Destination->getReg() && 1262 SpillCopy->Destination->getReg() == ReloadCopy->Source->getReg(); 1263 }; 1264 1265 auto IsChainedCopy = [&, this](const MachineInstr &Prev, 1266 const MachineInstr &Current) { 1267 if (!IsFoldableCopy(Prev) || !IsFoldableCopy(Current)) 1268 return false; 1269 std::optional<DestSourcePair> PrevCopy = 1270 isCopyInstr(Prev, *TII, UseCopyInstr); 1271 std::optional<DestSourcePair> CurrentCopy = 1272 isCopyInstr(Current, *TII, UseCopyInstr); 1273 if (!PrevCopy || !CurrentCopy) 1274 return false; 1275 return PrevCopy->Source->getReg() == CurrentCopy->Destination->getReg(); 1276 }; 1277 1278 for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) { 1279 std::optional<DestSourcePair> CopyOperands = 1280 isCopyInstr(MI, *TII, UseCopyInstr); 1281 1282 // Update track information via non-copy instruction. 1283 SmallSet<Register, 8> RegsToClobber; 1284 if (!CopyOperands) { 1285 for (const MachineOperand &MO : MI.operands()) { 1286 if (!MO.isReg()) 1287 continue; 1288 Register Reg = MO.getReg(); 1289 if (!Reg) 1290 continue; 1291 MachineInstr *LastUseCopy = 1292 Tracker.findLastSeenUseInCopy(Reg.asMCReg(), *TRI); 1293 if (LastUseCopy) { 1294 LLVM_DEBUG(dbgs() << "MCP: Copy source of\n"); 1295 LLVM_DEBUG(LastUseCopy->dump()); 1296 LLVM_DEBUG(dbgs() << "might be invalidated by\n"); 1297 LLVM_DEBUG(MI.dump()); 1298 CopySourceInvalid.insert(LastUseCopy); 1299 } 1300 // Must be noted Tracker.clobberRegister(Reg, ...) removes tracking of 1301 // Reg, i.e, COPY that defines Reg is removed from the mapping as well 1302 // as marking COPYs that uses Reg unavailable. 1303 // We don't invoke CopyTracker::clobberRegister(Reg, ...) if Reg is not 1304 // defined by a previous COPY, since we don't want to make COPYs uses 1305 // Reg unavailable. 1306 if (Tracker.findLastSeenDefInCopy(MI, Reg.asMCReg(), *TRI, *TII, 1307 UseCopyInstr)) 1308 // Thus we can keep the property#1. 1309 RegsToClobber.insert(Reg); 1310 } 1311 for (Register Reg : RegsToClobber) { 1312 Tracker.clobberRegister(Reg, *TRI, *TII, UseCopyInstr); 1313 LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Reg, TRI) 1314 << "\n"); 1315 } 1316 continue; 1317 } 1318 1319 Register Src = CopyOperands->Source->getReg(); 1320 Register Def = CopyOperands->Destination->getReg(); 1321 // Check if we can find a pair spill-reload copy. 1322 LLVM_DEBUG(dbgs() << "MCP: Searching paired spill for reload: "); 1323 LLVM_DEBUG(MI.dump()); 1324 MachineInstr *MaybeSpill = 1325 Tracker.findLastSeenDefInCopy(MI, Src.asMCReg(), *TRI, *TII, UseCopyInstr); 1326 bool MaybeSpillIsChained = ChainLeader.count(MaybeSpill); 1327 if (!MaybeSpillIsChained && MaybeSpill && 1328 IsSpillReloadPair(*MaybeSpill, MI)) { 1329 // Check if we already have an existing chain. Now we have a 1330 // spill-reload pair. 1331 // L2: r2 = COPY r3 1332 // L5: r3 = COPY r2 1333 // Looking for a valid COPY before L5 which uses r3. 1334 // This can be serverial cases. 1335 // Case #1: 1336 // No COPY is found, which can be r3 is def-use between (L2, L5), we 1337 // create a new chain for L2 and L5. 1338 // Case #2: 1339 // L2: r2 = COPY r3 1340 // L5: r3 = COPY r2 1341 // Such COPY is found and is L2, we create a new chain for L2 and L5. 1342 // Case #3: 1343 // L2: r2 = COPY r3 1344 // L3: r1 = COPY r3 1345 // L5: r3 = COPY r2 1346 // we create a new chain for L2 and L5. 1347 // Case #4: 1348 // L2: r2 = COPY r3 1349 // L3: r1 = COPY r3 1350 // L4: r3 = COPY r1 1351 // L5: r3 = COPY r2 1352 // Such COPY won't be found since L4 defines r3. we create a new chain 1353 // for L2 and L5. 1354 // Case #5: 1355 // L2: r2 = COPY r3 1356 // L3: r3 = COPY r1 1357 // L4: r1 = COPY r3 1358 // L5: r3 = COPY r2 1359 // COPY is found and is L4 which belongs to an existing chain, we add 1360 // L2 and L5 to this chain. 1361 LLVM_DEBUG(dbgs() << "MCP: Found spill: "); 1362 LLVM_DEBUG(MaybeSpill->dump()); 1363 MachineInstr *MaybePrevReload = 1364 Tracker.findLastSeenUseInCopy(Def.asMCReg(), *TRI); 1365 auto Leader = ChainLeader.find(MaybePrevReload); 1366 MachineInstr *L = nullptr; 1367 if (Leader == ChainLeader.end() || 1368 (MaybePrevReload && !IsChainedCopy(*MaybePrevReload, MI))) { 1369 L = &MI; 1370 assert(!SpillChain.count(L) && 1371 "SpillChain should not have contained newly found chain"); 1372 } else { 1373 assert(MaybePrevReload && 1374 "Found a valid leader through nullptr should not happend"); 1375 L = Leader->second; 1376 assert(SpillChain[L].size() > 0 && 1377 "Existing chain's length should be larger than zero"); 1378 } 1379 assert(!ChainLeader.count(&MI) && !ChainLeader.count(MaybeSpill) && 1380 "Newly found paired spill-reload should not belong to any chain " 1381 "at this point"); 1382 ChainLeader.insert({MaybeSpill, L}); 1383 ChainLeader.insert({&MI, L}); 1384 SpillChain[L].push_back(MaybeSpill); 1385 ReloadChain[L].push_back(&MI); 1386 LLVM_DEBUG(dbgs() << "MCP: Chain " << L << " now is:\n"); 1387 LLVM_DEBUG(printSpillReloadChain(SpillChain, ReloadChain, L)); 1388 } else if (MaybeSpill && !MaybeSpillIsChained) { 1389 // MaybeSpill is unable to pair with MI. That's to say adding MI makes 1390 // the chain invalid. 1391 // The COPY defines Src is no longer considered as a candidate of a 1392 // valid chain. Since we expect the Def of a spill copy isn't used by 1393 // any COPY instruction until a reload copy. For example: 1394 // L1: r1 = COPY r2 1395 // L2: r3 = COPY r1 1396 // If we later have 1397 // L1: r1 = COPY r2 1398 // L2: r3 = COPY r1 1399 // L3: r2 = COPY r1 1400 // L1 and L3 can't be a valid spill-reload pair. 1401 // Thus we keep the property#1. 1402 LLVM_DEBUG(dbgs() << "MCP: Not paired spill-reload:\n"); 1403 LLVM_DEBUG(MaybeSpill->dump()); 1404 LLVM_DEBUG(MI.dump()); 1405 Tracker.clobberRegister(Src.asMCReg(), *TRI, *TII, UseCopyInstr); 1406 LLVM_DEBUG(dbgs() << "MCP: Removed tracking of " << printReg(Src, TRI) 1407 << "\n"); 1408 } 1409 Tracker.trackCopy(&MI, *TRI, *TII, UseCopyInstr); 1410 } 1411 1412 for (auto I = SpillChain.begin(), E = SpillChain.end(); I != E; ++I) { 1413 auto &SC = I->second; 1414 assert(ReloadChain.count(I->first) && 1415 "Reload chain of the same leader should exist"); 1416 auto &RC = ReloadChain[I->first]; 1417 TryFoldSpillageCopies(SC, RC); 1418 } 1419 1420 MaybeDeadCopies.clear(); 1421 CopyDbgUsers.clear(); 1422 Tracker.clear(); 1423 } 1424 1425 bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) { 1426 if (skipFunction(MF.getFunction())) 1427 return false; 1428 1429 bool isSpillageCopyElimEnabled = false; 1430 switch (EnableSpillageCopyElimination) { 1431 case cl::BOU_UNSET: 1432 isSpillageCopyElimEnabled = 1433 MF.getSubtarget().enableSpillageCopyElimination(); 1434 break; 1435 case cl::BOU_TRUE: 1436 isSpillageCopyElimEnabled = true; 1437 break; 1438 case cl::BOU_FALSE: 1439 isSpillageCopyElimEnabled = false; 1440 break; 1441 } 1442 1443 Changed = false; 1444 1445 TRI = MF.getSubtarget().getRegisterInfo(); 1446 TII = MF.getSubtarget().getInstrInfo(); 1447 MRI = &MF.getRegInfo(); 1448 1449 for (MachineBasicBlock &MBB : MF) { 1450 if (isSpillageCopyElimEnabled) 1451 EliminateSpillageCopies(MBB); 1452 BackwardCopyPropagateBlock(MBB); 1453 ForwardCopyPropagateBlock(MBB); 1454 } 1455 1456 return Changed; 1457 } 1458 1459 MachineFunctionPass * 1460 llvm::createMachineCopyPropagationPass(bool UseCopyInstr = false) { 1461 return new MachineCopyPropagation(UseCopyInstr); 1462 } 1463