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 if (&FPType == &APFloat::IEEEsingle()) { 277 Attribute Attr = F.getFnAttribute("denormal-fp-math-f32"); 278 StringRef Val = Attr.getValueAsString(); 279 if (!Val.empty()) 280 return parseDenormalFPAttribute(Val); 281 282 // If the f32 variant of the attribute isn't specified, try to use the 283 // generic one. 284 } 285 286 // TODO: Should probably avoid the connection to the IR and store directly 287 // in the MachineFunction. 288 Attribute Attr = F.getFnAttribute("denormal-fp-math"); 289 return parseDenormalFPAttribute(Attr.getValueAsString()); 290 } 291 292 /// Should we be emitting segmented stack stuff for the function 293 bool MachineFunction::shouldSplitStack() const { 294 return getFunction().hasFnAttribute("split-stack"); 295 } 296 297 LLVM_NODISCARD unsigned 298 MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 299 FrameInstructions.push_back(Inst); 300 return FrameInstructions.size() - 1; 301 } 302 303 /// This discards all of the MachineBasicBlock numbers and recomputes them. 304 /// This guarantees that the MBB numbers are sequential, dense, and match the 305 /// ordering of the blocks within the function. If a specific MachineBasicBlock 306 /// is specified, only that block and those after it are renumbered. 307 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 308 if (empty()) { MBBNumbering.clear(); return; } 309 MachineFunction::iterator MBBI, E = end(); 310 if (MBB == nullptr) 311 MBBI = begin(); 312 else 313 MBBI = MBB->getIterator(); 314 315 // Figure out the block number this should have. 316 unsigned BlockNo = 0; 317 if (MBBI != begin()) 318 BlockNo = std::prev(MBBI)->getNumber() + 1; 319 320 for (; MBBI != E; ++MBBI, ++BlockNo) { 321 if (MBBI->getNumber() != (int)BlockNo) { 322 // Remove use of the old number. 323 if (MBBI->getNumber() != -1) { 324 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 325 "MBB number mismatch!"); 326 MBBNumbering[MBBI->getNumber()] = nullptr; 327 } 328 329 // If BlockNo is already taken, set that block's number to -1. 330 if (MBBNumbering[BlockNo]) 331 MBBNumbering[BlockNo]->setNumber(-1); 332 333 MBBNumbering[BlockNo] = &*MBBI; 334 MBBI->setNumber(BlockNo); 335 } 336 } 337 338 // Okay, all the blocks are renumbered. If we have compactified the block 339 // numbering, shrink MBBNumbering now. 340 assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 341 MBBNumbering.resize(BlockNo); 342 } 343 344 /// This is used with -fbasic-block-sections or -fbasicblock-labels option. 345 /// A unary encoding of basic block labels is done to keep ".strtab" sizes 346 /// small. 347 void MachineFunction::createBBLabels() { 348 const TargetInstrInfo *TII = getSubtarget().getInstrInfo(); 349 this->BBSectionsSymbolPrefix.resize(getNumBlockIDs(), 'a'); 350 for (auto MBBI = begin(), E = end(); MBBI != E; ++MBBI) { 351 assert( 352 (MBBI->getNumber() >= 0 && MBBI->getNumber() < (int)getNumBlockIDs()) && 353 "BasicBlock number was out of range!"); 354 // 'a' - Normal block. 355 // 'r' - Return block. 356 // 'l' - Landing Pad. 357 // 'L' - Return and landing pad. 358 bool isEHPad = MBBI->isEHPad(); 359 bool isRetBlock = MBBI->isReturnBlock() && !TII->isTailCall(MBBI->back()); 360 char type = 'a'; 361 if (isEHPad && isRetBlock) 362 type = 'L'; 363 else if (isEHPad) 364 type = 'l'; 365 else if (isRetBlock) 366 type = 'r'; 367 BBSectionsSymbolPrefix[MBBI->getNumber()] = type; 368 } 369 } 370 371 /// This method iterates over the basic blocks and assigns their IsBeginSection 372 /// and IsEndSection fields. This must be called after MBB layout is finalized 373 /// and the SectionID's are assigned to MBBs. 374 void MachineFunction::assignBeginEndSections() { 375 front().setIsBeginSection(); 376 auto CurrentSectionID = front().getSectionID(); 377 for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { 378 if (MBBI->getSectionID() == CurrentSectionID) 379 continue; 380 MBBI->setIsBeginSection(); 381 std::prev(MBBI)->setIsEndSection(); 382 CurrentSectionID = MBBI->getSectionID(); 383 } 384 back().setIsEndSection(); 385 } 386 387 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 388 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 389 const DebugLoc &DL, 390 bool NoImp) { 391 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 392 MachineInstr(*this, MCID, DL, NoImp); 393 } 394 395 /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 396 /// identical in all ways except the instruction has no parent, prev, or next. 397 MachineInstr * 398 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 399 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 400 MachineInstr(*this, *Orig); 401 } 402 403 MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, 404 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { 405 MachineInstr *FirstClone = nullptr; 406 MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 407 while (true) { 408 MachineInstr *Cloned = CloneMachineInstr(&*I); 409 MBB.insert(InsertBefore, Cloned); 410 if (FirstClone == nullptr) { 411 FirstClone = Cloned; 412 } else { 413 Cloned->bundleWithPred(); 414 } 415 416 if (!I->isBundledWithSucc()) 417 break; 418 ++I; 419 } 420 // Copy over call site info to the cloned instruction if needed. If Orig is in 421 // a bundle, copyCallSiteInfo takes care of finding the call instruction in 422 // the bundle. 423 if (Orig.shouldUpdateCallSiteInfo()) 424 copyCallSiteInfo(&Orig, FirstClone); 425 return *FirstClone; 426 } 427 428 /// Delete the given MachineInstr. 429 /// 430 /// This function also serves as the MachineInstr destructor - the real 431 /// ~MachineInstr() destructor must be empty. 432 void 433 MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 434 // Verify that a call site info is at valid state. This assertion should 435 // be triggered during the implementation of support for the 436 // call site info of a new architecture. If the assertion is triggered, 437 // back trace will tell where to insert a call to updateCallSiteInfo(). 438 assert((!MI->isCandidateForCallSiteEntry() || 439 CallSitesInfo.find(MI) == CallSitesInfo.end()) && 440 "Call site info was not updated!"); 441 // Strip it for parts. The operand array and the MI object itself are 442 // independently recyclable. 443 if (MI->Operands) 444 deallocateOperandArray(MI->CapOperands, MI->Operands); 445 // Don't call ~MachineInstr() which must be trivial anyway because 446 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 447 // destructors. 448 InstructionRecycler.Deallocate(Allocator, MI); 449 } 450 451 /// Allocate a new MachineBasicBlock. Use this instead of 452 /// `new MachineBasicBlock'. 453 MachineBasicBlock * 454 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 455 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 456 MachineBasicBlock(*this, bb); 457 } 458 459 /// Delete the given MachineBasicBlock. 460 void 461 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 462 assert(MBB->getParent() == this && "MBB parent mismatch!"); 463 MBB->~MachineBasicBlock(); 464 BasicBlockRecycler.Deallocate(Allocator, MBB); 465 } 466 467 MachineMemOperand *MachineFunction::getMachineMemOperand( 468 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 469 Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 470 SyncScope::ID SSID, AtomicOrdering Ordering, 471 AtomicOrdering FailureOrdering) { 472 return new (Allocator) 473 MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 474 SSID, Ordering, FailureOrdering); 475 } 476 477 MachineMemOperand * 478 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 479 int64_t Offset, uint64_t Size) { 480 const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 481 482 // If there is no pointer value, the offset isn't tracked so we need to adjust 483 // the base alignment. 484 Align Alignment = PtrInfo.V.isNull() 485 ? commonAlignment(MMO->getBaseAlign(), Offset) 486 : MMO->getBaseAlign(); 487 488 return new (Allocator) 489 MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size, 490 Alignment, AAMDNodes(), nullptr, MMO->getSyncScopeID(), 491 MMO->getOrdering(), MMO->getFailureOrdering()); 492 } 493 494 MachineMemOperand * 495 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 496 const AAMDNodes &AAInfo) { 497 MachinePointerInfo MPI = MMO->getValue() ? 498 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 499 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 500 501 return new (Allocator) MachineMemOperand( 502 MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, 503 MMO->getRanges(), MMO->getSyncScopeID(), MMO->getOrdering(), 504 MMO->getFailureOrdering()); 505 } 506 507 MachineMemOperand * 508 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 509 MachineMemOperand::Flags Flags) { 510 return new (Allocator) MachineMemOperand( 511 MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), 512 MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 513 MMO->getOrdering(), MMO->getFailureOrdering()); 514 } 515 516 MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( 517 ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 518 MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { 519 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 520 PostInstrSymbol, HeapAllocMarker); 521 } 522 523 const char *MachineFunction::createExternalSymbolName(StringRef Name) { 524 char *Dest = Allocator.Allocate<char>(Name.size() + 1); 525 llvm::copy(Name, Dest); 526 Dest[Name.size()] = 0; 527 return Dest; 528 } 529 530 uint32_t *MachineFunction::allocateRegMask() { 531 unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 532 unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 533 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 534 memset(Mask, 0, Size * sizeof(Mask[0])); 535 return Mask; 536 } 537 538 ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { 539 int* AllocMask = Allocator.Allocate<int>(Mask.size()); 540 copy(Mask, AllocMask); 541 return {AllocMask, Mask.size()}; 542 } 543 544 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 545 LLVM_DUMP_METHOD void MachineFunction::dump() const { 546 print(dbgs()); 547 } 548 #endif 549 550 StringRef MachineFunction::getName() const { 551 return getFunction().getName(); 552 } 553 554 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 555 OS << "# Machine code for function " << getName() << ": "; 556 getProperties().print(OS); 557 OS << '\n'; 558 559 // Print Frame Information 560 FrameInfo->print(*this, OS); 561 562 // Print JumpTable Information 563 if (JumpTableInfo) 564 JumpTableInfo->print(OS); 565 566 // Print Constant Pool 567 ConstantPool->print(OS); 568 569 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 570 571 if (RegInfo && !RegInfo->livein_empty()) { 572 OS << "Function Live Ins: "; 573 for (MachineRegisterInfo::livein_iterator 574 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 575 OS << printReg(I->first, TRI); 576 if (I->second) 577 OS << " in " << printReg(I->second, TRI); 578 if (std::next(I) != E) 579 OS << ", "; 580 } 581 OS << '\n'; 582 } 583 584 ModuleSlotTracker MST(getFunction().getParent()); 585 MST.incorporateFunction(getFunction()); 586 for (const auto &BB : *this) { 587 OS << '\n'; 588 // If we print the whole function, print it at its most verbose level. 589 BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 590 } 591 592 OS << "\n# End machine code for function " << getName() << ".\n\n"; 593 } 594 595 /// True if this function needs frame moves for debug or exceptions. 596 bool MachineFunction::needsFrameMoves() const { 597 return getMMI().hasDebugInfo() || 598 getTarget().Options.ForceDwarfFrameSection || 599 F.needsUnwindTableEntry(); 600 } 601 602 namespace llvm { 603 604 template<> 605 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 606 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 607 608 static std::string getGraphName(const MachineFunction *F) { 609 return ("CFG for '" + F->getName() + "' function").str(); 610 } 611 612 std::string getNodeLabel(const MachineBasicBlock *Node, 613 const MachineFunction *Graph) { 614 std::string OutStr; 615 { 616 raw_string_ostream OSS(OutStr); 617 618 if (isSimple()) { 619 OSS << printMBBReference(*Node); 620 if (const BasicBlock *BB = Node->getBasicBlock()) 621 OSS << ": " << BB->getName(); 622 } else 623 Node->print(OSS); 624 } 625 626 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 627 628 // Process string output to make it nicer... 629 for (unsigned i = 0; i != OutStr.length(); ++i) 630 if (OutStr[i] == '\n') { // Left justify 631 OutStr[i] = '\\'; 632 OutStr.insert(OutStr.begin()+i+1, 'l'); 633 } 634 return OutStr; 635 } 636 }; 637 638 } // end namespace llvm 639 640 void MachineFunction::viewCFG() const 641 { 642 #ifndef NDEBUG 643 ViewGraph(this, "mf" + getName()); 644 #else 645 errs() << "MachineFunction::viewCFG is only available in debug builds on " 646 << "systems with Graphviz or gv!\n"; 647 #endif // NDEBUG 648 } 649 650 void MachineFunction::viewCFGOnly() const 651 { 652 #ifndef NDEBUG 653 ViewGraph(this, "mf" + getName(), true); 654 #else 655 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 656 << "systems with Graphviz or gv!\n"; 657 #endif // NDEBUG 658 } 659 660 /// Add the specified physical register as a live-in value and 661 /// create a corresponding virtual register for it. 662 Register MachineFunction::addLiveIn(MCRegister PReg, 663 const TargetRegisterClass *RC) { 664 MachineRegisterInfo &MRI = getRegInfo(); 665 Register VReg = MRI.getLiveInVirtReg(PReg); 666 if (VReg) { 667 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 668 (void)VRegRC; 669 // A physical register can be added several times. 670 // Between two calls, the register class of the related virtual register 671 // may have been constrained to match some operation constraints. 672 // In that case, check that the current register class includes the 673 // physical register and is a sub class of the specified RC. 674 assert((VRegRC == RC || (VRegRC->contains(PReg) && 675 RC->hasSubClassEq(VRegRC))) && 676 "Register class mismatch!"); 677 return VReg; 678 } 679 VReg = MRI.createVirtualRegister(RC); 680 MRI.addLiveIn(PReg, VReg); 681 return VReg; 682 } 683 684 /// Return the MCSymbol for the specified non-empty jump table. 685 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 686 /// normal 'L' label is returned. 687 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 688 bool isLinkerPrivate) const { 689 const DataLayout &DL = getDataLayout(); 690 assert(JumpTableInfo && "No jump tables"); 691 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 692 693 StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 694 : DL.getPrivateGlobalPrefix(); 695 SmallString<60> Name; 696 raw_svector_ostream(Name) 697 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 698 return Ctx.getOrCreateSymbol(Name); 699 } 700 701 /// Return a function-local symbol to represent the PIC base. 702 MCSymbol *MachineFunction::getPICBaseSymbol() const { 703 const DataLayout &DL = getDataLayout(); 704 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 705 Twine(getFunctionNumber()) + "$pb"); 706 } 707 708 /// \name Exception Handling 709 /// \{ 710 711 LandingPadInfo & 712 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 713 unsigned N = LandingPads.size(); 714 for (unsigned i = 0; i < N; ++i) { 715 LandingPadInfo &LP = LandingPads[i]; 716 if (LP.LandingPadBlock == LandingPad) 717 return LP; 718 } 719 720 LandingPads.push_back(LandingPadInfo(LandingPad)); 721 return LandingPads[N]; 722 } 723 724 void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 725 MCSymbol *BeginLabel, MCSymbol *EndLabel) { 726 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 727 LP.BeginLabels.push_back(BeginLabel); 728 LP.EndLabels.push_back(EndLabel); 729 } 730 731 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 732 MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 733 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 734 LP.LandingPadLabel = LandingPadLabel; 735 736 const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 737 if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 738 if (const auto *PF = 739 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 740 getMMI().addPersonality(PF); 741 742 if (LPI->isCleanup()) 743 addCleanup(LandingPad); 744 745 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 746 // correct, but we need to do it this way because of how the DWARF EH 747 // emitter processes the clauses. 748 for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 749 Value *Val = LPI->getClause(I - 1); 750 if (LPI->isCatch(I - 1)) { 751 addCatchTypeInfo(LandingPad, 752 dyn_cast<GlobalValue>(Val->stripPointerCasts())); 753 } else { 754 // Add filters in a list. 755 auto *CVal = cast<Constant>(Val); 756 SmallVector<const GlobalValue *, 4> FilterList; 757 for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); 758 II != IE; ++II) 759 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); 760 761 addFilterTypeInfo(LandingPad, FilterList); 762 } 763 } 764 765 } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 766 for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 767 Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 768 addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 769 } 770 771 } else { 772 assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 773 } 774 775 return LandingPadLabel; 776 } 777 778 void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 779 ArrayRef<const GlobalValue *> TyInfo) { 780 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 781 for (unsigned N = TyInfo.size(); N; --N) 782 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 783 } 784 785 void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 786 ArrayRef<const GlobalValue *> TyInfo) { 787 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 788 std::vector<unsigned> IdsInFilter(TyInfo.size()); 789 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 790 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 791 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 792 } 793 794 void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 795 bool TidyIfNoBeginLabels) { 796 for (unsigned i = 0; i != LandingPads.size(); ) { 797 LandingPadInfo &LandingPad = LandingPads[i]; 798 if (LandingPad.LandingPadLabel && 799 !LandingPad.LandingPadLabel->isDefined() && 800 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 801 LandingPad.LandingPadLabel = nullptr; 802 803 // Special case: we *should* emit LPs with null LP MBB. This indicates 804 // "nounwind" case. 805 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 806 LandingPads.erase(LandingPads.begin() + i); 807 continue; 808 } 809 810 if (TidyIfNoBeginLabels) { 811 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 812 MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 813 MCSymbol *EndLabel = LandingPad.EndLabels[j]; 814 if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 815 (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 816 continue; 817 818 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 819 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 820 --j; 821 --e; 822 } 823 824 // Remove landing pads with no try-ranges. 825 if (LandingPads[i].BeginLabels.empty()) { 826 LandingPads.erase(LandingPads.begin() + i); 827 continue; 828 } 829 } 830 831 // If there is no landing pad, ensure that the list of typeids is empty. 832 // If the only typeid is a cleanup, this is the same as having no typeids. 833 if (!LandingPad.LandingPadBlock || 834 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 835 LandingPad.TypeIds.clear(); 836 ++i; 837 } 838 } 839 840 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 841 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 842 LP.TypeIds.push_back(0); 843 } 844 845 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, 846 const Function *Filter, 847 const BlockAddress *RecoverBA) { 848 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 849 SEHHandler Handler; 850 Handler.FilterOrFinally = Filter; 851 Handler.RecoverBA = RecoverBA; 852 LP.SEHHandlers.push_back(Handler); 853 } 854 855 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, 856 const Function *Cleanup) { 857 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 858 SEHHandler Handler; 859 Handler.FilterOrFinally = Cleanup; 860 Handler.RecoverBA = nullptr; 861 LP.SEHHandlers.push_back(Handler); 862 } 863 864 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 865 ArrayRef<unsigned> Sites) { 866 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 867 } 868 869 unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 870 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 871 if (TypeInfos[i] == TI) return i + 1; 872 873 TypeInfos.push_back(TI); 874 return TypeInfos.size(); 875 } 876 877 int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 878 // If the new filter coincides with the tail of an existing filter, then 879 // re-use the existing filter. Folding filters more than this requires 880 // re-ordering filters and/or their elements - probably not worth it. 881 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 882 E = FilterEnds.end(); I != E; ++I) { 883 unsigned i = *I, j = TyIds.size(); 884 885 while (i && j) 886 if (FilterIds[--i] != TyIds[--j]) 887 goto try_next; 888 889 if (!j) 890 // The new filter coincides with range [i, end) of the existing filter. 891 return -(1 + i); 892 893 try_next:; 894 } 895 896 // Add the new filter. 897 int FilterID = -(1 + FilterIds.size()); 898 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 899 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end()); 900 FilterEnds.push_back(FilterIds.size()); 901 FilterIds.push_back(0); // terminator 902 return FilterID; 903 } 904 905 MachineFunction::CallSiteInfoMap::iterator 906 MachineFunction::getCallSiteInfo(const MachineInstr *MI) { 907 assert(MI->isCandidateForCallSiteEntry() && 908 "Call site info refers only to call (MI) candidates"); 909 910 if (!Target.Options.EmitCallSiteInfo) 911 return CallSitesInfo.end(); 912 return CallSitesInfo.find(MI); 913 } 914 915 /// Return the call machine instruction or find a call within bundle. 916 static const MachineInstr *getCallInstr(const MachineInstr *MI) { 917 if (!MI->isBundle()) 918 return MI; 919 920 for (auto &BMI : make_range(getBundleStart(MI->getIterator()), 921 getBundleEnd(MI->getIterator()))) 922 if (BMI.isCandidateForCallSiteEntry()) 923 return &BMI; 924 925 llvm_unreachable("Unexpected bundle without a call site candidate"); 926 } 927 928 void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { 929 assert(MI->shouldUpdateCallSiteInfo() && 930 "Call site info refers only to call (MI) candidates or " 931 "candidates inside bundles"); 932 933 const MachineInstr *CallMI = getCallInstr(MI); 934 CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); 935 if (CSIt == CallSitesInfo.end()) 936 return; 937 CallSitesInfo.erase(CSIt); 938 } 939 940 void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, 941 const MachineInstr *New) { 942 assert(Old->shouldUpdateCallSiteInfo() && 943 "Call site info refers only to call (MI) candidates or " 944 "candidates inside bundles"); 945 946 if (!New->isCandidateForCallSiteEntry()) 947 return eraseCallSiteInfo(Old); 948 949 const MachineInstr *OldCallMI = getCallInstr(Old); 950 CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 951 if (CSIt == CallSitesInfo.end()) 952 return; 953 954 CallSiteInfo CSInfo = CSIt->second; 955 CallSitesInfo[New] = CSInfo; 956 } 957 958 void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, 959 const MachineInstr *New) { 960 assert(Old->shouldUpdateCallSiteInfo() && 961 "Call site info refers only to call (MI) candidates or " 962 "candidates inside bundles"); 963 964 if (!New->isCandidateForCallSiteEntry()) 965 return eraseCallSiteInfo(Old); 966 967 const MachineInstr *OldCallMI = getCallInstr(Old); 968 CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 969 if (CSIt == CallSitesInfo.end()) 970 return; 971 972 CallSiteInfo CSInfo = std::move(CSIt->second); 973 CallSitesInfo.erase(CSIt); 974 CallSitesInfo[New] = CSInfo; 975 } 976 977 /// \} 978 979 //===----------------------------------------------------------------------===// 980 // MachineJumpTableInfo implementation 981 //===----------------------------------------------------------------------===// 982 983 /// Return the size of each entry in the jump table. 984 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 985 // The size of a jump table entry is 4 bytes unless the entry is just the 986 // address of a block, in which case it is the pointer size. 987 switch (getEntryKind()) { 988 case MachineJumpTableInfo::EK_BlockAddress: 989 return TD.getPointerSize(); 990 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 991 return 8; 992 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 993 case MachineJumpTableInfo::EK_LabelDifference32: 994 case MachineJumpTableInfo::EK_Custom32: 995 return 4; 996 case MachineJumpTableInfo::EK_Inline: 997 return 0; 998 } 999 llvm_unreachable("Unknown jump table encoding!"); 1000 } 1001 1002 /// Return the alignment of each entry in the jump table. 1003 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 1004 // The alignment of a jump table entry is the alignment of int32 unless the 1005 // entry is just the address of a block, in which case it is the pointer 1006 // alignment. 1007 switch (getEntryKind()) { 1008 case MachineJumpTableInfo::EK_BlockAddress: 1009 return TD.getPointerABIAlignment(0).value(); 1010 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 1011 return TD.getABIIntegerTypeAlignment(64).value(); 1012 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1013 case MachineJumpTableInfo::EK_LabelDifference32: 1014 case MachineJumpTableInfo::EK_Custom32: 1015 return TD.getABIIntegerTypeAlignment(32).value(); 1016 case MachineJumpTableInfo::EK_Inline: 1017 return 1; 1018 } 1019 llvm_unreachable("Unknown jump table encoding!"); 1020 } 1021 1022 /// Create a new jump table entry in the jump table info. 1023 unsigned MachineJumpTableInfo::createJumpTableIndex( 1024 const std::vector<MachineBasicBlock*> &DestBBs) { 1025 assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 1026 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 1027 return JumpTables.size()-1; 1028 } 1029 1030 /// If Old is the target of any jump tables, update the jump tables to branch 1031 /// to New instead. 1032 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 1033 MachineBasicBlock *New) { 1034 assert(Old != New && "Not making a change?"); 1035 bool MadeChange = false; 1036 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 1037 ReplaceMBBInJumpTable(i, Old, New); 1038 return MadeChange; 1039 } 1040 1041 /// If Old is a target of the jump tables, update the jump table to branch to 1042 /// New instead. 1043 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 1044 MachineBasicBlock *Old, 1045 MachineBasicBlock *New) { 1046 assert(Old != New && "Not making a change?"); 1047 bool MadeChange = false; 1048 MachineJumpTableEntry &JTE = JumpTables[Idx]; 1049 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 1050 if (JTE.MBBs[j] == Old) { 1051 JTE.MBBs[j] = New; 1052 MadeChange = true; 1053 } 1054 return MadeChange; 1055 } 1056 1057 void MachineJumpTableInfo::print(raw_ostream &OS) const { 1058 if (JumpTables.empty()) return; 1059 1060 OS << "Jump Tables:\n"; 1061 1062 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 1063 OS << printJumpTableEntryReference(i) << ':'; 1064 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 1065 OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); 1066 if (i != e) 1067 OS << '\n'; 1068 } 1069 1070 OS << '\n'; 1071 } 1072 1073 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1074 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 1075 #endif 1076 1077 Printable llvm::printJumpTableEntryReference(unsigned Idx) { 1078 return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 1079 } 1080 1081 //===----------------------------------------------------------------------===// 1082 // MachineConstantPool implementation 1083 //===----------------------------------------------------------------------===// 1084 1085 void MachineConstantPoolValue::anchor() {} 1086 1087 Type *MachineConstantPoolEntry::getType() const { 1088 if (isMachineConstantPoolEntry()) 1089 return Val.MachineCPVal->getType(); 1090 return Val.ConstVal->getType(); 1091 } 1092 1093 bool MachineConstantPoolEntry::needsRelocation() const { 1094 if (isMachineConstantPoolEntry()) 1095 return true; 1096 return Val.ConstVal->needsRelocation(); 1097 } 1098 1099 SectionKind 1100 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 1101 if (needsRelocation()) 1102 return SectionKind::getReadOnlyWithRel(); 1103 switch (DL->getTypeAllocSize(getType())) { 1104 case 4: 1105 return SectionKind::getMergeableConst4(); 1106 case 8: 1107 return SectionKind::getMergeableConst8(); 1108 case 16: 1109 return SectionKind::getMergeableConst16(); 1110 case 32: 1111 return SectionKind::getMergeableConst32(); 1112 default: 1113 return SectionKind::getReadOnly(); 1114 } 1115 } 1116 1117 MachineConstantPool::~MachineConstantPool() { 1118 // A constant may be a member of both Constants and MachineCPVsSharingEntries, 1119 // so keep track of which we've deleted to avoid double deletions. 1120 DenseSet<MachineConstantPoolValue*> Deleted; 1121 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1122 if (Constants[i].isMachineConstantPoolEntry()) { 1123 Deleted.insert(Constants[i].Val.MachineCPVal); 1124 delete Constants[i].Val.MachineCPVal; 1125 } 1126 for (DenseSet<MachineConstantPoolValue*>::iterator I = 1127 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 1128 I != E; ++I) { 1129 if (Deleted.count(*I) == 0) 1130 delete *I; 1131 } 1132 } 1133 1134 /// Test whether the given two constants can be allocated the same constant pool 1135 /// entry. 1136 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 1137 const DataLayout &DL) { 1138 // Handle the trivial case quickly. 1139 if (A == B) return true; 1140 1141 // If they have the same type but weren't the same constant, quickly 1142 // reject them. 1143 if (A->getType() == B->getType()) return false; 1144 1145 // We can't handle structs or arrays. 1146 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 1147 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 1148 return false; 1149 1150 // For now, only support constants with the same size. 1151 uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 1152 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 1153 return false; 1154 1155 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 1156 1157 // Try constant folding a bitcast of both instructions to an integer. If we 1158 // get two identical ConstantInt's, then we are good to share them. We use 1159 // the constant folding APIs to do this so that we get the benefit of 1160 // DataLayout. 1161 if (isa<PointerType>(A->getType())) 1162 A = ConstantFoldCastOperand(Instruction::PtrToInt, 1163 const_cast<Constant *>(A), IntTy, DL); 1164 else if (A->getType() != IntTy) 1165 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 1166 IntTy, DL); 1167 if (isa<PointerType>(B->getType())) 1168 B = ConstantFoldCastOperand(Instruction::PtrToInt, 1169 const_cast<Constant *>(B), IntTy, DL); 1170 else if (B->getType() != IntTy) 1171 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 1172 IntTy, DL); 1173 1174 return A == B; 1175 } 1176 1177 /// Create a new entry in the constant pool or return an existing one. 1178 /// User must specify the log2 of the minimum required alignment for the object. 1179 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 1180 Align Alignment) { 1181 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1182 1183 // Check to see if we already have this constant. 1184 // 1185 // FIXME, this could be made much more efficient for large constant pools. 1186 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1187 if (!Constants[i].isMachineConstantPoolEntry() && 1188 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 1189 if (Constants[i].getAlign() < Alignment) 1190 Constants[i].Alignment = Alignment; 1191 return i; 1192 } 1193 1194 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 1195 return Constants.size()-1; 1196 } 1197 1198 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 1199 Align Alignment) { 1200 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1201 1202 // Check to see if we already have this constant. 1203 // 1204 // FIXME, this could be made much more efficient for large constant pools. 1205 int Idx = V->getExistingMachineCPValue(this, Alignment); 1206 if (Idx != -1) { 1207 MachineCPVsSharingEntries.insert(V); 1208 return (unsigned)Idx; 1209 } 1210 1211 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 1212 return Constants.size()-1; 1213 } 1214 1215 void MachineConstantPool::print(raw_ostream &OS) const { 1216 if (Constants.empty()) return; 1217 1218 OS << "Constant Pool:\n"; 1219 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 1220 OS << " cp#" << i << ": "; 1221 if (Constants[i].isMachineConstantPoolEntry()) 1222 Constants[i].Val.MachineCPVal->print(OS); 1223 else 1224 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 1225 OS << ", align=" << Constants[i].getAlign().value(); 1226 OS << "\n"; 1227 } 1228 } 1229 1230 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1231 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 1232 #endif 1233