LLVM 23.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
15
16// Implemented in X86MCInstLower.cpp
17namespace {
18 class X86MCInstLower;
19}
20
21namespace llvm {
22class MCCodeEmitter;
23class MCStreamer;
24class X86Subtarget;
25class TargetMachine;
26
28public:
29 static char ID;
30
31private:
32 const X86Subtarget *Subtarget = nullptr;
33 FaultMaps FM;
34 std::unique_ptr<MCCodeEmitter> CodeEmitter;
35 bool EmitFPOData = false;
36 bool ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags = false;
37 bool IndCSPrefix = false;
38 bool EnableImportCallOptimization = false;
39 bool SplitChainedAtEndOfBlock = false;
40
41 enum ImportCallKind : unsigned {
42 IMAGE_RETPOLINE_AMD64_IMPORT_BR = 0x02,
43 IMAGE_RETPOLINE_AMD64_IMPORT_CALL = 0x03,
44 IMAGE_RETPOLINE_AMD64_INDIR_BR = 0x04,
45 IMAGE_RETPOLINE_AMD64_INDIR_CALL = 0x05,
46 IMAGE_RETPOLINE_AMD64_INDIR_BR_REX = 0x06,
47 IMAGE_RETPOLINE_AMD64_CFG_BR = 0x08,
48 IMAGE_RETPOLINE_AMD64_CFG_CALL = 0x09,
49 IMAGE_RETPOLINE_AMD64_CFG_BR_REX = 0x0A,
50 IMAGE_RETPOLINE_AMD64_SWITCHTABLE_FIRST = 0x010,
51 IMAGE_RETPOLINE_AMD64_SWITCHTABLE_LAST = 0x01F,
52 };
53 struct ImportCallInfo {
54 MCSymbol *CalleeSymbol;
55 ImportCallKind Kind;
56 };
57 DenseMap<MCSection *, std::vector<ImportCallInfo>>
58 SectionToImportedFunctionCalls;
59
60 // This utility class tracks the length of a stackmap instruction's 'shadow'.
61 // It is used by the X86AsmPrinter to ensure that the stackmap shadow
62 // invariants (i.e. no other stackmaps, patchpoints, or control flow within
63 // the shadow) are met, while outputting a minimal number of NOPs for padding.
64 //
65 // To minimise the number of NOPs used, the shadow tracker counts the number
66 // of instruction bytes output since the last stackmap. Only if there are too
67 // few instruction bytes to cover the shadow are NOPs used for padding.
68 class StackMapShadowTracker {
69 public:
70 void startFunction(MachineFunction &MF) {
71 this->MF = &MF;
72 }
73 void count(const MCInst &Inst, const MCSubtargetInfo &STI,
74 MCCodeEmitter *CodeEmitter);
75
76 // Called to signal the start of a shadow of RequiredSize bytes.
77 void reset(unsigned RequiredSize) {
78 RequiredShadowSize = RequiredSize;
79 CurrentShadowSize = 0;
80 InShadow = true;
81 }
82
83 // Called before every stackmap/patchpoint, and at the end of basic blocks,
84 // to emit any necessary padding-NOPs.
85 void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
86 private:
87 const MachineFunction *MF = nullptr;
88 bool InShadow = false;
89
90 // RequiredShadowSize holds the length of the shadow specified in the most
91 // recently encountered STACKMAP instruction.
92 // CurrentShadowSize counts the number of bytes encoded since the most
93 // recently encountered STACKMAP, stopping when that number is greater than
94 // or equal to RequiredShadowSize.
95 unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
96 };
97
98 StackMapShadowTracker SMShadowTracker;
99
100 // All instructions emitted by the X86AsmPrinter should use this helper
101 // method.
102 //
103 // This helper function invokes the SMShadowTracker on each instruction before
104 // outputting it to the OutStream. This allows the shadow tracker to minimise
105 // the number of NOPs used for stackmap padding.
106 void EmitAndCountInstruction(MCInst &Inst);
107 void LowerSTACKMAP(const MachineInstr &MI);
108 void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
109 void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
110 void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
111 void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
112
113 void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
114
115 // XRay-specific lowering for X86.
116 void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
117 X86MCInstLower &MCIL);
118 void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
119 void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
120 void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
121 void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI,
122 X86MCInstLower &MCIL);
123
124 void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
125
126 // KCFI specific lowering for X86.
127 uint32_t MaskKCFIType(uint32_t Value);
128 void EmitKCFITypePadding(const MachineFunction &MF, bool HasType = true);
129 void LowerKCFI_CHECK(const MachineInstr &MI);
130
131 // Address sanitizer specific lowering for X86.
132 void LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI);
133
134 // Choose between emitting .seh_ directives and .cv_fpo_ directives.
135 void EmitSEHInstruction(const MachineInstr *MI);
136
137 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
138 void PrintOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
139 void PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo,
140 raw_ostream &O, StringRef Modifier = {});
141 void PrintPCRelImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
142 void PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo,
143 raw_ostream &O, StringRef Modifier = {});
144 void PrintMemReference(const MachineInstr *MI, unsigned OpNo, raw_ostream &O,
145 StringRef Modifier = {});
146 void PrintIntelMemReference(const MachineInstr *MI, unsigned OpNo,
147 raw_ostream &O, StringRef Modifier = {});
148 const MCSubtargetInfo *getIFuncMCSubtargetInfo() const override;
149 void emitMachOIFuncStubBody(Module &M, const GlobalIFunc &GI,
150 MCSymbol *LazyPointer) override;
151 void emitMachOIFuncStubHelperBody(Module &M, const GlobalIFunc &GI,
152 MCSymbol *LazyPointer) override;
153
154 void emitCallInstruction(const llvm::MCInst &MCI);
155 void maybeEmitNopAfterCallForWindowsEH(const MachineInstr *MI);
156
157 // Emits a label to mark the next instruction as being relevant to Import Call
158 // Optimization.
159 void emitLabelAndRecordForImportCallOptimization(ImportCallKind Kind);
160
161public:
162 X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
163
164 StringRef getPassName() const override {
165 return "X86 Assembly Printer";
166 }
167
168 const X86Subtarget &getSubtarget() const { return *Subtarget; }
169
170 void emitStartOfAsmFile(Module &M) override;
171
172 void emitEndOfAsmFile(Module &M) override;
173
174 void emitInstruction(const MachineInstr *MI) override;
175
176 void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
177 const MCSubtargetInfo *EndInfo,
178 const MachineInstr *MI) override;
179
180 void emitBasicBlockEnd(const MachineBasicBlock &MBB) override;
181
182 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
183 const char *ExtraCode, raw_ostream &O) override;
184 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
185 const char *ExtraCode, raw_ostream &O) override;
186
187 bool doInitialization(Module &M) override {
188 SMShadowTracker.reset(0);
189 SM.reset();
190 FM.reset();
192 }
193
194 bool runOnMachineFunction(MachineFunction &MF) override;
195 void emitFunctionBodyStart() override;
196 void emitFunctionBodyEnd() override;
197 void emitKCFITypeId(const MachineFunction &MF) override;
198
200 return ShouldEmitWeakSwiftAsyncExtendedFramePointerFlags;
201 }
202};
203
204} // end namespace llvm
205
206#endif
MachineBasicBlock & MBB
#define LLVM_LIBRARY_VISIBILITY
Definition Compiler.h:137
IRTranslator LLVM IR MI
Machine Check Debug Module
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:220
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:67
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Primary interface to the complete machine description for the target machine.
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)
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.
Definition Types.h:26
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:2002