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