LLVM  14.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 
13 #include "llvm/CodeGen/FaultMaps.h"
14 #include "llvm/CodeGen/StackMaps.h"
15 
16 // Implemented in X86MCInstLower.cpp
17 namespace {
18  class X86MCInstLower;
19 }
20 
21 namespace llvm {
22 class MCCodeEmitter;
23 class MCStreamer;
24 class X86Subtarget;
25 class TargetMachine;
26 struct ASanAccessInfo;
27 
29  const X86Subtarget *Subtarget = nullptr;
30  StackMaps SM;
31  FaultMaps FM;
32  std::unique_ptr<MCCodeEmitter> CodeEmitter;
33  bool EmitFPOData = false;
34 
35  // This utility class tracks the length of a stackmap instruction's 'shadow'.
36  // It is used by the X86AsmPrinter to ensure that the stackmap shadow
37  // invariants (i.e. no other stackmaps, patchpoints, or control flow within
38  // the shadow) are met, while outputting a minimal number of NOPs for padding.
39  //
40  // To minimise the number of NOPs used, the shadow tracker counts the number
41  // of instruction bytes output since the last stackmap. Only if there are too
42  // few instruction bytes to cover the shadow are NOPs used for padding.
43  class StackMapShadowTracker {
44  public:
45  void startFunction(MachineFunction &MF) {
46  this->MF = &MF;
47  }
48  void count(MCInst &Inst, const MCSubtargetInfo &STI,
49  MCCodeEmitter *CodeEmitter);
50 
51  // Called to signal the start of a shadow of RequiredSize bytes.
52  void reset(unsigned RequiredSize) {
53  RequiredShadowSize = RequiredSize;
54  CurrentShadowSize = 0;
55  InShadow = true;
56  }
57 
58  // Called before every stackmap/patchpoint, and at the end of basic blocks,
59  // to emit any necessary padding-NOPs.
60  void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
61  private:
62  const MachineFunction *MF = nullptr;
63  bool InShadow = false;
64 
65  // RequiredShadowSize holds the length of the shadow specified in the most
66  // recently encountered STACKMAP instruction.
67  // CurrentShadowSize counts the number of bytes encoded since the most
68  // recently encountered STACKMAP, stopping when that number is greater than
69  // or equal to RequiredShadowSize.
70  unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
71  };
72 
73  StackMapShadowTracker SMShadowTracker;
74 
75  // All instructions emitted by the X86AsmPrinter should use this helper
76  // method.
77  //
78  // This helper function invokes the SMShadowTracker on each instruction before
79  // outputting it to the OutStream. This allows the shadow tracker to minimise
80  // the number of NOPs used for stackmap padding.
81  void EmitAndCountInstruction(MCInst &Inst);
82  void LowerSTACKMAP(const MachineInstr &MI);
83  void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
84  void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
85  void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
86  void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
87 
88  void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
89 
90  // XRay-specific lowering for X86.
91  void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
92  X86MCInstLower &MCIL);
93  void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
94  void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
95  void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
96  void LowerPATCHABLE_TYPED_EVENT_CALL(const MachineInstr &MI,
97  X86MCInstLower &MCIL);
98 
99  void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
100 
101  // Address sanitizer specific lowering for X86.
102  void LowerASAN_CHECK_MEMACCESS(const MachineInstr &MI);
103  void emitAsanMemaccessSymbols(Module &M);
104  void emitAsanMemaccessPartial(Module &M, unsigned Reg,
105  const ASanAccessInfo &AccessInfo,
106  MCSubtargetInfo &STI);
107  void emitAsanMemaccessFull(Module &M, unsigned Reg,
108  const ASanAccessInfo &AccessInfo,
109  MCSubtargetInfo &STI);
110  void emitAsanReportError(Module &M, unsigned Reg,
111  const ASanAccessInfo &AccessInfo,
112  MCSubtargetInfo &STI);
113 
114  typedef std::tuple<unsigned /*Reg*/, uint32_t /*AccessInfo*/>
115  AsanMemaccessTuple;
116  std::map<AsanMemaccessTuple, MCSymbol *> AsanMemaccessSymbols;
117 
118  // Choose between emitting .seh_ directives and .cv_fpo_ directives.
119  void EmitSEHInstruction(const MachineInstr *MI);
120 
121  void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
122  void PrintOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
123  void PrintModifiedOperand(const MachineInstr *MI, unsigned OpNo,
124  raw_ostream &O, const char *Modifier);
125  void PrintPCRelImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
126  void PrintLeaMemReference(const MachineInstr *MI, unsigned OpNo,
127  raw_ostream &O, const char *Modifier);
128  void PrintMemReference(const MachineInstr *MI, unsigned OpNo, raw_ostream &O,
129  const char *Modifier);
130  void PrintIntelMemReference(const MachineInstr *MI, unsigned OpNo,
131  raw_ostream &O, const char *Modifier);
132 
133 public:
134  X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
135 
136  StringRef getPassName() const override {
137  return "X86 Assembly Printer";
138  }
139 
140  const X86Subtarget &getSubtarget() const { return *Subtarget; }
141 
142  void emitStartOfAsmFile(Module &M) override;
143 
144  void emitEndOfAsmFile(Module &M) override;
145 
146  void emitInstruction(const MachineInstr *MI) override;
147 
148  void emitBasicBlockEnd(const MachineBasicBlock &MBB) override {
150  SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
151  }
152 
153  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
154  const char *ExtraCode, raw_ostream &O) override;
155  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
156  const char *ExtraCode, raw_ostream &O) override;
157 
158  bool doInitialization(Module &M) override {
159  SMShadowTracker.reset(0);
160  SM.reset();
161  FM.reset();
163  }
164 
165  bool runOnMachineFunction(MachineFunction &MF) override;
166  void emitFunctionBodyStart() override;
167  void emitFunctionBodyEnd() override;
168 };
169 
170 } // end namespace llvm
171 
172 #endif
AsmPrinter.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1558
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::AsmPrinter::emitBasicBlockEnd
virtual void emitBasicBlockEnd(const MachineBasicBlock &MBB)
Targets can override this to emit stuff at the end of a basic block.
Definition: AsmPrinter.cpp:3339
llvm::X86Subtarget
Definition: X86Subtarget.h:52
llvm::FaultMaps
Definition: FaultMaps.h:23
llvm::X86AsmPrinter::emitBasicBlockEnd
void emitBasicBlockEnd(const MachineBasicBlock &MBB) override
Targets can override this to emit stuff at the end of a basic block.
Definition: X86AsmPrinter.h:148
llvm::StackMaps::reset
void reset()
Definition: StackMaps.h:294
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::X86AsmPrinter::getPassName
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: X86AsmPrinter.h:136
llvm::StackMaps
Definition: StackMaps.h:251
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::X86AsmPrinter
Definition: X86AsmPrinter.h:28
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:206
llvm::FaultMaps::reset
void reset()
Definition: FaultMaps.h:39
llvm::count
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:1664
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:80
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
LLVM_LIBRARY_VISIBILITY
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library,...
Definition: Compiler.h:131
llvm::X86AsmPrinter::doInitialization
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: X86AsmPrinter.h:158
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::ASanAccessInfo
Definition: AddressSanitizer.h:155
llvm::MCCodeEmitter
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
StackMaps.h
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::X86AsmPrinter::getSubtarget
const X86Subtarget & getSubtarget() const
Definition: X86AsmPrinter.h:140
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
FaultMaps.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::AsmPrinter::doInitialization
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:266