[X86] Optimize stackmap shadows on X86.
[oota-llvm.git] / lib / Target / X86 / X86AsmPrinter.h
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 X86ASMPRINTER_H
11 #define X86ASMPRINTER_H
12
13 #include "X86Subtarget.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/StackMaps.h"
16 #include "llvm/Target/TargetMachine.h"
17
18 // Implemented in X86MCInstLower.cpp
19 namespace {
20   class X86MCInstLower;
21 }
22
23 namespace llvm {
24 class MCStreamer;
25 class MCSymbol;
26
27 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
28   const X86Subtarget *Subtarget;
29   StackMaps SM;
30
31   void GenerateExportDirective(const MCSymbol *Sym, bool IsData);
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     void reset(unsigned RequiredSize) {
48       RequiredShadowSize = RequiredSize;
49       CurrentShadowSize = 0;
50       Count = true;
51     }
52     void emitShadowPadding(MCStreamer &OutStreamer, const MCSubtargetInfo &STI);
53   private:
54     TargetMachine &TM;
55     std::unique_ptr<MCCodeEmitter> CodeEmitter;
56     bool Count;
57     unsigned RequiredShadowSize, CurrentShadowSize;
58   };
59
60   StackMapShadowTracker SMShadowTracker;
61
62   // All instructions emitted by the X86AsmPrinter should use this helper
63   // method.
64   //
65   // This helper function invokes the SMShadowTracker on each instruction before
66   // outputting it to the OutStream. This allows the shadow tracker to minimise
67   // the number of NOPs used for stackmap padding.
68   void EmitAndCountInstruction(MCInst &Inst);
69
70   void InsertStackMapShadows(MachineFunction &MF);
71   void LowerSTACKMAP(const MachineInstr &MI);
72   void LowerPATCHPOINT(const MachineInstr &MI);
73
74   void LowerTlsAddr(X86MCInstLower &MCInstLowering, const MachineInstr &MI);
75
76  public:
77   explicit X86AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
78     : AsmPrinter(TM, Streamer), SM(*this), SMShadowTracker(TM) {
79     Subtarget = &TM.getSubtarget<X86Subtarget>();
80   }
81
82   const char *getPassName() const override {
83     return "X86 Assembly / Object Emitter";
84   }
85
86   const X86Subtarget &getSubtarget() const { return *Subtarget; }
87
88   void EmitStartOfAsmFile(Module &M) override;
89
90   void EmitEndOfAsmFile(Module &M) override;
91
92   void EmitInstruction(const MachineInstr *MI) override;
93
94   void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override {
95     SMShadowTracker.emitShadowPadding(OutStreamer, getSubtargetInfo());
96   }
97
98   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
99                        unsigned AsmVariant, const char *ExtraCode,
100                        raw_ostream &OS) override;
101   bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
102                              unsigned AsmVariant, const char *ExtraCode,
103                              raw_ostream &OS) override;
104
105   /// \brief Return the symbol for the specified constant pool entry.
106   MCSymbol *GetCPISymbol(unsigned CPID) const override;
107
108   bool runOnMachineFunction(MachineFunction &F) override;
109 };
110
111 } // end namespace llvm
112
113 #endif