MachineModuleInfo.cpp revision 993b3dd3d5cf100b4d59b9e5c62acb56c53fb7ac
1//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- 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#include "llvm/CodeGen/MachineModuleInfo.h" 11 12#include "llvm/Constants.h" 13#include "llvm/Analysis/ValueTracking.h" 14#include "llvm/CodeGen/MachineFunctionPass.h" 15#include "llvm/CodeGen/MachineFunction.h" 16#include "llvm/CodeGen/MachineLocation.h" 17#include "llvm/Target/TargetInstrInfo.h" 18#include "llvm/Target/TargetMachine.h" 19#include "llvm/Target/TargetOptions.h" 20#include "llvm/DerivedTypes.h" 21#include "llvm/GlobalVariable.h" 22#include "llvm/Intrinsics.h" 23#include "llvm/Instructions.h" 24#include "llvm/Module.h" 25#include "llvm/Support/Dwarf.h" 26#include "llvm/Support/Streams.h" 27using namespace llvm; 28using namespace llvm::dwarf; 29 30// Handle the Pass registration stuff necessary to use TargetData's. 31static RegisterPass<MachineModuleInfo> 32X("machinemoduleinfo", "Module Information"); 33char MachineModuleInfo::ID = 0; 34 35//===----------------------------------------------------------------------===// 36 37/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the 38/// specified value in their initializer somewhere. 39static void 40getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) { 41 // Scan though value users. 42 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 43 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) { 44 // If the user is a GlobalVariable then add to result. 45 Result.push_back(GV); 46 } else if (Constant *C = dyn_cast<Constant>(*I)) { 47 // If the user is a constant variable then scan its users 48 getGlobalVariablesUsing(C, Result); 49 } 50 } 51} 52 53/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the 54/// named GlobalVariable. 55static void 56getGlobalVariablesUsing(Module &M, const std::string &RootName, 57 std::vector<GlobalVariable*> &Result) { 58 std::vector<const Type*> FieldTypes; 59 FieldTypes.push_back(Type::Int32Ty); 60 FieldTypes.push_back(Type::Int32Ty); 61 62 // Get the GlobalVariable root. 63 GlobalVariable *UseRoot = M.getGlobalVariable(RootName, 64 StructType::get(FieldTypes)); 65 66 // If present and linkonce then scan for users. 67 if (UseRoot && UseRoot->hasLinkOnceLinkage()) 68 getGlobalVariablesUsing(UseRoot, Result); 69} 70 71/// isStringValue - Return true if the given value can be coerced to a string. 72/// 73static bool isStringValue(Value *V) { 74 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { 75 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { 76 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); 77 return Init->isString(); 78 } 79 } else if (Constant *C = dyn_cast<Constant>(V)) { 80 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 81 return isStringValue(GV); 82 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 83 if (CE->getOpcode() == Instruction::GetElementPtr) { 84 if (CE->getNumOperands() == 3 && 85 cast<Constant>(CE->getOperand(1))->isNullValue() && 86 isa<ConstantInt>(CE->getOperand(2))) { 87 return isStringValue(CE->getOperand(0)); 88 } 89 } 90 } 91 } 92 return false; 93} 94 95/// getGlobalVariable - Return either a direct or cast Global value. 96/// 97static GlobalVariable *getGlobalVariable(Value *V) { 98 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { 99 return GV; 100 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 101 if (CE->getOpcode() == Instruction::BitCast) { 102 return dyn_cast<GlobalVariable>(CE->getOperand(0)); 103 } else if (CE->getOpcode() == Instruction::GetElementPtr) { 104 for (unsigned int i=1; i<CE->getNumOperands(); i++) { 105 if (!CE->getOperand(i)->isNullValue()) 106 return NULL; 107 } 108 return dyn_cast<GlobalVariable>(CE->getOperand(0)); 109 } 110 } 111 return NULL; 112} 113 114/// isGlobalVariable - Return true if the given value can be coerced to a 115/// GlobalVariable. 116static bool isGlobalVariable(Value *V) { 117 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) { 118 return true; 119 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 120 if (CE->getOpcode() == Instruction::BitCast) { 121 return isa<GlobalVariable>(CE->getOperand(0)); 122 } else if (CE->getOpcode() == Instruction::GetElementPtr) { 123 for (unsigned int i=1; i<CE->getNumOperands(); i++) { 124 if (!CE->getOperand(i)->isNullValue()) 125 return false; 126 } 127 return isa<GlobalVariable>(CE->getOperand(0)); 128 } 129 } 130 return false; 131} 132 133/// getUIntOperand - Return ith operand if it is an unsigned integer. 134/// 135static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) { 136 // Make sure the GlobalVariable has an initializer. 137 if (!GV->hasInitializer()) return NULL; 138 139 // Get the initializer constant. 140 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer()); 141 if (!CI) return NULL; 142 143 // Check if there is at least i + 1 operands. 144 unsigned N = CI->getNumOperands(); 145 if (i >= N) return NULL; 146 147 // Check constant. 148 return dyn_cast<ConstantInt>(CI->getOperand(i)); 149} 150 151//===----------------------------------------------------------------------===// 152 153/// ApplyToFields - Target the visitor to each field of the debug information 154/// descriptor. 155void DIVisitor::ApplyToFields(DebugInfoDesc *DD) { 156 DD->ApplyToFields(this); 157} 158 159namespace { 160 161//===----------------------------------------------------------------------===// 162/// DICountVisitor - This DIVisitor counts all the fields in the supplied debug 163/// the supplied DebugInfoDesc. 164class DICountVisitor : public DIVisitor { 165private: 166 unsigned Count; // Running count of fields. 167 168public: 169 DICountVisitor() : DIVisitor(), Count(0) {} 170 171 // Accessors. 172 unsigned getCount() const { return Count; } 173 174 /// Apply - Count each of the fields. 175 /// 176 virtual void Apply(int &Field) { ++Count; } 177 virtual void Apply(unsigned &Field) { ++Count; } 178 virtual void Apply(int64_t &Field) { ++Count; } 179 virtual void Apply(uint64_t &Field) { ++Count; } 180 virtual void Apply(bool &Field) { ++Count; } 181 virtual void Apply(std::string &Field) { ++Count; } 182 virtual void Apply(DebugInfoDesc *&Field) { ++Count; } 183 virtual void Apply(GlobalVariable *&Field) { ++Count; } 184 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 185 ++Count; 186 } 187}; 188 189//===----------------------------------------------------------------------===// 190/// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the 191/// supplied DebugInfoDesc. 192class DIDeserializeVisitor : public DIVisitor { 193private: 194 DIDeserializer &DR; // Active deserializer. 195 unsigned I; // Current operand index. 196 ConstantStruct *CI; // GlobalVariable constant initializer. 197 198public: 199 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) 200 : DIVisitor(), DR(D), I(0), CI(cast<ConstantStruct>(GV->getInitializer())) 201 {} 202 203 /// Apply - Set the value of each of the fields. 204 /// 205 virtual void Apply(int &Field) { 206 Constant *C = CI->getOperand(I++); 207 Field = cast<ConstantInt>(C)->getSExtValue(); 208 } 209 virtual void Apply(unsigned &Field) { 210 Constant *C = CI->getOperand(I++); 211 Field = cast<ConstantInt>(C)->getZExtValue(); 212 } 213 virtual void Apply(int64_t &Field) { 214 Constant *C = CI->getOperand(I++); 215 Field = cast<ConstantInt>(C)->getSExtValue(); 216 } 217 virtual void Apply(uint64_t &Field) { 218 Constant *C = CI->getOperand(I++); 219 Field = cast<ConstantInt>(C)->getZExtValue(); 220 } 221 virtual void Apply(bool &Field) { 222 Constant *C = CI->getOperand(I++); 223 Field = cast<ConstantInt>(C)->getZExtValue(); 224 } 225 virtual void Apply(std::string &Field) { 226 Constant *C = CI->getOperand(I++); 227 // Fills in the string if it succeeds 228 if (!GetConstantStringInfo(C, Field)) 229 Field.clear(); 230 } 231 virtual void Apply(DebugInfoDesc *&Field) { 232 Constant *C = CI->getOperand(I++); 233 Field = DR.Deserialize(C); 234 } 235 virtual void Apply(GlobalVariable *&Field) { 236 Constant *C = CI->getOperand(I++); 237 Field = getGlobalVariable(C); 238 } 239 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 240 Field.resize(0); 241 Constant *C = CI->getOperand(I++); 242 GlobalVariable *GV = getGlobalVariable(C); 243 if (GV->hasInitializer()) { 244 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) { 245 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) { 246 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); 247 DebugInfoDesc *DE = DR.Deserialize(GVE); 248 Field.push_back(DE); 249 } 250 } else if (GV->getInitializer()->isNullValue()) { 251 if (const ArrayType *T = 252 dyn_cast<ArrayType>(GV->getType()->getElementType())) { 253 Field.resize(T->getNumElements()); 254 } 255 } 256 } 257 } 258}; 259 260//===----------------------------------------------------------------------===// 261/// DISerializeVisitor - This DIVisitor serializes all the fields in 262/// the supplied DebugInfoDesc. 263class DISerializeVisitor : public DIVisitor { 264private: 265 DISerializer &SR; // Active serializer. 266 std::vector<Constant*> &Elements; // Element accumulator. 267 268public: 269 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) 270 : DIVisitor() 271 , SR(S) 272 , Elements(E) 273 {} 274 275 /// Apply - Set the value of each of the fields. 276 /// 277 virtual void Apply(int &Field) { 278 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field))); 279 } 280 virtual void Apply(unsigned &Field) { 281 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field))); 282 } 283 virtual void Apply(int64_t &Field) { 284 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field))); 285 } 286 virtual void Apply(uint64_t &Field) { 287 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field))); 288 } 289 virtual void Apply(bool &Field) { 290 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field)); 291 } 292 virtual void Apply(std::string &Field) { 293 Elements.push_back(SR.getString(Field)); 294 } 295 virtual void Apply(DebugInfoDesc *&Field) { 296 GlobalVariable *GV = NULL; 297 298 // If non-NULL then convert to global. 299 if (Field) GV = SR.Serialize(Field); 300 301 // FIXME - At some point should use specific type. 302 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 303 304 if (GV) { 305 // Set to pointer to global. 306 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy)); 307 } else { 308 // Use NULL. 309 Elements.push_back(ConstantPointerNull::get(EmptyTy)); 310 } 311 } 312 virtual void Apply(GlobalVariable *&Field) { 313 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 314 if (Field) { 315 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy)); 316 } else { 317 Elements.push_back(ConstantPointerNull::get(EmptyTy)); 318 } 319 } 320 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 321 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 322 unsigned N = Field.size(); 323 ArrayType *AT = ArrayType::get(EmptyTy, N); 324 std::vector<Constant *> ArrayElements; 325 326 for (unsigned i = 0, N = Field.size(); i < N; ++i) { 327 if (DebugInfoDesc *Element = Field[i]) { 328 GlobalVariable *GVE = SR.Serialize(Element); 329 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy); 330 ArrayElements.push_back(cast<Constant>(CE)); 331 } else { 332 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy)); 333 } 334 } 335 336 Constant *CA = ConstantArray::get(AT, ArrayElements); 337 GlobalVariable *CAGV = new GlobalVariable(AT, true, 338 GlobalValue::InternalLinkage, 339 CA, "llvm.dbg.array", 340 SR.getModule()); 341 CAGV->setSection("llvm.metadata"); 342 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy); 343 Elements.push_back(CAE); 344 } 345}; 346 347//===----------------------------------------------------------------------===// 348/// DIGetTypesVisitor - This DIVisitor gathers all the field types in 349/// the supplied DebugInfoDesc. 350class DIGetTypesVisitor : public DIVisitor { 351private: 352 DISerializer &SR; // Active serializer. 353 std::vector<const Type*> &Fields; // Type accumulator. 354 355public: 356 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) 357 : DIVisitor() 358 , SR(S) 359 , Fields(F) 360 {} 361 362 /// Apply - Set the value of each of the fields. 363 /// 364 virtual void Apply(int &Field) { 365 Fields.push_back(Type::Int32Ty); 366 } 367 virtual void Apply(unsigned &Field) { 368 Fields.push_back(Type::Int32Ty); 369 } 370 virtual void Apply(int64_t &Field) { 371 Fields.push_back(Type::Int64Ty); 372 } 373 virtual void Apply(uint64_t &Field) { 374 Fields.push_back(Type::Int64Ty); 375 } 376 virtual void Apply(bool &Field) { 377 Fields.push_back(Type::Int1Ty); 378 } 379 virtual void Apply(std::string &Field) { 380 Fields.push_back(SR.getStrPtrType()); 381 } 382 virtual void Apply(DebugInfoDesc *&Field) { 383 // FIXME - At some point should use specific type. 384 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 385 Fields.push_back(EmptyTy); 386 } 387 virtual void Apply(GlobalVariable *&Field) { 388 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 389 Fields.push_back(EmptyTy); 390 } 391 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 392 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 393 Fields.push_back(EmptyTy); 394 } 395}; 396 397//===----------------------------------------------------------------------===// 398/// DIVerifyVisitor - This DIVisitor verifies all the field types against 399/// a constant initializer. 400class DIVerifyVisitor : public DIVisitor { 401private: 402 DIVerifier &VR; // Active verifier. 403 bool IsValid; // Validity status. 404 unsigned I; // Current operand index. 405 ConstantStruct *CI; // GlobalVariable constant initializer. 406 407public: 408 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) 409 : DIVisitor() 410 , VR(V) 411 , IsValid(true) 412 , I(0) 413 , CI(cast<ConstantStruct>(GV->getInitializer())) 414 { 415 } 416 417 // Accessors. 418 bool isValid() const { return IsValid; } 419 420 /// Apply - Set the value of each of the fields. 421 /// 422 virtual void Apply(int &Field) { 423 Constant *C = CI->getOperand(I++); 424 IsValid = IsValid && isa<ConstantInt>(C); 425 } 426 virtual void Apply(unsigned &Field) { 427 Constant *C = CI->getOperand(I++); 428 IsValid = IsValid && isa<ConstantInt>(C); 429 } 430 virtual void Apply(int64_t &Field) { 431 Constant *C = CI->getOperand(I++); 432 IsValid = IsValid && isa<ConstantInt>(C); 433 } 434 virtual void Apply(uint64_t &Field) { 435 Constant *C = CI->getOperand(I++); 436 IsValid = IsValid && isa<ConstantInt>(C); 437 } 438 virtual void Apply(bool &Field) { 439 Constant *C = CI->getOperand(I++); 440 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty; 441 } 442 virtual void Apply(std::string &Field) { 443 Constant *C = CI->getOperand(I++); 444 IsValid = IsValid && 445 (!C || isStringValue(C) || C->isNullValue()); 446 } 447 virtual void Apply(DebugInfoDesc *&Field) { 448 // FIXME - Prepare the correct descriptor. 449 Constant *C = CI->getOperand(I++); 450 IsValid = IsValid && isGlobalVariable(C); 451 } 452 virtual void Apply(GlobalVariable *&Field) { 453 Constant *C = CI->getOperand(I++); 454 IsValid = IsValid && isGlobalVariable(C); 455 } 456 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 457 Constant *C = CI->getOperand(I++); 458 IsValid = IsValid && isGlobalVariable(C); 459 if (!IsValid) return; 460 461 GlobalVariable *GV = getGlobalVariable(C); 462 IsValid = IsValid && GV && GV->hasInitializer(); 463 if (!IsValid) return; 464 465 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer()); 466 IsValid = IsValid && CA; 467 if (!IsValid) return; 468 469 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) { 470 IsValid = IsValid && isGlobalVariable(CA->getOperand(i)); 471 if (!IsValid) return; 472 473 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); 474 VR.Verify(GVE); 475 } 476 } 477}; 478 479} 480 481//===----------------------------------------------------------------------===// 482 483/// TagFromGlobal - Returns the tag number from a debug info descriptor 484/// GlobalVariable. Return DIIValid if operand is not an unsigned int. 485unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) { 486 ConstantInt *C = getUIntOperand(GV, 0); 487 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) : 488 (unsigned)DW_TAG_invalid; 489} 490 491/// VersionFromGlobal - Returns the version number from a debug info 492/// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned 493/// int. 494unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) { 495 ConstantInt *C = getUIntOperand(GV, 0); 496 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) : 497 (unsigned)DW_TAG_invalid; 498} 499 500/// DescFactory - Create an instance of debug info descriptor based on Tag. 501/// Return NULL if not a recognized Tag. 502DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) { 503 switch (Tag) { 504 case DW_TAG_anchor: return new AnchorDesc(); 505 case DW_TAG_compile_unit: return new CompileUnitDesc(); 506 case DW_TAG_variable: return new GlobalVariableDesc(); 507 case DW_TAG_subprogram: return new SubprogramDesc(); 508 case DW_TAG_lexical_block: return new BlockDesc(); 509 case DW_TAG_base_type: return new BasicTypeDesc(); 510 case DW_TAG_typedef: 511 case DW_TAG_pointer_type: 512 case DW_TAG_reference_type: 513 case DW_TAG_const_type: 514 case DW_TAG_volatile_type: 515 case DW_TAG_restrict_type: 516 case DW_TAG_member: 517 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag); 518 case DW_TAG_array_type: 519 case DW_TAG_structure_type: 520 case DW_TAG_union_type: 521 case DW_TAG_enumeration_type: 522 case DW_TAG_vector_type: 523 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag); 524 case DW_TAG_subrange_type: return new SubrangeDesc(); 525 case DW_TAG_enumerator: return new EnumeratorDesc(); 526 case DW_TAG_return_variable: 527 case DW_TAG_arg_variable: 528 case DW_TAG_auto_variable: return new VariableDesc(Tag); 529 default: break; 530 } 531 return NULL; 532} 533 534/// getLinkage - get linkage appropriate for this type of descriptor. 535/// 536GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const { 537 return GlobalValue::InternalLinkage; 538} 539 540/// ApplyToFields - Target the vistor to the fields of the descriptor. 541/// 542void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) { 543 Visitor->Apply(Tag); 544} 545 546//===----------------------------------------------------------------------===// 547 548AnchorDesc::AnchorDesc() 549: DebugInfoDesc(DW_TAG_anchor) 550, AnchorTag(0) 551{} 552AnchorDesc::AnchorDesc(AnchoredDesc *D) 553: DebugInfoDesc(DW_TAG_anchor) 554, AnchorTag(D->getTag()) 555{} 556 557// Implement isa/cast/dyncast. 558bool AnchorDesc::classof(const DebugInfoDesc *D) { 559 return D->getTag() == DW_TAG_anchor; 560} 561 562/// getLinkage - get linkage appropriate for this type of descriptor. 563/// 564GlobalValue::LinkageTypes AnchorDesc::getLinkage() const { 565 return GlobalValue::LinkOnceLinkage; 566} 567 568/// ApplyToFields - Target the visitor to the fields of the TransUnitDesc. 569/// 570void AnchorDesc::ApplyToFields(DIVisitor *Visitor) { 571 DebugInfoDesc::ApplyToFields(Visitor); 572 573 Visitor->Apply(AnchorTag); 574} 575 576/// getDescString - Return a string used to compose global names and labels. A 577/// A global variable name needs to be defined for each debug descriptor that is 578/// anchored. NOTE: that each global variable named here also needs to be added 579/// to the list of names left external in the internalizer. 580/// ExternalNames.insert("llvm.dbg.compile_units"); 581/// ExternalNames.insert("llvm.dbg.global_variables"); 582/// ExternalNames.insert("llvm.dbg.subprograms"); 583const char *AnchorDesc::getDescString() const { 584 switch (AnchorTag) { 585 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString; 586 case DW_TAG_variable: return GlobalVariableDesc::AnchorString; 587 case DW_TAG_subprogram: return SubprogramDesc::AnchorString; 588 default: break; 589 } 590 591 assert(0 && "Tag does not have a case for anchor string"); 592 return ""; 593} 594 595/// getTypeString - Return a string used to label this descriptors type. 596/// 597const char *AnchorDesc::getTypeString() const { 598 return "llvm.dbg.anchor.type"; 599} 600 601#ifndef NDEBUG 602void AnchorDesc::dump() { 603 cerr << getDescString() << " " 604 << "Version(" << getVersion() << "), " 605 << "Tag(" << getTag() << "), " 606 << "AnchorTag(" << AnchorTag << ")\n"; 607} 608#endif 609 610//===----------------------------------------------------------------------===// 611 612AnchoredDesc::AnchoredDesc(unsigned T) 613: DebugInfoDesc(T) 614, Anchor(NULL) 615{} 616 617/// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. 618/// 619void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) { 620 DebugInfoDesc::ApplyToFields(Visitor); 621 622 Visitor->Apply(Anchor); 623} 624 625//===----------------------------------------------------------------------===// 626 627CompileUnitDesc::CompileUnitDesc() 628: AnchoredDesc(DW_TAG_compile_unit) 629, Language(0) 630, FileName("") 631, Directory("") 632, Producer("") 633{} 634 635// Implement isa/cast/dyncast. 636bool CompileUnitDesc::classof(const DebugInfoDesc *D) { 637 return D->getTag() == DW_TAG_compile_unit; 638} 639 640/// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. 641/// 642void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) { 643 AnchoredDesc::ApplyToFields(Visitor); 644 645 // Handle cases out of sync with compiler. 646 if (getVersion() == 0) { 647 unsigned DebugVersion; 648 Visitor->Apply(DebugVersion); 649 } 650 651 Visitor->Apply(Language); 652 Visitor->Apply(FileName); 653 Visitor->Apply(Directory); 654 Visitor->Apply(Producer); 655} 656 657/// getDescString - Return a string used to compose global names and labels. 658/// 659const char *CompileUnitDesc::getDescString() const { 660 return "llvm.dbg.compile_unit"; 661} 662 663/// getTypeString - Return a string used to label this descriptors type. 664/// 665const char *CompileUnitDesc::getTypeString() const { 666 return "llvm.dbg.compile_unit.type"; 667} 668 669/// getAnchorString - Return a string used to label this descriptor's anchor. 670/// 671const char *const CompileUnitDesc::AnchorString = "llvm.dbg.compile_units"; 672const char *CompileUnitDesc::getAnchorString() const { 673 return AnchorString; 674} 675 676#ifndef NDEBUG 677void CompileUnitDesc::dump() { 678 cerr << getDescString() << " " 679 << "Version(" << getVersion() << "), " 680 << "Tag(" << getTag() << "), " 681 << "Anchor(" << getAnchor() << "), " 682 << "Language(" << Language << "), " 683 << "FileName(\"" << FileName << "\"), " 684 << "Directory(\"" << Directory << "\"), " 685 << "Producer(\"" << Producer << "\")\n"; 686} 687#endif 688 689//===----------------------------------------------------------------------===// 690 691TypeDesc::TypeDesc(unsigned T) 692: DebugInfoDesc(T) 693, Context(NULL) 694, Name("") 695, File(NULL) 696, Line(0) 697, Size(0) 698, Align(0) 699, Offset(0) 700, Flags(0) 701{} 702 703/// ApplyToFields - Target the visitor to the fields of the TypeDesc. 704/// 705void TypeDesc::ApplyToFields(DIVisitor *Visitor) { 706 DebugInfoDesc::ApplyToFields(Visitor); 707 708 Visitor->Apply(Context); 709 Visitor->Apply(Name); 710 Visitor->Apply(File); 711 Visitor->Apply(Line); 712 Visitor->Apply(Size); 713 Visitor->Apply(Align); 714 Visitor->Apply(Offset); 715 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags); 716} 717 718/// getDescString - Return a string used to compose global names and labels. 719/// 720const char *TypeDesc::getDescString() const { 721 return "llvm.dbg.type"; 722} 723 724/// getTypeString - Return a string used to label this descriptor's type. 725/// 726const char *TypeDesc::getTypeString() const { 727 return "llvm.dbg.type.type"; 728} 729 730#ifndef NDEBUG 731void TypeDesc::dump() { 732 cerr << getDescString() << " " 733 << "Version(" << getVersion() << "), " 734 << "Tag(" << getTag() << "), " 735 << "Context(" << Context << "), " 736 << "Name(\"" << Name << "\"), " 737 << "File(" << File << "), " 738 << "Line(" << Line << "), " 739 << "Size(" << Size << "), " 740 << "Align(" << Align << "), " 741 << "Offset(" << Offset << "), " 742 << "Flags(" << Flags << ")\n"; 743} 744#endif 745 746//===----------------------------------------------------------------------===// 747 748BasicTypeDesc::BasicTypeDesc() 749: TypeDesc(DW_TAG_base_type) 750, Encoding(0) 751{} 752 753// Implement isa/cast/dyncast. 754bool BasicTypeDesc::classof(const DebugInfoDesc *D) { 755 return D->getTag() == DW_TAG_base_type; 756} 757 758/// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc. 759/// 760void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) { 761 TypeDesc::ApplyToFields(Visitor); 762 763 Visitor->Apply(Encoding); 764} 765 766/// getDescString - Return a string used to compose global names and labels. 767/// 768const char *BasicTypeDesc::getDescString() const { 769 return "llvm.dbg.basictype"; 770} 771 772/// getTypeString - Return a string used to label this descriptor's type. 773/// 774const char *BasicTypeDesc::getTypeString() const { 775 return "llvm.dbg.basictype.type"; 776} 777 778#ifndef NDEBUG 779void BasicTypeDesc::dump() { 780 cerr << getDescString() << " " 781 << "Version(" << getVersion() << "), " 782 << "Tag(" << getTag() << "), " 783 << "Context(" << getContext() << "), " 784 << "Name(\"" << getName() << "\"), " 785 << "Size(" << getSize() << "), " 786 << "Encoding(" << Encoding << ")\n"; 787} 788#endif 789 790//===----------------------------------------------------------------------===// 791 792DerivedTypeDesc::DerivedTypeDesc(unsigned T) 793: TypeDesc(T) 794, FromType(NULL) 795{} 796 797// Implement isa/cast/dyncast. 798bool DerivedTypeDesc::classof(const DebugInfoDesc *D) { 799 unsigned T = D->getTag(); 800 switch (T) { 801 case DW_TAG_typedef: 802 case DW_TAG_pointer_type: 803 case DW_TAG_reference_type: 804 case DW_TAG_const_type: 805 case DW_TAG_volatile_type: 806 case DW_TAG_restrict_type: 807 case DW_TAG_member: 808 case DW_TAG_inheritance: 809 return true; 810 default: break; 811 } 812 return false; 813} 814 815/// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc. 816/// 817void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) { 818 TypeDesc::ApplyToFields(Visitor); 819 820 Visitor->Apply(FromType); 821} 822 823/// getDescString - Return a string used to compose global names and labels. 824/// 825const char *DerivedTypeDesc::getDescString() const { 826 return "llvm.dbg.derivedtype"; 827} 828 829/// getTypeString - Return a string used to label this descriptor's type. 830/// 831const char *DerivedTypeDesc::getTypeString() const { 832 return "llvm.dbg.derivedtype.type"; 833} 834 835#ifndef NDEBUG 836void DerivedTypeDesc::dump() { 837 cerr << getDescString() << " " 838 << "Version(" << getVersion() << "), " 839 << "Tag(" << getTag() << "), " 840 << "Context(" << getContext() << "), " 841 << "Name(\"" << getName() << "\"), " 842 << "Size(" << getSize() << "), " 843 << "File(" << getFile() << "), " 844 << "Line(" << getLine() << "), " 845 << "FromType(" << FromType << ")\n"; 846} 847#endif 848 849//===----------------------------------------------------------------------===// 850 851CompositeTypeDesc::CompositeTypeDesc(unsigned T) 852: DerivedTypeDesc(T) 853, Elements() 854{} 855 856// Implement isa/cast/dyncast. 857bool CompositeTypeDesc::classof(const DebugInfoDesc *D) { 858 unsigned T = D->getTag(); 859 switch (T) { 860 case DW_TAG_array_type: 861 case DW_TAG_structure_type: 862 case DW_TAG_union_type: 863 case DW_TAG_enumeration_type: 864 case DW_TAG_vector_type: 865 case DW_TAG_subroutine_type: 866 return true; 867 default: break; 868 } 869 return false; 870} 871 872/// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc. 873/// 874void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) { 875 DerivedTypeDesc::ApplyToFields(Visitor); 876 877 Visitor->Apply(Elements); 878} 879 880/// getDescString - Return a string used to compose global names and labels. 881/// 882const char *CompositeTypeDesc::getDescString() const { 883 return "llvm.dbg.compositetype"; 884} 885 886/// getTypeString - Return a string used to label this descriptor's type. 887/// 888const char *CompositeTypeDesc::getTypeString() const { 889 return "llvm.dbg.compositetype.type"; 890} 891 892#ifndef NDEBUG 893void CompositeTypeDesc::dump() { 894 cerr << getDescString() << " " 895 << "Version(" << getVersion() << "), " 896 << "Tag(" << getTag() << "), " 897 << "Context(" << getContext() << "), " 898 << "Name(\"" << getName() << "\"), " 899 << "Size(" << getSize() << "), " 900 << "File(" << getFile() << "), " 901 << "Line(" << getLine() << "), " 902 << "FromType(" << getFromType() << "), " 903 << "Elements.size(" << Elements.size() << ")\n"; 904} 905#endif 906 907//===----------------------------------------------------------------------===// 908 909SubrangeDesc::SubrangeDesc() 910: DebugInfoDesc(DW_TAG_subrange_type) 911, Lo(0) 912, Hi(0) 913{} 914 915// Implement isa/cast/dyncast. 916bool SubrangeDesc::classof(const DebugInfoDesc *D) { 917 return D->getTag() == DW_TAG_subrange_type; 918} 919 920/// ApplyToFields - Target the visitor to the fields of the SubrangeDesc. 921/// 922void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) { 923 DebugInfoDesc::ApplyToFields(Visitor); 924 925 Visitor->Apply(Lo); 926 Visitor->Apply(Hi); 927} 928 929/// getDescString - Return a string used to compose global names and labels. 930/// 931const char *SubrangeDesc::getDescString() const { 932 return "llvm.dbg.subrange"; 933} 934 935/// getTypeString - Return a string used to label this descriptor's type. 936/// 937const char *SubrangeDesc::getTypeString() const { 938 return "llvm.dbg.subrange.type"; 939} 940 941#ifndef NDEBUG 942void SubrangeDesc::dump() { 943 cerr << getDescString() << " " 944 << "Version(" << getVersion() << "), " 945 << "Tag(" << getTag() << "), " 946 << "Lo(" << Lo << "), " 947 << "Hi(" << Hi << ")\n"; 948} 949#endif 950 951//===----------------------------------------------------------------------===// 952 953EnumeratorDesc::EnumeratorDesc() 954: DebugInfoDesc(DW_TAG_enumerator) 955, Name("") 956, Value(0) 957{} 958 959// Implement isa/cast/dyncast. 960bool EnumeratorDesc::classof(const DebugInfoDesc *D) { 961 return D->getTag() == DW_TAG_enumerator; 962} 963 964/// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc. 965/// 966void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) { 967 DebugInfoDesc::ApplyToFields(Visitor); 968 969 Visitor->Apply(Name); 970 Visitor->Apply(Value); 971} 972 973/// getDescString - Return a string used to compose global names and labels. 974/// 975const char *EnumeratorDesc::getDescString() const { 976 return "llvm.dbg.enumerator"; 977} 978 979/// getTypeString - Return a string used to label this descriptor's type. 980/// 981const char *EnumeratorDesc::getTypeString() const { 982 return "llvm.dbg.enumerator.type"; 983} 984 985#ifndef NDEBUG 986void EnumeratorDesc::dump() { 987 cerr << getDescString() << " " 988 << "Version(" << getVersion() << "), " 989 << "Tag(" << getTag() << "), " 990 << "Name(" << Name << "), " 991 << "Value(" << Value << ")\n"; 992} 993#endif 994 995//===----------------------------------------------------------------------===// 996 997VariableDesc::VariableDesc(unsigned T) 998: DebugInfoDesc(T) 999, Context(NULL) 1000, Name("") 1001, File(NULL) 1002, Line(0) 1003, TyDesc(0) 1004{} 1005 1006// Implement isa/cast/dyncast. 1007bool VariableDesc::classof(const DebugInfoDesc *D) { 1008 unsigned T = D->getTag(); 1009 switch (T) { 1010 case DW_TAG_auto_variable: 1011 case DW_TAG_arg_variable: 1012 case DW_TAG_return_variable: 1013 return true; 1014 default: break; 1015 } 1016 return false; 1017} 1018 1019/// ApplyToFields - Target the visitor to the fields of the VariableDesc. 1020/// 1021void VariableDesc::ApplyToFields(DIVisitor *Visitor) { 1022 DebugInfoDesc::ApplyToFields(Visitor); 1023 1024 Visitor->Apply(Context); 1025 Visitor->Apply(Name); 1026 Visitor->Apply(File); 1027 Visitor->Apply(Line); 1028 Visitor->Apply(TyDesc); 1029} 1030 1031/// getDescString - Return a string used to compose global names and labels. 1032/// 1033const char *VariableDesc::getDescString() const { 1034 return "llvm.dbg.variable"; 1035} 1036 1037/// getTypeString - Return a string used to label this descriptor's type. 1038/// 1039const char *VariableDesc::getTypeString() const { 1040 return "llvm.dbg.variable.type"; 1041} 1042 1043#ifndef NDEBUG 1044void VariableDesc::dump() { 1045 cerr << getDescString() << " " 1046 << "Version(" << getVersion() << "), " 1047 << "Tag(" << getTag() << "), " 1048 << "Context(" << Context << "), " 1049 << "Name(\"" << Name << "\"), " 1050 << "File(" << File << "), " 1051 << "Line(" << Line << "), " 1052 << "TyDesc(" << TyDesc << ")\n"; 1053} 1054#endif 1055 1056//===----------------------------------------------------------------------===// 1057 1058GlobalDesc::GlobalDesc(unsigned T) 1059: AnchoredDesc(T) 1060, Context(0) 1061, Name("") 1062, FullName("") 1063, LinkageName("") 1064, File(NULL) 1065, Line(0) 1066, TyDesc(NULL) 1067, IsStatic(false) 1068, IsDefinition(false) 1069{} 1070 1071/// ApplyToFields - Target the visitor to the fields of the global. 1072/// 1073void GlobalDesc::ApplyToFields(DIVisitor *Visitor) { 1074 AnchoredDesc::ApplyToFields(Visitor); 1075 1076 Visitor->Apply(Context); 1077 Visitor->Apply(Name); 1078 Visitor->Apply(FullName); 1079 Visitor->Apply(LinkageName); 1080 Visitor->Apply(File); 1081 Visitor->Apply(Line); 1082 Visitor->Apply(TyDesc); 1083 Visitor->Apply(IsStatic); 1084 Visitor->Apply(IsDefinition); 1085} 1086 1087//===----------------------------------------------------------------------===// 1088 1089GlobalVariableDesc::GlobalVariableDesc() 1090: GlobalDesc(DW_TAG_variable) 1091, Global(NULL) 1092{} 1093 1094// Implement isa/cast/dyncast. 1095bool GlobalVariableDesc::classof(const DebugInfoDesc *D) { 1096 return D->getTag() == DW_TAG_variable; 1097} 1098 1099/// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc. 1100/// 1101void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) { 1102 GlobalDesc::ApplyToFields(Visitor); 1103 1104 Visitor->Apply(Global); 1105} 1106 1107/// getDescString - Return a string used to compose global names and labels. 1108/// 1109const char *GlobalVariableDesc::getDescString() const { 1110 return "llvm.dbg.global_variable"; 1111} 1112 1113/// getTypeString - Return a string used to label this descriptors type. 1114/// 1115const char *GlobalVariableDesc::getTypeString() const { 1116 return "llvm.dbg.global_variable.type"; 1117} 1118 1119/// getAnchorString - Return a string used to label this descriptor's anchor. 1120/// 1121const char *const GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables"; 1122const char *GlobalVariableDesc::getAnchorString() const { 1123 return AnchorString; 1124} 1125 1126#ifndef NDEBUG 1127void GlobalVariableDesc::dump() { 1128 cerr << getDescString() << " " 1129 << "Version(" << getVersion() << "), " 1130 << "Tag(" << getTag() << "), " 1131 << "Anchor(" << getAnchor() << "), " 1132 << "Name(\"" << getName() << "\"), " 1133 << "FullName(\"" << getFullName() << "\"), " 1134 << "LinkageName(\"" << getLinkageName() << "\"), " 1135 << "File(" << getFile() << ")," 1136 << "Line(" << getLine() << ")," 1137 << "Type(" << getType() << "), " 1138 << "IsStatic(" << (isStatic() ? "true" : "false") << "), " 1139 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), " 1140 << "Global(" << Global << ")\n"; 1141} 1142#endif 1143 1144//===----------------------------------------------------------------------===// 1145 1146SubprogramDesc::SubprogramDesc() 1147: GlobalDesc(DW_TAG_subprogram) 1148{} 1149 1150// Implement isa/cast/dyncast. 1151bool SubprogramDesc::classof(const DebugInfoDesc *D) { 1152 return D->getTag() == DW_TAG_subprogram; 1153} 1154 1155/// ApplyToFields - Target the visitor to the fields of the 1156/// SubprogramDesc. 1157void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) { 1158 GlobalDesc::ApplyToFields(Visitor); 1159} 1160 1161/// getDescString - Return a string used to compose global names and labels. 1162/// 1163const char *SubprogramDesc::getDescString() const { 1164 return "llvm.dbg.subprogram"; 1165} 1166 1167/// getTypeString - Return a string used to label this descriptors type. 1168/// 1169const char *SubprogramDesc::getTypeString() const { 1170 return "llvm.dbg.subprogram.type"; 1171} 1172 1173/// getAnchorString - Return a string used to label this descriptor's anchor. 1174/// 1175const char *const SubprogramDesc::AnchorString = "llvm.dbg.subprograms"; 1176const char *SubprogramDesc::getAnchorString() const { 1177 return AnchorString; 1178} 1179 1180#ifndef NDEBUG 1181void SubprogramDesc::dump() { 1182 cerr << getDescString() << " " 1183 << "Version(" << getVersion() << "), " 1184 << "Tag(" << getTag() << "), " 1185 << "Anchor(" << getAnchor() << "), " 1186 << "Name(\"" << getName() << "\"), " 1187 << "FullName(\"" << getFullName() << "\"), " 1188 << "LinkageName(\"" << getLinkageName() << "\"), " 1189 << "File(" << getFile() << ")," 1190 << "Line(" << getLine() << ")," 1191 << "Type(" << getType() << "), " 1192 << "IsStatic(" << (isStatic() ? "true" : "false") << "), " 1193 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n"; 1194} 1195#endif 1196 1197//===----------------------------------------------------------------------===// 1198 1199BlockDesc::BlockDesc() 1200: DebugInfoDesc(DW_TAG_lexical_block) 1201, Context(NULL) 1202{} 1203 1204// Implement isa/cast/dyncast. 1205bool BlockDesc::classof(const DebugInfoDesc *D) { 1206 return D->getTag() == DW_TAG_lexical_block; 1207} 1208 1209/// ApplyToFields - Target the visitor to the fields of the BlockDesc. 1210/// 1211void BlockDesc::ApplyToFields(DIVisitor *Visitor) { 1212 DebugInfoDesc::ApplyToFields(Visitor); 1213 1214 Visitor->Apply(Context); 1215} 1216 1217/// getDescString - Return a string used to compose global names and labels. 1218/// 1219const char *BlockDesc::getDescString() const { 1220 return "llvm.dbg.block"; 1221} 1222 1223/// getTypeString - Return a string used to label this descriptors type. 1224/// 1225const char *BlockDesc::getTypeString() const { 1226 return "llvm.dbg.block.type"; 1227} 1228 1229#ifndef NDEBUG 1230void BlockDesc::dump() { 1231 cerr << getDescString() << " " 1232 << "Version(" << getVersion() << "), " 1233 << "Tag(" << getTag() << ")," 1234 << "Context(" << Context << ")\n"; 1235} 1236#endif 1237 1238//===----------------------------------------------------------------------===// 1239 1240DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { 1241 return Deserialize(getGlobalVariable(V)); 1242} 1243DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { 1244 // Handle NULL. 1245 if (!GV) return NULL; 1246 1247 // Check to see if it has been already deserialized. 1248 DebugInfoDesc *&Slot = GlobalDescs[GV]; 1249 if (Slot) return Slot; 1250 1251 // Get the Tag from the global. 1252 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); 1253 1254 // Create an empty instance of the correct sort. 1255 Slot = DebugInfoDesc::DescFactory(Tag); 1256 1257 // If not a user defined descriptor. 1258 if (Slot) { 1259 // Deserialize the fields. 1260 DIDeserializeVisitor DRAM(*this, GV); 1261 DRAM.ApplyToFields(Slot); 1262 } 1263 1264 return Slot; 1265} 1266 1267//===----------------------------------------------------------------------===// 1268 1269/// getStrPtrType - Return a "sbyte *" type. 1270/// 1271const PointerType *DISerializer::getStrPtrType() { 1272 // If not already defined. 1273 if (!StrPtrTy) { 1274 // Construct the pointer to signed bytes. 1275 StrPtrTy = PointerType::getUnqual(Type::Int8Ty); 1276 } 1277 1278 return StrPtrTy; 1279} 1280 1281/// getEmptyStructPtrType - Return a "{ }*" type. 1282/// 1283const PointerType *DISerializer::getEmptyStructPtrType() { 1284 // If not already defined. 1285 if (EmptyStructPtrTy) return EmptyStructPtrTy; 1286 1287 // Construct the pointer to empty structure type. 1288 const StructType *EmptyStructTy = StructType::get(NULL, NULL); 1289 1290 // Construct the pointer to empty structure type. 1291 EmptyStructPtrTy = PointerType::getUnqual(EmptyStructTy); 1292 return EmptyStructPtrTy; 1293} 1294 1295/// getTagType - Return the type describing the specified descriptor (via tag.) 1296/// 1297const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { 1298 // Attempt to get the previously defined type. 1299 StructType *&Ty = TagTypes[DD->getTag()]; 1300 1301 // If not already defined. 1302 if (!Ty) { 1303 // Set up fields vector. 1304 std::vector<const Type*> Fields; 1305 // Get types of fields. 1306 DIGetTypesVisitor GTAM(*this, Fields); 1307 GTAM.ApplyToFields(DD); 1308 1309 // Construct structured type. 1310 Ty = StructType::get(Fields); 1311 1312 // Register type name with module. 1313 M->addTypeName(DD->getTypeString(), Ty); 1314 } 1315 1316 return Ty; 1317} 1318 1319/// getString - Construct the string as constant string global. 1320/// 1321Constant *DISerializer::getString(const std::string &String) { 1322 // Check string cache for previous edition. 1323 Constant *&Slot = StringCache[String]; 1324 1325 // Return Constant if previously defined. 1326 if (Slot) return Slot; 1327 1328 // If empty string then use a sbyte* null instead. 1329 if (String.empty()) { 1330 Slot = ConstantPointerNull::get(getStrPtrType()); 1331 } else { 1332 // Construct string as an llvm constant. 1333 Constant *ConstStr = ConstantArray::get(String); 1334 1335 // Otherwise create and return a new string global. 1336 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, 1337 GlobalVariable::InternalLinkage, 1338 ConstStr, ".str", M); 1339 StrGV->setSection("llvm.metadata"); 1340 1341 // Convert to generic string pointer. 1342 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType()); 1343 } 1344 1345 return Slot; 1346 1347} 1348 1349/// Serialize - Recursively cast the specified descriptor into a GlobalVariable 1350/// so that it can be serialized to a .bc or .ll file. 1351GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { 1352 // Check if the DebugInfoDesc is already in the map. 1353 GlobalVariable *&Slot = DescGlobals[DD]; 1354 1355 // See if DebugInfoDesc exists, if so return prior GlobalVariable. 1356 if (Slot) return Slot; 1357 1358 // Get the type associated with the Tag. 1359 const StructType *Ty = getTagType(DD); 1360 1361 // Create the GlobalVariable early to prevent infinite recursion. 1362 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), 1363 NULL, DD->getDescString(), M); 1364 GV->setSection("llvm.metadata"); 1365 1366 // Insert new GlobalVariable in DescGlobals map. 1367 Slot = GV; 1368 1369 // Set up elements vector 1370 std::vector<Constant*> Elements; 1371 // Add fields. 1372 DISerializeVisitor SRAM(*this, Elements); 1373 SRAM.ApplyToFields(DD); 1374 1375 // Set the globals initializer. 1376 GV->setInitializer(ConstantStruct::get(Ty, Elements)); 1377 1378 return GV; 1379} 1380 1381/// addDescriptor - Directly connect DD with existing GV. 1382void DISerializer::addDescriptor(DebugInfoDesc *DD, 1383 GlobalVariable *GV) { 1384 DescGlobals[DD] = GV; 1385} 1386 1387//===----------------------------------------------------------------------===// 1388 1389/// Verify - Return true if the GlobalVariable appears to be a valid 1390/// serialization of a DebugInfoDesc. 1391bool DIVerifier::Verify(Value *V) { 1392 return !V || Verify(getGlobalVariable(V)); 1393} 1394bool DIVerifier::Verify(GlobalVariable *GV) { 1395 // NULLs are valid. 1396 if (!GV) return true; 1397 1398 // Check prior validity. 1399 unsigned &ValiditySlot = Validity[GV]; 1400 1401 // If visited before then use old state. 1402 if (ValiditySlot) return ValiditySlot == Valid; 1403 1404 // Assume validity for the time being (recursion.) 1405 ValiditySlot = Valid; 1406 1407 // Make sure the global is internal or link once (anchor.) 1408 if (GV->getLinkage() != GlobalValue::InternalLinkage && 1409 GV->getLinkage() != GlobalValue::LinkOnceLinkage) { 1410 ValiditySlot = Invalid; 1411 return false; 1412 } 1413 1414 // Get the Tag. 1415 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); 1416 1417 // Check for user defined descriptors. 1418 if (Tag == DW_TAG_invalid) { 1419 ValiditySlot = Valid; 1420 return true; 1421 } 1422 1423 // Get the Version. 1424 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV); 1425 1426 // Check for version mismatch. 1427 if (Version != LLVMDebugVersion) { 1428 ValiditySlot = Invalid; 1429 return false; 1430 } 1431 1432 // Construct an empty DebugInfoDesc. 1433 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); 1434 1435 // Allow for user defined descriptors. 1436 if (!DD) return true; 1437 1438 // Get the initializer constant. 1439 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); 1440 1441 // Get the operand count. 1442 unsigned N = CI->getNumOperands(); 1443 1444 // Get the field count. 1445 unsigned &CountSlot = Counts[Tag]; 1446 if (!CountSlot) { 1447 // Check the operand count to the field count 1448 DICountVisitor CTAM; 1449 CTAM.ApplyToFields(DD); 1450 CountSlot = CTAM.getCount(); 1451 } 1452 1453 // Field count must be at most equal operand count. 1454 if (CountSlot > N) { 1455 delete DD; 1456 ValiditySlot = Invalid; 1457 return false; 1458 } 1459 1460 // Check each field for valid type. 1461 DIVerifyVisitor VRAM(*this, GV); 1462 VRAM.ApplyToFields(DD); 1463 1464 // Release empty DebugInfoDesc. 1465 delete DD; 1466 1467 // If fields are not valid. 1468 if (!VRAM.isValid()) { 1469 ValiditySlot = Invalid; 1470 return false; 1471 } 1472 1473 return true; 1474} 1475 1476/// isVerified - Return true if the specified GV has already been 1477/// verified as a debug information descriptor. 1478bool DIVerifier::isVerified(GlobalVariable *GV) { 1479 unsigned &ValiditySlot = Validity[GV]; 1480 if (ValiditySlot) return ValiditySlot == Valid; 1481 return false; 1482} 1483 1484//===----------------------------------------------------------------------===// 1485 1486DebugScope::~DebugScope() { 1487 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i]; 1488 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j]; 1489} 1490 1491//===----------------------------------------------------------------------===// 1492 1493MachineModuleInfo::MachineModuleInfo() 1494: ImmutablePass((intptr_t)&ID) 1495, DR() 1496, VR() 1497, CompileUnits() 1498, Directories() 1499, SourceFiles() 1500, Lines() 1501, LabelIDList() 1502, ScopeMap() 1503, RootScope(NULL) 1504, FrameMoves() 1505, LandingPads() 1506, Personalities() 1507, CallsEHReturn(0) 1508, CallsUnwindInit(0) 1509{ 1510 // Always emit "no personality" info 1511 Personalities.push_back(NULL); 1512} 1513MachineModuleInfo::~MachineModuleInfo() { 1514 1515} 1516 1517/// doInitialization - Initialize the state for a new module. 1518/// 1519bool MachineModuleInfo::doInitialization() { 1520 return false; 1521} 1522 1523/// doFinalization - Tear down the state after completion of a module. 1524/// 1525bool MachineModuleInfo::doFinalization() { 1526 return false; 1527} 1528 1529/// BeginFunction - Begin gathering function meta information. 1530/// 1531void MachineModuleInfo::BeginFunction(MachineFunction *MF) { 1532 // Coming soon. 1533} 1534 1535/// EndFunction - Discard function meta information. 1536/// 1537void MachineModuleInfo::EndFunction() { 1538 // Clean up scope information. 1539 if (RootScope) { 1540 delete RootScope; 1541 ScopeMap.clear(); 1542 RootScope = NULL; 1543 } 1544 1545 // Clean up line info. 1546 Lines.clear(); 1547 1548 // Clean up frame info. 1549 FrameMoves.clear(); 1550 1551 // Clean up exception info. 1552 LandingPads.clear(); 1553 TypeInfos.clear(); 1554 FilterIds.clear(); 1555 FilterEnds.clear(); 1556 CallsEHReturn = 0; 1557 CallsUnwindInit = 0; 1558} 1559 1560/// getDescFor - Convert a Value to a debug information descriptor. 1561/// 1562// FIXME - use new Value type when available. 1563DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) { 1564 return DR.Deserialize(V); 1565} 1566 1567/// AnalyzeModule - Scan the module for global debug information. 1568/// 1569void MachineModuleInfo::AnalyzeModule(Module &M) { 1570 SetupCompileUnits(M); 1571 1572 // Insert functions in the llvm.used array into UsedFunctions. 1573 GlobalVariable *GV = M.getGlobalVariable("llvm.used"); 1574 if (!GV || !GV->hasInitializer()) return; 1575 1576 // Should be an array of 'i8*'. 1577 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 1578 if (InitList == 0) return; 1579 1580 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 1581 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i))) 1582 if (CE->getOpcode() == Instruction::BitCast) 1583 if (Function *F = dyn_cast<Function>(CE->getOperand(0))) 1584 UsedFunctions.insert(F); 1585 } 1586} 1587 1588/// SetupCompileUnits - Set up the unique vector of compile units. 1589/// 1590void MachineModuleInfo::SetupCompileUnits(Module &M) { 1591 std::vector<CompileUnitDesc *> CU; 1592 getAnchoredDescriptors<CompileUnitDesc>(M, CU); 1593 1594 for (unsigned i = 0, N = CU.size(); i < N; i++) { 1595 CompileUnits.insert(CU[i]); 1596 } 1597} 1598 1599/// getCompileUnits - Return a vector of debug compile units. 1600/// 1601const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{ 1602 return CompileUnits; 1603} 1604 1605/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the 1606/// named GlobalVariable. 1607void 1608MachineModuleInfo::getGlobalVariablesUsing(Module &M, 1609 const std::string &RootName, 1610 std::vector<GlobalVariable*>&Result){ 1611 return ::getGlobalVariablesUsing(M, RootName, Result); 1612} 1613 1614/// RecordSourceLine - Records location information and associates it with a 1615/// debug label. Returns a unique label ID used to generate a label and 1616/// provide correspondence to the source line list. 1617unsigned MachineModuleInfo::RecordSourceLine(unsigned Line, unsigned Column, 1618 unsigned Source) { 1619 unsigned ID = NextLabelID(); 1620 Lines.push_back(SourceLineInfo(Line, Column, Source, ID)); 1621 return ID; 1622} 1623 1624/// RecordSource - Register a source file with debug info. Returns an source 1625/// ID. 1626unsigned MachineModuleInfo::RecordSource(const std::string &Directory, 1627 const std::string &Source) { 1628 unsigned DirectoryID = Directories.insert(Directory); 1629 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source)); 1630} 1631unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) { 1632 return RecordSource(CompileUnit->getDirectory(), 1633 CompileUnit->getFileName()); 1634} 1635 1636/// RecordRegionStart - Indicate the start of a region. 1637/// 1638unsigned MachineModuleInfo::RecordRegionStart(Value *V) { 1639 // FIXME - need to be able to handle split scopes because of bb cloning. 1640 DebugInfoDesc *ScopeDesc = DR.Deserialize(V); 1641 DebugScope *Scope = getOrCreateScope(ScopeDesc); 1642 unsigned ID = NextLabelID(); 1643 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID); 1644 return ID; 1645} 1646 1647/// RecordRegionEnd - Indicate the end of a region. 1648/// 1649unsigned MachineModuleInfo::RecordRegionEnd(Value *V) { 1650 // FIXME - need to be able to handle split scopes because of bb cloning. 1651 DebugInfoDesc *ScopeDesc = DR.Deserialize(V); 1652 DebugScope *Scope = getOrCreateScope(ScopeDesc); 1653 unsigned ID = NextLabelID(); 1654 Scope->setEndLabelID(ID); 1655 return ID; 1656} 1657 1658/// RecordVariable - Indicate the declaration of a local variable. 1659/// 1660void MachineModuleInfo::RecordVariable(GlobalValue *GV, unsigned FrameIndex) { 1661 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(GV)); 1662 DebugScope *Scope = getOrCreateScope(VD->getContext()); 1663 DebugVariable *DV = new DebugVariable(VD, FrameIndex); 1664 Scope->AddVariable(DV); 1665} 1666 1667/// getOrCreateScope - Returns the scope associated with the given descriptor. 1668/// 1669DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) { 1670 DebugScope *&Slot = ScopeMap[ScopeDesc]; 1671 if (!Slot) { 1672 // FIXME - breaks down when the context is an inlined function. 1673 DebugInfoDesc *ParentDesc = NULL; 1674 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) { 1675 ParentDesc = Block->getContext(); 1676 } 1677 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL; 1678 Slot = new DebugScope(Parent, ScopeDesc); 1679 if (Parent) { 1680 Parent->AddScope(Slot); 1681 } else if (RootScope) { 1682 // FIXME - Add inlined function scopes to the root so we can delete 1683 // them later. Long term, handle inlined functions properly. 1684 RootScope->AddScope(Slot); 1685 } else { 1686 // First function is top level function. 1687 RootScope = Slot; 1688 } 1689 } 1690 return Slot; 1691} 1692 1693//===-EH-------------------------------------------------------------------===// 1694 1695/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 1696/// specified MachineBasicBlock. 1697LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo 1698 (MachineBasicBlock *LandingPad) { 1699 unsigned N = LandingPads.size(); 1700 for (unsigned i = 0; i < N; ++i) { 1701 LandingPadInfo &LP = LandingPads[i]; 1702 if (LP.LandingPadBlock == LandingPad) 1703 return LP; 1704 } 1705 1706 LandingPads.push_back(LandingPadInfo(LandingPad)); 1707 return LandingPads[N]; 1708} 1709 1710/// addInvoke - Provide the begin and end labels of an invoke style call and 1711/// associate it with a try landing pad block. 1712void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, 1713 unsigned BeginLabel, unsigned EndLabel) { 1714 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1715 LP.BeginLabels.push_back(BeginLabel); 1716 LP.EndLabels.push_back(EndLabel); 1717} 1718 1719/// addLandingPad - Provide the label of a try LandingPad block. 1720/// 1721unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { 1722 unsigned LandingPadLabel = NextLabelID(); 1723 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1724 LP.LandingPadLabel = LandingPadLabel; 1725 return LandingPadLabel; 1726} 1727 1728/// addPersonality - Provide the personality function for the exception 1729/// information. 1730void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, 1731 Function *Personality) { 1732 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1733 LP.Personality = Personality; 1734 1735 for (unsigned i = 0; i < Personalities.size(); ++i) 1736 if (Personalities[i] == Personality) 1737 return; 1738 1739 Personalities.push_back(Personality); 1740} 1741 1742/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 1743/// 1744void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, 1745 std::vector<GlobalVariable *> &TyInfo) { 1746 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1747 for (unsigned N = TyInfo.size(); N; --N) 1748 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 1749} 1750 1751/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 1752/// 1753void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, 1754 std::vector<GlobalVariable *> &TyInfo) { 1755 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1756 std::vector<unsigned> IdsInFilter(TyInfo.size()); 1757 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 1758 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 1759 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 1760} 1761 1762/// addCleanup - Add a cleanup action for a landing pad. 1763/// 1764void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { 1765 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 1766 LP.TypeIds.push_back(0); 1767} 1768 1769/// TidyLandingPads - Remap landing pad labels and remove any deleted landing 1770/// pads. 1771void MachineModuleInfo::TidyLandingPads() { 1772 for (unsigned i = 0; i != LandingPads.size(); ) { 1773 LandingPadInfo &LandingPad = LandingPads[i]; 1774 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel); 1775 1776 // Special case: we *should* emit LPs with null LP MBB. This indicates 1777 // "nounwind" case. 1778 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 1779 LandingPads.erase(LandingPads.begin() + i); 1780 continue; 1781 } 1782 1783 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { 1784 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); 1785 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); 1786 1787 if (!BeginLabel || !EndLabel) { 1788 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 1789 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 1790 continue; 1791 } 1792 1793 LandingPad.BeginLabels[j] = BeginLabel; 1794 LandingPad.EndLabels[j] = EndLabel; 1795 ++j; 1796 } 1797 1798 // Remove landing pads with no try-ranges. 1799 if (LandingPads[i].BeginLabels.empty()) { 1800 LandingPads.erase(LandingPads.begin() + i); 1801 continue; 1802 } 1803 1804 // If there is no landing pad, ensure that the list of typeids is empty. 1805 // If the only typeid is a cleanup, this is the same as having no typeids. 1806 if (!LandingPad.LandingPadBlock || 1807 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 1808 LandingPad.TypeIds.clear(); 1809 1810 ++i; 1811 } 1812} 1813 1814/// getTypeIDFor - Return the type id for the specified typeinfo. This is 1815/// function wide. 1816unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { 1817 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 1818 if (TypeInfos[i] == TI) return i + 1; 1819 1820 TypeInfos.push_back(TI); 1821 return TypeInfos.size(); 1822} 1823 1824/// getFilterIDFor - Return the filter id for the specified typeinfos. This is 1825/// function wide. 1826int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { 1827 // If the new filter coincides with the tail of an existing filter, then 1828 // re-use the existing filter. Folding filters more than this requires 1829 // re-ordering filters and/or their elements - probably not worth it. 1830 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 1831 E = FilterEnds.end(); I != E; ++I) { 1832 unsigned i = *I, j = TyIds.size(); 1833 1834 while (i && j) 1835 if (FilterIds[--i] != TyIds[--j]) 1836 goto try_next; 1837 1838 if (!j) 1839 // The new filter coincides with range [i, end) of the existing filter. 1840 return -(1 + i); 1841 1842try_next:; 1843 } 1844 1845 // Add the new filter. 1846 int FilterID = -(1 + FilterIds.size()); 1847 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 1848 for (unsigned I = 0, N = TyIds.size(); I != N; ++I) 1849 FilterIds.push_back(TyIds[I]); 1850 FilterEnds.push_back(FilterIds.size()); 1851 FilterIds.push_back(0); // terminator 1852 return FilterID; 1853} 1854 1855/// getPersonality - Return the personality function for the current function. 1856Function *MachineModuleInfo::getPersonality() const { 1857 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per 1858 // function 1859 return !LandingPads.empty() ? LandingPads[0].Personality : NULL; 1860} 1861 1862/// getPersonalityIndex - Return unique index for current personality 1863/// function. NULL personality function should always get zero index. 1864unsigned MachineModuleInfo::getPersonalityIndex() const { 1865 const Function* Personality = NULL; 1866 1867 // Scan landing pads. If there is at least one non-NULL personality - use it. 1868 for (unsigned i = 0; i != LandingPads.size(); ++i) 1869 if (LandingPads[i].Personality) { 1870 Personality = LandingPads[i].Personality; 1871 break; 1872 } 1873 1874 for (unsigned i = 0; i < Personalities.size(); ++i) { 1875 if (Personalities[i] == Personality) 1876 return i; 1877 } 1878 1879 // This should never happen 1880 assert(0 && "Personality function should be set!"); 1881 return 0; 1882} 1883 1884//===----------------------------------------------------------------------===// 1885/// DebugLabelFolding pass - This pass prunes out redundant labels. This allows 1886/// a info consumer to determine if the range of two labels is empty, by seeing 1887/// if the labels map to the same reduced label. 1888 1889namespace llvm { 1890 1891struct DebugLabelFolder : public MachineFunctionPass { 1892 static char ID; 1893 DebugLabelFolder() : MachineFunctionPass((intptr_t)&ID) {} 1894 1895 virtual bool runOnMachineFunction(MachineFunction &MF); 1896 virtual const char *getPassName() const { return "Label Folder"; } 1897}; 1898 1899char DebugLabelFolder::ID = 0; 1900 1901bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) { 1902 // Get machine module info. 1903 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>(); 1904 if (!MMI) return false; 1905 1906 // Track if change is made. 1907 bool MadeChange = false; 1908 // No prior label to begin. 1909 unsigned PriorLabel = 0; 1910 1911 // Iterate through basic blocks. 1912 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); 1913 BB != E; ++BB) { 1914 // Iterate through instructions. 1915 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 1916 // Is it a label. 1917 if (I->isDebugLabel()) { 1918 // The label ID # is always operand #0, an immediate. 1919 unsigned NextLabel = I->getOperand(0).getImm(); 1920 1921 // If there was an immediate prior label. 1922 if (PriorLabel) { 1923 // Remap the current label to prior label. 1924 MMI->RemapLabel(NextLabel, PriorLabel); 1925 // Delete the current label. 1926 I = BB->erase(I); 1927 // Indicate a change has been made. 1928 MadeChange = true; 1929 continue; 1930 } else { 1931 // Start a new round. 1932 PriorLabel = NextLabel; 1933 } 1934 } else { 1935 // No consecutive labels. 1936 PriorLabel = 0; 1937 } 1938 1939 ++I; 1940 } 1941 } 1942 1943 return MadeChange; 1944} 1945 1946FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } 1947 1948} 1949 1950