xref: /llvm-project/llvm/lib/CodeGen/MachineFunction.cpp (revision 5e174e747451157ee60bc3143b8c6f2829657128)
1 //===- MachineFunction.cpp ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect native machine code information for a function.  This allows
11 // target-specific information about the generated code to be stored with each
12 // function.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Analysis/EHPersonalities.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/PseudoSourceValue.h"
36 #include "llvm/CodeGen/TargetFrameLowering.h"
37 #include "llvm/CodeGen/TargetLowering.h"
38 #include "llvm/CodeGen/TargetRegisterInfo.h"
39 #include "llvm/CodeGen/TargetSubtargetInfo.h"
40 #include "llvm/CodeGen/WasmEHFuncInfo.h"
41 #include "llvm/CodeGen/WinEHFuncInfo.h"
42 #include "llvm/Config/llvm-config.h"
43 #include "llvm/IR/Attributes.h"
44 #include "llvm/IR/BasicBlock.h"
45 #include "llvm/IR/Constant.h"
46 #include "llvm/IR/DataLayout.h"
47 #include "llvm/IR/DerivedTypes.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/Instruction.h"
51 #include "llvm/IR/Instructions.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/ModuleSlotTracker.h"
55 #include "llvm/IR/Value.h"
56 #include "llvm/MC/MCContext.h"
57 #include "llvm/MC/MCSymbol.h"
58 #include "llvm/MC/SectionKind.h"
59 #include "llvm/Support/Casting.h"
60 #include "llvm/Support/CommandLine.h"
61 #include "llvm/Support/Compiler.h"
62 #include "llvm/Support/DOTGraphTraits.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/GraphWriter.h"
66 #include "llvm/Support/raw_ostream.h"
67 #include "llvm/Target/TargetMachine.h"
68 #include <algorithm>
69 #include <cassert>
70 #include <cstddef>
71 #include <cstdint>
72 #include <iterator>
73 #include <string>
74 #include <utility>
75 #include <vector>
76 
77 using namespace llvm;
78 
79 #define DEBUG_TYPE "codegen"
80 
81 static cl::opt<unsigned>
82 AlignAllFunctions("align-all-functions",
83                   cl::desc("Force the alignment of all functions."),
84                   cl::init(0), cl::Hidden);
85 
86 static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
87   using P = MachineFunctionProperties::Property;
88 
89   switch(Prop) {
90   case P::FailedISel: return "FailedISel";
91   case P::IsSSA: return "IsSSA";
92   case P::Legalized: return "Legalized";
93   case P::NoPHIs: return "NoPHIs";
94   case P::NoVRegs: return "NoVRegs";
95   case P::RegBankSelected: return "RegBankSelected";
96   case P::Selected: return "Selected";
97   case P::TracksLiveness: return "TracksLiveness";
98   }
99   llvm_unreachable("Invalid machine function property");
100 }
101 
102 // Pin the vtable to this file.
103 void MachineFunction::Delegate::anchor() {}
104 
105 void MachineFunctionProperties::print(raw_ostream &OS) const {
106   const char *Separator = "";
107   for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
108     if (!Properties[I])
109       continue;
110     OS << Separator << getPropertyName(static_cast<Property>(I));
111     Separator = ", ";
112   }
113 }
114 
115 //===----------------------------------------------------------------------===//
116 // MachineFunction implementation
117 //===----------------------------------------------------------------------===//
118 
119 // Out-of-line virtual method.
120 MachineFunctionInfo::~MachineFunctionInfo() = default;
121 
122 void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
123   MBB->getParent()->DeleteMachineBasicBlock(MBB);
124 }
125 
126 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
127                                            const Function &F) {
128   if (F.hasFnAttribute(Attribute::StackAlignment))
129     return F.getFnStackAlignment();
130   return STI->getFrameLowering()->getStackAlignment();
131 }
132 
133 MachineFunction::MachineFunction(const Function &F, const TargetMachine &Target,
134                                  const TargetSubtargetInfo &STI,
135                                  unsigned FunctionNum, MachineModuleInfo &mmi)
136     : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
137   FunctionNumber = FunctionNum;
138   init();
139 }
140 
141 void MachineFunction::handleInsertion(const MachineInstr &MI) {
142   if (TheDelegate)
143     TheDelegate->MF_HandleInsertion(MI);
144 }
145 
146 void MachineFunction::handleRemoval(const MachineInstr &MI) {
147   if (TheDelegate)
148     TheDelegate->MF_HandleRemoval(MI);
149 }
150 
151 void MachineFunction::init() {
152   // Assume the function starts in SSA form with correct liveness.
153   Properties.set(MachineFunctionProperties::Property::IsSSA);
154   Properties.set(MachineFunctionProperties::Property::TracksLiveness);
155   if (STI->getRegisterInfo())
156     RegInfo = new (Allocator) MachineRegisterInfo(this);
157   else
158     RegInfo = nullptr;
159 
160   MFInfo = nullptr;
161   // We can realign the stack if the target supports it and the user hasn't
162   // explicitly asked us not to.
163   bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
164                       !F.hasFnAttribute("no-realign-stack");
165   FrameInfo = new (Allocator) MachineFrameInfo(
166       getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
167       /*ForceRealign=*/CanRealignSP &&
168           F.hasFnAttribute(Attribute::StackAlignment));
169 
170   if (F.hasFnAttribute(Attribute::StackAlignment))
171     FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
172 
173   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
174   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
175 
176   // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
177   // FIXME: Use Function::optForSize().
178   if (!F.hasFnAttribute(Attribute::OptimizeForSize))
179     Alignment = std::max(Alignment,
180                          STI->getTargetLowering()->getPrefFunctionAlignment());
181 
182   if (AlignAllFunctions)
183     Alignment = AlignAllFunctions;
184 
185   JumpTableInfo = nullptr;
186 
187   if (isFuncletEHPersonality(classifyEHPersonality(
188           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
189     WinEHInfo = new (Allocator) WinEHFuncInfo();
190   }
191 
192   if (isScopedEHPersonality(classifyEHPersonality(
193           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
194     WasmEHInfo = new (Allocator) WasmEHFuncInfo();
195   }
196 
197   assert(Target.isCompatibleDataLayout(getDataLayout()) &&
198          "Can't create a MachineFunction using a Module with a "
199          "Target-incompatible DataLayout attached\n");
200 
201   PSVManager =
202     llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
203                                                   getInstrInfo()));
204 }
205 
206 MachineFunction::~MachineFunction() {
207   clear();
208 }
209 
210 void MachineFunction::clear() {
211   Properties.reset();
212   // Don't call destructors on MachineInstr and MachineOperand. All of their
213   // memory comes from the BumpPtrAllocator which is about to be purged.
214   //
215   // Do call MachineBasicBlock destructors, it contains std::vectors.
216   for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
217     I->Insts.clearAndLeakNodesUnsafely();
218   MBBNumbering.clear();
219 
220   InstructionRecycler.clear(Allocator);
221   OperandRecycler.clear(Allocator);
222   BasicBlockRecycler.clear(Allocator);
223   CodeViewAnnotations.clear();
224   VariableDbgInfos.clear();
225   if (RegInfo) {
226     RegInfo->~MachineRegisterInfo();
227     Allocator.Deallocate(RegInfo);
228   }
229   if (MFInfo) {
230     MFInfo->~MachineFunctionInfo();
231     Allocator.Deallocate(MFInfo);
232   }
233 
234   FrameInfo->~MachineFrameInfo();
235   Allocator.Deallocate(FrameInfo);
236 
237   ConstantPool->~MachineConstantPool();
238   Allocator.Deallocate(ConstantPool);
239 
240   if (JumpTableInfo) {
241     JumpTableInfo->~MachineJumpTableInfo();
242     Allocator.Deallocate(JumpTableInfo);
243   }
244 
245   if (WinEHInfo) {
246     WinEHInfo->~WinEHFuncInfo();
247     Allocator.Deallocate(WinEHInfo);
248   }
249 
250   if (WasmEHInfo) {
251     WasmEHInfo->~WasmEHFuncInfo();
252     Allocator.Deallocate(WasmEHInfo);
253   }
254 }
255 
256 const DataLayout &MachineFunction::getDataLayout() const {
257   return F.getParent()->getDataLayout();
258 }
259 
260 /// Get the JumpTableInfo for this function.
261 /// If it does not already exist, allocate one.
262 MachineJumpTableInfo *MachineFunction::
263 getOrCreateJumpTableInfo(unsigned EntryKind) {
264   if (JumpTableInfo) return JumpTableInfo;
265 
266   JumpTableInfo = new (Allocator)
267     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
268   return JumpTableInfo;
269 }
270 
271 /// Should we be emitting segmented stack stuff for the function
272 bool MachineFunction::shouldSplitStack() const {
273   return getFunction().hasFnAttribute("split-stack");
274 }
275 
276 /// This discards all of the MachineBasicBlock numbers and recomputes them.
277 /// This guarantees that the MBB numbers are sequential, dense, and match the
278 /// ordering of the blocks within the function.  If a specific MachineBasicBlock
279 /// is specified, only that block and those after it are renumbered.
280 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
281   if (empty()) { MBBNumbering.clear(); return; }
282   MachineFunction::iterator MBBI, E = end();
283   if (MBB == nullptr)
284     MBBI = begin();
285   else
286     MBBI = MBB->getIterator();
287 
288   // Figure out the block number this should have.
289   unsigned BlockNo = 0;
290   if (MBBI != begin())
291     BlockNo = std::prev(MBBI)->getNumber() + 1;
292 
293   for (; MBBI != E; ++MBBI, ++BlockNo) {
294     if (MBBI->getNumber() != (int)BlockNo) {
295       // Remove use of the old number.
296       if (MBBI->getNumber() != -1) {
297         assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
298                "MBB number mismatch!");
299         MBBNumbering[MBBI->getNumber()] = nullptr;
300       }
301 
302       // If BlockNo is already taken, set that block's number to -1.
303       if (MBBNumbering[BlockNo])
304         MBBNumbering[BlockNo]->setNumber(-1);
305 
306       MBBNumbering[BlockNo] = &*MBBI;
307       MBBI->setNumber(BlockNo);
308     }
309   }
310 
311   // Okay, all the blocks are renumbered.  If we have compactified the block
312   // numbering, shrink MBBNumbering now.
313   assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
314   MBBNumbering.resize(BlockNo);
315 }
316 
317 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
318 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
319                                                   const DebugLoc &DL,
320                                                   bool NoImp) {
321   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
322     MachineInstr(*this, MCID, DL, NoImp);
323 }
324 
325 /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
326 /// identical in all ways except the instruction has no parent, prev, or next.
327 MachineInstr *
328 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
329   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
330              MachineInstr(*this, *Orig);
331 }
332 
333 MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
334     MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
335   MachineInstr *FirstClone = nullptr;
336   MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
337   while (true) {
338     MachineInstr *Cloned = CloneMachineInstr(&*I);
339     MBB.insert(InsertBefore, Cloned);
340     if (FirstClone == nullptr) {
341       FirstClone = Cloned;
342     } else {
343       Cloned->bundleWithPred();
344     }
345 
346     if (!I->isBundledWithSucc())
347       break;
348     ++I;
349   }
350   return *FirstClone;
351 }
352 
353 /// Delete the given MachineInstr.
354 ///
355 /// This function also serves as the MachineInstr destructor - the real
356 /// ~MachineInstr() destructor must be empty.
357 void
358 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
359   // Strip it for parts. The operand array and the MI object itself are
360   // independently recyclable.
361   if (MI->Operands)
362     deallocateOperandArray(MI->CapOperands, MI->Operands);
363   // Don't call ~MachineInstr() which must be trivial anyway because
364   // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
365   // destructors.
366   InstructionRecycler.Deallocate(Allocator, MI);
367 }
368 
369 /// Allocate a new MachineBasicBlock. Use this instead of
370 /// `new MachineBasicBlock'.
371 MachineBasicBlock *
372 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
373   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
374              MachineBasicBlock(*this, bb);
375 }
376 
377 /// Delete the given MachineBasicBlock.
378 void
379 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
380   assert(MBB->getParent() == this && "MBB parent mismatch!");
381   MBB->~MachineBasicBlock();
382   BasicBlockRecycler.Deallocate(Allocator, MBB);
383 }
384 
385 MachineMemOperand *MachineFunction::getMachineMemOperand(
386     MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
387     unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
388     SyncScope::ID SSID, AtomicOrdering Ordering,
389     AtomicOrdering FailureOrdering) {
390   return new (Allocator)
391       MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
392                         SSID, Ordering, FailureOrdering);
393 }
394 
395 MachineMemOperand *
396 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
397                                       int64_t Offset, uint64_t Size) {
398   if (MMO->getValue())
399     return new (Allocator)
400                MachineMemOperand(MachinePointerInfo(MMO->getValue(),
401                                                     MMO->getOffset()+Offset),
402                                  MMO->getFlags(), Size, MMO->getBaseAlignment(),
403                                  AAMDNodes(), nullptr, MMO->getSyncScopeID(),
404                                  MMO->getOrdering(), MMO->getFailureOrdering());
405   return new (Allocator)
406              MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
407                                                   MMO->getOffset()+Offset),
408                                MMO->getFlags(), Size, MMO->getBaseAlignment(),
409                                AAMDNodes(), nullptr, MMO->getSyncScopeID(),
410                                MMO->getOrdering(), MMO->getFailureOrdering());
411 }
412 
413 MachineMemOperand *
414 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
415                                       const AAMDNodes &AAInfo) {
416   MachinePointerInfo MPI = MMO->getValue() ?
417              MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
418              MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
419 
420   return new (Allocator)
421              MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
422                                MMO->getBaseAlignment(), AAInfo,
423                                MMO->getRanges(), MMO->getSyncScopeID(),
424                                MMO->getOrdering(), MMO->getFailureOrdering());
425 }
426 
427 MachineInstr::ExtraInfo *
428 MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
429                                    MCSymbol *PreInstrSymbol,
430                                    MCSymbol *PostInstrSymbol) {
431   return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
432                                          PostInstrSymbol);
433 }
434 
435 const char *MachineFunction::createExternalSymbolName(StringRef Name) {
436   char *Dest = Allocator.Allocate<char>(Name.size() + 1);
437   std::copy(Name.begin(), Name.end(), Dest);
438   Dest[Name.size()] = 0;
439   return Dest;
440 }
441 
442 uint32_t *MachineFunction::allocateRegMask() {
443   unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
444   unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
445   uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
446   memset(Mask, 0, Size * sizeof(Mask[0]));
447   return Mask;
448 }
449 
450 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
451 LLVM_DUMP_METHOD void MachineFunction::dump() const {
452   print(dbgs());
453 }
454 #endif
455 
456 StringRef MachineFunction::getName() const {
457   return getFunction().getName();
458 }
459 
460 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
461   OS << "# Machine code for function " << getName() << ": ";
462   getProperties().print(OS);
463   OS << '\n';
464 
465   // Print Frame Information
466   FrameInfo->print(*this, OS);
467 
468   // Print JumpTable Information
469   if (JumpTableInfo)
470     JumpTableInfo->print(OS);
471 
472   // Print Constant Pool
473   ConstantPool->print(OS);
474 
475   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
476 
477   if (RegInfo && !RegInfo->livein_empty()) {
478     OS << "Function Live Ins: ";
479     for (MachineRegisterInfo::livein_iterator
480          I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
481       OS << printReg(I->first, TRI);
482       if (I->second)
483         OS << " in " << printReg(I->second, TRI);
484       if (std::next(I) != E)
485         OS << ", ";
486     }
487     OS << '\n';
488   }
489 
490   ModuleSlotTracker MST(getFunction().getParent());
491   MST.incorporateFunction(getFunction());
492   for (const auto &BB : *this) {
493     OS << '\n';
494     // If we print the whole function, print it at its most verbose level.
495     BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
496   }
497 
498   OS << "\n# End machine code for function " << getName() << ".\n\n";
499 }
500 
501 namespace llvm {
502 
503   template<>
504   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
505     DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
506 
507     static std::string getGraphName(const MachineFunction *F) {
508       return ("CFG for '" + F->getName() + "' function").str();
509     }
510 
511     std::string getNodeLabel(const MachineBasicBlock *Node,
512                              const MachineFunction *Graph) {
513       std::string OutStr;
514       {
515         raw_string_ostream OSS(OutStr);
516 
517         if (isSimple()) {
518           OSS << printMBBReference(*Node);
519           if (const BasicBlock *BB = Node->getBasicBlock())
520             OSS << ": " << BB->getName();
521         } else
522           Node->print(OSS);
523       }
524 
525       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
526 
527       // Process string output to make it nicer...
528       for (unsigned i = 0; i != OutStr.length(); ++i)
529         if (OutStr[i] == '\n') {                            // Left justify
530           OutStr[i] = '\\';
531           OutStr.insert(OutStr.begin()+i+1, 'l');
532         }
533       return OutStr;
534     }
535   };
536 
537 } // end namespace llvm
538 
539 void MachineFunction::viewCFG() const
540 {
541 #ifndef NDEBUG
542   ViewGraph(this, "mf" + getName());
543 #else
544   errs() << "MachineFunction::viewCFG is only available in debug builds on "
545          << "systems with Graphviz or gv!\n";
546 #endif // NDEBUG
547 }
548 
549 void MachineFunction::viewCFGOnly() const
550 {
551 #ifndef NDEBUG
552   ViewGraph(this, "mf" + getName(), true);
553 #else
554   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
555          << "systems with Graphviz or gv!\n";
556 #endif // NDEBUG
557 }
558 
559 /// Add the specified physical register as a live-in value and
560 /// create a corresponding virtual register for it.
561 unsigned MachineFunction::addLiveIn(unsigned PReg,
562                                     const TargetRegisterClass *RC) {
563   MachineRegisterInfo &MRI = getRegInfo();
564   unsigned VReg = MRI.getLiveInVirtReg(PReg);
565   if (VReg) {
566     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
567     (void)VRegRC;
568     // A physical register can be added several times.
569     // Between two calls, the register class of the related virtual register
570     // may have been constrained to match some operation constraints.
571     // In that case, check that the current register class includes the
572     // physical register and is a sub class of the specified RC.
573     assert((VRegRC == RC || (VRegRC->contains(PReg) &&
574                              RC->hasSubClassEq(VRegRC))) &&
575             "Register class mismatch!");
576     return VReg;
577   }
578   VReg = MRI.createVirtualRegister(RC);
579   MRI.addLiveIn(PReg, VReg);
580   return VReg;
581 }
582 
583 /// Return the MCSymbol for the specified non-empty jump table.
584 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
585 /// normal 'L' label is returned.
586 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
587                                         bool isLinkerPrivate) const {
588   const DataLayout &DL = getDataLayout();
589   assert(JumpTableInfo && "No jump tables");
590   assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
591 
592   StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
593                                      : DL.getPrivateGlobalPrefix();
594   SmallString<60> Name;
595   raw_svector_ostream(Name)
596     << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
597   return Ctx.getOrCreateSymbol(Name);
598 }
599 
600 /// Return a function-local symbol to represent the PIC base.
601 MCSymbol *MachineFunction::getPICBaseSymbol() const {
602   const DataLayout &DL = getDataLayout();
603   return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
604                                Twine(getFunctionNumber()) + "$pb");
605 }
606 
607 /// \name Exception Handling
608 /// \{
609 
610 LandingPadInfo &
611 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
612   unsigned N = LandingPads.size();
613   for (unsigned i = 0; i < N; ++i) {
614     LandingPadInfo &LP = LandingPads[i];
615     if (LP.LandingPadBlock == LandingPad)
616       return LP;
617   }
618 
619   LandingPads.push_back(LandingPadInfo(LandingPad));
620   return LandingPads[N];
621 }
622 
623 void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
624                                 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
625   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
626   LP.BeginLabels.push_back(BeginLabel);
627   LP.EndLabels.push_back(EndLabel);
628 }
629 
630 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
631   MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
632   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
633   LP.LandingPadLabel = LandingPadLabel;
634 
635   const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
636   if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
637     if (const auto *PF =
638             dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
639       getMMI().addPersonality(PF);
640 
641     if (LPI->isCleanup())
642       addCleanup(LandingPad);
643 
644     // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
645     //        correct, but we need to do it this way because of how the DWARF EH
646     //        emitter processes the clauses.
647     for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
648       Value *Val = LPI->getClause(I - 1);
649       if (LPI->isCatch(I - 1)) {
650         addCatchTypeInfo(LandingPad,
651                          dyn_cast<GlobalValue>(Val->stripPointerCasts()));
652       } else {
653         // Add filters in a list.
654         auto *CVal = cast<Constant>(Val);
655         SmallVector<const GlobalValue *, 4> FilterList;
656         for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
657              II != IE; ++II)
658           FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
659 
660         addFilterTypeInfo(LandingPad, FilterList);
661       }
662     }
663 
664   } else if (isa<CatchPadInst>(FirstI)) {
665     // TODO
666 
667   } else {
668     assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
669   }
670 
671   return LandingPadLabel;
672 }
673 
674 void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
675                                        ArrayRef<const GlobalValue *> TyInfo) {
676   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
677   for (unsigned N = TyInfo.size(); N; --N)
678     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
679 }
680 
681 void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
682                                         ArrayRef<const GlobalValue *> TyInfo) {
683   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
684   std::vector<unsigned> IdsInFilter(TyInfo.size());
685   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
686     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
687   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
688 }
689 
690 void MachineFunction::tidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
691   for (unsigned i = 0; i != LandingPads.size(); ) {
692     LandingPadInfo &LandingPad = LandingPads[i];
693     if (LandingPad.LandingPadLabel &&
694         !LandingPad.LandingPadLabel->isDefined() &&
695         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
696       LandingPad.LandingPadLabel = nullptr;
697 
698     // Special case: we *should* emit LPs with null LP MBB. This indicates
699     // "nounwind" case.
700     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
701       LandingPads.erase(LandingPads.begin() + i);
702       continue;
703     }
704 
705     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
706       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
707       MCSymbol *EndLabel = LandingPad.EndLabels[j];
708       if ((BeginLabel->isDefined() ||
709            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
710           (EndLabel->isDefined() ||
711            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
712 
713       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
714       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
715       --j;
716       --e;
717     }
718 
719     // Remove landing pads with no try-ranges.
720     if (LandingPads[i].BeginLabels.empty()) {
721       LandingPads.erase(LandingPads.begin() + i);
722       continue;
723     }
724 
725     // If there is no landing pad, ensure that the list of typeids is empty.
726     // If the only typeid is a cleanup, this is the same as having no typeids.
727     if (!LandingPad.LandingPadBlock ||
728         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
729       LandingPad.TypeIds.clear();
730     ++i;
731   }
732 }
733 
734 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
735   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
736   LP.TypeIds.push_back(0);
737 }
738 
739 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
740                                          const Function *Filter,
741                                          const BlockAddress *RecoverBA) {
742   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
743   SEHHandler Handler;
744   Handler.FilterOrFinally = Filter;
745   Handler.RecoverBA = RecoverBA;
746   LP.SEHHandlers.push_back(Handler);
747 }
748 
749 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
750                                            const Function *Cleanup) {
751   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
752   SEHHandler Handler;
753   Handler.FilterOrFinally = Cleanup;
754   Handler.RecoverBA = nullptr;
755   LP.SEHHandlers.push_back(Handler);
756 }
757 
758 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
759                                             ArrayRef<unsigned> Sites) {
760   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
761 }
762 
763 unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
764   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
765     if (TypeInfos[i] == TI) return i + 1;
766 
767   TypeInfos.push_back(TI);
768   return TypeInfos.size();
769 }
770 
771 int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
772   // If the new filter coincides with the tail of an existing filter, then
773   // re-use the existing filter.  Folding filters more than this requires
774   // re-ordering filters and/or their elements - probably not worth it.
775   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
776        E = FilterEnds.end(); I != E; ++I) {
777     unsigned i = *I, j = TyIds.size();
778 
779     while (i && j)
780       if (FilterIds[--i] != TyIds[--j])
781         goto try_next;
782 
783     if (!j)
784       // The new filter coincides with range [i, end) of the existing filter.
785       return -(1 + i);
786 
787 try_next:;
788   }
789 
790   // Add the new filter.
791   int FilterID = -(1 + FilterIds.size());
792   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
793   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
794   FilterEnds.push_back(FilterIds.size());
795   FilterIds.push_back(0); // terminator
796   return FilterID;
797 }
798 
799 /// \}
800 
801 //===----------------------------------------------------------------------===//
802 //  MachineJumpTableInfo implementation
803 //===----------------------------------------------------------------------===//
804 
805 /// Return the size of each entry in the jump table.
806 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
807   // The size of a jump table entry is 4 bytes unless the entry is just the
808   // address of a block, in which case it is the pointer size.
809   switch (getEntryKind()) {
810   case MachineJumpTableInfo::EK_BlockAddress:
811     return TD.getPointerSize();
812   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
813     return 8;
814   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
815   case MachineJumpTableInfo::EK_LabelDifference32:
816   case MachineJumpTableInfo::EK_Custom32:
817     return 4;
818   case MachineJumpTableInfo::EK_Inline:
819     return 0;
820   }
821   llvm_unreachable("Unknown jump table encoding!");
822 }
823 
824 /// Return the alignment of each entry in the jump table.
825 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
826   // The alignment of a jump table entry is the alignment of int32 unless the
827   // entry is just the address of a block, in which case it is the pointer
828   // alignment.
829   switch (getEntryKind()) {
830   case MachineJumpTableInfo::EK_BlockAddress:
831     return TD.getPointerABIAlignment(0);
832   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
833     return TD.getABIIntegerTypeAlignment(64);
834   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
835   case MachineJumpTableInfo::EK_LabelDifference32:
836   case MachineJumpTableInfo::EK_Custom32:
837     return TD.getABIIntegerTypeAlignment(32);
838   case MachineJumpTableInfo::EK_Inline:
839     return 1;
840   }
841   llvm_unreachable("Unknown jump table encoding!");
842 }
843 
844 /// Create a new jump table entry in the jump table info.
845 unsigned MachineJumpTableInfo::createJumpTableIndex(
846                                const std::vector<MachineBasicBlock*> &DestBBs) {
847   assert(!DestBBs.empty() && "Cannot create an empty jump table!");
848   JumpTables.push_back(MachineJumpTableEntry(DestBBs));
849   return JumpTables.size()-1;
850 }
851 
852 /// If Old is the target of any jump tables, update the jump tables to branch
853 /// to New instead.
854 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
855                                                   MachineBasicBlock *New) {
856   assert(Old != New && "Not making a change?");
857   bool MadeChange = false;
858   for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
859     ReplaceMBBInJumpTable(i, Old, New);
860   return MadeChange;
861 }
862 
863 /// If Old is a target of the jump tables, update the jump table to branch to
864 /// New instead.
865 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
866                                                  MachineBasicBlock *Old,
867                                                  MachineBasicBlock *New) {
868   assert(Old != New && "Not making a change?");
869   bool MadeChange = false;
870   MachineJumpTableEntry &JTE = JumpTables[Idx];
871   for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
872     if (JTE.MBBs[j] == Old) {
873       JTE.MBBs[j] = New;
874       MadeChange = true;
875     }
876   return MadeChange;
877 }
878 
879 void MachineJumpTableInfo::print(raw_ostream &OS) const {
880   if (JumpTables.empty()) return;
881 
882   OS << "Jump Tables:\n";
883 
884   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
885     OS << printJumpTableEntryReference(i) << ": ";
886     for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
887       OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
888   }
889 
890   OS << '\n';
891 }
892 
893 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
894 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
895 #endif
896 
897 Printable llvm::printJumpTableEntryReference(unsigned Idx) {
898   return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
899 }
900 
901 //===----------------------------------------------------------------------===//
902 //  MachineConstantPool implementation
903 //===----------------------------------------------------------------------===//
904 
905 void MachineConstantPoolValue::anchor() {}
906 
907 Type *MachineConstantPoolEntry::getType() const {
908   if (isMachineConstantPoolEntry())
909     return Val.MachineCPVal->getType();
910   return Val.ConstVal->getType();
911 }
912 
913 bool MachineConstantPoolEntry::needsRelocation() const {
914   if (isMachineConstantPoolEntry())
915     return true;
916   return Val.ConstVal->needsRelocation();
917 }
918 
919 SectionKind
920 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
921   if (needsRelocation())
922     return SectionKind::getReadOnlyWithRel();
923   switch (DL->getTypeAllocSize(getType())) {
924   case 4:
925     return SectionKind::getMergeableConst4();
926   case 8:
927     return SectionKind::getMergeableConst8();
928   case 16:
929     return SectionKind::getMergeableConst16();
930   case 32:
931     return SectionKind::getMergeableConst32();
932   default:
933     return SectionKind::getReadOnly();
934   }
935 }
936 
937 MachineConstantPool::~MachineConstantPool() {
938   // A constant may be a member of both Constants and MachineCPVsSharingEntries,
939   // so keep track of which we've deleted to avoid double deletions.
940   DenseSet<MachineConstantPoolValue*> Deleted;
941   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
942     if (Constants[i].isMachineConstantPoolEntry()) {
943       Deleted.insert(Constants[i].Val.MachineCPVal);
944       delete Constants[i].Val.MachineCPVal;
945     }
946   for (DenseSet<MachineConstantPoolValue*>::iterator I =
947        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
948        I != E; ++I) {
949     if (Deleted.count(*I) == 0)
950       delete *I;
951   }
952 }
953 
954 /// Test whether the given two constants can be allocated the same constant pool
955 /// entry.
956 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
957                                       const DataLayout &DL) {
958   // Handle the trivial case quickly.
959   if (A == B) return true;
960 
961   // If they have the same type but weren't the same constant, quickly
962   // reject them.
963   if (A->getType() == B->getType()) return false;
964 
965   // We can't handle structs or arrays.
966   if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
967       isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
968     return false;
969 
970   // For now, only support constants with the same size.
971   uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
972   if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
973     return false;
974 
975   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
976 
977   // Try constant folding a bitcast of both instructions to an integer.  If we
978   // get two identical ConstantInt's, then we are good to share them.  We use
979   // the constant folding APIs to do this so that we get the benefit of
980   // DataLayout.
981   if (isa<PointerType>(A->getType()))
982     A = ConstantFoldCastOperand(Instruction::PtrToInt,
983                                 const_cast<Constant *>(A), IntTy, DL);
984   else if (A->getType() != IntTy)
985     A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
986                                 IntTy, DL);
987   if (isa<PointerType>(B->getType()))
988     B = ConstantFoldCastOperand(Instruction::PtrToInt,
989                                 const_cast<Constant *>(B), IntTy, DL);
990   else if (B->getType() != IntTy)
991     B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
992                                 IntTy, DL);
993 
994   return A == B;
995 }
996 
997 /// Create a new entry in the constant pool or return an existing one.
998 /// User must specify the log2 of the minimum required alignment for the object.
999 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
1000                                                    unsigned Alignment) {
1001   assert(Alignment && "Alignment must be specified!");
1002   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1003 
1004   // Check to see if we already have this constant.
1005   //
1006   // FIXME, this could be made much more efficient for large constant pools.
1007   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
1008     if (!Constants[i].isMachineConstantPoolEntry() &&
1009         CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
1010       if ((unsigned)Constants[i].getAlignment() < Alignment)
1011         Constants[i].Alignment = Alignment;
1012       return i;
1013     }
1014 
1015   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1016   return Constants.size()-1;
1017 }
1018 
1019 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1020                                                    unsigned Alignment) {
1021   assert(Alignment && "Alignment must be specified!");
1022   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1023 
1024   // Check to see if we already have this constant.
1025   //
1026   // FIXME, this could be made much more efficient for large constant pools.
1027   int Idx = V->getExistingMachineCPValue(this, Alignment);
1028   if (Idx != -1) {
1029     MachineCPVsSharingEntries.insert(V);
1030     return (unsigned)Idx;
1031   }
1032 
1033   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1034   return Constants.size()-1;
1035 }
1036 
1037 void MachineConstantPool::print(raw_ostream &OS) const {
1038   if (Constants.empty()) return;
1039 
1040   OS << "Constant Pool:\n";
1041   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1042     OS << "  cp#" << i << ": ";
1043     if (Constants[i].isMachineConstantPoolEntry())
1044       Constants[i].Val.MachineCPVal->print(OS);
1045     else
1046       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1047     OS << ", align=" << Constants[i].getAlignment();
1048     OS << "\n";
1049   }
1050 }
1051 
1052 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1053 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1054 #endif
1055