1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
18 // The following information can be retrieved from the MachineModuleInfo.
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.
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/PointerIntPair.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/IR/DebugLoc.h"
39 #include "llvm/IR/Metadata.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/MC/MCContext.h"
42 #include "llvm/MC/MachineLocation.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/DataTypes.h"
45 #include "llvm/Support/Dwarf.h"
49 /// Different personality functions used by a function.
50 enum class EHPersonality {
51 None, /// No exception handling
52 Itanium, /// An Itanium C++ EH personality like __gxx_personality_seh0
53 Win64SEH, /// x86_64 SEH, uses __C_specific_handler
56 //===----------------------------------------------------------------------===//
57 // Forward declarations.
61 class MMIAddrLabelMap;
62 class MachineBasicBlock;
63 class MachineFunction;
68 //===----------------------------------------------------------------------===//
69 /// LandingPadInfo - This structure is used to retain landing pad info for
70 /// the current function.
72 struct LandingPadInfo {
73 MachineBasicBlock *LandingPadBlock; // Landing pad block.
74 SmallVector<MCSymbol*, 1> BeginLabels; // Labels prior to invoke.
75 SmallVector<MCSymbol*, 1> EndLabels; // Labels after invoke.
76 SmallVector<MCSymbol*, 1> ClauseLabels; // Labels for each clause.
77 MCSymbol *LandingPadLabel; // Label at beginning of landing pad.
78 const Function *Personality; // Personality function.
79 std::vector<int> TypeIds; // List of type ids (filters negative)
81 explicit LandingPadInfo(MachineBasicBlock *MBB)
82 : LandingPadBlock(MBB), LandingPadLabel(nullptr), Personality(nullptr) {}
85 //===----------------------------------------------------------------------===//
86 /// MachineModuleInfoImpl - This class can be derived from and used by targets
87 /// to hold private target-specific information for each Module. Objects of
88 /// type are accessed/created with MMI::getInfo and destroyed when the
89 /// MachineModuleInfo is destroyed.
91 class MachineModuleInfoImpl {
93 typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy;
94 virtual ~MachineModuleInfoImpl();
95 typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy;
97 static SymbolListTy GetSortedStubs(const DenseMap<MCSymbol*, StubValueTy>&);
100 //===----------------------------------------------------------------------===//
101 /// MachineModuleInfo - This class contains meta information specific to a
102 /// module. Queries can be made by different debugging and exception handling
103 /// schemes and reformated for specific use.
105 class MachineModuleInfo : public ImmutablePass {
106 /// Context - This is the MCContext used for the entire code generator.
109 /// TheModule - This is the LLVM Module being worked on.
110 const Module *TheModule;
112 /// ObjFileMMI - This is the object-file-format-specific implementation of
113 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
115 MachineModuleInfoImpl *ObjFileMMI;
117 /// List of moves done by a function's prolog. Used to construct frame maps
118 /// by debug and exception handling consumers.
119 std::vector<MCCFIInstruction> FrameInstructions;
121 /// LandingPads - List of LandingPadInfo describing the landing pad
122 /// information in the current function.
123 std::vector<LandingPadInfo> LandingPads;
125 /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site
127 DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
129 /// CallSiteMap - Map of invoke call site index values to associated begin
130 /// EH_LABEL for the current function.
131 DenseMap<MCSymbol*, unsigned> CallSiteMap;
133 /// CurCallSite - The current call site index being processed, if any. 0 if
135 unsigned CurCallSite;
137 /// TypeInfos - List of C++ TypeInfo used in the current function.
138 std::vector<const GlobalValue *> TypeInfos;
140 /// FilterIds - List of typeids encoding filters used in the current function.
141 std::vector<unsigned> FilterIds;
143 /// FilterEnds - List of the indices in FilterIds corresponding to filter
145 std::vector<unsigned> FilterEnds;
147 /// Personalities - Vector of all personality functions ever seen. Used to
148 /// emit common EH frames.
149 std::vector<const Function *> Personalities;
151 /// UsedFunctions - The functions in the @llvm.used list in a more easily
152 /// searchable format. This does not include the functions in
153 /// llvm.compiler.used.
154 SmallPtrSet<const Function *, 32> UsedFunctions;
156 /// AddrLabelSymbols - This map keeps track of which symbol is being used for
157 /// the specified basic block's address of label.
158 MMIAddrLabelMap *AddrLabelSymbols;
161 bool CallsUnwindInit;
163 /// DbgInfoAvailable - True if debugging information is available
165 bool DbgInfoAvailable;
167 /// UsesVAFloatArgument - True if this module calls VarArg function with
168 /// floating-point arguments. This is used to emit an undefined reference
169 /// to _fltused on Windows targets.
170 bool UsesVAFloatArgument;
172 /// UsesMorestackAddr - True if the module calls the __morestack function
173 /// indirectly, as is required under the large code model on x86. This is used
174 /// to emit a definition of a symbol, __morestack_addr, containing the
175 /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more
177 bool UsesMorestackAddr;
179 EHPersonality PersonalityTypeCache;
181 EHPersonality getPersonalityTypeSlow();
184 static char ID; // Pass identification, replacement for typeid
186 struct VariableDbgInfo {
187 TrackingMDNodeRef Var;
188 TrackingMDNodeRef Expr;
192 VariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot, DebugLoc Loc)
193 : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
195 typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
196 VariableDbgInfoMapTy VariableDbgInfos;
198 MachineModuleInfo(); // DUMMY CONSTRUCTOR, DO NOT CALL.
200 MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
201 const MCObjectFileInfo *MOFI);
202 ~MachineModuleInfo();
204 // Initialization and Finalization
205 bool doInitialization(Module &) override;
206 bool doFinalization(Module &) override;
208 /// EndFunction - Discard function meta information.
212 const MCContext &getContext() const { return Context; }
213 MCContext &getContext() { return Context; }
215 void setModule(const Module *M) { TheModule = M; }
216 const Module *getModule() const { return TheModule; }
218 /// getInfo - Keep track of various per-function pieces of information for
219 /// backends that would like to do so.
221 template<typename Ty>
222 Ty &getObjFileInfo() {
223 if (ObjFileMMI == nullptr)
224 ObjFileMMI = new Ty(*this);
225 return *static_cast<Ty*>(ObjFileMMI);
228 template<typename Ty>
229 const Ty &getObjFileInfo() const {
230 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
233 /// AnalyzeModule - Scan the module for global debug information.
235 void AnalyzeModule(const Module &M);
237 /// hasDebugInfo - Returns true if valid debug info is present.
239 bool hasDebugInfo() const { return DbgInfoAvailable; }
240 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; }
242 bool callsEHReturn() const { return CallsEHReturn; }
243 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
245 bool callsUnwindInit() const { return CallsUnwindInit; }
246 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
248 bool usesVAFloatArgument() const {
249 return UsesVAFloatArgument;
252 void setUsesVAFloatArgument(bool b) {
253 UsesVAFloatArgument = b;
256 bool usesMorestackAddr() const {
257 return UsesMorestackAddr;
260 void setUsesMorestackAddr(bool b) {
261 UsesMorestackAddr = b;
264 /// \brief Returns a reference to a list of cfi instructions in the current
265 /// function's prologue. Used to construct frame maps for debug and exception
266 /// handling comsumers.
267 const std::vector<MCCFIInstruction> &getFrameInstructions() const {
268 return FrameInstructions;
271 unsigned LLVM_ATTRIBUTE_UNUSED_RESULT
272 addFrameInst(const MCCFIInstruction &Inst) {
273 FrameInstructions.push_back(Inst);
274 return FrameInstructions.size() - 1;
277 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
278 /// block when its address is taken. This cannot be its normal LBB label
279 /// because the block may be accessed outside its containing function.
280 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
282 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
283 /// basic block when its address is taken. If other blocks were RAUW'd to
284 /// this one, we may have to emit them as well, return the whole set.
285 std::vector<MCSymbol*> getAddrLabelSymbolToEmit(const BasicBlock *BB);
287 /// takeDeletedSymbolsForFunction - If the specified function has had any
288 /// references to address-taken blocks generated, but the block got deleted,
289 /// return the symbol now so we can emit it. This prevents emitting a
290 /// reference to a symbol that has no definition.
291 void takeDeletedSymbolsForFunction(const Function *F,
292 std::vector<MCSymbol*> &Result);
295 //===- EH ---------------------------------------------------------------===//
297 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
298 /// specified MachineBasicBlock.
299 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
301 /// addInvoke - Provide the begin and end labels of an invoke style call and
302 /// associate it with a try landing pad block.
303 void addInvoke(MachineBasicBlock *LandingPad,
304 MCSymbol *BeginLabel, MCSymbol *EndLabel);
306 /// addLandingPad - Add a new panding pad. Returns the label ID for the
307 /// landing pad entry.
308 MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
310 /// addPersonality - Provide the personality function for the exception
312 void addPersonality(MachineBasicBlock *LandingPad,
313 const Function *Personality);
315 /// getPersonalityIndex - Get index of the current personality function inside
316 /// Personalitites array
317 unsigned getPersonalityIndex() const;
319 /// getPersonalities - Return array of personality functions ever seen.
320 const std::vector<const Function *>& getPersonalities() const {
321 return Personalities;
324 /// isUsedFunction - Return true if the functions in the llvm.used list. This
325 /// does not return true for things in llvm.compiler.used unless they are also
327 bool isUsedFunction(const Function *F) const {
328 return UsedFunctions.count(F);
331 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
333 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
334 ArrayRef<const GlobalValue *> TyInfo);
336 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
338 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
339 ArrayRef<const GlobalValue *> TyInfo);
341 /// addCleanup - Add a cleanup action for a landing pad.
343 void addCleanup(MachineBasicBlock *LandingPad);
345 /// Add a clause for a landing pad. Returns a new label for the clause. This
346 /// is used by EH schemes that have more than one landing pad. In this case,
347 /// each clause gets its own basic block.
348 MCSymbol *addClauseForLandingPad(MachineBasicBlock *LandingPad);
350 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
352 unsigned getTypeIDFor(const GlobalValue *TI);
354 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
356 int getFilterIDFor(std::vector<unsigned> &TyIds);
358 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
360 void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
362 /// getLandingPads - Return a reference to the landing pad info for the
363 /// current function.
364 const std::vector<LandingPadInfo> &getLandingPads() const {
368 /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call
370 void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
372 /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH
374 SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
375 assert(hasCallSiteLandingPad(Sym) &&
376 "missing call site number for landing pad!");
377 return LPadToCallSiteMap[Sym];
380 /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an
381 /// associated call site.
382 bool hasCallSiteLandingPad(MCSymbol *Sym) {
383 return !LPadToCallSiteMap[Sym].empty();
386 /// setCallSiteBeginLabel - Map the begin label for a call site.
387 void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
388 CallSiteMap[BeginLabel] = Site;
391 /// getCallSiteBeginLabel - Get the call site number for a begin label.
392 unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) {
393 assert(hasCallSiteBeginLabel(BeginLabel) &&
394 "Missing call site number for EH_LABEL!");
395 return CallSiteMap[BeginLabel];
398 /// hasCallSiteBeginLabel - Return true if the begin label has a call site
399 /// number associated with it.
400 bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) {
401 return CallSiteMap[BeginLabel] != 0;
404 /// setCurrentCallSite - Set the call site currently being processed.
405 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
407 /// getCurrentCallSite - Get the call site currently being processed, if any.
408 /// return zero if none.
409 unsigned getCurrentCallSite() { return CurCallSite; }
411 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
413 const std::vector<const GlobalValue *> &getTypeInfos() const {
417 /// getFilterIds - Return a reference to the typeids encoding filters used in
418 /// the current function.
419 const std::vector<unsigned> &getFilterIds() const {
423 /// getPersonality - Return a personality function if available. The presence
424 /// of one is required to emit exception handling info.
425 const Function *getPersonality() const;
427 /// Classify the personality function amongst known EH styles.
428 EHPersonality getPersonalityType() {
429 if (PersonalityTypeCache != EHPersonality::None)
430 return PersonalityTypeCache;
431 return getPersonalityTypeSlow();
434 /// setVariableDbgInfo - Collect information used to emit debugging
435 /// information of a variable.
436 void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot,
438 VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
441 VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
443 }; // End class MachineModuleInfo
445 } // End llvm namespace