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/Support/Dwarf.h"
35 #include "llvm/Support/DataTypes.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/UniqueVector.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/SmallSet.h"
41 #include "llvm/ADT/StringMap.h"
42 #include "llvm/CodeGen/MachineLocation.h"
43 #include "llvm/GlobalValue.h"
44 #include "llvm/Pass.h"
48 //===----------------------------------------------------------------------===//
49 // Forward declarations.
53 class MachineBasicBlock;
54 class MachineFunction;
60 /// MachineModuleInfoImpl - This class can be derived from and used by targets
61 /// to hold private target-specific information for each Module. Objects of
62 /// type are accessed/created with MMI::getInfo and destroyed when the
63 /// MachineModuleInfo is destroyed.
64 class MachineModuleInfoImpl {
66 virtual ~MachineModuleInfoImpl();
71 //===----------------------------------------------------------------------===//
72 /// LandingPadInfo - This structure is used to retain landing pad info for
73 /// the current function.
75 struct LandingPadInfo {
76 MachineBasicBlock *LandingPadBlock; // Landing pad block.
77 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
78 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
79 unsigned LandingPadLabel; // Label at beginning of landing pad.
80 Function *Personality; // Personality function.
81 std::vector<int> TypeIds; // List of type ids (filters negative)
83 explicit LandingPadInfo(MachineBasicBlock *MBB)
84 : LandingPadBlock(MBB)
90 //===----------------------------------------------------------------------===//
91 /// MachineModuleInfo - This class contains meta information specific to a
92 /// module. Queries can be made by different debugging and exception handling
93 /// schemes and reformated for specific use.
95 class MachineModuleInfo : public ImmutablePass {
96 /// ObjFileMMI - This is the object-file-format-specific implementation of
97 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
99 MachineModuleInfoImpl *ObjFileMMI;
101 // LabelIDList - One entry per assigned label. Normally the entry is equal to
102 // the list index(+1). If the entry is zero then the label has been deleted.
103 // Any other value indicates the label has been deleted by is mapped to
105 std::vector<unsigned> LabelIDList;
107 // FrameMoves - List of moves done by a function's prolog. Used to construct
108 // frame maps by debug and exception handling consumers.
109 std::vector<MachineMove> FrameMoves;
111 // LandingPads - List of LandingPadInfo describing the landing pad information
112 // in the current function.
113 std::vector<LandingPadInfo> LandingPads;
115 // TypeInfos - List of C++ TypeInfo used in the current function.
117 std::vector<GlobalVariable *> TypeInfos;
119 // FilterIds - List of typeids encoding filters used in the current function.
121 std::vector<unsigned> FilterIds;
123 // FilterEnds - List of the indices in FilterIds corresponding to filter
126 std::vector<unsigned> FilterEnds;
128 // Personalities - Vector of all personality functions ever seen. Used to emit
130 std::vector<Function *> Personalities;
132 /// UsedFunctions - The functions in the @llvm.used list in a more easily
133 /// searchable format. This does not include the functions in
134 /// llvm.compiler.used.
135 SmallPtrSet<const Function *, 32> UsedFunctions;
137 /// UsedDbgLabels - labels are used by debug info entries.
138 SmallSet<unsigned, 8> UsedDbgLabels;
141 bool CallsUnwindInit;
143 /// DbgInfoAvailable - True if debugging information is available
145 bool DbgInfoAvailable;
148 static char ID; // Pass identification, replacement for typeid
150 typedef DenseMap<MDNode *, std::pair<MDNode *, unsigned> > VariableDbgInfoMapTy;
151 VariableDbgInfoMapTy VariableDbgInfo;
154 ~MachineModuleInfo();
156 bool doInitialization();
157 bool doFinalization();
159 /// BeginFunction - Begin gathering function meta information.
161 void BeginFunction(MachineFunction *) {}
163 /// EndFunction - Discard function meta information.
167 /// getInfo - Keep track of various per-function pieces of information for
168 /// backends that would like to do so.
170 template<typename Ty>
171 Ty &getObjFileInfo() {
173 ObjFileMMI = new Ty(*this);
175 assert((void*)dynamic_cast<Ty*>(ObjFileMMI) == (void*)ObjFileMMI &&
176 "Invalid concrete type or multiple inheritence for getInfo");
177 return *static_cast<Ty*>(ObjFileMMI);
180 template<typename Ty>
181 const Ty &getObjFileInfo() const {
182 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
185 /// AnalyzeModule - Scan the module for global debug information.
187 void AnalyzeModule(Module &M);
189 /// hasDebugInfo - Returns true if valid debug info is present.
191 bool hasDebugInfo() const { return DbgInfoAvailable; }
192 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
194 bool callsEHReturn() const { return CallsEHReturn; }
195 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
197 bool callsUnwindInit() const { return CallsUnwindInit; }
198 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
200 /// NextLabelID - Return the next unique label id.
202 unsigned NextLabelID() {
203 unsigned ID = (unsigned)LabelIDList.size() + 1;
204 LabelIDList.push_back(ID);
208 /// InvalidateLabel - Inhibit use of the specified label # from
209 /// MachineModuleInfo, for example because the code was deleted.
210 void InvalidateLabel(unsigned LabelID) {
211 // Remap to zero to indicate deletion.
212 RemapLabel(LabelID, 0);
215 /// RemapLabel - Indicate that a label has been merged into another.
217 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
218 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
219 "Old label ID out of range.");
220 assert(NewLabelID <= LabelIDList.size() &&
221 "New label ID out of range.");
222 LabelIDList[OldLabelID - 1] = NewLabelID;
225 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
226 /// ID != Mapped ID indicates that the label was folded into another label.
227 unsigned MappedLabel(unsigned LabelID) const {
228 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
229 return LabelID ? LabelIDList[LabelID - 1] : 0;
232 /// isDbgLabelUsed - Return true if label with LabelID is used by
234 bool isDbgLabelUsed(unsigned LabelID) {
235 return UsedDbgLabels.count(LabelID);
238 /// RecordUsedDbgLabel - Mark label with LabelID as used. This is used
239 /// by DwarfWriter to inform DebugLabelFolder that certain labels are
240 /// not to be deleted.
241 void RecordUsedDbgLabel(unsigned LabelID) {
242 UsedDbgLabels.insert(LabelID);
245 /// getFrameMoves - Returns a reference to a list of moves done in the current
246 /// function's prologue. Used to construct frame maps for debug and exception
247 /// handling comsumers.
248 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
250 //===-EH-----------------------------------------------------------------===//
252 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
253 /// specified MachineBasicBlock.
254 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
256 /// addInvoke - Provide the begin and end labels of an invoke style call and
257 /// associate it with a try landing pad block.
258 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
261 /// addLandingPad - Add a new panding pad. Returns the label ID for the
262 /// landing pad entry.
263 unsigned addLandingPad(MachineBasicBlock *LandingPad);
265 /// addPersonality - Provide the personality function for the exception
267 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
269 /// getPersonalityIndex - Get index of the current personality function inside
270 /// Personalitites array
271 unsigned getPersonalityIndex() const;
273 /// getPersonalities - Return array of personality functions ever seen.
274 const std::vector<Function *>& getPersonalities() const {
275 return Personalities;
278 /// isUsedFunction - Return true if the functions in the llvm.used list. This
279 /// does not return true for things in llvm.compiler.used unless they are also
281 bool isUsedFunction(const Function *F) {
282 return UsedFunctions.count(F);
285 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
287 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
288 std::vector<GlobalVariable *> &TyInfo);
290 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
292 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
293 std::vector<GlobalVariable *> &TyInfo);
295 /// addCleanup - Add a cleanup action for a landing pad.
297 void addCleanup(MachineBasicBlock *LandingPad);
299 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
301 unsigned getTypeIDFor(GlobalVariable *TI);
303 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
305 int getFilterIDFor(std::vector<unsigned> &TyIds);
307 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
309 void TidyLandingPads();
311 /// getLandingPads - Return a reference to the landing pad info for the
312 /// current function.
313 const std::vector<LandingPadInfo> &getLandingPads() const {
317 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
319 const std::vector<GlobalVariable *> &getTypeInfos() const {
323 /// getFilterIds - Return a reference to the typeids encoding filters used in
324 /// the current function.
325 const std::vector<unsigned> &getFilterIds() const {
329 /// getPersonality - Return a personality function if available. The presence
330 /// of one is required to emit exception handling info.
331 Function *getPersonality() const;
333 /// setVariableDbgInfo - Collect information used to emit debugging information
335 void setVariableDbgInfo(MDNode *N, MDNode *L, unsigned S) {
337 VariableDbgInfo[N] = std::make_pair(L, S);
340 VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfo; }
342 }; // End class MachineModuleInfo
344 } // End llvm namespace