]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/MachineFunction.cpp
Merge ^/head r319480 through r319547.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / MachineFunction.cpp
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/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"
46 using namespace llvm;
47
48 #define DEBUG_TYPE "codegen"
49
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);
54
55 void MachineFunctionInitializer::anchor() {}
56
57 static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
58   typedef MachineFunctionProperties::Property P;
59   switch(Prop) {
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";
68   }
69   llvm_unreachable("Invalid machine function property");
70 }
71
72 void MachineFunctionProperties::print(raw_ostream &OS) const {
73   const char *Separator = "";
74   for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
75     if (!Properties[I])
76       continue;
77     OS << Separator << getPropertyName(static_cast<Property>(I));
78     Separator = ", ";
79   }
80 }
81
82 //===----------------------------------------------------------------------===//
83 // MachineFunction implementation
84 //===----------------------------------------------------------------------===//
85
86 // Out-of-line virtual method.
87 MachineFunctionInfo::~MachineFunctionInfo() {}
88
89 void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
90   MBB->getParent()->DeleteMachineBasicBlock(MBB);
91 }
92
93 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
94                                            const Function *Fn) {
95   if (Fn->hasFnAttribute(Attribute::StackAlignment))
96     return Fn->getFnStackAlignment();
97   return STI->getFrameLowering()->getStackAlignment();
98 }
99
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()),
103       MMI(mmi) {
104   FunctionNumber = FunctionNum;
105   init();
106 }
107
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);
114   else
115     RegInfo = nullptr;
116
117   MFInfo = nullptr;
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));
126
127   if (Fn->hasFnAttribute(Attribute::StackAlignment))
128     FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
129
130   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
131   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
132
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());
138
139   if (AlignAllFunctions)
140     Alignment = AlignAllFunctions;
141
142   JumpTableInfo = nullptr;
143
144   if (isFuncletEHPersonality(classifyEHPersonality(
145           Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr))) {
146     WinEHInfo = new (Allocator) WinEHFuncInfo();
147   }
148
149   assert(Target.isCompatibleDataLayout(getDataLayout()) &&
150          "Can't create a MachineFunction using a Module with a "
151          "Target-incompatible DataLayout attached\n");
152
153   PSVManager = llvm::make_unique<PseudoSourceValueManager>();
154 }
155
156 MachineFunction::~MachineFunction() {
157   clear();
158 }
159
160 void MachineFunction::clear() {
161   Properties.reset();
162   // Don't call destructors on MachineInstr and MachineOperand. All of their
163   // memory comes from the BumpPtrAllocator which is about to be purged.
164   //
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();
168
169   InstructionRecycler.clear(Allocator);
170   OperandRecycler.clear(Allocator);
171   BasicBlockRecycler.clear(Allocator);
172   VariableDbgInfos.clear();
173   if (RegInfo) {
174     RegInfo->~MachineRegisterInfo();
175     Allocator.Deallocate(RegInfo);
176   }
177   if (MFInfo) {
178     MFInfo->~MachineFunctionInfo();
179     Allocator.Deallocate(MFInfo);
180   }
181
182   FrameInfo->~MachineFrameInfo();
183   Allocator.Deallocate(FrameInfo);
184
185   ConstantPool->~MachineConstantPool();
186   Allocator.Deallocate(ConstantPool);
187
188   if (JumpTableInfo) {
189     JumpTableInfo->~MachineJumpTableInfo();
190     Allocator.Deallocate(JumpTableInfo);
191   }
192
193   if (WinEHInfo) {
194     WinEHInfo->~WinEHFuncInfo();
195     Allocator.Deallocate(WinEHInfo);
196   }
197 }
198
199 const DataLayout &MachineFunction::getDataLayout() const {
200   return Fn->getParent()->getDataLayout();
201 }
202
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;
208
209   JumpTableInfo = new (Allocator)
210     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
211   return JumpTableInfo;
212 }
213
214 /// Should we be emitting segmented stack stuff for the function
215 bool MachineFunction::shouldSplitStack() const {
216   return getFunction()->hasFnAttribute("split-stack");
217 }
218
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();
226   if (MBB == nullptr)
227     MBBI = begin();
228   else
229     MBBI = MBB->getIterator();
230
231   // Figure out the block number this should have.
232   unsigned BlockNo = 0;
233   if (MBBI != begin())
234     BlockNo = std::prev(MBBI)->getNumber() + 1;
235
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;
243       }
244
245       // If BlockNo is already taken, set that block's number to -1.
246       if (MBBNumbering[BlockNo])
247         MBBNumbering[BlockNo]->setNumber(-1);
248
249       MBBNumbering[BlockNo] = &*MBBI;
250       MBBI->setNumber(BlockNo);
251     }
252   }
253
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);
258 }
259
260 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
261 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
262                                                   const DebugLoc &DL,
263                                                   bool NoImp) {
264   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
265     MachineInstr(*this, MCID, DL, NoImp);
266 }
267
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.
270 MachineInstr *
271 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
272   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
273              MachineInstr(*this, *Orig);
274 }
275
276 /// Delete the given MachineInstr.
277 ///
278 /// This function also serves as the MachineInstr destructor - the real
279 /// ~MachineInstr() destructor must be empty.
280 void
281 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
282   // Strip it for parts. The operand array and the MI object itself are
283   // independently recyclable.
284   if (MI->Operands)
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
288   // destructors.
289   InstructionRecycler.Deallocate(Allocator, MI);
290 }
291
292 /// Allocate a new MachineBasicBlock. Use this instead of
293 /// `new MachineBasicBlock'.
294 MachineBasicBlock *
295 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
296   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
297              MachineBasicBlock(*this, bb);
298 }
299
300 /// Delete the given MachineBasicBlock.
301 void
302 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
303   assert(MBB->getParent() == this && "MBB parent mismatch!");
304   MBB->~MachineBasicBlock();
305   BasicBlockRecycler.Deallocate(Allocator, MBB);
306 }
307
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);
316 }
317
318 MachineMemOperand *
319 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
320                                       int64_t Offset, uint64_t Size) {
321   if (MMO->getValue())
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());
334 }
335
336 MachineInstr::mmo_iterator
337 MachineFunction::allocateMemRefsArray(unsigned long Num) {
338   return Allocator.Allocate<MachineMemOperand *>(Num);
339 }
340
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.
345   unsigned Num = 0;
346   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
347     if ((*I)->isLoad())
348       ++Num;
349
350   // Allocate a new array and populate it with the load information.
351   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
352   unsigned Index = 0;
353   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
354     if ((*I)->isLoad()) {
355       if (!(*I)->isStore())
356         // Reuse the MMO.
357         Result[Index] = *I;
358       else {
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;
368       }
369       ++Index;
370     }
371   }
372   return std::make_pair(Result, Result + Num);
373 }
374
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.
379   unsigned Num = 0;
380   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
381     if ((*I)->isStore())
382       ++Num;
383
384   // Allocate a new array and populate it with the store information.
385   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
386   unsigned Index = 0;
387   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
388     if ((*I)->isStore()) {
389       if (!(*I)->isLoad())
390         // Reuse the MMO.
391         Result[Index] = *I;
392       else {
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;
402       }
403       ++Index;
404     }
405   }
406   return std::make_pair(Result, Result + Num);
407 }
408
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;
413   return Dest;
414 }
415
416 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
417 LLVM_DUMP_METHOD void MachineFunction::dump() const {
418   print(dbgs());
419 }
420 #endif
421
422 StringRef MachineFunction::getName() const {
423   assert(getFunction() && "No function!");
424   return getFunction()->getName();
425 }
426
427 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
428   OS << "# Machine code for function " << getName() << ": ";
429   getProperties().print(OS);
430   OS << '\n';
431
432   // Print Frame Information
433   FrameInfo->print(*this, OS);
434
435   // Print JumpTable Information
436   if (JumpTableInfo)
437     JumpTableInfo->print(OS);
438
439   // Print Constant Pool
440   ConstantPool->print(OS);
441
442   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
443
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);
449       if (I->second)
450         OS << " in " << PrintReg(I->second, TRI);
451       if (std::next(I) != E)
452         OS << ", ";
453     }
454     OS << '\n';
455   }
456
457   ModuleSlotTracker MST(getFunction()->getParent());
458   MST.incorporateFunction(*getFunction());
459   for (const auto &BB : *this) {
460     OS << '\n';
461     BB.print(OS, MST, Indexes);
462   }
463
464   OS << "\n# End machine code for function " << getName() << ".\n\n";
465 }
466
467 namespace llvm {
468   template<>
469   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
470
471   DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
472
473     static std::string getGraphName(const MachineFunction *F) {
474       return ("CFG for '" + F->getName() + "' function").str();
475     }
476
477     std::string getNodeLabel(const MachineBasicBlock *Node,
478                              const MachineFunction *Graph) {
479       std::string OutStr;
480       {
481         raw_string_ostream OSS(OutStr);
482
483         if (isSimple()) {
484           OSS << "BB#" << Node->getNumber();
485           if (const BasicBlock *BB = Node->getBasicBlock())
486             OSS << ": " << BB->getName();
487         } else
488           Node->print(OSS);
489       }
490
491       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
492
493       // Process string output to make it nicer...
494       for (unsigned i = 0; i != OutStr.length(); ++i)
495         if (OutStr[i] == '\n') {                            // Left justify
496           OutStr[i] = '\\';
497           OutStr.insert(OutStr.begin()+i+1, 'l');
498         }
499       return OutStr;
500     }
501   };
502 }
503
504 void MachineFunction::viewCFG() const
505 {
506 #ifndef NDEBUG
507   ViewGraph(this, "mf" + getName());
508 #else
509   errs() << "MachineFunction::viewCFG is only available in debug builds on "
510          << "systems with Graphviz or gv!\n";
511 #endif // NDEBUG
512 }
513
514 void MachineFunction::viewCFGOnly() const
515 {
516 #ifndef NDEBUG
517   ViewGraph(this, "mf" + getName(), true);
518 #else
519   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
520          << "systems with Graphviz or gv!\n";
521 #endif // NDEBUG
522 }
523
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);
530   if (VReg) {
531     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
532     (void)VRegRC;
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!");
541     return VReg;
542   }
543   VReg = MRI.createVirtualRegister(RC);
544   MRI.addLiveIn(PReg, VReg);
545   return VReg;
546 }
547
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!");
556
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);
563 }
564
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");
570 }
571
572 /// \name Exception Handling
573 /// \{
574
575 LandingPadInfo &
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)
581       return LP;
582   }
583
584   LandingPads.push_back(LandingPadInfo(LandingPad));
585   return LandingPads[N];
586 }
587
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);
593 }
594
595 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
596   MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
597   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
598   LP.LandingPadLabel = LandingPadLabel;
599   return LandingPadLabel;
600 }
601
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]));
607 }
608
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));
616 }
617
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;
625
626     // Special case: we *should* emit LPs with null LP MBB. This indicates
627     // "nounwind" case.
628     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
629       LandingPads.erase(LandingPads.begin() + i);
630       continue;
631     }
632
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;
640
641       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
642       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
643       --j;
644       --e;
645     }
646
647     // Remove landing pads with no try-ranges.
648     if (LandingPads[i].BeginLabels.empty()) {
649       LandingPads.erase(LandingPads.begin() + i);
650       continue;
651     }
652
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();
658     ++i;
659   }
660 }
661
662 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
663   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
664   LP.TypeIds.push_back(0);
665 }
666
667 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
668                                          const Function *Filter,
669                                          const BlockAddress *RecoverBA) {
670   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
671   SEHHandler Handler;
672   Handler.FilterOrFinally = Filter;
673   Handler.RecoverBA = RecoverBA;
674   LP.SEHHandlers.push_back(Handler);
675 }
676
677 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
678                                            const Function *Cleanup) {
679   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
680   SEHHandler Handler;
681   Handler.FilterOrFinally = Cleanup;
682   Handler.RecoverBA = nullptr;
683   LP.SEHHandlers.push_back(Handler);
684 }
685
686 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
687                                             ArrayRef<unsigned> Sites) {
688   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
689 }
690
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;
694
695   TypeInfos.push_back(TI);
696   return TypeInfos.size();
697 }
698
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();
706
707     while (i && j)
708       if (FilterIds[--i] != TyIds[--j])
709         goto try_next;
710
711     if (!j)
712       // The new filter coincides with range [i, end) of the existing filter.
713       return -(1 + i);
714
715 try_next:;
716   }
717
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
724   return FilterID;
725 }
726
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);
732
733   if (I.isCleanup())
734     MF.addCleanup(&MBB);
735
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()));
744     } else {
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();
749            II != IE; ++II)
750         FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
751
752       MF.addFilterTypeInfo(&MBB, FilterList);
753     }
754   }
755 }
756
757 /// \}
758
759 //===----------------------------------------------------------------------===//
760 //  MachineJumpTableInfo implementation
761 //===----------------------------------------------------------------------===//
762
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:
771     return 8;
772   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
773   case MachineJumpTableInfo::EK_LabelDifference32:
774   case MachineJumpTableInfo::EK_Custom32:
775     return 4;
776   case MachineJumpTableInfo::EK_Inline:
777     return 0;
778   }
779   llvm_unreachable("Unknown jump table encoding!");
780 }
781
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
786   // alignment.
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:
797     return 1;
798   }
799   llvm_unreachable("Unknown jump table encoding!");
800 }
801
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;
808 }
809
810 /// If Old is the target of any jump tables, update the jump tables to branch
811 /// to New instead.
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);
818   return MadeChange;
819 }
820
821 /// If Old is a target of the jump tables, update the jump table to branch to
822 /// New instead.
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) {
831       JTE.MBBs[j] = New;
832       MadeChange = true;
833     }
834   return MadeChange;
835 }
836
837 void MachineJumpTableInfo::print(raw_ostream &OS) const {
838   if (JumpTables.empty()) return;
839
840   OS << "Jump Tables:\n";
841
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();
846   }
847
848   OS << '\n';
849 }
850
851 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
852 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
853 #endif
854
855
856 //===----------------------------------------------------------------------===//
857 //  MachineConstantPool implementation
858 //===----------------------------------------------------------------------===//
859
860 void MachineConstantPoolValue::anchor() { }
861
862 Type *MachineConstantPoolEntry::getType() const {
863   if (isMachineConstantPoolEntry())
864     return Val.MachineCPVal->getType();
865   return Val.ConstVal->getType();
866 }
867
868 bool MachineConstantPoolEntry::needsRelocation() const {
869   if (isMachineConstantPoolEntry())
870     return true;
871   return Val.ConstVal->needsRelocation();
872 }
873
874 SectionKind
875 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
876   if (needsRelocation())
877     return SectionKind::getReadOnlyWithRel();
878   switch (DL->getTypeAllocSize(getType())) {
879   case 4:
880     return SectionKind::getMergeableConst4();
881   case 8:
882     return SectionKind::getMergeableConst8();
883   case 16:
884     return SectionKind::getMergeableConst16();
885   case 32:
886     return SectionKind::getMergeableConst32();
887   default:
888     return SectionKind::getReadOnly();
889   }
890 }
891
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;
900     }
901   for (DenseSet<MachineConstantPoolValue*>::iterator I =
902        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
903        I != E; ++I) {
904     if (Deleted.count(*I) == 0)
905       delete *I;
906   }
907 }
908
909 /// Test whether the given two constants can be allocated the same constant pool
910 /// entry.
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;
915
916   // If they have the same type but weren't the same constant, quickly
917   // reject them.
918   if (A->getType() == B->getType()) return false;
919
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()))
923     return false;
924
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)
928     return false;
929
930   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
931
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
935   // DataLayout.
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),
941                                 IntTy, DL);
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),
947                                 IntTy, DL);
948
949   return A == B;
950 }
951
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;
958
959   // Check to see if we already have this constant.
960   //
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;
967       return i;
968     }
969
970   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
971   return Constants.size()-1;
972 }
973
974 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
975                                                    unsigned Alignment) {
976   assert(Alignment && "Alignment must be specified!");
977   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
978
979   // Check to see if we already have this constant.
980   //
981   // FIXME, this could be made much more efficient for large constant pools.
982   int Idx = V->getExistingMachineCPValue(this, Alignment);
983   if (Idx != -1) {
984     MachineCPVsSharingEntries.insert(V);
985     return (unsigned)Idx;
986   }
987
988   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
989   return Constants.size()-1;
990 }
991
992 void MachineConstantPool::print(raw_ostream &OS) const {
993   if (Constants.empty()) return;
994
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);
1000     else
1001       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1002     OS << ", align=" << Constants[i].getAlignment();
1003     OS << "\n";
1004   }
1005 }
1006
1007 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1008 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1009 #endif