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