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/CodeGen/MachineConstantPool.h" 21 #include "llvm/CodeGen/MachineFrameInfo.h" 22 #include "llvm/CodeGen/MachineFunctionInitializer.h" 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/CodeGen/MachineInstr.h" 25 #include "llvm/CodeGen/MachineJumpTableInfo.h" 26 #include "llvm/CodeGen/MachineModuleInfo.h" 27 #include "llvm/CodeGen/MachineRegisterInfo.h" 28 #include "llvm/CodeGen/Passes.h" 29 #include "llvm/CodeGen/PseudoSourceValue.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/DebugInfo.h" 32 #include "llvm/IR/Function.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/ModuleSlotTracker.h" 35 #include "llvm/MC/MCAsmInfo.h" 36 #include "llvm/MC/MCContext.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/GraphWriter.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Target/TargetFrameLowering.h" 41 #include "llvm/Target/TargetLowering.h" 42 #include "llvm/Target/TargetMachine.h" 43 #include "llvm/Target/TargetSubtargetInfo.h" 44 using namespace llvm; 45 46 #define DEBUG_TYPE "codegen" 47 48 void MachineFunctionInitializer::anchor() {} 49 50 //===----------------------------------------------------------------------===// 51 // MachineFunction implementation 52 //===----------------------------------------------------------------------===// 53 54 // Out-of-line virtual method. 55 MachineFunctionInfo::~MachineFunctionInfo() {} 56 57 void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 58 MBB->getParent()->DeleteMachineBasicBlock(MBB); 59 } 60 61 MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM, 62 unsigned FunctionNum, MachineModuleInfo &mmi) 63 : Fn(F), Target(TM), STI(TM.getSubtargetImpl(*F)), Ctx(mmi.getContext()), 64 MMI(mmi) { 65 if (STI->getRegisterInfo()) 66 RegInfo = new (Allocator) MachineRegisterInfo(this); 67 else 68 RegInfo = nullptr; 69 70 MFInfo = nullptr; 71 FrameInfo = new (Allocator) 72 MachineFrameInfo(STI->getFrameLowering()->getStackAlignment(), 73 STI->getFrameLowering()->isStackRealignable(), 74 !F->hasFnAttribute("no-realign-stack")); 75 76 if (Fn->hasFnAttribute(Attribute::StackAlignment)) 77 FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment()); 78 79 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 80 Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 81 82 // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn. 83 // FIXME: Use Function::optForSize(). 84 if (!Fn->hasFnAttribute(Attribute::OptimizeForSize)) 85 Alignment = std::max(Alignment, 86 STI->getTargetLowering()->getPrefFunctionAlignment()); 87 88 FunctionNumber = FunctionNum; 89 JumpTableInfo = nullptr; 90 91 assert(TM.isCompatibleDataLayout(getDataLayout()) && 92 "Can't create a MachineFunction using a Module with a " 93 "Target-incompatible DataLayout attached\n"); 94 95 PSVManager = llvm::make_unique<PseudoSourceValueManager>(); 96 } 97 98 MachineFunction::~MachineFunction() { 99 // Don't call destructors on MachineInstr and MachineOperand. All of their 100 // memory comes from the BumpPtrAllocator which is about to be purged. 101 // 102 // Do call MachineBasicBlock destructors, it contains std::vectors. 103 for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 104 I->Insts.clearAndLeakNodesUnsafely(); 105 106 InstructionRecycler.clear(Allocator); 107 OperandRecycler.clear(Allocator); 108 BasicBlockRecycler.clear(Allocator); 109 if (RegInfo) { 110 RegInfo->~MachineRegisterInfo(); 111 Allocator.Deallocate(RegInfo); 112 } 113 if (MFInfo) { 114 MFInfo->~MachineFunctionInfo(); 115 Allocator.Deallocate(MFInfo); 116 } 117 118 FrameInfo->~MachineFrameInfo(); 119 Allocator.Deallocate(FrameInfo); 120 121 ConstantPool->~MachineConstantPool(); 122 Allocator.Deallocate(ConstantPool); 123 124 if (JumpTableInfo) { 125 JumpTableInfo->~MachineJumpTableInfo(); 126 Allocator.Deallocate(JumpTableInfo); 127 } 128 } 129 130 const DataLayout &MachineFunction::getDataLayout() const { 131 return Fn->getParent()->getDataLayout(); 132 } 133 134 /// Get the JumpTableInfo for this function. 135 /// If it does not already exist, allocate one. 136 MachineJumpTableInfo *MachineFunction:: 137 getOrCreateJumpTableInfo(unsigned EntryKind) { 138 if (JumpTableInfo) return JumpTableInfo; 139 140 JumpTableInfo = new (Allocator) 141 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 142 return JumpTableInfo; 143 } 144 145 /// Should we be emitting segmented stack stuff for the function 146 bool MachineFunction::shouldSplitStack() { 147 return getFunction()->hasFnAttribute("split-stack"); 148 } 149 150 /// This discards all of the MachineBasicBlock numbers and recomputes them. 151 /// This guarantees that the MBB numbers are sequential, dense, and match the 152 /// ordering of the blocks within the function. If a specific MachineBasicBlock 153 /// is specified, only that block and those after it are renumbered. 154 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 155 if (empty()) { MBBNumbering.clear(); return; } 156 MachineFunction::iterator MBBI, E = end(); 157 if (MBB == nullptr) 158 MBBI = begin(); 159 else 160 MBBI = MBB->getIterator(); 161 162 // Figure out the block number this should have. 163 unsigned BlockNo = 0; 164 if (MBBI != begin()) 165 BlockNo = std::prev(MBBI)->getNumber() + 1; 166 167 for (; MBBI != E; ++MBBI, ++BlockNo) { 168 if (MBBI->getNumber() != (int)BlockNo) { 169 // Remove use of the old number. 170 if (MBBI->getNumber() != -1) { 171 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 172 "MBB number mismatch!"); 173 MBBNumbering[MBBI->getNumber()] = nullptr; 174 } 175 176 // If BlockNo is already taken, set that block's number to -1. 177 if (MBBNumbering[BlockNo]) 178 MBBNumbering[BlockNo]->setNumber(-1); 179 180 MBBNumbering[BlockNo] = &*MBBI; 181 MBBI->setNumber(BlockNo); 182 } 183 } 184 185 // Okay, all the blocks are renumbered. If we have compactified the block 186 // numbering, shrink MBBNumbering now. 187 assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 188 MBBNumbering.resize(BlockNo); 189 } 190 191 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 192 MachineInstr * 193 MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 194 DebugLoc DL, bool NoImp) { 195 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 196 MachineInstr(*this, MCID, DL, NoImp); 197 } 198 199 /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 200 /// identical in all ways except the instruction has no parent, prev, or next. 201 MachineInstr * 202 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 203 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 204 MachineInstr(*this, *Orig); 205 } 206 207 /// Delete the given MachineInstr. 208 /// 209 /// This function also serves as the MachineInstr destructor - the real 210 /// ~MachineInstr() destructor must be empty. 211 void 212 MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 213 // Strip it for parts. The operand array and the MI object itself are 214 // independently recyclable. 215 if (MI->Operands) 216 deallocateOperandArray(MI->CapOperands, MI->Operands); 217 // Don't call ~MachineInstr() which must be trivial anyway because 218 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 219 // destructors. 220 InstructionRecycler.Deallocate(Allocator, MI); 221 } 222 223 /// Allocate a new MachineBasicBlock. Use this instead of 224 /// `new MachineBasicBlock'. 225 MachineBasicBlock * 226 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 227 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 228 MachineBasicBlock(*this, bb); 229 } 230 231 /// Delete the given MachineBasicBlock. 232 void 233 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 234 assert(MBB->getParent() == this && "MBB parent mismatch!"); 235 MBB->~MachineBasicBlock(); 236 BasicBlockRecycler.Deallocate(Allocator, MBB); 237 } 238 239 MachineMemOperand * 240 MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f, 241 uint64_t s, unsigned base_alignment, 242 const AAMDNodes &AAInfo, 243 const MDNode *Ranges) { 244 return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment, 245 AAInfo, Ranges); 246 } 247 248 MachineMemOperand * 249 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 250 int64_t Offset, uint64_t Size) { 251 if (MMO->getValue()) 252 return new (Allocator) 253 MachineMemOperand(MachinePointerInfo(MMO->getValue(), 254 MMO->getOffset()+Offset), 255 MMO->getFlags(), Size, 256 MMO->getBaseAlignment()); 257 return new (Allocator) 258 MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(), 259 MMO->getOffset()+Offset), 260 MMO->getFlags(), Size, 261 MMO->getBaseAlignment()); 262 } 263 264 MachineInstr::mmo_iterator 265 MachineFunction::allocateMemRefsArray(unsigned long Num) { 266 return Allocator.Allocate<MachineMemOperand *>(Num); 267 } 268 269 std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 270 MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 271 MachineInstr::mmo_iterator End) { 272 // Count the number of load mem refs. 273 unsigned Num = 0; 274 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 275 if ((*I)->isLoad()) 276 ++Num; 277 278 // Allocate a new array and populate it with the load information. 279 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 280 unsigned Index = 0; 281 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 282 if ((*I)->isLoad()) { 283 if (!(*I)->isStore()) 284 // Reuse the MMO. 285 Result[Index] = *I; 286 else { 287 // Clone the MMO and unset the store flag. 288 MachineMemOperand *JustLoad = 289 getMachineMemOperand((*I)->getPointerInfo(), 290 (*I)->getFlags() & ~MachineMemOperand::MOStore, 291 (*I)->getSize(), (*I)->getBaseAlignment(), 292 (*I)->getAAInfo()); 293 Result[Index] = JustLoad; 294 } 295 ++Index; 296 } 297 } 298 return std::make_pair(Result, Result + Num); 299 } 300 301 std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 302 MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 303 MachineInstr::mmo_iterator End) { 304 // Count the number of load mem refs. 305 unsigned Num = 0; 306 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 307 if ((*I)->isStore()) 308 ++Num; 309 310 // Allocate a new array and populate it with the store information. 311 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 312 unsigned Index = 0; 313 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 314 if ((*I)->isStore()) { 315 if (!(*I)->isLoad()) 316 // Reuse the MMO. 317 Result[Index] = *I; 318 else { 319 // Clone the MMO and unset the load flag. 320 MachineMemOperand *JustStore = 321 getMachineMemOperand((*I)->getPointerInfo(), 322 (*I)->getFlags() & ~MachineMemOperand::MOLoad, 323 (*I)->getSize(), (*I)->getBaseAlignment(), 324 (*I)->getAAInfo()); 325 Result[Index] = JustStore; 326 } 327 ++Index; 328 } 329 } 330 return std::make_pair(Result, Result + Num); 331 } 332 333 const char *MachineFunction::createExternalSymbolName(StringRef Name) { 334 char *Dest = Allocator.Allocate<char>(Name.size() + 1); 335 std::copy(Name.begin(), Name.end(), Dest); 336 Dest[Name.size()] = 0; 337 return Dest; 338 } 339 340 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 341 void MachineFunction::dump() const { 342 print(dbgs()); 343 } 344 #endif 345 346 StringRef MachineFunction::getName() const { 347 assert(getFunction() && "No function!"); 348 return getFunction()->getName(); 349 } 350 351 void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const { 352 OS << "# Machine code for function " << getName() << ": "; 353 if (RegInfo) { 354 OS << (RegInfo->isSSA() ? "SSA" : "Post SSA"); 355 if (!RegInfo->tracksLiveness()) 356 OS << ", not tracking liveness"; 357 } 358 OS << '\n'; 359 360 // Print Frame Information 361 FrameInfo->print(*this, OS); 362 363 // Print JumpTable Information 364 if (JumpTableInfo) 365 JumpTableInfo->print(OS); 366 367 // Print Constant Pool 368 ConstantPool->print(OS); 369 370 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 371 372 if (RegInfo && !RegInfo->livein_empty()) { 373 OS << "Function Live Ins: "; 374 for (MachineRegisterInfo::livein_iterator 375 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 376 OS << PrintReg(I->first, TRI); 377 if (I->second) 378 OS << " in " << PrintReg(I->second, TRI); 379 if (std::next(I) != E) 380 OS << ", "; 381 } 382 OS << '\n'; 383 } 384 385 ModuleSlotTracker MST(getFunction()->getParent()); 386 MST.incorporateFunction(*getFunction()); 387 for (const auto &BB : *this) { 388 OS << '\n'; 389 BB.print(OS, MST, Indexes); 390 } 391 392 OS << "\n# End machine code for function " << getName() << ".\n\n"; 393 } 394 395 namespace llvm { 396 template<> 397 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 398 399 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 400 401 static std::string getGraphName(const MachineFunction *F) { 402 return ("CFG for '" + F->getName() + "' function").str(); 403 } 404 405 std::string getNodeLabel(const MachineBasicBlock *Node, 406 const MachineFunction *Graph) { 407 std::string OutStr; 408 { 409 raw_string_ostream OSS(OutStr); 410 411 if (isSimple()) { 412 OSS << "BB#" << Node->getNumber(); 413 if (const BasicBlock *BB = Node->getBasicBlock()) 414 OSS << ": " << BB->getName(); 415 } else 416 Node->print(OSS); 417 } 418 419 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 420 421 // Process string output to make it nicer... 422 for (unsigned i = 0; i != OutStr.length(); ++i) 423 if (OutStr[i] == '\n') { // Left justify 424 OutStr[i] = '\\'; 425 OutStr.insert(OutStr.begin()+i+1, 'l'); 426 } 427 return OutStr; 428 } 429 }; 430 } 431 432 void MachineFunction::viewCFG() const 433 { 434 #ifndef NDEBUG 435 ViewGraph(this, "mf" + getName()); 436 #else 437 errs() << "MachineFunction::viewCFG is only available in debug builds on " 438 << "systems with Graphviz or gv!\n"; 439 #endif // NDEBUG 440 } 441 442 void MachineFunction::viewCFGOnly() const 443 { 444 #ifndef NDEBUG 445 ViewGraph(this, "mf" + getName(), true); 446 #else 447 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 448 << "systems with Graphviz or gv!\n"; 449 #endif // NDEBUG 450 } 451 452 /// Add the specified physical register as a live-in value and 453 /// create a corresponding virtual register for it. 454 unsigned MachineFunction::addLiveIn(unsigned PReg, 455 const TargetRegisterClass *RC) { 456 MachineRegisterInfo &MRI = getRegInfo(); 457 unsigned VReg = MRI.getLiveInVirtReg(PReg); 458 if (VReg) { 459 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 460 (void)VRegRC; 461 // A physical register can be added several times. 462 // Between two calls, the register class of the related virtual register 463 // may have been constrained to match some operation constraints. 464 // In that case, check that the current register class includes the 465 // physical register and is a sub class of the specified RC. 466 assert((VRegRC == RC || (VRegRC->contains(PReg) && 467 RC->hasSubClassEq(VRegRC))) && 468 "Register class mismatch!"); 469 return VReg; 470 } 471 VReg = MRI.createVirtualRegister(RC); 472 MRI.addLiveIn(PReg, VReg); 473 return VReg; 474 } 475 476 /// Return the MCSymbol for the specified non-empty jump table. 477 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 478 /// normal 'L' label is returned. 479 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 480 bool isLinkerPrivate) const { 481 const DataLayout &DL = getDataLayout(); 482 assert(JumpTableInfo && "No jump tables"); 483 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 484 485 const char *Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 486 : DL.getPrivateGlobalPrefix(); 487 SmallString<60> Name; 488 raw_svector_ostream(Name) 489 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 490 return Ctx.getOrCreateSymbol(Name); 491 } 492 493 /// Return a function-local symbol to represent the PIC base. 494 MCSymbol *MachineFunction::getPICBaseSymbol() const { 495 const DataLayout &DL = getDataLayout(); 496 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 497 Twine(getFunctionNumber()) + "$pb"); 498 } 499 500 //===----------------------------------------------------------------------===// 501 // MachineFrameInfo implementation 502 //===----------------------------------------------------------------------===// 503 504 /// Make sure the function is at least Align bytes aligned. 505 void MachineFrameInfo::ensureMaxAlignment(unsigned Align) { 506 if (!StackRealignable || !RealignOption) 507 assert(Align <= StackAlignment && 508 "For targets without stack realignment, Align is out of limit!"); 509 if (MaxAlignment < Align) MaxAlignment = Align; 510 } 511 512 /// Clamp the alignment if requested and emit a warning. 513 static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align, 514 unsigned StackAlign) { 515 if (!ShouldClamp || Align <= StackAlign) 516 return Align; 517 DEBUG(dbgs() << "Warning: requested alignment " << Align 518 << " exceeds the stack alignment " << StackAlign 519 << " when stack realignment is off" << '\n'); 520 return StackAlign; 521 } 522 523 /// Create a new statically sized stack object, returning a nonnegative 524 /// identifier to represent it. 525 int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment, 526 bool isSS, const AllocaInst *Alloca) { 527 assert(Size != 0 && "Cannot allocate zero size stack objects!"); 528 Alignment = clampStackAlignment(!StackRealignable || !RealignOption, 529 Alignment, StackAlignment); 530 Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca, 531 !isSS)); 532 int Index = (int)Objects.size() - NumFixedObjects - 1; 533 assert(Index >= 0 && "Bad frame index!"); 534 ensureMaxAlignment(Alignment); 535 return Index; 536 } 537 538 /// Create a new statically sized stack object that represents a spill slot, 539 /// returning a nonnegative identifier to represent it. 540 int MachineFrameInfo::CreateSpillStackObject(uint64_t Size, 541 unsigned Alignment) { 542 Alignment = clampStackAlignment(!StackRealignable || !RealignOption, 543 Alignment, StackAlignment); 544 CreateStackObject(Size, Alignment, true); 545 int Index = (int)Objects.size() - NumFixedObjects - 1; 546 ensureMaxAlignment(Alignment); 547 return Index; 548 } 549 550 /// Notify the MachineFrameInfo object that a variable sized object has been 551 /// created. This must be created whenever a variable sized object is created, 552 /// whether or not the index returned is actually used. 553 int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment, 554 const AllocaInst *Alloca) { 555 HasVarSizedObjects = true; 556 Alignment = clampStackAlignment(!StackRealignable || !RealignOption, 557 Alignment, StackAlignment); 558 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true)); 559 ensureMaxAlignment(Alignment); 560 return (int)Objects.size()-NumFixedObjects-1; 561 } 562 563 /// Create a new object at a fixed location on the stack. 564 /// All fixed objects should be created before other objects are created for 565 /// efficiency. By default, fixed objects are immutable. This returns an 566 /// index with a negative value. 567 int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, 568 bool Immutable, bool isAliased) { 569 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); 570 // The alignment of the frame index can be determined from its offset from 571 // the incoming frame position. If the frame object is at offset 32 and 572 // the stack is guaranteed to be 16-byte aligned, then we know that the 573 // object is 16-byte aligned. 574 unsigned Align = MinAlign(SPOffset, StackAlignment); 575 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align, 576 StackAlignment); 577 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable, 578 /*isSS*/ false, 579 /*Alloca*/ nullptr, isAliased)); 580 return -++NumFixedObjects; 581 } 582 583 /// Create a spill slot at a fixed location on the stack. 584 /// Returns an index with a negative value. 585 int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size, 586 int64_t SPOffset) { 587 unsigned Align = MinAlign(SPOffset, StackAlignment); 588 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align, 589 StackAlignment); 590 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, 591 /*Immutable*/ true, 592 /*isSS*/ true, 593 /*Alloca*/ nullptr, 594 /*isAliased*/ false)); 595 return -++NumFixedObjects; 596 } 597 598 BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const { 599 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 600 BitVector BV(TRI->getNumRegs()); 601 602 // Before CSI is calculated, no registers are considered pristine. They can be 603 // freely used and PEI will make sure they are saved. 604 if (!isCalleeSavedInfoValid()) 605 return BV; 606 607 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR) 608 BV.set(*CSR); 609 610 // Saved CSRs are not pristine. 611 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo(); 612 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 613 E = CSI.end(); I != E; ++I) 614 BV.reset(I->getReg()); 615 616 return BV; 617 } 618 619 unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const { 620 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); 621 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 622 unsigned MaxAlign = getMaxAlignment(); 623 int Offset = 0; 624 625 // This code is very, very similar to PEI::calculateFrameObjectOffsets(). 626 // It really should be refactored to share code. Until then, changes 627 // should keep in mind that there's tight coupling between the two. 628 629 for (int i = getObjectIndexBegin(); i != 0; ++i) { 630 int FixedOff = -getObjectOffset(i); 631 if (FixedOff > Offset) Offset = FixedOff; 632 } 633 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) { 634 if (isDeadObjectIndex(i)) 635 continue; 636 Offset += getObjectSize(i); 637 unsigned Align = getObjectAlignment(i); 638 // Adjust to alignment boundary 639 Offset = (Offset+Align-1)/Align*Align; 640 641 MaxAlign = std::max(Align, MaxAlign); 642 } 643 644 if (adjustsStack() && TFI->hasReservedCallFrame(MF)) 645 Offset += getMaxCallFrameSize(); 646 647 // Round up the size to a multiple of the alignment. If the function has 648 // any calls or alloca's, align to the target's StackAlignment value to 649 // ensure that the callee's frame or the alloca data is suitably aligned; 650 // otherwise, for leaf functions, align to the TransientStackAlignment 651 // value. 652 unsigned StackAlign; 653 if (adjustsStack() || hasVarSizedObjects() || 654 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0)) 655 StackAlign = TFI->getStackAlignment(); 656 else 657 StackAlign = TFI->getTransientStackAlignment(); 658 659 // If the frame pointer is eliminated, all frame offsets will be relative to 660 // SP not FP. Align to MaxAlign so this works. 661 StackAlign = std::max(StackAlign, MaxAlign); 662 unsigned AlignMask = StackAlign - 1; 663 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); 664 665 return (unsigned)Offset; 666 } 667 668 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ 669 if (Objects.empty()) return; 670 671 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering(); 672 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); 673 674 OS << "Frame Objects:\n"; 675 676 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 677 const StackObject &SO = Objects[i]; 678 OS << " fi#" << (int)(i-NumFixedObjects) << ": "; 679 if (SO.Size == ~0ULL) { 680 OS << "dead\n"; 681 continue; 682 } 683 if (SO.Size == 0) 684 OS << "variable sized"; 685 else 686 OS << "size=" << SO.Size; 687 OS << ", align=" << SO.Alignment; 688 689 if (i < NumFixedObjects) 690 OS << ", fixed"; 691 if (i < NumFixedObjects || SO.SPOffset != -1) { 692 int64_t Off = SO.SPOffset - ValOffset; 693 OS << ", at location [SP"; 694 if (Off > 0) 695 OS << "+" << Off; 696 else if (Off < 0) 697 OS << Off; 698 OS << "]"; 699 } 700 OS << "\n"; 701 } 702 } 703 704 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 705 void MachineFrameInfo::dump(const MachineFunction &MF) const { 706 print(MF, dbgs()); 707 } 708 #endif 709 710 //===----------------------------------------------------------------------===// 711 // MachineJumpTableInfo implementation 712 //===----------------------------------------------------------------------===// 713 714 /// Return the size of each entry in the jump table. 715 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 716 // The size of a jump table entry is 4 bytes unless the entry is just the 717 // address of a block, in which case it is the pointer size. 718 switch (getEntryKind()) { 719 case MachineJumpTableInfo::EK_BlockAddress: 720 return TD.getPointerSize(); 721 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 722 return 8; 723 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 724 case MachineJumpTableInfo::EK_LabelDifference32: 725 case MachineJumpTableInfo::EK_Custom32: 726 return 4; 727 case MachineJumpTableInfo::EK_Inline: 728 return 0; 729 } 730 llvm_unreachable("Unknown jump table encoding!"); 731 } 732 733 /// Return the alignment of each entry in the jump table. 734 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 735 // The alignment of a jump table entry is the alignment of int32 unless the 736 // entry is just the address of a block, in which case it is the pointer 737 // alignment. 738 switch (getEntryKind()) { 739 case MachineJumpTableInfo::EK_BlockAddress: 740 return TD.getPointerABIAlignment(); 741 case MachineJumpTableInfo::EK_GPRel64BlockAddress: 742 return TD.getABIIntegerTypeAlignment(64); 743 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 744 case MachineJumpTableInfo::EK_LabelDifference32: 745 case MachineJumpTableInfo::EK_Custom32: 746 return TD.getABIIntegerTypeAlignment(32); 747 case MachineJumpTableInfo::EK_Inline: 748 return 1; 749 } 750 llvm_unreachable("Unknown jump table encoding!"); 751 } 752 753 /// Create a new jump table entry in the jump table info. 754 unsigned MachineJumpTableInfo::createJumpTableIndex( 755 const std::vector<MachineBasicBlock*> &DestBBs) { 756 assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 757 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 758 return JumpTables.size()-1; 759 } 760 761 /// If Old is the target of any jump tables, update the jump tables to branch 762 /// to New instead. 763 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 764 MachineBasicBlock *New) { 765 assert(Old != New && "Not making a change?"); 766 bool MadeChange = false; 767 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 768 ReplaceMBBInJumpTable(i, Old, New); 769 return MadeChange; 770 } 771 772 /// If Old is a target of the jump tables, update the jump table to branch to 773 /// New instead. 774 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 775 MachineBasicBlock *Old, 776 MachineBasicBlock *New) { 777 assert(Old != New && "Not making a change?"); 778 bool MadeChange = false; 779 MachineJumpTableEntry &JTE = JumpTables[Idx]; 780 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 781 if (JTE.MBBs[j] == Old) { 782 JTE.MBBs[j] = New; 783 MadeChange = true; 784 } 785 return MadeChange; 786 } 787 788 void MachineJumpTableInfo::print(raw_ostream &OS) const { 789 if (JumpTables.empty()) return; 790 791 OS << "Jump Tables:\n"; 792 793 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 794 OS << " jt#" << i << ": "; 795 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 796 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); 797 } 798 799 OS << '\n'; 800 } 801 802 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 803 void MachineJumpTableInfo::dump() const { print(dbgs()); } 804 #endif 805 806 807 //===----------------------------------------------------------------------===// 808 // MachineConstantPool implementation 809 //===----------------------------------------------------------------------===// 810 811 void MachineConstantPoolValue::anchor() { } 812 813 Type *MachineConstantPoolEntry::getType() const { 814 if (isMachineConstantPoolEntry()) 815 return Val.MachineCPVal->getType(); 816 return Val.ConstVal->getType(); 817 } 818 819 bool MachineConstantPoolEntry::needsRelocation() const { 820 if (isMachineConstantPoolEntry()) 821 return true; 822 return Val.ConstVal->needsRelocation(); 823 } 824 825 SectionKind 826 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 827 if (needsRelocation()) 828 return SectionKind::getReadOnlyWithRel(); 829 switch (DL->getTypeAllocSize(getType())) { 830 case 4: 831 return SectionKind::getMergeableConst4(); 832 case 8: 833 return SectionKind::getMergeableConst8(); 834 case 16: 835 return SectionKind::getMergeableConst16(); 836 default: 837 return SectionKind::getReadOnly(); 838 } 839 } 840 841 MachineConstantPool::~MachineConstantPool() { 842 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 843 if (Constants[i].isMachineConstantPoolEntry()) 844 delete Constants[i].Val.MachineCPVal; 845 for (DenseSet<MachineConstantPoolValue*>::iterator I = 846 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 847 I != E; ++I) 848 delete *I; 849 } 850 851 /// Test whether the given two constants can be allocated the same constant pool 852 /// entry. 853 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 854 const DataLayout &DL) { 855 // Handle the trivial case quickly. 856 if (A == B) return true; 857 858 // If they have the same type but weren't the same constant, quickly 859 // reject them. 860 if (A->getType() == B->getType()) return false; 861 862 // We can't handle structs or arrays. 863 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 864 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 865 return false; 866 867 // For now, only support constants with the same size. 868 uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 869 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 870 return false; 871 872 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 873 874 // Try constant folding a bitcast of both instructions to an integer. If we 875 // get two identical ConstantInt's, then we are good to share them. We use 876 // the constant folding APIs to do this so that we get the benefit of 877 // DataLayout. 878 if (isa<PointerType>(A->getType())) 879 A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy, 880 const_cast<Constant *>(A), DL); 881 else if (A->getType() != IntTy) 882 A = ConstantFoldInstOperands(Instruction::BitCast, IntTy, 883 const_cast<Constant *>(A), DL); 884 if (isa<PointerType>(B->getType())) 885 B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy, 886 const_cast<Constant *>(B), DL); 887 else if (B->getType() != IntTy) 888 B = ConstantFoldInstOperands(Instruction::BitCast, IntTy, 889 const_cast<Constant *>(B), DL); 890 891 return A == B; 892 } 893 894 /// Create a new entry in the constant pool or return an existing one. 895 /// User must specify the log2 of the minimum required alignment for the object. 896 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 897 unsigned Alignment) { 898 assert(Alignment && "Alignment must be specified!"); 899 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 900 901 // Check to see if we already have this constant. 902 // 903 // FIXME, this could be made much more efficient for large constant pools. 904 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 905 if (!Constants[i].isMachineConstantPoolEntry() && 906 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 907 if ((unsigned)Constants[i].getAlignment() < Alignment) 908 Constants[i].Alignment = Alignment; 909 return i; 910 } 911 912 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 913 return Constants.size()-1; 914 } 915 916 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 917 unsigned Alignment) { 918 assert(Alignment && "Alignment must be specified!"); 919 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 920 921 // Check to see if we already have this constant. 922 // 923 // FIXME, this could be made much more efficient for large constant pools. 924 int Idx = V->getExistingMachineCPValue(this, Alignment); 925 if (Idx != -1) { 926 MachineCPVsSharingEntries.insert(V); 927 return (unsigned)Idx; 928 } 929 930 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 931 return Constants.size()-1; 932 } 933 934 void MachineConstantPool::print(raw_ostream &OS) const { 935 if (Constants.empty()) return; 936 937 OS << "Constant Pool:\n"; 938 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 939 OS << " cp#" << i << ": "; 940 if (Constants[i].isMachineConstantPoolEntry()) 941 Constants[i].Val.MachineCPVal->print(OS); 942 else 943 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 944 OS << ", align=" << Constants[i].getAlignment(); 945 OS << "\n"; 946 } 947 } 948 949 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 950 void MachineConstantPool::dump() const { print(dbgs()); } 951 #endif 952