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