1 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
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 // This file implements the AsmPrinter class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AsmPrinterHandler.h"
15 #include "CodeViewDebug.h"
16 #include "DwarfDebug.h"
17 #include "DwarfException.h"
18 #include "WinException.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/ADT/Twine.h"
30 #include "llvm/Analysis/ConstantFolding.h"
31 #include "llvm/Analysis/ObjectUtils.h"
32 #include "llvm/CodeGen/Analysis.h"
33 #include "llvm/CodeGen/AsmPrinter.h"
34 #include "llvm/CodeGen/GCMetadata.h"
35 #include "llvm/CodeGen/GCMetadataPrinter.h"
36 #include "llvm/CodeGen/GCStrategy.h"
37 #include "llvm/CodeGen/MachineBasicBlock.h"
38 #include "llvm/CodeGen/MachineConstantPool.h"
39 #include "llvm/CodeGen/MachineFrameInfo.h"
40 #include "llvm/CodeGen/MachineFunction.h"
41 #include "llvm/CodeGen/MachineFunctionPass.h"
42 #include "llvm/CodeGen/MachineInstr.h"
43 #include "llvm/CodeGen/MachineInstrBundle.h"
44 #include "llvm/CodeGen/MachineJumpTableInfo.h"
45 #include "llvm/CodeGen/MachineLoopInfo.h"
46 #include "llvm/CodeGen/MachineMemOperand.h"
47 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
48 #include "llvm/CodeGen/MachineOperand.h"
49 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
50 #include "llvm/IR/BasicBlock.h"
51 #include "llvm/IR/Constant.h"
52 #include "llvm/IR/Constants.h"
53 #include "llvm/IR/DataLayout.h"
54 #include "llvm/IR/DebugInfoMetadata.h"
55 #include "llvm/IR/DerivedTypes.h"
56 #include "llvm/IR/Function.h"
57 #include "llvm/IR/GlobalAlias.h"
58 #include "llvm/IR/GlobalIFunc.h"
59 #include "llvm/IR/GlobalIndirectSymbol.h"
60 #include "llvm/IR/GlobalObject.h"
61 #include "llvm/IR/GlobalValue.h"
62 #include "llvm/IR/GlobalVariable.h"
63 #include "llvm/IR/Mangler.h"
64 #include "llvm/IR/Metadata.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/IR/Operator.h"
67 #include "llvm/IR/Value.h"
68 #include "llvm/MC/MCAsmInfo.h"
69 #include "llvm/MC/MCContext.h"
70 #include "llvm/MC/MCDirectives.h"
71 #include "llvm/MC/MCExpr.h"
72 #include "llvm/MC/MCInst.h"
73 #include "llvm/MC/MCSection.h"
74 #include "llvm/MC/MCSectionELF.h"
75 #include "llvm/MC/MCSectionMachO.h"
76 #include "llvm/MC/MCStreamer.h"
77 #include "llvm/MC/MCSubtargetInfo.h"
78 #include "llvm/MC/MCSymbol.h"
79 #include "llvm/MC/MCTargetOptions.h"
80 #include "llvm/MC/MCValue.h"
81 #include "llvm/MC/SectionKind.h"
82 #include "llvm/Pass.h"
83 #include "llvm/Support/Casting.h"
84 #include "llvm/Support/Compiler.h"
85 #include "llvm/Support/Dwarf.h"
86 #include "llvm/Support/ELF.h"
87 #include "llvm/Support/ErrorHandling.h"
88 #include "llvm/Support/Format.h"
89 #include "llvm/Support/MathExtras.h"
90 #include "llvm/Support/raw_ostream.h"
91 #include "llvm/Support/TargetRegistry.h"
92 #include "llvm/Support/Timer.h"
93 #include "llvm/Target/TargetFrameLowering.h"
94 #include "llvm/Target/TargetInstrInfo.h"
95 #include "llvm/Target/TargetLowering.h"
96 #include "llvm/Target/TargetLoweringObjectFile.h"
97 #include "llvm/Target/TargetMachine.h"
98 #include "llvm/Target/TargetRegisterInfo.h"
99 #include "llvm/Target/TargetSubtargetInfo.h"
100 #include <algorithm>
101 #include <cassert>
102 #include <cinttypes>
103 #include <cstdint>
104 #include <limits>
105 #include <memory>
106 #include <string>
107 #include <utility>
108 #include <vector>
109 
110 using namespace llvm;
111 
112 #define DEBUG_TYPE "asm-printer"
113 
114 static const char *const DWARFGroupName = "dwarf";
115 static const char *const DWARFGroupDescription = "DWARF Emission";
116 static const char *const DbgTimerName = "emit";
117 static const char *const DbgTimerDescription = "Debug Info Emission";
118 static const char *const EHTimerName = "write_exception";
119 static const char *const EHTimerDescription = "DWARF Exception Writer";
120 static const char *const CodeViewLineTablesGroupName = "linetables";
121 static const char *const CodeViewLineTablesGroupDescription =
122   "CodeView Line Tables";
123 
124 STATISTIC(EmittedInsts, "Number of machine instrs printed");
125 
126 char AsmPrinter::ID = 0;
127 
128 typedef DenseMap<GCStrategy*, std::unique_ptr<GCMetadataPrinter>> gcp_map_type;
129 static gcp_map_type &getGCMap(void *&P) {
130   if (!P)
131     P = new gcp_map_type();
132   return *(gcp_map_type*)P;
133 }
134 
135 /// getGVAlignmentLog2 - Return the alignment to use for the specified global
136 /// value in log2 form.  This rounds up to the preferred alignment if possible
137 /// and legal.
138 static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &DL,
139                                    unsigned InBits = 0) {
140   unsigned NumBits = 0;
141   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
142     NumBits = DL.getPreferredAlignmentLog(GVar);
143 
144   // If InBits is specified, round it to it.
145   if (InBits > NumBits)
146     NumBits = InBits;
147 
148   // If the GV has a specified alignment, take it into account.
149   if (GV->getAlignment() == 0)
150     return NumBits;
151 
152   unsigned GVAlign = Log2_32(GV->getAlignment());
153 
154   // If the GVAlign is larger than NumBits, or if we are required to obey
155   // NumBits because the GV has an assigned section, obey it.
156   if (GVAlign > NumBits || GV->hasSection())
157     NumBits = GVAlign;
158   return NumBits;
159 }
160 
161 AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
162     : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
163       OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)) {
164   VerboseAsm = OutStreamer->isVerboseAsm();
165 }
166 
167 AsmPrinter::~AsmPrinter() {
168   assert(!DD && Handlers.empty() && "Debug/EH info didn't get finalized");
169 
170   if (GCMetadataPrinters) {
171     gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
172 
173     delete &GCMap;
174     GCMetadataPrinters = nullptr;
175   }
176 }
177 
178 bool AsmPrinter::isPositionIndependent() const {
179   return TM.isPositionIndependent();
180 }
181 
182 /// getFunctionNumber - Return a unique ID for the current function.
183 ///
184 unsigned AsmPrinter::getFunctionNumber() const {
185   return MF->getFunctionNumber();
186 }
187 
188 const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
189   return *TM.getObjFileLowering();
190 }
191 
192 const DataLayout &AsmPrinter::getDataLayout() const {
193   return MMI->getModule()->getDataLayout();
194 }
195 
196 // Do not use the cached DataLayout because some client use it without a Module
197 // (llvm-dsymutil, llvm-dwarfdump).
198 unsigned AsmPrinter::getPointerSize() const { return TM.getPointerSize(); }
199 
200 const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
201   assert(MF && "getSubtargetInfo requires a valid MachineFunction!");
202   return MF->getSubtarget<MCSubtargetInfo>();
203 }
204 
205 void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
206   S.EmitInstruction(Inst, getSubtargetInfo());
207 }
208 
209 /// getCurrentSection() - Return the current section we are emitting to.
210 const MCSection *AsmPrinter::getCurrentSection() const {
211   return OutStreamer->getCurrentSectionOnly();
212 }
213 
214 void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
215   AU.setPreservesAll();
216   MachineFunctionPass::getAnalysisUsage(AU);
217   AU.addRequired<MachineModuleInfo>();
218   AU.addRequired<MachineOptimizationRemarkEmitterPass>();
219   AU.addRequired<GCModuleInfo>();
220   if (isVerbose())
221     AU.addRequired<MachineLoopInfo>();
222 }
223 
224 bool AsmPrinter::doInitialization(Module &M) {
225   MMI = getAnalysisIfAvailable<MachineModuleInfo>();
226 
227   // Initialize TargetLoweringObjectFile.
228   const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
229     .Initialize(OutContext, TM);
230 
231   OutStreamer->InitSections(false);
232 
233   // Emit the version-min deplyment target directive if needed.
234   //
235   // FIXME: If we end up with a collection of these sorts of Darwin-specific
236   // or ELF-specific things, it may make sense to have a platform helper class
237   // that will work with the target helper class. For now keep it here, as the
238   // alternative is duplicated code in each of the target asm printers that
239   // use the directive, where it would need the same conditionalization
240   // anyway.
241   const Triple &TT = TM.getTargetTriple();
242   // If there is a version specified, Major will be non-zero.
243   if (TT.isOSDarwin() && TT.getOSMajorVersion() != 0) {
244     unsigned Major, Minor, Update;
245     MCVersionMinType VersionType;
246     if (TT.isWatchOS()) {
247       VersionType = MCVM_WatchOSVersionMin;
248       TT.getWatchOSVersion(Major, Minor, Update);
249     } else if (TT.isTvOS()) {
250       VersionType = MCVM_TvOSVersionMin;
251       TT.getiOSVersion(Major, Minor, Update);
252     } else if (TT.isMacOSX()) {
253       VersionType = MCVM_OSXVersionMin;
254       if (!TT.getMacOSXVersion(Major, Minor, Update))
255         Major = 0;
256     } else {
257       VersionType = MCVM_IOSVersionMin;
258       TT.getiOSVersion(Major, Minor, Update);
259     }
260     if (Major != 0)
261       OutStreamer->EmitVersionMin(VersionType, Major, Minor, Update);
262   }
263 
264   // Allow the target to emit any magic that it wants at the start of the file.
265   EmitStartOfAsmFile(M);
266 
267   // Very minimal debug info. It is ignored if we emit actual debug info. If we
268   // don't, this at least helps the user find where a global came from.
269   if (MAI->hasSingleParameterDotFile()) {
270     // .file "foo.c"
271     OutStreamer->EmitFileDirective(M.getSourceFileName());
272   }
273 
274   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
275   assert(MI && "AsmPrinter didn't require GCModuleInfo?");
276   for (auto &I : *MI)
277     if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
278       MP->beginAssembly(M, *MI, *this);
279 
280   // Emit module-level inline asm if it exists.
281   if (!M.getModuleInlineAsm().empty()) {
282     // We're at the module level. Construct MCSubtarget from the default CPU
283     // and target triple.
284     std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
285         TM.getTargetTriple().str(), TM.getTargetCPU(),
286         TM.getTargetFeatureString()));
287     OutStreamer->AddComment("Start of file scope inline assembly");
288     OutStreamer->AddBlankLine();
289     EmitInlineAsm(M.getModuleInlineAsm()+"\n",
290                   OutContext.getSubtargetCopy(*STI), TM.Options.MCOptions);
291     OutStreamer->AddComment("End of file scope inline assembly");
292     OutStreamer->AddBlankLine();
293   }
294 
295   if (MAI->doesSupportDebugInformation()) {
296     bool EmitCodeView = MMI->getModule()->getCodeViewFlag();
297     if (EmitCodeView && (TM.getTargetTriple().isKnownWindowsMSVCEnvironment() ||
298                          TM.getTargetTriple().isWindowsItaniumEnvironment())) {
299       Handlers.push_back(HandlerInfo(new CodeViewDebug(this),
300                                      DbgTimerName, DbgTimerDescription,
301                                      CodeViewLineTablesGroupName,
302                                      CodeViewLineTablesGroupDescription));
303     }
304     if (!EmitCodeView || MMI->getModule()->getDwarfVersion()) {
305       DD = new DwarfDebug(this, &M);
306       DD->beginModule();
307       Handlers.push_back(HandlerInfo(DD, DbgTimerName, DbgTimerDescription,
308                                      DWARFGroupName, DWARFGroupDescription));
309     }
310   }
311 
312   switch (MAI->getExceptionHandlingType()) {
313   case ExceptionHandling::SjLj:
314   case ExceptionHandling::DwarfCFI:
315   case ExceptionHandling::ARM:
316     isCFIMoveForDebugging = true;
317     if (MAI->getExceptionHandlingType() != ExceptionHandling::DwarfCFI)
318       break;
319     for (auto &F: M.getFunctionList()) {
320       // If the module contains any function with unwind data,
321       // .eh_frame has to be emitted.
322       // Ignore functions that won't get emitted.
323       if (!F.isDeclarationForLinker() && F.needsUnwindTableEntry()) {
324         isCFIMoveForDebugging = false;
325         break;
326       }
327     }
328     break;
329   default:
330     isCFIMoveForDebugging = false;
331     break;
332   }
333 
334   EHStreamer *ES = nullptr;
335   switch (MAI->getExceptionHandlingType()) {
336   case ExceptionHandling::None:
337     break;
338   case ExceptionHandling::SjLj:
339   case ExceptionHandling::DwarfCFI:
340     ES = new DwarfCFIException(this);
341     break;
342   case ExceptionHandling::ARM:
343     ES = new ARMException(this);
344     break;
345   case ExceptionHandling::WinEH:
346     switch (MAI->getWinEHEncodingType()) {
347     default: llvm_unreachable("unsupported unwinding information encoding");
348     case WinEH::EncodingType::Invalid:
349       break;
350     case WinEH::EncodingType::X86:
351     case WinEH::EncodingType::Itanium:
352       ES = new WinException(this);
353       break;
354     }
355     break;
356   }
357   if (ES)
358     Handlers.push_back(HandlerInfo(ES, EHTimerName, EHTimerDescription,
359                                    DWARFGroupName, DWARFGroupDescription));
360   return false;
361 }
362 
363 static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) {
364   if (!MAI.hasWeakDefCanBeHiddenDirective())
365     return false;
366 
367   return canBeOmittedFromSymbolTable(GV);
368 }
369 
370 void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
371   GlobalValue::LinkageTypes Linkage = GV->getLinkage();
372   switch (Linkage) {
373   case GlobalValue::CommonLinkage:
374   case GlobalValue::LinkOnceAnyLinkage:
375   case GlobalValue::LinkOnceODRLinkage:
376   case GlobalValue::WeakAnyLinkage:
377   case GlobalValue::WeakODRLinkage:
378     if (MAI->hasWeakDefDirective()) {
379       // .globl _foo
380       OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
381 
382       if (!canBeHidden(GV, *MAI))
383         // .weak_definition _foo
384         OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefinition);
385       else
386         OutStreamer->EmitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
387     } else if (MAI->hasLinkOnceDirective()) {
388       // .globl _foo
389       OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
390       //NOTE: linkonce is handled by the section the symbol was assigned to.
391     } else {
392       // .weak _foo
393       OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
394     }
395     return;
396   case GlobalValue::ExternalLinkage:
397     // If external, declare as a global symbol: .globl _foo
398     OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
399     return;
400   case GlobalValue::PrivateLinkage:
401   case GlobalValue::InternalLinkage:
402     return;
403   case GlobalValue::AppendingLinkage:
404   case GlobalValue::AvailableExternallyLinkage:
405   case GlobalValue::ExternalWeakLinkage:
406     llvm_unreachable("Should never emit this");
407   }
408   llvm_unreachable("Unknown linkage type!");
409 }
410 
411 void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
412                                    const GlobalValue *GV) const {
413   TM.getNameWithPrefix(Name, GV, getObjFileLowering().getMangler());
414 }
415 
416 MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
417   return TM.getSymbol(GV);
418 }
419 
420 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
421 void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
422   bool IsEmuTLSVar = TM.Options.EmulatedTLS && GV->isThreadLocal();
423   assert(!(IsEmuTLSVar && GV->hasCommonLinkage()) &&
424          "No emulated TLS variables in the common section");
425 
426   // Never emit TLS variable xyz in emulated TLS model.
427   // The initialization value is in __emutls_t.xyz instead of xyz.
428   if (IsEmuTLSVar)
429     return;
430 
431   if (GV->hasInitializer()) {
432     // Check to see if this is a special global used by LLVM, if so, emit it.
433     if (EmitSpecialLLVMGlobal(GV))
434       return;
435 
436     // Skip the emission of global equivalents. The symbol can be emitted later
437     // on by emitGlobalGOTEquivs in case it turns out to be needed.
438     if (GlobalGOTEquivs.count(getSymbol(GV)))
439       return;
440 
441     if (isVerbose()) {
442       // When printing the control variable __emutls_v.*,
443       // we don't need to print the original TLS variable name.
444       GV->printAsOperand(OutStreamer->GetCommentOS(),
445                      /*PrintType=*/false, GV->getParent());
446       OutStreamer->GetCommentOS() << '\n';
447     }
448   }
449 
450   MCSymbol *GVSym = getSymbol(GV);
451   MCSymbol *EmittedSym = GVSym;
452 
453   // getOrCreateEmuTLSControlSym only creates the symbol with name and default
454   // attributes.
455   // GV's or GVSym's attributes will be used for the EmittedSym.
456   EmitVisibility(EmittedSym, GV->getVisibility(), !GV->isDeclaration());
457 
458   if (!GV->hasInitializer())   // External globals require no extra code.
459     return;
460 
461   GVSym->redefineIfPossible();
462   if (GVSym->isDefined() || GVSym->isVariable())
463     report_fatal_error("symbol '" + Twine(GVSym->getName()) +
464                        "' is already defined");
465 
466   if (MAI->hasDotTypeDotSizeDirective())
467     OutStreamer->EmitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject);
468 
469   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
470 
471   const DataLayout &DL = GV->getParent()->getDataLayout();
472   uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
473 
474   // If the alignment is specified, we *must* obey it.  Overaligning a global
475   // with a specified alignment is a prompt way to break globals emitted to
476   // sections and expected to be contiguous (e.g. ObjC metadata).
477   unsigned AlignLog = getGVAlignmentLog2(GV, DL);
478 
479   for (const HandlerInfo &HI : Handlers) {
480     NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
481                        HI.TimerGroupName, HI.TimerGroupDescription,
482                        TimePassesIsEnabled);
483     HI.Handler->setSymbolSize(GVSym, Size);
484   }
485 
486   // Handle common symbols
487   if (GVKind.isCommon()) {
488     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
489     unsigned Align = 1 << AlignLog;
490     if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
491       Align = 0;
492 
493     // .comm _foo, 42, 4
494     OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
495     return;
496   }
497 
498   // Determine to which section this global should be emitted.
499   MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM);
500 
501   // If we have a bss global going to a section that supports the
502   // zerofill directive, do so here.
503   if (GVKind.isBSS() && MAI->hasMachoZeroFillDirective() &&
504       TheSection->isVirtualSection()) {
505     if (Size == 0)
506       Size = 1; // zerofill of 0 bytes is undefined.
507     unsigned Align = 1 << AlignLog;
508     EmitLinkage(GV, GVSym);
509     // .zerofill __DATA, __bss, _foo, 400, 5
510     OutStreamer->EmitZerofill(TheSection, GVSym, Size, Align);
511     return;
512   }
513 
514   // If this is a BSS local symbol and we are emitting in the BSS
515   // section use .lcomm/.comm directive.
516   if (GVKind.isBSSLocal() &&
517       getObjFileLowering().getBSSSection() == TheSection) {
518     if (Size == 0)
519       Size = 1; // .comm Foo, 0 is undefined, avoid it.
520     unsigned Align = 1 << AlignLog;
521 
522     // Use .lcomm only if it supports user-specified alignment.
523     // Otherwise, while it would still be correct to use .lcomm in some
524     // cases (e.g. when Align == 1), the external assembler might enfore
525     // some -unknown- default alignment behavior, which could cause
526     // spurious differences between external and integrated assembler.
527     // Prefer to simply fall back to .local / .comm in this case.
528     if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) {
529       // .lcomm _foo, 42
530       OutStreamer->EmitLocalCommonSymbol(GVSym, Size, Align);
531       return;
532     }
533 
534     if (!getObjFileLowering().getCommDirectiveSupportsAlignment())
535       Align = 0;
536 
537     // .local _foo
538     OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Local);
539     // .comm _foo, 42, 4
540     OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
541     return;
542   }
543 
544   // Handle thread local data for mach-o which requires us to output an
545   // additional structure of data and mangle the original symbol so that we
546   // can reference it later.
547   //
548   // TODO: This should become an "emit thread local global" method on TLOF.
549   // All of this macho specific stuff should be sunk down into TLOFMachO and
550   // stuff like "TLSExtraDataSection" should no longer be part of the parent
551   // TLOF class.  This will also make it more obvious that stuff like
552   // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
553   // specific code.
554   if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
555     // Emit the .tbss symbol
556     MCSymbol *MangSym =
557         OutContext.getOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
558 
559     if (GVKind.isThreadBSS()) {
560       TheSection = getObjFileLowering().getTLSBSSSection();
561       OutStreamer->EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog);
562     } else if (GVKind.isThreadData()) {
563       OutStreamer->SwitchSection(TheSection);
564 
565       EmitAlignment(AlignLog, GV);
566       OutStreamer->EmitLabel(MangSym);
567 
568       EmitGlobalConstant(GV->getParent()->getDataLayout(),
569                          GV->getInitializer());
570     }
571 
572     OutStreamer->AddBlankLine();
573 
574     // Emit the variable struct for the runtime.
575     MCSection *TLVSect = getObjFileLowering().getTLSExtraDataSection();
576 
577     OutStreamer->SwitchSection(TLVSect);
578     // Emit the linkage here.
579     EmitLinkage(GV, GVSym);
580     OutStreamer->EmitLabel(GVSym);
581 
582     // Three pointers in size:
583     //   - __tlv_bootstrap - used to make sure support exists
584     //   - spare pointer, used when mapped by the runtime
585     //   - pointer to mangled symbol above with initializer
586     unsigned PtrSize = DL.getPointerTypeSize(GV->getType());
587     OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
588                                 PtrSize);
589     OutStreamer->EmitIntValue(0, PtrSize);
590     OutStreamer->EmitSymbolValue(MangSym, PtrSize);
591 
592     OutStreamer->AddBlankLine();
593     return;
594   }
595 
596   MCSymbol *EmittedInitSym = GVSym;
597 
598   OutStreamer->SwitchSection(TheSection);
599 
600   EmitLinkage(GV, EmittedInitSym);
601   EmitAlignment(AlignLog, GV);
602 
603   OutStreamer->EmitLabel(EmittedInitSym);
604 
605   EmitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
606 
607   if (MAI->hasDotTypeDotSizeDirective())
608     // .size foo, 42
609     OutStreamer->emitELFSize(EmittedInitSym,
610                              MCConstantExpr::create(Size, OutContext));
611 
612   OutStreamer->AddBlankLine();
613 }
614 
615 /// Emit the directive and value for debug thread local expression
616 ///
617 /// \p Value - The value to emit.
618 /// \p Size - The size of the integer (in bytes) to emit.
619 void AsmPrinter::EmitDebugThreadLocal(const MCExpr *Value,
620                                       unsigned Size) const {
621   OutStreamer->EmitValue(Value, Size);
622 }
623 
624 /// EmitFunctionHeader - This method emits the header for the current
625 /// function.
626 void AsmPrinter::EmitFunctionHeader() {
627   // Print out constants referenced by the function
628   EmitConstantPool();
629 
630   // Print the 'header' of function.
631   const Function *F = MF->getFunction();
632 
633   OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(F, TM));
634   EmitVisibility(CurrentFnSym, F->getVisibility());
635 
636   EmitLinkage(F, CurrentFnSym);
637   if (MAI->hasFunctionAlignment())
638     EmitAlignment(MF->getAlignment(), F);
639 
640   if (MAI->hasDotTypeDotSizeDirective())
641     OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
642 
643   if (isVerbose()) {
644     F->printAsOperand(OutStreamer->GetCommentOS(),
645                    /*PrintType=*/false, F->getParent());
646     OutStreamer->GetCommentOS() << '\n';
647   }
648 
649   // Emit the prefix data.
650   if (F->hasPrefixData()) {
651     if (MAI->hasSubsectionsViaSymbols()) {
652       // Preserving prefix data on platforms which use subsections-via-symbols
653       // is a bit tricky. Here we introduce a symbol for the prefix data
654       // and use the .alt_entry attribute to mark the function's real entry point
655       // as an alternative entry point to the prefix-data symbol.
656       MCSymbol *PrefixSym = OutContext.createLinkerPrivateTempSymbol();
657       OutStreamer->EmitLabel(PrefixSym);
658 
659       EmitGlobalConstant(F->getParent()->getDataLayout(), F->getPrefixData());
660 
661       // Emit an .alt_entry directive for the actual function symbol.
662       OutStreamer->EmitSymbolAttribute(CurrentFnSym, MCSA_AltEntry);
663     } else {
664       EmitGlobalConstant(F->getParent()->getDataLayout(), F->getPrefixData());
665     }
666   }
667 
668   // Emit the CurrentFnSym.  This is a virtual function to allow targets to
669   // do their wild and crazy things as required.
670   EmitFunctionEntryLabel();
671 
672   // If the function had address-taken blocks that got deleted, then we have
673   // references to the dangling symbols.  Emit them at the start of the function
674   // so that we don't get references to undefined symbols.
675   std::vector<MCSymbol*> DeadBlockSyms;
676   MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
677   for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
678     OutStreamer->AddComment("Address taken block that was later removed");
679     OutStreamer->EmitLabel(DeadBlockSyms[i]);
680   }
681 
682   if (CurrentFnBegin) {
683     if (MAI->useAssignmentForEHBegin()) {
684       MCSymbol *CurPos = OutContext.createTempSymbol();
685       OutStreamer->EmitLabel(CurPos);
686       OutStreamer->EmitAssignment(CurrentFnBegin,
687                                  MCSymbolRefExpr::create(CurPos, OutContext));
688     } else {
689       OutStreamer->EmitLabel(CurrentFnBegin);
690     }
691   }
692 
693   // Emit pre-function debug and/or EH information.
694   for (const HandlerInfo &HI : Handlers) {
695     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
696                        HI.TimerGroupDescription, TimePassesIsEnabled);
697     HI.Handler->beginFunction(MF);
698   }
699 
700   // Emit the prologue data.
701   if (F->hasPrologueData())
702     EmitGlobalConstant(F->getParent()->getDataLayout(), F->getPrologueData());
703 }
704 
705 /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
706 /// function.  This can be overridden by targets as required to do custom stuff.
707 void AsmPrinter::EmitFunctionEntryLabel() {
708   CurrentFnSym->redefineIfPossible();
709 
710   // The function label could have already been emitted if two symbols end up
711   // conflicting due to asm renaming.  Detect this and emit an error.
712   if (CurrentFnSym->isVariable())
713     report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
714                        "' is a protected alias");
715   if (CurrentFnSym->isDefined())
716     report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
717                        "' label emitted multiple times to assembly file");
718 
719   return OutStreamer->EmitLabel(CurrentFnSym);
720 }
721 
722 /// emitComments - Pretty-print comments for instructions.
723 static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
724   const MachineFunction *MF = MI.getParent()->getParent();
725   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
726 
727   // Check for spills and reloads
728   int FI;
729 
730   const MachineFrameInfo &MFI = MF->getFrameInfo();
731 
732   // We assume a single instruction only has a spill or reload, not
733   // both.
734   const MachineMemOperand *MMO;
735   if (TII->isLoadFromStackSlotPostFE(MI, FI)) {
736     if (MFI.isSpillSlotObjectIndex(FI)) {
737       MMO = *MI.memoperands_begin();
738       CommentOS << MMO->getSize() << "-byte Reload\n";
739     }
740   } else if (TII->hasLoadFromStackSlot(MI, MMO, FI)) {
741     if (MFI.isSpillSlotObjectIndex(FI))
742       CommentOS << MMO->getSize() << "-byte Folded Reload\n";
743   } else if (TII->isStoreToStackSlotPostFE(MI, FI)) {
744     if (MFI.isSpillSlotObjectIndex(FI)) {
745       MMO = *MI.memoperands_begin();
746       CommentOS << MMO->getSize() << "-byte Spill\n";
747     }
748   } else if (TII->hasStoreToStackSlot(MI, MMO, FI)) {
749     if (MFI.isSpillSlotObjectIndex(FI))
750       CommentOS << MMO->getSize() << "-byte Folded Spill\n";
751   }
752 
753   // Check for spill-induced copies
754   if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
755     CommentOS << " Reload Reuse\n";
756 }
757 
758 /// emitImplicitDef - This method emits the specified machine instruction
759 /// that is an implicit def.
760 void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
761   unsigned RegNo = MI->getOperand(0).getReg();
762 
763   SmallString<128> Str;
764   raw_svector_ostream OS(Str);
765   OS << "implicit-def: "
766      << PrintReg(RegNo, MF->getSubtarget().getRegisterInfo());
767 
768   OutStreamer->AddComment(OS.str());
769   OutStreamer->AddBlankLine();
770 }
771 
772 static void emitKill(const MachineInstr *MI, AsmPrinter &AP) {
773   std::string Str;
774   raw_string_ostream OS(Str);
775   OS << "kill:";
776   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
777     const MachineOperand &Op = MI->getOperand(i);
778     assert(Op.isReg() && "KILL instruction must have only register operands");
779     OS << ' '
780        << PrintReg(Op.getReg(),
781                    AP.MF->getSubtarget().getRegisterInfo())
782        << (Op.isDef() ? "<def>" : "<kill>");
783   }
784   AP.OutStreamer->AddComment(OS.str());
785   AP.OutStreamer->AddBlankLine();
786 }
787 
788 /// emitDebugValueComment - This method handles the target-independent form
789 /// of DBG_VALUE, returning true if it was able to do so.  A false return
790 /// means the target will need to handle MI in EmitInstruction.
791 static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
792   // This code handles only the 4-operand target-independent form.
793   if (MI->getNumOperands() != 4)
794     return false;
795 
796   SmallString<128> Str;
797   raw_svector_ostream OS(Str);
798   OS << "DEBUG_VALUE: ";
799 
800   const DILocalVariable *V = MI->getDebugVariable();
801   if (auto *SP = dyn_cast<DISubprogram>(V->getScope())) {
802     StringRef Name = SP->getDisplayName();
803     if (!Name.empty())
804       OS << Name << ":";
805   }
806   OS << V->getName();
807 
808   const DIExpression *Expr = MI->getDebugExpression();
809   auto Fragment = Expr->getFragmentInfo();
810   if (Fragment)
811     OS << " [fragment offset=" << Fragment->OffsetInBits
812        << " size=" << Fragment->SizeInBits << "]";
813   OS << " <- ";
814 
815   // The second operand is only an offset if it's an immediate.
816   bool Deref = MI->getOperand(0).isReg() && MI->getOperand(1).isImm();
817   int64_t Offset = Deref ? MI->getOperand(1).getImm() : 0;
818 
819   for (unsigned i = 0; i < Expr->getNumElements(); ++i) {
820     uint64_t Op = Expr->getElement(i);
821     if (Op == dwarf::DW_OP_LLVM_fragment) {
822       // There can't be any operands after this in a valid expression
823       break;
824     } else if (Deref) {
825       // We currently don't support extra Offsets or derefs after the first
826       // one. Bail out early instead of emitting an incorrect comment
827       OS << " [complex expression]";
828       AP.OutStreamer->emitRawComment(OS.str());
829       return true;
830     } else if (Op == dwarf::DW_OP_deref) {
831       Deref = true;
832       continue;
833     }
834 
835     uint64_t ExtraOffset = Expr->getElement(i++);
836     if (Op == dwarf::DW_OP_plus)
837       Offset += ExtraOffset;
838     else {
839       assert(Op == dwarf::DW_OP_minus);
840       Offset -= ExtraOffset;
841     }
842   }
843 
844   // Register or immediate value. Register 0 means undef.
845   if (MI->getOperand(0).isFPImm()) {
846     APFloat APF = APFloat(MI->getOperand(0).getFPImm()->getValueAPF());
847     if (MI->getOperand(0).getFPImm()->getType()->isFloatTy()) {
848       OS << (double)APF.convertToFloat();
849     } else if (MI->getOperand(0).getFPImm()->getType()->isDoubleTy()) {
850       OS << APF.convertToDouble();
851     } else {
852       // There is no good way to print long double.  Convert a copy to
853       // double.  Ah well, it's only a comment.
854       bool ignored;
855       APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
856                   &ignored);
857       OS << "(long double) " << APF.convertToDouble();
858     }
859   } else if (MI->getOperand(0).isImm()) {
860     OS << MI->getOperand(0).getImm();
861   } else if (MI->getOperand(0).isCImm()) {
862     MI->getOperand(0).getCImm()->getValue().print(OS, false /*isSigned*/);
863   } else {
864     unsigned Reg;
865     if (MI->getOperand(0).isReg()) {
866       Reg = MI->getOperand(0).getReg();
867     } else {
868       assert(MI->getOperand(0).isFI() && "Unknown operand type");
869       const TargetFrameLowering *TFI = AP.MF->getSubtarget().getFrameLowering();
870       Offset += TFI->getFrameIndexReference(*AP.MF,
871                                             MI->getOperand(0).getIndex(), Reg);
872       Deref = true;
873     }
874     if (Reg == 0) {
875       // Suppress offset, it is not meaningful here.
876       OS << "undef";
877       // NOTE: Want this comment at start of line, don't emit with AddComment.
878       AP.OutStreamer->emitRawComment(OS.str());
879       return true;
880     }
881     if (Deref)
882       OS << '[';
883     OS << PrintReg(Reg, AP.MF->getSubtarget().getRegisterInfo());
884   }
885 
886   if (Deref)
887     OS << '+' << Offset << ']';
888 
889   // NOTE: Want this comment at start of line, don't emit with AddComment.
890   AP.OutStreamer->emitRawComment(OS.str());
891   return true;
892 }
893 
894 AsmPrinter::CFIMoveType AsmPrinter::needsCFIMoves() {
895   if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI &&
896       MF->getFunction()->needsUnwindTableEntry())
897     return CFI_M_EH;
898 
899   if (MMI->hasDebugInfo())
900     return CFI_M_Debug;
901 
902   return CFI_M_None;
903 }
904 
905 bool AsmPrinter::needsSEHMoves() {
906   return MAI->usesWindowsCFI() && MF->getFunction()->needsUnwindTableEntry();
907 }
908 
909 void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
910   ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType();
911   if (ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
912       ExceptionHandlingType != ExceptionHandling::ARM)
913     return;
914 
915   if (needsCFIMoves() == CFI_M_None)
916     return;
917 
918   const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
919   unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
920   const MCCFIInstruction &CFI = Instrs[CFIIndex];
921   emitCFIInstruction(CFI);
922 }
923 
924 void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) {
925   // The operands are the MCSymbol and the frame offset of the allocation.
926   MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol();
927   int FrameOffset = MI.getOperand(1).getImm();
928 
929   // Emit a symbol assignment.
930   OutStreamer->EmitAssignment(FrameAllocSym,
931                              MCConstantExpr::create(FrameOffset, OutContext));
932 }
933 
934 /// EmitFunctionBody - This method emits the body and trailer for a
935 /// function.
936 void AsmPrinter::EmitFunctionBody() {
937   EmitFunctionHeader();
938 
939   // Emit target-specific gunk before the function body.
940   EmitFunctionBodyStart();
941 
942   bool ShouldPrintDebugScopes = MMI->hasDebugInfo();
943 
944   // Print out code for the function.
945   bool HasAnyRealCode = false;
946   int NumInstsInFunction = 0;
947   for (auto &MBB : *MF) {
948     // Print a label for the basic block.
949     EmitBasicBlockStart(MBB);
950     for (auto &MI : MBB) {
951 
952       // Print the assembly for the instruction.
953       if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
954           !MI.isDebugValue()) {
955         HasAnyRealCode = true;
956         ++NumInstsInFunction;
957       }
958 
959       if (ShouldPrintDebugScopes) {
960         for (const HandlerInfo &HI : Handlers) {
961           NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
962                              HI.TimerGroupName, HI.TimerGroupDescription,
963                              TimePassesIsEnabled);
964           HI.Handler->beginInstruction(&MI);
965         }
966       }
967 
968       if (isVerbose())
969         emitComments(MI, OutStreamer->GetCommentOS());
970 
971       switch (MI.getOpcode()) {
972       case TargetOpcode::CFI_INSTRUCTION:
973         emitCFIInstruction(MI);
974         break;
975 
976       case TargetOpcode::LOCAL_ESCAPE:
977         emitFrameAlloc(MI);
978         break;
979 
980       case TargetOpcode::EH_LABEL:
981       case TargetOpcode::GC_LABEL:
982         OutStreamer->EmitLabel(MI.getOperand(0).getMCSymbol());
983         break;
984       case TargetOpcode::INLINEASM:
985         EmitInlineAsm(&MI);
986         break;
987       case TargetOpcode::DBG_VALUE:
988         if (isVerbose()) {
989           if (!emitDebugValueComment(&MI, *this))
990             EmitInstruction(&MI);
991         }
992         break;
993       case TargetOpcode::IMPLICIT_DEF:
994         if (isVerbose()) emitImplicitDef(&MI);
995         break;
996       case TargetOpcode::KILL:
997         if (isVerbose()) emitKill(&MI, *this);
998         break;
999       default:
1000         EmitInstruction(&MI);
1001         break;
1002       }
1003 
1004       if (ShouldPrintDebugScopes) {
1005         for (const HandlerInfo &HI : Handlers) {
1006           NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
1007                              HI.TimerGroupName, HI.TimerGroupDescription,
1008                              TimePassesIsEnabled);
1009           HI.Handler->endInstruction();
1010         }
1011       }
1012     }
1013 
1014     EmitBasicBlockEnd(MBB);
1015   }
1016 
1017   EmittedInsts += NumInstsInFunction;
1018   MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "InstructionCount",
1019                                       MF->getFunction()->getSubprogram(),
1020                                       &MF->front());
1021   R << ore::NV("NumInstructions", NumInstsInFunction)
1022     << " instructions in function";
1023   ORE->emit(R);
1024 
1025   // If the function is empty and the object file uses .subsections_via_symbols,
1026   // then we need to emit *something* to the function body to prevent the
1027   // labels from collapsing together.  Just emit a noop.
1028   if ((MAI->hasSubsectionsViaSymbols() && !HasAnyRealCode)) {
1029     MCInst Noop;
1030     MF->getSubtarget().getInstrInfo()->getNoopForMachoTarget(Noop);
1031     OutStreamer->AddComment("avoids zero-length function");
1032 
1033     // Targets can opt-out of emitting the noop here by leaving the opcode
1034     // unspecified.
1035     if (Noop.getOpcode())
1036       OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
1037   }
1038 
1039   const Function *F = MF->getFunction();
1040   for (const auto &BB : *F) {
1041     if (!BB.hasAddressTaken())
1042       continue;
1043     MCSymbol *Sym = GetBlockAddressSymbol(&BB);
1044     if (Sym->isDefined())
1045       continue;
1046     OutStreamer->AddComment("Address of block that was removed by CodeGen");
1047     OutStreamer->EmitLabel(Sym);
1048   }
1049 
1050   // Emit target-specific gunk after the function body.
1051   EmitFunctionBodyEnd();
1052 
1053   if (!MF->getLandingPads().empty() || MMI->hasDebugInfo() ||
1054       MF->hasEHFunclets() || MAI->hasDotTypeDotSizeDirective()) {
1055     // Create a symbol for the end of function.
1056     CurrentFnEnd = createTempSymbol("func_end");
1057     OutStreamer->EmitLabel(CurrentFnEnd);
1058   }
1059 
1060   // If the target wants a .size directive for the size of the function, emit
1061   // it.
1062   if (MAI->hasDotTypeDotSizeDirective()) {
1063     // We can get the size as difference between the function label and the
1064     // temp label.
1065     const MCExpr *SizeExp = MCBinaryExpr::createSub(
1066         MCSymbolRefExpr::create(CurrentFnEnd, OutContext),
1067         MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext);
1068     OutStreamer->emitELFSize(CurrentFnSym, SizeExp);
1069   }
1070 
1071   for (const HandlerInfo &HI : Handlers) {
1072     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1073                        HI.TimerGroupDescription, TimePassesIsEnabled);
1074     HI.Handler->markFunctionEnd();
1075   }
1076 
1077   // Print out jump tables referenced by the function.
1078   EmitJumpTableInfo();
1079 
1080   // Emit post-function debug and/or EH information.
1081   for (const HandlerInfo &HI : Handlers) {
1082     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1083                        HI.TimerGroupDescription, TimePassesIsEnabled);
1084     HI.Handler->endFunction(MF);
1085   }
1086 
1087   OutStreamer->AddBlankLine();
1088 }
1089 
1090 /// \brief Compute the number of Global Variables that uses a Constant.
1091 static unsigned getNumGlobalVariableUses(const Constant *C) {
1092   if (!C)
1093     return 0;
1094 
1095   if (isa<GlobalVariable>(C))
1096     return 1;
1097 
1098   unsigned NumUses = 0;
1099   for (auto *CU : C->users())
1100     NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU));
1101 
1102   return NumUses;
1103 }
1104 
1105 /// \brief Only consider global GOT equivalents if at least one user is a
1106 /// cstexpr inside an initializer of another global variables. Also, don't
1107 /// handle cstexpr inside instructions. During global variable emission,
1108 /// candidates are skipped and are emitted later in case at least one cstexpr
1109 /// isn't replaced by a PC relative GOT entry access.
1110 static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
1111                                      unsigned &NumGOTEquivUsers) {
1112   // Global GOT equivalents are unnamed private globals with a constant
1113   // pointer initializer to another global symbol. They must point to a
1114   // GlobalVariable or Function, i.e., as GlobalValue.
1115   if (!GV->hasGlobalUnnamedAddr() || !GV->hasInitializer() ||
1116       !GV->isConstant() || !GV->isDiscardableIfUnused() ||
1117       !dyn_cast<GlobalValue>(GV->getOperand(0)))
1118     return false;
1119 
1120   // To be a got equivalent, at least one of its users need to be a constant
1121   // expression used by another global variable.
1122   for (auto *U : GV->users())
1123     NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U));
1124 
1125   return NumGOTEquivUsers > 0;
1126 }
1127 
1128 /// \brief Unnamed constant global variables solely contaning a pointer to
1129 /// another globals variable is equivalent to a GOT table entry; it contains the
1130 /// the address of another symbol. Optimize it and replace accesses to these
1131 /// "GOT equivalents" by using the GOT entry for the final global instead.
1132 /// Compute GOT equivalent candidates among all global variables to avoid
1133 /// emitting them if possible later on, after it use is replaced by a GOT entry
1134 /// access.
1135 void AsmPrinter::computeGlobalGOTEquivs(Module &M) {
1136   if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1137     return;
1138 
1139   for (const auto &G : M.globals()) {
1140     unsigned NumGOTEquivUsers = 0;
1141     if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
1142       continue;
1143 
1144     const MCSymbol *GOTEquivSym = getSymbol(&G);
1145     GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
1146   }
1147 }
1148 
1149 /// \brief Constant expressions using GOT equivalent globals may not be eligible
1150 /// for PC relative GOT entry conversion, in such cases we need to emit such
1151 /// globals we previously omitted in EmitGlobalVariable.
1152 void AsmPrinter::emitGlobalGOTEquivs() {
1153   if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1154     return;
1155 
1156   SmallVector<const GlobalVariable *, 8> FailedCandidates;
1157   for (auto &I : GlobalGOTEquivs) {
1158     const GlobalVariable *GV = I.second.first;
1159     unsigned Cnt = I.second.second;
1160     if (Cnt)
1161       FailedCandidates.push_back(GV);
1162   }
1163   GlobalGOTEquivs.clear();
1164 
1165   for (auto *GV : FailedCandidates)
1166     EmitGlobalVariable(GV);
1167 }
1168 
1169 void AsmPrinter::emitGlobalIndirectSymbol(Module &M,
1170                                           const GlobalIndirectSymbol& GIS) {
1171   MCSymbol *Name = getSymbol(&GIS);
1172 
1173   if (GIS.hasExternalLinkage() || !MAI->getWeakRefDirective())
1174     OutStreamer->EmitSymbolAttribute(Name, MCSA_Global);
1175   else if (GIS.hasWeakLinkage() || GIS.hasLinkOnceLinkage())
1176     OutStreamer->EmitSymbolAttribute(Name, MCSA_WeakReference);
1177   else
1178     assert(GIS.hasLocalLinkage() && "Invalid alias or ifunc linkage");
1179 
1180   // Set the symbol type to function if the alias has a function type.
1181   // This affects codegen when the aliasee is not a function.
1182   if (GIS.getType()->getPointerElementType()->isFunctionTy()) {
1183     OutStreamer->EmitSymbolAttribute(Name, MCSA_ELF_TypeFunction);
1184     if (isa<GlobalIFunc>(GIS))
1185       OutStreamer->EmitSymbolAttribute(Name, MCSA_ELF_TypeIndFunction);
1186   }
1187 
1188   EmitVisibility(Name, GIS.getVisibility());
1189 
1190   const MCExpr *Expr = lowerConstant(GIS.getIndirectSymbol());
1191 
1192   if (isa<GlobalAlias>(&GIS) && MAI->hasAltEntry() && isa<MCBinaryExpr>(Expr))
1193     OutStreamer->EmitSymbolAttribute(Name, MCSA_AltEntry);
1194 
1195   // Emit the directives as assignments aka .set:
1196   OutStreamer->EmitAssignment(Name, Expr);
1197 
1198   if (auto *GA = dyn_cast<GlobalAlias>(&GIS)) {
1199     // If the aliasee does not correspond to a symbol in the output, i.e. the
1200     // alias is not of an object or the aliased object is private, then set the
1201     // size of the alias symbol from the type of the alias. We don't do this in
1202     // other situations as the alias and aliasee having differing types but same
1203     // size may be intentional.
1204     const GlobalObject *BaseObject = GA->getBaseObject();
1205     if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized() &&
1206         (!BaseObject || BaseObject->hasPrivateLinkage())) {
1207       const DataLayout &DL = M.getDataLayout();
1208       uint64_t Size = DL.getTypeAllocSize(GA->getValueType());
1209       OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext));
1210     }
1211   }
1212 }
1213 
1214 bool AsmPrinter::doFinalization(Module &M) {
1215   // Set the MachineFunction to nullptr so that we can catch attempted
1216   // accesses to MF specific features at the module level and so that
1217   // we can conditionalize accesses based on whether or not it is nullptr.
1218   MF = nullptr;
1219 
1220   // Gather all GOT equivalent globals in the module. We really need two
1221   // passes over the globals: one to compute and another to avoid its emission
1222   // in EmitGlobalVariable, otherwise we would not be able to handle cases
1223   // where the got equivalent shows up before its use.
1224   computeGlobalGOTEquivs(M);
1225 
1226   // Emit global variables.
1227   for (const auto &G : M.globals())
1228     EmitGlobalVariable(&G);
1229 
1230   // Emit remaining GOT equivalent globals.
1231   emitGlobalGOTEquivs();
1232 
1233   // Emit visibility info for declarations
1234   for (const Function &F : M) {
1235     if (!F.isDeclarationForLinker())
1236       continue;
1237     GlobalValue::VisibilityTypes V = F.getVisibility();
1238     if (V == GlobalValue::DefaultVisibility)
1239       continue;
1240 
1241     MCSymbol *Name = getSymbol(&F);
1242     EmitVisibility(Name, V, false);
1243   }
1244 
1245   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
1246 
1247   // Emit module flags.
1248   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
1249   M.getModuleFlagsMetadata(ModuleFlags);
1250   if (!ModuleFlags.empty())
1251     TLOF.emitModuleFlags(*OutStreamer, ModuleFlags, TM);
1252 
1253   if (TM.getTargetTriple().isOSBinFormatELF()) {
1254     MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
1255 
1256     // Output stubs for external and common global variables.
1257     MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
1258     if (!Stubs.empty()) {
1259       OutStreamer->SwitchSection(TLOF.getDataSection());
1260       const DataLayout &DL = M.getDataLayout();
1261 
1262       for (const auto &Stub : Stubs) {
1263         OutStreamer->EmitLabel(Stub.first);
1264         OutStreamer->EmitSymbolValue(Stub.second.getPointer(),
1265                                      DL.getPointerSize());
1266       }
1267     }
1268   }
1269 
1270   // Finalize debug and EH information.
1271   for (const HandlerInfo &HI : Handlers) {
1272     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1273                        HI.TimerGroupDescription, TimePassesIsEnabled);
1274     HI.Handler->endModule();
1275     delete HI.Handler;
1276   }
1277   Handlers.clear();
1278   DD = nullptr;
1279 
1280   // If the target wants to know about weak references, print them all.
1281   if (MAI->getWeakRefDirective()) {
1282     // FIXME: This is not lazy, it would be nice to only print weak references
1283     // to stuff that is actually used.  Note that doing so would require targets
1284     // to notice uses in operands (due to constant exprs etc).  This should
1285     // happen with the MC stuff eventually.
1286 
1287     // Print out module-level global objects here.
1288     for (const auto &GO : M.global_objects()) {
1289       if (!GO.hasExternalWeakLinkage())
1290         continue;
1291       OutStreamer->EmitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
1292     }
1293   }
1294 
1295   OutStreamer->AddBlankLine();
1296 
1297   // Print aliases in topological order, that is, for each alias a = b,
1298   // b must be printed before a.
1299   // This is because on some targets (e.g. PowerPC) linker expects aliases in
1300   // such an order to generate correct TOC information.
1301   SmallVector<const GlobalAlias *, 16> AliasStack;
1302   SmallPtrSet<const GlobalAlias *, 16> AliasVisited;
1303   for (const auto &Alias : M.aliases()) {
1304     for (const GlobalAlias *Cur = &Alias; Cur;
1305          Cur = dyn_cast<GlobalAlias>(Cur->getAliasee())) {
1306       if (!AliasVisited.insert(Cur).second)
1307         break;
1308       AliasStack.push_back(Cur);
1309     }
1310     for (const GlobalAlias *AncestorAlias : llvm::reverse(AliasStack))
1311       emitGlobalIndirectSymbol(M, *AncestorAlias);
1312     AliasStack.clear();
1313   }
1314   for (const auto &IFunc : M.ifuncs())
1315     emitGlobalIndirectSymbol(M, IFunc);
1316 
1317   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
1318   assert(MI && "AsmPrinter didn't require GCModuleInfo?");
1319   for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
1320     if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(**--I))
1321       MP->finishAssembly(M, *MI, *this);
1322 
1323   // Emit llvm.ident metadata in an '.ident' directive.
1324   EmitModuleIdents(M);
1325 
1326   // Emit __morestack address if needed for indirect calls.
1327   if (MMI->usesMorestackAddr()) {
1328     unsigned Align = 1;
1329     MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant(
1330         getDataLayout(), SectionKind::getReadOnly(),
1331         /*C=*/nullptr, Align);
1332     OutStreamer->SwitchSection(ReadOnlySection);
1333 
1334     MCSymbol *AddrSymbol =
1335         OutContext.getOrCreateSymbol(StringRef("__morestack_addr"));
1336     OutStreamer->EmitLabel(AddrSymbol);
1337 
1338     unsigned PtrSize = M.getDataLayout().getPointerSize(0);
1339     OutStreamer->EmitSymbolValue(GetExternalSymbolSymbol("__morestack"),
1340                                  PtrSize);
1341   }
1342 
1343   // If we don't have any trampolines, then we don't require stack memory
1344   // to be executable. Some targets have a directive to declare this.
1345   Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
1346   if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
1347     if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
1348       OutStreamer->SwitchSection(S);
1349 
1350   // Allow the target to emit any magic that it wants at the end of the file,
1351   // after everything else has gone out.
1352   EmitEndOfAsmFile(M);
1353 
1354   MMI = nullptr;
1355 
1356   OutStreamer->Finish();
1357   OutStreamer->reset();
1358 
1359   return false;
1360 }
1361 
1362 MCSymbol *AsmPrinter::getCurExceptionSym() {
1363   if (!CurExceptionSym)
1364     CurExceptionSym = createTempSymbol("exception");
1365   return CurExceptionSym;
1366 }
1367 
1368 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
1369   this->MF = &MF;
1370   // Get the function symbol.
1371   CurrentFnSym = getSymbol(MF.getFunction());
1372   CurrentFnSymForSize = CurrentFnSym;
1373   CurrentFnBegin = nullptr;
1374   CurExceptionSym = nullptr;
1375   bool NeedsLocalForSize = MAI->needsLocalForSize();
1376   if (!MF.getLandingPads().empty() || MMI->hasDebugInfo() ||
1377       MF.hasEHFunclets() || NeedsLocalForSize) {
1378     CurrentFnBegin = createTempSymbol("func_begin");
1379     if (NeedsLocalForSize)
1380       CurrentFnSymForSize = CurrentFnBegin;
1381   }
1382 
1383   ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
1384   if (isVerbose())
1385     LI = &getAnalysis<MachineLoopInfo>();
1386 }
1387 
1388 namespace {
1389 
1390 // Keep track the alignment, constpool entries per Section.
1391   struct SectionCPs {
1392     MCSection *S;
1393     unsigned Alignment;
1394     SmallVector<unsigned, 4> CPEs;
1395 
1396     SectionCPs(MCSection *s, unsigned a) : S(s), Alignment(a) {}
1397   };
1398 
1399 } // end anonymous namespace
1400 
1401 /// EmitConstantPool - Print to the current output stream assembly
1402 /// representations of the constants in the constant pool MCP. This is
1403 /// used to print out constants which have been "spilled to memory" by
1404 /// the code generator.
1405 ///
1406 void AsmPrinter::EmitConstantPool() {
1407   const MachineConstantPool *MCP = MF->getConstantPool();
1408   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
1409   if (CP.empty()) return;
1410 
1411   // Calculate sections for constant pool entries. We collect entries to go into
1412   // the same section together to reduce amount of section switch statements.
1413   SmallVector<SectionCPs, 4> CPSections;
1414   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
1415     const MachineConstantPoolEntry &CPE = CP[i];
1416     unsigned Align = CPE.getAlignment();
1417 
1418     SectionKind Kind = CPE.getSectionKind(&getDataLayout());
1419 
1420     const Constant *C = nullptr;
1421     if (!CPE.isMachineConstantPoolEntry())
1422       C = CPE.Val.ConstVal;
1423 
1424     MCSection *S = getObjFileLowering().getSectionForConstant(getDataLayout(),
1425                                                               Kind, C, Align);
1426 
1427     // The number of sections are small, just do a linear search from the
1428     // last section to the first.
1429     bool Found = false;
1430     unsigned SecIdx = CPSections.size();
1431     while (SecIdx != 0) {
1432       if (CPSections[--SecIdx].S == S) {
1433         Found = true;
1434         break;
1435       }
1436     }
1437     if (!Found) {
1438       SecIdx = CPSections.size();
1439       CPSections.push_back(SectionCPs(S, Align));
1440     }
1441 
1442     if (Align > CPSections[SecIdx].Alignment)
1443       CPSections[SecIdx].Alignment = Align;
1444     CPSections[SecIdx].CPEs.push_back(i);
1445   }
1446 
1447   // Now print stuff into the calculated sections.
1448   const MCSection *CurSection = nullptr;
1449   unsigned Offset = 0;
1450   for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
1451     for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
1452       unsigned CPI = CPSections[i].CPEs[j];
1453       MCSymbol *Sym = GetCPISymbol(CPI);
1454       if (!Sym->isUndefined())
1455         continue;
1456 
1457       if (CurSection != CPSections[i].S) {
1458         OutStreamer->SwitchSection(CPSections[i].S);
1459         EmitAlignment(Log2_32(CPSections[i].Alignment));
1460         CurSection = CPSections[i].S;
1461         Offset = 0;
1462       }
1463 
1464       MachineConstantPoolEntry CPE = CP[CPI];
1465 
1466       // Emit inter-object padding for alignment.
1467       unsigned AlignMask = CPE.getAlignment() - 1;
1468       unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
1469       OutStreamer->EmitZeros(NewOffset - Offset);
1470 
1471       Type *Ty = CPE.getType();
1472       Offset = NewOffset + getDataLayout().getTypeAllocSize(Ty);
1473 
1474       OutStreamer->EmitLabel(Sym);
1475       if (CPE.isMachineConstantPoolEntry())
1476         EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
1477       else
1478         EmitGlobalConstant(getDataLayout(), CPE.Val.ConstVal);
1479     }
1480   }
1481 }
1482 
1483 /// EmitJumpTableInfo - Print assembly representations of the jump tables used
1484 /// by the current function to the current output stream.
1485 ///
1486 void AsmPrinter::EmitJumpTableInfo() {
1487   const DataLayout &DL = MF->getDataLayout();
1488   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1489   if (!MJTI) return;
1490   if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
1491   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1492   if (JT.empty()) return;
1493 
1494   // Pick the directive to use to print the jump table entries, and switch to
1495   // the appropriate section.
1496   const Function *F = MF->getFunction();
1497   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
1498   bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
1499       MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32,
1500       *F);
1501   if (JTInDiffSection) {
1502     // Drop it in the readonly section.
1503     MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(*F, TM);
1504     OutStreamer->SwitchSection(ReadOnlySection);
1505   }
1506 
1507   EmitAlignment(Log2_32(MJTI->getEntryAlignment(DL)));
1508 
1509   // Jump tables in code sections are marked with a data_region directive
1510   // where that's supported.
1511   if (!JTInDiffSection)
1512     OutStreamer->EmitDataRegion(MCDR_DataRegionJT32);
1513 
1514   for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
1515     const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1516 
1517     // If this jump table was deleted, ignore it.
1518     if (JTBBs.empty()) continue;
1519 
1520     // For the EK_LabelDifference32 entry, if using .set avoids a relocation,
1521     /// emit a .set directive for each unique entry.
1522     if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
1523         MAI->doesSetDirectiveSuppressReloc()) {
1524       SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
1525       const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
1526       const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
1527       for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
1528         const MachineBasicBlock *MBB = JTBBs[ii];
1529         if (!EmittedSets.insert(MBB).second)
1530           continue;
1531 
1532         // .set LJTSet, LBB32-base
1533         const MCExpr *LHS =
1534           MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
1535         OutStreamer->EmitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
1536                                     MCBinaryExpr::createSub(LHS, Base,
1537                                                             OutContext));
1538       }
1539     }
1540 
1541     // On some targets (e.g. Darwin) we want to emit two consecutive labels
1542     // before each jump table.  The first label is never referenced, but tells
1543     // the assembler and linker the extents of the jump table object.  The
1544     // second label is actually referenced by the code.
1545     if (JTInDiffSection && DL.hasLinkerPrivateGlobalPrefix())
1546       // FIXME: This doesn't have to have any specific name, just any randomly
1547       // named and numbered 'l' label would work.  Simplify GetJTISymbol.
1548       OutStreamer->EmitLabel(GetJTISymbol(JTI, true));
1549 
1550     OutStreamer->EmitLabel(GetJTISymbol(JTI));
1551 
1552     for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
1553       EmitJumpTableEntry(MJTI, JTBBs[ii], JTI);
1554   }
1555   if (!JTInDiffSection)
1556     OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
1557 }
1558 
1559 /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
1560 /// current stream.
1561 void AsmPrinter::EmitJumpTableEntry(const MachineJumpTableInfo *MJTI,
1562                                     const MachineBasicBlock *MBB,
1563                                     unsigned UID) const {
1564   assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
1565   const MCExpr *Value = nullptr;
1566   switch (MJTI->getEntryKind()) {
1567   case MachineJumpTableInfo::EK_Inline:
1568     llvm_unreachable("Cannot emit EK_Inline jump table entry");
1569   case MachineJumpTableInfo::EK_Custom32:
1570     Value = MF->getSubtarget().getTargetLowering()->LowerCustomJumpTableEntry(
1571         MJTI, MBB, UID, OutContext);
1572     break;
1573   case MachineJumpTableInfo::EK_BlockAddress:
1574     // EK_BlockAddress - Each entry is a plain address of block, e.g.:
1575     //     .word LBB123
1576     Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
1577     break;
1578   case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
1579     // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
1580     // with a relocation as gp-relative, e.g.:
1581     //     .gprel32 LBB123
1582     MCSymbol *MBBSym = MBB->getSymbol();
1583     OutStreamer->EmitGPRel32Value(MCSymbolRefExpr::create(MBBSym, OutContext));
1584     return;
1585   }
1586 
1587   case MachineJumpTableInfo::EK_GPRel64BlockAddress: {
1588     // EK_GPRel64BlockAddress - Each entry is an address of block, encoded
1589     // with a relocation as gp-relative, e.g.:
1590     //     .gpdword LBB123
1591     MCSymbol *MBBSym = MBB->getSymbol();
1592     OutStreamer->EmitGPRel64Value(MCSymbolRefExpr::create(MBBSym, OutContext));
1593     return;
1594   }
1595 
1596   case MachineJumpTableInfo::EK_LabelDifference32: {
1597     // Each entry is the address of the block minus the address of the jump
1598     // table. This is used for PIC jump tables where gprel32 is not supported.
1599     // e.g.:
1600     //      .word LBB123 - LJTI1_2
1601     // If the .set directive avoids relocations, this is emitted as:
1602     //      .set L4_5_set_123, LBB123 - LJTI1_2
1603     //      .word L4_5_set_123
1604     if (MAI->doesSetDirectiveSuppressReloc()) {
1605       Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()),
1606                                       OutContext);
1607       break;
1608     }
1609     Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
1610     const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
1611     const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext);
1612     Value = MCBinaryExpr::createSub(Value, Base, OutContext);
1613     break;
1614   }
1615   }
1616 
1617   assert(Value && "Unknown entry kind!");
1618 
1619   unsigned EntrySize = MJTI->getEntrySize(getDataLayout());
1620   OutStreamer->EmitValue(Value, EntrySize);
1621 }
1622 
1623 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
1624 /// special global used by LLVM.  If so, emit it and return true, otherwise
1625 /// do nothing and return false.
1626 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
1627   if (GV->getName() == "llvm.used") {
1628     if (MAI->hasNoDeadStrip())    // No need to emit this at all.
1629       EmitLLVMUsedList(cast<ConstantArray>(GV->getInitializer()));
1630     return true;
1631   }
1632 
1633   // Ignore debug and non-emitted data.  This handles llvm.compiler.used.
1634   if (GV->getSection() == "llvm.metadata" ||
1635       GV->hasAvailableExternallyLinkage())
1636     return true;
1637 
1638   if (!GV->hasAppendingLinkage()) return false;
1639 
1640   assert(GV->hasInitializer() && "Not a special LLVM global!");
1641 
1642   if (GV->getName() == "llvm.global_ctors") {
1643     EmitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
1644                        /* isCtor */ true);
1645 
1646     return true;
1647   }
1648 
1649   if (GV->getName() == "llvm.global_dtors") {
1650     EmitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
1651                        /* isCtor */ false);
1652 
1653     return true;
1654   }
1655 
1656   report_fatal_error("unknown special variable");
1657 }
1658 
1659 /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
1660 /// global in the specified llvm.used list for which emitUsedDirectiveFor
1661 /// is true, as being used with this directive.
1662 void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
1663   // Should be an array of 'i8*'.
1664   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1665     const GlobalValue *GV =
1666       dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
1667     if (GV)
1668       OutStreamer->EmitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
1669   }
1670 }
1671 
1672 namespace {
1673 
1674 struct Structor {
1675   int Priority = 0;
1676   Constant *Func = nullptr;
1677   GlobalValue *ComdatKey = nullptr;
1678 
1679   Structor() = default;
1680 };
1681 
1682 }  // end anonymous namespace
1683 
1684 /// EmitXXStructorList - Emit the ctor or dtor list taking into account the init
1685 /// priority.
1686 void AsmPrinter::EmitXXStructorList(const DataLayout &DL, const Constant *List,
1687                                     bool isCtor) {
1688   // Should be an array of '{ int, void ()* }' structs.  The first value is the
1689   // init priority.
1690   if (!isa<ConstantArray>(List)) return;
1691 
1692   // Sanity check the structors list.
1693   const ConstantArray *InitList = dyn_cast<ConstantArray>(List);
1694   if (!InitList) return; // Not an array!
1695   StructType *ETy = dyn_cast<StructType>(InitList->getType()->getElementType());
1696   // FIXME: Only allow the 3-field form in LLVM 4.0.
1697   if (!ETy || ETy->getNumElements() < 2 || ETy->getNumElements() > 3)
1698     return; // Not an array of two or three elements!
1699   if (!isa<IntegerType>(ETy->getTypeAtIndex(0U)) ||
1700       !isa<PointerType>(ETy->getTypeAtIndex(1U))) return; // Not (int, ptr).
1701   if (ETy->getNumElements() == 3 && !isa<PointerType>(ETy->getTypeAtIndex(2U)))
1702     return; // Not (int, ptr, ptr).
1703 
1704   // Gather the structors in a form that's convenient for sorting by priority.
1705   SmallVector<Structor, 8> Structors;
1706   for (Value *O : InitList->operands()) {
1707     ConstantStruct *CS = dyn_cast<ConstantStruct>(O);
1708     if (!CS) continue; // Malformed.
1709     if (CS->getOperand(1)->isNullValue())
1710       break;  // Found a null terminator, skip the rest.
1711     ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1712     if (!Priority) continue; // Malformed.
1713     Structors.push_back(Structor());
1714     Structor &S = Structors.back();
1715     S.Priority = Priority->getLimitedValue(65535);
1716     S.Func = CS->getOperand(1);
1717     if (ETy->getNumElements() == 3 && !CS->getOperand(2)->isNullValue())
1718       S.ComdatKey =
1719           dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts());
1720   }
1721 
1722   // Emit the function pointers in the target-specific order
1723   unsigned Align = Log2_32(DL.getPointerPrefAlignment());
1724   std::stable_sort(Structors.begin(), Structors.end(),
1725                    [](const Structor &L,
1726                       const Structor &R) { return L.Priority < R.Priority; });
1727   for (Structor &S : Structors) {
1728     const TargetLoweringObjectFile &Obj = getObjFileLowering();
1729     const MCSymbol *KeySym = nullptr;
1730     if (GlobalValue *GV = S.ComdatKey) {
1731       if (GV->isDeclarationForLinker())
1732         // If the associated variable is not defined in this module
1733         // (it might be available_externally, or have been an
1734         // available_externally definition that was dropped by the
1735         // EliminateAvailableExternally pass), some other TU
1736         // will provide its dynamic initializer.
1737         continue;
1738 
1739       KeySym = getSymbol(GV);
1740     }
1741     MCSection *OutputSection =
1742         (isCtor ? Obj.getStaticCtorSection(S.Priority, KeySym)
1743                 : Obj.getStaticDtorSection(S.Priority, KeySym));
1744     OutStreamer->SwitchSection(OutputSection);
1745     if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
1746       EmitAlignment(Align);
1747     EmitXXStructor(DL, S.Func);
1748   }
1749 }
1750 
1751 void AsmPrinter::EmitModuleIdents(Module &M) {
1752   if (!MAI->hasIdentDirective())
1753     return;
1754 
1755   if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
1756     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1757       const MDNode *N = NMD->getOperand(i);
1758       assert(N->getNumOperands() == 1 &&
1759              "llvm.ident metadata entry can have only one operand");
1760       const MDString *S = cast<MDString>(N->getOperand(0));
1761       OutStreamer->EmitIdent(S->getString());
1762     }
1763   }
1764 }
1765 
1766 //===--------------------------------------------------------------------===//
1767 // Emission and print routines
1768 //
1769 
1770 /// EmitInt8 - Emit a byte directive and value.
1771 ///
1772 void AsmPrinter::EmitInt8(int Value) const {
1773   OutStreamer->EmitIntValue(Value, 1);
1774 }
1775 
1776 /// EmitInt16 - Emit a short directive and value.
1777 ///
1778 void AsmPrinter::EmitInt16(int Value) const {
1779   OutStreamer->EmitIntValue(Value, 2);
1780 }
1781 
1782 /// EmitInt32 - Emit a long directive and value.
1783 ///
1784 void AsmPrinter::EmitInt32(int Value) const {
1785   OutStreamer->EmitIntValue(Value, 4);
1786 }
1787 
1788 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
1789 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
1790 /// .set if it avoids relocations.
1791 void AsmPrinter::EmitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
1792                                      unsigned Size) const {
1793   OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, Size);
1794 }
1795 
1796 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
1797 /// where the size in bytes of the directive is specified by Size and Label
1798 /// specifies the label.  This implicitly uses .set if it is available.
1799 void AsmPrinter::EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
1800                                      unsigned Size,
1801                                      bool IsSectionRelative) const {
1802   if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) {
1803     OutStreamer->EmitCOFFSecRel32(Label, Offset);
1804     if (Size > 4)
1805       OutStreamer->EmitZeros(Size - 4);
1806     return;
1807   }
1808 
1809   // Emit Label+Offset (or just Label if Offset is zero)
1810   const MCExpr *Expr = MCSymbolRefExpr::create(Label, OutContext);
1811   if (Offset)
1812     Expr = MCBinaryExpr::createAdd(
1813         Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
1814 
1815   OutStreamer->EmitValue(Expr, Size);
1816 }
1817 
1818 //===----------------------------------------------------------------------===//
1819 
1820 // EmitAlignment - Emit an alignment directive to the specified power of
1821 // two boundary.  For example, if you pass in 3 here, you will get an 8
1822 // byte alignment.  If a global value is specified, and if that global has
1823 // an explicit alignment requested, it will override the alignment request
1824 // if required for correctness.
1825 //
1826 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalObject *GV) const {
1827   if (GV)
1828     NumBits = getGVAlignmentLog2(GV, GV->getParent()->getDataLayout(), NumBits);
1829 
1830   if (NumBits == 0) return;   // 1-byte aligned: no need to emit alignment.
1831 
1832   assert(NumBits <
1833              static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
1834          "undefined behavior");
1835   if (getCurrentSection()->getKind().isText())
1836     OutStreamer->EmitCodeAlignment(1u << NumBits);
1837   else
1838     OutStreamer->EmitValueToAlignment(1u << NumBits);
1839 }
1840 
1841 //===----------------------------------------------------------------------===//
1842 // Constant emission.
1843 //===----------------------------------------------------------------------===//
1844 
1845 const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
1846   MCContext &Ctx = OutContext;
1847 
1848   if (CV->isNullValue() || isa<UndefValue>(CV))
1849     return MCConstantExpr::create(0, Ctx);
1850 
1851   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
1852     return MCConstantExpr::create(CI->getZExtValue(), Ctx);
1853 
1854   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
1855     return MCSymbolRefExpr::create(getSymbol(GV), Ctx);
1856 
1857   if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
1858     return MCSymbolRefExpr::create(GetBlockAddressSymbol(BA), Ctx);
1859 
1860   const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
1861   if (!CE) {
1862     llvm_unreachable("Unknown constant value to lower!");
1863   }
1864 
1865   switch (CE->getOpcode()) {
1866   default:
1867     // If the code isn't optimized, there may be outstanding folding
1868     // opportunities. Attempt to fold the expression using DataLayout as a
1869     // last resort before giving up.
1870     if (Constant *C = ConstantFoldConstant(CE, getDataLayout()))
1871       if (C != CE)
1872         return lowerConstant(C);
1873 
1874     // Otherwise report the problem to the user.
1875     {
1876       std::string S;
1877       raw_string_ostream OS(S);
1878       OS << "Unsupported expression in static initializer: ";
1879       CE->printAsOperand(OS, /*PrintType=*/false,
1880                      !MF ? nullptr : MF->getFunction()->getParent());
1881       report_fatal_error(OS.str());
1882     }
1883   case Instruction::GetElementPtr: {
1884     // Generate a symbolic expression for the byte address
1885     APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0);
1886     cast<GEPOperator>(CE)->accumulateConstantOffset(getDataLayout(), OffsetAI);
1887 
1888     const MCExpr *Base = lowerConstant(CE->getOperand(0));
1889     if (!OffsetAI)
1890       return Base;
1891 
1892     int64_t Offset = OffsetAI.getSExtValue();
1893     return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx),
1894                                    Ctx);
1895   }
1896 
1897   case Instruction::Trunc:
1898     // We emit the value and depend on the assembler to truncate the generated
1899     // expression properly.  This is important for differences between
1900     // blockaddress labels.  Since the two labels are in the same function, it
1901     // is reasonable to treat their delta as a 32-bit value.
1902     LLVM_FALLTHROUGH;
1903   case Instruction::BitCast:
1904     return lowerConstant(CE->getOperand(0));
1905 
1906   case Instruction::IntToPtr: {
1907     const DataLayout &DL = getDataLayout();
1908 
1909     // Handle casts to pointers by changing them into casts to the appropriate
1910     // integer type.  This promotes constant folding and simplifies this code.
1911     Constant *Op = CE->getOperand(0);
1912     Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
1913                                       false/*ZExt*/);
1914     return lowerConstant(Op);
1915   }
1916 
1917   case Instruction::PtrToInt: {
1918     const DataLayout &DL = getDataLayout();
1919 
1920     // Support only foldable casts to/from pointers that can be eliminated by
1921     // changing the pointer to the appropriately sized integer type.
1922     Constant *Op = CE->getOperand(0);
1923     Type *Ty = CE->getType();
1924 
1925     const MCExpr *OpExpr = lowerConstant(Op);
1926 
1927     // We can emit the pointer value into this slot if the slot is an
1928     // integer slot equal to the size of the pointer.
1929     if (DL.getTypeAllocSize(Ty) == DL.getTypeAllocSize(Op->getType()))
1930       return OpExpr;
1931 
1932     // Otherwise the pointer is smaller than the resultant integer, mask off
1933     // the high bits so we are sure to get a proper truncation if the input is
1934     // a constant expr.
1935     unsigned InBits = DL.getTypeAllocSizeInBits(Op->getType());
1936     const MCExpr *MaskExpr = MCConstantExpr::create(~0ULL >> (64-InBits), Ctx);
1937     return MCBinaryExpr::createAnd(OpExpr, MaskExpr, Ctx);
1938   }
1939 
1940   case Instruction::Sub: {
1941     GlobalValue *LHSGV;
1942     APInt LHSOffset;
1943     if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHSGV, LHSOffset,
1944                                    getDataLayout())) {
1945       GlobalValue *RHSGV;
1946       APInt RHSOffset;
1947       if (IsConstantOffsetFromGlobal(CE->getOperand(1), RHSGV, RHSOffset,
1948                                      getDataLayout())) {
1949         const MCExpr *RelocExpr =
1950             getObjFileLowering().lowerRelativeReference(LHSGV, RHSGV, TM);
1951         if (!RelocExpr)
1952           RelocExpr = MCBinaryExpr::createSub(
1953               MCSymbolRefExpr::create(getSymbol(LHSGV), Ctx),
1954               MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx), Ctx);
1955         int64_t Addend = (LHSOffset - RHSOffset).getSExtValue();
1956         if (Addend != 0)
1957           RelocExpr = MCBinaryExpr::createAdd(
1958               RelocExpr, MCConstantExpr::create(Addend, Ctx), Ctx);
1959         return RelocExpr;
1960       }
1961     }
1962   }
1963   // else fallthrough
1964 
1965   // The MC library also has a right-shift operator, but it isn't consistently
1966   // signed or unsigned between different targets.
1967   case Instruction::Add:
1968   case Instruction::Mul:
1969   case Instruction::SDiv:
1970   case Instruction::SRem:
1971   case Instruction::Shl:
1972   case Instruction::And:
1973   case Instruction::Or:
1974   case Instruction::Xor: {
1975     const MCExpr *LHS = lowerConstant(CE->getOperand(0));
1976     const MCExpr *RHS = lowerConstant(CE->getOperand(1));
1977     switch (CE->getOpcode()) {
1978     default: llvm_unreachable("Unknown binary operator constant cast expr");
1979     case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
1980     case Instruction::Sub: return MCBinaryExpr::createSub(LHS, RHS, Ctx);
1981     case Instruction::Mul: return MCBinaryExpr::createMul(LHS, RHS, Ctx);
1982     case Instruction::SDiv: return MCBinaryExpr::createDiv(LHS, RHS, Ctx);
1983     case Instruction::SRem: return MCBinaryExpr::createMod(LHS, RHS, Ctx);
1984     case Instruction::Shl: return MCBinaryExpr::createShl(LHS, RHS, Ctx);
1985     case Instruction::And: return MCBinaryExpr::createAnd(LHS, RHS, Ctx);
1986     case Instruction::Or:  return MCBinaryExpr::createOr (LHS, RHS, Ctx);
1987     case Instruction::Xor: return MCBinaryExpr::createXor(LHS, RHS, Ctx);
1988     }
1989   }
1990   }
1991 }
1992 
1993 static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *C,
1994                                    AsmPrinter &AP,
1995                                    const Constant *BaseCV = nullptr,
1996                                    uint64_t Offset = 0);
1997 
1998 static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP);
1999 
2000 /// isRepeatedByteSequence - Determine whether the given value is
2001 /// composed of a repeated sequence of identical bytes and return the
2002 /// byte value.  If it is not a repeated sequence, return -1.
2003 static int isRepeatedByteSequence(const ConstantDataSequential *V) {
2004   StringRef Data = V->getRawDataValues();
2005   assert(!Data.empty() && "Empty aggregates should be CAZ node");
2006   char C = Data[0];
2007   for (unsigned i = 1, e = Data.size(); i != e; ++i)
2008     if (Data[i] != C) return -1;
2009   return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1.
2010 }
2011 
2012 /// isRepeatedByteSequence - Determine whether the given value is
2013 /// composed of a repeated sequence of identical bytes and return the
2014 /// byte value.  If it is not a repeated sequence, return -1.
2015 static int isRepeatedByteSequence(const Value *V, const DataLayout &DL) {
2016   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
2017     uint64_t Size = DL.getTypeAllocSizeInBits(V->getType());
2018     assert(Size % 8 == 0);
2019 
2020     // Extend the element to take zero padding into account.
2021     APInt Value = CI->getValue().zextOrSelf(Size);
2022     if (!Value.isSplat(8))
2023       return -1;
2024 
2025     return Value.zextOrTrunc(8).getZExtValue();
2026   }
2027   if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
2028     // Make sure all array elements are sequences of the same repeated
2029     // byte.
2030     assert(CA->getNumOperands() != 0 && "Should be a CAZ");
2031     Constant *Op0 = CA->getOperand(0);
2032     int Byte = isRepeatedByteSequence(Op0, DL);
2033     if (Byte == -1)
2034       return -1;
2035 
2036     // All array elements must be equal.
2037     for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i)
2038       if (CA->getOperand(i) != Op0)
2039         return -1;
2040     return Byte;
2041   }
2042 
2043   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
2044     return isRepeatedByteSequence(CDS);
2045 
2046   return -1;
2047 }
2048 
2049 static void emitGlobalConstantDataSequential(const DataLayout &DL,
2050                                              const ConstantDataSequential *CDS,
2051                                              AsmPrinter &AP) {
2052   // See if we can aggregate this into a .fill, if so, emit it as such.
2053   int Value = isRepeatedByteSequence(CDS, DL);
2054   if (Value != -1) {
2055     uint64_t Bytes = DL.getTypeAllocSize(CDS->getType());
2056     // Don't emit a 1-byte object as a .fill.
2057     if (Bytes > 1)
2058       return AP.OutStreamer->emitFill(Bytes, Value);
2059   }
2060 
2061   // If this can be emitted with .ascii/.asciz, emit it as such.
2062   if (CDS->isString())
2063     return AP.OutStreamer->EmitBytes(CDS->getAsString());
2064 
2065   // Otherwise, emit the values in successive locations.
2066   unsigned ElementByteSize = CDS->getElementByteSize();
2067   if (isa<IntegerType>(CDS->getElementType())) {
2068     for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
2069       if (AP.isVerbose())
2070         AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
2071                                                  CDS->getElementAsInteger(i));
2072       AP.OutStreamer->EmitIntValue(CDS->getElementAsInteger(i),
2073                                    ElementByteSize);
2074     }
2075   } else {
2076     for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I)
2077       emitGlobalConstantFP(cast<ConstantFP>(CDS->getElementAsConstant(I)), AP);
2078   }
2079 
2080   unsigned Size = DL.getTypeAllocSize(CDS->getType());
2081   unsigned EmittedSize = DL.getTypeAllocSize(CDS->getType()->getElementType()) *
2082                         CDS->getNumElements();
2083   if (unsigned Padding = Size - EmittedSize)
2084     AP.OutStreamer->EmitZeros(Padding);
2085 }
2086 
2087 static void emitGlobalConstantArray(const DataLayout &DL,
2088                                     const ConstantArray *CA, AsmPrinter &AP,
2089                                     const Constant *BaseCV, uint64_t Offset) {
2090   // See if we can aggregate some values.  Make sure it can be
2091   // represented as a series of bytes of the constant value.
2092   int Value = isRepeatedByteSequence(CA, DL);
2093 
2094   if (Value != -1) {
2095     uint64_t Bytes = DL.getTypeAllocSize(CA->getType());
2096     AP.OutStreamer->emitFill(Bytes, Value);
2097   }
2098   else {
2099     for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
2100       emitGlobalConstantImpl(DL, CA->getOperand(i), AP, BaseCV, Offset);
2101       Offset += DL.getTypeAllocSize(CA->getOperand(i)->getType());
2102     }
2103   }
2104 }
2105 
2106 static void emitGlobalConstantVector(const DataLayout &DL,
2107                                      const ConstantVector *CV, AsmPrinter &AP) {
2108   for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
2109     emitGlobalConstantImpl(DL, CV->getOperand(i), AP);
2110 
2111   unsigned Size = DL.getTypeAllocSize(CV->getType());
2112   unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
2113                          CV->getType()->getNumElements();
2114   if (unsigned Padding = Size - EmittedSize)
2115     AP.OutStreamer->EmitZeros(Padding);
2116 }
2117 
2118 static void emitGlobalConstantStruct(const DataLayout &DL,
2119                                      const ConstantStruct *CS, AsmPrinter &AP,
2120                                      const Constant *BaseCV, uint64_t Offset) {
2121   // Print the fields in successive locations. Pad to align if needed!
2122   unsigned Size = DL.getTypeAllocSize(CS->getType());
2123   const StructLayout *Layout = DL.getStructLayout(CS->getType());
2124   uint64_t SizeSoFar = 0;
2125   for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
2126     const Constant *Field = CS->getOperand(i);
2127 
2128     // Print the actual field value.
2129     emitGlobalConstantImpl(DL, Field, AP, BaseCV, Offset + SizeSoFar);
2130 
2131     // Check if padding is needed and insert one or more 0s.
2132     uint64_t FieldSize = DL.getTypeAllocSize(Field->getType());
2133     uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
2134                         - Layout->getElementOffset(i)) - FieldSize;
2135     SizeSoFar += FieldSize + PadSize;
2136 
2137     // Insert padding - this may include padding to increase the size of the
2138     // current field up to the ABI size (if the struct is not packed) as well
2139     // as padding to ensure that the next field starts at the right offset.
2140     AP.OutStreamer->EmitZeros(PadSize);
2141   }
2142   assert(SizeSoFar == Layout->getSizeInBytes() &&
2143          "Layout of constant struct may be incorrect!");
2144 }
2145 
2146 static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) {
2147   APInt API = CFP->getValueAPF().bitcastToAPInt();
2148 
2149   // First print a comment with what we think the original floating-point value
2150   // should have been.
2151   if (AP.isVerbose()) {
2152     SmallString<8> StrVal;
2153     CFP->getValueAPF().toString(StrVal);
2154 
2155     if (CFP->getType())
2156       CFP->getType()->print(AP.OutStreamer->GetCommentOS());
2157     else
2158       AP.OutStreamer->GetCommentOS() << "Printing <null> Type";
2159     AP.OutStreamer->GetCommentOS() << ' ' << StrVal << '\n';
2160   }
2161 
2162   // Now iterate through the APInt chunks, emitting them in endian-correct
2163   // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit
2164   // floats).
2165   unsigned NumBytes = API.getBitWidth() / 8;
2166   unsigned TrailingBytes = NumBytes % sizeof(uint64_t);
2167   const uint64_t *p = API.getRawData();
2168 
2169   // PPC's long double has odd notions of endianness compared to how LLVM
2170   // handles it: p[0] goes first for *big* endian on PPC.
2171   if (AP.getDataLayout().isBigEndian() && !CFP->getType()->isPPC_FP128Ty()) {
2172     int Chunk = API.getNumWords() - 1;
2173 
2174     if (TrailingBytes)
2175       AP.OutStreamer->EmitIntValue(p[Chunk--], TrailingBytes);
2176 
2177     for (; Chunk >= 0; --Chunk)
2178       AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
2179   } else {
2180     unsigned Chunk;
2181     for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk)
2182       AP.OutStreamer->EmitIntValue(p[Chunk], sizeof(uint64_t));
2183 
2184     if (TrailingBytes)
2185       AP.OutStreamer->EmitIntValue(p[Chunk], TrailingBytes);
2186   }
2187 
2188   // Emit the tail padding for the long double.
2189   const DataLayout &DL = AP.getDataLayout();
2190   AP.OutStreamer->EmitZeros(DL.getTypeAllocSize(CFP->getType()) -
2191                             DL.getTypeStoreSize(CFP->getType()));
2192 }
2193 
2194 static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
2195   const DataLayout &DL = AP.getDataLayout();
2196   unsigned BitWidth = CI->getBitWidth();
2197 
2198   // Copy the value as we may massage the layout for constants whose bit width
2199   // is not a multiple of 64-bits.
2200   APInt Realigned(CI->getValue());
2201   uint64_t ExtraBits = 0;
2202   unsigned ExtraBitsSize = BitWidth & 63;
2203 
2204   if (ExtraBitsSize) {
2205     // The bit width of the data is not a multiple of 64-bits.
2206     // The extra bits are expected to be at the end of the chunk of the memory.
2207     // Little endian:
2208     // * Nothing to be done, just record the extra bits to emit.
2209     // Big endian:
2210     // * Record the extra bits to emit.
2211     // * Realign the raw data to emit the chunks of 64-bits.
2212     if (DL.isBigEndian()) {
2213       // Basically the structure of the raw data is a chunk of 64-bits cells:
2214       //    0        1         BitWidth / 64
2215       // [chunk1][chunk2] ... [chunkN].
2216       // The most significant chunk is chunkN and it should be emitted first.
2217       // However, due to the alignment issue chunkN contains useless bits.
2218       // Realign the chunks so that they contain only useless information:
2219       // ExtraBits     0       1       (BitWidth / 64) - 1
2220       //       chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
2221       ExtraBits = Realigned.getRawData()[0] &
2222         (((uint64_t)-1) >> (64 - ExtraBitsSize));
2223       Realigned = Realigned.lshr(ExtraBitsSize);
2224     } else
2225       ExtraBits = Realigned.getRawData()[BitWidth / 64];
2226   }
2227 
2228   // We don't expect assemblers to support integer data directives
2229   // for more than 64 bits, so we emit the data in at most 64-bit
2230   // quantities at a time.
2231   const uint64_t *RawData = Realigned.getRawData();
2232   for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
2233     uint64_t Val = DL.isBigEndian() ? RawData[e - i - 1] : RawData[i];
2234     AP.OutStreamer->EmitIntValue(Val, 8);
2235   }
2236 
2237   if (ExtraBitsSize) {
2238     // Emit the extra bits after the 64-bits chunks.
2239 
2240     // Emit a directive that fills the expected size.
2241     uint64_t Size = AP.getDataLayout().getTypeAllocSize(CI->getType());
2242     Size -= (BitWidth / 64) * 8;
2243     assert(Size && Size * 8 >= ExtraBitsSize &&
2244            (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
2245            == ExtraBits && "Directive too small for extra bits.");
2246     AP.OutStreamer->EmitIntValue(ExtraBits, Size);
2247   }
2248 }
2249 
2250 /// \brief Transform a not absolute MCExpr containing a reference to a GOT
2251 /// equivalent global, by a target specific GOT pc relative access to the
2252 /// final symbol.
2253 static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
2254                                          const Constant *BaseCst,
2255                                          uint64_t Offset) {
2256   // The global @foo below illustrates a global that uses a got equivalent.
2257   //
2258   //  @bar = global i32 42
2259   //  @gotequiv = private unnamed_addr constant i32* @bar
2260   //  @foo = i32 trunc (i64 sub (i64 ptrtoint (i32** @gotequiv to i64),
2261   //                             i64 ptrtoint (i32* @foo to i64))
2262   //                        to i32)
2263   //
2264   // The cstexpr in @foo is converted into the MCExpr `ME`, where we actually
2265   // check whether @foo is suitable to use a GOTPCREL. `ME` is usually in the
2266   // form:
2267   //
2268   //  foo = cstexpr, where
2269   //    cstexpr := <gotequiv> - "." + <cst>
2270   //    cstexpr := <gotequiv> - (<foo> - <offset from @foo base>) + <cst>
2271   //
2272   // After canonicalization by evaluateAsRelocatable `ME` turns into:
2273   //
2274   //  cstexpr := <gotequiv> - <foo> + gotpcrelcst, where
2275   //    gotpcrelcst := <offset from @foo base> + <cst>
2276   //
2277   MCValue MV;
2278   if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute())
2279     return;
2280   const MCSymbolRefExpr *SymA = MV.getSymA();
2281   if (!SymA)
2282     return;
2283 
2284   // Check that GOT equivalent symbol is cached.
2285   const MCSymbol *GOTEquivSym = &SymA->getSymbol();
2286   if (!AP.GlobalGOTEquivs.count(GOTEquivSym))
2287     return;
2288 
2289   const GlobalValue *BaseGV = dyn_cast_or_null<GlobalValue>(BaseCst);
2290   if (!BaseGV)
2291     return;
2292 
2293   // Check for a valid base symbol
2294   const MCSymbol *BaseSym = AP.getSymbol(BaseGV);
2295   const MCSymbolRefExpr *SymB = MV.getSymB();
2296 
2297   if (!SymB || BaseSym != &SymB->getSymbol())
2298     return;
2299 
2300   // Make sure to match:
2301   //
2302   //    gotpcrelcst := <offset from @foo base> + <cst>
2303   //
2304   // If gotpcrelcst is positive it means that we can safely fold the pc rel
2305   // displacement into the GOTPCREL. We can also can have an extra offset <cst>
2306   // if the target knows how to encode it.
2307   //
2308   int64_t GOTPCRelCst = Offset + MV.getConstant();
2309   if (GOTPCRelCst < 0)
2310     return;
2311   if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0)
2312     return;
2313 
2314   // Emit the GOT PC relative to replace the got equivalent global, i.e.:
2315   //
2316   //  bar:
2317   //    .long 42
2318   //  gotequiv:
2319   //    .quad bar
2320   //  foo:
2321   //    .long gotequiv - "." + <cst>
2322   //
2323   // is replaced by the target specific equivalent to:
2324   //
2325   //  bar:
2326   //    .long 42
2327   //  foo:
2328   //    .long bar@GOTPCREL+<gotpcrelcst>
2329   //
2330   AsmPrinter::GOTEquivUsePair Result = AP.GlobalGOTEquivs[GOTEquivSym];
2331   const GlobalVariable *GV = Result.first;
2332   int NumUses = (int)Result.second;
2333   const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0));
2334   const MCSymbol *FinalSym = AP.getSymbol(FinalGV);
2335   *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel(
2336       FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
2337 
2338   // Update GOT equivalent usage information
2339   --NumUses;
2340   if (NumUses >= 0)
2341     AP.GlobalGOTEquivs[GOTEquivSym] = std::make_pair(GV, NumUses);
2342 }
2343 
2344 static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
2345                                    AsmPrinter &AP, const Constant *BaseCV,
2346                                    uint64_t Offset) {
2347   uint64_t Size = DL.getTypeAllocSize(CV->getType());
2348 
2349   // Globals with sub-elements such as combinations of arrays and structs
2350   // are handled recursively by emitGlobalConstantImpl. Keep track of the
2351   // constant symbol base and the current position with BaseCV and Offset.
2352   if (!BaseCV && CV->hasOneUse())
2353     BaseCV = dyn_cast<Constant>(CV->user_back());
2354 
2355   if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
2356     return AP.OutStreamer->EmitZeros(Size);
2357 
2358   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
2359     switch (Size) {
2360     case 1:
2361     case 2:
2362     case 4:
2363     case 8:
2364       if (AP.isVerbose())
2365         AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
2366                                                  CI->getZExtValue());
2367       AP.OutStreamer->EmitIntValue(CI->getZExtValue(), Size);
2368       return;
2369     default:
2370       emitGlobalConstantLargeInt(CI, AP);
2371       return;
2372     }
2373   }
2374 
2375   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
2376     return emitGlobalConstantFP(CFP, AP);
2377 
2378   if (isa<ConstantPointerNull>(CV)) {
2379     AP.OutStreamer->EmitIntValue(0, Size);
2380     return;
2381   }
2382 
2383   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV))
2384     return emitGlobalConstantDataSequential(DL, CDS, AP);
2385 
2386   if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
2387     return emitGlobalConstantArray(DL, CVA, AP, BaseCV, Offset);
2388 
2389   if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
2390     return emitGlobalConstantStruct(DL, CVS, AP, BaseCV, Offset);
2391 
2392   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
2393     // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of
2394     // vectors).
2395     if (CE->getOpcode() == Instruction::BitCast)
2396       return emitGlobalConstantImpl(DL, CE->getOperand(0), AP);
2397 
2398     if (Size > 8) {
2399       // If the constant expression's size is greater than 64-bits, then we have
2400       // to emit the value in chunks. Try to constant fold the value and emit it
2401       // that way.
2402       Constant *New = ConstantFoldConstant(CE, DL);
2403       if (New && New != CE)
2404         return emitGlobalConstantImpl(DL, New, AP);
2405     }
2406   }
2407 
2408   if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
2409     return emitGlobalConstantVector(DL, V, AP);
2410 
2411   // Otherwise, it must be a ConstantExpr.  Lower it to an MCExpr, then emit it
2412   // thread the streamer with EmitValue.
2413   const MCExpr *ME = AP.lowerConstant(CV);
2414 
2415   // Since lowerConstant already folded and got rid of all IR pointer and
2416   // integer casts, detect GOT equivalent accesses by looking into the MCExpr
2417   // directly.
2418   if (AP.getObjFileLowering().supportIndirectSymViaGOTPCRel())
2419     handleIndirectSymViaGOTPCRel(AP, &ME, BaseCV, Offset);
2420 
2421   AP.OutStreamer->EmitValue(ME, Size);
2422 }
2423 
2424 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
2425 void AsmPrinter::EmitGlobalConstant(const DataLayout &DL, const Constant *CV) {
2426   uint64_t Size = DL.getTypeAllocSize(CV->getType());
2427   if (Size)
2428     emitGlobalConstantImpl(DL, CV, *this);
2429   else if (MAI->hasSubsectionsViaSymbols()) {
2430     // If the global has zero size, emit a single byte so that two labels don't
2431     // look like they are at the same location.
2432     OutStreamer->EmitIntValue(0, 1);
2433   }
2434 }
2435 
2436 void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
2437   // Target doesn't support this yet!
2438   llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
2439 }
2440 
2441 void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
2442   if (Offset > 0)
2443     OS << '+' << Offset;
2444   else if (Offset < 0)
2445     OS << Offset;
2446 }
2447 
2448 //===----------------------------------------------------------------------===//
2449 // Symbol Lowering Routines.
2450 //===----------------------------------------------------------------------===//
2451 
2452 MCSymbol *AsmPrinter::createTempSymbol(const Twine &Name) const {
2453   return OutContext.createTempSymbol(Name, true);
2454 }
2455 
2456 MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
2457   return MMI->getAddrLabelSymbol(BA->getBasicBlock());
2458 }
2459 
2460 MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
2461   return MMI->getAddrLabelSymbol(BB);
2462 }
2463 
2464 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
2465 MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
2466   const DataLayout &DL = getDataLayout();
2467   return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
2468                                       "CPI" + Twine(getFunctionNumber()) + "_" +
2469                                       Twine(CPID));
2470 }
2471 
2472 /// GetJTISymbol - Return the symbol for the specified jump table entry.
2473 MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
2474   return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
2475 }
2476 
2477 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
2478 /// FIXME: privatize to AsmPrinter.
2479 MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
2480   const DataLayout &DL = getDataLayout();
2481   return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
2482                                       Twine(getFunctionNumber()) + "_" +
2483                                       Twine(UID) + "_set_" + Twine(MBBID));
2484 }
2485 
2486 MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
2487                                                    StringRef Suffix) const {
2488   return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, TM);
2489 }
2490 
2491 /// Return the MCSymbol for the specified ExternalSymbol.
2492 MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
2493   SmallString<60> NameStr;
2494   Mangler::getNameWithPrefix(NameStr, Sym, getDataLayout());
2495   return OutContext.getOrCreateSymbol(NameStr);
2496 }
2497 
2498 /// PrintParentLoopComment - Print comments about parent loops of this one.
2499 static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2500                                    unsigned FunctionNumber) {
2501   if (!Loop) return;
2502   PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
2503   OS.indent(Loop->getLoopDepth()*2)
2504     << "Parent Loop BB" << FunctionNumber << "_"
2505     << Loop->getHeader()->getNumber()
2506     << " Depth=" << Loop->getLoopDepth() << '\n';
2507 }
2508 
2509 
2510 /// PrintChildLoopComment - Print comments about child loops within
2511 /// the loop for this basic block, with nesting.
2512 static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
2513                                   unsigned FunctionNumber) {
2514   // Add child loop information
2515   for (const MachineLoop *CL : *Loop) {
2516     OS.indent(CL->getLoopDepth()*2)
2517       << "Child Loop BB" << FunctionNumber << "_"
2518       << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth()
2519       << '\n';
2520     PrintChildLoopComment(OS, CL, FunctionNumber);
2521   }
2522 }
2523 
2524 /// emitBasicBlockLoopComments - Pretty-print comments for basic blocks.
2525 static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
2526                                        const MachineLoopInfo *LI,
2527                                        const AsmPrinter &AP) {
2528   // Add loop depth information
2529   const MachineLoop *Loop = LI->getLoopFor(&MBB);
2530   if (!Loop) return;
2531 
2532   MachineBasicBlock *Header = Loop->getHeader();
2533   assert(Header && "No header for loop");
2534 
2535   // If this block is not a loop header, just print out what is the loop header
2536   // and return.
2537   if (Header != &MBB) {
2538     AP.OutStreamer->AddComment("  in Loop: Header=BB" +
2539                                Twine(AP.getFunctionNumber())+"_" +
2540                                Twine(Loop->getHeader()->getNumber())+
2541                                " Depth="+Twine(Loop->getLoopDepth()));
2542     return;
2543   }
2544 
2545   // Otherwise, it is a loop header.  Print out information about child and
2546   // parent loops.
2547   raw_ostream &OS = AP.OutStreamer->GetCommentOS();
2548 
2549   PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
2550 
2551   OS << "=>";
2552   OS.indent(Loop->getLoopDepth()*2-2);
2553 
2554   OS << "This ";
2555   if (Loop->empty())
2556     OS << "Inner ";
2557   OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
2558 
2559   PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
2560 }
2561 
2562 /// EmitBasicBlockStart - This method prints the label for the specified
2563 /// MachineBasicBlock, an alignment (if present) and a comment describing
2564 /// it if appropriate.
2565 void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
2566   // End the previous funclet and start a new one.
2567   if (MBB.isEHFuncletEntry()) {
2568     for (const HandlerInfo &HI : Handlers) {
2569       HI.Handler->endFunclet();
2570       HI.Handler->beginFunclet(MBB);
2571     }
2572   }
2573 
2574   // Emit an alignment directive for this block, if needed.
2575   if (unsigned Align = MBB.getAlignment())
2576     EmitAlignment(Align);
2577 
2578   // If the block has its address taken, emit any labels that were used to
2579   // reference the block.  It is possible that there is more than one label
2580   // here, because multiple LLVM BB's may have been RAUW'd to this block after
2581   // the references were generated.
2582   if (MBB.hasAddressTaken()) {
2583     const BasicBlock *BB = MBB.getBasicBlock();
2584     if (isVerbose())
2585       OutStreamer->AddComment("Block address taken");
2586 
2587     // MBBs can have their address taken as part of CodeGen without having
2588     // their corresponding BB's address taken in IR
2589     if (BB->hasAddressTaken())
2590       for (MCSymbol *Sym : MMI->getAddrLabelSymbolToEmit(BB))
2591         OutStreamer->EmitLabel(Sym);
2592   }
2593 
2594   // Print some verbose block comments.
2595   if (isVerbose()) {
2596     if (const BasicBlock *BB = MBB.getBasicBlock()) {
2597       if (BB->hasName()) {
2598         BB->printAsOperand(OutStreamer->GetCommentOS(),
2599                            /*PrintType=*/false, BB->getModule());
2600         OutStreamer->GetCommentOS() << '\n';
2601       }
2602     }
2603     emitBasicBlockLoopComments(MBB, LI, *this);
2604   }
2605 
2606   // Print the main label for the block.
2607   if (MBB.pred_empty() ||
2608       (isBlockOnlyReachableByFallthrough(&MBB) && !MBB.isEHFuncletEntry())) {
2609     if (isVerbose()) {
2610       // NOTE: Want this comment at start of line, don't emit with AddComment.
2611       OutStreamer->emitRawComment(" BB#" + Twine(MBB.getNumber()) + ":", false);
2612     }
2613   } else {
2614     OutStreamer->EmitLabel(MBB.getSymbol());
2615   }
2616 }
2617 
2618 void AsmPrinter::EmitVisibility(MCSymbol *Sym, unsigned Visibility,
2619                                 bool IsDefinition) const {
2620   MCSymbolAttr Attr = MCSA_Invalid;
2621 
2622   switch (Visibility) {
2623   default: break;
2624   case GlobalValue::HiddenVisibility:
2625     if (IsDefinition)
2626       Attr = MAI->getHiddenVisibilityAttr();
2627     else
2628       Attr = MAI->getHiddenDeclarationVisibilityAttr();
2629     break;
2630   case GlobalValue::ProtectedVisibility:
2631     Attr = MAI->getProtectedVisibilityAttr();
2632     break;
2633   }
2634 
2635   if (Attr != MCSA_Invalid)
2636     OutStreamer->EmitSymbolAttribute(Sym, Attr);
2637 }
2638 
2639 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
2640 /// exactly one predecessor and the control transfer mechanism between
2641 /// the predecessor and this block is a fall-through.
2642 bool AsmPrinter::
2643 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
2644   // If this is a landing pad, it isn't a fall through.  If it has no preds,
2645   // then nothing falls through to it.
2646   if (MBB->isEHPad() || MBB->pred_empty())
2647     return false;
2648 
2649   // If there isn't exactly one predecessor, it can't be a fall through.
2650   if (MBB->pred_size() > 1)
2651     return false;
2652 
2653   // The predecessor has to be immediately before this block.
2654   MachineBasicBlock *Pred = *MBB->pred_begin();
2655   if (!Pred->isLayoutSuccessor(MBB))
2656     return false;
2657 
2658   // If the block is completely empty, then it definitely does fall through.
2659   if (Pred->empty())
2660     return true;
2661 
2662   // Check the terminators in the previous blocks
2663   for (const auto &MI : Pred->terminators()) {
2664     // If it is not a simple branch, we are in a table somewhere.
2665     if (!MI.isBranch() || MI.isIndirectBranch())
2666       return false;
2667 
2668     // If we are the operands of one of the branches, this is not a fall
2669     // through. Note that targets with delay slots will usually bundle
2670     // terminators with the delay slot instruction.
2671     for (ConstMIBundleOperands OP(MI); OP.isValid(); ++OP) {
2672       if (OP->isJTI())
2673         return false;
2674       if (OP->isMBB() && OP->getMBB() == MBB)
2675         return false;
2676     }
2677   }
2678 
2679   return true;
2680 }
2681 
2682 GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
2683   if (!S.usesMetadata())
2684     return nullptr;
2685 
2686   assert(!S.useStatepoints() && "statepoints do not currently support custom"
2687          " stackmap formats, please see the documentation for a description of"
2688          " the default format.  If you really need a custom serialized format,"
2689          " please file a bug");
2690 
2691   gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
2692   gcp_map_type::iterator GCPI = GCMap.find(&S);
2693   if (GCPI != GCMap.end())
2694     return GCPI->second.get();
2695 
2696   auto Name = S.getName();
2697 
2698   for (GCMetadataPrinterRegistry::iterator
2699          I = GCMetadataPrinterRegistry::begin(),
2700          E = GCMetadataPrinterRegistry::end(); I != E; ++I)
2701     if (Name == I->getName()) {
2702       std::unique_ptr<GCMetadataPrinter> GMP = I->instantiate();
2703       GMP->S = &S;
2704       auto IterBool = GCMap.insert(std::make_pair(&S, std::move(GMP)));
2705       return IterBool.first->second.get();
2706     }
2707 
2708   report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
2709 }
2710 
2711 /// Pin vtable to this file.
2712 AsmPrinterHandler::~AsmPrinterHandler() = default;
2713 
2714 void AsmPrinterHandler::markFunctionEnd() {}
2715 
2716 // In the binary's "xray_instr_map" section, an array of these function entries
2717 // describes each instrumentation point.  When XRay patches your code, the index
2718 // into this table will be given to your handler as a patch point identifier.
2719 void AsmPrinter::XRayFunctionEntry::emit(int Bytes, MCStreamer *Out,
2720                                          const MCSymbol *CurrentFnSym) const {
2721   Out->EmitSymbolValue(Sled, Bytes);
2722   Out->EmitSymbolValue(CurrentFnSym, Bytes);
2723   auto Kind8 = static_cast<uint8_t>(Kind);
2724   Out->EmitBytes(StringRef(reinterpret_cast<const char *>(&Kind8), 1));
2725   Out->EmitBytes(
2726       StringRef(reinterpret_cast<const char *>(&AlwaysInstrument), 1));
2727   Out->EmitZeros(2 * Bytes - 2);  // Pad the previous two entries
2728 }
2729 
2730 void AsmPrinter::emitXRayTable() {
2731   if (Sleds.empty())
2732     return;
2733 
2734   auto PrevSection = OutStreamer->getCurrentSectionOnly();
2735   auto Fn = MF->getFunction();
2736   MCSection *Section = nullptr;
2737   if (MF->getSubtarget().getTargetTriple().isOSBinFormatELF()) {
2738     if (Fn->hasComdat()) {
2739       Section = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
2740                                          ELF::SHF_ALLOC | ELF::SHF_GROUP, 0,
2741                                          Fn->getComdat()->getName());
2742     } else {
2743       Section = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
2744                                          ELF::SHF_ALLOC);
2745     }
2746   } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) {
2747     Section = OutContext.getMachOSection("__DATA", "xray_instr_map", 0,
2748                                          SectionKind::getReadOnlyWithRel());
2749   } else {
2750     llvm_unreachable("Unsupported target");
2751   }
2752 
2753   // Before we switch over, we force a reference to a label inside the
2754   // xray_instr_map section. Since this function is always called just
2755   // before the function's end, we assume that this is happening after
2756   // the last return instruction.
2757 
2758   auto WordSizeBytes = TM.getPointerSize();
2759   MCSymbol *Tmp = OutContext.createTempSymbol("xray_synthetic_", true);
2760   OutStreamer->EmitCodeAlignment(16);
2761   OutStreamer->EmitSymbolValue(Tmp, WordSizeBytes, false);
2762   OutStreamer->SwitchSection(Section);
2763   OutStreamer->EmitLabel(Tmp);
2764   for (const auto &Sled : Sleds)
2765     Sled.emit(WordSizeBytes, OutStreamer.get(), CurrentFnSym);
2766 
2767   OutStreamer->SwitchSection(PrevSection);
2768   Sleds.clear();
2769 }
2770 
2771 void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI,
2772   SledKind Kind) {
2773   auto Fn = MI.getParent()->getParent()->getFunction();
2774   auto Attr = Fn->getFnAttribute("function-instrument");
2775   bool LogArgs = Fn->hasFnAttribute("xray-log-args");
2776   bool AlwaysInstrument =
2777     Attr.isStringAttribute() && Attr.getValueAsString() == "xray-always";
2778   if (Kind == SledKind::FUNCTION_ENTER && LogArgs)
2779     Kind = SledKind::LOG_ARGS_ENTER;
2780   Sleds.emplace_back(
2781     XRayFunctionEntry{ Sled, CurrentFnSym, Kind, AlwaysInstrument, Fn });
2782 }
2783 
2784 uint16_t AsmPrinter::getDwarfVersion() const {
2785   return OutStreamer->getContext().getDwarfVersion();
2786 }
2787 
2788 void AsmPrinter::setDwarfVersion(uint16_t Version) {
2789   OutStreamer->getContext().setDwarfVersion(Version);
2790 }
2791