1 //===- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map -----------------===// 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 file implements the VirtRegMap class. 10 // 11 // It also contains implementations of the Spiller interface, which, given a 12 // virtual register map and a machine function, eliminates all virtual 13 // references by replacing them with physical register references - adding spill 14 // code as necessary. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/CodeGen/VirtRegMap.h" 19 #include "LiveDebugVariables.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/Statistic.h" 22 #include "llvm/CodeGen/LiveInterval.h" 23 #include "llvm/CodeGen/LiveIntervals.h" 24 #include "llvm/CodeGen/LiveStacks.h" 25 #include "llvm/CodeGen/MachineBasicBlock.h" 26 #include "llvm/CodeGen/MachineFrameInfo.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/MachineFunctionPass.h" 29 #include "llvm/CodeGen/MachineInstr.h" 30 #include "llvm/CodeGen/MachineOperand.h" 31 #include "llvm/CodeGen/MachineRegisterInfo.h" 32 #include "llvm/CodeGen/SlotIndexes.h" 33 #include "llvm/CodeGen/TargetInstrInfo.h" 34 #include "llvm/CodeGen/TargetOpcodes.h" 35 #include "llvm/CodeGen/TargetRegisterInfo.h" 36 #include "llvm/CodeGen/TargetSubtargetInfo.h" 37 #include "llvm/Config/llvm-config.h" 38 #include "llvm/MC/LaneBitmask.h" 39 #include "llvm/Pass.h" 40 #include "llvm/Support/Compiler.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include <cassert> 44 #include <iterator> 45 #include <utility> 46 47 using namespace llvm; 48 49 #define DEBUG_TYPE "regalloc" 50 51 STATISTIC(NumSpillSlots, "Number of spill slots allocated"); 52 STATISTIC(NumIdCopies, "Number of identity moves eliminated after rewriting"); 53 54 //===----------------------------------------------------------------------===// 55 // VirtRegMap implementation 56 //===----------------------------------------------------------------------===// 57 58 char VirtRegMap::ID = 0; 59 60 INITIALIZE_PASS(VirtRegMap, "virtregmap", "Virtual Register Map", false, false) 61 62 bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) { 63 MRI = &mf.getRegInfo(); 64 TII = mf.getSubtarget().getInstrInfo(); 65 TRI = mf.getSubtarget().getRegisterInfo(); 66 MF = &mf; 67 68 Virt2PhysMap.clear(); 69 Virt2StackSlotMap.clear(); 70 Virt2SplitMap.clear(); 71 Virt2ShapeMap.clear(); 72 73 grow(); 74 return false; 75 } 76 77 void VirtRegMap::grow() { 78 unsigned NumRegs = MF->getRegInfo().getNumVirtRegs(); 79 Virt2PhysMap.resize(NumRegs); 80 Virt2StackSlotMap.resize(NumRegs); 81 Virt2SplitMap.resize(NumRegs); 82 } 83 84 void VirtRegMap::assignVirt2Phys(Register virtReg, MCPhysReg physReg) { 85 assert(virtReg.isVirtual() && Register::isPhysicalRegister(physReg)); 86 assert(Virt2PhysMap[virtReg.id()] == NO_PHYS_REG && 87 "attempt to assign physical register to already mapped " 88 "virtual register"); 89 assert(!getRegInfo().isReserved(physReg) && 90 "Attempt to map virtReg to a reserved physReg"); 91 Virt2PhysMap[virtReg.id()] = physReg; 92 } 93 94 unsigned VirtRegMap::createSpillSlot(const TargetRegisterClass *RC) { 95 unsigned Size = TRI->getSpillSize(*RC); 96 Align Alignment = TRI->getSpillAlign(*RC); 97 int SS = MF->getFrameInfo().CreateSpillStackObject(Size, Alignment); 98 ++NumSpillSlots; 99 return SS; 100 } 101 102 bool VirtRegMap::hasPreferredPhys(Register VirtReg) const { 103 Register Hint = MRI->getSimpleHint(VirtReg); 104 if (!Hint.isValid()) 105 return false; 106 if (Hint.isVirtual()) 107 Hint = getPhys(Hint); 108 return Register(getPhys(VirtReg)) == Hint; 109 } 110 111 bool VirtRegMap::hasKnownPreference(Register VirtReg) const { 112 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(VirtReg); 113 if (Register::isPhysicalRegister(Hint.second)) 114 return true; 115 if (Register::isVirtualRegister(Hint.second)) 116 return hasPhys(Hint.second); 117 return false; 118 } 119 120 int VirtRegMap::assignVirt2StackSlot(Register virtReg) { 121 assert(virtReg.isVirtual()); 122 assert(Virt2StackSlotMap[virtReg.id()] == NO_STACK_SLOT && 123 "attempt to assign stack slot to already spilled register"); 124 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg); 125 return Virt2StackSlotMap[virtReg.id()] = createSpillSlot(RC); 126 } 127 128 void VirtRegMap::assignVirt2StackSlot(Register virtReg, int SS) { 129 assert(virtReg.isVirtual()); 130 assert(Virt2StackSlotMap[virtReg.id()] == NO_STACK_SLOT && 131 "attempt to assign stack slot to already spilled register"); 132 assert((SS >= 0 || 133 (SS >= MF->getFrameInfo().getObjectIndexBegin())) && 134 "illegal fixed frame index"); 135 Virt2StackSlotMap[virtReg.id()] = SS; 136 } 137 138 void VirtRegMap::print(raw_ostream &OS, const Module*) const { 139 OS << "********** REGISTER MAP **********\n"; 140 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 141 unsigned Reg = Register::index2VirtReg(i); 142 if (Virt2PhysMap[Reg] != (unsigned)VirtRegMap::NO_PHYS_REG) { 143 OS << '[' << printReg(Reg, TRI) << " -> " 144 << printReg(Virt2PhysMap[Reg], TRI) << "] " 145 << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; 146 } 147 } 148 149 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 150 unsigned Reg = Register::index2VirtReg(i); 151 if (Virt2StackSlotMap[Reg] != VirtRegMap::NO_STACK_SLOT) { 152 OS << '[' << printReg(Reg, TRI) << " -> fi#" << Virt2StackSlotMap[Reg] 153 << "] " << TRI->getRegClassName(MRI->getRegClass(Reg)) << "\n"; 154 } 155 } 156 OS << '\n'; 157 } 158 159 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 160 LLVM_DUMP_METHOD void VirtRegMap::dump() const { 161 print(dbgs()); 162 } 163 #endif 164 165 //===----------------------------------------------------------------------===// 166 // VirtRegRewriter 167 //===----------------------------------------------------------------------===// 168 // 169 // The VirtRegRewriter is the last of the register allocator passes. 170 // It rewrites virtual registers to physical registers as specified in the 171 // VirtRegMap analysis. It also updates live-in information on basic blocks 172 // according to LiveIntervals. 173 // 174 namespace { 175 176 class VirtRegRewriter : public MachineFunctionPass { 177 MachineFunction *MF; 178 const TargetRegisterInfo *TRI; 179 const TargetInstrInfo *TII; 180 MachineRegisterInfo *MRI; 181 SlotIndexes *Indexes; 182 LiveIntervals *LIS; 183 VirtRegMap *VRM; 184 bool ClearVirtRegs; 185 186 void rewrite(); 187 void addMBBLiveIns(); 188 bool readsUndefSubreg(const MachineOperand &MO) const; 189 void addLiveInsForSubRanges(const LiveInterval &LI, Register PhysReg) const; 190 void handleIdentityCopy(MachineInstr &MI) const; 191 void expandCopyBundle(MachineInstr &MI) const; 192 bool subRegLiveThrough(const MachineInstr &MI, MCRegister SuperPhysReg) const; 193 194 public: 195 static char ID; 196 VirtRegRewriter(bool ClearVirtRegs_ = true) : 197 MachineFunctionPass(ID), 198 ClearVirtRegs(ClearVirtRegs_) {} 199 200 void getAnalysisUsage(AnalysisUsage &AU) const override; 201 202 bool runOnMachineFunction(MachineFunction&) override; 203 204 MachineFunctionProperties getSetProperties() const override { 205 if (ClearVirtRegs) { 206 return MachineFunctionProperties().set( 207 MachineFunctionProperties::Property::NoVRegs); 208 } 209 210 return MachineFunctionProperties(); 211 } 212 }; 213 214 } // end anonymous namespace 215 216 char VirtRegRewriter::ID = 0; 217 218 char &llvm::VirtRegRewriterID = VirtRegRewriter::ID; 219 220 INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter", 221 "Virtual Register Rewriter", false, false) 222 INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 223 INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 224 INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) 225 INITIALIZE_PASS_DEPENDENCY(LiveStacks) 226 INITIALIZE_PASS_DEPENDENCY(VirtRegMap) 227 INITIALIZE_PASS_END(VirtRegRewriter, "virtregrewriter", 228 "Virtual Register Rewriter", false, false) 229 230 void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const { 231 AU.setPreservesCFG(); 232 AU.addRequired<LiveIntervals>(); 233 AU.addRequired<SlotIndexes>(); 234 AU.addPreserved<SlotIndexes>(); 235 AU.addRequired<LiveDebugVariables>(); 236 AU.addRequired<LiveStacks>(); 237 AU.addPreserved<LiveStacks>(); 238 AU.addRequired<VirtRegMap>(); 239 MachineFunctionPass::getAnalysisUsage(AU); 240 } 241 242 bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { 243 MF = &fn; 244 TRI = MF->getSubtarget().getRegisterInfo(); 245 TII = MF->getSubtarget().getInstrInfo(); 246 MRI = &MF->getRegInfo(); 247 Indexes = &getAnalysis<SlotIndexes>(); 248 LIS = &getAnalysis<LiveIntervals>(); 249 VRM = &getAnalysis<VirtRegMap>(); 250 LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n" 251 << "********** Function: " << MF->getName() << '\n'); 252 LLVM_DEBUG(VRM->dump()); 253 254 // Add kill flags while we still have virtual registers. 255 LIS->addKillFlags(VRM); 256 257 // Live-in lists on basic blocks are required for physregs. 258 addMBBLiveIns(); 259 260 // Rewrite virtual registers. 261 rewrite(); 262 263 // Write out new DBG_VALUE instructions. 264 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); 265 266 if (ClearVirtRegs) { 267 // All machine operands and other references to virtual registers have been 268 // replaced. Remove the virtual registers and release all the transient data. 269 VRM->clearAllVirt(); 270 MRI->clearVirtRegs(); 271 } 272 273 return true; 274 } 275 276 void VirtRegRewriter::addLiveInsForSubRanges(const LiveInterval &LI, 277 Register PhysReg) const { 278 assert(!LI.empty()); 279 assert(LI.hasSubRanges()); 280 281 using SubRangeIteratorPair = 282 std::pair<const LiveInterval::SubRange *, LiveInterval::const_iterator>; 283 284 SmallVector<SubRangeIteratorPair, 4> SubRanges; 285 SlotIndex First; 286 SlotIndex Last; 287 for (const LiveInterval::SubRange &SR : LI.subranges()) { 288 SubRanges.push_back(std::make_pair(&SR, SR.begin())); 289 if (!First.isValid() || SR.segments.front().start < First) 290 First = SR.segments.front().start; 291 if (!Last.isValid() || SR.segments.back().end > Last) 292 Last = SR.segments.back().end; 293 } 294 295 // Check all mbb start positions between First and Last while 296 // simulatenously advancing an iterator for each subrange. 297 for (SlotIndexes::MBBIndexIterator MBBI = Indexes->findMBBIndex(First); 298 MBBI != Indexes->MBBIndexEnd() && MBBI->first <= Last; ++MBBI) { 299 SlotIndex MBBBegin = MBBI->first; 300 // Advance all subrange iterators so that their end position is just 301 // behind MBBBegin (or the iterator is at the end). 302 LaneBitmask LaneMask; 303 for (auto &RangeIterPair : SubRanges) { 304 const LiveInterval::SubRange *SR = RangeIterPair.first; 305 LiveInterval::const_iterator &SRI = RangeIterPair.second; 306 while (SRI != SR->end() && SRI->end <= MBBBegin) 307 ++SRI; 308 if (SRI == SR->end()) 309 continue; 310 if (SRI->start <= MBBBegin) 311 LaneMask |= SR->LaneMask; 312 } 313 if (LaneMask.none()) 314 continue; 315 MachineBasicBlock *MBB = MBBI->second; 316 MBB->addLiveIn(PhysReg, LaneMask); 317 } 318 } 319 320 // Compute MBB live-in lists from virtual register live ranges and their 321 // assignments. 322 void VirtRegRewriter::addMBBLiveIns() { 323 for (unsigned Idx = 0, IdxE = MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) { 324 Register VirtReg = Register::index2VirtReg(Idx); 325 if (MRI->reg_nodbg_empty(VirtReg)) 326 continue; 327 LiveInterval &LI = LIS->getInterval(VirtReg); 328 if (LI.empty() || LIS->intervalIsInOneMBB(LI)) 329 continue; 330 // This is a virtual register that is live across basic blocks. Its 331 // assigned PhysReg must be marked as live-in to those blocks. 332 Register PhysReg = VRM->getPhys(VirtReg); 333 assert(PhysReg != VirtRegMap::NO_PHYS_REG && "Unmapped virtual register."); 334 335 if (LI.hasSubRanges()) { 336 addLiveInsForSubRanges(LI, PhysReg); 337 } else { 338 // Go over MBB begin positions and see if we have segments covering them. 339 // The following works because segments and the MBBIndex list are both 340 // sorted by slot indexes. 341 SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(); 342 for (const auto &Seg : LI) { 343 I = Indexes->advanceMBBIndex(I, Seg.start); 344 for (; I != Indexes->MBBIndexEnd() && I->first < Seg.end; ++I) { 345 MachineBasicBlock *MBB = I->second; 346 MBB->addLiveIn(PhysReg); 347 } 348 } 349 } 350 } 351 352 // Sort and unique MBB LiveIns as we've not checked if SubReg/PhysReg were in 353 // each MBB's LiveIns set before calling addLiveIn on them. 354 for (MachineBasicBlock &MBB : *MF) 355 MBB.sortUniqueLiveIns(); 356 } 357 358 /// Returns true if the given machine operand \p MO only reads undefined lanes. 359 /// The function only works for use operands with a subregister set. 360 bool VirtRegRewriter::readsUndefSubreg(const MachineOperand &MO) const { 361 // Shortcut if the operand is already marked undef. 362 if (MO.isUndef()) 363 return true; 364 365 Register Reg = MO.getReg(); 366 const LiveInterval &LI = LIS->getInterval(Reg); 367 const MachineInstr &MI = *MO.getParent(); 368 SlotIndex BaseIndex = LIS->getInstructionIndex(MI); 369 // This code is only meant to handle reading undefined subregisters which 370 // we couldn't properly detect before. 371 assert(LI.liveAt(BaseIndex) && 372 "Reads of completely dead register should be marked undef already"); 373 unsigned SubRegIdx = MO.getSubReg(); 374 assert(SubRegIdx != 0 && LI.hasSubRanges()); 375 LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(SubRegIdx); 376 // See if any of the relevant subregister liveranges is defined at this point. 377 for (const LiveInterval::SubRange &SR : LI.subranges()) { 378 if ((SR.LaneMask & UseMask).any() && SR.liveAt(BaseIndex)) 379 return false; 380 } 381 return true; 382 } 383 384 void VirtRegRewriter::handleIdentityCopy(MachineInstr &MI) const { 385 if (!MI.isIdentityCopy()) 386 return; 387 LLVM_DEBUG(dbgs() << "Identity copy: " << MI); 388 ++NumIdCopies; 389 390 // Copies like: 391 // %r0 = COPY undef %r0 392 // %al = COPY %al, implicit-def %eax 393 // give us additional liveness information: The target (super-)register 394 // must not be valid before this point. Replace the COPY with a KILL 395 // instruction to maintain this information. 396 if (MI.getOperand(1).isUndef() || MI.getNumOperands() > 2) { 397 MI.setDesc(TII->get(TargetOpcode::KILL)); 398 LLVM_DEBUG(dbgs() << " replace by: " << MI); 399 return; 400 } 401 402 if (Indexes) 403 Indexes->removeSingleMachineInstrFromMaps(MI); 404 MI.eraseFromBundle(); 405 LLVM_DEBUG(dbgs() << " deleted.\n"); 406 } 407 408 /// The liverange splitting logic sometimes produces bundles of copies when 409 /// subregisters are involved. Expand these into a sequence of copy instructions 410 /// after processing the last in the bundle. Does not update LiveIntervals 411 /// which we shouldn't need for this instruction anymore. 412 void VirtRegRewriter::expandCopyBundle(MachineInstr &MI) const { 413 if (!MI.isCopy() && !MI.isKill()) 414 return; 415 416 if (MI.isBundledWithPred() && !MI.isBundledWithSucc()) { 417 SmallVector<MachineInstr *, 2> MIs({&MI}); 418 419 // Only do this when the complete bundle is made out of COPYs and KILLs. 420 MachineBasicBlock &MBB = *MI.getParent(); 421 for (MachineBasicBlock::reverse_instr_iterator I = 422 std::next(MI.getReverseIterator()), E = MBB.instr_rend(); 423 I != E && I->isBundledWithSucc(); ++I) { 424 if (!I->isCopy() && !I->isKill()) 425 return; 426 MIs.push_back(&*I); 427 } 428 MachineInstr *FirstMI = MIs.back(); 429 430 auto anyRegsAlias = [](const MachineInstr *Dst, 431 ArrayRef<MachineInstr *> Srcs, 432 const TargetRegisterInfo *TRI) { 433 for (const MachineInstr *Src : Srcs) 434 if (Src != Dst) 435 if (TRI->regsOverlap(Dst->getOperand(0).getReg(), 436 Src->getOperand(1).getReg())) 437 return true; 438 return false; 439 }; 440 441 // If any of the destination registers in the bundle of copies alias any of 442 // the source registers, try to schedule the instructions to avoid any 443 // clobbering. 444 for (int E = MIs.size(), PrevE = E; E > 1; PrevE = E) { 445 for (int I = E; I--; ) 446 if (!anyRegsAlias(MIs[I], makeArrayRef(MIs).take_front(E), TRI)) { 447 if (I + 1 != E) 448 std::swap(MIs[I], MIs[E - 1]); 449 --E; 450 } 451 if (PrevE == E) { 452 MF->getFunction().getContext().emitError( 453 "register rewriting failed: cycle in copy bundle"); 454 break; 455 } 456 } 457 458 MachineInstr *BundleStart = FirstMI; 459 for (MachineInstr *BundledMI : llvm::reverse(MIs)) { 460 // If instruction is in the middle of the bundle, move it before the 461 // bundle starts, otherwise, just unbundle it. When we get to the last 462 // instruction, the bundle will have been completely undone. 463 if (BundledMI != BundleStart) { 464 BundledMI->removeFromBundle(); 465 MBB.insert(BundleStart, BundledMI); 466 } else if (BundledMI->isBundledWithSucc()) { 467 BundledMI->unbundleFromSucc(); 468 BundleStart = &*std::next(BundledMI->getIterator()); 469 } 470 471 if (Indexes && BundledMI != FirstMI) 472 Indexes->insertMachineInstrInMaps(*BundledMI); 473 } 474 } 475 } 476 477 /// Check whether (part of) \p SuperPhysReg is live through \p MI. 478 /// \pre \p MI defines a subregister of a virtual register that 479 /// has been assigned to \p SuperPhysReg. 480 bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI, 481 MCRegister SuperPhysReg) const { 482 SlotIndex MIIndex = LIS->getInstructionIndex(MI); 483 SlotIndex BeforeMIUses = MIIndex.getBaseIndex(); 484 SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex(); 485 for (MCRegUnitIterator Unit(SuperPhysReg, TRI); Unit.isValid(); ++Unit) { 486 const LiveRange &UnitRange = LIS->getRegUnit(*Unit); 487 // If the regunit is live both before and after MI, 488 // we assume it is live through. 489 // Generally speaking, this is not true, because something like 490 // "RU = op RU" would match that description. 491 // However, we know that we are trying to assess whether 492 // a def of a virtual reg, vreg, is live at the same time of RU. 493 // If we are in the "RU = op RU" situation, that means that vreg 494 // is defined at the same time as RU (i.e., "vreg, RU = op RU"). 495 // Thus, vreg and RU interferes and vreg cannot be assigned to 496 // SuperPhysReg. Therefore, this situation cannot happen. 497 if (UnitRange.liveAt(AfterMIDefs) && UnitRange.liveAt(BeforeMIUses)) 498 return true; 499 } 500 return false; 501 } 502 503 void VirtRegRewriter::rewrite() { 504 bool NoSubRegLiveness = !MRI->subRegLivenessEnabled(); 505 SmallVector<Register, 8> SuperDeads; 506 SmallVector<Register, 8> SuperDefs; 507 SmallVector<Register, 8> SuperKills; 508 509 for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); 510 MBBI != MBBE; ++MBBI) { 511 LLVM_DEBUG(MBBI->print(dbgs(), Indexes)); 512 for (MachineBasicBlock::instr_iterator 513 MII = MBBI->instr_begin(), MIE = MBBI->instr_end(); MII != MIE;) { 514 MachineInstr *MI = &*MII; 515 ++MII; 516 517 for (MachineInstr::mop_iterator MOI = MI->operands_begin(), 518 MOE = MI->operands_end(); MOI != MOE; ++MOI) { 519 MachineOperand &MO = *MOI; 520 521 // Make sure MRI knows about registers clobbered by regmasks. 522 if (MO.isRegMask()) 523 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); 524 525 if (!MO.isReg() || !MO.getReg().isVirtual()) 526 continue; 527 Register VirtReg = MO.getReg(); 528 MCRegister PhysReg = VRM->getPhys(VirtReg); 529 assert(PhysReg != VirtRegMap::NO_PHYS_REG && 530 "Instruction uses unmapped VirtReg"); 531 assert(!MRI->isReserved(PhysReg) && "Reserved register assignment"); 532 533 // Preserve semantics of sub-register operands. 534 unsigned SubReg = MO.getSubReg(); 535 if (SubReg != 0) { 536 if (NoSubRegLiveness || !MRI->shouldTrackSubRegLiveness(VirtReg)) { 537 // A virtual register kill refers to the whole register, so we may 538 // have to add implicit killed operands for the super-register. A 539 // partial redef always kills and redefines the super-register. 540 if ((MO.readsReg() && (MO.isDef() || MO.isKill())) || 541 (MO.isDef() && subRegLiveThrough(*MI, PhysReg))) 542 SuperKills.push_back(PhysReg); 543 544 if (MO.isDef()) { 545 // Also add implicit defs for the super-register. 546 if (MO.isDead()) 547 SuperDeads.push_back(PhysReg); 548 else 549 SuperDefs.push_back(PhysReg); 550 } 551 } else { 552 if (MO.isUse()) { 553 if (readsUndefSubreg(MO)) 554 // We need to add an <undef> flag if the subregister is 555 // completely undefined (and we are not adding super-register 556 // defs). 557 MO.setIsUndef(true); 558 } else if (!MO.isDead()) { 559 assert(MO.isDef()); 560 } 561 } 562 563 // The def undef and def internal flags only make sense for 564 // sub-register defs, and we are substituting a full physreg. An 565 // implicit killed operand from the SuperKills list will represent the 566 // partial read of the super-register. 567 if (MO.isDef()) { 568 MO.setIsUndef(false); 569 MO.setIsInternalRead(false); 570 } 571 572 // PhysReg operands cannot have subregister indexes. 573 PhysReg = TRI->getSubReg(PhysReg, SubReg); 574 assert(PhysReg.isValid() && "Invalid SubReg for physical register"); 575 MO.setSubReg(0); 576 } 577 // Rewrite. Note we could have used MachineOperand::substPhysReg(), but 578 // we need the inlining here. 579 MO.setReg(PhysReg); 580 MO.setIsRenamable(true); 581 } 582 583 // Add any missing super-register kills after rewriting the whole 584 // instruction. 585 while (!SuperKills.empty()) 586 MI->addRegisterKilled(SuperKills.pop_back_val(), TRI, true); 587 588 while (!SuperDeads.empty()) 589 MI->addRegisterDead(SuperDeads.pop_back_val(), TRI, true); 590 591 while (!SuperDefs.empty()) 592 MI->addRegisterDefined(SuperDefs.pop_back_val(), TRI); 593 594 LLVM_DEBUG(dbgs() << "> " << *MI); 595 596 expandCopyBundle(*MI); 597 598 // We can remove identity copies right now. 599 handleIdentityCopy(*MI); 600 } 601 } 602 } 603 604 FunctionPass *llvm::createVirtRegRewriter(bool ClearVirtRegs) { 605 return new VirtRegRewriter(ClearVirtRegs); 606 } 607