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