1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
14 //===----------------------------------------------------------------------===//
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"
46 #define DEBUG_TYPE "codegen"
48 void MachineFunctionInitializer::anchor() {}
50 //===----------------------------------------------------------------------===//
51 // MachineFunction implementation
52 //===----------------------------------------------------------------------===//
54 // Out-of-line virtual method.
55 MachineFunctionInfo::~MachineFunctionInfo() {}
57 void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
58 MBB->getParent()->DeleteMachineBasicBlock(MBB);
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()),
65 if (STI->getRegisterInfo())
66 RegInfo = new (Allocator) MachineRegisterInfo(this);
71 FrameInfo = new (Allocator)
72 MachineFrameInfo(STI->getFrameLowering()->getStackAlignment(),
73 STI->getFrameLowering()->isStackRealignable(),
74 !F->hasFnAttribute("no-realign-stack"));
76 if (Fn->hasFnAttribute(Attribute::StackAlignment))
77 FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
79 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
80 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
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());
88 FunctionNumber = FunctionNum;
89 JumpTableInfo = nullptr;
91 assert(TM.isCompatibleDataLayout(getDataLayout()) &&
92 "Can't create a MachineFunction using a Module with a "
93 "Target-incompatible DataLayout attached\n");
95 PSVManager = llvm::make_unique<PseudoSourceValueManager>();
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.
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();
106 InstructionRecycler.clear(Allocator);
107 OperandRecycler.clear(Allocator);
108 BasicBlockRecycler.clear(Allocator);
110 RegInfo->~MachineRegisterInfo();
111 Allocator.Deallocate(RegInfo);
114 MFInfo->~MachineFunctionInfo();
115 Allocator.Deallocate(MFInfo);
118 FrameInfo->~MachineFrameInfo();
119 Allocator.Deallocate(FrameInfo);
121 ConstantPool->~MachineConstantPool();
122 Allocator.Deallocate(ConstantPool);
125 JumpTableInfo->~MachineJumpTableInfo();
126 Allocator.Deallocate(JumpTableInfo);
130 const DataLayout &MachineFunction::getDataLayout() const {
131 return Fn->getParent()->getDataLayout();
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;
140 JumpTableInfo = new (Allocator)
141 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
142 return JumpTableInfo;
145 /// Should we be emitting segmented stack stuff for the function
146 bool MachineFunction::shouldSplitStack() {
147 return getFunction()->hasFnAttribute("split-stack");
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();
160 MBBI = MBB->getIterator();
162 // Figure out the block number this should have.
163 unsigned BlockNo = 0;
165 BlockNo = std::prev(MBBI)->getNumber() + 1;
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;
176 // If BlockNo is already taken, set that block's number to -1.
177 if (MBBNumbering[BlockNo])
178 MBBNumbering[BlockNo]->setNumber(-1);
180 MBBNumbering[BlockNo] = &*MBBI;
181 MBBI->setNumber(BlockNo);
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);
191 /// Allocate a new MachineInstr. Use this instead of `new 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);
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.
202 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
203 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
204 MachineInstr(*this, *Orig);
207 /// Delete the given MachineInstr.
209 /// This function also serves as the MachineInstr destructor - the real
210 /// ~MachineInstr() destructor must be empty.
212 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
213 // Strip it for parts. The operand array and the MI object itself are
214 // independently recyclable.
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
220 InstructionRecycler.Deallocate(Allocator, MI);
223 /// Allocate a new MachineBasicBlock. Use this instead of
224 /// `new MachineBasicBlock'.
226 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
227 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
228 MachineBasicBlock(*this, bb);
231 /// Delete the given MachineBasicBlock.
233 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
234 assert(MBB->getParent() == this && "MBB parent mismatch!");
235 MBB->~MachineBasicBlock();
236 BasicBlockRecycler.Deallocate(Allocator, MBB);
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,
249 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
250 int64_t Offset, uint64_t Size) {
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());
264 MachineInstr::mmo_iterator
265 MachineFunction::allocateMemRefsArray(unsigned long Num) {
266 return Allocator.Allocate<MachineMemOperand *>(Num);
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.
274 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
278 // Allocate a new array and populate it with the load information.
279 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
281 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
282 if ((*I)->isLoad()) {
283 if (!(*I)->isStore())
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(),
293 Result[Index] = JustLoad;
298 return std::make_pair(Result, Result + Num);
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.
306 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
310 // Allocate a new array and populate it with the store information.
311 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
313 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
314 if ((*I)->isStore()) {
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(),
325 Result[Index] = JustStore;
330 return std::make_pair(Result, Result + Num);
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;
340 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
341 void MachineFunction::dump() const {
346 StringRef MachineFunction::getName() const {
347 assert(getFunction() && "No function!");
348 return getFunction()->getName();
351 void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
352 OS << "# Machine code for function " << getName() << ": ";
354 OS << (RegInfo->isSSA() ? "SSA" : "Post SSA");
355 if (!RegInfo->tracksLiveness())
356 OS << ", not tracking liveness";
360 // Print Frame Information
361 FrameInfo->print(*this, OS);
363 // Print JumpTable Information
365 JumpTableInfo->print(OS);
367 // Print Constant Pool
368 ConstantPool->print(OS);
370 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
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);
378 OS << " in " << PrintReg(I->second, TRI);
379 if (std::next(I) != E)
385 ModuleSlotTracker MST(getFunction()->getParent());
386 MST.incorporateFunction(*getFunction());
387 for (const auto &BB : *this) {
389 BB.print(OS, MST, Indexes);
392 OS << "\n# End machine code for function " << getName() << ".\n\n";
397 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
399 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
401 static std::string getGraphName(const MachineFunction *F) {
402 return ("CFG for '" + F->getName() + "' function").str();
405 std::string getNodeLabel(const MachineBasicBlock *Node,
406 const MachineFunction *Graph) {
409 raw_string_ostream OSS(OutStr);
412 OSS << "BB#" << Node->getNumber();
413 if (const BasicBlock *BB = Node->getBasicBlock())
414 OSS << ": " << BB->getName();
419 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
421 // Process string output to make it nicer...
422 for (unsigned i = 0; i != OutStr.length(); ++i)
423 if (OutStr[i] == '\n') { // Left justify
425 OutStr.insert(OutStr.begin()+i+1, 'l');
432 void MachineFunction::viewCFG() const
435 ViewGraph(this, "mf" + getName());
437 errs() << "MachineFunction::viewCFG is only available in debug builds on "
438 << "systems with Graphviz or gv!\n";
442 void MachineFunction::viewCFGOnly() const
445 ViewGraph(this, "mf" + getName(), true);
447 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
448 << "systems with Graphviz or gv!\n";
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);
459 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
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!");
471 VReg = MRI.createVirtualRegister(RC);
472 MRI.addLiveIn(PReg, VReg);
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!");
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);
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");
500 //===----------------------------------------------------------------------===//
501 // MachineFrameInfo implementation
502 //===----------------------------------------------------------------------===//
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;
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)
517 DEBUG(dbgs() << "Warning: requested alignment " << Align
518 << " exceeds the stack alignment " << StackAlign
519 << " when stack realignment is off" << '\n');
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,
532 int Index = (int)Objects.size() - NumFixedObjects - 1;
533 assert(Index >= 0 && "Bad frame index!");
534 ensureMaxAlignment(Alignment);
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);
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;
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,
577 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
579 /*Alloca*/ nullptr, isAliased));
580 return -++NumFixedObjects;
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,
587 unsigned Align = MinAlign(SPOffset, StackAlignment);
588 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
590 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset,
594 /*isAliased*/ false));
595 return -++NumFixedObjects;
598 BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
599 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
600 BitVector BV(TRI->getNumRegs());
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())
607 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
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());
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();
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.
629 for (int i = getObjectIndexBegin(); i != 0; ++i) {
630 int FixedOff = -getObjectOffset(i);
631 if (FixedOff > Offset) Offset = FixedOff;
633 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
634 if (isDeadObjectIndex(i))
636 Offset += getObjectSize(i);
637 unsigned Align = getObjectAlignment(i);
638 // Adjust to alignment boundary
639 Offset = (Offset+Align-1)/Align*Align;
641 MaxAlign = std::max(Align, MaxAlign);
644 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
645 Offset += getMaxCallFrameSize();
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
653 if (adjustsStack() || hasVarSizedObjects() ||
654 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
655 StackAlign = TFI->getStackAlignment();
657 StackAlign = TFI->getTransientStackAlignment();
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);
665 return (unsigned)Offset;
668 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
669 if (Objects.empty()) return;
671 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
672 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
674 OS << "Frame Objects:\n";
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) {
684 OS << "variable sized";
686 OS << "size=" << SO.Size;
687 OS << ", align=" << SO.Alignment;
689 if (i < NumFixedObjects)
691 if (i < NumFixedObjects || SO.SPOffset != -1) {
692 int64_t Off = SO.SPOffset - ValOffset;
693 OS << ", at location [SP";
704 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
705 void MachineFrameInfo::dump(const MachineFunction &MF) const {
710 //===----------------------------------------------------------------------===//
711 // MachineJumpTableInfo implementation
712 //===----------------------------------------------------------------------===//
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:
723 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
724 case MachineJumpTableInfo::EK_LabelDifference32:
725 case MachineJumpTableInfo::EK_Custom32:
727 case MachineJumpTableInfo::EK_Inline:
730 llvm_unreachable("Unknown jump table encoding!");
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
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:
750 llvm_unreachable("Unknown jump table encoding!");
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;
761 /// If Old is the target of any jump tables, update the jump tables to branch
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);
772 /// If Old is a target of the jump tables, update the jump table to branch to
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) {
788 void MachineJumpTableInfo::print(raw_ostream &OS) const {
789 if (JumpTables.empty()) return;
791 OS << "Jump Tables:\n";
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();
802 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
803 void MachineJumpTableInfo::dump() const { print(dbgs()); }
807 //===----------------------------------------------------------------------===//
808 // MachineConstantPool implementation
809 //===----------------------------------------------------------------------===//
811 void MachineConstantPoolValue::anchor() { }
813 Type *MachineConstantPoolEntry::getType() const {
814 if (isMachineConstantPoolEntry())
815 return Val.MachineCPVal->getType();
816 return Val.ConstVal->getType();
820 unsigned MachineConstantPoolEntry::getRelocationInfo() const {
821 if (isMachineConstantPoolEntry())
822 return Val.MachineCPVal->getRelocationInfo();
823 return Val.ConstVal->getRelocationInfo();
827 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
829 switch (getRelocationInfo()) {
831 llvm_unreachable("Unknown section kind");
832 case Constant::GlobalRelocations:
833 Kind = SectionKind::getReadOnlyWithRel();
835 case Constant::LocalRelocation:
836 Kind = SectionKind::getReadOnlyWithRelLocal();
838 case Constant::NoRelocation:
839 switch (DL->getTypeAllocSize(getType())) {
841 Kind = SectionKind::getMergeableConst4();
844 Kind = SectionKind::getMergeableConst8();
847 Kind = SectionKind::getMergeableConst16();
850 Kind = SectionKind::getReadOnly();
857 MachineConstantPool::~MachineConstantPool() {
858 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
859 if (Constants[i].isMachineConstantPoolEntry())
860 delete Constants[i].Val.MachineCPVal;
861 for (DenseSet<MachineConstantPoolValue*>::iterator I =
862 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
867 /// Test whether the given two constants can be allocated the same constant pool
869 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
870 const DataLayout &DL) {
871 // Handle the trivial case quickly.
872 if (A == B) return true;
874 // If they have the same type but weren't the same constant, quickly
876 if (A->getType() == B->getType()) return false;
878 // We can't handle structs or arrays.
879 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
880 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
883 // For now, only support constants with the same size.
884 uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
885 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
888 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
890 // Try constant folding a bitcast of both instructions to an integer. If we
891 // get two identical ConstantInt's, then we are good to share them. We use
892 // the constant folding APIs to do this so that we get the benefit of
894 if (isa<PointerType>(A->getType()))
895 A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
896 const_cast<Constant *>(A), DL);
897 else if (A->getType() != IntTy)
898 A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
899 const_cast<Constant *>(A), DL);
900 if (isa<PointerType>(B->getType()))
901 B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
902 const_cast<Constant *>(B), DL);
903 else if (B->getType() != IntTy)
904 B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
905 const_cast<Constant *>(B), DL);
910 /// Create a new entry in the constant pool or return an existing one.
911 /// User must specify the log2 of the minimum required alignment for the object.
912 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
913 unsigned Alignment) {
914 assert(Alignment && "Alignment must be specified!");
915 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
917 // Check to see if we already have this constant.
919 // FIXME, this could be made much more efficient for large constant pools.
920 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
921 if (!Constants[i].isMachineConstantPoolEntry() &&
922 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
923 if ((unsigned)Constants[i].getAlignment() < Alignment)
924 Constants[i].Alignment = Alignment;
928 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
929 return Constants.size()-1;
932 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
933 unsigned Alignment) {
934 assert(Alignment && "Alignment must be specified!");
935 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
937 // Check to see if we already have this constant.
939 // FIXME, this could be made much more efficient for large constant pools.
940 int Idx = V->getExistingMachineCPValue(this, Alignment);
942 MachineCPVsSharingEntries.insert(V);
943 return (unsigned)Idx;
946 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
947 return Constants.size()-1;
950 void MachineConstantPool::print(raw_ostream &OS) const {
951 if (Constants.empty()) return;
953 OS << "Constant Pool:\n";
954 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
955 OS << " cp#" << i << ": ";
956 if (Constants[i].isMachineConstantPoolEntry())
957 Constants[i].Val.MachineCPVal->print(OS);
959 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
960 OS << ", align=" << Constants[i].getAlignment();
965 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
966 void MachineConstantPool::dump() const { print(dbgs()); }