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.
52 class MachineBasicBlock;
53 class MachineFunction;
58 //===----------------------------------------------------------------------===//
59 /// LandingPadInfo - This structure is used to retain landing pad info for
60 /// the current function.
62 struct LandingPadInfo {
63 MachineBasicBlock *LandingPadBlock; // Landing pad block.
64 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
65 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
66 unsigned LandingPadLabel; // Label at beginning of landing pad.
67 Function *Personality; // Personality function.
68 std::vector<int> TypeIds; // List of type ids (filters negative)
70 explicit LandingPadInfo(MachineBasicBlock *MBB)
71 : LandingPadBlock(MBB)
77 //===----------------------------------------------------------------------===//
78 /// MachineModuleInfo - This class contains meta information specific to a
79 /// module. Queries can be made by different debugging and exception handling
80 /// schemes and reformated for specific use.
82 class MachineModuleInfo : public ImmutablePass {
84 // LabelIDList - One entry per assigned label. Normally the entry is equal to
85 // the list index(+1). If the entry is zero then the label has been deleted.
86 // Any other value indicates the label has been deleted by is mapped to
88 std::vector<unsigned> LabelIDList;
90 // FrameMoves - List of moves done by a function's prolog. Used to construct
91 // frame maps by debug and exception handling consumers.
92 std::vector<MachineMove> FrameMoves;
94 // LandingPads - List of LandingPadInfo describing the landing pad information
95 // in the current function.
96 std::vector<LandingPadInfo> LandingPads;
98 // TypeInfos - List of C++ TypeInfo used in the current function.
100 std::vector<GlobalVariable *> TypeInfos;
102 // FilterIds - List of typeids encoding filters used in the current function.
104 std::vector<unsigned> FilterIds;
106 // FilterEnds - List of the indices in FilterIds corresponding to filter
109 std::vector<unsigned> FilterEnds;
111 // Personalities - Vector of all personality functions ever seen. Used to emit
113 std::vector<Function *> Personalities;
115 /// UsedFunctions - The functions in the @llvm.used list in a more easily
116 /// searchable format. This does not include the functions in
117 /// llvm.compiler.used.
118 SmallPtrSet<const Function *, 32> UsedFunctions;
120 /// UsedDbgLabels - labels are used by debug info entries.
121 SmallSet<unsigned, 8> UsedDbgLabels;
124 bool CallsUnwindInit;
126 /// DbgInfoAvailable - True if debugging information is available
128 bool DbgInfoAvailable;
130 static char ID; // Pass identification, replacement for typeid
133 ~MachineModuleInfo();
135 /// doInitialization - Initialize the state for a new module.
137 bool doInitialization();
139 /// doFinalization - Tear down the state after completion of a module.
141 bool doFinalization();
143 /// BeginFunction - Begin gathering function meta information.
145 void BeginFunction(MachineFunction *MF);
147 /// EndFunction - Discard function meta information.
151 /// AnalyzeModule - Scan the module for global debug information.
153 void AnalyzeModule(Module &M);
155 /// hasDebugInfo - Returns true if valid debug info is present.
157 bool hasDebugInfo() const { return DbgInfoAvailable; }
158 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
160 bool callsEHReturn() const { return CallsEHReturn; }
161 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
163 bool callsUnwindInit() const { return CallsUnwindInit; }
164 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
166 /// NextLabelID - Return the next unique label id.
168 unsigned NextLabelID() {
169 unsigned ID = (unsigned)LabelIDList.size() + 1;
170 LabelIDList.push_back(ID);
174 /// InvalidateLabel - Inhibit use of the specified label # from
175 /// MachineModuleInfo, for example because the code was deleted.
176 void InvalidateLabel(unsigned LabelID) {
177 // Remap to zero to indicate deletion.
178 RemapLabel(LabelID, 0);
181 /// RemapLabel - Indicate that a label has been merged into another.
183 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
184 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
185 "Old label ID out of range.");
186 assert(NewLabelID <= LabelIDList.size() &&
187 "New label ID out of range.");
188 LabelIDList[OldLabelID - 1] = NewLabelID;
191 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
192 /// ID != Mapped ID indicates that the label was folded into another label.
193 unsigned MappedLabel(unsigned LabelID) const {
194 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
195 return LabelID ? LabelIDList[LabelID - 1] : 0;
198 /// isDbgLabelUsed - Return true if label with LabelID is used by
200 bool isDbgLabelUsed(unsigned LabelID) {
201 return UsedDbgLabels.count(LabelID);
204 /// RecordUsedDbgLabel - Mark label with LabelID as used. This is used
205 /// by DwarfWriter to inform DebugLabelFolder that certain labels are
206 /// not to be deleted.
207 void RecordUsedDbgLabel(unsigned LabelID) {
208 UsedDbgLabels.insert(LabelID);
211 /// getFrameMoves - Returns a reference to a list of moves done in the current
212 /// function's prologue. Used to construct frame maps for debug and exception
213 /// handling comsumers.
214 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
216 //===-EH-----------------------------------------------------------------===//
218 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
219 /// specified MachineBasicBlock.
220 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
222 /// addInvoke - Provide the begin and end labels of an invoke style call and
223 /// associate it with a try landing pad block.
224 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
227 /// addLandingPad - Add a new panding pad. Returns the label ID for the
228 /// landing pad entry.
229 unsigned addLandingPad(MachineBasicBlock *LandingPad);
231 /// addPersonality - Provide the personality function for the exception
233 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
235 /// getPersonalityIndex - Get index of the current personality function inside
236 /// Personalitites array
237 unsigned getPersonalityIndex() const;
239 /// getPersonalities - Return array of personality functions ever seen.
240 const std::vector<Function *>& getPersonalities() const {
241 return Personalities;
244 /// isUsedFunction - Return true if the functions in the llvm.used list. This
245 /// does not return true for things in llvm.compiler.used unless they are also
247 bool isUsedFunction(const Function *F) {
248 return UsedFunctions.count(F);
251 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
253 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
254 std::vector<GlobalVariable *> &TyInfo);
256 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
258 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
259 std::vector<GlobalVariable *> &TyInfo);
261 /// addCleanup - Add a cleanup action for a landing pad.
263 void addCleanup(MachineBasicBlock *LandingPad);
265 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
267 unsigned getTypeIDFor(GlobalVariable *TI);
269 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
271 int getFilterIDFor(std::vector<unsigned> &TyIds);
273 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
275 void TidyLandingPads();
277 /// getLandingPads - Return a reference to the landing pad info for the
278 /// current function.
279 const std::vector<LandingPadInfo> &getLandingPads() const {
283 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
285 const std::vector<GlobalVariable *> &getTypeInfos() const {
289 /// getFilterIds - Return a reference to the typeids encoding filters used in
290 /// the current function.
291 const std::vector<unsigned> &getFilterIds() const {
295 /// getPersonality - Return a personality function if available. The presence
296 /// of one is required to emit exception handling info.
297 Function *getPersonality() const;
299 }; // End class MachineModuleInfo
301 } // End llvm namespace