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