LLVM  6.0.0svn
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"
17 #include "llvm/MC/MCCodeEmitter.h"
19 
20 // Implemented in X86MCInstLower.cpp
21 namespace {
22  class X86MCInstLower;
23 }
24 
25 namespace llvm {
26 class MCStreamer;
27 class MCSymbol;
28 
30  const X86Subtarget *Subtarget;
31  StackMaps SM;
32  FaultMaps FM;
33  std::unique_ptr<MCCodeEmitter> CodeEmitter;
34  bool EmitFPOData = false;
35 
36  // This utility class tracks the length of a stackmap instruction's 'shadow'.
37  // It is used by the X86AsmPrinter to ensure that the stackmap shadow
38  // invariants (i.e. no other stackmaps, patchpoints, or control flow within
39  // the shadow) are met, while outputting a minimal number of NOPs for padding.
40  //
41  // To minimise the number of NOPs used, the shadow tracker counts the number
42  // of instruction bytes output since the last stackmap. Only if there are too
43  // few instruction bytes to cover the shadow are NOPs used for padding.
44  class StackMapShadowTracker {
45  public:
46  void startFunction(MachineFunction &MF) {
47  this->MF = &MF;
48  }
49  void count(MCInst &Inst, const MCSubtargetInfo &STI,
50  MCCodeEmitter *CodeEmitter);
51 
52  // Called to signal the start of a shadow of RequiredSize bytes.
53  void reset(unsigned RequiredSize) {
54  RequiredShadowSize = RequiredSize;
55  CurrentShadowSize = 0;
56  InShadow = true;
57  }
58 
59  // Called before every stackmap/patchpoint, and at the end of basic blocks,
60  // to emit any necessary padding-NOPs.
61  void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
62  private:
63  const MachineFunction *MF;
64  bool InShadow = false;
65 
66  // RequiredShadowSize holds the length of the shadow specified in the most
67  // recently encountered STACKMAP instruction.
68  // CurrentShadowSize counts the number of bytes encoded since the most
69  // recently encountered STACKMAP, stopping when that number is greater than
70  // or equal to RequiredShadowSize.
71  unsigned RequiredShadowSize = 0, CurrentShadowSize = 0;
72  };
73 
74  StackMapShadowTracker SMShadowTracker;
75 
76  // All instructions emitted by the X86AsmPrinter should use this helper
77  // method.
78  //
79  // This helper function invokes the SMShadowTracker on each instruction before
80  // outputting it to the OutStream. This allows the shadow tracker to minimise
81  // the number of NOPs used for stackmap padding.
82  void EmitAndCountInstruction(MCInst &Inst);
83  void LowerSTACKMAP(const MachineInstr &MI);
84  void LowerPATCHPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
85  void LowerSTATEPOINT(const MachineInstr &MI, X86MCInstLower &MCIL);
86  void LowerFAULTING_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
87  void LowerPATCHABLE_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
88 
89  void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
90 
91  // XRay-specific lowering for X86.
92  void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
93  X86MCInstLower &MCIL);
94  void LowerPATCHABLE_RET(const MachineInstr &MI, X86MCInstLower &MCIL);
95  void LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
96  void LowerPATCHABLE_EVENT_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
97 
98  void LowerFENTRY_CALL(const MachineInstr &MI, X86MCInstLower &MCIL);
99 
100  // Helper function that emits the XRay sleds we've collected for a particular
101  // function.
102  void EmitXRayTable();
103 
104  // Choose between emitting .seh_ directives and .cv_fpo_ directives.
105  void EmitSEHInstruction(const MachineInstr *MI);
106 
107 public:
108  X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
109 
110  StringRef getPassName() const override {
111  return "X86 Assembly Printer";
112  }
113 
114  const X86Subtarget &getSubtarget() const { return *Subtarget; }
115 
116  void EmitStartOfAsmFile(Module &M) override;
117 
118  void EmitEndOfAsmFile(Module &M) override;
119 
120  void EmitInstruction(const MachineInstr *MI) override;
121 
122  void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
123  AsmPrinter::EmitBasicBlockEnd(MBB);
124  SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
125  }
126 
127  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
128  unsigned AsmVariant, const char *ExtraCode,
129  raw_ostream &OS) override;
130  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
131  unsigned AsmVariant, const char *ExtraCode,
132  raw_ostream &OS) override;
133 
134  /// \brief Return the symbol for the specified constant pool entry.
135  MCSymbol *GetCPISymbol(unsigned CPID) const override;
136 
137  bool doInitialization(Module &M) override {
138  SMShadowTracker.reset(0);
139  SM.reset();
140  FM.reset();
141  return AsmPrinter::doInitialization(M);
142  }
143 
144  bool runOnMachineFunction(MachineFunction &F) override;
145  void EmitFunctionBodyStart() override;
146  void EmitFunctionBodyEnd() override;
147 };
148 
149 } // end namespace llvm
150 
151 #endif
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
F(f)
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
void reset()
Definition: FaultMaps.h:42
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_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:874
Streaming machine code generation interface.
Definition: MCStreamer.h:169
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:105
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Representation of each machine instruction.
Definition: MachineInstr.h:59
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.
Definition: TargetMachine.h:57
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const X86Subtarget & getSubtarget() const