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