1 //===-- lib/Codegen/MachineRegisterInfo.cpp -------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implementation of the MachineRegisterInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineRegisterInfo.h" 15 #include "llvm/CodeGen/MachineInstrBuilder.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/Support/raw_os_ostream.h" 18 #include "llvm/Target/TargetInstrInfo.h" 19 #include "llvm/Target/TargetMachine.h" 20 #include "llvm/Target/TargetSubtargetInfo.h" 21 22 using namespace llvm; 23 24 // Pin the vtable to this file. 25 void MachineRegisterInfo::Delegate::anchor() {} 26 27 MachineRegisterInfo::MachineRegisterInfo(const MachineFunction *MF) 28 : MF(MF), TheDelegate(nullptr), IsSSA(true), TracksLiveness(true), 29 TracksSubRegLiveness(false) { 30 unsigned NumRegs = getTargetRegisterInfo()->getNumRegs(); 31 VRegInfo.reserve(256); 32 RegAllocHints.reserve(256); 33 UsedPhysRegMask.resize(NumRegs); 34 PhysRegUseDefLists.reset(new MachineOperand*[NumRegs]()); 35 } 36 37 /// setRegClass - Set the register class of the specified virtual register. 38 /// 39 void 40 MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) { 41 assert(RC && RC->isAllocatable() && "Invalid RC for virtual register"); 42 VRegInfo[Reg].first = RC; 43 } 44 45 const TargetRegisterClass * 46 MachineRegisterInfo::constrainRegClass(unsigned Reg, 47 const TargetRegisterClass *RC, 48 unsigned MinNumRegs) { 49 const TargetRegisterClass *OldRC = getRegClass(Reg); 50 if (OldRC == RC) 51 return RC; 52 const TargetRegisterClass *NewRC = 53 getTargetRegisterInfo()->getCommonSubClass(OldRC, RC); 54 if (!NewRC || NewRC == OldRC) 55 return NewRC; 56 if (NewRC->getNumRegs() < MinNumRegs) 57 return nullptr; 58 setRegClass(Reg, NewRC); 59 return NewRC; 60 } 61 62 bool 63 MachineRegisterInfo::recomputeRegClass(unsigned Reg) { 64 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 65 const TargetRegisterClass *OldRC = getRegClass(Reg); 66 const TargetRegisterClass *NewRC = 67 getTargetRegisterInfo()->getLargestLegalSuperClass(OldRC, *MF); 68 69 // Stop early if there is no room to grow. 70 if (NewRC == OldRC) 71 return false; 72 73 // Accumulate constraints from all uses. 74 for (MachineOperand &MO : reg_nodbg_operands(Reg)) { 75 // Apply the effect of the given operand to NewRC. 76 MachineInstr *MI = MO.getParent(); 77 unsigned OpNo = &MO - &MI->getOperand(0); 78 NewRC = MI->getRegClassConstraintEffect(OpNo, NewRC, TII, 79 getTargetRegisterInfo()); 80 if (!NewRC || NewRC == OldRC) 81 return false; 82 } 83 setRegClass(Reg, NewRC); 84 return true; 85 } 86 87 /// createVirtualRegister - Create and return a new virtual register in the 88 /// function with the specified register class. 89 /// 90 unsigned 91 MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){ 92 assert(RegClass && "Cannot create register without RegClass!"); 93 assert(RegClass->isAllocatable() && 94 "Virtual register RegClass must be allocatable."); 95 96 // New virtual register number. 97 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs()); 98 VRegInfo.grow(Reg); 99 VRegInfo[Reg].first = RegClass; 100 RegAllocHints.grow(Reg); 101 if (TheDelegate) 102 TheDelegate->MRI_NoteNewVirtualRegister(Reg); 103 return Reg; 104 } 105 106 #ifdef LLVM_BUILD_GLOBAL_ISEL 107 unsigned 108 MachineRegisterInfo::getSize(unsigned VReg) const { 109 DenseMap<unsigned, unsigned>::const_iterator SizeIt = 110 VRegToSize.find(VReg); 111 return SizeIt != VRegToSize.end()? SizeIt->second: 0; 112 } 113 114 unsigned 115 MachineRegisterInfo::createGenericVirtualRegister(unsigned Size) { 116 assert(Size && "Cannot create empty virtual register"); 117 118 // New virtual register number. 119 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs()); 120 VRegInfo.grow(Reg); 121 // FIXME: Should we use a dummy register class? 122 VRegInfo[Reg].first = nullptr; 123 VRegToSize[Reg] = Size; 124 RegAllocHints.grow(Reg); 125 if (TheDelegate) 126 TheDelegate->MRI_NoteNewVirtualRegister(Reg); 127 return Reg; 128 } 129 #endif // LLVM_BUILD_GLOBAL_ISEL 130 131 /// clearVirtRegs - Remove all virtual registers (after physreg assignment). 132 void MachineRegisterInfo::clearVirtRegs() { 133 #ifndef NDEBUG 134 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) { 135 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 136 if (!VRegInfo[Reg].second) 137 continue; 138 verifyUseList(Reg); 139 llvm_unreachable("Remaining virtual register operands"); 140 } 141 #endif 142 VRegInfo.clear(); 143 for (auto &I : LiveIns) 144 I.second = 0; 145 } 146 147 void MachineRegisterInfo::verifyUseList(unsigned Reg) const { 148 #ifndef NDEBUG 149 bool Valid = true; 150 for (MachineOperand &M : reg_operands(Reg)) { 151 MachineOperand *MO = &M; 152 MachineInstr *MI = MO->getParent(); 153 if (!MI) { 154 errs() << PrintReg(Reg, getTargetRegisterInfo()) 155 << " use list MachineOperand " << MO 156 << " has no parent instruction.\n"; 157 Valid = false; 158 continue; 159 } 160 MachineOperand *MO0 = &MI->getOperand(0); 161 unsigned NumOps = MI->getNumOperands(); 162 if (!(MO >= MO0 && MO < MO0+NumOps)) { 163 errs() << PrintReg(Reg, getTargetRegisterInfo()) 164 << " use list MachineOperand " << MO 165 << " doesn't belong to parent MI: " << *MI; 166 Valid = false; 167 } 168 if (!MO->isReg()) { 169 errs() << PrintReg(Reg, getTargetRegisterInfo()) 170 << " MachineOperand " << MO << ": " << *MO 171 << " is not a register\n"; 172 Valid = false; 173 } 174 if (MO->getReg() != Reg) { 175 errs() << PrintReg(Reg, getTargetRegisterInfo()) 176 << " use-list MachineOperand " << MO << ": " 177 << *MO << " is the wrong register\n"; 178 Valid = false; 179 } 180 } 181 assert(Valid && "Invalid use list"); 182 #endif 183 } 184 185 void MachineRegisterInfo::verifyUseLists() const { 186 #ifndef NDEBUG 187 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) 188 verifyUseList(TargetRegisterInfo::index2VirtReg(i)); 189 for (unsigned i = 1, e = getTargetRegisterInfo()->getNumRegs(); i != e; ++i) 190 verifyUseList(i); 191 #endif 192 } 193 194 /// Add MO to the linked list of operands for its register. 195 void MachineRegisterInfo::addRegOperandToUseList(MachineOperand *MO) { 196 assert(!MO->isOnRegUseList() && "Already on list"); 197 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 198 MachineOperand *const Head = HeadRef; 199 200 // Head points to the first list element. 201 // Next is NULL on the last list element. 202 // Prev pointers are circular, so Head->Prev == Last. 203 204 // Head is NULL for an empty list. 205 if (!Head) { 206 MO->Contents.Reg.Prev = MO; 207 MO->Contents.Reg.Next = nullptr; 208 HeadRef = MO; 209 return; 210 } 211 assert(MO->getReg() == Head->getReg() && "Different regs on the same list!"); 212 213 // Insert MO between Last and Head in the circular Prev chain. 214 MachineOperand *Last = Head->Contents.Reg.Prev; 215 assert(Last && "Inconsistent use list"); 216 assert(MO->getReg() == Last->getReg() && "Different regs on the same list!"); 217 Head->Contents.Reg.Prev = MO; 218 MO->Contents.Reg.Prev = Last; 219 220 // Def operands always precede uses. This allows def_iterator to stop early. 221 // Insert def operands at the front, and use operands at the back. 222 if (MO->isDef()) { 223 // Insert def at the front. 224 MO->Contents.Reg.Next = Head; 225 HeadRef = MO; 226 } else { 227 // Insert use at the end. 228 MO->Contents.Reg.Next = nullptr; 229 Last->Contents.Reg.Next = MO; 230 } 231 } 232 233 /// Remove MO from its use-def list. 234 void MachineRegisterInfo::removeRegOperandFromUseList(MachineOperand *MO) { 235 assert(MO->isOnRegUseList() && "Operand not on use list"); 236 MachineOperand *&HeadRef = getRegUseDefListHead(MO->getReg()); 237 MachineOperand *const Head = HeadRef; 238 assert(Head && "List already empty"); 239 240 // Unlink this from the doubly linked list of operands. 241 MachineOperand *Next = MO->Contents.Reg.Next; 242 MachineOperand *Prev = MO->Contents.Reg.Prev; 243 244 // Prev links are circular, next link is NULL instead of looping back to Head. 245 if (MO == Head) 246 HeadRef = Next; 247 else 248 Prev->Contents.Reg.Next = Next; 249 250 (Next ? Next : Head)->Contents.Reg.Prev = Prev; 251 252 MO->Contents.Reg.Prev = nullptr; 253 MO->Contents.Reg.Next = nullptr; 254 } 255 256 /// Move NumOps operands from Src to Dst, updating use-def lists as needed. 257 /// 258 /// The Dst range is assumed to be uninitialized memory. (Or it may contain 259 /// operands that won't be destroyed, which is OK because the MO destructor is 260 /// trivial anyway). 261 /// 262 /// The Src and Dst ranges may overlap. 263 void MachineRegisterInfo::moveOperands(MachineOperand *Dst, 264 MachineOperand *Src, 265 unsigned NumOps) { 266 assert(Src != Dst && NumOps && "Noop moveOperands"); 267 268 // Copy backwards if Dst is within the Src range. 269 int Stride = 1; 270 if (Dst >= Src && Dst < Src + NumOps) { 271 Stride = -1; 272 Dst += NumOps - 1; 273 Src += NumOps - 1; 274 } 275 276 // Copy one operand at a time. 277 do { 278 new (Dst) MachineOperand(*Src); 279 280 // Dst takes Src's place in the use-def chain. 281 if (Src->isReg()) { 282 MachineOperand *&Head = getRegUseDefListHead(Src->getReg()); 283 MachineOperand *Prev = Src->Contents.Reg.Prev; 284 MachineOperand *Next = Src->Contents.Reg.Next; 285 assert(Head && "List empty, but operand is chained"); 286 assert(Prev && "Operand was not on use-def list"); 287 288 // Prev links are circular, next link is NULL instead of looping back to 289 // Head. 290 if (Src == Head) 291 Head = Dst; 292 else 293 Prev->Contents.Reg.Next = Dst; 294 295 // Update Prev pointer. This also works when Src was pointing to itself 296 // in a 1-element list. In that case Head == Dst. 297 (Next ? Next : Head)->Contents.Reg.Prev = Dst; 298 } 299 300 Dst += Stride; 301 Src += Stride; 302 } while (--NumOps); 303 } 304 305 /// replaceRegWith - Replace all instances of FromReg with ToReg in the 306 /// machine function. This is like llvm-level X->replaceAllUsesWith(Y), 307 /// except that it also changes any definitions of the register as well. 308 /// If ToReg is a physical register we apply the sub register to obtain the 309 /// final/proper physical register. 310 void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) { 311 assert(FromReg != ToReg && "Cannot replace a reg with itself"); 312 313 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 314 315 // TODO: This could be more efficient by bulk changing the operands. 316 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) { 317 MachineOperand &O = *I; 318 ++I; 319 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 320 O.substPhysReg(ToReg, *TRI); 321 } else { 322 O.setReg(ToReg); 323 } 324 } 325 } 326 327 /// getVRegDef - Return the machine instr that defines the specified virtual 328 /// register or null if none is found. This assumes that the code is in SSA 329 /// form, so there should only be one definition. 330 MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const { 331 // Since we are in SSA form, we can use the first definition. 332 def_instr_iterator I = def_instr_begin(Reg); 333 assert((I.atEnd() || std::next(I) == def_instr_end()) && 334 "getVRegDef assumes a single definition or no definition"); 335 return !I.atEnd() ? &*I : nullptr; 336 } 337 338 /// getUniqueVRegDef - Return the unique machine instr that defines the 339 /// specified virtual register or null if none is found. If there are 340 /// multiple definitions or no definition, return null. 341 MachineInstr *MachineRegisterInfo::getUniqueVRegDef(unsigned Reg) const { 342 if (def_empty(Reg)) return nullptr; 343 def_instr_iterator I = def_instr_begin(Reg); 344 if (std::next(I) != def_instr_end()) 345 return nullptr; 346 return &*I; 347 } 348 349 bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const { 350 use_nodbg_iterator UI = use_nodbg_begin(RegNo); 351 if (UI == use_nodbg_end()) 352 return false; 353 return ++UI == use_nodbg_end(); 354 } 355 356 /// clearKillFlags - Iterate over all the uses of the given register and 357 /// clear the kill flag from the MachineOperand. This function is used by 358 /// optimization passes which extend register lifetimes and need only 359 /// preserve conservative kill flag information. 360 void MachineRegisterInfo::clearKillFlags(unsigned Reg) const { 361 for (MachineOperand &MO : use_operands(Reg)) 362 MO.setIsKill(false); 363 } 364 365 bool MachineRegisterInfo::isLiveIn(unsigned Reg) const { 366 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 367 if (I->first == Reg || I->second == Reg) 368 return true; 369 return false; 370 } 371 372 /// getLiveInPhysReg - If VReg is a live-in virtual register, return the 373 /// corresponding live-in physical register. 374 unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const { 375 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 376 if (I->second == VReg) 377 return I->first; 378 return 0; 379 } 380 381 /// getLiveInVirtReg - If PReg is a live-in physical register, return the 382 /// corresponding live-in physical register. 383 unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const { 384 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) 385 if (I->first == PReg) 386 return I->second; 387 return 0; 388 } 389 390 /// EmitLiveInCopies - Emit copies to initialize livein virtual registers 391 /// into the given entry block. 392 void 393 MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB, 394 const TargetRegisterInfo &TRI, 395 const TargetInstrInfo &TII) { 396 // Emit the copies into the top of the block. 397 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i) 398 if (LiveIns[i].second) { 399 if (use_empty(LiveIns[i].second)) { 400 // The livein has no uses. Drop it. 401 // 402 // It would be preferable to have isel avoid creating live-in 403 // records for unused arguments in the first place, but it's 404 // complicated by the debug info code for arguments. 405 LiveIns.erase(LiveIns.begin() + i); 406 --i; --e; 407 } else { 408 // Emit a copy. 409 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(), 410 TII.get(TargetOpcode::COPY), LiveIns[i].second) 411 .addReg(LiveIns[i].first); 412 413 // Add the register to the entry block live-in set. 414 EntryMBB->addLiveIn(LiveIns[i].first); 415 } 416 } else { 417 // Add the register to the entry block live-in set. 418 EntryMBB->addLiveIn(LiveIns[i].first); 419 } 420 } 421 422 LaneBitmask MachineRegisterInfo::getMaxLaneMaskForVReg(unsigned Reg) const { 423 // Lane masks are only defined for vregs. 424 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 425 const TargetRegisterClass &TRC = *getRegClass(Reg); 426 return TRC.getLaneMask(); 427 } 428 429 #ifndef NDEBUG 430 void MachineRegisterInfo::dumpUses(unsigned Reg) const { 431 for (MachineInstr &I : use_instructions(Reg)) 432 I.dump(); 433 } 434 #endif 435 436 void MachineRegisterInfo::freezeReservedRegs(const MachineFunction &MF) { 437 ReservedRegs = getTargetRegisterInfo()->getReservedRegs(MF); 438 assert(ReservedRegs.size() == getTargetRegisterInfo()->getNumRegs() && 439 "Invalid ReservedRegs vector from target"); 440 } 441 442 bool MachineRegisterInfo::isConstantPhysReg(unsigned PhysReg, 443 const MachineFunction &MF) const { 444 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg)); 445 446 // Check if any overlapping register is modified, or allocatable so it may be 447 // used later. 448 for (MCRegAliasIterator AI(PhysReg, getTargetRegisterInfo(), true); 449 AI.isValid(); ++AI) 450 if (!def_empty(*AI) || isAllocatable(*AI)) 451 return false; 452 return true; 453 } 454 455 /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the 456 /// specified register as undefined which causes the DBG_VALUE to be 457 /// deleted during LiveDebugVariables analysis. 458 void MachineRegisterInfo::markUsesInDebugValueAsUndef(unsigned Reg) const { 459 // Mark any DBG_VALUE that uses Reg as undef (but don't delete it.) 460 MachineRegisterInfo::use_instr_iterator nextI; 461 for (use_instr_iterator I = use_instr_begin(Reg), E = use_instr_end(); 462 I != E; I = nextI) { 463 nextI = std::next(I); // I is invalidated by the setReg 464 MachineInstr *UseMI = &*I; 465 if (UseMI->isDebugValue()) 466 UseMI->getOperand(0).setReg(0U); 467 } 468 } 469 470 static const Function *getCalledFunction(const MachineInstr &MI) { 471 for (const MachineOperand &MO : MI.operands()) { 472 if (!MO.isGlobal()) 473 continue; 474 const Function *Func = dyn_cast<Function>(MO.getGlobal()); 475 if (Func != nullptr) 476 return Func; 477 } 478 return nullptr; 479 } 480 481 static bool isNoReturnDef(const MachineOperand &MO) { 482 // Anything which is not a noreturn function is a real def. 483 const MachineInstr &MI = *MO.getParent(); 484 if (!MI.isCall()) 485 return false; 486 const MachineBasicBlock &MBB = *MI.getParent(); 487 if (!MBB.succ_empty()) 488 return false; 489 const MachineFunction &MF = *MBB.getParent(); 490 // We need to keep correct unwind information even if the function will 491 // not return, since the runtime may need it. 492 if (MF.getFunction()->hasFnAttribute(Attribute::UWTable)) 493 return false; 494 const Function *Called = getCalledFunction(MI); 495 return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) || 496 !Called->hasFnAttribute(Attribute::NoUnwind)); 497 } 498 499 bool MachineRegisterInfo::isPhysRegModified(unsigned PhysReg) const { 500 if (UsedPhysRegMask.test(PhysReg)) 501 return true; 502 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 503 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) { 504 for (const MachineOperand &MO : make_range(def_begin(*AI), def_end())) { 505 if (isNoReturnDef(MO)) 506 continue; 507 return true; 508 } 509 } 510 return false; 511 } 512 513 bool MachineRegisterInfo::isPhysRegUsed(unsigned PhysReg) const { 514 if (UsedPhysRegMask.test(PhysReg)) 515 return true; 516 const TargetRegisterInfo *TRI = getTargetRegisterInfo(); 517 for (MCRegAliasIterator AliasReg(PhysReg, TRI, true); AliasReg.isValid(); 518 ++AliasReg) { 519 if (!reg_nodbg_empty(*AliasReg)) 520 return true; 521 } 522 return false; 523 } 524