LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - AsmPrinter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1269 1327 95.6 %
Date: 2018-09-23 13:06:45 Functions: 91 94 96.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13