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