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/StringMap.h"
41 #include "llvm/CodeGen/MachineLocation.h"
42 #include "llvm/GlobalValue.h"
43 #include "llvm/Pass.h"
47 //===----------------------------------------------------------------------===//
48 // Forward declarations.
51 class MachineBasicBlock;
52 class MachineFunction;
57 //===----------------------------------------------------------------------===//
58 /// SourceLineInfo - This class is used to record source line correspondence.
60 class SourceLineInfo {
61 unsigned Line; // Source line number.
62 unsigned Column; // Source column.
63 unsigned SourceID; // Source ID number.
64 unsigned LabelID; // Label in code ID number.
66 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
67 : Line(L), Column(C), SourceID(S), LabelID(I) {}
70 unsigned getLine() const { return Line; }
71 unsigned getColumn() const { return Column; }
72 unsigned getSourceID() const { return SourceID; }
73 unsigned getLabelID() const { return LabelID; }
76 //===----------------------------------------------------------------------===//
77 /// LandingPadInfo - This structure is used to retain landing pad info for
78 /// the current function.
80 struct LandingPadInfo {
81 MachineBasicBlock *LandingPadBlock; // Landing pad block.
82 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
83 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
84 unsigned LandingPadLabel; // Label at beginning of landing pad.
85 Function *Personality; // Personality function.
86 std::vector<int> TypeIds; // List of type ids (filters negative)
88 explicit LandingPadInfo(MachineBasicBlock *MBB)
89 : LandingPadBlock(MBB)
95 //===----------------------------------------------------------------------===//
96 /// MachineModuleInfo - This class contains meta information specific to a
97 /// module. Queries can be made by different debugging and exception handling
98 /// schemes and reformated for specific use.
100 class MachineModuleInfo : public ImmutablePass {
102 // Lines - List of of source line correspondence.
103 std::vector<SourceLineInfo> Lines;
105 // LabelIDList - One entry per assigned label. Normally the entry is equal to
106 // the list index(+1). If the entry is zero then the label has been deleted.
107 // Any other value indicates the label has been deleted by is mapped to
109 std::vector<unsigned> LabelIDList;
111 // FrameMoves - List of moves done by a function's prolog. Used to construct
112 // frame maps by debug and exception handling consumers.
113 std::vector<MachineMove> FrameMoves;
115 // LandingPads - List of LandingPadInfo describing the landing pad information
116 // in the current function.
117 std::vector<LandingPadInfo> LandingPads;
119 // TypeInfos - List of C++ TypeInfo used in the current function.
121 std::vector<GlobalVariable *> TypeInfos;
123 // FilterIds - List of typeids encoding filters used in the current function.
125 std::vector<unsigned> FilterIds;
127 // FilterEnds - List of the indices in FilterIds corresponding to filter
130 std::vector<unsigned> FilterEnds;
132 // Personalities - Vector of all personality functions ever seen. Used to emit
134 std::vector<Function *> Personalities;
136 // UsedFunctions - the functions in the llvm.used list in a more easily
137 // searchable format.
138 SmallPtrSet<const Function *, 32> UsedFunctions;
141 bool CallsUnwindInit;
143 /// DbgInfoAvailable - True if debugging information is available
145 bool DbgInfoAvailable;
147 static char ID; // Pass identification, replacement for typeid
150 ~MachineModuleInfo();
152 /// doInitialization - Initialize the state for a new module.
154 bool doInitialization();
156 /// doFinalization - Tear down the state after completion of a module.
158 bool doFinalization();
160 /// BeginFunction - Begin gathering function meta information.
162 void BeginFunction(MachineFunction *MF);
164 /// EndFunction - Discard function meta information.
168 /// AnalyzeModule - Scan the module for global debug information.
170 void AnalyzeModule(Module &M);
172 /// hasDebugInfo - Returns true if valid debug info is present.
174 bool hasDebugInfo() const { return DbgInfoAvailable; }
175 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
177 bool callsEHReturn() const { return CallsEHReturn; }
178 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
180 bool callsUnwindInit() const { return CallsUnwindInit; }
181 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
183 /// NextLabelID - Return the next unique label id.
185 unsigned NextLabelID() {
186 unsigned ID = (unsigned)LabelIDList.size() + 1;
187 LabelIDList.push_back(ID);
191 /// RecordSourceLine - Records location information and associates it with a
192 /// label. Returns a unique label ID used to generate a label and
193 /// provide correspondence to the source line list.
194 unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
196 /// InvalidateLabel - Inhibit use of the specified label # from
197 /// MachineModuleInfo, for example because the code was deleted.
198 void InvalidateLabel(unsigned LabelID) {
199 // Remap to zero to indicate deletion.
200 RemapLabel(LabelID, 0);
203 /// RemapLabel - Indicate that a label has been merged into another.
205 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
206 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
207 "Old label ID out of range.");
208 assert(NewLabelID <= LabelIDList.size() &&
209 "New label ID out of range.");
210 LabelIDList[OldLabelID - 1] = NewLabelID;
213 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
214 /// ID != Mapped ID indicates that the label was folded into another label.
215 unsigned MappedLabel(unsigned LabelID) const {
216 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
217 return LabelID ? LabelIDList[LabelID - 1] : 0;
220 /// getSourceLines - Return a vector of source lines.
222 const std::vector<SourceLineInfo> &getSourceLines() const {
226 /// getFrameMoves - Returns a reference to a list of moves done in the current
227 /// function's prologue. Used to construct frame maps for debug and exception
228 /// handling comsumers.
229 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
231 //===-EH-----------------------------------------------------------------===//
233 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
234 /// specified MachineBasicBlock.
235 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
237 /// addInvoke - Provide the begin and end labels of an invoke style call and
238 /// associate it with a try landing pad block.
239 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
242 /// addLandingPad - Add a new panding pad. Returns the label ID for the
243 /// landing pad entry.
244 unsigned addLandingPad(MachineBasicBlock *LandingPad);
246 /// addPersonality - Provide the personality function for the exception
248 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
250 /// getPersonalityIndex - Get index of the current personality function inside
251 /// Personalitites array
252 unsigned getPersonalityIndex() const;
254 /// getPersonalities - Return array of personality functions ever seen.
255 const std::vector<Function *>& getPersonalities() const {
256 return Personalities;
259 // UsedFunctions - Return set of the functions in the llvm.used list.
260 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
261 return UsedFunctions;
264 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
266 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
267 std::vector<GlobalVariable *> &TyInfo);
269 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
271 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
272 std::vector<GlobalVariable *> &TyInfo);
274 /// addCleanup - Add a cleanup action for a landing pad.
276 void addCleanup(MachineBasicBlock *LandingPad);
278 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
280 unsigned getTypeIDFor(GlobalVariable *TI);
282 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
284 int getFilterIDFor(std::vector<unsigned> &TyIds);
286 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
288 void TidyLandingPads();
290 /// getLandingPads - Return a reference to the landing pad info for the
291 /// current function.
292 const std::vector<LandingPadInfo> &getLandingPads() const {
296 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
298 const std::vector<GlobalVariable *> &getTypeInfos() const {
302 /// getFilterIds - Return a reference to the typeids encoding filters used in
303 /// the current function.
304 const std::vector<unsigned> &getFilterIds() const {
308 /// getPersonality - Return a personality function if available. The presence
309 /// of one is required to emit exception handling info.
310 Function *getPersonality() const;
312 }; // End class MachineModuleInfo
314 } // End llvm namespace