xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 "PseudoProbePrinter.h"
18 #include "WasmException.h"
19 #include "WinCFGuard.h"
20 #include "WinException.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/TinyPtrVector.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/Analysis/ConstantFolding.h"
34 #include "llvm/Analysis/EHPersonalities.h"
35 #include "llvm/Analysis/MemoryLocation.h"
36 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
37 #include "llvm/BinaryFormat/COFF.h"
38 #include "llvm/BinaryFormat/Dwarf.h"
39 #include "llvm/BinaryFormat/ELF.h"
40 #include "llvm/CodeGen/GCMetadata.h"
41 #include "llvm/CodeGen/GCMetadataPrinter.h"
42 #include "llvm/CodeGen/MachineBasicBlock.h"
43 #include "llvm/CodeGen/MachineConstantPool.h"
44 #include "llvm/CodeGen/MachineDominators.h"
45 #include "llvm/CodeGen/MachineFrameInfo.h"
46 #include "llvm/CodeGen/MachineFunction.h"
47 #include "llvm/CodeGen/MachineFunctionPass.h"
48 #include "llvm/CodeGen/MachineInstr.h"
49 #include "llvm/CodeGen/MachineInstrBundle.h"
50 #include "llvm/CodeGen/MachineJumpTableInfo.h"
51 #include "llvm/CodeGen/MachineLoopInfo.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/Config/config.h"
63 #include "llvm/IR/BasicBlock.h"
64 #include "llvm/IR/Comdat.h"
65 #include "llvm/IR/Constant.h"
66 #include "llvm/IR/Constants.h"
67 #include "llvm/IR/DataLayout.h"
68 #include "llvm/IR/DebugInfoMetadata.h"
69 #include "llvm/IR/DerivedTypes.h"
70 #include "llvm/IR/Function.h"
71 #include "llvm/IR/GCStrategy.h"
72 #include "llvm/IR/GlobalAlias.h"
73 #include "llvm/IR/GlobalIFunc.h"
74 #include "llvm/IR/GlobalObject.h"
75 #include "llvm/IR/GlobalValue.h"
76 #include "llvm/IR/GlobalVariable.h"
77 #include "llvm/IR/Instruction.h"
78 #include "llvm/IR/Mangler.h"
79 #include "llvm/IR/Metadata.h"
80 #include "llvm/IR/Module.h"
81 #include "llvm/IR/Operator.h"
82 #include "llvm/IR/PseudoProbe.h"
83 #include "llvm/IR/Type.h"
84 #include "llvm/IR/Value.h"
85 #include "llvm/IR/ValueHandle.h"
86 #include "llvm/MC/MCAsmInfo.h"
87 #include "llvm/MC/MCContext.h"
88 #include "llvm/MC/MCDirectives.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/RemarkStreamer.h"
104 #include "llvm/Support/Casting.h"
105 #include "llvm/Support/Compiler.h"
106 #include "llvm/Support/ErrorHandling.h"
107 #include "llvm/Support/FileSystem.h"
108 #include "llvm/Support/Format.h"
109 #include "llvm/Support/MathExtras.h"
110 #include "llvm/Support/Path.h"
111 #include "llvm/Support/Timer.h"
112 #include "llvm/Support/raw_ostream.h"
113 #include "llvm/Target/TargetLoweringObjectFile.h"
114 #include "llvm/Target/TargetMachine.h"
115 #include "llvm/Target/TargetOptions.h"
116 #include <algorithm>
117 #include <cassert>
118 #include <cinttypes>
119 #include <cstdint>
120 #include <iterator>
121 #include <memory>
122 #include <optional>
123 #include <string>
124 #include <utility>
125 #include <vector>
126 
127 using namespace llvm;
128 
129 #define DEBUG_TYPE "asm-printer"
130 
131 const char DWARFGroupName[] = "dwarf";
132 const char DWARFGroupDescription[] = "DWARF Emission";
133 const char DbgTimerName[] = "emit";
134 const char DbgTimerDescription[] = "Debug Info Emission";
135 const char EHTimerName[] = "write_exception";
136 const char EHTimerDescription[] = "DWARF Exception Writer";
137 const char CFGuardName[] = "Control Flow Guard";
138 const char CFGuardDescription[] = "Control Flow Guard";
139 const char CodeViewLineTablesGroupName[] = "linetables";
140 const char CodeViewLineTablesGroupDescription[] = "CodeView Line Tables";
141 const char PPTimerName[] = "emit";
142 const char PPTimerDescription[] = "Pseudo Probe Emission";
143 const char PPGroupName[] = "pseudo probe";
144 const char PPGroupDescription[] = "Pseudo Probe Emission";
145 
146 STATISTIC(EmittedInsts, "Number of machine instrs printed");
147 
148 char AsmPrinter::ID = 0;
149 
150 namespace {
151 class AddrLabelMapCallbackPtr final : CallbackVH {
152   AddrLabelMap *Map = nullptr;
153 
154 public:
155   AddrLabelMapCallbackPtr() = default;
156   AddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {}
157 
158   void setPtr(BasicBlock *BB) {
159     ValueHandleBase::operator=(BB);
160   }
161 
162   void setMap(AddrLabelMap *map) { Map = map; }
163 
164   void deleted() override;
165   void allUsesReplacedWith(Value *V2) override;
166 };
167 } // namespace
168 
169 class llvm::AddrLabelMap {
170   MCContext &Context;
171   struct AddrLabelSymEntry {
172     /// The symbols for the label.
173     TinyPtrVector<MCSymbol *> Symbols;
174 
175     Function *Fn;   // The containing function of the BasicBlock.
176     unsigned Index; // The index in BBCallbacks for the BasicBlock.
177   };
178 
179   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
180 
181   /// Callbacks for the BasicBlock's that we have entries for.  We use this so
182   /// we get notified if a block is deleted or RAUWd.
183   std::vector<AddrLabelMapCallbackPtr> BBCallbacks;
184 
185   /// This is a per-function list of symbols whose corresponding BasicBlock got
186   /// deleted.  These symbols need to be emitted at some point in the file, so
187   /// AsmPrinter emits them after the function body.
188   DenseMap<AssertingVH<Function>, std::vector<MCSymbol *>>
189       DeletedAddrLabelsNeedingEmission;
190 
191 public:
192   AddrLabelMap(MCContext &context) : Context(context) {}
193 
194   ~AddrLabelMap() {
195     assert(DeletedAddrLabelsNeedingEmission.empty() &&
196            "Some labels for deleted blocks never got emitted");
197   }
198 
199   ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
200 
201   void takeDeletedSymbolsForFunction(Function *F,
202                                      std::vector<MCSymbol *> &Result);
203 
204   void UpdateForDeletedBlock(BasicBlock *BB);
205   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
206 };
207 
208 ArrayRef<MCSymbol *> AddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
209   assert(BB->hasAddressTaken() &&
210          "Shouldn't get label for block without address taken");
211   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
212 
213   // If we already had an entry for this block, just return it.
214   if (!Entry.Symbols.empty()) {
215     assert(BB->getParent() == Entry.Fn && "Parent changed");
216     return Entry.Symbols;
217   }
218 
219   // Otherwise, this is a new entry, create a new symbol for it and add an
220   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
221   BBCallbacks.emplace_back(BB);
222   BBCallbacks.back().setMap(this);
223   Entry.Index = BBCallbacks.size() - 1;
224   Entry.Fn = BB->getParent();
225   MCSymbol *Sym = BB->hasAddressTaken() ? Context.createNamedTempSymbol()
226                                         : Context.createTempSymbol();
227   Entry.Symbols.push_back(Sym);
228   return Entry.Symbols;
229 }
230 
231 /// If we have any deleted symbols for F, return them.
232 void AddrLabelMap::takeDeletedSymbolsForFunction(
233     Function *F, std::vector<MCSymbol *> &Result) {
234   DenseMap<AssertingVH<Function>, std::vector<MCSymbol *>>::iterator I =
235       DeletedAddrLabelsNeedingEmission.find(F);
236 
237   // If there are no entries for the function, just return.
238   if (I == DeletedAddrLabelsNeedingEmission.end())
239     return;
240 
241   // Otherwise, take the list.
242   std::swap(Result, I->second);
243   DeletedAddrLabelsNeedingEmission.erase(I);
244 }
245 
246 //===- Address of Block Management ----------------------------------------===//
247 
248 ArrayRef<MCSymbol *>
249 AsmPrinter::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
250   // Lazily create AddrLabelSymbols.
251   if (!AddrLabelSymbols)
252     AddrLabelSymbols = std::make_unique<AddrLabelMap>(OutContext);
253   return AddrLabelSymbols->getAddrLabelSymbolToEmit(
254       const_cast<BasicBlock *>(BB));
255 }
256 
257 void AsmPrinter::takeDeletedSymbolsForFunction(
258     const Function *F, std::vector<MCSymbol *> &Result) {
259   // If no blocks have had their addresses taken, we're done.
260   if (!AddrLabelSymbols)
261     return;
262   return AddrLabelSymbols->takeDeletedSymbolsForFunction(
263       const_cast<Function *>(F), Result);
264 }
265 
266 void AddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
267   // If the block got deleted, there is no need for the symbol.  If the symbol
268   // was already emitted, we can just forget about it, otherwise we need to
269   // queue it up for later emission when the function is output.
270   AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
271   AddrLabelSymbols.erase(BB);
272   assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
273   BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
274 
275 #if !LLVM_MEMORY_SANITIZER_BUILD
276   // BasicBlock is destroyed already, so this access is UB detectable by msan.
277   assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
278          "Block/parent mismatch");
279 #endif
280 
281   for (MCSymbol *Sym : Entry.Symbols) {
282     if (Sym->isDefined())
283       return;
284 
285     // If the block is not yet defined, we need to emit it at the end of the
286     // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
287     // for the containing Function.  Since the block is being deleted, its
288     // parent may already be removed, we have to get the function from 'Entry'.
289     DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
290   }
291 }
292 
293 void AddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
294   // Get the entry for the RAUW'd block and remove it from our map.
295   AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
296   AddrLabelSymbols.erase(Old);
297   assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
298 
299   AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
300 
301   // If New is not address taken, just move our symbol over to it.
302   if (NewEntry.Symbols.empty()) {
303     BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
304     NewEntry = std::move(OldEntry);          // Set New's entry.
305     return;
306   }
307 
308   BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
309 
310   // Otherwise, we need to add the old symbols to the new block's set.
311   llvm::append_range(NewEntry.Symbols, OldEntry.Symbols);
312 }
313 
314 void AddrLabelMapCallbackPtr::deleted() {
315   Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
316 }
317 
318 void AddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
319   Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
320 }
321 
322 /// getGVAlignment - Return the alignment to use for the specified global
323 /// value.  This rounds up to the preferred alignment if possible and legal.
324 Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
325                                  Align InAlign) {
326   Align Alignment;
327   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
328     Alignment = DL.getPreferredAlign(GVar);
329 
330   // If InAlign is specified, round it to it.
331   if (InAlign > Alignment)
332     Alignment = InAlign;
333 
334   // If the GV has a specified alignment, take it into account.
335   const MaybeAlign GVAlign(GV->getAlign());
336   if (!GVAlign)
337     return Alignment;
338 
339   assert(GVAlign && "GVAlign must be set");
340 
341   // If the GVAlign is larger than NumBits, or if we are required to obey
342   // NumBits because the GV has an assigned section, obey it.
343   if (*GVAlign > Alignment || GV->hasSection())
344     Alignment = *GVAlign;
345   return Alignment;
346 }
347 
348 AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
349     : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
350       OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)),
351       SM(*this) {
352   VerboseAsm = OutStreamer->isVerboseAsm();
353   DwarfUsesRelocationsAcrossSections =
354       MAI->doesDwarfUseRelocationsAcrossSections();
355 }
356 
357 AsmPrinter::~AsmPrinter() {
358   assert(!DD && Handlers.size() == NumUserHandlers &&
359          "Debug/EH info didn't get finalized");
360 }
361 
362 bool AsmPrinter::isPositionIndependent() const {
363   return TM.isPositionIndependent();
364 }
365 
366 /// getFunctionNumber - Return a unique ID for the current function.
367 unsigned AsmPrinter::getFunctionNumber() const {
368   return MF->getFunctionNumber();
369 }
370 
371 const TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
372   return *TM.getObjFileLowering();
373 }
374 
375 const DataLayout &AsmPrinter::getDataLayout() const {
376   return MMI->getModule()->getDataLayout();
377 }
378 
379 // Do not use the cached DataLayout because some client use it without a Module
380 // (dsymutil, llvm-dwarfdump).
381 unsigned AsmPrinter::getPointerSize() const {
382   return TM.getPointerSize(0); // FIXME: Default address space
383 }
384 
385 const MCSubtargetInfo &AsmPrinter::getSubtargetInfo() const {
386   assert(MF && "getSubtargetInfo requires a valid MachineFunction!");
387   return MF->getSubtarget<MCSubtargetInfo>();
388 }
389 
390 void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
391   S.emitInstruction(Inst, getSubtargetInfo());
392 }
393 
394 void AsmPrinter::emitInitialRawDwarfLocDirective(const MachineFunction &MF) {
395   if (DD) {
396     assert(OutStreamer->hasRawTextSupport() &&
397            "Expected assembly output mode.");
398     // This is NVPTX specific and it's unclear why.
399     // PR51079: If we have code without debug information we need to give up.
400     DISubprogram *MFSP = MF.getFunction().getSubprogram();
401     if (!MFSP)
402       return;
403     (void)DD->emitInitialLocDirective(MF, /*CUID=*/0);
404   }
405 }
406 
407 /// getCurrentSection() - Return the current section we are emitting to.
408 const MCSection *AsmPrinter::getCurrentSection() const {
409   return OutStreamer->getCurrentSectionOnly();
410 }
411 
412 void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
413   AU.setPreservesAll();
414   MachineFunctionPass::getAnalysisUsage(AU);
415   AU.addRequired<MachineOptimizationRemarkEmitterPass>();
416   AU.addRequired<GCModuleInfo>();
417 }
418 
419 bool AsmPrinter::doInitialization(Module &M) {
420   auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
421   MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
422   HasSplitStack = false;
423   HasNoSplitStack = false;
424 
425   AddrLabelSymbols = nullptr;
426 
427   // Initialize TargetLoweringObjectFile.
428   const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
429     .Initialize(OutContext, TM);
430 
431   const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
432       .getModuleMetadata(M);
433 
434   OutStreamer->initSections(false, *TM.getMCSubtargetInfo());
435 
436   // Emit the version-min deployment target directive if needed.
437   //
438   // FIXME: If we end up with a collection of these sorts of Darwin-specific
439   // or ELF-specific things, it may make sense to have a platform helper class
440   // that will work with the target helper class. For now keep it here, as the
441   // alternative is duplicated code in each of the target asm printers that
442   // use the directive, where it would need the same conditionalization
443   // anyway.
444   const Triple &Target = TM.getTargetTriple();
445   Triple TVT(M.getDarwinTargetVariantTriple());
446   OutStreamer->emitVersionForTarget(
447       Target, M.getSDKVersion(),
448       M.getDarwinTargetVariantTriple().empty() ? nullptr : &TVT,
449       M.getDarwinTargetVariantSDKVersion());
450 
451   // Allow the target to emit any magic that it wants at the start of the file.
452   emitStartOfAsmFile(M);
453 
454   // Very minimal debug info. It is ignored if we emit actual debug info. If we
455   // don't, this at least helps the user find where a global came from.
456   if (MAI->hasSingleParameterDotFile()) {
457     // .file "foo.c"
458 
459     SmallString<128> FileName;
460     if (MAI->hasBasenameOnlyForFileDirective())
461       FileName = llvm::sys::path::filename(M.getSourceFileName());
462     else
463       FileName = M.getSourceFileName();
464     if (MAI->hasFourStringsDotFile()) {
465 #ifdef PACKAGE_VENDOR
466       const char VerStr[] =
467           PACKAGE_VENDOR " " PACKAGE_NAME " version " PACKAGE_VERSION;
468 #else
469       const char VerStr[] = PACKAGE_NAME " version " PACKAGE_VERSION;
470 #endif
471       // TODO: Add timestamp and description.
472       OutStreamer->emitFileDirective(FileName, VerStr, "", "");
473     } else {
474       OutStreamer->emitFileDirective(FileName);
475     }
476   }
477 
478   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
479   assert(MI && "AsmPrinter didn't require GCModuleInfo?");
480   for (const auto &I : *MI)
481     if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
482       MP->beginAssembly(M, *MI, *this);
483 
484   // Emit module-level inline asm if it exists.
485   if (!M.getModuleInlineAsm().empty()) {
486     OutStreamer->AddComment("Start of file scope inline assembly");
487     OutStreamer->addBlankLine();
488     emitInlineAsm(M.getModuleInlineAsm() + "\n", *TM.getMCSubtargetInfo(),
489                   TM.Options.MCOptions);
490     OutStreamer->AddComment("End of file scope inline assembly");
491     OutStreamer->addBlankLine();
492   }
493 
494   if (MAI->doesSupportDebugInformation()) {
495     bool EmitCodeView = M.getCodeViewFlag();
496     if (EmitCodeView && TM.getTargetTriple().isOSWindows()) {
497       Handlers.emplace_back(std::make_unique<CodeViewDebug>(this),
498                             DbgTimerName, DbgTimerDescription,
499                             CodeViewLineTablesGroupName,
500                             CodeViewLineTablesGroupDescription);
501     }
502     if (!EmitCodeView || M.getDwarfVersion()) {
503       if (MMI->hasDebugInfo()) {
504         DD = new DwarfDebug(this);
505         Handlers.emplace_back(std::unique_ptr<DwarfDebug>(DD), DbgTimerName,
506                               DbgTimerDescription, DWARFGroupName,
507                               DWARFGroupDescription);
508       }
509     }
510   }
511 
512   if (M.getNamedMetadata(PseudoProbeDescMetadataName)) {
513     PP = new PseudoProbeHandler(this);
514     Handlers.emplace_back(std::unique_ptr<PseudoProbeHandler>(PP), PPTimerName,
515                           PPTimerDescription, PPGroupName, PPGroupDescription);
516   }
517 
518   switch (MAI->getExceptionHandlingType()) {
519   case ExceptionHandling::None:
520     // We may want to emit CFI for debug.
521     [[fallthrough]];
522   case ExceptionHandling::SjLj:
523   case ExceptionHandling::DwarfCFI:
524   case ExceptionHandling::ARM:
525     for (auto &F : M.getFunctionList()) {
526       if (getFunctionCFISectionType(F) != CFISection::None)
527         ModuleCFISection = getFunctionCFISectionType(F);
528       // If any function needsUnwindTableEntry(), it needs .eh_frame and hence
529       // the module needs .eh_frame. If we have found that case, we are done.
530       if (ModuleCFISection == CFISection::EH)
531         break;
532     }
533     assert(MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI ||
534            ModuleCFISection != CFISection::EH);
535     break;
536   default:
537     break;
538   }
539 
540   EHStreamer *ES = nullptr;
541   switch (MAI->getExceptionHandlingType()) {
542   case ExceptionHandling::None:
543     if (!needsCFIForDebug())
544       break;
545     [[fallthrough]];
546   case ExceptionHandling::SjLj:
547   case ExceptionHandling::DwarfCFI:
548     ES = new DwarfCFIException(this);
549     break;
550   case ExceptionHandling::ARM:
551     ES = new ARMException(this);
552     break;
553   case ExceptionHandling::WinEH:
554     switch (MAI->getWinEHEncodingType()) {
555     default: llvm_unreachable("unsupported unwinding information encoding");
556     case WinEH::EncodingType::Invalid:
557       break;
558     case WinEH::EncodingType::X86:
559     case WinEH::EncodingType::Itanium:
560       ES = new WinException(this);
561       break;
562     }
563     break;
564   case ExceptionHandling::Wasm:
565     ES = new WasmException(this);
566     break;
567   case ExceptionHandling::AIX:
568     ES = new AIXException(this);
569     break;
570   }
571   if (ES)
572     Handlers.emplace_back(std::unique_ptr<EHStreamer>(ES), EHTimerName,
573                           EHTimerDescription, DWARFGroupName,
574                           DWARFGroupDescription);
575 
576   // Emit tables for any value of cfguard flag (i.e. cfguard=1 or cfguard=2).
577   if (mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
578     Handlers.emplace_back(std::make_unique<WinCFGuard>(this), CFGuardName,
579                           CFGuardDescription, DWARFGroupName,
580                           DWARFGroupDescription);
581 
582   for (const HandlerInfo &HI : Handlers) {
583     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
584                        HI.TimerGroupDescription, TimePassesIsEnabled);
585     HI.Handler->beginModule(&M);
586   }
587 
588   return false;
589 }
590 
591 static bool canBeHidden(const GlobalValue *GV, const MCAsmInfo &MAI) {
592   if (!MAI.hasWeakDefCanBeHiddenDirective())
593     return false;
594 
595   return GV->canBeOmittedFromSymbolTable();
596 }
597 
598 void AsmPrinter::emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
599   GlobalValue::LinkageTypes Linkage = GV->getLinkage();
600   switch (Linkage) {
601   case GlobalValue::CommonLinkage:
602   case GlobalValue::LinkOnceAnyLinkage:
603   case GlobalValue::LinkOnceODRLinkage:
604   case GlobalValue::WeakAnyLinkage:
605   case GlobalValue::WeakODRLinkage:
606     if (MAI->hasWeakDefDirective()) {
607       // .globl _foo
608       OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global);
609 
610       if (!canBeHidden(GV, *MAI))
611         // .weak_definition _foo
612         OutStreamer->emitSymbolAttribute(GVSym, MCSA_WeakDefinition);
613       else
614         OutStreamer->emitSymbolAttribute(GVSym, MCSA_WeakDefAutoPrivate);
615     } else if (MAI->avoidWeakIfComdat() && GV->hasComdat()) {
616       // .globl _foo
617       OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global);
618       //NOTE: linkonce is handled by the section the symbol was assigned to.
619     } else {
620       // .weak _foo
621       OutStreamer->emitSymbolAttribute(GVSym, MCSA_Weak);
622     }
623     return;
624   case GlobalValue::ExternalLinkage:
625     OutStreamer->emitSymbolAttribute(GVSym, MCSA_Global);
626     return;
627   case GlobalValue::PrivateLinkage:
628   case GlobalValue::InternalLinkage:
629     return;
630   case GlobalValue::ExternalWeakLinkage:
631   case GlobalValue::AvailableExternallyLinkage:
632   case GlobalValue::AppendingLinkage:
633     llvm_unreachable("Should never emit this");
634   }
635   llvm_unreachable("Unknown linkage type!");
636 }
637 
638 void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
639                                    const GlobalValue *GV) const {
640   TM.getNameWithPrefix(Name, GV, getObjFileLowering().getMangler());
641 }
642 
643 MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
644   return TM.getSymbol(GV);
645 }
646 
647 MCSymbol *AsmPrinter::getSymbolPreferLocal(const GlobalValue &GV) const {
648   // On ELF, use .Lfoo$local if GV is a non-interposable GlobalObject with an
649   // exact definion (intersection of GlobalValue::hasExactDefinition() and
650   // !isInterposable()). These linkages include: external, appending, internal,
651   // private. It may be profitable to use a local alias for external. The
652   // assembler would otherwise be conservative and assume a global default
653   // visibility symbol can be interposable, even if the code generator already
654   // assumed it.
655   if (TM.getTargetTriple().isOSBinFormatELF() && GV.canBenefitFromLocalAlias()) {
656     const Module &M = *GV.getParent();
657     if (TM.getRelocationModel() != Reloc::Static &&
658         M.getPIELevel() == PIELevel::Default && GV.isDSOLocal())
659       return getSymbolWithGlobalValueBase(&GV, "$local");
660   }
661   return TM.getSymbol(&GV);
662 }
663 
664 /// EmitGlobalVariable - Emit the specified global variable to the .s file.
665 void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
666   bool IsEmuTLSVar = TM.useEmulatedTLS() && GV->isThreadLocal();
667   assert(!(IsEmuTLSVar && GV->hasCommonLinkage()) &&
668          "No emulated TLS variables in the common section");
669 
670   // Never emit TLS variable xyz in emulated TLS model.
671   // The initialization value is in __emutls_t.xyz instead of xyz.
672   if (IsEmuTLSVar)
673     return;
674 
675   if (GV->hasInitializer()) {
676     // Check to see if this is a special global used by LLVM, if so, emit it.
677     if (emitSpecialLLVMGlobal(GV))
678       return;
679 
680     // Skip the emission of global equivalents. The symbol can be emitted later
681     // on by emitGlobalGOTEquivs in case it turns out to be needed.
682     if (GlobalGOTEquivs.count(getSymbol(GV)))
683       return;
684 
685     if (isVerbose()) {
686       // When printing the control variable __emutls_v.*,
687       // we don't need to print the original TLS variable name.
688       GV->printAsOperand(OutStreamer->getCommentOS(),
689                          /*PrintType=*/false, GV->getParent());
690       OutStreamer->getCommentOS() << '\n';
691     }
692   }
693 
694   MCSymbol *GVSym = getSymbol(GV);
695   MCSymbol *EmittedSym = GVSym;
696 
697   // getOrCreateEmuTLSControlSym only creates the symbol with name and default
698   // attributes.
699   // GV's or GVSym's attributes will be used for the EmittedSym.
700   emitVisibility(EmittedSym, GV->getVisibility(), !GV->isDeclaration());
701 
702   if (GV->isTagged()) {
703     Triple T = TM.getTargetTriple();
704 
705     if (T.getArch() != Triple::aarch64 || !T.isAndroid())
706       OutContext.reportError(SMLoc(),
707                              "Tagged symbols (-fsanitize=memtag-globals) are "
708                              "only supported on aarch64 + Android.");
709     OutStreamer->emitSymbolAttribute(EmittedSym, MAI->getMemtagAttr());
710   }
711 
712   if (!GV->hasInitializer())   // External globals require no extra code.
713     return;
714 
715   GVSym->redefineIfPossible();
716   if (GVSym->isDefined() || GVSym->isVariable())
717     OutContext.reportError(SMLoc(), "symbol '" + Twine(GVSym->getName()) +
718                                         "' is already defined");
719 
720   if (MAI->hasDotTypeDotSizeDirective())
721     OutStreamer->emitSymbolAttribute(EmittedSym, MCSA_ELF_TypeObject);
722 
723   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
724 
725   const DataLayout &DL = GV->getParent()->getDataLayout();
726   uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
727 
728   // If the alignment is specified, we *must* obey it.  Overaligning a global
729   // with a specified alignment is a prompt way to break globals emitted to
730   // sections and expected to be contiguous (e.g. ObjC metadata).
731   const Align Alignment = getGVAlignment(GV, DL);
732 
733   for (const HandlerInfo &HI : Handlers) {
734     NamedRegionTimer T(HI.TimerName, HI.TimerDescription,
735                        HI.TimerGroupName, HI.TimerGroupDescription,
736                        TimePassesIsEnabled);
737     HI.Handler->setSymbolSize(GVSym, Size);
738   }
739 
740   // Handle common symbols
741   if (GVKind.isCommon()) {
742     if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
743     // .comm _foo, 42, 4
744     OutStreamer->emitCommonSymbol(GVSym, Size, Alignment);
745     return;
746   }
747 
748   // Determine to which section this global should be emitted.
749   MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM);
750 
751   // If we have a bss global going to a section that supports the
752   // zerofill directive, do so here.
753   if (GVKind.isBSS() && MAI->hasMachoZeroFillDirective() &&
754       TheSection->isVirtualSection()) {
755     if (Size == 0)
756       Size = 1; // zerofill of 0 bytes is undefined.
757     emitLinkage(GV, GVSym);
758     // .zerofill __DATA, __bss, _foo, 400, 5
759     OutStreamer->emitZerofill(TheSection, GVSym, Size, Alignment);
760     return;
761   }
762 
763   // If this is a BSS local symbol and we are emitting in the BSS
764   // section use .lcomm/.comm directive.
765   if (GVKind.isBSSLocal() &&
766       getObjFileLowering().getBSSSection() == TheSection) {
767     if (Size == 0)
768       Size = 1; // .comm Foo, 0 is undefined, avoid it.
769 
770     // Use .lcomm only if it supports user-specified alignment.
771     // Otherwise, while it would still be correct to use .lcomm in some
772     // cases (e.g. when Align == 1), the external assembler might enfore
773     // some -unknown- default alignment behavior, which could cause
774     // spurious differences between external and integrated assembler.
775     // Prefer to simply fall back to .local / .comm in this case.
776     if (MAI->getLCOMMDirectiveAlignmentType() != LCOMM::NoAlignment) {
777       // .lcomm _foo, 42
778       OutStreamer->emitLocalCommonSymbol(GVSym, Size, Alignment);
779       return;
780     }
781 
782     // .local _foo
783     OutStreamer->emitSymbolAttribute(GVSym, MCSA_Local);
784     // .comm _foo, 42, 4
785     OutStreamer->emitCommonSymbol(GVSym, Size, Alignment);
786     return;
787   }
788 
789   // Handle thread local data for mach-o which requires us to output an
790   // additional structure of data and mangle the original symbol so that we
791   // can reference it later.
792   //
793   // TODO: This should become an "emit thread local global" method on TLOF.
794   // All of this macho specific stuff should be sunk down into TLOFMachO and
795   // stuff like "TLSExtraDataSection" should no longer be part of the parent
796   // TLOF class.  This will also make it more obvious that stuff like
797   // MCStreamer::EmitTBSSSymbol is macho specific and only called from macho
798   // specific code.
799   if (GVKind.isThreadLocal() && MAI->hasMachoTBSSDirective()) {
800     // Emit the .tbss symbol
801     MCSymbol *MangSym =
802         OutContext.getOrCreateSymbol(GVSym->getName() + Twine("$tlv$init"));
803 
804     if (GVKind.isThreadBSS()) {
805       TheSection = getObjFileLowering().getTLSBSSSection();
806       OutStreamer->emitTBSSSymbol(TheSection, MangSym, Size, Alignment);
807     } else if (GVKind.isThreadData()) {
808       OutStreamer->switchSection(TheSection);
809 
810       emitAlignment(Alignment, GV);
811       OutStreamer->emitLabel(MangSym);
812 
813       emitGlobalConstant(GV->getParent()->getDataLayout(),
814                          GV->getInitializer());
815     }
816 
817     OutStreamer->addBlankLine();
818 
819     // Emit the variable struct for the runtime.
820     MCSection *TLVSect = getObjFileLowering().getTLSExtraDataSection();
821 
822     OutStreamer->switchSection(TLVSect);
823     // Emit the linkage here.
824     emitLinkage(GV, GVSym);
825     OutStreamer->emitLabel(GVSym);
826 
827     // Three pointers in size:
828     //   - __tlv_bootstrap - used to make sure support exists
829     //   - spare pointer, used when mapped by the runtime
830     //   - pointer to mangled symbol above with initializer
831     unsigned PtrSize = DL.getPointerTypeSize(GV->getType());
832     OutStreamer->emitSymbolValue(GetExternalSymbolSymbol("_tlv_bootstrap"),
833                                 PtrSize);
834     OutStreamer->emitIntValue(0, PtrSize);
835     OutStreamer->emitSymbolValue(MangSym, PtrSize);
836 
837     OutStreamer->addBlankLine();
838     return;
839   }
840 
841   MCSymbol *EmittedInitSym = GVSym;
842 
843   OutStreamer->switchSection(TheSection);
844 
845   emitLinkage(GV, EmittedInitSym);
846   emitAlignment(Alignment, GV);
847 
848   OutStreamer->emitLabel(EmittedInitSym);
849   MCSymbol *LocalAlias = getSymbolPreferLocal(*GV);
850   if (LocalAlias != EmittedInitSym)
851     OutStreamer->emitLabel(LocalAlias);
852 
853   emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
854 
855   if (MAI->hasDotTypeDotSizeDirective())
856     // .size foo, 42
857     OutStreamer->emitELFSize(EmittedInitSym,
858                              MCConstantExpr::create(Size, OutContext));
859 
860   OutStreamer->addBlankLine();
861 }
862 
863 /// Emit the directive and value for debug thread local expression
864 ///
865 /// \p Value - The value to emit.
866 /// \p Size - The size of the integer (in bytes) to emit.
867 void AsmPrinter::emitDebugValue(const MCExpr *Value, unsigned Size) const {
868   OutStreamer->emitValue(Value, Size);
869 }
870 
871 void AsmPrinter::emitFunctionHeaderComment() {}
872 
873 /// EmitFunctionHeader - This method emits the header for the current
874 /// function.
875 void AsmPrinter::emitFunctionHeader() {
876   const Function &F = MF->getFunction();
877 
878   if (isVerbose())
879     OutStreamer->getCommentOS()
880         << "-- Begin function "
881         << GlobalValue::dropLLVMManglingEscape(F.getName()) << '\n';
882 
883   // Print out constants referenced by the function
884   emitConstantPool();
885 
886   // Print the 'header' of function.
887   // If basic block sections are desired, explicitly request a unique section
888   // for this function's entry block.
889   if (MF->front().isBeginSection())
890     MF->setSection(getObjFileLowering().getUniqueSectionForFunction(F, TM));
891   else
892     MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
893   OutStreamer->switchSection(MF->getSection());
894 
895   if (!MAI->hasVisibilityOnlyWithLinkage())
896     emitVisibility(CurrentFnSym, F.getVisibility());
897 
898   if (MAI->needsFunctionDescriptors())
899     emitLinkage(&F, CurrentFnDescSym);
900 
901   emitLinkage(&F, CurrentFnSym);
902   if (MAI->hasFunctionAlignment())
903     emitAlignment(MF->getAlignment(), &F);
904 
905   if (MAI->hasDotTypeDotSizeDirective())
906     OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
907 
908   if (F.hasFnAttribute(Attribute::Cold))
909     OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_Cold);
910 
911   if (isVerbose()) {
912     F.printAsOperand(OutStreamer->getCommentOS(),
913                      /*PrintType=*/false, F.getParent());
914     emitFunctionHeaderComment();
915     OutStreamer->getCommentOS() << '\n';
916   }
917 
918   // Emit the prefix data.
919   if (F.hasPrefixData()) {
920     if (MAI->hasSubsectionsViaSymbols()) {
921       // Preserving prefix data on platforms which use subsections-via-symbols
922       // is a bit tricky. Here we introduce a symbol for the prefix data
923       // and use the .alt_entry attribute to mark the function's real entry point
924       // as an alternative entry point to the prefix-data symbol.
925       MCSymbol *PrefixSym = OutContext.createLinkerPrivateTempSymbol();
926       OutStreamer->emitLabel(PrefixSym);
927 
928       emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData());
929 
930       // Emit an .alt_entry directive for the actual function symbol.
931       OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_AltEntry);
932     } else {
933       emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData());
934     }
935   }
936 
937   // Emit KCFI type information before patchable-function-prefix nops.
938   emitKCFITypeId(*MF);
939 
940   // Emit M NOPs for -fpatchable-function-entry=N,M where M>0. We arbitrarily
941   // place prefix data before NOPs.
942   unsigned PatchableFunctionPrefix = 0;
943   unsigned PatchableFunctionEntry = 0;
944   (void)F.getFnAttribute("patchable-function-prefix")
945       .getValueAsString()
946       .getAsInteger(10, PatchableFunctionPrefix);
947   (void)F.getFnAttribute("patchable-function-entry")
948       .getValueAsString()
949       .getAsInteger(10, PatchableFunctionEntry);
950   if (PatchableFunctionPrefix) {
951     CurrentPatchableFunctionEntrySym =
952         OutContext.createLinkerPrivateTempSymbol();
953     OutStreamer->emitLabel(CurrentPatchableFunctionEntrySym);
954     emitNops(PatchableFunctionPrefix);
955   } else if (PatchableFunctionEntry) {
956     // May be reassigned when emitting the body, to reference the label after
957     // the initial BTI (AArch64) or endbr32/endbr64 (x86).
958     CurrentPatchableFunctionEntrySym = CurrentFnBegin;
959   }
960 
961   // Emit the function descriptor. This is a virtual function to allow targets
962   // to emit their specific function descriptor. Right now it is only used by
963   // the AIX target. The PowerPC 64-bit V1 ELF target also uses function
964   // descriptors and should be converted to use this hook as well.
965   if (MAI->needsFunctionDescriptors())
966     emitFunctionDescriptor();
967 
968   // Emit the CurrentFnSym. This is a virtual function to allow targets to do
969   // their wild and crazy things as required.
970   emitFunctionEntryLabel();
971 
972   // If the function had address-taken blocks that got deleted, then we have
973   // references to the dangling symbols.  Emit them at the start of the function
974   // so that we don't get references to undefined symbols.
975   std::vector<MCSymbol*> DeadBlockSyms;
976   takeDeletedSymbolsForFunction(&F, DeadBlockSyms);
977   for (MCSymbol *DeadBlockSym : DeadBlockSyms) {
978     OutStreamer->AddComment("Address taken block that was later removed");
979     OutStreamer->emitLabel(DeadBlockSym);
980   }
981 
982   if (CurrentFnBegin) {
983     if (MAI->useAssignmentForEHBegin()) {
984       MCSymbol *CurPos = OutContext.createTempSymbol();
985       OutStreamer->emitLabel(CurPos);
986       OutStreamer->emitAssignment(CurrentFnBegin,
987                                  MCSymbolRefExpr::create(CurPos, OutContext));
988     } else {
989       OutStreamer->emitLabel(CurrentFnBegin);
990     }
991   }
992 
993   // Emit pre-function debug and/or EH information.
994   for (const HandlerInfo &HI : Handlers) {
995     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
996                        HI.TimerGroupDescription, TimePassesIsEnabled);
997     HI.Handler->beginFunction(MF);
998   }
999   for (const HandlerInfo &HI : Handlers) {
1000     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1001                        HI.TimerGroupDescription, TimePassesIsEnabled);
1002     HI.Handler->beginBasicBlockSection(MF->front());
1003   }
1004 
1005   // Emit the prologue data.
1006   if (F.hasPrologueData())
1007     emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrologueData());
1008 
1009   // Emit the function prologue data for the indirect call sanitizer.
1010   if (const MDNode *MD = F.getMetadata(LLVMContext::MD_func_sanitize)) {
1011     assert(TM.getTargetTriple().getArch() == Triple::x86 ||
1012            TM.getTargetTriple().getArch() == Triple::x86_64);
1013     assert(MD->getNumOperands() == 2);
1014 
1015     auto *PrologueSig = mdconst::extract<Constant>(MD->getOperand(0));
1016     auto *FTRTTIProxy = mdconst::extract<Constant>(MD->getOperand(1));
1017     assert(PrologueSig && FTRTTIProxy);
1018     emitGlobalConstant(F.getParent()->getDataLayout(), PrologueSig);
1019 
1020     const MCExpr *Proxy = lowerConstant(FTRTTIProxy);
1021     const MCExpr *FnExp = MCSymbolRefExpr::create(CurrentFnSym, OutContext);
1022     const MCExpr *PCRel = MCBinaryExpr::createSub(Proxy, FnExp, OutContext);
1023     // Use 32 bit since only small code model is supported.
1024     OutStreamer->emitValue(PCRel, 4u);
1025   }
1026 }
1027 
1028 /// EmitFunctionEntryLabel - Emit the label that is the entrypoint for the
1029 /// function.  This can be overridden by targets as required to do custom stuff.
1030 void AsmPrinter::emitFunctionEntryLabel() {
1031   CurrentFnSym->redefineIfPossible();
1032 
1033   // The function label could have already been emitted if two symbols end up
1034   // conflicting due to asm renaming.  Detect this and emit an error.
1035   if (CurrentFnSym->isVariable())
1036     report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
1037                        "' is a protected alias");
1038 
1039   OutStreamer->emitLabel(CurrentFnSym);
1040 
1041   if (TM.getTargetTriple().isOSBinFormatELF()) {
1042     MCSymbol *Sym = getSymbolPreferLocal(MF->getFunction());
1043     if (Sym != CurrentFnSym) {
1044       cast<MCSymbolELF>(Sym)->setType(ELF::STT_FUNC);
1045       CurrentFnBeginLocal = Sym;
1046       OutStreamer->emitLabel(Sym);
1047       if (MAI->hasDotTypeDotSizeDirective())
1048         OutStreamer->emitSymbolAttribute(Sym, MCSA_ELF_TypeFunction);
1049     }
1050   }
1051 }
1052 
1053 /// emitComments - Pretty-print comments for instructions.
1054 static void emitComments(const MachineInstr &MI, raw_ostream &CommentOS) {
1055   const MachineFunction *MF = MI.getMF();
1056   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1057 
1058   // Check for spills and reloads
1059 
1060   // We assume a single instruction only has a spill or reload, not
1061   // both.
1062   std::optional<unsigned> Size;
1063   if ((Size = MI.getRestoreSize(TII))) {
1064     CommentOS << *Size << "-byte Reload\n";
1065   } else if ((Size = MI.getFoldedRestoreSize(TII))) {
1066     if (*Size) {
1067       if (*Size == unsigned(MemoryLocation::UnknownSize))
1068         CommentOS << "Unknown-size Folded Reload\n";
1069       else
1070         CommentOS << *Size << "-byte Folded Reload\n";
1071     }
1072   } else if ((Size = MI.getSpillSize(TII))) {
1073     CommentOS << *Size << "-byte Spill\n";
1074   } else if ((Size = MI.getFoldedSpillSize(TII))) {
1075     if (*Size) {
1076       if (*Size == unsigned(MemoryLocation::UnknownSize))
1077         CommentOS << "Unknown-size Folded Spill\n";
1078       else
1079         CommentOS << *Size << "-byte Folded Spill\n";
1080     }
1081   }
1082 
1083   // Check for spill-induced copies
1084   if (MI.getAsmPrinterFlag(MachineInstr::ReloadReuse))
1085     CommentOS << " Reload Reuse\n";
1086 }
1087 
1088 /// emitImplicitDef - This method emits the specified machine instruction
1089 /// that is an implicit def.
1090 void AsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
1091   Register RegNo = MI->getOperand(0).getReg();
1092 
1093   SmallString<128> Str;
1094   raw_svector_ostream OS(Str);
1095   OS << "implicit-def: "
1096      << printReg(RegNo, MF->getSubtarget().getRegisterInfo());
1097 
1098   OutStreamer->AddComment(OS.str());
1099   OutStreamer->addBlankLine();
1100 }
1101 
1102 static void emitKill(const MachineInstr *MI, AsmPrinter &AP) {
1103   std::string Str;
1104   raw_string_ostream OS(Str);
1105   OS << "kill:";
1106   for (const MachineOperand &Op : MI->operands()) {
1107     assert(Op.isReg() && "KILL instruction must have only register operands");
1108     OS << ' ' << (Op.isDef() ? "def " : "killed ")
1109        << printReg(Op.getReg(), AP.MF->getSubtarget().getRegisterInfo());
1110   }
1111   AP.OutStreamer->AddComment(OS.str());
1112   AP.OutStreamer->addBlankLine();
1113 }
1114 
1115 /// emitDebugValueComment - This method handles the target-independent form
1116 /// of DBG_VALUE, returning true if it was able to do so.  A false return
1117 /// means the target will need to handle MI in EmitInstruction.
1118 static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
1119   // This code handles only the 4-operand target-independent form.
1120   if (MI->isNonListDebugValue() && MI->getNumOperands() != 4)
1121     return false;
1122 
1123   SmallString<128> Str;
1124   raw_svector_ostream OS(Str);
1125   OS << "DEBUG_VALUE: ";
1126 
1127   const DILocalVariable *V = MI->getDebugVariable();
1128   if (auto *SP = dyn_cast<DISubprogram>(V->getScope())) {
1129     StringRef Name = SP->getName();
1130     if (!Name.empty())
1131       OS << Name << ":";
1132   }
1133   OS << V->getName();
1134   OS << " <- ";
1135 
1136   const DIExpression *Expr = MI->getDebugExpression();
1137   // First convert this to a non-variadic expression if possible, to simplify
1138   // the output.
1139   if (auto NonVariadicExpr = DIExpression::convertToNonVariadicExpression(Expr))
1140     Expr = *NonVariadicExpr;
1141   // Then, output the possibly-simplified expression.
1142   if (Expr->getNumElements()) {
1143     OS << '[';
1144     ListSeparator LS;
1145     for (auto &Op : Expr->expr_ops()) {
1146       OS << LS << dwarf::OperationEncodingString(Op.getOp());
1147       for (unsigned I = 0; I < Op.getNumArgs(); ++I)
1148         OS << ' ' << Op.getArg(I);
1149     }
1150     OS << "] ";
1151   }
1152 
1153   // Register or immediate value. Register 0 means undef.
1154   for (const MachineOperand &Op : MI->debug_operands()) {
1155     if (&Op != MI->debug_operands().begin())
1156       OS << ", ";
1157     switch (Op.getType()) {
1158     case MachineOperand::MO_FPImmediate: {
1159       APFloat APF = APFloat(Op.getFPImm()->getValueAPF());
1160       Type *ImmTy = Op.getFPImm()->getType();
1161       if (ImmTy->isBFloatTy() || ImmTy->isHalfTy() || ImmTy->isFloatTy() ||
1162           ImmTy->isDoubleTy()) {
1163         OS << APF.convertToDouble();
1164       } else {
1165         // There is no good way to print long double.  Convert a copy to
1166         // double.  Ah well, it's only a comment.
1167         bool ignored;
1168         APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
1169                     &ignored);
1170         OS << "(long double) " << APF.convertToDouble();
1171       }
1172       break;
1173     }
1174     case MachineOperand::MO_Immediate: {
1175       OS << Op.getImm();
1176       break;
1177     }
1178     case MachineOperand::MO_CImmediate: {
1179       Op.getCImm()->getValue().print(OS, false /*isSigned*/);
1180       break;
1181     }
1182     case MachineOperand::MO_TargetIndex: {
1183       OS << "!target-index(" << Op.getIndex() << "," << Op.getOffset() << ")";
1184       break;
1185     }
1186     case MachineOperand::MO_Register:
1187     case MachineOperand::MO_FrameIndex: {
1188       Register Reg;
1189       std::optional<StackOffset> Offset;
1190       if (Op.isReg()) {
1191         Reg = Op.getReg();
1192       } else {
1193         const TargetFrameLowering *TFI =
1194             AP.MF->getSubtarget().getFrameLowering();
1195         Offset = TFI->getFrameIndexReference(*AP.MF, Op.getIndex(), Reg);
1196       }
1197       if (!Reg) {
1198         // Suppress offset, it is not meaningful here.
1199         OS << "undef";
1200         break;
1201       }
1202       // The second operand is only an offset if it's an immediate.
1203       if (MI->isIndirectDebugValue())
1204         Offset = StackOffset::getFixed(MI->getDebugOffset().getImm());
1205       if (Offset)
1206         OS << '[';
1207       OS << printReg(Reg, AP.MF->getSubtarget().getRegisterInfo());
1208       if (Offset)
1209         OS << '+' << Offset->getFixed() << ']';
1210       break;
1211     }
1212     default:
1213       llvm_unreachable("Unknown operand type");
1214     }
1215   }
1216 
1217   // NOTE: Want this comment at start of line, don't emit with AddComment.
1218   AP.OutStreamer->emitRawComment(OS.str());
1219   return true;
1220 }
1221 
1222 /// This method handles the target-independent form of DBG_LABEL, returning
1223 /// true if it was able to do so.  A false return means the target will need
1224 /// to handle MI in EmitInstruction.
1225 static bool emitDebugLabelComment(const MachineInstr *MI, AsmPrinter &AP) {
1226   if (MI->getNumOperands() != 1)
1227     return false;
1228 
1229   SmallString<128> Str;
1230   raw_svector_ostream OS(Str);
1231   OS << "DEBUG_LABEL: ";
1232 
1233   const DILabel *V = MI->getDebugLabel();
1234   if (auto *SP = dyn_cast<DISubprogram>(
1235           V->getScope()->getNonLexicalBlockFileScope())) {
1236     StringRef Name = SP->getName();
1237     if (!Name.empty())
1238       OS << Name << ":";
1239   }
1240   OS << V->getName();
1241 
1242   // NOTE: Want this comment at start of line, don't emit with AddComment.
1243   AP.OutStreamer->emitRawComment(OS.str());
1244   return true;
1245 }
1246 
1247 AsmPrinter::CFISection
1248 AsmPrinter::getFunctionCFISectionType(const Function &F) const {
1249   // Ignore functions that won't get emitted.
1250   if (F.isDeclarationForLinker())
1251     return CFISection::None;
1252 
1253   if (MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI &&
1254       F.needsUnwindTableEntry())
1255     return CFISection::EH;
1256 
1257   if (MMI->hasDebugInfo() || TM.Options.ForceDwarfFrameSection)
1258     return CFISection::Debug;
1259 
1260   return CFISection::None;
1261 }
1262 
1263 AsmPrinter::CFISection
1264 AsmPrinter::getFunctionCFISectionType(const MachineFunction &MF) const {
1265   return getFunctionCFISectionType(MF.getFunction());
1266 }
1267 
1268 bool AsmPrinter::needsSEHMoves() {
1269   return MAI->usesWindowsCFI() && MF->getFunction().needsUnwindTableEntry();
1270 }
1271 
1272 bool AsmPrinter::needsCFIForDebug() const {
1273   return MAI->getExceptionHandlingType() == ExceptionHandling::None &&
1274          MAI->doesUseCFIForDebug() && ModuleCFISection == CFISection::Debug;
1275 }
1276 
1277 void AsmPrinter::emitCFIInstruction(const MachineInstr &MI) {
1278   ExceptionHandling ExceptionHandlingType = MAI->getExceptionHandlingType();
1279   if (!needsCFIForDebug() &&
1280       ExceptionHandlingType != ExceptionHandling::DwarfCFI &&
1281       ExceptionHandlingType != ExceptionHandling::ARM)
1282     return;
1283 
1284   if (getFunctionCFISectionType(*MF) == CFISection::None)
1285     return;
1286 
1287   // If there is no "real" instruction following this CFI instruction, skip
1288   // emitting it; it would be beyond the end of the function's FDE range.
1289   auto *MBB = MI.getParent();
1290   auto I = std::next(MI.getIterator());
1291   while (I != MBB->end() && I->isTransient())
1292     ++I;
1293   if (I == MBB->instr_end() &&
1294       MBB->getReverseIterator() == MBB->getParent()->rbegin())
1295     return;
1296 
1297   const std::vector<MCCFIInstruction> &Instrs = MF->getFrameInstructions();
1298   unsigned CFIIndex = MI.getOperand(0).getCFIIndex();
1299   const MCCFIInstruction &CFI = Instrs[CFIIndex];
1300   emitCFIInstruction(CFI);
1301 }
1302 
1303 void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) {
1304   // The operands are the MCSymbol and the frame offset of the allocation.
1305   MCSymbol *FrameAllocSym = MI.getOperand(0).getMCSymbol();
1306   int FrameOffset = MI.getOperand(1).getImm();
1307 
1308   // Emit a symbol assignment.
1309   OutStreamer->emitAssignment(FrameAllocSym,
1310                              MCConstantExpr::create(FrameOffset, OutContext));
1311 }
1312 
1313 /// Returns the BB metadata to be emitted in the .llvm_bb_addr_map section for a
1314 /// given basic block. This can be used to capture more precise profile
1315 /// information. We use the last 4 bits (LSBs) to encode the following
1316 /// information:
1317 ///  * (1): set if return block (ret or tail call).
1318 ///  * (2): set if ends with a tail call.
1319 ///  * (3): set if exception handling (EH) landing pad.
1320 ///  * (4): set if the block can fall through to its next.
1321 /// The remaining bits are zero.
1322 static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
1323   const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo();
1324   return ((unsigned)MBB.isReturnBlock()) |
1325          ((!MBB.empty() && TII->isTailCall(MBB.back())) << 1) |
1326          (MBB.isEHPad() << 2) |
1327          (const_cast<MachineBasicBlock &>(MBB).canFallThrough() << 3);
1328 }
1329 
1330 void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
1331   MCSection *BBAddrMapSection =
1332       getObjFileLowering().getBBAddrMapSection(*MF.getSection());
1333   assert(BBAddrMapSection && ".llvm_bb_addr_map section is not initialized.");
1334 
1335   const MCSymbol *FunctionSymbol = getFunctionBegin();
1336 
1337   OutStreamer->pushSection();
1338   OutStreamer->switchSection(BBAddrMapSection);
1339   OutStreamer->AddComment("version");
1340   uint8_t BBAddrMapVersion = OutStreamer->getContext().getBBAddrMapVersion();
1341   OutStreamer->emitInt8(BBAddrMapVersion);
1342   OutStreamer->AddComment("feature");
1343   OutStreamer->emitInt8(0);
1344   OutStreamer->AddComment("function address");
1345   OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize());
1346   OutStreamer->AddComment("number of basic blocks");
1347   OutStreamer->emitULEB128IntValue(MF.size());
1348   const MCSymbol *PrevMBBEndSymbol = FunctionSymbol;
1349   // Emit BB Information for each basic block in the funciton.
1350   for (const MachineBasicBlock &MBB : MF) {
1351     const MCSymbol *MBBSymbol =
1352         MBB.isEntryBlock() ? FunctionSymbol : MBB.getSymbol();
1353     // TODO: Remove this check when version 1 is deprecated.
1354     if (BBAddrMapVersion > 1) {
1355       OutStreamer->AddComment("BB id");
1356       // Emit the BB ID for this basic block.
1357       OutStreamer->emitULEB128IntValue(*MBB.getBBID());
1358     }
1359     // Emit the basic block offset relative to the end of the previous block.
1360     // This is zero unless the block is padded due to alignment.
1361     emitLabelDifferenceAsULEB128(MBBSymbol, PrevMBBEndSymbol);
1362     // Emit the basic block size. When BBs have alignments, their size cannot
1363     // always be computed from their offsets.
1364     emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
1365     // Emit the Metadata.
1366     OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
1367     PrevMBBEndSymbol = MBB.getEndSymbol();
1368   }
1369   OutStreamer->popSection();
1370 }
1371 
1372 void AsmPrinter::emitKCFITrapEntry(const MachineFunction &MF,
1373                                    const MCSymbol *Symbol) {
1374   MCSection *Section =
1375       getObjFileLowering().getKCFITrapSection(*MF.getSection());
1376   if (!Section)
1377     return;
1378 
1379   OutStreamer->pushSection();
1380   OutStreamer->switchSection(Section);
1381 
1382   MCSymbol *Loc = OutContext.createLinkerPrivateTempSymbol();
1383   OutStreamer->emitLabel(Loc);
1384   OutStreamer->emitAbsoluteSymbolDiff(Symbol, Loc, 4);
1385 
1386   OutStreamer->popSection();
1387 }
1388 
1389 void AsmPrinter::emitKCFITypeId(const MachineFunction &MF) {
1390   const Function &F = MF.getFunction();
1391   if (const MDNode *MD = F.getMetadata(LLVMContext::MD_kcfi_type))
1392     emitGlobalConstant(F.getParent()->getDataLayout(),
1393                        mdconst::extract<ConstantInt>(MD->getOperand(0)));
1394 }
1395 
1396 void AsmPrinter::emitPseudoProbe(const MachineInstr &MI) {
1397   if (PP) {
1398     auto GUID = MI.getOperand(0).getImm();
1399     auto Index = MI.getOperand(1).getImm();
1400     auto Type = MI.getOperand(2).getImm();
1401     auto Attr = MI.getOperand(3).getImm();
1402     DILocation *DebugLoc = MI.getDebugLoc();
1403     PP->emitPseudoProbe(GUID, Index, Type, Attr, DebugLoc);
1404   }
1405 }
1406 
1407 void AsmPrinter::emitStackSizeSection(const MachineFunction &MF) {
1408   if (!MF.getTarget().Options.EmitStackSizeSection)
1409     return;
1410 
1411   MCSection *StackSizeSection =
1412       getObjFileLowering().getStackSizesSection(*getCurrentSection());
1413   if (!StackSizeSection)
1414     return;
1415 
1416   const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
1417   // Don't emit functions with dynamic stack allocations.
1418   if (FrameInfo.hasVarSizedObjects())
1419     return;
1420 
1421   OutStreamer->pushSection();
1422   OutStreamer->switchSection(StackSizeSection);
1423 
1424   const MCSymbol *FunctionSymbol = getFunctionBegin();
1425   uint64_t StackSize =
1426       FrameInfo.getStackSize() + FrameInfo.getUnsafeStackSize();
1427   OutStreamer->emitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
1428   OutStreamer->emitULEB128IntValue(StackSize);
1429 
1430   OutStreamer->popSection();
1431 }
1432 
1433 void AsmPrinter::emitStackUsage(const MachineFunction &MF) {
1434   const std::string &OutputFilename = MF.getTarget().Options.StackUsageOutput;
1435 
1436   // OutputFilename empty implies -fstack-usage is not passed.
1437   if (OutputFilename.empty())
1438     return;
1439 
1440   const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
1441   uint64_t StackSize =
1442       FrameInfo.getStackSize() + FrameInfo.getUnsafeStackSize();
1443 
1444   if (StackUsageStream == nullptr) {
1445     std::error_code EC;
1446     StackUsageStream =
1447         std::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::OF_Text);
1448     if (EC) {
1449       errs() << "Could not open file: " << EC.message();
1450       return;
1451     }
1452   }
1453 
1454   *StackUsageStream << MF.getFunction().getParent()->getName();
1455   if (const DISubprogram *DSP = MF.getFunction().getSubprogram())
1456     *StackUsageStream << ':' << DSP->getLine();
1457 
1458   *StackUsageStream << ':' << MF.getName() << '\t' << StackSize << '\t';
1459   if (FrameInfo.hasVarSizedObjects())
1460     *StackUsageStream << "dynamic\n";
1461   else
1462     *StackUsageStream << "static\n";
1463 }
1464 
1465 void AsmPrinter::emitPCSectionsLabel(const MachineFunction &MF,
1466                                      const MDNode &MD) {
1467   MCSymbol *S = MF.getContext().createTempSymbol("pcsection");
1468   OutStreamer->emitLabel(S);
1469   PCSectionsSymbols[&MD].emplace_back(S);
1470 }
1471 
1472 void AsmPrinter::emitPCSections(const MachineFunction &MF) {
1473   const Function &F = MF.getFunction();
1474   if (PCSectionsSymbols.empty() && !F.hasMetadata(LLVMContext::MD_pcsections))
1475     return;
1476 
1477   const CodeModel::Model CM = MF.getTarget().getCodeModel();
1478   const unsigned RelativeRelocSize =
1479       (CM == CodeModel::Medium || CM == CodeModel::Large) ? getPointerSize()
1480                                                           : 4;
1481 
1482   // Switch to PCSection, short-circuiting the common case where the current
1483   // section is still valid (assume most MD_pcsections contain just 1 section).
1484   auto SwitchSection = [&, Prev = StringRef()](const StringRef &Sec) mutable {
1485     if (Sec == Prev)
1486       return;
1487     MCSection *S = getObjFileLowering().getPCSection(Sec, MF.getSection());
1488     assert(S && "PC section is not initialized");
1489     OutStreamer->switchSection(S);
1490     Prev = Sec;
1491   };
1492   // Emit symbols into sections and data as specified in the pcsections MDNode.
1493   auto EmitForMD = [&](const MDNode &MD, ArrayRef<const MCSymbol *> Syms,
1494                        bool Deltas) {
1495     // Expect the first operand to be a section name. After that, a tuple of
1496     // constants may appear, which will simply be emitted into the current
1497     // section (the user of MD_pcsections decides the format of encoded data).
1498     assert(isa<MDString>(MD.getOperand(0)) && "first operand not a string");
1499     for (const MDOperand &MDO : MD.operands()) {
1500       if (auto *S = dyn_cast<MDString>(MDO)) {
1501         SwitchSection(S->getString());
1502         const MCSymbol *Prev = Syms.front();
1503         for (const MCSymbol *Sym : Syms) {
1504           if (Sym == Prev || !Deltas) {
1505             // Use the entry itself as the base of the relative offset.
1506             MCSymbol *Base = MF.getContext().createTempSymbol("pcsection_base");
1507             OutStreamer->emitLabel(Base);
1508             // Emit relative relocation `addr - base`, which avoids a dynamic
1509             // relocation in the final binary. User will get the address with
1510             // `base + addr`.
1511             emitLabelDifference(Sym, Base, RelativeRelocSize);
1512           } else {
1513             emitLabelDifference(Sym, Prev, 4);
1514           }
1515           Prev = Sym;
1516         }
1517       } else {
1518         assert(isa<MDNode>(MDO) && "expecting either string or tuple");
1519         const auto *AuxMDs = cast<MDNode>(MDO);
1520         for (const MDOperand &AuxMDO : AuxMDs->operands()) {
1521           assert(isa<ConstantAsMetadata>(AuxMDO) && "expecting a constant");
1522           const auto *C = cast<ConstantAsMetadata>(AuxMDO);
1523           emitGlobalConstant(F.getParent()->getDataLayout(), C->getValue());
1524         }
1525       }
1526     }
1527   };
1528 
1529   OutStreamer->pushSection();
1530   // Emit PCs for function start and function size.
1531   if (const MDNode *MD = F.getMetadata(LLVMContext::MD_pcsections))
1532     EmitForMD(*MD, {getFunctionBegin(), getFunctionEnd()}, true);
1533   // Emit PCs for instructions collected.
1534   for (const auto &MS : PCSectionsSymbols)
1535     EmitForMD(*MS.first, MS.second, false);
1536   OutStreamer->popSection();
1537   PCSectionsSymbols.clear();
1538 }
1539 
1540 /// Returns true if function begin and end labels should be emitted.
1541 static bool needFuncLabels(const MachineFunction &MF) {
1542   MachineModuleInfo &MMI = MF.getMMI();
1543   if (!MF.getLandingPads().empty() || MF.hasEHFunclets() ||
1544       MMI.hasDebugInfo() ||
1545       MF.getFunction().hasMetadata(LLVMContext::MD_pcsections))
1546     return true;
1547 
1548   // We might emit an EH table that uses function begin and end labels even if
1549   // we don't have any landingpads.
1550   if (!MF.getFunction().hasPersonalityFn())
1551     return false;
1552   return !isNoOpWithoutInvoke(
1553       classifyEHPersonality(MF.getFunction().getPersonalityFn()));
1554 }
1555 
1556 /// EmitFunctionBody - This method emits the body and trailer for a
1557 /// function.
1558 void AsmPrinter::emitFunctionBody() {
1559   emitFunctionHeader();
1560 
1561   // Emit target-specific gunk before the function body.
1562   emitFunctionBodyStart();
1563 
1564   if (isVerbose()) {
1565     // Get MachineDominatorTree or compute it on the fly if it's unavailable
1566     MDT = getAnalysisIfAvailable<MachineDominatorTree>();
1567     if (!MDT) {
1568       OwnedMDT = std::make_unique<MachineDominatorTree>();
1569       OwnedMDT->getBase().recalculate(*MF);
1570       MDT = OwnedMDT.get();
1571     }
1572 
1573     // Get MachineLoopInfo or compute it on the fly if it's unavailable
1574     MLI = getAnalysisIfAvailable<MachineLoopInfo>();
1575     if (!MLI) {
1576       OwnedMLI = std::make_unique<MachineLoopInfo>();
1577       OwnedMLI->getBase().analyze(MDT->getBase());
1578       MLI = OwnedMLI.get();
1579     }
1580   }
1581 
1582   // Print out code for the function.
1583   bool HasAnyRealCode = false;
1584   int NumInstsInFunction = 0;
1585 
1586   bool CanDoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE);
1587   for (auto &MBB : *MF) {
1588     // Print a label for the basic block.
1589     emitBasicBlockStart(MBB);
1590     DenseMap<StringRef, unsigned> MnemonicCounts;
1591     for (auto &MI : MBB) {
1592       // Print the assembly for the instruction.
1593       if (!MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
1594           !MI.isDebugInstr()) {
1595         HasAnyRealCode = true;
1596         ++NumInstsInFunction;
1597       }
1598 
1599       // If there is a pre-instruction symbol, emit a label for it here.
1600       if (MCSymbol *S = MI.getPreInstrSymbol())
1601         OutStreamer->emitLabel(S);
1602 
1603       if (MDNode *MD = MI.getPCSections())
1604         emitPCSectionsLabel(*MF, *MD);
1605 
1606       for (const HandlerInfo &HI : Handlers) {
1607         NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1608                            HI.TimerGroupDescription, TimePassesIsEnabled);
1609         HI.Handler->beginInstruction(&MI);
1610       }
1611 
1612       if (isVerbose())
1613         emitComments(MI, OutStreamer->getCommentOS());
1614 
1615       switch (MI.getOpcode()) {
1616       case TargetOpcode::CFI_INSTRUCTION:
1617         emitCFIInstruction(MI);
1618         break;
1619       case TargetOpcode::LOCAL_ESCAPE:
1620         emitFrameAlloc(MI);
1621         break;
1622       case TargetOpcode::ANNOTATION_LABEL:
1623       case TargetOpcode::EH_LABEL:
1624       case TargetOpcode::GC_LABEL:
1625         OutStreamer->emitLabel(MI.getOperand(0).getMCSymbol());
1626         break;
1627       case TargetOpcode::INLINEASM:
1628       case TargetOpcode::INLINEASM_BR:
1629         emitInlineAsm(&MI);
1630         break;
1631       case TargetOpcode::DBG_VALUE:
1632       case TargetOpcode::DBG_VALUE_LIST:
1633         if (isVerbose()) {
1634           if (!emitDebugValueComment(&MI, *this))
1635             emitInstruction(&MI);
1636         }
1637         break;
1638       case TargetOpcode::DBG_INSTR_REF:
1639         // This instruction reference will have been resolved to a machine
1640         // location, and a nearby DBG_VALUE created. We can safely ignore
1641         // the instruction reference.
1642         break;
1643       case TargetOpcode::DBG_PHI:
1644         // This instruction is only used to label a program point, it's purely
1645         // meta information.
1646         break;
1647       case TargetOpcode::DBG_LABEL:
1648         if (isVerbose()) {
1649           if (!emitDebugLabelComment(&MI, *this))
1650             emitInstruction(&MI);
1651         }
1652         break;
1653       case TargetOpcode::IMPLICIT_DEF:
1654         if (isVerbose()) emitImplicitDef(&MI);
1655         break;
1656       case TargetOpcode::KILL:
1657         if (isVerbose()) emitKill(&MI, *this);
1658         break;
1659       case TargetOpcode::PSEUDO_PROBE:
1660         emitPseudoProbe(MI);
1661         break;
1662       case TargetOpcode::ARITH_FENCE:
1663         if (isVerbose())
1664           OutStreamer->emitRawComment("ARITH_FENCE");
1665         break;
1666       case TargetOpcode::MEMBARRIER:
1667         OutStreamer->emitRawComment("MEMBARRIER");
1668         break;
1669       default:
1670         emitInstruction(&MI);
1671         if (CanDoExtraAnalysis) {
1672           MCInst MCI;
1673           MCI.setOpcode(MI.getOpcode());
1674           auto Name = OutStreamer->getMnemonic(MCI);
1675           auto I = MnemonicCounts.insert({Name, 0u});
1676           I.first->second++;
1677         }
1678         break;
1679       }
1680 
1681       // If there is a post-instruction symbol, emit a label for it here.
1682       if (MCSymbol *S = MI.getPostInstrSymbol())
1683         OutStreamer->emitLabel(S);
1684 
1685       for (const HandlerInfo &HI : Handlers) {
1686         NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1687                            HI.TimerGroupDescription, TimePassesIsEnabled);
1688         HI.Handler->endInstruction();
1689       }
1690     }
1691 
1692     // We must emit temporary symbol for the end of this basic block, if either
1693     // we have BBLabels enabled or if this basic blocks marks the end of a
1694     // section.
1695     if (MF->hasBBLabels() ||
1696         (MAI->hasDotTypeDotSizeDirective() && MBB.isEndSection()))
1697       OutStreamer->emitLabel(MBB.getEndSymbol());
1698 
1699     if (MBB.isEndSection()) {
1700       // The size directive for the section containing the entry block is
1701       // handled separately by the function section.
1702       if (!MBB.sameSection(&MF->front())) {
1703         if (MAI->hasDotTypeDotSizeDirective()) {
1704           // Emit the size directive for the basic block section.
1705           const MCExpr *SizeExp = MCBinaryExpr::createSub(
1706               MCSymbolRefExpr::create(MBB.getEndSymbol(), OutContext),
1707               MCSymbolRefExpr::create(CurrentSectionBeginSym, OutContext),
1708               OutContext);
1709           OutStreamer->emitELFSize(CurrentSectionBeginSym, SizeExp);
1710         }
1711         MBBSectionRanges[MBB.getSectionIDNum()] =
1712             MBBSectionRange{CurrentSectionBeginSym, MBB.getEndSymbol()};
1713       }
1714     }
1715     emitBasicBlockEnd(MBB);
1716 
1717     if (CanDoExtraAnalysis) {
1718       // Skip empty blocks.
1719       if (MBB.empty())
1720         continue;
1721 
1722       MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "InstructionMix",
1723                                           MBB.begin()->getDebugLoc(), &MBB);
1724 
1725       // Generate instruction mix remark. First, sort counts in descending order
1726       // by count and name.
1727       SmallVector<std::pair<StringRef, unsigned>, 128> MnemonicVec;
1728       for (auto &KV : MnemonicCounts)
1729         MnemonicVec.emplace_back(KV.first, KV.second);
1730 
1731       sort(MnemonicVec, [](const std::pair<StringRef, unsigned> &A,
1732                            const std::pair<StringRef, unsigned> &B) {
1733         if (A.second > B.second)
1734           return true;
1735         if (A.second == B.second)
1736           return StringRef(A.first) < StringRef(B.first);
1737         return false;
1738       });
1739       R << "BasicBlock: " << ore::NV("BasicBlock", MBB.getName()) << "\n";
1740       for (auto &KV : MnemonicVec) {
1741         auto Name = (Twine("INST_") + getToken(KV.first.trim()).first).str();
1742         R << KV.first << ": " << ore::NV(Name, KV.second) << "\n";
1743       }
1744       ORE->emit(R);
1745     }
1746   }
1747 
1748   EmittedInsts += NumInstsInFunction;
1749   MachineOptimizationRemarkAnalysis R(DEBUG_TYPE, "InstructionCount",
1750                                       MF->getFunction().getSubprogram(),
1751                                       &MF->front());
1752   R << ore::NV("NumInstructions", NumInstsInFunction)
1753     << " instructions in function";
1754   ORE->emit(R);
1755 
1756   // If the function is empty and the object file uses .subsections_via_symbols,
1757   // then we need to emit *something* to the function body to prevent the
1758   // labels from collapsing together.  Just emit a noop.
1759   // Similarly, don't emit empty functions on Windows either. It can lead to
1760   // duplicate entries (two functions with the same RVA) in the Guard CF Table
1761   // after linking, causing the kernel not to load the binary:
1762   // https://developercommunity.visualstudio.com/content/problem/45366/vc-linker-creates-invalid-dll-with-clang-cl.html
1763   // FIXME: Hide this behind some API in e.g. MCAsmInfo or MCTargetStreamer.
1764   const Triple &TT = TM.getTargetTriple();
1765   if (!HasAnyRealCode && (MAI->hasSubsectionsViaSymbols() ||
1766                           (TT.isOSWindows() && TT.isOSBinFormatCOFF()))) {
1767     MCInst Noop = MF->getSubtarget().getInstrInfo()->getNop();
1768 
1769     // Targets can opt-out of emitting the noop here by leaving the opcode
1770     // unspecified.
1771     if (Noop.getOpcode()) {
1772       OutStreamer->AddComment("avoids zero-length function");
1773       emitNops(1);
1774     }
1775   }
1776 
1777   // Switch to the original section in case basic block sections was used.
1778   OutStreamer->switchSection(MF->getSection());
1779 
1780   const Function &F = MF->getFunction();
1781   for (const auto &BB : F) {
1782     if (!BB.hasAddressTaken())
1783       continue;
1784     MCSymbol *Sym = GetBlockAddressSymbol(&BB);
1785     if (Sym->isDefined())
1786       continue;
1787     OutStreamer->AddComment("Address of block that was removed by CodeGen");
1788     OutStreamer->emitLabel(Sym);
1789   }
1790 
1791   // Emit target-specific gunk after the function body.
1792   emitFunctionBodyEnd();
1793 
1794   // Even though wasm supports .type and .size in general, function symbols
1795   // are automatically sized.
1796   bool EmitFunctionSize = MAI->hasDotTypeDotSizeDirective() && !TT.isWasm();
1797 
1798   if (needFuncLabels(*MF) || EmitFunctionSize) {
1799     // Create a symbol for the end of function.
1800     CurrentFnEnd = createTempSymbol("func_end");
1801     OutStreamer->emitLabel(CurrentFnEnd);
1802   }
1803 
1804   // If the target wants a .size directive for the size of the function, emit
1805   // it.
1806   if (EmitFunctionSize) {
1807     // We can get the size as difference between the function label and the
1808     // temp label.
1809     const MCExpr *SizeExp = MCBinaryExpr::createSub(
1810         MCSymbolRefExpr::create(CurrentFnEnd, OutContext),
1811         MCSymbolRefExpr::create(CurrentFnSymForSize, OutContext), OutContext);
1812     OutStreamer->emitELFSize(CurrentFnSym, SizeExp);
1813     if (CurrentFnBeginLocal)
1814       OutStreamer->emitELFSize(CurrentFnBeginLocal, SizeExp);
1815   }
1816 
1817   // Call endBasicBlockSection on the last block now, if it wasn't already
1818   // called.
1819   if (!MF->back().isEndSection()) {
1820     for (const HandlerInfo &HI : Handlers) {
1821       NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1822                          HI.TimerGroupDescription, TimePassesIsEnabled);
1823       HI.Handler->endBasicBlockSection(MF->back());
1824     }
1825   }
1826   for (const HandlerInfo &HI : Handlers) {
1827     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1828                        HI.TimerGroupDescription, TimePassesIsEnabled);
1829     HI.Handler->markFunctionEnd();
1830   }
1831 
1832   MBBSectionRanges[MF->front().getSectionIDNum()] =
1833       MBBSectionRange{CurrentFnBegin, CurrentFnEnd};
1834 
1835   // Print out jump tables referenced by the function.
1836   emitJumpTableInfo();
1837 
1838   // Emit post-function debug and/or EH information.
1839   for (const HandlerInfo &HI : Handlers) {
1840     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1841                        HI.TimerGroupDescription, TimePassesIsEnabled);
1842     HI.Handler->endFunction(MF);
1843   }
1844 
1845   // Emit section containing BB address offsets and their metadata, when
1846   // BB labels are requested for this function. Skip empty functions.
1847   if (MF->hasBBLabels() && HasAnyRealCode)
1848     emitBBAddrMapSection(*MF);
1849 
1850   // Emit sections containing instruction and function PCs.
1851   emitPCSections(*MF);
1852 
1853   // Emit section containing stack size metadata.
1854   emitStackSizeSection(*MF);
1855 
1856   // Emit .su file containing function stack size information.
1857   emitStackUsage(*MF);
1858 
1859   emitPatchableFunctionEntries();
1860 
1861   if (isVerbose())
1862     OutStreamer->getCommentOS() << "-- End function\n";
1863 
1864   OutStreamer->addBlankLine();
1865 }
1866 
1867 /// Compute the number of Global Variables that uses a Constant.
1868 static unsigned getNumGlobalVariableUses(const Constant *C) {
1869   if (!C)
1870     return 0;
1871 
1872   if (isa<GlobalVariable>(C))
1873     return 1;
1874 
1875   unsigned NumUses = 0;
1876   for (const auto *CU : C->users())
1877     NumUses += getNumGlobalVariableUses(dyn_cast<Constant>(CU));
1878 
1879   return NumUses;
1880 }
1881 
1882 /// Only consider global GOT equivalents if at least one user is a
1883 /// cstexpr inside an initializer of another global variables. Also, don't
1884 /// handle cstexpr inside instructions. During global variable emission,
1885 /// candidates are skipped and are emitted later in case at least one cstexpr
1886 /// isn't replaced by a PC relative GOT entry access.
1887 static bool isGOTEquivalentCandidate(const GlobalVariable *GV,
1888                                      unsigned &NumGOTEquivUsers) {
1889   // Global GOT equivalents are unnamed private globals with a constant
1890   // pointer initializer to another global symbol. They must point to a
1891   // GlobalVariable or Function, i.e., as GlobalValue.
1892   if (!GV->hasGlobalUnnamedAddr() || !GV->hasInitializer() ||
1893       !GV->isConstant() || !GV->isDiscardableIfUnused() ||
1894       !isa<GlobalValue>(GV->getOperand(0)))
1895     return false;
1896 
1897   // To be a got equivalent, at least one of its users need to be a constant
1898   // expression used by another global variable.
1899   for (const auto *U : GV->users())
1900     NumGOTEquivUsers += getNumGlobalVariableUses(dyn_cast<Constant>(U));
1901 
1902   return NumGOTEquivUsers > 0;
1903 }
1904 
1905 /// Unnamed constant global variables solely contaning a pointer to
1906 /// another globals variable is equivalent to a GOT table entry; it contains the
1907 /// the address of another symbol. Optimize it and replace accesses to these
1908 /// "GOT equivalents" by using the GOT entry for the final global instead.
1909 /// Compute GOT equivalent candidates among all global variables to avoid
1910 /// emitting them if possible later on, after it use is replaced by a GOT entry
1911 /// access.
1912 void AsmPrinter::computeGlobalGOTEquivs(Module &M) {
1913   if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1914     return;
1915 
1916   for (const auto &G : M.globals()) {
1917     unsigned NumGOTEquivUsers = 0;
1918     if (!isGOTEquivalentCandidate(&G, NumGOTEquivUsers))
1919       continue;
1920 
1921     const MCSymbol *GOTEquivSym = getSymbol(&G);
1922     GlobalGOTEquivs[GOTEquivSym] = std::make_pair(&G, NumGOTEquivUsers);
1923   }
1924 }
1925 
1926 /// Constant expressions using GOT equivalent globals may not be eligible
1927 /// for PC relative GOT entry conversion, in such cases we need to emit such
1928 /// globals we previously omitted in EmitGlobalVariable.
1929 void AsmPrinter::emitGlobalGOTEquivs() {
1930   if (!getObjFileLowering().supportIndirectSymViaGOTPCRel())
1931     return;
1932 
1933   SmallVector<const GlobalVariable *, 8> FailedCandidates;
1934   for (auto &I : GlobalGOTEquivs) {
1935     const GlobalVariable *GV = I.second.first;
1936     unsigned Cnt = I.second.second;
1937     if (Cnt)
1938       FailedCandidates.push_back(GV);
1939   }
1940   GlobalGOTEquivs.clear();
1941 
1942   for (const auto *GV : FailedCandidates)
1943     emitGlobalVariable(GV);
1944 }
1945 
1946 void AsmPrinter::emitGlobalAlias(Module &M, const GlobalAlias &GA) {
1947   MCSymbol *Name = getSymbol(&GA);
1948   bool IsFunction = GA.getValueType()->isFunctionTy();
1949   // Treat bitcasts of functions as functions also. This is important at least
1950   // on WebAssembly where object and function addresses can't alias each other.
1951   if (!IsFunction)
1952     IsFunction = isa<Function>(GA.getAliasee()->stripPointerCasts());
1953 
1954   // AIX's assembly directive `.set` is not usable for aliasing purpose,
1955   // so AIX has to use the extra-label-at-definition strategy. At this
1956   // point, all the extra label is emitted, we just have to emit linkage for
1957   // those labels.
1958   if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
1959     assert(MAI->hasVisibilityOnlyWithLinkage() &&
1960            "Visibility should be handled with emitLinkage() on AIX.");
1961 
1962     // Linkage for alias of global variable has been emitted.
1963     if (isa<GlobalVariable>(GA.getAliaseeObject()))
1964       return;
1965 
1966     emitLinkage(&GA, Name);
1967     // If it's a function, also emit linkage for aliases of function entry
1968     // point.
1969     if (IsFunction)
1970       emitLinkage(&GA,
1971                   getObjFileLowering().getFunctionEntryPointSymbol(&GA, TM));
1972     return;
1973   }
1974 
1975   if (GA.hasExternalLinkage() || !MAI->getWeakRefDirective())
1976     OutStreamer->emitSymbolAttribute(Name, MCSA_Global);
1977   else if (GA.hasWeakLinkage() || GA.hasLinkOnceLinkage())
1978     OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference);
1979   else
1980     assert(GA.hasLocalLinkage() && "Invalid alias linkage");
1981 
1982   // Set the symbol type to function if the alias has a function type.
1983   // This affects codegen when the aliasee is not a function.
1984   if (IsFunction) {
1985     OutStreamer->emitSymbolAttribute(Name, MCSA_ELF_TypeFunction);
1986     if (TM.getTargetTriple().isOSBinFormatCOFF()) {
1987       OutStreamer->beginCOFFSymbolDef(Name);
1988       OutStreamer->emitCOFFSymbolStorageClass(
1989           GA.hasLocalLinkage() ? COFF::IMAGE_SYM_CLASS_STATIC
1990                                : COFF::IMAGE_SYM_CLASS_EXTERNAL);
1991       OutStreamer->emitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
1992                                       << COFF::SCT_COMPLEX_TYPE_SHIFT);
1993       OutStreamer->endCOFFSymbolDef();
1994     }
1995   }
1996 
1997   emitVisibility(Name, GA.getVisibility());
1998 
1999   const MCExpr *Expr = lowerConstant(GA.getAliasee());
2000 
2001   if (MAI->hasAltEntry() && isa<MCBinaryExpr>(Expr))
2002     OutStreamer->emitSymbolAttribute(Name, MCSA_AltEntry);
2003 
2004   // Emit the directives as assignments aka .set:
2005   OutStreamer->emitAssignment(Name, Expr);
2006   MCSymbol *LocalAlias = getSymbolPreferLocal(GA);
2007   if (LocalAlias != Name)
2008     OutStreamer->emitAssignment(LocalAlias, Expr);
2009 
2010   // If the aliasee does not correspond to a symbol in the output, i.e. the
2011   // alias is not of an object or the aliased object is private, then set the
2012   // size of the alias symbol from the type of the alias. We don't do this in
2013   // other situations as the alias and aliasee having differing types but same
2014   // size may be intentional.
2015   const GlobalObject *BaseObject = GA.getAliaseeObject();
2016   if (MAI->hasDotTypeDotSizeDirective() && GA.getValueType()->isSized() &&
2017       (!BaseObject || BaseObject->hasPrivateLinkage())) {
2018     const DataLayout &DL = M.getDataLayout();
2019     uint64_t Size = DL.getTypeAllocSize(GA.getValueType());
2020     OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext));
2021   }
2022 }
2023 
2024 void AsmPrinter::emitGlobalIFunc(Module &M, const GlobalIFunc &GI) {
2025   assert(!TM.getTargetTriple().isOSBinFormatXCOFF() &&
2026          "IFunc is not supported on AIX.");
2027 
2028   MCSymbol *Name = getSymbol(&GI);
2029 
2030   if (GI.hasExternalLinkage() || !MAI->getWeakRefDirective())
2031     OutStreamer->emitSymbolAttribute(Name, MCSA_Global);
2032   else if (GI.hasWeakLinkage() || GI.hasLinkOnceLinkage())
2033     OutStreamer->emitSymbolAttribute(Name, MCSA_WeakReference);
2034   else
2035     assert(GI.hasLocalLinkage() && "Invalid ifunc linkage");
2036 
2037   OutStreamer->emitSymbolAttribute(Name, MCSA_ELF_TypeIndFunction);
2038   emitVisibility(Name, GI.getVisibility());
2039 
2040   // Emit the directives as assignments aka .set:
2041   const MCExpr *Expr = lowerConstant(GI.getResolver());
2042   OutStreamer->emitAssignment(Name, Expr);
2043   MCSymbol *LocalAlias = getSymbolPreferLocal(GI);
2044   if (LocalAlias != Name)
2045     OutStreamer->emitAssignment(LocalAlias, Expr);
2046 }
2047 
2048 void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
2049   if (!RS.needsSection())
2050     return;
2051 
2052   remarks::RemarkSerializer &RemarkSerializer = RS.getSerializer();
2053 
2054   std::optional<SmallString<128>> Filename;
2055   if (std::optional<StringRef> FilenameRef = RS.getFilename()) {
2056     Filename = *FilenameRef;
2057     sys::fs::make_absolute(*Filename);
2058     assert(!Filename->empty() && "The filename can't be empty.");
2059   }
2060 
2061   std::string Buf;
2062   raw_string_ostream OS(Buf);
2063   std::unique_ptr<remarks::MetaSerializer> MetaSerializer =
2064       Filename ? RemarkSerializer.metaSerializer(OS, Filename->str())
2065                : RemarkSerializer.metaSerializer(OS);
2066   MetaSerializer->emit();
2067 
2068   // Switch to the remarks section.
2069   MCSection *RemarksSection =
2070       OutContext.getObjectFileInfo()->getRemarksSection();
2071   OutStreamer->switchSection(RemarksSection);
2072 
2073   OutStreamer->emitBinaryData(OS.str());
2074 }
2075 
2076 bool AsmPrinter::doFinalization(Module &M) {
2077   // Set the MachineFunction to nullptr so that we can catch attempted
2078   // accesses to MF specific features at the module level and so that
2079   // we can conditionalize accesses based on whether or not it is nullptr.
2080   MF = nullptr;
2081 
2082   // Gather all GOT equivalent globals in the module. We really need two
2083   // passes over the globals: one to compute and another to avoid its emission
2084   // in EmitGlobalVariable, otherwise we would not be able to handle cases
2085   // where the got equivalent shows up before its use.
2086   computeGlobalGOTEquivs(M);
2087 
2088   // Emit global variables.
2089   for (const auto &G : M.globals())
2090     emitGlobalVariable(&G);
2091 
2092   // Emit remaining GOT equivalent globals.
2093   emitGlobalGOTEquivs();
2094 
2095   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
2096 
2097   // Emit linkage(XCOFF) and visibility info for declarations
2098   for (const Function &F : M) {
2099     if (!F.isDeclarationForLinker())
2100       continue;
2101 
2102     MCSymbol *Name = getSymbol(&F);
2103     // Function getSymbol gives us the function descriptor symbol for XCOFF.
2104 
2105     if (!TM.getTargetTriple().isOSBinFormatXCOFF()) {
2106       GlobalValue::VisibilityTypes V = F.getVisibility();
2107       if (V == GlobalValue::DefaultVisibility)
2108         continue;
2109 
2110       emitVisibility(Name, V, false);
2111       continue;
2112     }
2113 
2114     if (F.isIntrinsic())
2115       continue;
2116 
2117     // Handle the XCOFF case.
2118     // Variable `Name` is the function descriptor symbol (see above). Get the
2119     // function entry point symbol.
2120     MCSymbol *FnEntryPointSym = TLOF.getFunctionEntryPointSymbol(&F, TM);
2121     // Emit linkage for the function entry point.
2122     emitLinkage(&F, FnEntryPointSym);
2123 
2124     // Emit linkage for the function descriptor.
2125     emitLinkage(&F, Name);
2126   }
2127 
2128   // Emit the remarks section contents.
2129   // FIXME: Figure out when is the safest time to emit this section. It should
2130   // not come after debug info.
2131   if (remarks::RemarkStreamer *RS = M.getContext().getMainRemarkStreamer())
2132     emitRemarksSection(*RS);
2133 
2134   TLOF.emitModuleMetadata(*OutStreamer, M);
2135 
2136   if (TM.getTargetTriple().isOSBinFormatELF()) {
2137     MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
2138 
2139     // Output stubs for external and common global variables.
2140     MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
2141     if (!Stubs.empty()) {
2142       OutStreamer->switchSection(TLOF.getDataSection());
2143       const DataLayout &DL = M.getDataLayout();
2144 
2145       emitAlignment(Align(DL.getPointerSize()));
2146       for (const auto &Stub : Stubs) {
2147         OutStreamer->emitLabel(Stub.first);
2148         OutStreamer->emitSymbolValue(Stub.second.getPointer(),
2149                                      DL.getPointerSize());
2150       }
2151     }
2152   }
2153 
2154   if (TM.getTargetTriple().isOSBinFormatCOFF()) {
2155     MachineModuleInfoCOFF &MMICOFF =
2156         MMI->getObjFileInfo<MachineModuleInfoCOFF>();
2157 
2158     // Output stubs for external and common global variables.
2159     MachineModuleInfoCOFF::SymbolListTy Stubs = MMICOFF.GetGVStubList();
2160     if (!Stubs.empty()) {
2161       const DataLayout &DL = M.getDataLayout();
2162 
2163       for (const auto &Stub : Stubs) {
2164         SmallString<256> SectionName = StringRef(".rdata$");
2165         SectionName += Stub.first->getName();
2166         OutStreamer->switchSection(OutContext.getCOFFSection(
2167             SectionName,
2168             COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
2169                 COFF::IMAGE_SCN_LNK_COMDAT,
2170             SectionKind::getReadOnly(), Stub.first->getName(),
2171             COFF::IMAGE_COMDAT_SELECT_ANY));
2172         emitAlignment(Align(DL.getPointerSize()));
2173         OutStreamer->emitSymbolAttribute(Stub.first, MCSA_Global);
2174         OutStreamer->emitLabel(Stub.first);
2175         OutStreamer->emitSymbolValue(Stub.second.getPointer(),
2176                                      DL.getPointerSize());
2177       }
2178     }
2179   }
2180 
2181   // This needs to happen before emitting debug information since that can end
2182   // arbitrary sections.
2183   if (auto *TS = OutStreamer->getTargetStreamer())
2184     TS->emitConstantPools();
2185 
2186   // Emit Stack maps before any debug info. Mach-O requires that no data or
2187   // text sections come after debug info has been emitted. This matters for
2188   // stack maps as they are arbitrary data, and may even have a custom format
2189   // through user plugins.
2190   emitStackMaps();
2191 
2192   // Finalize debug and EH information.
2193   for (const HandlerInfo &HI : Handlers) {
2194     NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2195                        HI.TimerGroupDescription, TimePassesIsEnabled);
2196     HI.Handler->endModule();
2197   }
2198 
2199   // This deletes all the ephemeral handlers that AsmPrinter added, while
2200   // keeping all the user-added handlers alive until the AsmPrinter is
2201   // destroyed.
2202   Handlers.erase(Handlers.begin() + NumUserHandlers, Handlers.end());
2203   DD = nullptr;
2204 
2205   // If the target wants to know about weak references, print them all.
2206   if (MAI->getWeakRefDirective()) {
2207     // FIXME: This is not lazy, it would be nice to only print weak references
2208     // to stuff that is actually used.  Note that doing so would require targets
2209     // to notice uses in operands (due to constant exprs etc).  This should
2210     // happen with the MC stuff eventually.
2211 
2212     // Print out module-level global objects here.
2213     for (const auto &GO : M.global_objects()) {
2214       if (!GO.hasExternalWeakLinkage())
2215         continue;
2216       OutStreamer->emitSymbolAttribute(getSymbol(&GO), MCSA_WeakReference);
2217     }
2218     if (shouldEmitWeakSwiftAsyncExtendedFramePointerFlags()) {
2219       auto SymbolName = "swift_async_extendedFramePointerFlags";
2220       auto Global = M.getGlobalVariable(SymbolName);
2221       if (!Global) {
2222         auto Int8PtrTy = Type::getInt8PtrTy(M.getContext());
2223         Global = new GlobalVariable(M, Int8PtrTy, false,
2224                                     GlobalValue::ExternalWeakLinkage, nullptr,
2225                                     SymbolName);
2226         OutStreamer->emitSymbolAttribute(getSymbol(Global), MCSA_WeakReference);
2227       }
2228     }
2229   }
2230 
2231   // Print aliases in topological order, that is, for each alias a = b,
2232   // b must be printed before a.
2233   // This is because on some targets (e.g. PowerPC) linker expects aliases in
2234   // such an order to generate correct TOC information.
2235   SmallVector<const GlobalAlias *, 16> AliasStack;
2236   SmallPtrSet<const GlobalAlias *, 16> AliasVisited;
2237   for (const auto &Alias : M.aliases()) {
2238     for (const GlobalAlias *Cur = &Alias; Cur;
2239          Cur = dyn_cast<GlobalAlias>(Cur->getAliasee())) {
2240       if (!AliasVisited.insert(Cur).second)
2241         break;
2242       AliasStack.push_back(Cur);
2243     }
2244     for (const GlobalAlias *AncestorAlias : llvm::reverse(AliasStack))
2245       emitGlobalAlias(M, *AncestorAlias);
2246     AliasStack.clear();
2247   }
2248   for (const auto &IFunc : M.ifuncs())
2249     emitGlobalIFunc(M, IFunc);
2250 
2251   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
2252   assert(MI && "AsmPrinter didn't require GCModuleInfo?");
2253   for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
2254     if (GCMetadataPrinter *MP = getOrCreateGCPrinter(**--I))
2255       MP->finishAssembly(M, *MI, *this);
2256 
2257   // Emit llvm.ident metadata in an '.ident' directive.
2258   emitModuleIdents(M);
2259 
2260   // Emit bytes for llvm.commandline metadata.
2261   emitModuleCommandLines(M);
2262 
2263   // Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if
2264   // split-stack is used.
2265   if (TM.getTargetTriple().isOSBinFormatELF() && HasSplitStack) {
2266     OutStreamer->switchSection(OutContext.getELFSection(".note.GNU-split-stack",
2267                                                         ELF::SHT_PROGBITS, 0));
2268     if (HasNoSplitStack)
2269       OutStreamer->switchSection(OutContext.getELFSection(
2270           ".note.GNU-no-split-stack", ELF::SHT_PROGBITS, 0));
2271   }
2272 
2273   // If we don't have any trampolines, then we don't require stack memory
2274   // to be executable. Some targets have a directive to declare this.
2275   Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
2276   if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
2277     if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
2278       OutStreamer->switchSection(S);
2279 
2280   if (TM.Options.EmitAddrsig) {
2281     // Emit address-significance attributes for all globals.
2282     OutStreamer->emitAddrsig();
2283     for (const GlobalValue &GV : M.global_values()) {
2284       if (!GV.use_empty() && !GV.isThreadLocal() &&
2285           !GV.hasDLLImportStorageClass() && !GV.getName().startswith("llvm.") &&
2286           !GV.hasAtLeastLocalUnnamedAddr())
2287         OutStreamer->emitAddrsigSym(getSymbol(&GV));
2288     }
2289   }
2290 
2291   // Emit symbol partition specifications (ELF only).
2292   if (TM.getTargetTriple().isOSBinFormatELF()) {
2293     unsigned UniqueID = 0;
2294     for (const GlobalValue &GV : M.global_values()) {
2295       if (!GV.hasPartition() || GV.isDeclarationForLinker() ||
2296           GV.getVisibility() != GlobalValue::DefaultVisibility)
2297         continue;
2298 
2299       OutStreamer->switchSection(
2300           OutContext.getELFSection(".llvm_sympart", ELF::SHT_LLVM_SYMPART, 0, 0,
2301                                    "", false, ++UniqueID, nullptr));
2302       OutStreamer->emitBytes(GV.getPartition());
2303       OutStreamer->emitZeros(1);
2304       OutStreamer->emitValue(
2305           MCSymbolRefExpr::create(getSymbol(&GV), OutContext),
2306           MAI->getCodePointerSize());
2307     }
2308   }
2309 
2310   // Allow the target to emit any magic that it wants at the end of the file,
2311   // after everything else has gone out.
2312   emitEndOfAsmFile(M);
2313 
2314   MMI = nullptr;
2315   AddrLabelSymbols = nullptr;
2316 
2317   OutStreamer->finish();
2318   OutStreamer->reset();
2319   OwnedMLI.reset();
2320   OwnedMDT.reset();
2321 
2322   return false;
2323 }
2324 
2325 MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) {
2326   auto Res = MBBSectionExceptionSyms.try_emplace(MBB.getSectionIDNum());
2327   if (Res.second)
2328     Res.first->second = createTempSymbol("exception");
2329   return Res.first->second;
2330 }
2331 
2332 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
2333   this->MF = &MF;
2334   const Function &F = MF.getFunction();
2335 
2336   // Record that there are split-stack functions, so we will emit a special
2337   // section to tell the linker.
2338   if (MF.shouldSplitStack()) {
2339     HasSplitStack = true;
2340 
2341     if (!MF.getFrameInfo().needsSplitStackProlog())
2342       HasNoSplitStack = true;
2343   } else
2344     HasNoSplitStack = true;
2345 
2346   // Get the function symbol.
2347   if (!MAI->needsFunctionDescriptors()) {
2348     CurrentFnSym = getSymbol(&MF.getFunction());
2349   } else {
2350     assert(TM.getTargetTriple().isOSAIX() &&
2351            "Only AIX uses the function descriptor hooks.");
2352     // AIX is unique here in that the name of the symbol emitted for the
2353     // function body does not have the same name as the source function's
2354     // C-linkage name.
2355     assert(CurrentFnDescSym && "The function descriptor symbol needs to be"
2356                                " initalized first.");
2357 
2358     // Get the function entry point symbol.
2359     CurrentFnSym = getObjFileLowering().getFunctionEntryPointSymbol(&F, TM);
2360   }
2361 
2362   CurrentFnSymForSize = CurrentFnSym;
2363   CurrentFnBegin = nullptr;
2364   CurrentFnBeginLocal = nullptr;
2365   CurrentSectionBeginSym = nullptr;
2366   MBBSectionRanges.clear();
2367   MBBSectionExceptionSyms.clear();
2368   bool NeedsLocalForSize = MAI->needsLocalForSize();
2369   if (F.hasFnAttribute("patchable-function-entry") ||
2370       F.hasFnAttribute("function-instrument") ||
2371       F.hasFnAttribute("xray-instruction-threshold") ||
2372       needFuncLabels(MF) || NeedsLocalForSize ||
2373       MF.getTarget().Options.EmitStackSizeSection || MF.hasBBLabels()) {
2374     CurrentFnBegin = createTempSymbol("func_begin");
2375     if (NeedsLocalForSize)
2376       CurrentFnSymForSize = CurrentFnBegin;
2377   }
2378 
2379   ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
2380 }
2381 
2382 namespace {
2383 
2384 // Keep track the alignment, constpool entries per Section.
2385   struct SectionCPs {
2386     MCSection *S;
2387     Align Alignment;
2388     SmallVector<unsigned, 4> CPEs;
2389 
2390     SectionCPs(MCSection *s, Align a) : S(s), Alignment(a) {}
2391   };
2392 
2393 } // end anonymous namespace
2394 
2395 /// EmitConstantPool - Print to the current output stream assembly
2396 /// representations of the constants in the constant pool MCP. This is
2397 /// used to print out constants which have been "spilled to memory" by
2398 /// the code generator.
2399 void AsmPrinter::emitConstantPool() {
2400   const MachineConstantPool *MCP = MF->getConstantPool();
2401   const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
2402   if (CP.empty()) return;
2403 
2404   // Calculate sections for constant pool entries. We collect entries to go into
2405   // the same section together to reduce amount of section switch statements.
2406   SmallVector<SectionCPs, 4> CPSections;
2407   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
2408     const MachineConstantPoolEntry &CPE = CP[i];
2409     Align Alignment = CPE.getAlign();
2410 
2411     SectionKind Kind = CPE.getSectionKind(&getDataLayout());
2412 
2413     const Constant *C = nullptr;
2414     if (!CPE.isMachineConstantPoolEntry())
2415       C = CPE.Val.ConstVal;
2416 
2417     MCSection *S = getObjFileLowering().getSectionForConstant(
2418         getDataLayout(), Kind, C, Alignment);
2419 
2420     // The number of sections are small, just do a linear search from the
2421     // last section to the first.
2422     bool Found = false;
2423     unsigned SecIdx = CPSections.size();
2424     while (SecIdx != 0) {
2425       if (CPSections[--SecIdx].S == S) {
2426         Found = true;
2427         break;
2428       }
2429     }
2430     if (!Found) {
2431       SecIdx = CPSections.size();
2432       CPSections.push_back(SectionCPs(S, Alignment));
2433     }
2434 
2435     if (Alignment > CPSections[SecIdx].Alignment)
2436       CPSections[SecIdx].Alignment = Alignment;
2437     CPSections[SecIdx].CPEs.push_back(i);
2438   }
2439 
2440   // Now print stuff into the calculated sections.
2441   const MCSection *CurSection = nullptr;
2442   unsigned Offset = 0;
2443   for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
2444     for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
2445       unsigned CPI = CPSections[i].CPEs[j];
2446       MCSymbol *Sym = GetCPISymbol(CPI);
2447       if (!Sym->isUndefined())
2448         continue;
2449 
2450       if (CurSection != CPSections[i].S) {
2451         OutStreamer->switchSection(CPSections[i].S);
2452         emitAlignment(Align(CPSections[i].Alignment));
2453         CurSection = CPSections[i].S;
2454         Offset = 0;
2455       }
2456 
2457       MachineConstantPoolEntry CPE = CP[CPI];
2458 
2459       // Emit inter-object padding for alignment.
2460       unsigned NewOffset = alignTo(Offset, CPE.getAlign());
2461       OutStreamer->emitZeros(NewOffset - Offset);
2462 
2463       Offset = NewOffset + CPE.getSizeInBytes(getDataLayout());
2464 
2465       OutStreamer->emitLabel(Sym);
2466       if (CPE.isMachineConstantPoolEntry())
2467         emitMachineConstantPoolValue(CPE.Val.MachineCPVal);
2468       else
2469         emitGlobalConstant(getDataLayout(), CPE.Val.ConstVal);
2470     }
2471   }
2472 }
2473 
2474 // Print assembly representations of the jump tables used by the current
2475 // function.
2476 void AsmPrinter::emitJumpTableInfo() {
2477   const DataLayout &DL = MF->getDataLayout();
2478   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
2479   if (!MJTI) return;
2480   if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_Inline) return;
2481   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
2482   if (JT.empty()) return;
2483 
2484   // Pick the directive to use to print the jump table entries, and switch to
2485   // the appropriate section.
2486   const Function &F = MF->getFunction();
2487   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
2488   bool JTInDiffSection = !TLOF.shouldPutJumpTableInFunctionSection(
2489       MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32,
2490       F);
2491   if (JTInDiffSection) {
2492     // Drop it in the readonly section.
2493     MCSection *ReadOnlySection = TLOF.getSectionForJumpTable(F, TM);
2494     OutStreamer->switchSection(ReadOnlySection);
2495   }
2496 
2497   emitAlignment(Align(MJTI->getEntryAlignment(DL)));
2498 
2499   // Jump tables in code sections are marked with a data_region directive
2500   // where that's supported.
2501   if (!JTInDiffSection)
2502     OutStreamer->emitDataRegion(MCDR_DataRegionJT32);
2503 
2504   for (unsigned JTI = 0, e = JT.size(); JTI != e; ++JTI) {
2505     const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
2506 
2507     // If this jump table was deleted, ignore it.
2508     if (JTBBs.empty()) continue;
2509 
2510     // For the EK_LabelDifference32 entry, if using .set avoids a relocation,
2511     /// emit a .set directive for each unique entry.
2512     if (MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 &&
2513         MAI->doesSetDirectiveSuppressReloc()) {
2514       SmallPtrSet<const MachineBasicBlock*, 16> EmittedSets;
2515       const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
2516       const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF,JTI,OutContext);
2517       for (const MachineBasicBlock *MBB : JTBBs) {
2518         if (!EmittedSets.insert(MBB).second)
2519           continue;
2520 
2521         // .set LJTSet, LBB32-base
2522         const MCExpr *LHS =
2523           MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
2524         OutStreamer->emitAssignment(GetJTSetSymbol(JTI, MBB->getNumber()),
2525                                     MCBinaryExpr::createSub(LHS, Base,
2526                                                             OutContext));
2527       }
2528     }
2529 
2530     // On some targets (e.g. Darwin) we want to emit two consecutive labels
2531     // before each jump table.  The first label is never referenced, but tells
2532     // the assembler and linker the extents of the jump table object.  The
2533     // second label is actually referenced by the code.
2534     if (JTInDiffSection && DL.hasLinkerPrivateGlobalPrefix())
2535       // FIXME: This doesn't have to have any specific name, just any randomly
2536       // named and numbered local label started with 'l' would work.  Simplify
2537       // GetJTISymbol.
2538       OutStreamer->emitLabel(GetJTISymbol(JTI, true));
2539 
2540     MCSymbol* JTISymbol = GetJTISymbol(JTI);
2541     OutStreamer->emitLabel(JTISymbol);
2542 
2543     for (const MachineBasicBlock *MBB : JTBBs)
2544       emitJumpTableEntry(MJTI, MBB, JTI);
2545   }
2546   if (!JTInDiffSection)
2547     OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
2548 }
2549 
2550 /// EmitJumpTableEntry - Emit a jump table entry for the specified MBB to the
2551 /// current stream.
2552 void AsmPrinter::emitJumpTableEntry(const MachineJumpTableInfo *MJTI,
2553                                     const MachineBasicBlock *MBB,
2554                                     unsigned UID) const {
2555   assert(MBB && MBB->getNumber() >= 0 && "Invalid basic block");
2556   const MCExpr *Value = nullptr;
2557   switch (MJTI->getEntryKind()) {
2558   case MachineJumpTableInfo::EK_Inline:
2559     llvm_unreachable("Cannot emit EK_Inline jump table entry");
2560   case MachineJumpTableInfo::EK_Custom32:
2561     Value = MF->getSubtarget().getTargetLowering()->LowerCustomJumpTableEntry(
2562         MJTI, MBB, UID, OutContext);
2563     break;
2564   case MachineJumpTableInfo::EK_BlockAddress:
2565     // EK_BlockAddress - Each entry is a plain address of block, e.g.:
2566     //     .word LBB123
2567     Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
2568     break;
2569   case MachineJumpTableInfo::EK_GPRel32BlockAddress: {
2570     // EK_GPRel32BlockAddress - Each entry is an address of block, encoded
2571     // with a relocation as gp-relative, e.g.:
2572     //     .gprel32 LBB123
2573     MCSymbol *MBBSym = MBB->getSymbol();
2574     OutStreamer->emitGPRel32Value(MCSymbolRefExpr::create(MBBSym, OutContext));
2575     return;
2576   }
2577 
2578   case MachineJumpTableInfo::EK_GPRel64BlockAddress: {
2579     // EK_GPRel64BlockAddress - Each entry is an address of block, encoded
2580     // with a relocation as gp-relative, e.g.:
2581     //     .gpdword LBB123
2582     MCSymbol *MBBSym = MBB->getSymbol();
2583     OutStreamer->emitGPRel64Value(MCSymbolRefExpr::create(MBBSym, OutContext));
2584     return;
2585   }
2586 
2587   case MachineJumpTableInfo::EK_LabelDifference32: {
2588     // Each entry is the address of the block minus the address of the jump
2589     // table. This is used for PIC jump tables where gprel32 is not supported.
2590     // e.g.:
2591     //      .word LBB123 - LJTI1_2
2592     // If the .set directive avoids relocations, this is emitted as:
2593     //      .set L4_5_set_123, LBB123 - LJTI1_2
2594     //      .word L4_5_set_123
2595     if (MAI->doesSetDirectiveSuppressReloc()) {
2596       Value = MCSymbolRefExpr::create(GetJTSetSymbol(UID, MBB->getNumber()),
2597                                       OutContext);
2598       break;
2599     }
2600     Value = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
2601     const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
2602     const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, UID, OutContext);
2603     Value = MCBinaryExpr::createSub(Value, Base, OutContext);
2604     break;
2605   }
2606   }
2607 
2608   assert(Value && "Unknown entry kind!");
2609 
2610   unsigned EntrySize = MJTI->getEntrySize(getDataLayout());
2611   OutStreamer->emitValue(Value, EntrySize);
2612 }
2613 
2614 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
2615 /// special global used by LLVM.  If so, emit it and return true, otherwise
2616 /// do nothing and return false.
2617 bool AsmPrinter::emitSpecialLLVMGlobal(const GlobalVariable *GV) {
2618   if (GV->getName() == "llvm.used") {
2619     if (MAI->hasNoDeadStrip())    // No need to emit this at all.
2620       emitLLVMUsedList(cast<ConstantArray>(GV->getInitializer()));
2621     return true;
2622   }
2623 
2624   // Ignore debug and non-emitted data.  This handles llvm.compiler.used.
2625   if (GV->getSection() == "llvm.metadata" ||
2626       GV->hasAvailableExternallyLinkage())
2627     return true;
2628 
2629   if (!GV->hasAppendingLinkage()) return false;
2630 
2631   assert(GV->hasInitializer() && "Not a special LLVM global!");
2632 
2633   if (GV->getName() == "llvm.global_ctors") {
2634     emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
2635                        /* isCtor */ true);
2636 
2637     return true;
2638   }
2639 
2640   if (GV->getName() == "llvm.global_dtors") {
2641     emitXXStructorList(GV->getParent()->getDataLayout(), GV->getInitializer(),
2642                        /* isCtor */ false);
2643 
2644     return true;
2645   }
2646 
2647   report_fatal_error("unknown special variable");
2648 }
2649 
2650 /// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
2651 /// global in the specified llvm.used list.
2652 void AsmPrinter::emitLLVMUsedList(const ConstantArray *InitList) {
2653   // Should be an array of 'i8*'.
2654   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
2655     const GlobalValue *GV =
2656       dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
2657     if (GV)
2658       OutStreamer->emitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
2659   }
2660 }
2661 
2662 void AsmPrinter::preprocessXXStructorList(const DataLayout &DL,
2663                                           const Constant *List,
2664                                           SmallVector<Structor, 8> &Structors) {
2665   // Should be an array of '{ i32, void ()*, i8* }' structs.  The first value is
2666   // the init priority.
2667   if (!isa<ConstantArray>(List))
2668     return;
2669 
2670   // Gather the structors in a form that's convenient for sorting by priority.
2671   for (Value *O : cast<ConstantArray>(List)->operands()) {
2672     auto *CS = cast<ConstantStruct>(O);
2673     if (CS->getOperand(1)->isNullValue())
2674       break; // Found a null terminator, skip the rest.
2675     ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
2676     if (!Priority)
2677       continue; // Malformed.
2678     Structors.push_back(Structor());
2679     Structor &S = Structors.back();
2680     S.Priority = Priority->getLimitedValue(65535);
2681     S.Func = CS->getOperand(1);
2682     if (!CS->getOperand(2)->isNullValue()) {
2683       if (TM.getTargetTriple().isOSAIX())
2684         llvm::report_fatal_error(
2685             "associated data of XXStructor list is not yet supported on AIX");
2686       S.ComdatKey =
2687           dyn_cast<GlobalValue>(CS->getOperand(2)->stripPointerCasts());
2688     }
2689   }
2690 
2691   // Emit the function pointers in the target-specific order
2692   llvm::stable_sort(Structors, [](const Structor &L, const Structor &R) {
2693     return L.Priority < R.Priority;
2694   });
2695 }
2696 
2697 /// EmitXXStructorList - Emit the ctor or dtor list taking into account the init
2698 /// priority.
2699 void AsmPrinter::emitXXStructorList(const DataLayout &DL, const Constant *List,
2700                                     bool IsCtor) {
2701   SmallVector<Structor, 8> Structors;
2702   preprocessXXStructorList(DL, List, Structors);
2703   if (Structors.empty())
2704     return;
2705 
2706   // Emit the structors in reverse order if we are using the .ctor/.dtor
2707   // initialization scheme.
2708   if (!TM.Options.UseInitArray)
2709     std::reverse(Structors.begin(), Structors.end());
2710 
2711   const Align Align = DL.getPointerPrefAlignment();
2712   for (Structor &S : Structors) {
2713     const TargetLoweringObjectFile &Obj = getObjFileLowering();
2714     const MCSymbol *KeySym = nullptr;
2715     if (GlobalValue *GV = S.ComdatKey) {
2716       if (GV->isDeclarationForLinker())
2717         // If the associated variable is not defined in this module
2718         // (it might be available_externally, or have been an
2719         // available_externally definition that was dropped by the
2720         // EliminateAvailableExternally pass), some other TU
2721         // will provide its dynamic initializer.
2722         continue;
2723 
2724       KeySym = getSymbol(GV);
2725     }
2726 
2727     MCSection *OutputSection =
2728         (IsCtor ? Obj.getStaticCtorSection(S.Priority, KeySym)
2729                 : Obj.getStaticDtorSection(S.Priority, KeySym));
2730     OutStreamer->switchSection(OutputSection);
2731     if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
2732       emitAlignment(Align);
2733     emitXXStructor(DL, S.Func);
2734   }
2735 }
2736 
2737 void AsmPrinter::emitModuleIdents(Module &M) {
2738   if (!MAI->hasIdentDirective())
2739     return;
2740 
2741   if (const NamedMDNode *NMD = M.getNamedMetadata("llvm.ident")) {
2742     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2743       const MDNode *N = NMD->getOperand(i);
2744       assert(N->getNumOperands() == 1 &&
2745              "llvm.ident metadata entry can have only one operand");
2746       const MDString *S = cast<MDString>(N->getOperand(0));
2747       OutStreamer->emitIdent(S->getString());
2748     }
2749   }
2750 }
2751 
2752 void AsmPrinter::emitModuleCommandLines(Module &M) {
2753   MCSection *CommandLine = getObjFileLowering().getSectionForCommandLines();
2754   if (!CommandLine)
2755     return;
2756 
2757   const NamedMDNode *NMD = M.getNamedMetadata("llvm.commandline");
2758   if (!NMD || !NMD->getNumOperands())
2759     return;
2760 
2761   OutStreamer->pushSection();
2762   OutStreamer->switchSection(CommandLine);
2763   OutStreamer->emitZeros(1);
2764   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2765     const MDNode *N = NMD->getOperand(i);
2766     assert(N->getNumOperands() == 1 &&
2767            "llvm.commandline metadata entry can have only one operand");
2768     const MDString *S = cast<MDString>(N->getOperand(0));
2769     OutStreamer->emitBytes(S->getString());
2770     OutStreamer->emitZeros(1);
2771   }
2772   OutStreamer->popSection();
2773 }
2774 
2775 //===--------------------------------------------------------------------===//
2776 // Emission and print routines
2777 //
2778 
2779 /// Emit a byte directive and value.
2780 ///
2781 void AsmPrinter::emitInt8(int Value) const { OutStreamer->emitInt8(Value); }
2782 
2783 /// Emit a short directive and value.
2784 void AsmPrinter::emitInt16(int Value) const { OutStreamer->emitInt16(Value); }
2785 
2786 /// Emit a long directive and value.
2787 void AsmPrinter::emitInt32(int Value) const { OutStreamer->emitInt32(Value); }
2788 
2789 /// Emit a long long directive and value.
2790 void AsmPrinter::emitInt64(uint64_t Value) const {
2791   OutStreamer->emitInt64(Value);
2792 }
2793 
2794 /// Emit something like ".long Hi-Lo" where the size in bytes of the directive
2795 /// is specified by Size and Hi/Lo specify the labels. This implicitly uses
2796 /// .set if it avoids relocations.
2797 void AsmPrinter::emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo,
2798                                      unsigned Size) const {
2799   OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, Size);
2800 }
2801 
2802 /// EmitLabelPlusOffset - Emit something like ".long Label+Offset"
2803 /// where the size in bytes of the directive is specified by Size and Label
2804 /// specifies the label.  This implicitly uses .set if it is available.
2805 void AsmPrinter::emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset,
2806                                      unsigned Size,
2807                                      bool IsSectionRelative) const {
2808   if (MAI->needsDwarfSectionOffsetDirective() && IsSectionRelative) {
2809     OutStreamer->emitCOFFSecRel32(Label, Offset);
2810     if (Size > 4)
2811       OutStreamer->emitZeros(Size - 4);
2812     return;
2813   }
2814 
2815   // Emit Label+Offset (or just Label if Offset is zero)
2816   const MCExpr *Expr = MCSymbolRefExpr::create(Label, OutContext);
2817   if (Offset)
2818     Expr = MCBinaryExpr::createAdd(
2819         Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
2820 
2821   OutStreamer->emitValue(Expr, Size);
2822 }
2823 
2824 //===----------------------------------------------------------------------===//
2825 
2826 // EmitAlignment - Emit an alignment directive to the specified power of
2827 // two boundary.  If a global value is specified, and if that global has
2828 // an explicit alignment requested, it will override the alignment request
2829 // if required for correctness.
2830 void AsmPrinter::emitAlignment(Align Alignment, const GlobalObject *GV,
2831                                unsigned MaxBytesToEmit) const {
2832   if (GV)
2833     Alignment = getGVAlignment(GV, GV->getParent()->getDataLayout(), Alignment);
2834 
2835   if (Alignment == Align(1))
2836     return; // 1-byte aligned: no need to emit alignment.
2837 
2838   if (getCurrentSection()->getKind().isText()) {
2839     const MCSubtargetInfo *STI = nullptr;
2840     if (this->MF)
2841       STI = &getSubtargetInfo();
2842     else
2843       STI = TM.getMCSubtargetInfo();
2844     OutStreamer->emitCodeAlignment(Alignment, STI, MaxBytesToEmit);
2845   } else
2846     OutStreamer->emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
2847 }
2848 
2849 //===----------------------------------------------------------------------===//
2850 // Constant emission.
2851 //===----------------------------------------------------------------------===//
2852 
2853 const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
2854   MCContext &Ctx = OutContext;
2855 
2856   if (CV->isNullValue() || isa<UndefValue>(CV))
2857     return MCConstantExpr::create(0, Ctx);
2858 
2859   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV))
2860     return MCConstantExpr::create(CI->getZExtValue(), Ctx);
2861 
2862   if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
2863     return MCSymbolRefExpr::create(getSymbol(GV), Ctx);
2864 
2865   if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV))
2866     return MCSymbolRefExpr::create(GetBlockAddressSymbol(BA), Ctx);
2867 
2868   if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV))
2869     return getObjFileLowering().lowerDSOLocalEquivalent(Equiv, TM);
2870 
2871   if (const NoCFIValue *NC = dyn_cast<NoCFIValue>(CV))
2872     return MCSymbolRefExpr::create(getSymbol(NC->getGlobalValue()), Ctx);
2873 
2874   const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
2875   if (!CE) {
2876     llvm_unreachable("Unknown constant value to lower!");
2877   }
2878 
2879   // The constant expression opcodes are limited to those that are necessary
2880   // to represent relocations on supported targets. Expressions involving only
2881   // constant addresses are constant folded instead.
2882   switch (CE->getOpcode()) {
2883   default:
2884     break; // Error
2885   case Instruction::AddrSpaceCast: {
2886     const Constant *Op = CE->getOperand(0);
2887     unsigned DstAS = CE->getType()->getPointerAddressSpace();
2888     unsigned SrcAS = Op->getType()->getPointerAddressSpace();
2889     if (TM.isNoopAddrSpaceCast(SrcAS, DstAS))
2890       return lowerConstant(Op);
2891 
2892     break; // Error
2893   }
2894   case Instruction::GetElementPtr: {
2895     // Generate a symbolic expression for the byte address
2896     APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0);
2897     cast<GEPOperator>(CE)->accumulateConstantOffset(getDataLayout(), OffsetAI);
2898 
2899     const MCExpr *Base = lowerConstant(CE->getOperand(0));
2900     if (!OffsetAI)
2901       return Base;
2902 
2903     int64_t Offset = OffsetAI.getSExtValue();
2904     return MCBinaryExpr::createAdd(Base, MCConstantExpr::create(Offset, Ctx),
2905                                    Ctx);
2906   }
2907 
2908   case Instruction::Trunc:
2909     // We emit the value and depend on the assembler to truncate the generated
2910     // expression properly.  This is important for differences between
2911     // blockaddress labels.  Since the two labels are in the same function, it
2912     // is reasonable to treat their delta as a 32-bit value.
2913     [[fallthrough]];
2914   case Instruction::BitCast:
2915     return lowerConstant(CE->getOperand(0));
2916 
2917   case Instruction::IntToPtr: {
2918     const DataLayout &DL = getDataLayout();
2919 
2920     // Handle casts to pointers by changing them into casts to the appropriate
2921     // integer type.  This promotes constant folding and simplifies this code.
2922     Constant *Op = CE->getOperand(0);
2923     Op = ConstantExpr::getIntegerCast(Op, DL.getIntPtrType(CV->getType()),
2924                                       false/*ZExt*/);
2925     return lowerConstant(Op);
2926   }
2927 
2928   case Instruction::PtrToInt: {
2929     const DataLayout &DL = getDataLayout();
2930 
2931     // Support only foldable casts to/from pointers that can be eliminated by
2932     // changing the pointer to the appropriately sized integer type.
2933     Constant *Op = CE->getOperand(0);
2934     Type *Ty = CE->getType();
2935 
2936     const MCExpr *OpExpr = lowerConstant(Op);
2937 
2938     // We can emit the pointer value into this slot if the slot is an
2939     // integer slot equal to the size of the pointer.
2940     //
2941     // If the pointer is larger than the resultant integer, then
2942     // as with Trunc just depend on the assembler to truncate it.
2943     if (DL.getTypeAllocSize(Ty).getFixedValue() <=
2944         DL.getTypeAllocSize(Op->getType()).getFixedValue())
2945       return OpExpr;
2946 
2947     break; // Error
2948   }
2949 
2950   case Instruction::Sub: {
2951     GlobalValue *LHSGV;
2952     APInt LHSOffset;
2953     DSOLocalEquivalent *DSOEquiv;
2954     if (IsConstantOffsetFromGlobal(CE->getOperand(0), LHSGV, LHSOffset,
2955                                    getDataLayout(), &DSOEquiv)) {
2956       GlobalValue *RHSGV;
2957       APInt RHSOffset;
2958       if (IsConstantOffsetFromGlobal(CE->getOperand(1), RHSGV, RHSOffset,
2959                                      getDataLayout())) {
2960         const MCExpr *RelocExpr =
2961             getObjFileLowering().lowerRelativeReference(LHSGV, RHSGV, TM);
2962         if (!RelocExpr) {
2963           const MCExpr *LHSExpr =
2964               MCSymbolRefExpr::create(getSymbol(LHSGV), Ctx);
2965           if (DSOEquiv &&
2966               getObjFileLowering().supportDSOLocalEquivalentLowering())
2967             LHSExpr =
2968                 getObjFileLowering().lowerDSOLocalEquivalent(DSOEquiv, TM);
2969           RelocExpr = MCBinaryExpr::createSub(
2970               LHSExpr, MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx), Ctx);
2971         }
2972         int64_t Addend = (LHSOffset - RHSOffset).getSExtValue();
2973         if (Addend != 0)
2974           RelocExpr = MCBinaryExpr::createAdd(
2975               RelocExpr, MCConstantExpr::create(Addend, Ctx), Ctx);
2976         return RelocExpr;
2977       }
2978     }
2979 
2980     const MCExpr *LHS = lowerConstant(CE->getOperand(0));
2981     const MCExpr *RHS = lowerConstant(CE->getOperand(1));
2982     return MCBinaryExpr::createSub(LHS, RHS, Ctx);
2983     break;
2984   }
2985 
2986   case Instruction::Add: {
2987     const MCExpr *LHS = lowerConstant(CE->getOperand(0));
2988     const MCExpr *RHS = lowerConstant(CE->getOperand(1));
2989     return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
2990   }
2991   }
2992 
2993   // If the code isn't optimized, there may be outstanding folding
2994   // opportunities. Attempt to fold the expression using DataLayout as a
2995   // last resort before giving up.
2996   Constant *C = ConstantFoldConstant(CE, getDataLayout());
2997   if (C != CE)
2998     return lowerConstant(C);
2999 
3000   // Otherwise report the problem to the user.
3001   std::string S;
3002   raw_string_ostream OS(S);
3003   OS << "Unsupported expression in static initializer: ";
3004   CE->printAsOperand(OS, /*PrintType=*/false,
3005                      !MF ? nullptr : MF->getFunction().getParent());
3006   report_fatal_error(Twine(OS.str()));
3007 }
3008 
3009 static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *C,
3010                                    AsmPrinter &AP,
3011                                    const Constant *BaseCV = nullptr,
3012                                    uint64_t Offset = 0,
3013                                    AsmPrinter::AliasMapTy *AliasList = nullptr);
3014 
3015 static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP);
3016 static void emitGlobalConstantFP(APFloat APF, Type *ET, AsmPrinter &AP);
3017 
3018 /// isRepeatedByteSequence - Determine whether the given value is
3019 /// composed of a repeated sequence of identical bytes and return the
3020 /// byte value.  If it is not a repeated sequence, return -1.
3021 static int isRepeatedByteSequence(const ConstantDataSequential *V) {
3022   StringRef Data = V->getRawDataValues();
3023   assert(!Data.empty() && "Empty aggregates should be CAZ node");
3024   char C = Data[0];
3025   for (unsigned i = 1, e = Data.size(); i != e; ++i)
3026     if (Data[i] != C) return -1;
3027   return static_cast<uint8_t>(C); // Ensure 255 is not returned as -1.
3028 }
3029 
3030 /// isRepeatedByteSequence - Determine whether the given value is
3031 /// composed of a repeated sequence of identical bytes and return the
3032 /// byte value.  If it is not a repeated sequence, return -1.
3033 static int isRepeatedByteSequence(const Value *V, const DataLayout &DL) {
3034   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
3035     uint64_t Size = DL.getTypeAllocSizeInBits(V->getType());
3036     assert(Size % 8 == 0);
3037 
3038     // Extend the element to take zero padding into account.
3039     APInt Value = CI->getValue().zext(Size);
3040     if (!Value.isSplat(8))
3041       return -1;
3042 
3043     return Value.zextOrTrunc(8).getZExtValue();
3044   }
3045   if (const ConstantArray *CA = dyn_cast<ConstantArray>(V)) {
3046     // Make sure all array elements are sequences of the same repeated
3047     // byte.
3048     assert(CA->getNumOperands() != 0 && "Should be a CAZ");
3049     Constant *Op0 = CA->getOperand(0);
3050     int Byte = isRepeatedByteSequence(Op0, DL);
3051     if (Byte == -1)
3052       return -1;
3053 
3054     // All array elements must be equal.
3055     for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i)
3056       if (CA->getOperand(i) != Op0)
3057         return -1;
3058     return Byte;
3059   }
3060 
3061   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V))
3062     return isRepeatedByteSequence(CDS);
3063 
3064   return -1;
3065 }
3066 
3067 static void emitGlobalAliasInline(AsmPrinter &AP, uint64_t Offset,
3068                                   AsmPrinter::AliasMapTy *AliasList) {
3069   if (AliasList) {
3070     auto AliasIt = AliasList->find(Offset);
3071     if (AliasIt != AliasList->end()) {
3072       for (const GlobalAlias *GA : AliasIt->second)
3073         AP.OutStreamer->emitLabel(AP.getSymbol(GA));
3074       AliasList->erase(Offset);
3075     }
3076   }
3077 }
3078 
3079 static void emitGlobalConstantDataSequential(
3080     const DataLayout &DL, const ConstantDataSequential *CDS, AsmPrinter &AP,
3081     AsmPrinter::AliasMapTy *AliasList) {
3082   // See if we can aggregate this into a .fill, if so, emit it as such.
3083   int Value = isRepeatedByteSequence(CDS, DL);
3084   if (Value != -1) {
3085     uint64_t Bytes = DL.getTypeAllocSize(CDS->getType());
3086     // Don't emit a 1-byte object as a .fill.
3087     if (Bytes > 1)
3088       return AP.OutStreamer->emitFill(Bytes, Value);
3089   }
3090 
3091   // If this can be emitted with .ascii/.asciz, emit it as such.
3092   if (CDS->isString())
3093     return AP.OutStreamer->emitBytes(CDS->getAsString());
3094 
3095   // Otherwise, emit the values in successive locations.
3096   unsigned ElementByteSize = CDS->getElementByteSize();
3097   if (isa<IntegerType>(CDS->getElementType())) {
3098     for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3099       emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
3100       if (AP.isVerbose())
3101         AP.OutStreamer->getCommentOS()
3102             << format("0x%" PRIx64 "\n", CDS->getElementAsInteger(I));
3103       AP.OutStreamer->emitIntValue(CDS->getElementAsInteger(I),
3104                                    ElementByteSize);
3105     }
3106   } else {
3107     Type *ET = CDS->getElementType();
3108     for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3109       emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
3110       emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP);
3111     }
3112   }
3113 
3114   unsigned Size = DL.getTypeAllocSize(CDS->getType());
3115   unsigned EmittedSize =
3116       DL.getTypeAllocSize(CDS->getElementType()) * CDS->getNumElements();
3117   assert(EmittedSize <= Size && "Size cannot be less than EmittedSize!");
3118   if (unsigned Padding = Size - EmittedSize)
3119     AP.OutStreamer->emitZeros(Padding);
3120 }
3121 
3122 static void emitGlobalConstantArray(const DataLayout &DL,
3123                                     const ConstantArray *CA, AsmPrinter &AP,
3124                                     const Constant *BaseCV, uint64_t Offset,
3125                                     AsmPrinter::AliasMapTy *AliasList) {
3126   // See if we can aggregate some values.  Make sure it can be
3127   // represented as a series of bytes of the constant value.
3128   int Value = isRepeatedByteSequence(CA, DL);
3129 
3130   if (Value != -1) {
3131     uint64_t Bytes = DL.getTypeAllocSize(CA->getType());
3132     AP.OutStreamer->emitFill(Bytes, Value);
3133   } else {
3134     for (unsigned I = 0, E = CA->getNumOperands(); I != E; ++I) {
3135       emitGlobalConstantImpl(DL, CA->getOperand(I), AP, BaseCV, Offset,
3136                              AliasList);
3137       Offset += DL.getTypeAllocSize(CA->getOperand(I)->getType());
3138     }
3139   }
3140 }
3141 
3142 static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
3143 
3144 static void emitGlobalConstantVector(const DataLayout &DL,
3145                                      const ConstantVector *CV, AsmPrinter &AP,
3146                                      AsmPrinter::AliasMapTy *AliasList) {
3147   Type *ElementType = CV->getType()->getElementType();
3148   uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
3149   uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
3150   uint64_t EmittedSize;
3151   if (ElementSizeInBits != ElementAllocSizeInBits) {
3152     // If the allocation size of an element is different from the size in bits,
3153     // printing each element separately will insert incorrect padding.
3154     //
3155     // The general algorithm here is complicated; instead of writing it out
3156     // here, just use the existing code in ConstantFolding.
3157     Type *IntT =
3158         IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
3159     ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
3160         ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
3161     if (!CI) {
3162       report_fatal_error(
3163           "Cannot lower vector global with unusual element type");
3164     }
3165     emitGlobalAliasInline(AP, 0, AliasList);
3166     emitGlobalConstantLargeInt(CI, AP);
3167     EmittedSize = DL.getTypeStoreSize(CV->getType());
3168   } else {
3169     for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
3170       emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
3171       emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
3172     }
3173     EmittedSize =
3174         DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
3175   }
3176 
3177   unsigned Size = DL.getTypeAllocSize(CV->getType());
3178   if (unsigned Padding = Size - EmittedSize)
3179     AP.OutStreamer->emitZeros(Padding);
3180 }
3181 
3182 static void emitGlobalConstantStruct(const DataLayout &DL,
3183                                      const ConstantStruct *CS, AsmPrinter &AP,
3184                                      const Constant *BaseCV, uint64_t Offset,
3185                                      AsmPrinter::AliasMapTy *AliasList) {
3186   // Print the fields in successive locations. Pad to align if needed!
3187   unsigned Size = DL.getTypeAllocSize(CS->getType());
3188   const StructLayout *Layout = DL.getStructLayout(CS->getType());
3189   uint64_t SizeSoFar = 0;
3190   for (unsigned I = 0, E = CS->getNumOperands(); I != E; ++I) {
3191     const Constant *Field = CS->getOperand(I);
3192 
3193     // Print the actual field value.
3194     emitGlobalConstantImpl(DL, Field, AP, BaseCV, Offset + SizeSoFar,
3195                            AliasList);
3196 
3197     // Check if padding is needed and insert one or more 0s.
3198     uint64_t FieldSize = DL.getTypeAllocSize(Field->getType());
3199     uint64_t PadSize = ((I == E - 1 ? Size : Layout->getElementOffset(I + 1)) -
3200                         Layout->getElementOffset(I)) -
3201                        FieldSize;
3202     SizeSoFar += FieldSize + PadSize;
3203 
3204     // Insert padding - this may include padding to increase the size of the
3205     // current field up to the ABI size (if the struct is not packed) as well
3206     // as padding to ensure that the next field starts at the right offset.
3207     AP.OutStreamer->emitZeros(PadSize);
3208   }
3209   assert(SizeSoFar == Layout->getSizeInBytes() &&
3210          "Layout of constant struct may be incorrect!");
3211 }
3212 
3213 static void emitGlobalConstantFP(APFloat APF, Type *ET, AsmPrinter &AP) {
3214   assert(ET && "Unknown float type");
3215   APInt API = APF.bitcastToAPInt();
3216 
3217   // First print a comment with what we think the original floating-point value
3218   // should have been.
3219   if (AP.isVerbose()) {
3220     SmallString<8> StrVal;
3221     APF.toString(StrVal);
3222     ET->print(AP.OutStreamer->getCommentOS());
3223     AP.OutStreamer->getCommentOS() << ' ' << StrVal << '\n';
3224   }
3225 
3226   // Now iterate through the APInt chunks, emitting them in endian-correct
3227   // order, possibly with a smaller chunk at beginning/end (e.g. for x87 80-bit
3228   // floats).
3229   unsigned NumBytes = API.getBitWidth() / 8;
3230   unsigned TrailingBytes = NumBytes % sizeof(uint64_t);
3231   const uint64_t *p = API.getRawData();
3232 
3233   // PPC's long double has odd notions of endianness compared to how LLVM
3234   // handles it: p[0] goes first for *big* endian on PPC.
3235   if (AP.getDataLayout().isBigEndian() && !ET->isPPC_FP128Ty()) {
3236     int Chunk = API.getNumWords() - 1;
3237 
3238     if (TrailingBytes)
3239       AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk--], TrailingBytes);
3240 
3241     for (; Chunk >= 0; --Chunk)
3242       AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], sizeof(uint64_t));
3243   } else {
3244     unsigned Chunk;
3245     for (Chunk = 0; Chunk < NumBytes / sizeof(uint64_t); ++Chunk)
3246       AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], sizeof(uint64_t));
3247 
3248     if (TrailingBytes)
3249       AP.OutStreamer->emitIntValueInHexWithPadding(p[Chunk], TrailingBytes);
3250   }
3251 
3252   // Emit the tail padding for the long double.
3253   const DataLayout &DL = AP.getDataLayout();
3254   AP.OutStreamer->emitZeros(DL.getTypeAllocSize(ET) - DL.getTypeStoreSize(ET));
3255 }
3256 
3257 static void emitGlobalConstantFP(const ConstantFP *CFP, AsmPrinter &AP) {
3258   emitGlobalConstantFP(CFP->getValueAPF(), CFP->getType(), AP);
3259 }
3260 
3261 static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
3262   const DataLayout &DL = AP.getDataLayout();
3263   unsigned BitWidth = CI->getBitWidth();
3264 
3265   // Copy the value as we may massage the layout for constants whose bit width
3266   // is not a multiple of 64-bits.
3267   APInt Realigned(CI->getValue());
3268   uint64_t ExtraBits = 0;
3269   unsigned ExtraBitsSize = BitWidth & 63;
3270 
3271   if (ExtraBitsSize) {
3272     // The bit width of the data is not a multiple of 64-bits.
3273     // The extra bits are expected to be at the end of the chunk of the memory.
3274     // Little endian:
3275     // * Nothing to be done, just record the extra bits to emit.
3276     // Big endian:
3277     // * Record the extra bits to emit.
3278     // * Realign the raw data to emit the chunks of 64-bits.
3279     if (DL.isBigEndian()) {
3280       // Basically the structure of the raw data is a chunk of 64-bits cells:
3281       //    0        1         BitWidth / 64
3282       // [chunk1][chunk2] ... [chunkN].
3283       // The most significant chunk is chunkN and it should be emitted first.
3284       // However, due to the alignment issue chunkN contains useless bits.
3285       // Realign the chunks so that they contain only useful information:
3286       // ExtraBits     0       1       (BitWidth / 64) - 1
3287       //       chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
3288       ExtraBitsSize = alignTo(ExtraBitsSize, 8);
3289       ExtraBits = Realigned.getRawData()[0] &
3290         (((uint64_t)-1) >> (64 - ExtraBitsSize));
3291       Realigned.lshrInPlace(ExtraBitsSize);
3292     } else
3293       ExtraBits = Realigned.getRawData()[BitWidth / 64];
3294   }
3295 
3296   // We don't expect assemblers to support integer data directives
3297   // for more than 64 bits, so we emit the data in at most 64-bit
3298   // quantities at a time.
3299   const uint64_t *RawData = Realigned.getRawData();
3300   for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
3301     uint64_t Val = DL.isBigEndian() ? RawData[e - i - 1] : RawData[i];
3302     AP.OutStreamer->emitIntValue(Val, 8);
3303   }
3304 
3305   if (ExtraBitsSize) {
3306     // Emit the extra bits after the 64-bits chunks.
3307 
3308     // Emit a directive that fills the expected size.
3309     uint64_t Size = AP.getDataLayout().getTypeStoreSize(CI->getType());
3310     Size -= (BitWidth / 64) * 8;
3311     assert(Size && Size * 8 >= ExtraBitsSize &&
3312            (ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
3313            == ExtraBits && "Directive too small for extra bits.");
3314     AP.OutStreamer->emitIntValue(ExtraBits, Size);
3315   }
3316 }
3317 
3318 /// Transform a not absolute MCExpr containing a reference to a GOT
3319 /// equivalent global, by a target specific GOT pc relative access to the
3320 /// final symbol.
3321 static void handleIndirectSymViaGOTPCRel(AsmPrinter &AP, const MCExpr **ME,
3322                                          const Constant *BaseCst,
3323                                          uint64_t Offset) {
3324   // The global @foo below illustrates a global that uses a got equivalent.
3325   //
3326   //  @bar = global i32 42
3327   //  @gotequiv = private unnamed_addr constant i32* @bar
3328   //  @foo = i32 trunc (i64 sub (i64 ptrtoint (i32** @gotequiv to i64),
3329   //                             i64 ptrtoint (i32* @foo to i64))
3330   //                        to i32)
3331   //
3332   // The cstexpr in @foo is converted into the MCExpr `ME`, where we actually
3333   // check whether @foo is suitable to use a GOTPCREL. `ME` is usually in the
3334   // form:
3335   //
3336   //  foo = cstexpr, where
3337   //    cstexpr := <gotequiv> - "." + <cst>
3338   //    cstexpr := <gotequiv> - (<foo> - <offset from @foo base>) + <cst>
3339   //
3340   // After canonicalization by evaluateAsRelocatable `ME` turns into:
3341   //
3342   //  cstexpr := <gotequiv> - <foo> + gotpcrelcst, where
3343   //    gotpcrelcst := <offset from @foo base> + <cst>
3344   MCValue MV;
3345   if (!(*ME)->evaluateAsRelocatable(MV, nullptr, nullptr) || MV.isAbsolute())
3346     return;
3347   const MCSymbolRefExpr *SymA = MV.getSymA();
3348   if (!SymA)
3349     return;
3350 
3351   // Check that GOT equivalent symbol is cached.
3352   const MCSymbol *GOTEquivSym = &SymA->getSymbol();
3353   if (!AP.GlobalGOTEquivs.count(GOTEquivSym))
3354     return;
3355 
3356   const GlobalValue *BaseGV = dyn_cast_or_null<GlobalValue>(BaseCst);
3357   if (!BaseGV)
3358     return;
3359 
3360   // Check for a valid base symbol
3361   const MCSymbol *BaseSym = AP.getSymbol(BaseGV);
3362   const MCSymbolRefExpr *SymB = MV.getSymB();
3363 
3364   if (!SymB || BaseSym != &SymB->getSymbol())
3365     return;
3366 
3367   // Make sure to match:
3368   //
3369   //    gotpcrelcst := <offset from @foo base> + <cst>
3370   //
3371   // If gotpcrelcst is positive it means that we can safely fold the pc rel
3372   // displacement into the GOTPCREL. We can also can have an extra offset <cst>
3373   // if the target knows how to encode it.
3374   int64_t GOTPCRelCst = Offset + MV.getConstant();
3375   if (GOTPCRelCst < 0)
3376     return;
3377   if (!AP.getObjFileLowering().supportGOTPCRelWithOffset() && GOTPCRelCst != 0)
3378     return;
3379 
3380   // Emit the GOT PC relative to replace the got equivalent global, i.e.:
3381   //
3382   //  bar:
3383   //    .long 42
3384   //  gotequiv:
3385   //    .quad bar
3386   //  foo:
3387   //    .long gotequiv - "." + <cst>
3388   //
3389   // is replaced by the target specific equivalent to:
3390   //
3391   //  bar:
3392   //    .long 42
3393   //  foo:
3394   //    .long bar@GOTPCREL+<gotpcrelcst>
3395   AsmPrinter::GOTEquivUsePair Result = AP.GlobalGOTEquivs[GOTEquivSym];
3396   const GlobalVariable *GV = Result.first;
3397   int NumUses = (int)Result.second;
3398   const GlobalValue *FinalGV = dyn_cast<GlobalValue>(GV->getOperand(0));
3399   const MCSymbol *FinalSym = AP.getSymbol(FinalGV);
3400   *ME = AP.getObjFileLowering().getIndirectSymViaGOTPCRel(
3401       FinalGV, FinalSym, MV, Offset, AP.MMI, *AP.OutStreamer);
3402 
3403   // Update GOT equivalent usage information
3404   --NumUses;
3405   if (NumUses >= 0)
3406     AP.GlobalGOTEquivs[GOTEquivSym] = std::make_pair(GV, NumUses);
3407 }
3408 
3409 static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
3410                                    AsmPrinter &AP, const Constant *BaseCV,
3411                                    uint64_t Offset,
3412                                    AsmPrinter::AliasMapTy *AliasList) {
3413   emitGlobalAliasInline(AP, Offset, AliasList);
3414   uint64_t Size = DL.getTypeAllocSize(CV->getType());
3415 
3416   // Globals with sub-elements such as combinations of arrays and structs
3417   // are handled recursively by emitGlobalConstantImpl. Keep track of the
3418   // constant symbol base and the current position with BaseCV and Offset.
3419   if (!BaseCV && CV->hasOneUse())
3420     BaseCV = dyn_cast<Constant>(CV->user_back());
3421 
3422   if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
3423     return AP.OutStreamer->emitZeros(Size);
3424 
3425   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
3426     const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
3427 
3428     if (StoreSize <= 8) {
3429       if (AP.isVerbose())
3430         AP.OutStreamer->getCommentOS()
3431             << format("0x%" PRIx64 "\n", CI->getZExtValue());
3432       AP.OutStreamer->emitIntValue(CI->getZExtValue(), StoreSize);
3433     } else {
3434       emitGlobalConstantLargeInt(CI, AP);
3435     }
3436 
3437     // Emit tail padding if needed
3438     if (Size != StoreSize)
3439       AP.OutStreamer->emitZeros(Size - StoreSize);
3440 
3441     return;
3442   }
3443 
3444   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
3445     return emitGlobalConstantFP(CFP, AP);
3446 
3447   if (isa<ConstantPointerNull>(CV)) {
3448     AP.OutStreamer->emitIntValue(0, Size);
3449     return;
3450   }
3451 
3452   if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(CV))
3453     return emitGlobalConstantDataSequential(DL, CDS, AP, AliasList);
3454 
3455   if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
3456     return emitGlobalConstantArray(DL, CVA, AP, BaseCV, Offset, AliasList);
3457 
3458   if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
3459     return emitGlobalConstantStruct(DL, CVS, AP, BaseCV, Offset, AliasList);
3460 
3461   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
3462     // Look through bitcasts, which might not be able to be MCExpr'ized (e.g. of
3463     // vectors).
3464     if (CE->getOpcode() == Instruction::BitCast)
3465       return emitGlobalConstantImpl(DL, CE->getOperand(0), AP);
3466 
3467     if (Size > 8) {
3468       // If the constant expression's size is greater than 64-bits, then we have
3469       // to emit the value in chunks. Try to constant fold the value and emit it
3470       // that way.
3471       Constant *New = ConstantFoldConstant(CE, DL);
3472       if (New != CE)
3473         return emitGlobalConstantImpl(DL, New, AP);
3474     }
3475   }
3476 
3477   if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
3478     return emitGlobalConstantVector(DL, V, AP, AliasList);
3479 
3480   // Otherwise, it must be a ConstantExpr.  Lower it to an MCExpr, then emit it
3481   // thread the streamer with EmitValue.
3482   const MCExpr *ME = AP.lowerConstant(CV);
3483 
3484   // Since lowerConstant already folded and got rid of all IR pointer and
3485   // integer casts, detect GOT equivalent accesses by looking into the MCExpr
3486   // directly.
3487   if (AP.getObjFileLowering().supportIndirectSymViaGOTPCRel())
3488     handleIndirectSymViaGOTPCRel(AP, &ME, BaseCV, Offset);
3489 
3490   AP.OutStreamer->emitValue(ME, Size);
3491 }
3492 
3493 /// EmitGlobalConstant - Print a general LLVM constant to the .s file.
3494 void AsmPrinter::emitGlobalConstant(const DataLayout &DL, const Constant *CV,
3495                                     AliasMapTy *AliasList) {
3496   uint64_t Size = DL.getTypeAllocSize(CV->getType());
3497   if (Size)
3498     emitGlobalConstantImpl(DL, CV, *this, nullptr, 0, AliasList);
3499   else if (MAI->hasSubsectionsViaSymbols()) {
3500     // If the global has zero size, emit a single byte so that two labels don't
3501     // look like they are at the same location.
3502     OutStreamer->emitIntValue(0, 1);
3503   }
3504   if (!AliasList)
3505     return;
3506   // TODO: These remaining aliases are not emitted in the correct location. Need
3507   // to handle the case where the alias offset doesn't refer to any sub-element.
3508   for (auto &AliasPair : *AliasList) {
3509     for (const GlobalAlias *GA : AliasPair.second)
3510       OutStreamer->emitLabel(getSymbol(GA));
3511   }
3512 }
3513 
3514 void AsmPrinter::emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
3515   // Target doesn't support this yet!
3516   llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
3517 }
3518 
3519 void AsmPrinter::printOffset(int64_t Offset, raw_ostream &OS) const {
3520   if (Offset > 0)
3521     OS << '+' << Offset;
3522   else if (Offset < 0)
3523     OS << Offset;
3524 }
3525 
3526 void AsmPrinter::emitNops(unsigned N) {
3527   MCInst Nop = MF->getSubtarget().getInstrInfo()->getNop();
3528   for (; N; --N)
3529     EmitToStreamer(*OutStreamer, Nop);
3530 }
3531 
3532 //===----------------------------------------------------------------------===//
3533 // Symbol Lowering Routines.
3534 //===----------------------------------------------------------------------===//
3535 
3536 MCSymbol *AsmPrinter::createTempSymbol(const Twine &Name) const {
3537   return OutContext.createTempSymbol(Name, true);
3538 }
3539 
3540 MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
3541   return const_cast<AsmPrinter *>(this)->getAddrLabelSymbol(
3542       BA->getBasicBlock());
3543 }
3544 
3545 MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BasicBlock *BB) const {
3546   return const_cast<AsmPrinter *>(this)->getAddrLabelSymbol(BB);
3547 }
3548 
3549 /// GetCPISymbol - Return the symbol for the specified constant pool entry.
3550 MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
3551   if (getSubtargetInfo().getTargetTriple().isWindowsMSVCEnvironment()) {
3552     const MachineConstantPoolEntry &CPE =
3553         MF->getConstantPool()->getConstants()[CPID];
3554     if (!CPE.isMachineConstantPoolEntry()) {
3555       const DataLayout &DL = MF->getDataLayout();
3556       SectionKind Kind = CPE.getSectionKind(&DL);
3557       const Constant *C = CPE.Val.ConstVal;
3558       Align Alignment = CPE.Alignment;
3559       if (const MCSectionCOFF *S = dyn_cast<MCSectionCOFF>(
3560               getObjFileLowering().getSectionForConstant(DL, Kind, C,
3561                                                          Alignment))) {
3562         if (MCSymbol *Sym = S->getCOMDATSymbol()) {
3563           if (Sym->isUndefined())
3564             OutStreamer->emitSymbolAttribute(Sym, MCSA_Global);
3565           return Sym;
3566         }
3567       }
3568     }
3569   }
3570 
3571   const DataLayout &DL = getDataLayout();
3572   return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
3573                                       "CPI" + Twine(getFunctionNumber()) + "_" +
3574                                       Twine(CPID));
3575 }
3576 
3577 /// GetJTISymbol - Return the symbol for the specified jump table entry.
3578 MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
3579   return MF->getJTISymbol(JTID, OutContext, isLinkerPrivate);
3580 }
3581 
3582 /// GetJTSetSymbol - Return the symbol for the specified jump table .set
3583 /// FIXME: privatize to AsmPrinter.
3584 MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
3585   const DataLayout &DL = getDataLayout();
3586   return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
3587                                       Twine(getFunctionNumber()) + "_" +
3588                                       Twine(UID) + "_set_" + Twine(MBBID));
3589 }
3590 
3591 MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
3592                                                    StringRef Suffix) const {
3593   return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, TM);
3594 }
3595 
3596 /// Return the MCSymbol for the specified ExternalSymbol.
3597 MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
3598   SmallString<60> NameStr;
3599   Mangler::getNameWithPrefix(NameStr, Sym, getDataLayout());
3600   return OutContext.getOrCreateSymbol(NameStr);
3601 }
3602 
3603 /// PrintParentLoopComment - Print comments about parent loops of this one.
3604 static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
3605                                    unsigned FunctionNumber) {
3606   if (!Loop) return;
3607   PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
3608   OS.indent(Loop->getLoopDepth()*2)
3609     << "Parent Loop BB" << FunctionNumber << "_"
3610     << Loop->getHeader()->getNumber()
3611     << " Depth=" << Loop->getLoopDepth() << '\n';
3612 }
3613 
3614 /// PrintChildLoopComment - Print comments about child loops within
3615 /// the loop for this basic block, with nesting.
3616 static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
3617                                   unsigned FunctionNumber) {
3618   // Add child loop information
3619   for (const MachineLoop *CL : *Loop) {
3620     OS.indent(CL->getLoopDepth()*2)
3621       << "Child Loop BB" << FunctionNumber << "_"
3622       << CL->getHeader()->getNumber() << " Depth " << CL->getLoopDepth()
3623       << '\n';
3624     PrintChildLoopComment(OS, CL, FunctionNumber);
3625   }
3626 }
3627 
3628 /// emitBasicBlockLoopComments - Pretty-print comments for basic blocks.
3629 static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
3630                                        const MachineLoopInfo *LI,
3631                                        const AsmPrinter &AP) {
3632   // Add loop depth information
3633   const MachineLoop *Loop = LI->getLoopFor(&MBB);
3634   if (!Loop) return;
3635 
3636   MachineBasicBlock *Header = Loop->getHeader();
3637   assert(Header && "No header for loop");
3638 
3639   // If this block is not a loop header, just print out what is the loop header
3640   // and return.
3641   if (Header != &MBB) {
3642     AP.OutStreamer->AddComment("  in Loop: Header=BB" +
3643                                Twine(AP.getFunctionNumber())+"_" +
3644                                Twine(Loop->getHeader()->getNumber())+
3645                                " Depth="+Twine(Loop->getLoopDepth()));
3646     return;
3647   }
3648 
3649   // Otherwise, it is a loop header.  Print out information about child and
3650   // parent loops.
3651   raw_ostream &OS = AP.OutStreamer->getCommentOS();
3652 
3653   PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
3654 
3655   OS << "=>";
3656   OS.indent(Loop->getLoopDepth()*2-2);
3657 
3658   OS << "This ";
3659   if (Loop->isInnermost())
3660     OS << "Inner ";
3661   OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
3662 
3663   PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
3664 }
3665 
3666 /// emitBasicBlockStart - This method prints the label for the specified
3667 /// MachineBasicBlock, an alignment (if present) and a comment describing
3668 /// it if appropriate.
3669 void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
3670   // End the previous funclet and start a new one.
3671   if (MBB.isEHFuncletEntry()) {
3672     for (const HandlerInfo &HI : Handlers) {
3673       HI.Handler->endFunclet();
3674       HI.Handler->beginFunclet(MBB);
3675     }
3676   }
3677 
3678   // Switch to a new section if this basic block must begin a section. The
3679   // entry block is always placed in the function section and is handled
3680   // separately.
3681   if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
3682     OutStreamer->switchSection(
3683         getObjFileLowering().getSectionForMachineBasicBlock(MF->getFunction(),
3684                                                             MBB, TM));
3685     CurrentSectionBeginSym = MBB.getSymbol();
3686   }
3687 
3688   // Emit an alignment directive for this block, if needed.
3689   const Align Alignment = MBB.getAlignment();
3690   if (Alignment != Align(1))
3691     emitAlignment(Alignment, nullptr, MBB.getMaxBytesForAlignment());
3692 
3693   // If the block has its address taken, emit any labels that were used to
3694   // reference the block.  It is possible that there is more than one label
3695   // here, because multiple LLVM BB's may have been RAUW'd to this block after
3696   // the references were generated.
3697   if (MBB.isIRBlockAddressTaken()) {
3698     if (isVerbose())
3699       OutStreamer->AddComment("Block address taken");
3700 
3701     BasicBlock *BB = MBB.getAddressTakenIRBlock();
3702     assert(BB && BB->hasAddressTaken() && "Missing BB");
3703     for (MCSymbol *Sym : getAddrLabelSymbolToEmit(BB))
3704       OutStreamer->emitLabel(Sym);
3705   } else if (isVerbose() && MBB.isMachineBlockAddressTaken()) {
3706     OutStreamer->AddComment("Block address taken");
3707   }
3708 
3709   // Print some verbose block comments.
3710   if (isVerbose()) {
3711     if (const BasicBlock *BB = MBB.getBasicBlock()) {
3712       if (BB->hasName()) {
3713         BB->printAsOperand(OutStreamer->getCommentOS(),
3714                            /*PrintType=*/false, BB->getModule());
3715         OutStreamer->getCommentOS() << '\n';
3716       }
3717     }
3718 
3719     assert(MLI != nullptr && "MachineLoopInfo should has been computed");
3720     emitBasicBlockLoopComments(MBB, MLI, *this);
3721   }
3722 
3723   // Print the main label for the block.
3724   if (shouldEmitLabelForBasicBlock(MBB)) {
3725     if (isVerbose() && MBB.hasLabelMustBeEmitted())
3726       OutStreamer->AddComment("Label of block must be emitted");
3727     OutStreamer->emitLabel(MBB.getSymbol());
3728   } else {
3729     if (isVerbose()) {
3730       // NOTE: Want this comment at start of line, don't emit with AddComment.
3731       OutStreamer->emitRawComment(" %bb." + Twine(MBB.getNumber()) + ":",
3732                                   false);
3733     }
3734   }
3735 
3736   if (MBB.isEHCatchretTarget() &&
3737       MAI->getExceptionHandlingType() == ExceptionHandling::WinEH) {
3738     OutStreamer->emitLabel(MBB.getEHCatchretSymbol());
3739   }
3740 
3741   // With BB sections, each basic block must handle CFI information on its own
3742   // if it begins a section (Entry block call is handled separately, next to
3743   // beginFunction).
3744   if (MBB.isBeginSection() && !MBB.isEntryBlock())
3745     for (const HandlerInfo &HI : Handlers)
3746       HI.Handler->beginBasicBlockSection(MBB);
3747 }
3748 
3749 void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
3750   // Check if CFI information needs to be updated for this MBB with basic block
3751   // sections.
3752   if (MBB.isEndSection())
3753     for (const HandlerInfo &HI : Handlers)
3754       HI.Handler->endBasicBlockSection(MBB);
3755 }
3756 
3757 void AsmPrinter::emitVisibility(MCSymbol *Sym, unsigned Visibility,
3758                                 bool IsDefinition) const {
3759   MCSymbolAttr Attr = MCSA_Invalid;
3760 
3761   switch (Visibility) {
3762   default: break;
3763   case GlobalValue::HiddenVisibility:
3764     if (IsDefinition)
3765       Attr = MAI->getHiddenVisibilityAttr();
3766     else
3767       Attr = MAI->getHiddenDeclarationVisibilityAttr();
3768     break;
3769   case GlobalValue::ProtectedVisibility:
3770     Attr = MAI->getProtectedVisibilityAttr();
3771     break;
3772   }
3773 
3774   if (Attr != MCSA_Invalid)
3775     OutStreamer->emitSymbolAttribute(Sym, Attr);
3776 }
3777 
3778 bool AsmPrinter::shouldEmitLabelForBasicBlock(
3779     const MachineBasicBlock &MBB) const {
3780   // With `-fbasic-block-sections=`, a label is needed for every non-entry block
3781   // in the labels mode (option `=labels`) and every section beginning in the
3782   // sections mode (`=all` and `=list=`).
3783   if ((MF->hasBBLabels() || MBB.isBeginSection()) && !MBB.isEntryBlock())
3784     return true;
3785   // A label is needed for any block with at least one predecessor (when that
3786   // predecessor is not the fallthrough predecessor, or if it is an EH funclet
3787   // entry, or if a label is forced).
3788   return !MBB.pred_empty() &&
3789          (!isBlockOnlyReachableByFallthrough(&MBB) || MBB.isEHFuncletEntry() ||
3790           MBB.hasLabelMustBeEmitted());
3791 }
3792 
3793 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
3794 /// exactly one predecessor and the control transfer mechanism between
3795 /// the predecessor and this block is a fall-through.
3796 bool AsmPrinter::
3797 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
3798   // If this is a landing pad, it isn't a fall through.  If it has no preds,
3799   // then nothing falls through to it.
3800   if (MBB->isEHPad() || MBB->pred_empty())
3801     return false;
3802 
3803   // If there isn't exactly one predecessor, it can't be a fall through.
3804   if (MBB->pred_size() > 1)
3805     return false;
3806 
3807   // The predecessor has to be immediately before this block.
3808   MachineBasicBlock *Pred = *MBB->pred_begin();
3809   if (!Pred->isLayoutSuccessor(MBB))
3810     return false;
3811 
3812   // If the block is completely empty, then it definitely does fall through.
3813   if (Pred->empty())
3814     return true;
3815 
3816   // Check the terminators in the previous blocks
3817   for (const auto &MI : Pred->terminators()) {
3818     // If it is not a simple branch, we are in a table somewhere.
3819     if (!MI.isBranch() || MI.isIndirectBranch())
3820       return false;
3821 
3822     // If we are the operands of one of the branches, this is not a fall
3823     // through. Note that targets with delay slots will usually bundle
3824     // terminators with the delay slot instruction.
3825     for (ConstMIBundleOperands OP(MI); OP.isValid(); ++OP) {
3826       if (OP->isJTI())
3827         return false;
3828       if (OP->isMBB() && OP->getMBB() == MBB)
3829         return false;
3830     }
3831   }
3832 
3833   return true;
3834 }
3835 
3836 GCMetadataPrinter *AsmPrinter::getOrCreateGCPrinter(GCStrategy &S) {
3837   if (!S.usesMetadata())
3838     return nullptr;
3839 
3840   auto [GCPI, Inserted] = GCMetadataPrinters.insert({&S, nullptr});
3841   if (!Inserted)
3842     return GCPI->second.get();
3843 
3844   auto Name = S.getName();
3845 
3846   for (const GCMetadataPrinterRegistry::entry &GCMetaPrinter :
3847        GCMetadataPrinterRegistry::entries())
3848     if (Name == GCMetaPrinter.getName()) {
3849       std::unique_ptr<GCMetadataPrinter> GMP = GCMetaPrinter.instantiate();
3850       GMP->S = &S;
3851       GCPI->second = std::move(GMP);
3852       return GCPI->second.get();
3853     }
3854 
3855   report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
3856 }
3857 
3858 void AsmPrinter::emitStackMaps() {
3859   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
3860   assert(MI && "AsmPrinter didn't require GCModuleInfo?");
3861   bool NeedsDefault = false;
3862   if (MI->begin() == MI->end())
3863     // No GC strategy, use the default format.
3864     NeedsDefault = true;
3865   else
3866     for (const auto &I : *MI) {
3867       if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
3868         if (MP->emitStackMaps(SM, *this))
3869           continue;
3870       // The strategy doesn't have printer or doesn't emit custom stack maps.
3871       // Use the default format.
3872       NeedsDefault = true;
3873     }
3874 
3875   if (NeedsDefault)
3876     SM.serializeToStackMapSection();
3877 }
3878 
3879 /// Pin vtable to this file.
3880 AsmPrinterHandler::~AsmPrinterHandler() = default;
3881 
3882 void AsmPrinterHandler::markFunctionEnd() {}
3883 
3884 // In the binary's "xray_instr_map" section, an array of these function entries
3885 // describes each instrumentation point.  When XRay patches your code, the index
3886 // into this table will be given to your handler as a patch point identifier.
3887 void AsmPrinter::XRayFunctionEntry::emit(int Bytes, MCStreamer *Out) const {
3888   auto Kind8 = static_cast<uint8_t>(Kind);
3889   Out->emitBinaryData(StringRef(reinterpret_cast<const char *>(&Kind8), 1));
3890   Out->emitBinaryData(
3891       StringRef(reinterpret_cast<const char *>(&AlwaysInstrument), 1));
3892   Out->emitBinaryData(StringRef(reinterpret_cast<const char *>(&Version), 1));
3893   auto Padding = (4 * Bytes) - ((2 * Bytes) + 3);
3894   assert(Padding >= 0 && "Instrumentation map entry > 4 * Word Size");
3895   Out->emitZeros(Padding);
3896 }
3897 
3898 void AsmPrinter::emitXRayTable() {
3899   if (Sleds.empty())
3900     return;
3901 
3902   auto PrevSection = OutStreamer->getCurrentSectionOnly();
3903   const Function &F = MF->getFunction();
3904   MCSection *InstMap = nullptr;
3905   MCSection *FnSledIndex = nullptr;
3906   const Triple &TT = TM.getTargetTriple();
3907   // Use PC-relative addresses on all targets.
3908   if (TT.isOSBinFormatELF()) {
3909     auto LinkedToSym = cast<MCSymbolELF>(CurrentFnSym);
3910     auto Flags = ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER;
3911     StringRef GroupName;
3912     if (F.hasComdat()) {
3913       Flags |= ELF::SHF_GROUP;
3914       GroupName = F.getComdat()->getName();
3915     }
3916     InstMap = OutContext.getELFSection("xray_instr_map", ELF::SHT_PROGBITS,
3917                                        Flags, 0, GroupName, F.hasComdat(),
3918                                        MCSection::NonUniqueID, LinkedToSym);
3919 
3920     if (!TM.Options.XRayOmitFunctionIndex)
3921       FnSledIndex = OutContext.getELFSection(
3922           "xray_fn_idx", ELF::SHT_PROGBITS, Flags | ELF::SHF_WRITE, 0,
3923           GroupName, F.hasComdat(), MCSection::NonUniqueID, LinkedToSym);
3924   } else if (MF->getSubtarget().getTargetTriple().isOSBinFormatMachO()) {
3925     InstMap = OutContext.getMachOSection("__DATA", "xray_instr_map", 0,
3926                                          SectionKind::getReadOnlyWithRel());
3927     if (!TM.Options.XRayOmitFunctionIndex)
3928       FnSledIndex = OutContext.getMachOSection(
3929           "__DATA", "xray_fn_idx", 0, SectionKind::getReadOnlyWithRel());
3930   } else {
3931     llvm_unreachable("Unsupported target");
3932   }
3933 
3934   auto WordSizeBytes = MAI->getCodePointerSize();
3935 
3936   // Now we switch to the instrumentation map section. Because this is done
3937   // per-function, we are able to create an index entry that will represent the
3938   // range of sleds associated with a function.
3939   auto &Ctx = OutContext;
3940   MCSymbol *SledsStart = OutContext.createTempSymbol("xray_sleds_start", true);
3941   OutStreamer->switchSection(InstMap);
3942   OutStreamer->emitLabel(SledsStart);
3943   for (const auto &Sled : Sleds) {
3944     MCSymbol *Dot = Ctx.createTempSymbol();
3945     OutStreamer->emitLabel(Dot);
3946     OutStreamer->emitValueImpl(
3947         MCBinaryExpr::createSub(MCSymbolRefExpr::create(Sled.Sled, Ctx),
3948                                 MCSymbolRefExpr::create(Dot, Ctx), Ctx),
3949         WordSizeBytes);
3950     OutStreamer->emitValueImpl(
3951         MCBinaryExpr::createSub(
3952             MCSymbolRefExpr::create(CurrentFnBegin, Ctx),
3953             MCBinaryExpr::createAdd(MCSymbolRefExpr::create(Dot, Ctx),
3954                                     MCConstantExpr::create(WordSizeBytes, Ctx),
3955                                     Ctx),
3956             Ctx),
3957         WordSizeBytes);
3958     Sled.emit(WordSizeBytes, OutStreamer.get());
3959   }
3960   MCSymbol *SledsEnd = OutContext.createTempSymbol("xray_sleds_end", true);
3961   OutStreamer->emitLabel(SledsEnd);
3962 
3963   // We then emit a single entry in the index per function. We use the symbols
3964   // that bound the instrumentation map as the range for a specific function.
3965   // Each entry here will be 2 * word size aligned, as we're writing down two
3966   // pointers. This should work for both 32-bit and 64-bit platforms.
3967   if (FnSledIndex) {
3968     OutStreamer->switchSection(FnSledIndex);
3969     OutStreamer->emitCodeAlignment(Align(2 * WordSizeBytes),
3970                                    &getSubtargetInfo());
3971     OutStreamer->emitSymbolValue(SledsStart, WordSizeBytes, false);
3972     OutStreamer->emitSymbolValue(SledsEnd, WordSizeBytes, false);
3973     OutStreamer->switchSection(PrevSection);
3974   }
3975   Sleds.clear();
3976 }
3977 
3978 void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI,
3979                             SledKind Kind, uint8_t Version) {
3980   const Function &F = MI.getMF()->getFunction();
3981   auto Attr = F.getFnAttribute("function-instrument");
3982   bool LogArgs = F.hasFnAttribute("xray-log-args");
3983   bool AlwaysInstrument =
3984     Attr.isStringAttribute() && Attr.getValueAsString() == "xray-always";
3985   if (Kind == SledKind::FUNCTION_ENTER && LogArgs)
3986     Kind = SledKind::LOG_ARGS_ENTER;
3987   Sleds.emplace_back(XRayFunctionEntry{Sled, CurrentFnSym, Kind,
3988                                        AlwaysInstrument, &F, Version});
3989 }
3990 
3991 void AsmPrinter::emitPatchableFunctionEntries() {
3992   const Function &F = MF->getFunction();
3993   unsigned PatchableFunctionPrefix = 0, PatchableFunctionEntry = 0;
3994   (void)F.getFnAttribute("patchable-function-prefix")
3995       .getValueAsString()
3996       .getAsInteger(10, PatchableFunctionPrefix);
3997   (void)F.getFnAttribute("patchable-function-entry")
3998       .getValueAsString()
3999       .getAsInteger(10, PatchableFunctionEntry);
4000   if (!PatchableFunctionPrefix && !PatchableFunctionEntry)
4001     return;
4002   const unsigned PointerSize = getPointerSize();
4003   if (TM.getTargetTriple().isOSBinFormatELF()) {
4004     auto Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC;
4005     const MCSymbolELF *LinkedToSym = nullptr;
4006     StringRef GroupName;
4007 
4008     // GNU as < 2.35 did not support section flag 'o'. GNU ld < 2.36 did not
4009     // support mixed SHF_LINK_ORDER and non-SHF_LINK_ORDER sections.
4010     if (MAI->useIntegratedAssembler() || MAI->binutilsIsAtLeast(2, 36)) {
4011       Flags |= ELF::SHF_LINK_ORDER;
4012       if (F.hasComdat()) {
4013         Flags |= ELF::SHF_GROUP;
4014         GroupName = F.getComdat()->getName();
4015       }
4016       LinkedToSym = cast<MCSymbolELF>(CurrentFnSym);
4017     }
4018     OutStreamer->switchSection(OutContext.getELFSection(
4019         "__patchable_function_entries", ELF::SHT_PROGBITS, Flags, 0, GroupName,
4020         F.hasComdat(), MCSection::NonUniqueID, LinkedToSym));
4021     emitAlignment(Align(PointerSize));
4022     OutStreamer->emitSymbolValue(CurrentPatchableFunctionEntrySym, PointerSize);
4023   }
4024 }
4025 
4026 uint16_t AsmPrinter::getDwarfVersion() const {
4027   return OutStreamer->getContext().getDwarfVersion();
4028 }
4029 
4030 void AsmPrinter::setDwarfVersion(uint16_t Version) {
4031   OutStreamer->getContext().setDwarfVersion(Version);
4032 }
4033 
4034 bool AsmPrinter::isDwarf64() const {
4035   return OutStreamer->getContext().getDwarfFormat() == dwarf::DWARF64;
4036 }
4037 
4038 unsigned int AsmPrinter::getDwarfOffsetByteSize() const {
4039   return dwarf::getDwarfOffsetByteSize(
4040       OutStreamer->getContext().getDwarfFormat());
4041 }
4042 
4043 dwarf::FormParams AsmPrinter::getDwarfFormParams() const {
4044   return {getDwarfVersion(), uint8_t(getPointerSize()),
4045           OutStreamer->getContext().getDwarfFormat(),
4046           doesDwarfUseRelocationsAcrossSections()};
4047 }
4048 
4049 unsigned int AsmPrinter::getUnitLengthFieldByteSize() const {
4050   return dwarf::getUnitLengthFieldByteSize(
4051       OutStreamer->getContext().getDwarfFormat());
4052 }
4053