LLVM  3.7.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 
33  // This utility class tracks the length of a stackmap instruction's 'shadow'.
34  // It is used by the X86AsmPrinter to ensure that the stackmap shadow
35  // invariants (i.e. no other stackmaps, patchpoints, or control flow within
36  // the shadow) are met, while outputting a minimal number of NOPs for padding.
37  //
38  // To minimise the number of NOPs used, the shadow tracker counts the number
39  // of instruction bytes output since the last stackmap. Only if there are too
40  // few instruction bytes to cover the shadow are NOPs used for padding.
41  class StackMapShadowTracker {
42  public:
43  StackMapShadowTracker(TargetMachine &TM);
44  ~StackMapShadowTracker();
45  void startFunction(MachineFunction &MF);
46  void count(MCInst &Inst, const MCSubtargetInfo &STI);
47 
48  // Called to signal the start of a shadow of RequiredSize bytes.
49  void reset(unsigned RequiredSize) {
50  RequiredShadowSize = RequiredSize;
51  CurrentShadowSize = 0;
52  InShadow = true;
53  }
54 
55  // Called before every stackmap/patchpoint, and at the end of basic blocks,
56  // to emit any necessary padding-NOPs.
57  void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
58  private:
60  const MachineFunction *MF;
61  std::unique_ptr<MCCodeEmitter> CodeEmitter;
62  bool InShadow;
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, CurrentShadowSize;
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 
82  void InsertStackMapShadows(MachineFunction &MF);
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_LOAD_OP(const MachineInstr &MI, X86MCInstLower &MCIL);
87 
88  void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
89 
90  public:
92  std::unique_ptr<MCStreamer> Streamer)
93  : AsmPrinter(TM, std::move(Streamer)), SM(*this), FM(*this),
94  SMShadowTracker(TM) {}
95 
96  const char *getPassName() const override {
97  return "X86 Assembly / Object Emitter";
98  }
99 
100  const X86Subtarget &getSubtarget() const { return *Subtarget; }
101 
102  void EmitStartOfAsmFile(Module &M) override;
103 
104  void EmitEndOfAsmFile(Module &M) override;
105 
106  void EmitInstruction(const MachineInstr *MI) override;
107 
108  void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
109  SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo());
110  }
111 
112  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
113  unsigned AsmVariant, const char *ExtraCode,
114  raw_ostream &OS) override;
115  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
116  unsigned AsmVariant, const char *ExtraCode,
117  raw_ostream &OS) override;
118 
119  /// \brief Return the symbol for the specified constant pool entry.
120  MCSymbol *GetCPISymbol(unsigned CPID) const override;
121 
122  bool doInitialization(Module &M) override {
123  SMShadowTracker.reset(0);
124  SM.reset();
126  }
127 
128  bool runOnMachineFunction(MachineFunction &F) override;
129 };
130 
131 } // end namespace llvm
132 
133 #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:114
F(f)
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
const char * getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: X86AsmPrinter.h:96
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
Streaming machine code generation interface.
Definition: MCStreamer.h:157
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:110
X86AsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
Definition: X86AsmPrinter.h:91
const X86Subtarget & getSubtarget() const
Representation of each machine instruction.
Definition: MachineInstr.h:51
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:172
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:38
Primary interface to the complete machine description for the target machine.