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