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 // MachineJumpTableInfo implementation
761 //===----------------------------------------------------------------------===//
763 /// Return the size of each entry in the jump table.
764 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
765 // The size of a jump table entry is 4 bytes unless the entry is just the
766 // address of a block, in which case it is the pointer size.
767 switch (getEntryKind()) {
768 case MachineJumpTableInfo::EK_BlockAddress:
769 return TD.getPointerSize();
770 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
772 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
773 case MachineJumpTableInfo::EK_LabelDifference32:
774 case MachineJumpTableInfo::EK_Custom32:
776 case MachineJumpTableInfo::EK_Inline:
779 llvm_unreachable("Unknown jump table encoding!");
782 /// Return the alignment of each entry in the jump table.
783 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
784 // The alignment of a jump table entry is the alignment of int32 unless the
785 // entry is just the address of a block, in which case it is the pointer
787 switch (getEntryKind()) {
788 case MachineJumpTableInfo::EK_BlockAddress:
789 return TD.getPointerABIAlignment();
790 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
791 return TD.getABIIntegerTypeAlignment(64);
792 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
793 case MachineJumpTableInfo::EK_LabelDifference32:
794 case MachineJumpTableInfo::EK_Custom32:
795 return TD.getABIIntegerTypeAlignment(32);
796 case MachineJumpTableInfo::EK_Inline:
799 llvm_unreachable("Unknown jump table encoding!");
802 /// Create a new jump table entry in the jump table info.
803 unsigned MachineJumpTableInfo::createJumpTableIndex(
804 const std::vector<MachineBasicBlock*> &DestBBs) {
805 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
806 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
807 return JumpTables.size()-1;
810 /// If Old is the target of any jump tables, update the jump tables to branch
812 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
813 MachineBasicBlock *New) {
814 assert(Old != New && "Not making a change?");
815 bool MadeChange = false;
816 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
817 ReplaceMBBInJumpTable(i, Old, New);
821 /// If Old is a target of the jump tables, update the jump table to branch to
823 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
824 MachineBasicBlock *Old,
825 MachineBasicBlock *New) {
826 assert(Old != New && "Not making a change?");
827 bool MadeChange = false;
828 MachineJumpTableEntry &JTE = JumpTables[Idx];
829 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
830 if (JTE.MBBs[j] == Old) {
837 void MachineJumpTableInfo::print(raw_ostream &OS) const {
838 if (JumpTables.empty()) return;
840 OS << "Jump Tables:\n";
842 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
843 OS << " jt#" << i << ": ";
844 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
845 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
851 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
852 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
856 //===----------------------------------------------------------------------===//
857 // MachineConstantPool implementation
858 //===----------------------------------------------------------------------===//
860 void MachineConstantPoolValue::anchor() { }
862 Type *MachineConstantPoolEntry::getType() const {
863 if (isMachineConstantPoolEntry())
864 return Val.MachineCPVal->getType();
865 return Val.ConstVal->getType();
868 bool MachineConstantPoolEntry::needsRelocation() const {
869 if (isMachineConstantPoolEntry())
871 return Val.ConstVal->needsRelocation();
875 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
876 if (needsRelocation())
877 return SectionKind::getReadOnlyWithRel();
878 switch (DL->getTypeAllocSize(getType())) {
880 return SectionKind::getMergeableConst4();
882 return SectionKind::getMergeableConst8();
884 return SectionKind::getMergeableConst16();
886 return SectionKind::getMergeableConst32();
888 return SectionKind::getReadOnly();
892 MachineConstantPool::~MachineConstantPool() {
893 // A constant may be a member of both Constants and MachineCPVsSharingEntries,
894 // so keep track of which we've deleted to avoid double deletions.
895 DenseSet<MachineConstantPoolValue*> Deleted;
896 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
897 if (Constants[i].isMachineConstantPoolEntry()) {
898 Deleted.insert(Constants[i].Val.MachineCPVal);
899 delete Constants[i].Val.MachineCPVal;
901 for (DenseSet<MachineConstantPoolValue*>::iterator I =
902 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
904 if (Deleted.count(*I) == 0)
909 /// Test whether the given two constants can be allocated the same constant pool
911 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
912 const DataLayout &DL) {
913 // Handle the trivial case quickly.
914 if (A == B) return true;
916 // If they have the same type but weren't the same constant, quickly
918 if (A->getType() == B->getType()) return false;
920 // We can't handle structs or arrays.
921 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
922 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
925 // For now, only support constants with the same size.
926 uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
927 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
930 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
932 // Try constant folding a bitcast of both instructions to an integer. If we
933 // get two identical ConstantInt's, then we are good to share them. We use
934 // the constant folding APIs to do this so that we get the benefit of
936 if (isa<PointerType>(A->getType()))
937 A = ConstantFoldCastOperand(Instruction::PtrToInt,
938 const_cast<Constant *>(A), IntTy, DL);
939 else if (A->getType() != IntTy)
940 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
942 if (isa<PointerType>(B->getType()))
943 B = ConstantFoldCastOperand(Instruction::PtrToInt,
944 const_cast<Constant *>(B), IntTy, DL);
945 else if (B->getType() != IntTy)
946 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
952 /// Create a new entry in the constant pool or return an existing one.
953 /// User must specify the log2 of the minimum required alignment for the object.
954 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
955 unsigned Alignment) {
956 assert(Alignment && "Alignment must be specified!");
957 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
959 // Check to see if we already have this constant.
961 // FIXME, this could be made much more efficient for large constant pools.
962 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
963 if (!Constants[i].isMachineConstantPoolEntry() &&
964 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
965 if ((unsigned)Constants[i].getAlignment() < Alignment)
966 Constants[i].Alignment = Alignment;
970 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
971 return Constants.size()-1;
974 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
975 unsigned Alignment) {
976 assert(Alignment && "Alignment must be specified!");
977 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
979 // Check to see if we already have this constant.
981 // FIXME, this could be made much more efficient for large constant pools.
982 int Idx = V->getExistingMachineCPValue(this, Alignment);
984 MachineCPVsSharingEntries.insert(V);
985 return (unsigned)Idx;
988 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
989 return Constants.size()-1;
992 void MachineConstantPool::print(raw_ostream &OS) const {
993 if (Constants.empty()) return;
995 OS << "Constant Pool:\n";
996 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
997 OS << " cp#" << i << ": ";
998 if (Constants[i].isMachineConstantPoolEntry())
999 Constants[i].Val.MachineCPVal->print(OS);
1001 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1002 OS << ", align=" << Constants[i].getAlignment();
1007 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1008 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }