MachineModuleInfo.h revision 305635abeae1d20519b60856c89479e8b7b5d4dd
1//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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 12// schemes. 13// 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. 17// 18// The following information can be retrieved from the MachineModuleInfo. 19// 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. 28// 29//===----------------------------------------------------------------------===// 30 31#ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H 32#define LLVM_CODEGEN_MACHINEMODULEINFO_H 33 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" 41 42namespace llvm { 43 44//===----------------------------------------------------------------------===// 45// Forward declarations. 46class AnchoredDesc; 47class CompileUnitDesc; 48class Constant; 49class DebugInfoDesc; 50class GlobalVariable; 51class MachineBasicBlock; 52class MachineFunction; 53class MachineMove; 54class Module; 55class PointerType; 56class StructType; 57class VariableDesc; 58 59//===----------------------------------------------------------------------===// 60/// DIVisitor - Subclasses of this class apply steps to each of the fields in 61/// the supplied DebugInfoDesc. 62class DIVisitor { 63public: 64 DIVisitor() {} 65 virtual ~DIVisitor() {} 66 67 /// ApplyToFields - Target the visitor to each field of the debug information 68 /// descriptor. 69 void ApplyToFields(DebugInfoDesc *DD); 70 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; 82}; 83 84//===----------------------------------------------------------------------===// 85/// DIDeserializer - This class is responsible for casting GlobalVariables 86/// into DebugInfoDesc objects. 87class DIDeserializer { 88private: 89 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs; 90 // Previously defined gloabls. 91 92public: 93 DIDeserializer() {} 94 ~DIDeserializer() {} 95 96 const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const { 97 return GlobalDescs; 98 } 99 100 /// Deserialize - Reconstitute a GlobalVariable into it's component 101 /// DebugInfoDesc objects. 102 DebugInfoDesc *Deserialize(Value *V); 103 DebugInfoDesc *Deserialize(GlobalVariable *GV); 104}; 105 106//===----------------------------------------------------------------------===// 107/// DISerializer - This class is responsible for casting DebugInfoDesc objects 108/// into GlobalVariables. 109class DISerializer { 110private: 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. 120 121public: 122 DISerializer() 123 : M(NULL) 124 , StrPtrTy(NULL) 125 , EmptyStructPtrTy(NULL) 126 , TagTypes() 127 , DescGlobals() 128 , StringCache() 129 {} 130 ~DISerializer() {} 131 132 // Accessors 133 Module *getModule() const { return M; }; 134 void setModule(Module *module) { M = module; } 135 136 /// getStrPtrType - Return a "i8*" type. 137 /// 138 const PointerType *getStrPtrType(); 139 140 /// getEmptyStructPtrType - Return a "{ }*" type. 141 /// 142 const PointerType *getEmptyStructPtrType(); 143 144 /// getTagType - Return the type describing the specified descriptor (via 145 /// tag.) 146 const StructType *getTagType(DebugInfoDesc *DD); 147 148 /// getString - Construct the string as constant string global. 149 /// 150 Constant *getString(const std::string &String); 151 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); 155 156 /// addDescriptor - Directly connect DD with existing GV. 157 void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV); 158}; 159 160//===----------------------------------------------------------------------===// 161/// DIVerifier - This class is responsible for verifying the given network of 162/// GlobalVariables are valid as DebugInfoDesc objects. 163class DIVerifier { 164private: 165 enum { 166 Unknown = 0, 167 Invalid, 168 Valid 169 }; 170 std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results. 171 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type. 172 173public: 174 DIVerifier() 175 : Validity() 176 , Counts() 177 {} 178 ~DIVerifier() {} 179 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); 184 185 /// isVerified - Return true if the specified GV has already been 186 /// verified as a debug information descriptor. 187 bool isVerified(GlobalVariable *GV); 188}; 189 190//===----------------------------------------------------------------------===// 191/// SourceLineInfo - This class is used to record source line correspondence. 192/// 193class SourceLineInfo { 194private: 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. 199 200public: 201 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I) 202 : Line(L), Column(C), SourceID(S), LabelID(I) {} 203 204 // Accessors 205 unsigned getLine() const { return Line; } 206 unsigned getColumn() const { return Column; } 207 unsigned getSourceID() const { return SourceID; } 208 unsigned getLabelID() const { return LabelID; } 209}; 210 211//===----------------------------------------------------------------------===// 212/// SourceFileInfo - This class is used to track source information. 213/// 214class SourceFileInfo { 215private: 216 unsigned DirectoryID; // Directory ID number. 217 std::string Name; // File name (not including directory.) 218 219public: 220 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {} 221 222 // Accessors 223 unsigned getDirectoryID() const { return DirectoryID; } 224 const std::string &getName() const { return Name; } 225 226 /// operator== - Used by UniqueVector to locate entry. 227 /// 228 bool operator==(const SourceFileInfo &SI) const { 229 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName(); 230 } 231 232 /// operator< - Used by UniqueVector to locate entry. 233 /// 234 bool operator<(const SourceFileInfo &SI) const { 235 return getDirectoryID() < SI.getDirectoryID() || 236 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName()); 237 } 238}; 239 240//===----------------------------------------------------------------------===// 241/// DebugVariable - This class is used to track local variable information. 242/// 243class DebugVariable { 244private: 245 VariableDesc *Desc; // Variable Descriptor. 246 unsigned FrameIndex; // Variable frame index. 247 248public: 249 DebugVariable(VariableDesc *D, unsigned I) 250 : Desc(D) 251 , FrameIndex(I) 252 {} 253 254 // Accessors. 255 VariableDesc *getDesc() const { return Desc; } 256 unsigned getFrameIndex() const { return FrameIndex; } 257}; 258 259//===----------------------------------------------------------------------===// 260/// DebugScope - This class is used to track scope information. 261/// 262class DebugScope { 263private: 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. 271 272public: 273 DebugScope(DebugScope *P, DebugInfoDesc *D) 274 : Parent(P) 275 , Desc(D) 276 , StartLabelID(0) 277 , EndLabelID(0) 278 , Scopes() 279 , Variables() 280 {} 281 ~DebugScope(); 282 283 // Accessors. 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; } 292 293 /// AddScope - Add a scope to the scope. 294 /// 295 void AddScope(DebugScope *S) { Scopes.push_back(S); } 296 297 /// AddVariable - Add a variable to the scope. 298 /// 299 void AddVariable(DebugVariable *V) { Variables.push_back(V); } 300}; 301 302//===----------------------------------------------------------------------===// 303/// LandingPadInfo - This structure is used to retain landing pad info for 304/// the current function. 305/// 306struct 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) 313 314 explicit LandingPadInfo(MachineBasicBlock *MBB) 315 : LandingPadBlock(MBB) 316 , LandingPadLabel(0) 317 , Personality(NULL) 318 {} 319}; 320 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. 325/// 326class MachineModuleInfo : public ImmutablePass { 327private: 328 // Use the same deserializer/verifier for the module. 329 DIDeserializer DR; 330 DIVerifier VR; 331 332 // CompileUnits - Uniquing vector for compile units. 333 UniqueVector<CompileUnitDesc *> CompileUnits; 334 335 // Directories - Uniquing vector for directories. 336 UniqueVector<std::string> Directories; 337 338 // SourceFiles - Uniquing vector for source files. 339 UniqueVector<SourceFileInfo> SourceFiles; 340 341 // Lines - List of of source line correspondence. 342 std::vector<SourceLineInfo> Lines; 343 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 347 // another label. 348 std::vector<unsigned> LabelIDList; 349 350 // ScopeMap - Tracks the scopes in the current function. 351 std::map<DebugInfoDesc *, DebugScope *> ScopeMap; 352 353 // RootScope - Top level scope for the current function. 354 // 355 DebugScope *RootScope; 356 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; 360 361 // LandingPads - List of LandingPadInfo describing the landing pad information 362 // in the current function. 363 std::vector<LandingPadInfo> LandingPads; 364 365 // TypeInfos - List of C++ TypeInfo used in the current function. 366 // 367 std::vector<GlobalVariable *> TypeInfos; 368 369 // FilterIds - List of typeids encoding filters used in the current function. 370 // 371 std::vector<unsigned> FilterIds; 372 373 // FilterEnds - List of the indices in FilterIds corresponding to filter 374 // terminators. 375 // 376 std::vector<unsigned> FilterEnds; 377 378 // Personalities - Vector of all personality functions ever seen. Used to emit 379 // common EH frames. 380 std::vector<Function *> Personalities; 381 382 // UsedFunctions - the functions in the llvm.used list in a more easily 383 // searchable format. 384 SmallPtrSet<const Function *, 32> UsedFunctions; 385 386 bool CallsEHReturn; 387 bool CallsUnwindInit; 388public: 389 static char ID; // Pass identification, replacement for typeid 390 391 MachineModuleInfo(); 392 ~MachineModuleInfo(); 393 394 /// doInitialization - Initialize the state for a new module. 395 /// 396 bool doInitialization(); 397 398 /// doFinalization - Tear down the state after completion of a module. 399 /// 400 bool doFinalization(); 401 402 /// BeginFunction - Begin gathering function meta information. 403 /// 404 void BeginFunction(MachineFunction *MF); 405 406 /// EndFunction - Discard function meta information. 407 /// 408 void EndFunction(); 409 410 /// getDescFor - Convert a Value to a debug information descriptor. 411 /// 412 // FIXME - use new Value type when available. 413 DebugInfoDesc *getDescFor(Value *V); 414 415 /// Verify - Verify that a Value is debug information descriptor. 416 /// 417 bool Verify(Value *V) { return VR.Verify(V); } 418 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); } 422 423 /// AnalyzeModule - Scan the module for global debug information. 424 /// 425 void AnalyzeModule(Module &M); 426 427 /// hasDebugInfo - Returns true if valid debug info is present. 428 /// 429 bool hasDebugInfo() const { return !CompileUnits.empty(); } 430 431 bool callsEHReturn() const { return CallsEHReturn; } 432 void setCallsEHReturn(bool b) { CallsEHReturn = b; } 433 434 bool callsUnwindInit() const { return CallsUnwindInit; } 435 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; } 436 437 /// NextLabelID - Return the next unique label id. 438 /// 439 unsigned NextLabelID() { 440 unsigned ID = (unsigned)LabelIDList.size() + 1; 441 LabelIDList.push_back(ID); 442 return ID; 443 } 444 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); 449 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); 455 } 456 457 /// RemapLabel - Indicate that a label has been merged into another. 458 /// 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; 465 } 466 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; 472 } 473 474 /// RecordSource - Register a source file with debug info. Returns an source 475 /// ID. 476 unsigned RecordSource(const std::string &Directory, 477 const std::string &Source); 478 unsigned RecordSource(const CompileUnitDesc *CompileUnit); 479 480 /// getDirectories - Return the UniqueVector of std::string representing 481 /// directories. 482 const UniqueVector<std::string> &getDirectories() const { 483 return Directories; 484 } 485 486 /// getSourceFiles - Return the UniqueVector of source files. 487 /// 488 const UniqueVector<SourceFileInfo> &getSourceFiles() const { 489 return SourceFiles; 490 } 491 492 /// getSourceLines - Return a vector of source lines. 493 /// 494 const std::vector<SourceLineInfo> &getSourceLines() const { 495 return Lines; 496 } 497 498 /// SetupCompileUnits - Set up the unique vector of compile units. 499 /// 500 void SetupCompileUnits(Module &M); 501 502 /// getCompileUnits - Return a vector of debug compile units. 503 /// 504 const UniqueVector<CompileUnitDesc *> getCompileUnits() const; 505 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); 510 511 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors. 512 /// 513 void getAnchoredDescriptors(Module &M, const AnchoredDesc *Desc, 514 std::vector<void*> &AnchoredDescs); 515 516 /// RecordRegionStart - Indicate the start of a region. 517 /// 518 unsigned RecordRegionStart(Value *V); 519 520 /// RecordRegionEnd - Indicate the end of a region. 521 /// 522 unsigned RecordRegionEnd(Value *V); 523 524 /// RecordVariable - Indicate the declaration of a local variable. 525 /// 526 void RecordVariable(GlobalValue *GV, unsigned FrameIndex); 527 528 /// getRootScope - Return current functions root scope. 529 /// 530 DebugScope *getRootScope() { return RootScope; } 531 532 /// getOrCreateScope - Returns the scope associated with the given descriptor. 533 /// 534 DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc); 535 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; } 540 541 //===-EH-----------------------------------------------------------------===// 542 543 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 544 /// specified MachineBasicBlock. 545 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad); 546 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, 550 unsigned EndLabel); 551 552 /// addLandingPad - Add a new panding pad. Returns the label ID for the 553 /// landing pad entry. 554 unsigned addLandingPad(MachineBasicBlock *LandingPad); 555 556 /// addPersonality - Provide the personality function for the exception 557 /// information. 558 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality); 559 560 /// getPersonalityIndex - Get index of the current personality function inside 561 /// Personalitites array 562 unsigned getPersonalityIndex() const; 563 564 /// getPersonalities - Return array of personality functions ever seen. 565 const std::vector<Function *>& getPersonalities() const { 566 return Personalities; 567 } 568 569 // UsedFunctions - Return set of the functions in the llvm.used list. 570 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const { 571 return UsedFunctions; 572 } 573 574 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 575 /// 576 void addCatchTypeInfo(MachineBasicBlock *LandingPad, 577 std::vector<GlobalVariable *> &TyInfo); 578 579 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 580 /// 581 void addFilterTypeInfo(MachineBasicBlock *LandingPad, 582 std::vector<GlobalVariable *> &TyInfo); 583 584 /// addCleanup - Add a cleanup action for a landing pad. 585 /// 586 void addCleanup(MachineBasicBlock *LandingPad); 587 588 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 589 /// function wide. 590 unsigned getTypeIDFor(GlobalVariable *TI); 591 592 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is 593 /// function wide. 594 int getFilterIDFor(std::vector<unsigned> &TyIds); 595 596 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 597 /// pads. 598 void TidyLandingPads(); 599 600 /// getLandingPads - Return a reference to the landing pad info for the 601 /// current function. 602 const std::vector<LandingPadInfo> &getLandingPads() const { 603 return LandingPads; 604 } 605 606 /// getTypeInfos - Return a reference to the C++ typeinfo for the current 607 /// function. 608 const std::vector<GlobalVariable *> &getTypeInfos() const { 609 return TypeInfos; 610 } 611 612 /// getFilterIds - Return a reference to the typeids encoding filters used in 613 /// the current function. 614 const std::vector<unsigned> &getFilterIds() const { 615 return FilterIds; 616 } 617 618 /// getPersonality - Return a personality function if available. The presence 619 /// of one is required to emit exception handling info. 620 Function *getPersonality() const; 621 622 DIDeserializer *getDIDeserializer() { return &DR; } 623}; // End class MachineModuleInfo 624 625} // End llvm namespace 626 627#endif 628