1 //===- MachineFunction.cpp ------------------------------------------------===// 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 // Collect native machine code information for a function. This allows 10 // target-specific information about the generated code to be stored with each 11 // function. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/MachineFunction.h" 16 #include "llvm/ADT/BitVector.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/Analysis/ConstantFolding.h" 25 #include "llvm/Analysis/EHPersonalities.h" 26 #include "llvm/CodeGen/MachineBasicBlock.h" 27 #include "llvm/CodeGen/MachineConstantPool.h" 28 #include "llvm/CodeGen/MachineFrameInfo.h" 29 #include "llvm/CodeGen/MachineInstr.h" 30 #include "llvm/CodeGen/MachineJumpTableInfo.h" 31 #include "llvm/CodeGen/MachineMemOperand.h" 32 #include "llvm/CodeGen/MachineModuleInfo.h" 33 #include "llvm/CodeGen/MachineRegisterInfo.h" 34 #include "llvm/CodeGen/PseudoSourceValue.h" 35 #include "llvm/CodeGen/TargetFrameLowering.h" 36 #include "llvm/CodeGen/TargetLowering.h" 37 #include "llvm/CodeGen/TargetRegisterInfo.h" 38 #include "llvm/CodeGen/TargetSubtargetInfo.h" 39 #include "llvm/CodeGen/WasmEHFuncInfo.h" 40 #include "llvm/CodeGen/WinEHFuncInfo.h" 41 #include "llvm/Config/llvm-config.h" 42 #include "llvm/IR/Attributes.h" 43 #include "llvm/IR/BasicBlock.h" 44 #include "llvm/IR/Constant.h" 45 #include "llvm/IR/DataLayout.h" 46 #include "llvm/IR/DebugInfoMetadata.h" 47 #include "llvm/IR/DerivedTypes.h" 48 #include "llvm/IR/Function.h" 49 #include "llvm/IR/GlobalValue.h" 50 #include "llvm/IR/Instruction.h" 51 #include "llvm/IR/Instructions.h" 52 #include "llvm/IR/Metadata.h" 53 #include "llvm/IR/Module.h" 54 #include "llvm/IR/ModuleSlotTracker.h" 55 #include "llvm/IR/Value.h" 56 #include "llvm/MC/MCContext.h" 57 #include "llvm/MC/MCSymbol.h" 58 #include "llvm/MC/SectionKind.h" 59 #include "llvm/Support/Casting.h" 60 #include "llvm/Support/CommandLine.h" 61 #include "llvm/Support/Compiler.h" 62 #include "llvm/Support/DOTGraphTraits.h" 63 #include "llvm/Support/Debug.h" 64 #include "llvm/Support/ErrorHandling.h" 65 #include "llvm/Support/GraphWriter.h" 66 #include "llvm/Support/raw_ostream.h" 67 #include "llvm/Target/TargetMachine.h" 68 #include <algorithm> 69 #include <cassert> 70 #include <cstddef> 71 #include <cstdint> 72 #include <iterator> 73 #include <string> 74 #include <utility> 75 #include <vector> 76 77 using namespace llvm; 78 79 #define DEBUG_TYPE "codegen" 80 81 static cl::opt<unsigned> 82 AlignAllFunctions("align-all-functions", 83 cl::desc("Force the alignment of all functions."), 84 cl::init(0), cl::Hidden); 85 86 static const char *getPropertyName(MachineFunctionProperties::Property Prop) { 87 using P = MachineFunctionProperties::Property; 88 89 switch(Prop) { 90 case P::FailedISel: return "FailedISel"; 91 case P::IsSSA: return "IsSSA"; 92 case P::Legalized: return "Legalized"; 93 case P::NoPHIs: return "NoPHIs"; 94 case P::NoVRegs: return "NoVRegs"; 95 case P::RegBankSelected: return "RegBankSelected"; 96 case P::Selected: return "Selected"; 97 case P::TracksLiveness: return "TracksLiveness"; 98 } 99 llvm_unreachable("Invalid machine function property"); 100 } 101 102 // Pin the vtable to this file. 103 void MachineFunction::Delegate::anchor() {} 104 105 void MachineFunctionProperties::print(raw_ostream &OS) const { 106 const char *Separator = ""; 107 for (BitVector::size_type I = 0; I < Properties.size(); ++I) { 108 if (!Properties[I]) 109 continue; 110 OS << Separator << getPropertyName(static_cast<Property>(I)); 111 Separator = ", "; 112 } 113 } 114 115 //===----------------------------------------------------------------------===// 116 // MachineFunction implementation 117 //===----------------------------------------------------------------------===// 118 119 // Out-of-line virtual method. 120 MachineFunctionInfo::~MachineFunctionInfo() = default; 121 122 void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 123 MBB->getParent()->DeleteMachineBasicBlock(MBB); 124 } 125 126 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, 127 const Function &F) { 128 if (F.hasFnAttribute(Attribute::StackAlignment)) 129 return F.getFnStackAlignment(); 130 return STI->getFrameLowering()->getStackAlignment(); 131 } 132 133 MachineFunction::MachineFunction(const Function &F, 134 const LLVMTargetMachine &Target, 135 const TargetSubtargetInfo &STI, 136 unsigned FunctionNum, MachineModuleInfo &mmi) 137 : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { 138 FunctionNumber = FunctionNum; 139 init(); 140 } 141 142 void MachineFunction::handleInsertion(MachineInstr &MI) { 143 if (TheDelegate) 144 TheDelegate->MF_HandleInsertion(MI); 145 } 146 147 void MachineFunction::handleRemoval(MachineInstr &MI) { 148 if (TheDelegate) 149 TheDelegate->MF_HandleRemoval(MI); 150 } 151 152 void MachineFunction::init() { 153 // Assume the function starts in SSA form with correct liveness. 154 Properties.set(MachineFunctionProperties::Property::IsSSA); 155 Properties.set(MachineFunctionProperties::Property::TracksLiveness); 156 if (STI->getRegisterInfo()) 157 RegInfo = new (Allocator) MachineRegisterInfo(this); 158 else 159 RegInfo = nullptr; 160 161 MFInfo = nullptr; 162 // We can realign the stack if the target supports it and the user hasn't 163 // explicitly asked us not to. 164 bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 165 !F.hasFnAttribute("no-realign-stack"); 166 FrameInfo = new (Allocator) MachineFrameInfo( 167 getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 168 /*ForcedRealign=*/CanRealignSP && 169 F.hasFnAttribute(Attribute::StackAlignment)); 170 171 if (F.hasFnAttribute(Attribute::StackAlignment)) 172 FrameInfo->ensureMaxAlignment(F.getFnStackAlignment()); 173 174 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 175 Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 176 177 // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 178 // FIXME: Use Function::hasOptSize(). 179 if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 180 Alignment = std::max(Alignment, 181 STI->getTargetLowering()->getPrefFunctionAlignment()); 182 183 if (AlignAllFunctions) 184 Alignment = AlignAllFunctions; 185 186 JumpTableInfo = nullptr; 187 188 if (isFuncletEHPersonality(classifyEHPersonality( 189 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 190 WinEHInfo = new (Allocator) WinEHFuncInfo(); 191 } 192 193 if (isScopedEHPersonality(classifyEHPersonality( 194 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 195 WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 196 } 197 198 assert(Target.isCompatibleDataLayout(getDataLayout()) && 199 "Can't create a MachineFunction using a Module with a " 200 "Target-incompatible DataLayout attached\n"); 201 202 PSVManager = 203 llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget(). 204 getInstrInfo())); 205 } 206 207 MachineFunction::~MachineFunction() { 208 clear(); 209 } 210 211 void MachineFunction::clear() { 212 Properties.reset(); 213 // Don't call destructors on MachineInstr and MachineOperand. All of their 214 // memory comes from the BumpPtrAllocator which is about to be purged. 215 // 216 // Do call MachineBasicBlock destructors, it contains std::vectors. 217 for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 218 I->Insts.clearAndLeakNodesUnsafely(); 219 MBBNumbering.clear(); 220 221 InstructionRecycler.clear(Allocator); 222 OperandRecycler.clear(Allocator); 223 BasicBlockRecycler.clear(Allocator); 224 CodeViewAnnotations.clear(); 225 VariableDbgInfos.clear(); 226 if (RegInfo) { 227 RegInfo->~MachineRegisterInfo(); 228 Allocator.Deallocate(RegInfo); 229 } 230 if (MFInfo) { 231 MFInfo->~MachineFunctionInfo(); 232 Allocator.Deallocate(MFInfo); 233 } 234 235 FrameInfo->~MachineFrameInfo(); 236 Allocator.Deallocate(FrameInfo); 237 238 ConstantPool->~MachineConstantPool(); 239 Allocator.Deallocate(ConstantPool); 240 241 if (JumpTableInfo) { 242 JumpTableInfo->~MachineJumpTableInfo(); 243 Allocator.Deallocate(JumpTableInfo); 244 } 245 246 if (WinEHInfo) { 247 WinEHInfo->~WinEHFuncInfo(); 248 Allocator.Deallocate(WinEHInfo); 249 } 250 251 if (WasmEHInfo) { 252 WasmEHInfo->~WasmEHFuncInfo(); 253 Allocator.Deallocate(WasmEHInfo); 254 } 255 } 256 257 const DataLayout &MachineFunction::getDataLayout() const { 258 return F.getParent()->getDataLayout(); 259 } 260 261 /// Get the JumpTableInfo for this function. 262 /// If it does not already exist, allocate one. 263 MachineJumpTableInfo *MachineFunction:: 264 getOrCreateJumpTableInfo(unsigned EntryKind) { 265 if (JumpTableInfo) return JumpTableInfo; 266 267 JumpTableInfo = new (Allocator) 268 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 269 return JumpTableInfo; 270 } 271 272 /// Should we be emitting segmented stack stuff for the function 273 bool MachineFunction::shouldSplitStack() const { 274 return getFunction().hasFnAttribute("split-stack"); 275 } 276 277 LLVM_NODISCARD unsigned 278 MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 279 FrameInstructions.push_back(Inst); 280 return FrameInstructions.size() - 1; 281 } 282 283 /// This discards all of the MachineBasicBlock numbers and recomputes them. 284 /// This guarantees that the MBB numbers are sequential, dense, and match the 285 /// ordering of the blocks within the function. If a specific MachineBasicBlock 286 /// is specified, only that block and those after it are renumbered. 287 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 288 if (empty()) { MBBNumbering.clear(); return; } 289 MachineFunction::iterator MBBI, E = end(); 290 if (MBB == nullptr) 291 MBBI = begin(); 292 else 293 MBBI = MBB->getIterator(); 294 295 // Figure out the block number this should have. 296 unsigned BlockNo = 0; 297 if (MBBI != begin()) 298 BlockNo = std::prev(MBBI)->getNumber() + 1; 299 300 for (; MBBI != E; ++MBBI, ++BlockNo) { 301 if (MBBI->getNumber() != (int)BlockNo) { 302 // Remove use of the old number. 303 if (MBBI->getNumber() != -1) { 304 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 305 "MBB number mismatch!"); 306 MBBNumbering[MBBI->getNumber()] = nullptr; 307 } 308 309 // If BlockNo is already taken, set that block's number to -1. 310 if (MBBNumbering[BlockNo]) 311 MBBNumbering[BlockNo]->setNumber(-1); 312 313 MBBNumbering[BlockNo] = &*MBBI; 314 MBBI->setNumber(BlockNo); 315 } 316 } 317 318 // Okay, all the blocks are renumbered. If we have compactified the block 319 // numbering, shrink MBBNumbering now. 320 assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 321 MBBNumbering.resize(BlockNo); 322 } 323 324 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 325 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 326 const DebugLoc &DL, 327 bool NoImp) { 328 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 329 MachineInstr(*this, MCID, DL, NoImp); 330 } 331 332 /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 333 /// identical in all ways except the instruction has no parent, prev, or next. 334 MachineInstr * 335 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 336 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 337 MachineInstr(*this, *Orig); 338 } 339 340 MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, 341 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { 342 MachineInstr *FirstClone = nullptr; 343 MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 344 while (true) { 345 MachineInstr *Cloned = CloneMachineInstr(&*I); 346 MBB.insert(InsertBefore, Cloned); 347 if (FirstClone == nullptr) { 348 FirstClone = Cloned; 349 } else { 350 Cloned->bundleWithPred(); 351 } 352 353 if (!I->isBundledWithSucc()) 354 break; 355 ++I; 356 } 357 return *FirstClone; 358 } 359 360 /// Delete the given MachineInstr. 361 /// 362 /// This function also serves as the MachineInstr destructor - the real 363 /// ~MachineInstr() destructor must be empty. 364 void 365 MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 366 // Verify that a call site info is at valid state. This assertion should 367 // be triggered during the implementation of support for the 368 // call site info of a new architecture. If the assertion is triggered, 369 // back trace will tell where to insert a call to updateCallSiteInfo(). 370 assert((!MI->isCall(MachineInstr::IgnoreBundle) || 371 CallSitesInfo.find(MI) == CallSitesInfo.end()) && 372 "Call site info was not updated!"); 373 // Strip it for parts. The operand array and the MI object itself are 374 // independently recyclable. 375 if (MI->Operands) 376 deallocateOperandArray(MI->CapOperands, MI->Operands); 377 // Don't call ~MachineInstr() which must be trivial anyway because 378 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 379 // destructors. 380 InstructionRecycler.Deallocate(Allocator, MI); 381 } 382 383 /// Allocate a new MachineBasicBlock. Use this instead of 384 /// `new MachineBasicBlock'. 385 MachineBasicBlock * 386 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 387 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 388 MachineBasicBlock(*this, bb); 389 } 390 391 /// Delete the given MachineBasicBlock. 392 void 393 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 394 assert(MBB->getParent() == this && "MBB parent mismatch!"); 395 MBB->~MachineBasicBlock(); 396 BasicBlockRecycler.Deallocate(Allocator, MBB); 397 } 398 399 MachineMemOperand *MachineFunction::getMachineMemOperand( 400 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 401 unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 402 SyncScope::ID SSID, AtomicOrdering Ordering, 403 AtomicOrdering FailureOrdering) { 404 return new (Allocator) 405 MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 406 SSID, Ordering, FailureOrdering); 407 } 408 409 MachineMemOperand * 410 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 411 int64_t Offset, uint64_t Size) { 412 const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 413 414 // If there is no pointer value, the offset isn't tracked so we need to adjust 415 // the base alignment. 416 unsigned Align = PtrInfo.V.isNull() 417 ? MinAlign(MMO->getBaseAlignment(), Offset) 418 : MMO->getBaseAlignment(); 419 420 return new (Allocator) 421 MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size, 422 Align, AAMDNodes(), nullptr, MMO->getSyncScopeID(), 423 MMO->getOrdering(), MMO->getFailureOrdering()); 424 } 425 426 MachineMemOperand * 427 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 428 const AAMDNodes &AAInfo) { 429 MachinePointerInfo MPI = MMO->getValue() ? 430 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 431 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 432 433 return new (Allocator) 434 MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(), 435 MMO->getBaseAlignment(), AAInfo, 436 MMO->getRanges(), MMO->getSyncScopeID(), 437 MMO->getOrdering(), MMO->getFailureOrdering()); 438 } 439 440 MachineMemOperand * 441 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 442 MachineMemOperand::Flags Flags) { 443 return new (Allocator) MachineMemOperand( 444 MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlignment(), 445 MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 446 MMO->getOrdering(), MMO->getFailureOrdering()); 447 } 448 449 MachineInstr::ExtraInfo * 450 MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs, 451 MCSymbol *PreInstrSymbol, 452 MCSymbol *PostInstrSymbol) { 453 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 454 PostInstrSymbol); 455 } 456 457 const char *MachineFunction::createExternalSymbolName(StringRef Name) { 458 char *Dest = Allocator.Allocate<char>(Name.size() + 1); 459 llvm::copy(Name, Dest); 460 Dest[Name.size()] = 0; 461 return Dest; 462 } 463 464 uint32_t *MachineFunction::allocateRegMask() { 465 unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 466 unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 467 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 468 memset(Mask, 0, Size * sizeof(Mask[0])); 469 return Mask; 470 } 471 472 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 473 LLVM_DUMP_METHOD void MachineFunction::dump() const { 474 print(dbgs()); 475 } 476 #endif 477 478 StringRef MachineFunction::getName() const { 479 return getFunction().getName(); 480 } 481 482 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 483 OS << "# Machine code for function " << getName() << ": "; 484 getProperties().print(OS); 485 OS << '\n'; 486 487 // Print Frame Information 488 FrameInfo->print(*this, OS); 489 490 // Print JumpTable Information 491 if (JumpTableInfo) 492 JumpTableInfo->print(OS); 493 494 // Print Constant Pool 495 ConstantPool->print(OS); 496 497 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 498 499 if (RegInfo && !RegInfo->livein_empty()) { 500 OS << "Function Live Ins: "; 501 for (MachineRegisterInfo::livein_iterator 502 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 503 OS << printReg(I->first, TRI); 504 if (I->second) 505 OS << " in " << printReg(I->second, TRI); 506 if (std::next(I) != E) 507 OS << ", "; 508 } 509 OS << '\n'; 510 } 511 512 ModuleSlotTracker MST(getFunction().getParent()); 513 MST.incorporateFunction(getFunction()); 514 for (const auto &BB : *this) { 515 OS << '\n'; 516 // If we print the whole function, print it at its most verbose level. 517 BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 518 } 519 520 OS << "\n# End machine code for function " << getName() << ".\n\n"; 521 } 522 523 namespace llvm { 524 525 template<> 526 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 527 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 528 529 static std::string getGraphName(const MachineFunction *F) { 530 return ("CFG for '" + F->getName() + "' function").str(); 531 } 532 533 std::string getNodeLabel(const MachineBasicBlock *Node, 534 const MachineFunction *Graph) { 535 std::string OutStr; 536 { 537 raw_string_ostream OSS(OutStr); 538 539 if (isSimple()) { 540 OSS << printMBBReference(*Node); 541 if (const BasicBlock *BB = Node->getBasicBlock()) 542 OSS << ": " << BB->getName(); 543 } else 544 Node->print(OSS); 545 } 546 547 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 548 549 // Process string output to make it nicer... 550 for (unsigned i = 0; i != OutStr.length(); ++i) 551 if (OutStr[i] == '\n') { // Left justify 552 OutStr[i] = '\\'; 553 OutStr.insert(OutStr.begin()+i+1, 'l'); 554 } 555 return OutStr; 556 } 557 }; 558 559 } // end namespace llvm 560 561 void MachineFunction::viewCFG() const 562 { 563 #ifndef NDEBUG 564 ViewGraph(this, "mf" + getName()); 565 #else 566 errs() << "MachineFunction::viewCFG is only available in debug builds on " 567 << "systems with Graphviz or gv!\n"; 568 #endif // NDEBUG 569 } 570 571 void MachineFunction::viewCFGOnly() const 572 { 573 #ifndef NDEBUG 574 ViewGraph(this, "mf" + getName(), true); 575 #else 576 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 577 << "systems with Graphviz or gv!\n"; 578 #endif // NDEBUG 579 } 580 581 /// Add the specified physical register as a live-in value and 582 /// create a corresponding virtual register for it. 583 unsigned MachineFunction::addLiveIn(unsigned PReg, 584 const TargetRegisterClass *RC) { 585 MachineRegisterInfo &MRI = getRegInfo(); 586 unsigned VReg = MRI.getLiveInVirtReg(PReg); 587 if (VReg) { 588 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 589 (void)VRegRC; 590 // A physical register can be added several times. 591 // Between two calls, the register class of the related virtual register 592 // may have been constrained to match some operation constraints. 593 // In that case, check that the current register class includes the 594 // physical register and is a sub class of the specified RC. 595 assert((VRegRC == RC || (VRegRC->contains(PReg) && 596 RC->hasSubClassEq(VRegRC))) && 597 "Register class mismatch!"); 598 return VReg; 599 } 600 VReg = MRI.createVirtualRegister(RC); 601 MRI.addLiveIn(PReg, VReg); 602 return VReg; 603 } 604 605 /// Return the MCSymbol for the specified non-empty jump table. 606 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 607 /// normal 'L' label is returned. 608 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 609 bool isLinkerPrivate) const { 610 const DataLayout &DL = getDataLayout(); 611 assert(JumpTableInfo && "No jump tables"); 612 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 613 614 StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 615 : DL.getPrivateGlobalPrefix(); 616 SmallString<60> Name; 617 raw_svector_ostream(Name) 618 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 619 return Ctx.getOrCreateSymbol(Name); 620 } 621 622 /// Return a function-local symbol to represent the PIC base. 623 MCSymbol *MachineFunction::getPICBaseSymbol() const { 624 const DataLayout &DL = getDataLayout(); 625 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 626 Twine(getFunctionNumber()) + "$pb"); 627 } 628 629 /// \name Exception Handling 630 /// \{ 631 632 LandingPadInfo & 633 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 634 unsigned N = LandingPads.size(); 635 for (unsigned i = 0; i < N; ++i) { 636 LandingPadInfo &LP = LandingPads[i]; 637 if (LP.LandingPadBlock == LandingPad) 638 return LP; 639 } 640 641 LandingPads.push_back(LandingPadInfo(LandingPad)); 642 return LandingPads[N]; 643 } 644 645 void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 646 MCSymbol *BeginLabel, MCSymbol *EndLabel) { 647 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 648 LP.BeginLabels.push_back(BeginLabel); 649 LP.EndLabels.push_back(EndLabel); 650 } 651 652 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 653 MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 654 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 655 LP.LandingPadLabel = LandingPadLabel; 656 657 const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 658 if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 659 if (const auto *PF = 660 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 661 getMMI().addPersonality(PF); 662 663 if (LPI->isCleanup()) 664 addCleanup(LandingPad); 665 666 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 667 // correct, but we need to do it this way because of how the DWARF EH 668 // emitter processes the clauses. 669 for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 670 Value *Val = LPI->getClause(I - 1); 671 if (LPI->isCatch(I - 1)) { 672 addCatchTypeInfo(LandingPad, 673 dyn_cast<GlobalValue>(Val->stripPointerCasts())); 674 } else { 675 // Add filters in a list. 676 auto *CVal = cast<Constant>(Val); 677 SmallVector<const GlobalValue *, 4> FilterList; 678 for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); 679 II != IE; ++II) 680 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); 681 682 addFilterTypeInfo(LandingPad, FilterList); 683 } 684 } 685 686 } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 687 for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 688 Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 689 addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 690 } 691 692 } else { 693 assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 694 } 695 696 return LandingPadLabel; 697 } 698 699 void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 700 ArrayRef<const GlobalValue *> TyInfo) { 701 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 702 for (unsigned N = TyInfo.size(); N; --N) 703 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 704 } 705 706 void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 707 ArrayRef<const GlobalValue *> TyInfo) { 708 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 709 std::vector<unsigned> IdsInFilter(TyInfo.size()); 710 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 711 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 712 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 713 } 714 715 void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 716 bool TidyIfNoBeginLabels) { 717 for (unsigned i = 0; i != LandingPads.size(); ) { 718 LandingPadInfo &LandingPad = LandingPads[i]; 719 if (LandingPad.LandingPadLabel && 720 !LandingPad.LandingPadLabel->isDefined() && 721 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 722 LandingPad.LandingPadLabel = nullptr; 723 724 // Special case: we *should* emit LPs with null LP MBB. This indicates 725 // "nounwind" case. 726 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 727 LandingPads.erase(LandingPads.begin() + i); 728 continue; 729 } 730 731 if (TidyIfNoBeginLabels) { 732 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 733 MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 734 MCSymbol *EndLabel = LandingPad.EndLabels[j]; 735 if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 736 (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 737 continue; 738 739 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 740 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 741 --j; 742 --e; 743 } 744 745 // Remove landing pads with no try-ranges. 746 if (LandingPads[i].BeginLabels.empty()) { 747 LandingPads.erase(LandingPads.begin() + i); 748 continue; 749 } 750 } 751 752 // If there is no landing pad, ensure that the list of typeids is empty. 753 // If the only typeid is a cleanup, this is the same as having no typeids. 754 if (!LandingPad.LandingPadBlock || 755 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 756 LandingPad.TypeIds.clear(); 757 ++i; 758 } 759 } 760 761 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 762 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 763 LP.TypeIds.push_back(0); 764 } 765 766 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, 767 const Function *Filter, 768 const BlockAddress *RecoverBA) { 769 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 770 SEHHandler Handler; 771 Handler.FilterOrFinally = Filter; 772 Handler.RecoverBA = RecoverBA; 773 LP.SEHHandlers.push_back(Handler); 774 } 775 776 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, 777 const Function *Cleanup) { 778 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 779 SEHHandler Handler; 780 Handler.FilterOrFinally = Cleanup; 781 Handler.RecoverBA = nullptr; 782 LP.SEHHandlers.push_back(Handler); 783 } 784 785 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 786 ArrayRef<unsigned> Sites) { 787 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 788 } 789 790 unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 791 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 792 if (TypeInfos[i] == TI) return i + 1; 793 794 TypeInfos.push_back(TI); 795 return TypeInfos.size(); 796 } 797 798 int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 799 // If the new filter coincides with the tail of an existing filter, then 800 // re-use the existing filter. Folding filters more than this requires 801 // re-ordering filters and/or their elements - probably not worth it. 802 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 803 E = FilterEnds.end(); I != E; ++I) { 804 unsigned i = *I, j = TyIds.size(); 805 806 while (i && j) 807 if (FilterIds[--i] != TyIds[--j]) 808 goto try_next; 809 810 if (!j) 811 // The new filter coincides with range [i, end) of the existing filter. 812 return -(1 + i); 813 814 try_next:; 815 } 816 817 // Add the new filter. 818 int FilterID = -(1 + FilterIds.size()); 819 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 820 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end()); 821 FilterEnds.push_back(FilterIds.size()); 822 FilterIds.push_back(0); // terminator 823 return FilterID; 824 } 825 826 void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD) { 827 MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true); 828 MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true); 829 I->setPreInstrSymbol(*this, BeginLabel); 830 I->setPostInstrSymbol(*this, EndLabel); 831 832 DIType *DI = dyn_cast<DIType>(MD); 833 CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI)); 834 } 835 836 void MachineFunction::updateCallSiteInfo(const MachineInstr *Old, 837 const MachineInstr *New) { 838 if (!Target.Options.EnableDebugEntryValues || Old == New) 839 return; 840 841 assert(Old->isCall() && (!New || New->isCall()) && 842 "Call site info referes only to call instructions!"); 843 CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old); 844 if (CSIt == CallSitesInfo.end()) 845 return; 846 CallSiteInfo CSInfo = std::move(CSIt->second); 847 CallSitesInfo.erase(CSIt); 848 if (New) 849 CallSitesInfo[New] = CSInfo; 850 } 851 852 /// \} 853 854 //===----------------------------------------------------------------------===// 855 // MachineJumpTableInfo implementation 856 //===----------------------------------------------------------------------===// 857 858 /// Return the size of each entry in the jump table. 859 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 860 // The size of a jump table entry is 4 bytes unless the entry is just the 861 // address of a block, in which case it is the pointer size. 862 switch (getEntryKind()) { 863 case MachineJumpTableInfo::EK_BlockAddress: 864 return TD.getPointerSize(); 865 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 866 return 8; 867 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 868 case MachineJumpTableInfo::EK_LabelDifference32: 869 case MachineJumpTableInfo::EK_Custom32: 870 return 4; 871 case MachineJumpTableInfo::EK_Inline: 872 return 0; 873 } 874 llvm_unreachable("Unknown jump table encoding!"); 875 } 876 877 /// Return the alignment of each entry in the jump table. 878 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 879 // The alignment of a jump table entry is the alignment of int32 unless the 880 // entry is just the address of a block, in which case it is the pointer 881 // alignment. 882 switch (getEntryKind()) { 883 case MachineJumpTableInfo::EK_BlockAddress: 884 return TD.getPointerABIAlignment(0); 885 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 886 return TD.getABIIntegerTypeAlignment(64); 887 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 888 case MachineJumpTableInfo::EK_LabelDifference32: 889 case MachineJumpTableInfo::EK_Custom32: 890 return TD.getABIIntegerTypeAlignment(32); 891 case MachineJumpTableInfo::EK_Inline: 892 return 1; 893 } 894 llvm_unreachable("Unknown jump table encoding!"); 895 } 896 897 /// Create a new jump table entry in the jump table info. 898 unsigned MachineJumpTableInfo::createJumpTableIndex( 899 const std::vector<MachineBasicBlock*> &DestBBs) { 900 assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 901 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 902 return JumpTables.size()-1; 903 } 904 905 /// If Old is the target of any jump tables, update the jump tables to branch 906 /// to New instead. 907 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 908 MachineBasicBlock *New) { 909 assert(Old != New && "Not making a change?"); 910 bool MadeChange = false; 911 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 912 ReplaceMBBInJumpTable(i, Old, New); 913 return MadeChange; 914 } 915 916 /// If Old is a target of the jump tables, update the jump table to branch to 917 /// New instead. 918 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 919 MachineBasicBlock *Old, 920 MachineBasicBlock *New) { 921 assert(Old != New && "Not making a change?"); 922 bool MadeChange = false; 923 MachineJumpTableEntry &JTE = JumpTables[Idx]; 924 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 925 if (JTE.MBBs[j] == Old) { 926 JTE.MBBs[j] = New; 927 MadeChange = true; 928 } 929 return MadeChange; 930 } 931 932 void MachineJumpTableInfo::print(raw_ostream &OS) const { 933 if (JumpTables.empty()) return; 934 935 OS << "Jump Tables:\n"; 936 937 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 938 OS << printJumpTableEntryReference(i) << ':'; 939 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 940 OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); 941 if (i != e) 942 OS << '\n'; 943 } 944 945 OS << '\n'; 946 } 947 948 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 949 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 950 #endif 951 952 Printable llvm::printJumpTableEntryReference(unsigned Idx) { 953 return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 954 } 955 956 //===----------------------------------------------------------------------===// 957 // MachineConstantPool implementation 958 //===----------------------------------------------------------------------===// 959 960 void MachineConstantPoolValue::anchor() {} 961 962 Type *MachineConstantPoolEntry::getType() const { 963 if (isMachineConstantPoolEntry()) 964 return Val.MachineCPVal->getType(); 965 return Val.ConstVal->getType(); 966 } 967 968 bool MachineConstantPoolEntry::needsRelocation() const { 969 if (isMachineConstantPoolEntry()) 970 return true; 971 return Val.ConstVal->needsRelocation(); 972 } 973 974 SectionKind 975 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 976 if (needsRelocation()) 977 return SectionKind::getReadOnlyWithRel(); 978 switch (DL->getTypeAllocSize(getType())) { 979 case 4: 980 return SectionKind::getMergeableConst4(); 981 case 8: 982 return SectionKind::getMergeableConst8(); 983 case 16: 984 return SectionKind::getMergeableConst16(); 985 case 32: 986 return SectionKind::getMergeableConst32(); 987 default: 988 return SectionKind::getReadOnly(); 989 } 990 } 991 992 MachineConstantPool::~MachineConstantPool() { 993 // A constant may be a member of both Constants and MachineCPVsSharingEntries, 994 // so keep track of which we've deleted to avoid double deletions. 995 DenseSet<MachineConstantPoolValue*> Deleted; 996 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 997 if (Constants[i].isMachineConstantPoolEntry()) { 998 Deleted.insert(Constants[i].Val.MachineCPVal); 999 delete Constants[i].Val.MachineCPVal; 1000 } 1001 for (DenseSet<MachineConstantPoolValue*>::iterator I = 1002 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 1003 I != E; ++I) { 1004 if (Deleted.count(*I) == 0) 1005 delete *I; 1006 } 1007 } 1008 1009 /// Test whether the given two constants can be allocated the same constant pool 1010 /// entry. 1011 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 1012 const DataLayout &DL) { 1013 // Handle the trivial case quickly. 1014 if (A == B) return true; 1015 1016 // If they have the same type but weren't the same constant, quickly 1017 // reject them. 1018 if (A->getType() == B->getType()) return false; 1019 1020 // We can't handle structs or arrays. 1021 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 1022 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 1023 return false; 1024 1025 // For now, only support constants with the same size. 1026 uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 1027 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 1028 return false; 1029 1030 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 1031 1032 // Try constant folding a bitcast of both instructions to an integer. If we 1033 // get two identical ConstantInt's, then we are good to share them. We use 1034 // the constant folding APIs to do this so that we get the benefit of 1035 // DataLayout. 1036 if (isa<PointerType>(A->getType())) 1037 A = ConstantFoldCastOperand(Instruction::PtrToInt, 1038 const_cast<Constant *>(A), IntTy, DL); 1039 else if (A->getType() != IntTy) 1040 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 1041 IntTy, DL); 1042 if (isa<PointerType>(B->getType())) 1043 B = ConstantFoldCastOperand(Instruction::PtrToInt, 1044 const_cast<Constant *>(B), IntTy, DL); 1045 else if (B->getType() != IntTy) 1046 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 1047 IntTy, DL); 1048 1049 return A == B; 1050 } 1051 1052 /// Create a new entry in the constant pool or return an existing one. 1053 /// User must specify the log2 of the minimum required alignment for the object. 1054 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 1055 unsigned Alignment) { 1056 assert(Alignment && "Alignment must be specified!"); 1057 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1058 1059 // Check to see if we already have this constant. 1060 // 1061 // FIXME, this could be made much more efficient for large constant pools. 1062 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1063 if (!Constants[i].isMachineConstantPoolEntry() && 1064 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 1065 if ((unsigned)Constants[i].getAlignment() < Alignment) 1066 Constants[i].Alignment = Alignment; 1067 return i; 1068 } 1069 1070 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 1071 return Constants.size()-1; 1072 } 1073 1074 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 1075 unsigned Alignment) { 1076 assert(Alignment && "Alignment must be specified!"); 1077 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1078 1079 // Check to see if we already have this constant. 1080 // 1081 // FIXME, this could be made much more efficient for large constant pools. 1082 int Idx = V->getExistingMachineCPValue(this, Alignment); 1083 if (Idx != -1) { 1084 MachineCPVsSharingEntries.insert(V); 1085 return (unsigned)Idx; 1086 } 1087 1088 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 1089 return Constants.size()-1; 1090 } 1091 1092 void MachineConstantPool::print(raw_ostream &OS) const { 1093 if (Constants.empty()) return; 1094 1095 OS << "Constant Pool:\n"; 1096 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 1097 OS << " cp#" << i << ": "; 1098 if (Constants[i].isMachineConstantPoolEntry()) 1099 Constants[i].Val.MachineCPVal->print(OS); 1100 else 1101 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 1102 OS << ", align=" << Constants[i].getAlignment(); 1103 OS << "\n"; 1104 } 1105 } 1106 1107 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1108 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 1109 #endif 1110