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/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"
48 #define DEBUG_TYPE "codegen"
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);
55 void MachineFunctionInitializer::anchor() {}
57 static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
58 typedef MachineFunctionProperties::Property P;
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";
69 llvm_unreachable("Invalid machine function property");
72 void MachineFunctionProperties::print(raw_ostream &OS) const {
73 const char *Separator = "";
74 for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
77 OS << Separator << getPropertyName(static_cast<Property>(I));
82 //===----------------------------------------------------------------------===//
83 // MachineFunction implementation
84 //===----------------------------------------------------------------------===//
86 // Out-of-line virtual method.
87 MachineFunctionInfo::~MachineFunctionInfo() {}
89 void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
90 MBB->getParent()->DeleteMachineBasicBlock(MBB);
93 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
95 if (Fn->hasFnAttribute(Attribute::StackAlignment))
96 return Fn->getFnStackAlignment();
97 return STI->getFrameLowering()->getStackAlignment();
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()),
104 FunctionNumber = FunctionNum;
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);
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));
127 if (Fn->hasFnAttribute(Attribute::StackAlignment))
128 FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
130 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
131 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
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());
139 if (AlignAllFunctions)
140 Alignment = AlignAllFunctions;
142 JumpTableInfo = nullptr;
144 if (isFuncletEHPersonality(classifyEHPersonality(
145 Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr))) {
146 WinEHInfo = new (Allocator) WinEHFuncInfo();
149 assert(Target.isCompatibleDataLayout(getDataLayout()) &&
150 "Can't create a MachineFunction using a Module with a "
151 "Target-incompatible DataLayout attached\n");
153 PSVManager = llvm::make_unique<PseudoSourceValueManager>();
156 MachineFunction::~MachineFunction() {
160 void MachineFunction::clear() {
162 // Don't call destructors on MachineInstr and MachineOperand. All of their
163 // memory comes from the BumpPtrAllocator which is about to be purged.
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();
169 InstructionRecycler.clear(Allocator);
170 OperandRecycler.clear(Allocator);
171 BasicBlockRecycler.clear(Allocator);
172 VariableDbgInfos.clear();
174 RegInfo->~MachineRegisterInfo();
175 Allocator.Deallocate(RegInfo);
178 MFInfo->~MachineFunctionInfo();
179 Allocator.Deallocate(MFInfo);
182 FrameInfo->~MachineFrameInfo();
183 Allocator.Deallocate(FrameInfo);
185 ConstantPool->~MachineConstantPool();
186 Allocator.Deallocate(ConstantPool);
189 JumpTableInfo->~MachineJumpTableInfo();
190 Allocator.Deallocate(JumpTableInfo);
194 WinEHInfo->~WinEHFuncInfo();
195 Allocator.Deallocate(WinEHInfo);
199 const DataLayout &MachineFunction::getDataLayout() const {
200 return Fn->getParent()->getDataLayout();
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;
209 JumpTableInfo = new (Allocator)
210 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
211 return JumpTableInfo;
214 /// Should we be emitting segmented stack stuff for the function
215 bool MachineFunction::shouldSplitStack() const {
216 return getFunction()->hasFnAttribute("split-stack");
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();
229 MBBI = MBB->getIterator();
231 // Figure out the block number this should have.
232 unsigned BlockNo = 0;
234 BlockNo = std::prev(MBBI)->getNumber() + 1;
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;
245 // If BlockNo is already taken, set that block's number to -1.
246 if (MBBNumbering[BlockNo])
247 MBBNumbering[BlockNo]->setNumber(-1);
249 MBBNumbering[BlockNo] = &*MBBI;
250 MBBI->setNumber(BlockNo);
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);
260 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
261 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
264 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
265 MachineInstr(*this, MCID, DL, NoImp);
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.
271 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
272 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
273 MachineInstr(*this, *Orig);
276 /// Delete the given MachineInstr.
278 /// This function also serves as the MachineInstr destructor - the real
279 /// ~MachineInstr() destructor must be empty.
281 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
282 // Strip it for parts. The operand array and the MI object itself are
283 // independently recyclable.
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
289 InstructionRecycler.Deallocate(Allocator, MI);
292 /// Allocate a new MachineBasicBlock. Use this instead of
293 /// `new MachineBasicBlock'.
295 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
296 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
297 MachineBasicBlock(*this, bb);
300 /// Delete the given MachineBasicBlock.
302 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
303 assert(MBB->getParent() == this && "MBB parent mismatch!");
304 MBB->~MachineBasicBlock();
305 BasicBlockRecycler.Deallocate(Allocator, MBB);
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);
319 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
320 int64_t Offset, uint64_t Size) {
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());
336 MachineInstr::mmo_iterator
337 MachineFunction::allocateMemRefsArray(unsigned long Num) {
338 return Allocator.Allocate<MachineMemOperand *>(Num);
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.
346 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
350 // Allocate a new array and populate it with the load information.
351 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
353 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
354 if ((*I)->isLoad()) {
355 if (!(*I)->isStore())
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;
372 return std::make_pair(Result, Result + Num);
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.
380 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
384 // Allocate a new array and populate it with the store information.
385 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
387 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
388 if ((*I)->isStore()) {
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;
406 return std::make_pair(Result, Result + Num);
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;
416 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
417 LLVM_DUMP_METHOD void MachineFunction::dump() const {
422 StringRef MachineFunction::getName() const {
423 assert(getFunction() && "No function!");
424 return getFunction()->getName();
427 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
428 OS << "# Machine code for function " << getName() << ": ";
429 getProperties().print(OS);
432 // Print Frame Information
433 FrameInfo->print(*this, OS);
435 // Print JumpTable Information
437 JumpTableInfo->print(OS);
439 // Print Constant Pool
440 ConstantPool->print(OS);
442 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
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);
450 OS << " in " << PrintReg(I->second, TRI);
451 if (std::next(I) != E)
457 ModuleSlotTracker MST(getFunction()->getParent());
458 MST.incorporateFunction(*getFunction());
459 for (const auto &BB : *this) {
461 BB.print(OS, MST, Indexes);
464 OS << "\n# End machine code for function " << getName() << ".\n\n";
469 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
471 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
473 static std::string getGraphName(const MachineFunction *F) {
474 return ("CFG for '" + F->getName() + "' function").str();
477 std::string getNodeLabel(const MachineBasicBlock *Node,
478 const MachineFunction *Graph) {
481 raw_string_ostream OSS(OutStr);
484 OSS << "BB#" << Node->getNumber();
485 if (const BasicBlock *BB = Node->getBasicBlock())
486 OSS << ": " << BB->getName();
491 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
493 // Process string output to make it nicer...
494 for (unsigned i = 0; i != OutStr.length(); ++i)
495 if (OutStr[i] == '\n') { // Left justify
497 OutStr.insert(OutStr.begin()+i+1, 'l');
504 void MachineFunction::viewCFG() const
507 ViewGraph(this, "mf" + getName());
509 errs() << "MachineFunction::viewCFG is only available in debug builds on "
510 << "systems with Graphviz or gv!\n";
514 void MachineFunction::viewCFGOnly() const
517 ViewGraph(this, "mf" + getName(), true);
519 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
520 << "systems with Graphviz or gv!\n";
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);
531 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
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!");
543 VReg = MRI.createVirtualRegister(RC);
544 MRI.addLiveIn(PReg, VReg);
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!");
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);
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");
572 /// \name Exception Handling
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)
584 LandingPads.push_back(LandingPadInfo(LandingPad));
585 return LandingPads[N];
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);
595 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
596 MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
597 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
598 LP.LandingPadLabel = LandingPadLabel;
599 return LandingPadLabel;
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]));
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));
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;
626 // Special case: we *should* emit LPs with null LP MBB. This indicates
628 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
629 LandingPads.erase(LandingPads.begin() + i);
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;
641 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
642 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
647 // Remove landing pads with no try-ranges.
648 if (LandingPads[i].BeginLabels.empty()) {
649 LandingPads.erase(LandingPads.begin() + i);
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();
662 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
663 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
664 LP.TypeIds.push_back(0);
667 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
668 const Function *Filter,
669 const BlockAddress *RecoverBA) {
670 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
672 Handler.FilterOrFinally = Filter;
673 Handler.RecoverBA = RecoverBA;
674 LP.SEHHandlers.push_back(Handler);
677 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
678 const Function *Cleanup) {
679 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
681 Handler.FilterOrFinally = Cleanup;
682 Handler.RecoverBA = nullptr;
683 LP.SEHHandlers.push_back(Handler);
686 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
687 ArrayRef<unsigned> Sites) {
688 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
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;
695 TypeInfos.push_back(TI);
696 return TypeInfos.size();
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();
708 if (FilterIds[--i] != TyIds[--j])
712 // The new filter coincides with range [i, end) of the existing filter.
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
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);
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()));
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();
750 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
752 MF.addFilterTypeInfo(&MBB, FilterList);
759 //===----------------------------------------------------------------------===//
760 // MachineFrameInfo implementation
761 //===----------------------------------------------------------------------===//
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;
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)
776 DEBUG(dbgs() << "Warning: requested alignment " << Align
777 << " exceeds the stack alignment " << StackAlign
778 << " when stack realignment is off" << '\n');
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,
790 int Index = (int)Objects.size() - NumFixedObjects - 1;
791 assert(Index >= 0 && "Bad frame index!");
792 ensureMaxAlignment(Alignment);
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);
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;
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
832 unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
833 Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
834 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
836 /*Alloca*/ nullptr, isAliased));
837 return -++NumFixedObjects;
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,
845 unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
846 Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
847 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
850 /*isAliased*/ false));
851 return -++NumFixedObjects;
854 BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
855 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
856 BitVector BV(TRI->getNumRegs());
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())
863 const MachineRegisterInfo &MRI = MF.getRegInfo();
864 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
868 // Saved CSRs are not pristine.
869 for (auto &I : getCalleeSavedInfo())
870 for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
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();
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.
886 for (int i = getObjectIndexBegin(); i != 0; ++i) {
887 int FixedOff = -getObjectOffset(i);
888 if (FixedOff > Offset) Offset = FixedOff;
890 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
891 if (isDeadObjectIndex(i))
893 Offset += getObjectSize(i);
894 unsigned Align = getObjectAlignment(i);
895 // Adjust to alignment boundary
896 Offset = (Offset+Align-1)/Align*Align;
898 MaxAlign = std::max(Align, MaxAlign);
901 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
902 Offset += getMaxCallFrameSize();
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
910 if (adjustsStack() || hasVarSizedObjects() ||
911 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
912 StackAlign = TFI->getStackAlignment();
914 StackAlign = TFI->getTransientStackAlignment();
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);
922 return (unsigned)Offset;
925 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
926 if (Objects.empty()) return;
928 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
929 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
931 OS << "Frame Objects:\n";
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) {
941 OS << "variable sized";
943 OS << "size=" << SO.Size;
944 OS << ", align=" << SO.Alignment;
946 if (i < NumFixedObjects)
948 if (i < NumFixedObjects || SO.SPOffset != -1) {
949 int64_t Off = SO.SPOffset - ValOffset;
950 OS << ", at location [SP";
961 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
962 LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
967 //===----------------------------------------------------------------------===//
968 // MachineJumpTableInfo implementation
969 //===----------------------------------------------------------------------===//
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:
980 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
981 case MachineJumpTableInfo::EK_LabelDifference32:
982 case MachineJumpTableInfo::EK_Custom32:
984 case MachineJumpTableInfo::EK_Inline:
987 llvm_unreachable("Unknown jump table encoding!");
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
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:
1007 llvm_unreachable("Unknown jump table encoding!");
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;
1018 /// If Old is the target of any jump tables, update the jump tables to branch
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);
1029 /// If Old is a target of the jump tables, update the jump table to branch to
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) {
1045 void MachineJumpTableInfo::print(raw_ostream &OS) const {
1046 if (JumpTables.empty()) return;
1048 OS << "Jump Tables:\n";
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();
1059 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1060 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
1064 //===----------------------------------------------------------------------===//
1065 // MachineConstantPool implementation
1066 //===----------------------------------------------------------------------===//
1068 void MachineConstantPoolValue::anchor() { }
1070 Type *MachineConstantPoolEntry::getType() const {
1071 if (isMachineConstantPoolEntry())
1072 return Val.MachineCPVal->getType();
1073 return Val.ConstVal->getType();
1076 bool MachineConstantPoolEntry::needsRelocation() const {
1077 if (isMachineConstantPoolEntry())
1079 return Val.ConstVal->needsRelocation();
1083 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
1084 if (needsRelocation())
1085 return SectionKind::getReadOnlyWithRel();
1086 switch (DL->getTypeAllocSize(getType())) {
1088 return SectionKind::getMergeableConst4();
1090 return SectionKind::getMergeableConst8();
1092 return SectionKind::getMergeableConst16();
1094 return SectionKind::getMergeableConst32();
1096 return SectionKind::getReadOnly();
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;
1109 for (DenseSet<MachineConstantPoolValue*>::iterator I =
1110 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
1112 if (Deleted.count(*I) == 0)
1117 /// Test whether the given two constants can be allocated the same constant pool
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;
1124 // If they have the same type but weren't the same constant, quickly
1126 if (A->getType() == B->getType()) return false;
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()))
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)
1138 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
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
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),
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),
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;
1167 // Check to see if we already have this constant.
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;
1178 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1179 return Constants.size()-1;
1182 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1183 unsigned Alignment) {
1184 assert(Alignment && "Alignment must be specified!");
1185 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1187 // Check to see if we already have this constant.
1189 // FIXME, this could be made much more efficient for large constant pools.
1190 int Idx = V->getExistingMachineCPValue(this, Alignment);
1192 MachineCPVsSharingEntries.insert(V);
1193 return (unsigned)Idx;
1196 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1197 return Constants.size()-1;
1200 void MachineConstantPool::print(raw_ostream &OS) const {
1201 if (Constants.empty()) return;
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);
1209 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1210 OS << ", align=" << Constants[i].getAlignment();
1215 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1216 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }