Bug Summary

File:llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Warning:line 315, column 25
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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