1//===--- DIBuilder.cpp - Debug Information Builder ------------------------===// 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// This file implements the DIBuilder. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Analysis/DIBuilder.h" 15#include "llvm/Analysis/DebugInfo.h" 16#include "llvm/Constants.h" 17#include "llvm/IntrinsicInst.h" 18#include "llvm/Module.h" 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/Support/Dwarf.h" 21 22using namespace llvm; 23using namespace llvm::dwarf; 24 25static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) { 26 assert((Tag & LLVMDebugVersionMask) == 0 && 27 "Tag too large for debug encoding!"); 28 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion); 29} 30 31DIBuilder::DIBuilder(Module &m) 32 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0), 33 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0), 34 ValueFn(0) 35{} 36 37/// finalize - Construct any deferred debug info descriptors. 38void DIBuilder::finalize() { 39 DIArray Enums = getOrCreateArray(AllEnumTypes); 40 DIType(TempEnumTypes).replaceAllUsesWith(Enums); 41 42 DIArray RetainTypes = getOrCreateArray(AllRetainTypes); 43 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes); 44 45 DIArray SPs = getOrCreateArray(AllSubprograms); 46 DIType(TempSubprograms).replaceAllUsesWith(SPs); 47 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { 48 DISubprogram SP(SPs.getElement(i)); 49 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) { 50 SmallVector<Value *, 4> Variables; 51 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii) 52 Variables.push_back(NMD->getOperand(ii)); 53 if (MDNode *Temp = SP.getVariablesNodes()) { 54 DIArray AV = getOrCreateArray(Variables); 55 DIType(Temp).replaceAllUsesWith(AV); 56 } 57 NMD->eraseFromParent(); 58 } 59 } 60 61 DIArray GVs = getOrCreateArray(AllGVs); 62 DIType(TempGVs).replaceAllUsesWith(GVs); 63} 64 65/// getNonCompileUnitScope - If N is compile unit return NULL otherwise return 66/// N. 67static MDNode *getNonCompileUnitScope(MDNode *N) { 68 if (DIDescriptor(N).isCompileUnit()) 69 return NULL; 70 return N; 71} 72 73/// createCompileUnit - A CompileUnit provides an anchor for all debugging 74/// information generated during this instance of compilation. 75void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, 76 StringRef Directory, StringRef Producer, 77 bool isOptimized, StringRef Flags, 78 unsigned RunTimeVer) { 79 assert (Lang <= dwarf::DW_LANG_D && Lang >= dwarf::DW_LANG_C89 80 && "Invalid Language tag"); 81 assert (!Filename.empty() 82 && "Unable to create compile unit without filename"); 83 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; 84 TempEnumTypes = MDNode::getTemporary(VMContext, TElts); 85 Value *THElts[] = { TempEnumTypes }; 86 MDNode *EnumHolder = MDNode::get(VMContext, THElts); 87 88 TempRetainTypes = MDNode::getTemporary(VMContext, TElts); 89 Value *TRElts[] = { TempRetainTypes }; 90 MDNode *RetainHolder = MDNode::get(VMContext, TRElts); 91 92 TempSubprograms = MDNode::getTemporary(VMContext, TElts); 93 Value *TSElts[] = { TempSubprograms }; 94 MDNode *SPHolder = MDNode::get(VMContext, TSElts); 95 96 TempGVs = MDNode::getTemporary(VMContext, TElts); 97 Value *TVElts[] = { TempGVs }; 98 MDNode *GVHolder = MDNode::get(VMContext, TVElts); 99 100 Value *Elts[] = { 101 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit), 102 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 103 ConstantInt::get(Type::getInt32Ty(VMContext), Lang), 104 MDString::get(VMContext, Filename), 105 MDString::get(VMContext, Directory), 106 MDString::get(VMContext, Producer), 107 // Deprecate isMain field. 108 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain 109 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), 110 MDString::get(VMContext, Flags), 111 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer), 112 EnumHolder, 113 RetainHolder, 114 SPHolder, 115 GVHolder 116 }; 117 TheCU = DICompileUnit(MDNode::get(VMContext, Elts)); 118 119 // Create a named metadata so that it is easier to find cu in a module. 120 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); 121 NMD->addOperand(TheCU); 122} 123 124/// createFile - Create a file descriptor to hold debugging information 125/// for a file. 126DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) { 127 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit"); 128 assert(!Filename.empty() && "Unable to create file without name"); 129 Value *Elts[] = { 130 GetTagConstant(VMContext, dwarf::DW_TAG_file_type), 131 MDString::get(VMContext, Filename), 132 MDString::get(VMContext, Directory), 133 NULL // TheCU 134 }; 135 return DIFile(MDNode::get(VMContext, Elts)); 136} 137 138/// createEnumerator - Create a single enumerator value. 139DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) { 140 assert(!Name.empty() && "Unable to create enumerator without name"); 141 Value *Elts[] = { 142 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator), 143 MDString::get(VMContext, Name), 144 ConstantInt::get(Type::getInt64Ty(VMContext), Val) 145 }; 146 return DIEnumerator(MDNode::get(VMContext, Elts)); 147} 148 149/// createNullPtrType - Create C++0x nullptr type. 150DIType DIBuilder::createNullPtrType(StringRef Name) { 151 assert(!Name.empty() && "Unable to create type without name"); 152 // nullptr is encoded in DIBasicType format. Line number, filename, 153 // ,size, alignment, offset and flags are always empty here. 154 Value *Elts[] = { 155 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type), 156 NULL, //TheCU, 157 MDString::get(VMContext, Name), 158 NULL, // Filename 159 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 160 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 161 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 162 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 163 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags; 164 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Encoding 165 }; 166 return DIType(MDNode::get(VMContext, Elts)); 167} 168 169/// createBasicType - Create debugging information entry for a basic 170/// type, e.g 'char'. 171DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, 172 uint64_t AlignInBits, 173 unsigned Encoding) { 174 assert(!Name.empty() && "Unable to create type without name"); 175 // Basic types are encoded in DIBasicType format. Line number, filename, 176 // offset and flags are always empty here. 177 Value *Elts[] = { 178 GetTagConstant(VMContext, dwarf::DW_TAG_base_type), 179 NULL, //TheCU, 180 MDString::get(VMContext, Name), 181 NULL, // Filename 182 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 183 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 184 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 185 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 186 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags; 187 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding) 188 }; 189 return DIType(MDNode::get(VMContext, Elts)); 190} 191 192/// createQaulifiedType - Create debugging information entry for a qualified 193/// type, e.g. 'const int'. 194DIType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) { 195 // Qualified types are encoded in DIDerivedType format. 196 Value *Elts[] = { 197 GetTagConstant(VMContext, Tag), 198 NULL, //TheCU, 199 MDString::get(VMContext, StringRef()), // Empty name. 200 NULL, // Filename 201 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 202 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 203 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 204 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 205 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags 206 FromTy 207 }; 208 return DIType(MDNode::get(VMContext, Elts)); 209} 210 211/// createPointerType - Create debugging information entry for a pointer. 212DIType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits, 213 uint64_t AlignInBits, StringRef Name) { 214 // Pointer types are encoded in DIDerivedType format. 215 Value *Elts[] = { 216 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type), 217 NULL, //TheCU, 218 MDString::get(VMContext, Name), 219 NULL, // Filename 220 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 221 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 222 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 223 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 224 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags 225 PointeeTy 226 }; 227 return DIType(MDNode::get(VMContext, Elts)); 228} 229 230/// createReferenceType - Create debugging information entry for a reference. 231DIType DIBuilder::createReferenceType(DIType RTy) { 232 assert(RTy.Verify() && "Unable to create reference type"); 233 // References are encoded in DIDerivedType format. 234 Value *Elts[] = { 235 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type), 236 NULL, // TheCU, 237 NULL, // Name 238 NULL, // Filename 239 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 240 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 241 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 242 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 243 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags 244 RTy 245 }; 246 return DIType(MDNode::get(VMContext, Elts)); 247} 248 249/// createTypedef - Create debugging information entry for a typedef. 250DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File, 251 unsigned LineNo, DIDescriptor Context) { 252 // typedefs are encoded in DIDerivedType format. 253 assert(Ty.Verify() && "Invalid typedef type!"); 254 Value *Elts[] = { 255 GetTagConstant(VMContext, dwarf::DW_TAG_typedef), 256 getNonCompileUnitScope(Context), 257 MDString::get(VMContext, Name), 258 File, 259 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), 260 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 261 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 262 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 263 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags 264 Ty 265 }; 266 return DIType(MDNode::get(VMContext, Elts)); 267} 268 269/// createFriend - Create debugging information entry for a 'friend'. 270DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) { 271 // typedefs are encoded in DIDerivedType format. 272 assert(Ty.Verify() && "Invalid type!"); 273 assert(FriendTy.Verify() && "Invalid friend type!"); 274 Value *Elts[] = { 275 GetTagConstant(VMContext, dwarf::DW_TAG_friend), 276 Ty, 277 NULL, // Name 278 Ty.getFile(), 279 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 280 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 281 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 282 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset 283 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags 284 FriendTy 285 }; 286 return DIType(MDNode::get(VMContext, Elts)); 287} 288 289/// createInheritance - Create debugging information entry to establish 290/// inheritance relationship between two types. 291DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy, 292 uint64_t BaseOffset, unsigned Flags) { 293 assert(Ty.Verify() && "Unable to create inheritance"); 294 // TAG_inheritance is encoded in DIDerivedType format. 295 Value *Elts[] = { 296 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance), 297 Ty, 298 NULL, // Name 299 Ty.getFile(), 300 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line 301 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size 302 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align 303 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset), 304 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 305 BaseTy 306 }; 307 return DIType(MDNode::get(VMContext, Elts)); 308} 309 310/// createMemberType - Create debugging information entry for a member. 311DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name, 312 DIFile File, unsigned LineNumber, 313 uint64_t SizeInBits, uint64_t AlignInBits, 314 uint64_t OffsetInBits, unsigned Flags, 315 DIType Ty) { 316 // TAG_member is encoded in DIDerivedType format. 317 Value *Elts[] = { 318 GetTagConstant(VMContext, dwarf::DW_TAG_member), 319 getNonCompileUnitScope(Scope), 320 MDString::get(VMContext, Name), 321 File, 322 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 323 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 324 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 325 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), 326 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 327 Ty 328 }; 329 return DIType(MDNode::get(VMContext, Elts)); 330} 331 332/// createObjCIVar - Create debugging information entry for Objective-C 333/// instance variable. 334DIType DIBuilder::createObjCIVar(StringRef Name, 335 DIFile File, unsigned LineNumber, 336 uint64_t SizeInBits, uint64_t AlignInBits, 337 uint64_t OffsetInBits, unsigned Flags, 338 DIType Ty, StringRef PropertyName, 339 StringRef GetterName, StringRef SetterName, 340 unsigned PropertyAttributes) { 341 // TAG_member is encoded in DIDerivedType format. 342 Value *Elts[] = { 343 GetTagConstant(VMContext, dwarf::DW_TAG_member), 344 getNonCompileUnitScope(File), 345 MDString::get(VMContext, Name), 346 File, 347 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 348 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 349 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 350 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), 351 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 352 Ty, 353 MDString::get(VMContext, PropertyName), 354 MDString::get(VMContext, GetterName), 355 MDString::get(VMContext, SetterName), 356 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes) 357 }; 358 return DIType(MDNode::get(VMContext, Elts)); 359} 360 361/// createClassType - Create debugging information entry for a class. 362DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, 363 DIFile File, unsigned LineNumber, 364 uint64_t SizeInBits, uint64_t AlignInBits, 365 uint64_t OffsetInBits, unsigned Flags, 366 DIType DerivedFrom, DIArray Elements, 367 MDNode *VTableHoder, MDNode *TemplateParams) { 368 // TAG_class_type is encoded in DICompositeType format. 369 Value *Elts[] = { 370 GetTagConstant(VMContext, dwarf::DW_TAG_class_type), 371 getNonCompileUnitScope(Context), 372 MDString::get(VMContext, Name), 373 File, 374 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 375 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 376 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 377 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits), 378 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 379 DerivedFrom, 380 Elements, 381 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 382 VTableHoder, 383 TemplateParams 384 }; 385 return DIType(MDNode::get(VMContext, Elts)); 386} 387 388/// createTemplateTypeParameter - Create debugging information for template 389/// type parameter. 390DITemplateTypeParameter 391DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name, 392 DIType Ty, MDNode *File, unsigned LineNo, 393 unsigned ColumnNo) { 394 Value *Elts[] = { 395 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter), 396 getNonCompileUnitScope(Context), 397 MDString::get(VMContext, Name), 398 Ty, 399 File, 400 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), 401 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo) 402 }; 403 return DITemplateTypeParameter(MDNode::get(VMContext, Elts)); 404} 405 406/// createTemplateValueParameter - Create debugging information for template 407/// value parameter. 408DITemplateValueParameter 409DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name, 410 DIType Ty, uint64_t Val, 411 MDNode *File, unsigned LineNo, 412 unsigned ColumnNo) { 413 Value *Elts[] = { 414 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter), 415 getNonCompileUnitScope(Context), 416 MDString::get(VMContext, Name), 417 Ty, 418 ConstantInt::get(Type::getInt64Ty(VMContext), Val), 419 File, 420 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), 421 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo) 422 }; 423 return DITemplateValueParameter(MDNode::get(VMContext, Elts)); 424} 425 426/// createStructType - Create debugging information entry for a struct. 427DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name, 428 DIFile File, unsigned LineNumber, 429 uint64_t SizeInBits, uint64_t AlignInBits, 430 unsigned Flags, DIArray Elements, 431 unsigned RunTimeLang) { 432 // TAG_structure_type is encoded in DICompositeType format. 433 Value *Elts[] = { 434 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type), 435 getNonCompileUnitScope(Context), 436 MDString::get(VMContext, Name), 437 File, 438 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 439 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 440 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 441 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 442 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 443 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 444 Elements, 445 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), 446 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 447 }; 448 return DIType(MDNode::get(VMContext, Elts)); 449} 450 451/// createUnionType - Create debugging information entry for an union. 452DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name, 453 DIFile File, 454 unsigned LineNumber, uint64_t SizeInBits, 455 uint64_t AlignInBits, unsigned Flags, 456 DIArray Elements, unsigned RunTimeLang) { 457 // TAG_union_type is encoded in DICompositeType format. 458 Value *Elts[] = { 459 GetTagConstant(VMContext, dwarf::DW_TAG_union_type), 460 getNonCompileUnitScope(Scope), 461 MDString::get(VMContext, Name), 462 File, 463 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 464 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 465 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 466 ConstantInt::get(Type::getInt64Ty(VMContext), 0), 467 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 468 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 469 Elements, 470 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), 471 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 472 }; 473 return DIType(MDNode::get(VMContext, Elts)); 474} 475 476/// createSubroutineType - Create subroutine type. 477DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) { 478 // TAG_subroutine_type is encoded in DICompositeType format. 479 Value *Elts[] = { 480 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type), 481 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 482 MDString::get(VMContext, ""), 483 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 484 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 485 ConstantInt::get(Type::getInt64Ty(VMContext), 0), 486 ConstantInt::get(Type::getInt64Ty(VMContext), 0), 487 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 488 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 489 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 490 ParameterTypes, 491 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 492 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 493 }; 494 return DIType(MDNode::get(VMContext, Elts)); 495} 496 497/// createEnumerationType - Create debugging information entry for an 498/// enumeration. 499DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name, 500 DIFile File, unsigned LineNumber, 501 uint64_t SizeInBits, 502 uint64_t AlignInBits, 503 DIArray Elements) { 504 // TAG_enumeration_type is encoded in DICompositeType format. 505 Value *Elts[] = { 506 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type), 507 getNonCompileUnitScope(Scope), 508 MDString::get(VMContext, Name), 509 File, 510 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 511 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), 512 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 513 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 514 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 515 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 516 Elements, 517 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 518 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 519 }; 520 MDNode *Node = MDNode::get(VMContext, Elts); 521 AllEnumTypes.push_back(Node); 522 return DIType(Node); 523} 524 525/// createArrayType - Create debugging information entry for an array. 526DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits, 527 DIType Ty, DIArray Subscripts) { 528 // TAG_array_type is encoded in DICompositeType format. 529 Value *Elts[] = { 530 GetTagConstant(VMContext, dwarf::DW_TAG_array_type), 531 NULL, //TheCU, 532 MDString::get(VMContext, ""), 533 NULL, //TheCU, 534 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 535 ConstantInt::get(Type::getInt64Ty(VMContext), Size), 536 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 537 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 538 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 539 Ty, 540 Subscripts, 541 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 542 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 543 }; 544 return DIType(MDNode::get(VMContext, Elts)); 545} 546 547/// createVectorType - Create debugging information entry for a vector. 548DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits, 549 DIType Ty, DIArray Subscripts) { 550 // TAG_vector_type is encoded in DICompositeType format. 551 Value *Elts[] = { 552 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type), 553 NULL, //TheCU, 554 MDString::get(VMContext, ""), 555 NULL, //TheCU, 556 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 557 ConstantInt::get(Type::getInt64Ty(VMContext), Size), 558 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), 559 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 560 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 561 Ty, 562 Subscripts, 563 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 564 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 565 }; 566 return DIType(MDNode::get(VMContext, Elts)); 567} 568 569/// createArtificialType - Create a new DIType with "artificial" flag set. 570DIType DIBuilder::createArtificialType(DIType Ty) { 571 if (Ty.isArtificial()) 572 return Ty; 573 574 SmallVector<Value *, 9> Elts; 575 MDNode *N = Ty; 576 assert (N && "Unexpected input DIType!"); 577 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 578 if (Value *V = N->getOperand(i)) 579 Elts.push_back(V); 580 else 581 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))); 582 } 583 584 unsigned CurFlags = Ty.getFlags(); 585 CurFlags = CurFlags | DIType::FlagArtificial; 586 587 // Flags are stored at this slot. 588 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags); 589 590 return DIType(MDNode::get(VMContext, Elts)); 591} 592 593/// retainType - Retain DIType in a module even if it is not referenced 594/// through debug info anchors. 595void DIBuilder::retainType(DIType T) { 596 AllRetainTypes.push_back(T); 597} 598 599/// createUnspecifiedParameter - Create unspeicified type descriptor 600/// for the subroutine type. 601DIDescriptor DIBuilder::createUnspecifiedParameter() { 602 Value *Elts[] = { 603 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters) 604 }; 605 return DIDescriptor(MDNode::get(VMContext, Elts)); 606} 607 608/// createTemporaryType - Create a temporary forward-declared type. 609DIType DIBuilder::createTemporaryType() { 610 // Give the temporary MDNode a tag. It doesn't matter what tag we 611 // use here as long as DIType accepts it. 612 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; 613 MDNode *Node = MDNode::getTemporary(VMContext, Elts); 614 return DIType(Node); 615} 616 617/// createTemporaryType - Create a temporary forward-declared type. 618DIType DIBuilder::createTemporaryType(DIFile F) { 619 // Give the temporary MDNode a tag. It doesn't matter what tag we 620 // use here as long as DIType accepts it. 621 Value *Elts[] = { 622 GetTagConstant(VMContext, DW_TAG_base_type), 623 TheCU, 624 NULL, 625 F 626 }; 627 MDNode *Node = MDNode::getTemporary(VMContext, Elts); 628 return DIType(Node); 629} 630 631/// getOrCreateArray - Get a DIArray, create one if required. 632DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) { 633 if (Elements.empty()) { 634 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)); 635 return DIArray(MDNode::get(VMContext, Null)); 636 } 637 return DIArray(MDNode::get(VMContext, Elements)); 638} 639 640/// getOrCreateSubrange - Create a descriptor for a value range. This 641/// implicitly uniques the values returned. 642DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Hi) { 643 Value *Elts[] = { 644 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type), 645 ConstantInt::get(Type::getInt64Ty(VMContext), Lo), 646 ConstantInt::get(Type::getInt64Ty(VMContext), Hi) 647 }; 648 649 return DISubrange(MDNode::get(VMContext, Elts)); 650} 651 652/// createGlobalVariable - Create a new descriptor for the specified global. 653DIGlobalVariable DIBuilder:: 654createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber, 655 DIType Ty, bool isLocalToUnit, llvm::Value *Val) { 656 Value *Elts[] = { 657 GetTagConstant(VMContext, dwarf::DW_TAG_variable), 658 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 659 NULL, // TheCU, 660 MDString::get(VMContext, Name), 661 MDString::get(VMContext, Name), 662 MDString::get(VMContext, Name), 663 F, 664 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 665 Ty, 666 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), 667 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ 668 Val 669 }; 670 MDNode *Node = MDNode::get(VMContext, Elts); 671 AllGVs.push_back(Node); 672 return DIGlobalVariable(Node); 673} 674 675/// createStaticVariable - Create a new descriptor for the specified static 676/// variable. 677DIGlobalVariable DIBuilder:: 678createStaticVariable(DIDescriptor Context, StringRef Name, 679 StringRef LinkageName, DIFile F, unsigned LineNumber, 680 DIType Ty, bool isLocalToUnit, llvm::Value *Val) { 681 Value *Elts[] = { 682 GetTagConstant(VMContext, dwarf::DW_TAG_variable), 683 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 684 getNonCompileUnitScope(Context), 685 MDString::get(VMContext, Name), 686 MDString::get(VMContext, Name), 687 MDString::get(VMContext, LinkageName), 688 F, 689 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), 690 Ty, 691 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), 692 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ 693 Val 694 }; 695 MDNode *Node = MDNode::get(VMContext, Elts); 696 AllGVs.push_back(Node); 697 return DIGlobalVariable(Node); 698} 699 700/// createVariable - Create a new descriptor for the specified variable. 701DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, 702 StringRef Name, DIFile File, 703 unsigned LineNo, DIType Ty, 704 bool AlwaysPreserve, unsigned Flags, 705 unsigned ArgNo) { 706 Value *Elts[] = { 707 GetTagConstant(VMContext, Tag), 708 getNonCompileUnitScope(Scope), 709 MDString::get(VMContext, Name), 710 File, 711 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))), 712 Ty, 713 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 714 Constant::getNullValue(Type::getInt32Ty(VMContext)), 715 }; 716 MDNode *Node = MDNode::get(VMContext, Elts); 717 if (AlwaysPreserve) { 718 // The optimizer may remove local variable. If there is an interest 719 // to preserve variable info in such situation then stash it in a 720 // named mdnode. 721 DISubprogram Fn(getDISubprogram(Scope)); 722 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn); 723 FnLocals->addOperand(Node); 724 } 725 return DIVariable(Node); 726} 727 728/// createComplexVariable - Create a new descriptor for the specified variable 729/// which has a complex address expression for its address. 730DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope, 731 StringRef Name, DIFile F, 732 unsigned LineNo, 733 DIType Ty, ArrayRef<Value *> Addr, 734 unsigned ArgNo) { 735 SmallVector<Value *, 15> Elts; 736 Elts.push_back(GetTagConstant(VMContext, Tag)); 737 Elts.push_back(getNonCompileUnitScope(Scope)), 738 Elts.push_back(MDString::get(VMContext, Name)); 739 Elts.push_back(F); 740 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), 741 (LineNo | (ArgNo << 24)))); 742 Elts.push_back(Ty); 743 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))); 744 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))); 745 Elts.append(Addr.begin(), Addr.end()); 746 747 return DIVariable(MDNode::get(VMContext, Elts)); 748} 749 750/// createFunction - Create a new descriptor for the specified function. 751DISubprogram DIBuilder::createFunction(DIDescriptor Context, 752 StringRef Name, 753 StringRef LinkageName, 754 DIFile File, unsigned LineNo, 755 DIType Ty, 756 bool isLocalToUnit, bool isDefinition, 757 unsigned Flags, bool isOptimized, 758 Function *Fn, 759 MDNode *TParams, 760 MDNode *Decl) { 761 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; 762 MDNode *Temp = MDNode::getTemporary(VMContext, TElts); 763 Value *TVElts[] = { Temp }; 764 MDNode *THolder = MDNode::get(VMContext, TVElts); 765 766 Value *Elts[] = { 767 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram), 768 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 769 getNonCompileUnitScope(Context), 770 MDString::get(VMContext, Name), 771 MDString::get(VMContext, Name), 772 MDString::get(VMContext, LinkageName), 773 File, 774 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), 775 Ty, 776 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit), 777 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition), 778 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 779 ConstantInt::get(Type::getInt32Ty(VMContext), 0), 780 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 781 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 782 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), 783 Fn, 784 TParams, 785 Decl, 786 THolder 787 }; 788 MDNode *Node = MDNode::get(VMContext, Elts); 789 790 // Create a named metadata so that we do not lose this mdnode. 791 AllSubprograms.push_back(Node); 792 return DISubprogram(Node); 793} 794 795/// createMethod - Create a new descriptor for the specified C++ method. 796DISubprogram DIBuilder::createMethod(DIDescriptor Context, 797 StringRef Name, 798 StringRef LinkageName, 799 DIFile F, 800 unsigned LineNo, DIType Ty, 801 bool isLocalToUnit, 802 bool isDefinition, 803 unsigned VK, unsigned VIndex, 804 MDNode *VTableHolder, 805 unsigned Flags, 806 bool isOptimized, 807 Function *Fn, 808 MDNode *TParam) { 809 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; 810 MDNode *Temp = MDNode::getTemporary(VMContext, TElts); 811 Value *TVElts[] = { Temp }; 812 MDNode *THolder = MDNode::get(VMContext, TVElts); 813 814 Value *Elts[] = { 815 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram), 816 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 817 getNonCompileUnitScope(Context), 818 MDString::get(VMContext, Name), 819 MDString::get(VMContext, Name), 820 MDString::get(VMContext, LinkageName), 821 F, 822 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), 823 Ty, 824 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit), 825 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition), 826 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK), 827 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex), 828 VTableHolder, 829 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), 830 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), 831 Fn, 832 TParam, 833 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), 834 THolder 835 }; 836 MDNode *Node = MDNode::get(VMContext, Elts); 837 return DISubprogram(Node); 838} 839 840/// createNameSpace - This creates new descriptor for a namespace 841/// with the specified parent scope. 842DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name, 843 DIFile File, unsigned LineNo) { 844 Value *Elts[] = { 845 GetTagConstant(VMContext, dwarf::DW_TAG_namespace), 846 getNonCompileUnitScope(Scope), 847 MDString::get(VMContext, Name), 848 File, 849 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo) 850 }; 851 return DINameSpace(MDNode::get(VMContext, Elts)); 852} 853 854/// createLexicalBlockFile - This creates a new MDNode that encapsulates 855/// an existing scope with a new filename. 856DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope, 857 DIFile File) { 858 Value *Elts[] = { 859 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block), 860 Scope, 861 File 862 }; 863 return DILexicalBlockFile(MDNode::get(VMContext, Elts)); 864} 865 866DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File, 867 unsigned Line, unsigned Col) { 868 // Defeat MDNode uniqing for lexical blocks by using unique id. 869 static unsigned int unique_id = 0; 870 Value *Elts[] = { 871 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block), 872 getNonCompileUnitScope(Scope), 873 ConstantInt::get(Type::getInt32Ty(VMContext), Line), 874 ConstantInt::get(Type::getInt32Ty(VMContext), Col), 875 File, 876 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++) 877 }; 878 return DILexicalBlock(MDNode::get(VMContext, Elts)); 879} 880 881/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call. 882Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, 883 Instruction *InsertBefore) { 884 assert(Storage && "no storage passed to dbg.declare"); 885 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare"); 886 if (!DeclareFn) 887 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); 888 889 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo }; 890 return CallInst::Create(DeclareFn, Args, "", InsertBefore); 891} 892 893/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call. 894Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, 895 BasicBlock *InsertAtEnd) { 896 assert(Storage && "no storage passed to dbg.declare"); 897 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare"); 898 if (!DeclareFn) 899 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); 900 901 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo }; 902 903 // If this block already has a terminator then insert this intrinsic 904 // before the terminator. 905 if (TerminatorInst *T = InsertAtEnd->getTerminator()) 906 return CallInst::Create(DeclareFn, Args, "", T); 907 else 908 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd); 909} 910 911/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. 912Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, 913 DIVariable VarInfo, 914 Instruction *InsertBefore) { 915 assert(V && "no value passed to dbg.value"); 916 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); 917 if (!ValueFn) 918 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); 919 920 Value *Args[] = { MDNode::get(V->getContext(), V), 921 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), 922 VarInfo }; 923 return CallInst::Create(ValueFn, Args, "", InsertBefore); 924} 925 926/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. 927Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, 928 DIVariable VarInfo, 929 BasicBlock *InsertAtEnd) { 930 assert(V && "no value passed to dbg.value"); 931 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); 932 if (!ValueFn) 933 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); 934 935 Value *Args[] = { MDNode::get(V->getContext(), V), 936 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), 937 VarInfo }; 938 return CallInst::Create(ValueFn, Args, "", InsertAtEnd); 939} 940