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/GlobalValue.h"
35 #include "llvm/Pass.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/UniqueVector.h"
39 #include "llvm/Support/DataTypes.h"
40 #include "llvm/Support/Dwarf.h"
44 //===----------------------------------------------------------------------===//
45 // Forward declarations.
47 class CompileUnitDesc;
51 class MachineBasicBlock;
52 class MachineFunction;
59 //===----------------------------------------------------------------------===//
60 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
61 /// the supplied DebugInfoDesc.
65 virtual ~DIVisitor() {}
67 /// ApplyToFields - Target the visitor to each field of the debug information
69 void ApplyToFields(DebugInfoDesc *DD);
71 /// Apply - Subclasses override each of these methods to perform the
72 /// appropriate action for the type of field.
73 virtual void Apply(int &Field) = 0;
74 virtual void Apply(unsigned &Field) = 0;
75 virtual void Apply(int64_t &Field) = 0;
76 virtual void Apply(uint64_t &Field) = 0;
77 virtual void Apply(bool &Field) = 0;
78 virtual void Apply(std::string &Field) = 0;
79 virtual void Apply(DebugInfoDesc *&Field) = 0;
80 virtual void Apply(GlobalVariable *&Field) = 0;
81 virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
84 //===----------------------------------------------------------------------===//
85 /// DIDeserializer - This class is responsible for casting GlobalVariables
86 /// into DebugInfoDesc objects.
87 class DIDeserializer {
89 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
90 // Previously defined gloabls.
96 const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const {
100 /// Deserialize - Reconstitute a GlobalVariable into it's component
101 /// DebugInfoDesc objects.
102 DebugInfoDesc *Deserialize(Value *V);
103 DebugInfoDesc *Deserialize(GlobalVariable *GV);
106 //===----------------------------------------------------------------------===//
107 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
108 /// into GlobalVariables.
111 Module *M; // Definition space module.
112 PointerType *StrPtrTy; // A "i8*" type. Created lazily.
113 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
114 std::map<unsigned, StructType *> TagTypes;
115 // Types per Tag. Created lazily.
116 std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
117 // Previously defined descriptors.
118 std::map<const std::string, Constant *> StringCache;
119 // Previously defined strings.
125 , EmptyStructPtrTy(NULL)
133 Module *getModule() const { return M; };
134 void setModule(Module *module) { M = module; }
136 /// getStrPtrType - Return a "i8*" type.
138 const PointerType *getStrPtrType();
140 /// getEmptyStructPtrType - Return a "{ }*" type.
142 const PointerType *getEmptyStructPtrType();
144 /// getTagType - Return the type describing the specified descriptor (via
146 const StructType *getTagType(DebugInfoDesc *DD);
148 /// getString - Construct the string as constant string global.
150 Constant *getString(const std::string &String);
152 /// Serialize - Recursively cast the specified descriptor into a
153 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
154 GlobalVariable *Serialize(DebugInfoDesc *DD);
156 /// addDescriptor - Directly connect DD with existing GV.
157 void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV);
160 //===----------------------------------------------------------------------===//
161 /// DIVerifier - This class is responsible for verifying the given network of
162 /// GlobalVariables are valid as DebugInfoDesc objects.
170 std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
171 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
180 /// Verify - Return true if the GlobalVariable appears to be a valid
181 /// serialization of a DebugInfoDesc.
182 bool Verify(Value *V);
183 bool Verify(GlobalVariable *GV);
185 /// isVerified - Return true if the specified GV has already been
186 /// verified as a debug information descriptor.
187 bool isVerified(GlobalVariable *GV);
190 //===----------------------------------------------------------------------===//
191 /// SourceLineInfo - This class is used to record source line correspondence.
193 class SourceLineInfo {
195 unsigned Line; // Source line number.
196 unsigned Column; // Source column.
197 unsigned SourceID; // Source ID number.
198 unsigned LabelID; // Label in code ID number.
201 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
202 : Line(L), Column(C), SourceID(S), LabelID(I) {}
205 unsigned getLine() const { return Line; }
206 unsigned getColumn() const { return Column; }
207 unsigned getSourceID() const { return SourceID; }
208 unsigned getLabelID() const { return LabelID; }
211 //===----------------------------------------------------------------------===//
212 /// SourceFileInfo - This class is used to track source information.
214 class SourceFileInfo {
216 unsigned DirectoryID; // Directory ID number.
217 std::string Name; // File name (not including directory.)
220 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
223 unsigned getDirectoryID() const { return DirectoryID; }
224 const std::string &getName() const { return Name; }
226 /// operator== - Used by UniqueVector to locate entry.
228 bool operator==(const SourceFileInfo &SI) const {
229 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
232 /// operator< - Used by UniqueVector to locate entry.
234 bool operator<(const SourceFileInfo &SI) const {
235 return getDirectoryID() < SI.getDirectoryID() ||
236 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
240 //===----------------------------------------------------------------------===//
241 /// DebugVariable - This class is used to track local variable information.
243 class DebugVariable {
245 VariableDesc *Desc; // Variable Descriptor.
246 unsigned FrameIndex; // Variable frame index.
249 DebugVariable(VariableDesc *D, unsigned I)
255 VariableDesc *getDesc() const { return Desc; }
256 unsigned getFrameIndex() const { return FrameIndex; }
259 //===----------------------------------------------------------------------===//
260 /// DebugScope - This class is used to track scope information.
264 DebugScope *Parent; // Parent to this scope.
265 DebugInfoDesc *Desc; // Debug info descriptor for scope.
266 // Either subprogram or block.
267 unsigned StartLabelID; // Label ID of the beginning of scope.
268 unsigned EndLabelID; // Label ID of the end of scope.
269 std::vector<DebugScope *> Scopes; // Scopes defined in scope.
270 std::vector<DebugVariable *> Variables;// Variables declared in scope.
273 DebugScope(DebugScope *P, DebugInfoDesc *D)
284 DebugScope *getParent() const { return Parent; }
285 DebugInfoDesc *getDesc() const { return Desc; }
286 unsigned getStartLabelID() const { return StartLabelID; }
287 unsigned getEndLabelID() const { return EndLabelID; }
288 std::vector<DebugScope *> &getScopes() { return Scopes; }
289 std::vector<DebugVariable *> &getVariables() { return Variables; }
290 void setStartLabelID(unsigned S) { StartLabelID = S; }
291 void setEndLabelID(unsigned E) { EndLabelID = E; }
293 /// AddScope - Add a scope to the scope.
295 void AddScope(DebugScope *S) { Scopes.push_back(S); }
297 /// AddVariable - Add a variable to the scope.
299 void AddVariable(DebugVariable *V) { Variables.push_back(V); }
302 //===----------------------------------------------------------------------===//
303 /// LandingPadInfo - This structure is used to retain landing pad info for
304 /// the current function.
306 struct LandingPadInfo {
307 MachineBasicBlock *LandingPadBlock; // Landing pad block.
308 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
309 SmallVector<unsigned, 1> EndLabels; // Labels after invoke.
310 unsigned LandingPadLabel; // Label at beginning of landing pad.
311 Function *Personality; // Personality function.
312 std::vector<int> TypeIds; // List of type ids (filters negative)
314 explicit LandingPadInfo(MachineBasicBlock *MBB)
315 : LandingPadBlock(MBB)
321 //===----------------------------------------------------------------------===//
322 /// MachineModuleInfo - This class contains meta information specific to a
323 /// module. Queries can be made by different debugging and exception handling
324 /// schemes and reformated for specific use.
326 class MachineModuleInfo : public ImmutablePass {
328 // Use the same deserializer/verifier for the module.
332 // CompileUnits - Uniquing vector for compile units.
333 UniqueVector<CompileUnitDesc *> CompileUnits;
335 // Directories - Uniquing vector for directories.
336 UniqueVector<std::string> Directories;
338 // SourceFiles - Uniquing vector for source files.
339 UniqueVector<SourceFileInfo> SourceFiles;
341 // Lines - List of of source line correspondence.
342 std::vector<SourceLineInfo> Lines;
344 // LabelIDList - One entry per assigned label. Normally the entry is equal to
345 // the list index(+1). If the entry is zero then the label has been deleted.
346 // Any other value indicates the label has been deleted by is mapped to
348 std::vector<unsigned> LabelIDList;
350 // ScopeMap - Tracks the scopes in the current function.
351 std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
353 // RootScope - Top level scope for the current function.
355 DebugScope *RootScope;
357 // FrameMoves - List of moves done by a function's prolog. Used to construct
358 // frame maps by debug and exception handling consumers.
359 std::vector<MachineMove> FrameMoves;
361 // LandingPads - List of LandingPadInfo describing the landing pad information
362 // in the current function.
363 std::vector<LandingPadInfo> LandingPads;
365 // TypeInfos - List of C++ TypeInfo used in the current function.
367 std::vector<GlobalVariable *> TypeInfos;
369 // FilterIds - List of typeids encoding filters used in the current function.
371 std::vector<unsigned> FilterIds;
373 // FilterEnds - List of the indices in FilterIds corresponding to filter
376 std::vector<unsigned> FilterEnds;
378 // Personalities - Vector of all personality functions ever seen. Used to emit
380 std::vector<Function *> Personalities;
382 // UsedFunctions - the functions in the llvm.used list in a more easily
383 // searchable format.
384 SmallPtrSet<const Function *, 32> UsedFunctions;
387 bool CallsUnwindInit;
389 static char ID; // Pass identification, replacement for typeid
392 ~MachineModuleInfo();
394 /// doInitialization - Initialize the state for a new module.
396 bool doInitialization();
398 /// doFinalization - Tear down the state after completion of a module.
400 bool doFinalization();
402 /// BeginFunction - Begin gathering function meta information.
404 void BeginFunction(MachineFunction *MF);
406 /// EndFunction - Discard function meta information.
410 /// getDescFor - Convert a Value to a debug information descriptor.
412 // FIXME - use new Value type when available.
413 DebugInfoDesc *getDescFor(Value *V);
415 /// Verify - Verify that a Value is debug information descriptor.
417 bool Verify(Value *V) { return VR.Verify(V); }
419 /// isVerified - Return true if the specified GV has already been
420 /// verified as a debug information descriptor.
421 bool isVerified(GlobalVariable *GV) { return VR.isVerified(GV); }
423 /// AnalyzeModule - Scan the module for global debug information.
425 void AnalyzeModule(Module &M);
427 /// hasDebugInfo - Returns true if valid debug info is present.
429 bool hasDebugInfo() const { return !CompileUnits.empty(); }
431 bool callsEHReturn() const { return CallsEHReturn; }
432 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
434 bool callsUnwindInit() const { return CallsUnwindInit; }
435 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
437 /// NextLabelID - Return the next unique label id.
439 unsigned NextLabelID() {
440 unsigned ID = (unsigned)LabelIDList.size() + 1;
441 LabelIDList.push_back(ID);
445 /// RecordSourceLine - Records location information and associates it with a
446 /// label. Returns a unique label ID used to generate a label and
447 /// provide correspondence to the source line list.
448 unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
450 /// InvalidateLabel - Inhibit use of the specified label # from
451 /// MachineModuleInfo, for example because the code was deleted.
452 void InvalidateLabel(unsigned LabelID) {
453 // Remap to zero to indicate deletion.
454 RemapLabel(LabelID, 0);
457 /// RemapLabel - Indicate that a label has been merged into another.
459 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
460 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
461 "Old label ID out of range.");
462 assert(NewLabelID <= LabelIDList.size() &&
463 "New label ID out of range.");
464 LabelIDList[OldLabelID - 1] = NewLabelID;
467 /// MappedLabel - Find out the label's final ID. Zero indicates deletion.
468 /// ID != Mapped ID indicates that the label was folded into another label.
469 unsigned MappedLabel(unsigned LabelID) const {
470 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
471 return LabelID ? LabelIDList[LabelID - 1] : 0;
474 /// RecordSource - Register a source file with debug info. Returns an source
476 unsigned RecordSource(const std::string &Directory,
477 const std::string &Source);
478 unsigned RecordSource(const CompileUnitDesc *CompileUnit);
480 /// getDirectories - Return the UniqueVector of std::string representing
482 const UniqueVector<std::string> &getDirectories() const {
486 /// getSourceFiles - Return the UniqueVector of source files.
488 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
492 /// getSourceLines - Return a vector of source lines.
494 const std::vector<SourceLineInfo> &getSourceLines() const {
498 /// SetupCompileUnits - Set up the unique vector of compile units.
500 void SetupCompileUnits(Module &M);
502 /// getCompileUnits - Return a vector of debug compile units.
504 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
506 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
507 /// named GlobalVariable.
508 void getGlobalVariablesUsing(Module &M, const std::string &RootName,
509 std::vector<GlobalVariable*> &Result);
511 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
513 void getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc,
514 std::vector<void*> &AnchoredDescs);
516 /// RecordRegionStart - Indicate the start of a region.
518 unsigned RecordRegionStart(Value *V);
520 /// RecordRegionEnd - Indicate the end of a region.
522 unsigned RecordRegionEnd(Value *V);
524 /// RecordVariable - Indicate the declaration of a local variable.
526 void RecordVariable(GlobalValue *GV, unsigned FrameIndex);
528 /// getRootScope - Return current functions root scope.
530 DebugScope *getRootScope() { return RootScope; }
532 /// getOrCreateScope - Returns the scope associated with the given descriptor.
534 DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
536 /// getFrameMoves - Returns a reference to a list of moves done in the current
537 /// function's prologue. Used to construct frame maps for debug and exception
538 /// handling comsumers.
539 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
541 //===-EH-----------------------------------------------------------------===//
543 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
544 /// specified MachineBasicBlock.
545 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
547 /// addInvoke - Provide the begin and end labels of an invoke style call and
548 /// associate it with a try landing pad block.
549 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
552 /// addLandingPad - Add a new panding pad. Returns the label ID for the
553 /// landing pad entry.
554 unsigned addLandingPad(MachineBasicBlock *LandingPad);
556 /// addPersonality - Provide the personality function for the exception
558 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
560 /// getPersonalityIndex - Get index of the current personality function inside
561 /// Personalitites array
562 unsigned getPersonalityIndex() const;
564 /// getPersonalities - Return array of personality functions ever seen.
565 const std::vector<Function *>& getPersonalities() const {
566 return Personalities;
569 // UsedFunctions - Return set of the functions in the llvm.used list.
570 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
571 return UsedFunctions;
574 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
576 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
577 std::vector<GlobalVariable *> &TyInfo);
579 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
581 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
582 std::vector<GlobalVariable *> &TyInfo);
584 /// addCleanup - Add a cleanup action for a landing pad.
586 void addCleanup(MachineBasicBlock *LandingPad);
588 /// getTypeIDFor - Return the type id for the specified typeinfo. This is
590 unsigned getTypeIDFor(GlobalVariable *TI);
592 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is
594 int getFilterIDFor(std::vector<unsigned> &TyIds);
596 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
598 void TidyLandingPads();
600 /// getLandingPads - Return a reference to the landing pad info for the
601 /// current function.
602 const std::vector<LandingPadInfo> &getLandingPads() const {
606 /// getTypeInfos - Return a reference to the C++ typeinfo for the current
608 const std::vector<GlobalVariable *> &getTypeInfos() const {
612 /// getFilterIds - Return a reference to the typeids encoding filters used in
613 /// the current function.
614 const std::vector<unsigned> &getFilterIds() const {
618 /// getPersonality - Return a personality function if available. The presence
619 /// of one is required to emit exception handling info.
620 Function *getPersonality() const;
622 DIDeserializer *getDIDeserializer() { return &DR; }
623 }; // End class MachineModuleInfo
625 } // End llvm namespace