Coverage Report

Created: 2019-07-24 05:18

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