| 1 | //===- llvm/CodeGen/AsmPrinter.h - AsmPrinter Framework ---------*- C++ -*-===// |
| 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 contains a class to be used as the base class for target specific |
| 10 | // asm writers. This class primarily handles common functionality used by |
| 11 | // all asm writers. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CODEGEN_ASMPRINTER_H |
| 16 | #define LLVM_CODEGEN_ASMPRINTER_H |
| 17 | |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/MapVector.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| 22 | #include "llvm/Analysis/StaticDataProfileInfo.h" |
| 23 | #include "llvm/BinaryFormat/Dwarf.h" |
| 24 | #include "llvm/CodeGen/DwarfStringPoolEntry.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/StackMaps.h" |
| 27 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 28 | #include "llvm/IR/InlineAsm.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include <cstdint> |
| 32 | #include <memory> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | class AddrLabelMap; |
| 39 | class AsmPrinterHandler; |
| 40 | class BasicBlock; |
| 41 | class BlockAddress; |
| 42 | class Constant; |
| 43 | class ConstantArray; |
| 44 | class ConstantPtrAuth; |
| 45 | class DataLayout; |
| 46 | class DebugHandlerBase; |
| 47 | class DIE; |
| 48 | class DIEAbbrev; |
| 49 | class DwarfDebug; |
| 50 | class EHStreamer; |
| 51 | class GCMetadataPrinter; |
| 52 | class GCStrategy; |
| 53 | class GlobalAlias; |
| 54 | class GlobalObject; |
| 55 | class GlobalValue; |
| 56 | class GlobalVariable; |
| 57 | class MachineBasicBlock; |
| 58 | class MachineConstantPoolValue; |
| 59 | class MachineDominatorTree; |
| 60 | class MachineFunction; |
| 61 | class MachineInstr; |
| 62 | class MachineJumpTableInfo; |
| 63 | class MachineLoopInfo; |
| 64 | class MachineModuleInfo; |
| 65 | class ; |
| 66 | class MCAsmInfo; |
| 67 | class MCCFIInstruction; |
| 68 | class MCContext; |
| 69 | class MCExpr; |
| 70 | class MCInst; |
| 71 | class MCSection; |
| 72 | class MCStreamer; |
| 73 | class MCSubtargetInfo; |
| 74 | class MCSymbol; |
| 75 | class MCTargetOptions; |
| 76 | class MDNode; |
| 77 | class Module; |
| 78 | class PseudoProbeHandler; |
| 79 | class raw_ostream; |
| 80 | class StringRef; |
| 81 | class TargetLoweringObjectFile; |
| 82 | class TargetMachine; |
| 83 | class Twine; |
| 84 | |
| 85 | namespace remarks { |
| 86 | class ; |
| 87 | } |
| 88 | |
| 89 | /// This class is intended to be used as a driving class for all asm writers. |
| 90 | class LLVM_ABI AsmPrinter : public MachineFunctionPass { |
| 91 | public: |
| 92 | /// Target machine description. |
| 93 | TargetMachine &TM; |
| 94 | |
| 95 | /// Target Asm Printer information. |
| 96 | const MCAsmInfo *MAI = nullptr; |
| 97 | |
| 98 | /// This is the context for the output file that we are streaming. This owns |
| 99 | /// all of the global MC-related objects for the generated translation unit. |
| 100 | MCContext &OutContext; |
| 101 | |
| 102 | /// This is the MCStreamer object for the file we are generating. This |
| 103 | /// contains the transient state for the current translation unit that we are |
| 104 | /// generating (such as the current section etc). |
| 105 | std::unique_ptr<MCStreamer> OutStreamer; |
| 106 | |
| 107 | /// The current machine function. |
| 108 | MachineFunction *MF = nullptr; |
| 109 | |
| 110 | /// This is a pointer to the current MachineModuleInfo. |
| 111 | MachineModuleInfo *MMI = nullptr; |
| 112 | |
| 113 | /// This is a pointer to the current MachineDominatorTree. |
| 114 | MachineDominatorTree *MDT = nullptr; |
| 115 | |
| 116 | /// This is a pointer to the current MachineLoopInfo. |
| 117 | MachineLoopInfo *MLI = nullptr; |
| 118 | |
| 119 | /// Optimization remark emitter. |
| 120 | MachineOptimizationRemarkEmitter *ORE = nullptr; |
| 121 | |
| 122 | /// The symbol for the entry in __patchable_function_entires. |
| 123 | MCSymbol *CurrentPatchableFunctionEntrySym = nullptr; |
| 124 | |
| 125 | /// The symbol for the current function. This is recalculated at the beginning |
| 126 | /// of each call to runOnMachineFunction(). |
| 127 | MCSymbol *CurrentFnSym = nullptr; |
| 128 | |
| 129 | /// The symbol for the current function descriptor on AIX. This is created |
| 130 | /// at the beginning of each call to SetupMachineFunction(). |
| 131 | MCSymbol *CurrentFnDescSym = nullptr; |
| 132 | |
| 133 | /// The symbol used to represent the start of the current function for the |
| 134 | /// purpose of calculating its size (e.g. using the .size directive). By |
| 135 | /// default, this is equal to CurrentFnSym. |
| 136 | MCSymbol *CurrentFnSymForSize = nullptr; |
| 137 | |
| 138 | /// Provides the profile information for constants. |
| 139 | const StaticDataProfileInfo *SDPI = nullptr; |
| 140 | |
| 141 | /// The profile summary information. |
| 142 | const ProfileSummaryInfo *PSI = nullptr; |
| 143 | |
| 144 | /// Map a basic block section ID to the begin and end symbols of that section |
| 145 | /// which determine the section's range. |
| 146 | struct MBBSectionRange { |
| 147 | MCSymbol *BeginLabel, *EndLabel; |
| 148 | }; |
| 149 | |
| 150 | MapVector<MBBSectionID, MBBSectionRange> MBBSectionRanges; |
| 151 | |
| 152 | /// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of |
| 153 | /// its number of uses by other globals. |
| 154 | using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>; |
| 155 | MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs; |
| 156 | |
| 157 | // Flags representing which CFI section is required for a function/module. |
| 158 | enum class CFISection : unsigned { |
| 159 | None = 0, ///< Do not emit either .eh_frame or .debug_frame |
| 160 | EH = 1, ///< Emit .eh_frame |
| 161 | Debug = 2 ///< Emit .debug_frame |
| 162 | }; |
| 163 | |
| 164 | private: |
| 165 | MCSymbol *CurrentFnEnd = nullptr; |
| 166 | |
| 167 | /// Map a basic block section ID to the exception symbol associated with that |
| 168 | /// section. Map entries are assigned and looked up via |
| 169 | /// AsmPrinter::getMBBExceptionSym. |
| 170 | DenseMap<MBBSectionID, MCSymbol *> MBBSectionExceptionSyms; |
| 171 | |
| 172 | // The symbol used to represent the start of the current BB section of the |
| 173 | // function. This is used to calculate the size of the BB section. |
| 174 | MCSymbol *CurrentSectionBeginSym = nullptr; |
| 175 | |
| 176 | /// This map keeps track of which symbol is being used for the specified basic |
| 177 | /// block's address of label. |
| 178 | std::unique_ptr<AddrLabelMap> AddrLabelSymbols; |
| 179 | |
| 180 | /// The garbage collection metadata printer table. |
| 181 | DenseMap<GCStrategy *, std::unique_ptr<GCMetadataPrinter>> GCMetadataPrinters; |
| 182 | |
| 183 | /// Emit comments in assembly output if this is true. |
| 184 | bool VerboseAsm; |
| 185 | |
| 186 | /// Output stream for the stack usage file (i.e., .su file). |
| 187 | std::unique_ptr<raw_fd_ostream> StackUsageStream; |
| 188 | |
| 189 | /// List of symbols to be inserted into PC sections. |
| 190 | DenseMap<const MDNode *, SmallVector<const MCSymbol *>> PCSectionsSymbols; |
| 191 | |
| 192 | static char ID; |
| 193 | |
| 194 | protected: |
| 195 | MCSymbol *CurrentFnBegin = nullptr; |
| 196 | |
| 197 | /// For dso_local functions, the current $local alias for the function. |
| 198 | MCSymbol *CurrentFnBeginLocal = nullptr; |
| 199 | |
| 200 | /// A handle to the EH info emitter (if present). |
| 201 | // Only for EHStreamer subtypes, but some C++ compilers will incorrectly warn |
| 202 | // us if we declare that directly. |
| 203 | SmallVector<std::unique_ptr<AsmPrinterHandler>, 1> EHHandlers; |
| 204 | |
| 205 | // A vector of all Debuginfo emitters we should use. Protected so that |
| 206 | // targets can add their own. This vector maintains ownership of the |
| 207 | // emitters. |
| 208 | SmallVector<std::unique_ptr<AsmPrinterHandler>, 2> Handlers; |
| 209 | size_t NumUserHandlers = 0; |
| 210 | |
| 211 | StackMaps SM; |
| 212 | |
| 213 | private: |
| 214 | /// If generated on the fly this own the instance. |
| 215 | std::unique_ptr<MachineDominatorTree> OwnedMDT; |
| 216 | |
| 217 | /// If generated on the fly this own the instance. |
| 218 | std::unique_ptr<MachineLoopInfo> OwnedMLI; |
| 219 | |
| 220 | /// If the target supports dwarf debug info, this pointer is non-null. |
| 221 | DwarfDebug *DD = nullptr; |
| 222 | |
| 223 | /// A handler that supports pseudo probe emission with embedded inline |
| 224 | /// context. |
| 225 | std::unique_ptr<PseudoProbeHandler> PP; |
| 226 | |
| 227 | /// CFISection type the module needs i.e. either .eh_frame or .debug_frame. |
| 228 | CFISection ModuleCFISection = CFISection::None; |
| 229 | |
| 230 | /// True if the module contains split-stack functions. This is used to |
| 231 | /// emit .note.GNU-split-stack section as required by the linker for |
| 232 | /// special handling split-stack function calling no-split-stack function. |
| 233 | bool HasSplitStack = false; |
| 234 | |
| 235 | /// True if the module contains no-split-stack functions. This is used to emit |
| 236 | /// .note.GNU-no-split-stack section when it also contains functions without a |
| 237 | /// split stack prologue. |
| 238 | bool HasNoSplitStack = false; |
| 239 | |
| 240 | /// True if debugging information is available in this module. |
| 241 | bool DbgInfoAvailable = false; |
| 242 | |
| 243 | protected: |
| 244 | AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer, |
| 245 | char &ID = AsmPrinter::ID); |
| 246 | |
| 247 | public: |
| 248 | ~AsmPrinter() override; |
| 249 | |
| 250 | DwarfDebug *getDwarfDebug() { return DD; } |
| 251 | DwarfDebug *getDwarfDebug() const { return DD; } |
| 252 | |
| 253 | uint16_t getDwarfVersion() const; |
| 254 | void setDwarfVersion(uint16_t Version); |
| 255 | |
| 256 | bool isDwarf64() const; |
| 257 | |
| 258 | /// Returns 4 for DWARF32 and 8 for DWARF64. |
| 259 | unsigned int getDwarfOffsetByteSize() const; |
| 260 | |
| 261 | /// Returns 4 for DWARF32 and 12 for DWARF64. |
| 262 | unsigned int getUnitLengthFieldByteSize() const; |
| 263 | |
| 264 | /// Returns information about the byte size of DW_FORM values. |
| 265 | dwarf::FormParams getDwarfFormParams() const; |
| 266 | |
| 267 | bool isPositionIndependent() const; |
| 268 | |
| 269 | /// Return true if assembly output should contain comments. |
| 270 | bool isVerbose() const { return VerboseAsm; } |
| 271 | |
| 272 | /// Return a unique ID for the current function. |
| 273 | unsigned getFunctionNumber() const; |
| 274 | |
| 275 | /// Return symbol for the function pseudo stack if the stack frame is not a |
| 276 | /// register based. |
| 277 | virtual const MCSymbol *getFunctionFrameSymbol() const { return nullptr; } |
| 278 | |
| 279 | MCSymbol *getFunctionBegin() const { return CurrentFnBegin; } |
| 280 | MCSymbol *getFunctionEnd() const { return CurrentFnEnd; } |
| 281 | |
| 282 | // Return the exception symbol associated with the MBB section containing a |
| 283 | // given basic block. |
| 284 | MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB); |
| 285 | |
| 286 | /// Return the symbol to be used for the specified basic block when its |
| 287 | /// address is taken. This cannot be its normal LBB label because the block |
| 288 | /// may be accessed outside its containing function. |
| 289 | MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) { |
| 290 | return getAddrLabelSymbolToEmit(BB).front(); |
| 291 | } |
| 292 | |
| 293 | /// Return the symbol to be used for the specified basic block when its |
| 294 | /// address is taken. If other blocks were RAUW'd to this one, we may have |
| 295 | /// to emit them as well, return the whole set. |
| 296 | ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB); |
| 297 | |
| 298 | /// If the specified function has had any references to address-taken blocks |
| 299 | /// generated, but the block got deleted, return the symbol now so we can |
| 300 | /// emit it. This prevents emitting a reference to a symbol that has no |
| 301 | /// definition. |
| 302 | void takeDeletedSymbolsForFunction(const Function *F, |
| 303 | std::vector<MCSymbol *> &Result); |
| 304 | |
| 305 | /// Return information about object file lowering. |
| 306 | const TargetLoweringObjectFile &getObjFileLowering() const; |
| 307 | |
| 308 | /// Return information about data layout. |
| 309 | const DataLayout &getDataLayout() const; |
| 310 | |
| 311 | /// Return the pointer size from the TargetMachine |
| 312 | unsigned getPointerSize() const; |
| 313 | |
| 314 | /// Return information about subtarget. |
| 315 | const MCSubtargetInfo &getSubtargetInfo() const; |
| 316 | |
| 317 | void EmitToStreamer(MCStreamer &S, const MCInst &Inst); |
| 318 | |
| 319 | /// Emits inital debug location directive. |
| 320 | void emitInitialRawDwarfLocDirective(const MachineFunction &MF); |
| 321 | |
| 322 | /// Return the current section we are emitting to. |
| 323 | const MCSection *getCurrentSection() const; |
| 324 | |
| 325 | void getNameWithPrefix(SmallVectorImpl<char> &Name, |
| 326 | const GlobalValue *GV) const; |
| 327 | |
| 328 | MCSymbol *getSymbol(const GlobalValue *GV) const; |
| 329 | |
| 330 | /// Similar to getSymbol() but preferred for references. On ELF, this uses a |
| 331 | /// local symbol if a reference to GV is guaranteed to be resolved to the |
| 332 | /// definition in the same module. |
| 333 | MCSymbol *getSymbolPreferLocal(const GlobalValue &GV) const; |
| 334 | |
| 335 | bool doesDwarfUseRelocationsAcrossSections() const { |
| 336 | return DwarfUsesRelocationsAcrossSections; |
| 337 | } |
| 338 | |
| 339 | void setDwarfUsesRelocationsAcrossSections(bool Enable) { |
| 340 | DwarfUsesRelocationsAcrossSections = Enable; |
| 341 | } |
| 342 | |
| 343 | /// Returns a section suffix (hot or unlikely) for the constant if profiles |
| 344 | /// are available. Returns empty string otherwise. |
| 345 | StringRef getConstantSectionSuffix(const Constant *C) const; |
| 346 | |
| 347 | //===------------------------------------------------------------------===// |
| 348 | // XRay instrumentation implementation. |
| 349 | //===------------------------------------------------------------------===// |
| 350 | public: |
| 351 | // This describes the kind of sled we're storing in the XRay table. |
| 352 | enum class SledKind : uint8_t { |
| 353 | FUNCTION_ENTER = 0, |
| 354 | FUNCTION_EXIT = 1, |
| 355 | TAIL_CALL = 2, |
| 356 | LOG_ARGS_ENTER = 3, |
| 357 | CUSTOM_EVENT = 4, |
| 358 | TYPED_EVENT = 5, |
| 359 | }; |
| 360 | |
| 361 | // The table will contain these structs that point to the sled, the function |
| 362 | // containing the sled, and what kind of sled (and whether they should always |
| 363 | // be instrumented). We also use a version identifier that the runtime can use |
| 364 | // to decide what to do with the sled, depending on the version of the sled. |
| 365 | struct XRayFunctionEntry { |
| 366 | const MCSymbol *Sled; |
| 367 | const MCSymbol *Function; |
| 368 | SledKind Kind; |
| 369 | bool AlwaysInstrument; |
| 370 | const class Function *Fn; |
| 371 | uint8_t Version; |
| 372 | |
| 373 | LLVM_ABI void emit(int, MCStreamer *) const; |
| 374 | }; |
| 375 | |
| 376 | // All the sleds to be emitted. |
| 377 | SmallVector<XRayFunctionEntry, 4> Sleds; |
| 378 | |
| 379 | // Helper function to record a given XRay sled. |
| 380 | void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind, |
| 381 | uint8_t Version = 0); |
| 382 | |
| 383 | /// Emit a table with all XRay instrumentation points. |
| 384 | void emitXRayTable(); |
| 385 | |
| 386 | void emitPatchableFunctionEntries(); |
| 387 | |
| 388 | //===------------------------------------------------------------------===// |
| 389 | // MachineFunctionPass Implementation. |
| 390 | //===------------------------------------------------------------------===// |
| 391 | |
| 392 | /// Record analysis usage. |
| 393 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 394 | |
| 395 | /// Set up the AsmPrinter when we are working on a new module. If your pass |
| 396 | /// overrides this, it must make sure to explicitly call this implementation. |
| 397 | bool doInitialization(Module &M) override; |
| 398 | |
| 399 | /// Shut down the asmprinter. If you override this in your pass, you must make |
| 400 | /// sure to call it explicitly. |
| 401 | bool doFinalization(Module &M) override; |
| 402 | |
| 403 | /// Emit the specified function out to the OutStreamer. |
| 404 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 405 | SetupMachineFunction(MF); |
| 406 | emitFunctionBody(); |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | //===------------------------------------------------------------------===// |
| 411 | // Coarse grained IR lowering routines. |
| 412 | //===------------------------------------------------------------------===// |
| 413 | |
| 414 | /// This should be called when a new MachineFunction is being processed from |
| 415 | /// runOnMachineFunction. |
| 416 | virtual void SetupMachineFunction(MachineFunction &MF); |
| 417 | |
| 418 | /// This method emits the body and trailer for a function. |
| 419 | void emitFunctionBody(); |
| 420 | |
| 421 | void emitCFIInstruction(const MachineInstr &MI); |
| 422 | |
| 423 | void emitFrameAlloc(const MachineInstr &MI); |
| 424 | |
| 425 | void emitStackSizeSection(const MachineFunction &MF); |
| 426 | |
| 427 | void emitStackUsage(const MachineFunction &MF); |
| 428 | |
| 429 | void emitBBAddrMapSection(const MachineFunction &MF); |
| 430 | |
| 431 | void emitKCFITrapEntry(const MachineFunction &MF, const MCSymbol *Symbol); |
| 432 | virtual void emitKCFITypeId(const MachineFunction &MF); |
| 433 | |
| 434 | void emitPseudoProbe(const MachineInstr &MI); |
| 435 | |
| 436 | void (remarks::RemarkStreamer &RS); |
| 437 | |
| 438 | /// Emits a label as reference for PC sections. |
| 439 | void emitPCSectionsLabel(const MachineFunction &MF, const MDNode &MD); |
| 440 | |
| 441 | /// Emits the PC sections collected from instructions. |
| 442 | void emitPCSections(const MachineFunction &MF); |
| 443 | |
| 444 | /// Get the CFISection type for a function. |
| 445 | CFISection getFunctionCFISectionType(const Function &F) const; |
| 446 | |
| 447 | /// Get the CFISection type for a function. |
| 448 | CFISection getFunctionCFISectionType(const MachineFunction &MF) const; |
| 449 | |
| 450 | /// Get the CFISection type for the module. |
| 451 | CFISection getModuleCFISectionType() const { return ModuleCFISection; } |
| 452 | |
| 453 | /// Returns true if valid debug info is present. |
| 454 | bool hasDebugInfo() const { return DbgInfoAvailable; } |
| 455 | |
| 456 | bool needsSEHMoves(); |
| 457 | |
| 458 | /// Since emitting CFI unwind information is entangled with supporting the |
| 459 | /// exceptions, this returns true for platforms which use CFI unwind |
| 460 | /// information for other purposes (debugging, sanitizers, ...) when |
| 461 | /// `MCAsmInfo::ExceptionsType == ExceptionHandling::None`. |
| 462 | bool usesCFIWithoutEH() const; |
| 463 | |
| 464 | /// Print to the current output stream assembly representations of the |
| 465 | /// constants in the constant pool MCP. This is used to print out constants |
| 466 | /// which have been "spilled to memory" by the code generator. |
| 467 | virtual void emitConstantPool(); |
| 468 | |
| 469 | /// Print assembly representations of the jump tables used by the current |
| 470 | /// function to the current output stream. |
| 471 | virtual void emitJumpTableInfo(); |
| 472 | |
| 473 | /// Emit the specified global variable to the .s file. |
| 474 | virtual void emitGlobalVariable(const GlobalVariable *GV); |
| 475 | |
| 476 | /// Check to see if the specified global is a special global used by LLVM. If |
| 477 | /// so, emit it and return true, otherwise do nothing and return false. |
| 478 | bool emitSpecialLLVMGlobal(const GlobalVariable *GV); |
| 479 | |
| 480 | /// `llvm.global_ctors` and `llvm.global_dtors` are arrays of Structor |
| 481 | /// structs. |
| 482 | /// |
| 483 | /// Priority - init priority |
| 484 | /// Func - global initialization or global clean-up function |
| 485 | /// ComdatKey - associated data |
| 486 | struct Structor { |
| 487 | int Priority = 0; |
| 488 | Constant *Func = nullptr; |
| 489 | GlobalValue *ComdatKey = nullptr; |
| 490 | |
| 491 | Structor() = default; |
| 492 | }; |
| 493 | |
| 494 | /// This method gathers an array of Structors and then sorts them out by |
| 495 | /// Priority. |
| 496 | /// @param List The initializer of `llvm.global_ctors` or `llvm.global_dtors` |
| 497 | /// array. |
| 498 | /// @param[out] Structors Sorted Structor structs by Priority. |
| 499 | void preprocessXXStructorList(const DataLayout &DL, const Constant *List, |
| 500 | SmallVector<Structor, 8> &Structors); |
| 501 | |
| 502 | /// This method emits `llvm.global_ctors` or `llvm.global_dtors` list. |
| 503 | virtual void emitXXStructorList(const DataLayout &DL, const Constant *List, |
| 504 | bool IsCtor); |
| 505 | |
| 506 | /// Emit an alignment directive to the specified power of two boundary. If a |
| 507 | /// global value is specified, and if that global has an explicit alignment |
| 508 | /// requested, it will override the alignment request if required for |
| 509 | /// correctness. |
| 510 | void emitAlignment(Align Alignment, const GlobalObject *GV = nullptr, |
| 511 | unsigned MaxBytesToEmit = 0) const; |
| 512 | |
| 513 | /// Lower the specified LLVM Constant to an MCExpr. |
| 514 | /// When BaseCV is present, we are lowering the element at BaseCV plus Offset. |
| 515 | virtual const MCExpr *lowerConstant(const Constant *CV, |
| 516 | const Constant *BaseCV = nullptr, |
| 517 | uint64_t Offset = 0); |
| 518 | |
| 519 | /// Print a general LLVM constant to the .s file. |
| 520 | /// On AIX, when an alias refers to a sub-element of a global variable, the |
| 521 | /// label of that alias needs to be emitted before the corresponding element. |
| 522 | using AliasMapTy = DenseMap<uint64_t, SmallVector<const GlobalAlias *, 1>>; |
| 523 | void emitGlobalConstant(const DataLayout &DL, const Constant *CV, |
| 524 | AliasMapTy *AliasList = nullptr); |
| 525 | |
| 526 | /// Unnamed constant global variables solely contaning a pointer to |
| 527 | /// another globals variable act like a global variable "proxy", or GOT |
| 528 | /// equivalents, i.e., it's only used to hold the address of the latter. One |
| 529 | /// optimization is to replace accesses to these proxies by using the GOT |
| 530 | /// entry for the final global instead. Hence, we select GOT equivalent |
| 531 | /// candidates among all the module global variables, avoid emitting them |
| 532 | /// unnecessarily and finally replace references to them by pc relative |
| 533 | /// accesses to GOT entries. |
| 534 | void computeGlobalGOTEquivs(Module &M); |
| 535 | |
| 536 | /// Constant expressions using GOT equivalent globals may not be |
| 537 | /// eligible for PC relative GOT entry conversion, in such cases we need to |
| 538 | /// emit the proxies we previously omitted in EmitGlobalVariable. |
| 539 | void emitGlobalGOTEquivs(); |
| 540 | |
| 541 | /// Emit the stack maps. |
| 542 | void emitStackMaps(); |
| 543 | |
| 544 | //===------------------------------------------------------------------===// |
| 545 | // Overridable Hooks |
| 546 | //===------------------------------------------------------------------===// |
| 547 | |
| 548 | void addAsmPrinterHandler(std::unique_ptr<AsmPrinterHandler> Handler); |
| 549 | |
| 550 | // Targets can, or in the case of EmitInstruction, must implement these to |
| 551 | // customize output. |
| 552 | |
| 553 | /// This virtual method can be overridden by targets that want to emit |
| 554 | /// something at the start of their file. |
| 555 | virtual void emitStartOfAsmFile(Module &) {} |
| 556 | |
| 557 | /// This virtual method can be overridden by targets that want to emit |
| 558 | /// something at the end of their file. |
| 559 | virtual void emitEndOfAsmFile(Module &) {} |
| 560 | |
| 561 | /// Targets can override this to emit stuff before the first basic block in |
| 562 | /// the function. |
| 563 | virtual void emitFunctionBodyStart() {} |
| 564 | |
| 565 | /// Targets can override this to emit stuff after the last basic block in the |
| 566 | /// function. |
| 567 | virtual void emitFunctionBodyEnd() {} |
| 568 | |
| 569 | /// Targets can override this to emit stuff at the start of a basic block. |
| 570 | /// By default, this method prints the label for the specified |
| 571 | /// MachineBasicBlock, an alignment (if present) and a comment describing it |
| 572 | /// if appropriate. |
| 573 | virtual void emitBasicBlockStart(const MachineBasicBlock &MBB); |
| 574 | |
| 575 | /// Targets can override this to emit stuff at the end of a basic block. |
| 576 | virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB); |
| 577 | |
| 578 | /// Targets should implement this to emit instructions. |
| 579 | virtual void emitInstruction(const MachineInstr *) { |
| 580 | llvm_unreachable("EmitInstruction not implemented" ); |
| 581 | } |
| 582 | |
| 583 | /// Return the symbol for the specified constant pool entry. |
| 584 | virtual MCSymbol *GetCPISymbol(unsigned CPID) const; |
| 585 | |
| 586 | virtual void emitFunctionEntryLabel(); |
| 587 | |
| 588 | virtual void emitFunctionDescriptor() { |
| 589 | llvm_unreachable("Function descriptor is target-specific." ); |
| 590 | } |
| 591 | |
| 592 | virtual void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV); |
| 593 | |
| 594 | /// Targets can override this to change how global constants that are part of |
| 595 | /// a C++ static/global constructor list are emitted. |
| 596 | virtual void emitXXStructor(const DataLayout &DL, const Constant *CV) { |
| 597 | emitGlobalConstant(DL, CV); |
| 598 | } |
| 599 | |
| 600 | virtual const MCExpr *lowerConstantPtrAuth(const ConstantPtrAuth &CPA) { |
| 601 | report_fatal_error(reason: "ptrauth constant lowering not implemented" ); |
| 602 | } |
| 603 | |
| 604 | /// Lower the specified BlockAddress to an MCExpr. |
| 605 | virtual const MCExpr *lowerBlockAddressConstant(const BlockAddress &BA); |
| 606 | |
| 607 | /// Return true if the basic block has exactly one predecessor and the control |
| 608 | /// transfer mechanism between the predecessor and this block is a |
| 609 | /// fall-through. |
| 610 | virtual bool |
| 611 | isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const; |
| 612 | |
| 613 | /// Targets can override this to customize the output of IMPLICIT_DEF |
| 614 | /// instructions in verbose mode. |
| 615 | virtual void emitImplicitDef(const MachineInstr *MI) const; |
| 616 | |
| 617 | /// getSubtargetInfo() cannot be used where this is needed because we don't |
| 618 | /// have a MachineFunction when we're lowering a GlobalIFunc, and |
| 619 | /// getSubtargetInfo requires one. Override the implementation in targets |
| 620 | /// that support the Mach-O IFunc lowering. |
| 621 | virtual const MCSubtargetInfo *getIFuncMCSubtargetInfo() const { |
| 622 | return nullptr; |
| 623 | } |
| 624 | |
| 625 | virtual void emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI, |
| 626 | MCSymbol *LazyPointer) { |
| 627 | llvm_unreachable( |
| 628 | "Mach-O IFunc lowering is not yet supported on this target" ); |
| 629 | } |
| 630 | |
| 631 | virtual void emitMachOIFuncStubHelperBody(Module &M, const GlobalIFunc &GI, |
| 632 | MCSymbol *LazyPointer) { |
| 633 | llvm_unreachable( |
| 634 | "Mach-O IFunc lowering is not yet supported on this target" ); |
| 635 | } |
| 636 | |
| 637 | /// Emit N NOP instructions. |
| 638 | void emitNops(unsigned N); |
| 639 | |
| 640 | //===------------------------------------------------------------------===// |
| 641 | // Symbol Lowering Routines. |
| 642 | //===------------------------------------------------------------------===// |
| 643 | |
| 644 | MCSymbol *createTempSymbol(const Twine &Name) const; |
| 645 | |
| 646 | /// Return the MCSymbol for a private symbol with global value name as its |
| 647 | /// base, with the specified suffix. |
| 648 | MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, |
| 649 | StringRef Suffix) const; |
| 650 | |
| 651 | /// Return the MCSymbol for the specified ExternalSymbol. |
| 652 | MCSymbol *GetExternalSymbolSymbol(const Twine &Sym) const; |
| 653 | |
| 654 | /// Return the symbol for the specified jump table entry. |
| 655 | MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const; |
| 656 | |
| 657 | /// Return the symbol for the specified jump table .set |
| 658 | /// FIXME: privatize to AsmPrinter. |
| 659 | MCSymbol *GetJTSetSymbol(unsigned UID, unsigned MBBID) const; |
| 660 | |
| 661 | /// Return the MCSymbol used to satisfy BlockAddress uses of the specified |
| 662 | /// basic block. |
| 663 | MCSymbol *GetBlockAddressSymbol(const BlockAddress *BA) const; |
| 664 | MCSymbol *GetBlockAddressSymbol(const BasicBlock *BB) const; |
| 665 | |
| 666 | //===------------------------------------------------------------------===// |
| 667 | // Emission Helper Routines. |
| 668 | //===------------------------------------------------------------------===// |
| 669 | |
| 670 | /// This is just convenient handler for printing offsets. |
| 671 | void printOffset(int64_t Offset, raw_ostream &OS) const; |
| 672 | |
| 673 | /// Emit a byte directive and value. |
| 674 | void emitInt8(int Value) const; |
| 675 | |
| 676 | /// Emit a short directive and value. |
| 677 | void emitInt16(int Value) const; |
| 678 | |
| 679 | /// Emit a long directive and value. |
| 680 | void emitInt32(int Value) const; |
| 681 | |
| 682 | /// Emit a long long directive and value. |
| 683 | void emitInt64(uint64_t Value) const; |
| 684 | |
| 685 | /// Emit the specified signed leb128 value. |
| 686 | void emitSLEB128(int64_t Value, const char *Desc = nullptr) const; |
| 687 | |
| 688 | /// Emit the specified unsigned leb128 value. |
| 689 | void emitULEB128(uint64_t Value, const char *Desc = nullptr, |
| 690 | unsigned PadTo = 0) const; |
| 691 | |
| 692 | /// Emit something like ".long Hi-Lo" where the size in bytes of the directive |
| 693 | /// is specified by Size and Hi/Lo specify the labels. This implicitly uses |
| 694 | /// .set if it is available. |
| 695 | void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, |
| 696 | unsigned Size) const; |
| 697 | |
| 698 | /// Emit something like ".uleb128 Hi-Lo". |
| 699 | void emitLabelDifferenceAsULEB128(const MCSymbol *Hi, |
| 700 | const MCSymbol *Lo) const; |
| 701 | |
| 702 | /// Emit something like ".long Label+Offset" where the size in bytes of the |
| 703 | /// directive is specified by Size and Label specifies the label. This |
| 704 | /// implicitly uses .set if it is available. |
| 705 | void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, |
| 706 | unsigned Size, bool IsSectionRelative = false) const; |
| 707 | |
| 708 | /// Emit something like ".long Label" where the size in bytes of the directive |
| 709 | /// is specified by Size and Label specifies the label. |
| 710 | void emitLabelReference(const MCSymbol *Label, unsigned Size, |
| 711 | bool IsSectionRelative = false) const { |
| 712 | emitLabelPlusOffset(Label, Offset: 0, Size, IsSectionRelative); |
| 713 | } |
| 714 | |
| 715 | //===------------------------------------------------------------------===// |
| 716 | // Dwarf Emission Helper Routines |
| 717 | //===------------------------------------------------------------------===// |
| 718 | |
| 719 | /// Emit a .byte 42 directive that corresponds to an encoding. If verbose |
| 720 | /// assembly output is enabled, we output comments describing the encoding. |
| 721 | /// Desc is a string saying what the encoding is specifying (e.g. "LSDA"). |
| 722 | void emitEncodingByte(unsigned Val, const char *Desc = nullptr) const; |
| 723 | |
| 724 | /// Return the size of the encoding in bytes. |
| 725 | unsigned GetSizeOfEncodedValue(unsigned Encoding) const; |
| 726 | |
| 727 | /// Emit reference to a ttype global with a specified encoding. |
| 728 | virtual void emitTTypeReference(const GlobalValue *GV, unsigned Encoding); |
| 729 | |
| 730 | /// Emit a reference to a symbol for use in dwarf. Different object formats |
| 731 | /// represent this in different ways. Some use a relocation others encode |
| 732 | /// the label offset in its section. |
| 733 | void emitDwarfSymbolReference(const MCSymbol *Label, |
| 734 | bool ForceOffset = false) const; |
| 735 | |
| 736 | /// Emit the 4- or 8-byte offset of a string from the start of its section. |
| 737 | /// |
| 738 | /// When possible, emit a DwarfStringPool section offset without any |
| 739 | /// relocations, and without using the symbol. Otherwise, defers to \a |
| 740 | /// emitDwarfSymbolReference(). |
| 741 | /// |
| 742 | /// The length of the emitted value depends on the DWARF format. |
| 743 | void emitDwarfStringOffset(DwarfStringPoolEntry S) const; |
| 744 | |
| 745 | /// Emit the 4-or 8-byte offset of a string from the start of its section. |
| 746 | void emitDwarfStringOffset(DwarfStringPoolEntryRef S) const { |
| 747 | emitDwarfStringOffset(S: S.getEntry()); |
| 748 | } |
| 749 | |
| 750 | /// Emit something like ".long Label + Offset" or ".quad Label + Offset" |
| 751 | /// depending on the DWARF format. |
| 752 | void emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const; |
| 753 | |
| 754 | /// Emit 32- or 64-bit value depending on the DWARF format. |
| 755 | void emitDwarfLengthOrOffset(uint64_t Value) const; |
| 756 | |
| 757 | /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen |
| 758 | /// according to the settings. |
| 759 | void emitDwarfUnitLength(uint64_t Length, const Twine &) const; |
| 760 | |
| 761 | /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen |
| 762 | /// according to the settings. |
| 763 | /// Return the end symbol generated inside, the caller needs to emit it. |
| 764 | MCSymbol *emitDwarfUnitLength(const Twine &Prefix, |
| 765 | const Twine &) const; |
| 766 | |
| 767 | /// Emit reference to a call site with a specified encoding |
| 768 | void emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, |
| 769 | unsigned Encoding) const; |
| 770 | /// Emit an integer value corresponding to the call site encoding |
| 771 | void emitCallSiteValue(uint64_t Value, unsigned Encoding) const; |
| 772 | |
| 773 | /// Get the value for DW_AT_APPLE_isa. Zero if no isa encoding specified. |
| 774 | virtual unsigned getISAEncoding() { return 0; } |
| 775 | |
| 776 | /// Emit the directive and value for debug thread local expression |
| 777 | /// |
| 778 | /// \p Value - The value to emit. |
| 779 | /// \p Size - The size of the integer (in bytes) to emit. |
| 780 | virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const; |
| 781 | |
| 782 | //===------------------------------------------------------------------===// |
| 783 | // Dwarf Lowering Routines |
| 784 | //===------------------------------------------------------------------===// |
| 785 | |
| 786 | /// Emit frame instruction to describe the layout of the frame. |
| 787 | void emitCFIInstruction(const MCCFIInstruction &Inst) const; |
| 788 | |
| 789 | /// Emit Dwarf abbreviation table. |
| 790 | template <typename T> void emitDwarfAbbrevs(const T &Abbrevs) const { |
| 791 | // For each abbreviation. |
| 792 | for (const auto &Abbrev : Abbrevs) |
| 793 | emitDwarfAbbrev(Abbrev: *Abbrev); |
| 794 | |
| 795 | // Mark end of abbreviations. |
| 796 | emitULEB128(Value: 0, Desc: "EOM(3)" ); |
| 797 | } |
| 798 | |
| 799 | void emitDwarfAbbrev(const DIEAbbrev &Abbrev) const; |
| 800 | |
| 801 | /// Recursively emit Dwarf DIE tree. |
| 802 | void emitDwarfDIE(const DIE &Die) const; |
| 803 | |
| 804 | //===------------------------------------------------------------------===// |
| 805 | // CodeView Helper Routines |
| 806 | //===------------------------------------------------------------------===// |
| 807 | |
| 808 | /// Gets information required to create a CodeView debug symbol for a jump |
| 809 | /// table. |
| 810 | /// Return value is <Base Address, Base Offset, Branch Address, Entry Size> |
| 811 | virtual std::tuple<const MCSymbol *, uint64_t, const MCSymbol *, |
| 812 | codeview::JumpTableEntrySize> |
| 813 | getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr, |
| 814 | const MCSymbol *BranchLabel) const; |
| 815 | |
| 816 | //===------------------------------------------------------------------===// |
| 817 | // COFF Helper Routines |
| 818 | //===------------------------------------------------------------------===// |
| 819 | |
| 820 | /// Emits symbols and data to allow functions marked with the |
| 821 | /// loader-replaceable attribute to be replaceable. |
| 822 | void emitCOFFReplaceableFunctionData(Module &M); |
| 823 | |
| 824 | /// Emits the @feat.00 symbol indicating the features enabled in this module. |
| 825 | void emitCOFFFeatureSymbol(Module &M); |
| 826 | |
| 827 | //===------------------------------------------------------------------===// |
| 828 | // Inline Asm Support |
| 829 | //===------------------------------------------------------------------===// |
| 830 | |
| 831 | // These are hooks that targets can override to implement inline asm |
| 832 | // support. These should probably be moved out of AsmPrinter someday. |
| 833 | |
| 834 | /// Print information related to the specified machine instr that is |
| 835 | /// independent of the operand, and may be independent of the instr itself. |
| 836 | /// This can be useful for portably encoding the comment character or other |
| 837 | /// bits of target-specific knowledge into the asmstrings. The syntax used is |
| 838 | /// ${:comment}. Targets can override this to add support for their own |
| 839 | /// strange codes. |
| 840 | virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, |
| 841 | StringRef Code) const; |
| 842 | |
| 843 | /// Print the MachineOperand as a symbol. Targets with complex handling of |
| 844 | /// symbol references should override the base implementation. |
| 845 | virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS); |
| 846 | |
| 847 | /// Print the specified operand of MI, an INLINEASM instruction, using the |
| 848 | /// specified assembler variant. Targets should override this to format as |
| 849 | /// appropriate. This method can return true if the operand is erroneous. |
| 850 | virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 851 | const char *, raw_ostream &OS); |
| 852 | |
| 853 | /// Print the specified operand of MI, an INLINEASM instruction, using the |
| 854 | /// specified assembler variant as an address. Targets should override this to |
| 855 | /// format as appropriate. This method can return true if the operand is |
| 856 | /// erroneous. |
| 857 | virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 858 | const char *, raw_ostream &OS); |
| 859 | |
| 860 | /// Let the target do anything it needs to do before emitting inlineasm. |
| 861 | /// \p StartInfo - the subtarget info before parsing inline asm |
| 862 | virtual void emitInlineAsmStart() const; |
| 863 | |
| 864 | /// Let the target do anything it needs to do after emitting inlineasm. |
| 865 | /// This callback can be used restore the original mode in case the |
| 866 | /// inlineasm contains directives to switch modes. |
| 867 | /// \p StartInfo - the original subtarget info before inline asm |
| 868 | /// \p EndInfo - the final subtarget info after parsing the inline asm, |
| 869 | /// or NULL if the value is unknown. |
| 870 | virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, |
| 871 | const MCSubtargetInfo *EndInfo) const; |
| 872 | |
| 873 | /// This emits visibility information about symbol, if this is supported by |
| 874 | /// the target. |
| 875 | void emitVisibility(MCSymbol *Sym, unsigned Visibility, |
| 876 | bool IsDefinition = true) const; |
| 877 | |
| 878 | /// This emits linkage information about \p GVSym based on \p GV, if this is |
| 879 | /// supported by the target. |
| 880 | virtual void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const; |
| 881 | |
| 882 | /// Return the alignment for the specified \p GV. |
| 883 | static Align getGVAlignment(const GlobalObject *GV, const DataLayout &DL, |
| 884 | Align InAlign = Align(1)); |
| 885 | |
| 886 | private: |
| 887 | /// Private state for PrintSpecial() |
| 888 | // Assign a unique ID to this machine instruction. |
| 889 | mutable const MachineInstr *LastMI = nullptr; |
| 890 | mutable unsigned LastFn = 0; |
| 891 | mutable unsigned Counter = ~0U; |
| 892 | |
| 893 | bool DwarfUsesRelocationsAcrossSections = false; |
| 894 | |
| 895 | /// This method emits the header for the current function. |
| 896 | virtual void (); |
| 897 | |
| 898 | /// This method emits a comment next to header for the current function. |
| 899 | virtual void (); |
| 900 | |
| 901 | /// This method emits prefix-like data before the current function. |
| 902 | void emitFunctionPrefix(ArrayRef<const Constant *> Prefix); |
| 903 | |
| 904 | /// Emit a blob of inline asm to the output streamer. |
| 905 | void |
| 906 | emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, |
| 907 | const MCTargetOptions &MCOptions, |
| 908 | const MDNode *LocMDNode = nullptr, |
| 909 | InlineAsm::AsmDialect AsmDialect = InlineAsm::AD_ATT) const; |
| 910 | |
| 911 | /// This method formats and emits the specified machine instruction that is an |
| 912 | /// inline asm. |
| 913 | void emitInlineAsm(const MachineInstr *MI) const; |
| 914 | |
| 915 | /// Add inline assembly info to the diagnostics machinery, so we can |
| 916 | /// emit file and position info. Returns SrcMgr memory buffer position. |
| 917 | unsigned addInlineAsmDiagBuffer(StringRef AsmStr, |
| 918 | const MDNode *LocMDNode) const; |
| 919 | |
| 920 | //===------------------------------------------------------------------===// |
| 921 | // Internal Implementation Details |
| 922 | //===------------------------------------------------------------------===// |
| 923 | |
| 924 | virtual void emitJumpTableImpl(const MachineJumpTableInfo &MJTI, |
| 925 | ArrayRef<unsigned> JumpTableIndices); |
| 926 | |
| 927 | void emitJumpTableSizesSection(const MachineJumpTableInfo &MJTI, |
| 928 | const Function &F) const; |
| 929 | |
| 930 | void emitLLVMUsedList(const ConstantArray *InitList); |
| 931 | /// Emit llvm.ident metadata in an '.ident' directive. |
| 932 | void emitModuleIdents(Module &M); |
| 933 | /// Emit bytes for llvm.commandline metadata. |
| 934 | virtual void emitModuleCommandLines(Module &M); |
| 935 | |
| 936 | GCMetadataPrinter *getOrCreateGCPrinter(GCStrategy &S); |
| 937 | void emitGlobalIFunc(Module &M, const GlobalIFunc &GI); |
| 938 | |
| 939 | /// This method decides whether the specified basic block requires a label. |
| 940 | bool shouldEmitLabelForBasicBlock(const MachineBasicBlock &MBB) const; |
| 941 | |
| 942 | protected: |
| 943 | virtual void emitJumpTableEntry(const MachineJumpTableInfo &MJTI, |
| 944 | const MachineBasicBlock *MBB, |
| 945 | unsigned uid) const; |
| 946 | virtual void emitGlobalAlias(const Module &M, const GlobalAlias &GA); |
| 947 | virtual bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const { |
| 948 | return false; |
| 949 | } |
| 950 | }; |
| 951 | |
| 952 | } // end namespace llvm |
| 953 | |
| 954 | #endif // LLVM_CODEGEN_ASMPRINTER_H |
| 955 | |