MachineModuleInfo.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- 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. // Collect meta information for a module. This information should be in a
  11. // neutral form that can be used by different debugging and exception handling
  12. // schemes.
  13. //
  14. // The organization of information is primarily clustered around the source
  15. // compile units. The main exception is source line correspondence where
  16. // inlining may interleave code from various compile units.
  17. //
  18. // The following information can be retrieved from the MachineModuleInfo.
  19. //
  20. // -- Source directories - Directories are uniqued based on their canonical
  21. // string and assigned a sequential numeric ID (base 1.)
  22. // -- Source files - Files are also uniqued based on their name and directory
  23. // ID. A file ID is sequential number (base 1.)
  24. // -- Source line correspondence - A vector of file ID, line#, column# triples.
  25. // A DEBUG_LOCATION instruction is generated by the DAG Legalizer
  26. // corresponding to each entry in the source line list. This allows a debug
  27. // emitter to generate labels referenced by debug information tables.
  28. //
  29. //===----------------------------------------------------------------------===//
  30. #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
  31. #define LLVM_CODEGEN_MACHINEMODULEINFO_H
  32. #include "llvm/ADT/DenseMap.h"
  33. #include "llvm/ADT/PointerIntPair.h"
  34. #include "llvm/ADT/SmallPtrSet.h"
  35. #include "llvm/ADT/SmallVector.h"
  36. #include "llvm/Analysis/LibCallSemantics.h"
  37. #include "llvm/IR/DebugLoc.h"
  38. #include "llvm/IR/Metadata.h"
  39. #include "llvm/IR/ValueHandle.h"
  40. #include "llvm/MC/MCContext.h"
  41. #include "llvm/MC/MachineLocation.h"
  42. #include "llvm/Pass.h"
  43. #include "llvm/Support/DataTypes.h"
  44. #include "llvm/Support/Dwarf.h"
  45. namespace llvm {
  46. //===----------------------------------------------------------------------===//
  47. // Forward declarations.
  48. class Constant;
  49. class GlobalVariable;
  50. class BlockAddress;
  51. class MDNode;
  52. class MMIAddrLabelMap;
  53. class MachineBasicBlock;
  54. class MachineFunction;
  55. class Module;
  56. class PointerType;
  57. class StructType;
  58. struct WinEHFuncInfo;
  59. struct SEHHandler {
  60. // Filter or finally function. Null indicates a catch-all.
  61. const Function *FilterOrFinally;
  62. // Address of block to recover at. Null for a finally handler.
  63. const BlockAddress *RecoverBA;
  64. };
  65. //===----------------------------------------------------------------------===//
  66. /// LandingPadInfo - This structure is used to retain landing pad info for
  67. /// the current function.
  68. ///
  69. struct LandingPadInfo {
  70. MachineBasicBlock *LandingPadBlock; // Landing pad block.
  71. SmallVector<MCSymbol *, 1> BeginLabels; // Labels prior to invoke.
  72. SmallVector<MCSymbol *, 1> EndLabels; // Labels after invoke.
  73. SmallVector<SEHHandler, 1> SEHHandlers; // SEH handlers active at this lpad.
  74. MCSymbol *LandingPadLabel; // Label at beginning of landing pad.
  75. const Function *Personality; // Personality function.
  76. std::vector<int> TypeIds; // List of type ids (filters negative).
  77. int WinEHState; // WinEH specific state number.
  78. explicit LandingPadInfo(MachineBasicBlock *MBB)
  79. : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr),
  80. WinEHState(-1) {}
  81. };
  82. //===----------------------------------------------------------------------===//
  83. /// MachineModuleInfoImpl - This class can be derived from and used by targets
  84. /// to hold private target-specific information for each Module. Objects of
  85. /// type are accessed/created with MMI::getInfo and destroyed when the
  86. /// MachineModuleInfo is destroyed.
  87. ///
  88. class MachineModuleInfoImpl {
  89. public:
  90. typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
  91. virtual ~MachineModuleInfoImpl();
  92. typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
  93. protected:
  94. /// Return the entries from a DenseMap in a deterministic sorted orer.
  95. /// Clears the map.
  96. static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
  97. };
  98. // //
  99. ///////////////////////////////////////////////////////////////////////////////
  100. /// MachineModuleInfo - This class contains meta information specific to a
  101. /// module. Queries can be made by different debugging and exception handling
  102. /// schemes and reformated for specific use.
  103. ///
  104. class MachineModuleInfo : public ImmutablePass {
  105. /// Context - This is the MCContext used for the entire code generator.
  106. MCContext Context;
  107. /// TheModule - This is the LLVM Module being worked on.
  108. const Module *TheModule;
  109. /// ObjFileMMI - This is the object-file-format-specific implementation of
  110. /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
  111. /// want.
  112. MachineModuleInfoImpl *ObjFileMMI;
  113. /// List of moves done by a function's prolog. Used to construct frame maps
  114. /// by debug and exception handling consumers.
  115. std::vector<MCCFIInstruction> FrameInstructions;
  116. /// LandingPads - List of LandingPadInfo describing the landing pad
  117. /// information in the current function.
  118. std::vector<LandingPadInfo> LandingPads;
  119. /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
  120. /// indexes.
  121. DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
  122. /// CallSiteMap - Map of invoke call site index values to associated begin
  123. /// EH_LABEL for the current function.
  124. DenseMap<MCSymbol*, unsigned> CallSiteMap;
  125. /// CurCallSite - The current call site index being processed, if any. 0 if
  126. /// none.
  127. unsigned CurCallSite;
  128. /// TypeInfos - List of C++ TypeInfo used in the current function.
  129. std::vector<const GlobalValue *> TypeInfos;
  130. /// FilterIds - List of typeids encoding filters used in the current function.
  131. std::vector<unsigned> FilterIds;
  132. /// FilterEnds - List of the indices in FilterIds corresponding to filter
  133. /// terminators.
  134. std::vector<unsigned> FilterEnds;
  135. /// Personalities - Vector of all personality functions ever seen. Used to
  136. /// emit common EH frames.
  137. std::vector<const Function *> Personalities;
  138. /// AddrLabelSymbols - This map keeps track of which symbol is being used for
  139. /// the specified basic block's address of label.
  140. MMIAddrLabelMap *AddrLabelSymbols;
  141. bool CallsEHReturn;
  142. bool CallsUnwindInit;
  143. /// DbgInfoAvailable - True if debugging information is available
  144. /// in this module.
  145. bool DbgInfoAvailable;
  146. /// UsesVAFloatArgument - True if this module calls VarArg function with
  147. /// floating-point arguments. This is used to emit an undefined reference
  148. /// to _fltused on Windows targets.
  149. bool UsesVAFloatArgument;
  150. /// UsesMorestackAddr - True if the module calls the __morestack function
  151. /// indirectly, as is required under the large code model on x86. This is used
  152. /// to emit a definition of a symbol, __morestack_addr, containing the
  153. /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
  154. /// details.
  155. bool UsesMorestackAddr;
  156. EHPersonality PersonalityTypeCache;
  157. DenseMap<const Function *, std::unique_ptr<WinEHFuncInfo>> FuncInfoMap;
  158. public:
  159. static char ID; // Pass identification, replacement for typeid
  160. struct VariableDbgInfo {
  161. const DILocalVariable *Var;
  162. const DIExpression *Expr;
  163. unsigned Slot;
  164. const DILocation *Loc;
  165. VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
  166. unsigned Slot, const DILocation *Loc)
  167. : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
  168. };
  169. typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
  170. VariableDbgInfoMapTy VariableDbgInfos;
  171. MachineModuleInfo(); // DUMMY CONSTRUCTOR, DO NOT CALL.
  172. // Real constructor.
  173. MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
  174. const MCObjectFileInfo *MOFI);
  175. ~MachineModuleInfo() override;
  176. // Initialization and Finalization
  177. bool doInitialization(Module &) override;
  178. bool doFinalization(Module &) override;
  179. /// EndFunction - Discard function meta information.
  180. ///
  181. void EndFunction();
  182. const MCContext &getContext() const { return Context; }
  183. MCContext &getContext() { return Context; }
  184. void setModule(const Module *M) { TheModule = M; }
  185. const Module *getModule() const { return TheModule; }
  186. const Function *getWinEHParent(const Function *F) const;
  187. WinEHFuncInfo &getWinEHFuncInfo(const Function *F);
  188. bool hasWinEHFuncInfo(const Function *F) const {
  189. return FuncInfoMap.count(getWinEHParent(F)) > 0;
  190. }
  191. /// getInfo - Keep track of various per-function pieces of information for
  192. /// backends that would like to do so.
  193. ///
  194. template<typename Ty>
  195. Ty &getObjFileInfo() {
  196. if (ObjFileMMI == nullptr)
  197. ObjFileMMI = new Ty(*this);
  198. return *static_cast<Ty*>(ObjFileMMI);
  199. }
  200. template<typename Ty>
  201. const Ty &getObjFileInfo() const {
  202. return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
  203. }
  204. /// hasDebugInfo - Returns true if valid debug info is present.
  205. ///
  206. bool hasDebugInfo() const { return DbgInfoAvailable; }
  207. void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
  208. bool callsEHReturn() const { return CallsEHReturn; }
  209. void setCallsEHReturn(bool b) { CallsEHReturn = b; }
  210. bool callsUnwindInit() const { return CallsUnwindInit; }
  211. void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
  212. bool usesVAFloatArgument() const {
  213. return UsesVAFloatArgument;
  214. }
  215. void setUsesVAFloatArgument(bool b) {
  216. UsesVAFloatArgument = b;
  217. }
  218. bool usesMorestackAddr() const {
  219. return UsesMorestackAddr;
  220. }
  221. void setUsesMorestackAddr(bool b) {
  222. UsesMorestackAddr = b;
  223. }
  224. /// \brief Returns a reference to a list of cfi instructions in the current
  225. /// function's prologue. Used to construct frame maps for debug and exception
  226. /// handling comsumers.
  227. const std::vector<MCCFIInstruction> &getFrameInstructions() const {
  228. return FrameInstructions;
  229. }
  230. unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
  231. addFrameInst(const MCCFIInstruction &Inst) {
  232. FrameInstructions.push_back(Inst);
  233. return FrameInstructions.size() - 1;
  234. }
  235. /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
  236. /// block when its address is taken. This cannot be its normal LBB label
  237. /// because the block may be accessed outside its containing function.
  238. MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) {
  239. return getAddrLabelSymbolToEmit(BB).front();
  240. }
  241. /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
  242. /// basic block when its address is taken. If other blocks were RAUW'd to
  243. /// this one, we may have to emit them as well, return the whole set.
  244. ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
  245. /// takeDeletedSymbolsForFunction - If the specified function has had any
  246. /// references to address-taken blocks generated, but the block got deleted,
  247. /// return the symbol now so we can emit it. This prevents emitting a
  248. /// reference to a symbol that has no definition.
  249. void takeDeletedSymbolsForFunction(const Function *F,
  250. std::vector<MCSymbol*> &Result);
  251. //===- EH ---------------------------------------------------------------===//
  252. /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
  253. /// specified MachineBasicBlock.
  254. LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
  255. /// addInvoke - Provide the begin and end labels of an invoke style call and
  256. /// associate it with a try landing pad block.
  257. void addInvoke(MachineBasicBlock *LandingPad,
  258. MCSymbol *BeginLabel, MCSymbol *EndLabel);
  259. /// addLandingPad - Add a new panding pad. Returns the label ID for the
  260. /// landing pad entry.
  261. MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
  262. /// addPersonality - Provide the personality function for the exception
  263. /// information.
  264. void addPersonality(MachineBasicBlock *LandingPad,
  265. const Function *Personality);
  266. void addPersonality(const Function *Personality);
  267. void addWinEHState(MachineBasicBlock *LandingPad, int State);
  268. /// getPersonalityIndex - Get index of the current personality function inside
  269. /// Personalitites array
  270. unsigned getPersonalityIndex() const;
  271. /// getPersonalities - Return array of personality functions ever seen.
  272. const std::vector<const Function *>& getPersonalities() const {
  273. return Personalities;
  274. }
  275. /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
  276. ///
  277. void addCatchTypeInfo(MachineBasicBlock *LandingPad,
  278. ArrayRef<const GlobalValue *> TyInfo);
  279. /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
  280. ///
  281. void addFilterTypeInfo(MachineBasicBlock *LandingPad,
  282. ArrayRef<const GlobalValue *> TyInfo);
  283. /// addCleanup - Add a cleanup action for a landing pad.
  284. ///
  285. void addCleanup(MachineBasicBlock *LandingPad);
  286. void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter,
  287. const BlockAddress *RecoverLabel);
  288. void addSEHCleanupHandler(MachineBasicBlock *LandingPad,
  289. const Function *Cleanup);
  290. /// getTypeIDFor - Return the type id for the specified typeinfo. This is
  291. /// function wide.
  292. unsigned getTypeIDFor(const GlobalValue *TI);
  293. /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
  294. /// function wide.
  295. int getFilterIDFor(std::vector<unsigned> &TyIds);
  296. /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
  297. /// pads.
  298. void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
  299. /// getLandingPads - Return a reference to the landing pad info for the
  300. /// current function.
  301. const std::vector<LandingPadInfo> &getLandingPads() const {
  302. return LandingPads;
  303. }
  304. /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
  305. /// site indexes.
  306. void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
  307. /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
  308. /// symbol.
  309. SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
  310. assert(hasCallSiteLandingPad(Sym) &&
  311. "missing call site number for landing pad!");
  312. return LPadToCallSiteMap[Sym];
  313. }
  314. /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
  315. /// associated call site.
  316. bool hasCallSiteLandingPad(MCSymbol *Sym) {
  317. return !LPadToCallSiteMap[Sym].empty();
  318. }
  319. /// setCallSiteBeginLabel - Map the begin label for a call site.
  320. void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
  321. CallSiteMap[BeginLabel] = Site;
  322. }
  323. /// getCallSiteBeginLabel - Get the call site number for a begin label.
  324. unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
  325. assert(hasCallSiteBeginLabel(BeginLabel) &&
  326. "Missing call site number for EH_LABEL!");
  327. return CallSiteMap[BeginLabel];
  328. }
  329. /// hasCallSiteBeginLabel - Return true if the begin label has a call site
  330. /// number associated with it.
  331. bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
  332. return CallSiteMap[BeginLabel] != 0;
  333. }
  334. /// setCurrentCallSite - Set the call site currently being processed.
  335. void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
  336. /// getCurrentCallSite - Get the call site currently being processed, if any.
  337. /// return zero if none.
  338. unsigned getCurrentCallSite() { return CurCallSite; }
  339. /// getTypeInfos - Return a reference to the C++ typeinfo for the current
  340. /// function.
  341. const std::vector<const GlobalValue *> &getTypeInfos() const {
  342. return TypeInfos;
  343. }
  344. /// getFilterIds - Return a reference to the typeids encoding filters used in
  345. /// the current function.
  346. const std::vector<unsigned> &getFilterIds() const {
  347. return FilterIds;
  348. }
  349. /// getPersonality - Return a personality function if available. The presence
  350. /// of one is required to emit exception handling info.
  351. const Function *getPersonality() const;
  352. /// Classify the personality function amongst known EH styles.
  353. EHPersonality getPersonalityType();
  354. /// setVariableDbgInfo - Collect information used to emit debugging
  355. /// information of a variable.
  356. void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
  357. unsigned Slot, const DILocation *Loc) {
  358. VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
  359. }
  360. VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
  361. }; // End class MachineModuleInfo
  362. } // End llvm namespace
  363. #endif