X86AsmPrinter.h revision 327952
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" 14#include "llvm/CodeGen/AsmPrinter.h" 15#include "llvm/CodeGen/FaultMaps.h" 16#include "llvm/CodeGen/StackMaps.h" 17#include "llvm/MC/MCCodeEmitter.h" 18#include "llvm/Target/TargetMachine.h" 19 20// Implemented in X86MCInstLower.cpp 21namespace { 22 class X86MCInstLower; 23} 24 25namespace llvm { 26class MCStreamer; 27class MCSymbol; 28 29class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter { 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 // Choose between emitting .seh_ directives and .cv_fpo_ directives. 101 void EmitSEHInstruction(const MachineInstr *MI); 102 103public: 104 X86AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer); 105 106 StringRef getPassName() const override { 107 return "X86 Assembly Printer"; 108 } 109 110 const X86Subtarget &getSubtarget() const { return *Subtarget; } 111 112 void EmitStartOfAsmFile(Module &M) override; 113 114 void EmitEndOfAsmFile(Module &M) override; 115 116 void EmitInstruction(const MachineInstr *MI) override; 117 118 void EmitBasicBlockEnd(const MachineBasicBlock &MBB) override { 119 AsmPrinter::EmitBasicBlockEnd(MBB); 120 SMShadowTracker.emitShadowPadding(*OutStreamer, getSubtargetInfo()); 121 } 122 123 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 124 unsigned AsmVariant, const char *ExtraCode, 125 raw_ostream &OS) override; 126 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 127 unsigned AsmVariant, const char *ExtraCode, 128 raw_ostream &OS) override; 129 130 /// \brief Return the symbol for the specified constant pool entry. 131 MCSymbol *GetCPISymbol(unsigned CPID) const override; 132 133 bool doInitialization(Module &M) override { 134 SMShadowTracker.reset(0); 135 SM.reset(); 136 FM.reset(); 137 return AsmPrinter::doInitialization(M); 138 } 139 140 bool runOnMachineFunction(MachineFunction &F) override; 141 void EmitFunctionBodyStart() override; 142 void EmitFunctionBodyEnd() override; 143}; 144 145} // end namespace llvm 146 147#endif 148