LLVM  4.0.0
X86AsmPrinter.h
Go to the documentation of this file.
1 //===-- X86AsmPrinter.h - X86 implementation of AsmPrinter ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
11 #define LLVM_LIB_TARGET_X86_X86ASMPRINTER_H
12 
13 #include "X86Subtarget.h"
15 #include "llvm/CodeGen/FaultMaps.h"
16 #include "llvm/CodeGen/StackMaps.h"
18 
19 // Implemented in X86MCInstLower.cpp
20 namespace {
21  class X86MCInstLower;
22 }
23 
24 namespace llvm {
25 class MCStreamer;
26 class MCSymbol;
27 
29  const X86Subtarget *Subtarget;
30  StackMaps SM;
31  FaultMaps FM;
32  std::unique_ptr<MCCodeEmitter> CodeEmitter;
33 
34  // This utility class tracks the length of a stackmap instruction's 'shadow'.
35  // It is used by the X86AsmPrinter to ensure that the stackmap shadow
36  // invariants (i.e. no other stackmaps, patchpoints, or control flow within
37  // the shadow) are met, while outputting a minimal number of NOPs for padding.
38  //
39  // To minimise the number of NOPs used, the shadow tracker counts the number
40  // of instruction bytes output since the last stackmap. Only if there are too
41  // few instruction bytes to cover the shadow are NOPs used for padding.
42  class StackMapShadowTracker {
43  public:
44  void startFunction(MachineFunction &MF) {
45  this->MF = &MF;
46  }
47  void count(MCInst &Inst, const MCSubtargetInfo &STI,
48  MCCodeEmitter *CodeEmitter);
49 
50  // Called to signal the start of a shadow of RequiredSize bytes.
51  void reset(unsigned RequiredSize) {
52  RequiredShadowSize = RequiredSize;
53  CurrentShadowSize = 0;
54  InShadow = true;
55  }
56 
57  // Called before every stackmap/patchpoint, and at the end of basic blocks,
58  // to emit any necessary padding-NOPs.
59  void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
60  private:
61  const MachineFunction *MF;
62  bool InShadow = false;
63 
64  // RequiredShadowSize holds the length of the shadow specified in the most
65  // recently encountered STACKMAP instruction.
66  // CurrentShadowSize counts the number of bytes encoded since the most
67  // recently encountered STACKMAP, stopping when that number is greater than
68  // or equal to RequiredShadowSize.
69  unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
70  };
71 
72  StackMapShadowTracker SMShadowTracker;
73 
74  // All instructions emitted by the X86AsmPrinter should use this helper
75  // method.
76  //
77  // This helper function invokes the SMShadowTracker on each instruction before
78  // outputting it to the OutStream. This allows the shadow tracker to minimise
79  // the number of NOPs used for stackmap padding.
80  void EmitAndCountInstruction(MCInst &Inst);
81  void LowerSTACKMAP(const MachineInstr &MI);
82  void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
83  void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
84  void LowerFAULTING_LOAD_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
85  void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
86 
87  void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
88 
89  // XRay-specific lowering for X86.
90  void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
91  X86MCInstLower &MCIL);
92  void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
93  void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
94 
95  // Helper function that emits the XRay sleds we've collected for a particular
96  // function.
97  void EmitXRayTable();
98 
99 public:
101  std::unique_ptr<MCStreamer> Streamer)
102  : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this) {}
103 
104  StringRef getPassName() const override {
105  return "X86 Assembly Printer";
106  }
107 
108  const X86Subtarget &getSubtarget() const { return *Subtarget; }
109 
110  void EmitStartOfAsmFile(Module &M) override;
111 
112  void EmitEndOfAsmFile(Module &M) override;
113 
114  void EmitInstruction(const MachineInstr *MI) override;
115 
116  void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
117  SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
118  }
119 
120  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
121  unsigned AsmVariant, const char *ExtraCode,
122  raw_ostream &OS) override;
123  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
124  unsigned AsmVariant, const char *ExtraCode,
125  raw_ostream &OS) override;
126 
127  /// \brief Return the symbol for the specified constant pool entry.
128  MCSymbol *GetCPISymbol(unsigned CPID) const override;
129 
130  bool doInitialization(Module &M) override {
131  SMShadowTracker.reset(0);
132  SM.reset();
134  }
135 
136  bool runOnMachineFunction(MachineFunction &F) override;
137 };
138 
139 } // end namespace llvm
140 
141 #endif
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
#define F(x, y, z)
Definition: MD5.cpp:51
MachineBasicBlock * MBB
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:791
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
Streaming machine code generation interface.
Definition: MCStreamer.h:161
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:67
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:105
X86AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
const X86Subtarget & getSubtarget() const
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Representation of each machine instruction.
Definition: MachineInstr.h:52
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:179
void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override
Targets can override this to emit stuff at the end of a basic block.
MCSubtargetInfo - Generic base class for all target subtargets.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Primary interface to the complete machine description for the target machine.
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47