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