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