LLVM 24.0.0git
X86AsmPrinter.h
Go to the documentation of this file.
1//===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- 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#ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
10#define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11
12#include "llvm/ADT/MapVector.h"
17
18// Implemented in X86MCInstLower.cpp
19namespace {
20 class X86MCInstLower;
21}
22
23namespace llvm {
24class MCCodeEmitter;
25class MCStreamer;
26class X86Subtarget;
27class TargetMachine;
28
30public:
31 static char ID;
32
33private:
34 const X86Subtarget *Subtarget = nullptr;
35 FaultMaps FM;
36 std::unique_ptr<MCCodeEmitter> CodeEmitter;
37 bool EmitFPOData = false;
38 bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;
39 bool IndCSPrefix = false;
40 bool EnableImportCallOptimization = false;
41 bool SplitChainedAtEndOfBlock = false;
42
43 enum ImportCallKind : unsigned {
44 IMAGE_RETPOLINE_AMD64_IMPORT_BR = 0x02,
45 IMAGE_RETPOLINE_AMD64_IMPORT_CALL = 0x03,
46 IMAGE_RETPOLINE_AMD64_INDIR_BR = 0x04,
47 IMAGE_RETPOLINE_AMD64_INDIR_CALL = 0x05,
48 IMAGE_RETPOLINE_AMD64_INDIR_BR_REX = 0x06,
49 IMAGE_RETPOLINE_AMD64_CFG_BR = 0x08,
50 IMAGE_RETPOLINE_AMD64_CFG_CALL = 0x09,
51 IMAGE_RETPOLINE_AMD64_CFG_BR_REX = 0x0A,
52 IMAGE_RETPOLINE_AMD64_SWITCHTABLE_FIRST = 0x010,
53 IMAGE_RETPOLINE_AMD64_SWITCHTABLE_LAST = 0x01F,
54 };
55 struct ImportCallInfo {
56 MCSymbol *CalleeSymbol;
57 ImportCallKind Kind;
58 };
59 MapVector<MCSection *, std::vector<ImportCallInfo>>
60 SectionToImportedFunctionCalls;
61
62 // This utility class tracks the length of a stackmap instruction's 'shadow'.
63 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
64 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
65 // the shadow) are met, while outputting a minimal number of NOPs for padding.
66 //
67 // To minimise the number of NOPs used, the shadow tracker counts the number
68 // of instruction bytes output since the last stackmap. Only if there are too
69 // few instruction bytes to cover the shadow are NOPs used for padding.
70 class StackMapShadowTracker {
71 public:
72 void startFunction(MachineFunction &MF) {
73 this->MF = &MF;
74 }
75 void count(const MCInst &Inst, const MCSubtargetInfo &STI,
76 MCCodeEmitter *CodeEmitter);
77
78 // Called to signal the start of a shadow of RequiredSize bytes.
79 void reset(unsigned RequiredSize) {
80 RequiredShadowSize = RequiredSize;
81 CurrentShadowSize = 0;
82 InShadow = true;
83 }
84
85 // Called before every stackmap/patchpoint, and at the end of basic blocks,
86 // to emit any necessary padding-NOPs.
87 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
88 private:
89 const MachineFunction *MF = nullptr;
90 bool InShadow = false;
91
92 // RequiredShadowSize holds the length of the shadow specified in the most
93 // recently encountered STACKMAP instruction.
94 // CurrentShadowSize counts the number of bytes encoded since the most
95 // recently encountered STACKMAP, stopping when that number is greater than
96 // or equal to RequiredShadowSize.
97 unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
98 };
99
100 StackMapShadowTracker SMShadowTracker;
101
102 // All instructions emitted by the X86AsmPrinter should use this helper
103 // method.
104 //
105 // This helper function invokes the SMShadowTracker on each instruction before
106 // outputting it to the OutStream. This allows the shadow tracker to minimise
107 // the number of NOPs used for stackmap padding.
108 void EmitAndCountInstruction(MCInst &Inst);
109 void LowerSTACKMAP(const MachineInstr &MI);
110 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
111 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
112 void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
113 void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
114
115 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
116
117 // XRay-specific lowering for X86.
118 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
119 X86MCInstLower &MCIL);
120 void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
121 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
122 void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
123 void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI,
124 X86MCInstLower &MCIL);
125
126 void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
127
128 // KCFI specific lowering for X86.
129 uint32_t MaskKCFIType(uint32_t Value);
130 void EmitKCFITypePadding(const MachineFunction &MF, bool HasType = true);
131 void LowerKCFI_CHECK(const MachineInstr &MI);
132
133 // Address sanitizer specific lowering for X86.
134 void LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI);
135
136 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
137 void EmitSEHInstruction(const MachineInstr *MI);
138
139 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
140 void PrintOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
141 void PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo,
142 raw_ostream &O, StringRef Modifier = {});
143 void PrintPCRelImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
144 void PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo,
145 raw_ostream &O, StringRef Modifier = {});
146 void PrintMemReference(const MachineInstr *MI, unsigned OpNo, raw_ostream &O,
147 StringRef Modifier = {});
148 void PrintIntelMemReference(const MachineInstr *MI, unsigned OpNo,
149 raw_ostream &O, StringRef Modifier = {});
150 const MCSubtargetInfo *getIFuncMCSubtargetInfo() const override;
151 void emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI,
152 MCSymbol *LazyPointer) override;
153 void emitMachOIFuncStubHelperBody(Module &M, const GlobalIFunc &GI,
154 MCSymbol *LazyPointer) override;
155
156 void emitCallInstruction(const llvm::MCInst &MCI);
157 void maybeEmitNopAfterCallForWindowsEH(const MachineInstr *MI);
158
159 // Emits a label to mark the next instruction as being relevant to Import Call
160 // Optimization.
161 void emitLabelAndRecordForImportCallOptimization(ImportCallKind Kind);
162
163public:
164 X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
165
166 StringRef getPassName() const override {
167 return "X86 Assembly Printer";
168 }
169
170 const X86Subtarget &getSubtarget() const { return *Subtarget; }
171
172 void emitStartOfAsmFile(Module &M) override;
173
174 void emitEndOfAsmFile(Module &M) override;
175
176 void emitInstruction(const MachineInstr *MI) override;
177
178 void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
179 const MCSubtargetInfo *EndInfo,
180 const MachineInstr *MI) override;
181
182 void emitBasicBlockEnd(const MachineBasicBlock &MBB) override;
183
184 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
185 const char *ExtraCode, raw_ostream &O) override;
186 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
187 const char *ExtraCode, raw_ostream &O) override;
188
189 bool doInitialization(Module &M) override {
190 SMShadowTracker.reset(0);
191 SM.reset();
192 FM.reset();
194 }
195
196 bool runOnMachineFunction(MachineFunction &MF) override;
197 void emitFunctionBodyStart() override;
198 void emitFunctionBodyEnd() override;
199 void emitKCFITypeId(const MachineFunction &MF) override;
200
202 return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;
203 }
204
205 std::function<ProfileSummaryInfo *(Module &)> GetPSI;
206 std::function<const StaticDataProfileInfo *(Module &)> GetSDPI;
207};
208
210 : public RequiredPassInfoMixin<X86AsmPrinterBeginPass> {
211public:
213};
214
215class X86AsmPrinterPass : public RequiredPassInfoMixin<X86AsmPrinterPass> {
216public:
219 // AsmPrinter needs to run regardless of optimization level.
220};
221
223 : public RequiredPassInfoMixin<X86AsmPrinterEndPass> {
224public:
226};
227
228} // end namespace llvm
229
230#endif
MachineBasicBlock & MBB
Interfaces for producing common pass manager configurations.
#define LLVM_LIBRARY_VISIBILITY
Definition Compiler.h:137
IRTranslator LLVM IR MI
Machine Check Debug Module
This file implements a map that provides insertion order iteration.
ModuleAnalysisManager MAM
std::unique_ptr< MCStreamer > && Streamer
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer, char &ID=AsmPrinter::ID)
MCCodeEmitter - Generic instruction encoding interface.
Streaming machine code generation interface.
Definition MCStreamer.h:221
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Representation of each machine instruction.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Analysis providing profile information.
A class that holds the constants that represent static data and their profile information and provide...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
std::function< ProfileSummaryInfo *(Module &)> GetPSI
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
const X86Subtarget & getSubtarget() const
X86AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
std::function< const StaticDataProfileInfo *(Module &)> GetSDPI
bool shouldEmitWeakSwiftAsyncExtendedFramePointerFlags() const override
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2028
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in for passes that should not be skipped.