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