LLVM API Documentation

DIBuilder.cpp
Go to the documentation of this file.
00001 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the DIBuilder.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/DIBuilder.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/IR/Constants.h"
00017 #include "llvm/IR/DebugInfo.h"
00018 #include "llvm/IR/IntrinsicInst.h"
00019 #include "llvm/IR/Module.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/Dwarf.h"
00022 
00023 using namespace llvm;
00024 using namespace llvm::dwarf;
00025 
00026 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
00027   assert((Tag & LLVMDebugVersionMask) == 0 &&
00028          "Tag too large for debug encoding!");
00029   return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
00030 }
00031 
00032 DIBuilder::DIBuilder(Module &m)
00033     : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
00034       TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
00035       DeclareFn(nullptr), ValueFn(nullptr) {}
00036 
00037 /// finalize - Construct any deferred debug info descriptors.
00038 void DIBuilder::finalize() {
00039   DIArray Enums = getOrCreateArray(AllEnumTypes);
00040   DIType(TempEnumTypes).replaceAllUsesWith(Enums);
00041 
00042   SmallVector<Value *, 16> RetainValues;
00043   // Declarations and definitions of the same type may be retained. Some
00044   // clients RAUW these pairs, leaving duplicates in the retained types
00045   // list. Use a set to remove the duplicates while we transform the
00046   // TrackingVHs back into Values.
00047   SmallPtrSet<Value *, 16> RetainSet;
00048   for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
00049     if (RetainSet.insert(AllRetainTypes[I]))
00050       RetainValues.push_back(AllRetainTypes[I]);
00051   DIArray RetainTypes = getOrCreateArray(RetainValues);
00052   DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
00053 
00054   DIArray SPs = getOrCreateArray(AllSubprograms);
00055   DIType(TempSubprograms).replaceAllUsesWith(SPs);
00056   for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
00057     DISubprogram SP(SPs.getElement(i));
00058     SmallVector<Value *, 4> Variables;
00059     if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
00060       for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
00061         Variables.push_back(NMD->getOperand(ii));
00062       NMD->eraseFromParent();
00063     }
00064     if (MDNode *Temp = SP.getVariablesNodes()) {
00065       DIArray AV = getOrCreateArray(Variables);
00066       DIType(Temp).replaceAllUsesWith(AV);
00067     }
00068   }
00069 
00070   DIArray GVs = getOrCreateArray(AllGVs);
00071   DIType(TempGVs).replaceAllUsesWith(GVs);
00072 
00073   SmallVector<Value *, 16> RetainValuesI;
00074   for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
00075     RetainValuesI.push_back(AllImportedModules[I]);
00076   DIArray IMs = getOrCreateArray(RetainValuesI);
00077   DIType(TempImportedModules).replaceAllUsesWith(IMs);
00078 }
00079 
00080 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
00081 /// N.
00082 static MDNode *getNonCompileUnitScope(MDNode *N) {
00083   if (DIDescriptor(N).isCompileUnit())
00084     return nullptr;
00085   return N;
00086 }
00087 
00088 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
00089                                   StringRef Directory) {
00090   assert(!Filename.empty() && "Unable to create file without name");
00091   Value *Pair[] = {
00092     MDString::get(VMContext, Filename),
00093     MDString::get(VMContext, Directory)
00094   };
00095   return MDNode::get(VMContext, Pair);
00096 }
00097 
00098 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
00099 /// information generated during this instance of compilation.
00100 DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
00101                                            StringRef Directory,
00102                                            StringRef Producer, bool isOptimized,
00103                                            StringRef Flags, unsigned RunTimeVer,
00104                                            StringRef SplitName,
00105                                            DebugEmissionKind Kind,
00106                                            bool EmitDebugInfo) {
00107 
00108   assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
00109           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
00110          "Invalid Language tag");
00111   assert(!Filename.empty() &&
00112          "Unable to create compile unit without filename");
00113   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
00114   TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
00115 
00116   TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
00117 
00118   TempSubprograms = MDNode::getTemporary(VMContext, TElts);
00119 
00120   TempGVs = MDNode::getTemporary(VMContext, TElts);
00121 
00122   TempImportedModules = MDNode::getTemporary(VMContext, TElts);
00123 
00124   Value *Elts[] = {
00125     GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
00126     createFilePathPair(VMContext, Filename, Directory),
00127     ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
00128     MDString::get(VMContext, Producer),
00129     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
00130     MDString::get(VMContext, Flags),
00131     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
00132     TempEnumTypes,
00133     TempRetainTypes,
00134     TempSubprograms,
00135     TempGVs,
00136     TempImportedModules,
00137     MDString::get(VMContext, SplitName),
00138     ConstantInt::get(Type::getInt32Ty(VMContext), Kind)
00139   };
00140 
00141   MDNode *CUNode = MDNode::get(VMContext, Elts);
00142 
00143   // Create a named metadata so that it is easier to find cu in a module.
00144   // Note that we only generate this when the caller wants to actually
00145   // emit debug information. When we are only interested in tracking
00146   // source line locations throughout the backend, we prevent codegen from
00147   // emitting debug info in the final output by not generating llvm.dbg.cu.
00148   if (EmitDebugInfo) {
00149     NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
00150     NMD->addOperand(CUNode);
00151   }
00152 
00153   return DICompileUnit(CUNode);
00154 }
00155 
00156 static DIImportedEntity
00157 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
00158                      Value *NS, unsigned Line, StringRef Name,
00159                      SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
00160   const MDNode *R;
00161   if (Name.empty()) {
00162     Value *Elts[] = {
00163       GetTagConstant(C, Tag),
00164       Context,
00165       NS,
00166       ConstantInt::get(Type::getInt32Ty(C), Line),
00167     };
00168     R = MDNode::get(C, Elts);
00169   } else {
00170     Value *Elts[] = {
00171       GetTagConstant(C, Tag),
00172       Context,
00173       NS,
00174       ConstantInt::get(Type::getInt32Ty(C), Line),
00175       MDString::get(C, Name)
00176     };
00177     R = MDNode::get(C, Elts);
00178   }
00179   DIImportedEntity M(R);
00180   assert(M.Verify() && "Imported module should be valid");
00181   AllImportedModules.push_back(TrackingVH<MDNode>(M));
00182   return M;
00183 }
00184 
00185 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00186                                                  DINameSpace NS,
00187                                                  unsigned Line) {
00188   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
00189                                 Context, NS, Line, StringRef(), AllImportedModules);
00190 }
00191 
00192 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00193                                                  DIImportedEntity NS,
00194                                                  unsigned Line) {
00195   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
00196                                 Context, NS, Line, StringRef(), AllImportedModules);
00197 }
00198 
00199 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
00200                                                       DIScope Decl,
00201                                                       unsigned Line, StringRef Name) {
00202   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
00203                                 Context, Decl.getRef(), Line, Name,
00204                                 AllImportedModules);
00205 }
00206 
00207 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
00208                                                       DIImportedEntity Imp,
00209                                                       unsigned Line, StringRef Name) {
00210   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
00211                                 Context, Imp, Line, Name, AllImportedModules);
00212 }
00213 
00214 /// createFile - Create a file descriptor to hold debugging information
00215 /// for a file.
00216 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
00217   Value *Elts[] = {
00218     GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
00219     createFilePathPair(VMContext, Filename, Directory)
00220   };
00221   return DIFile(MDNode::get(VMContext, Elts));
00222 }
00223 
00224 /// createEnumerator - Create a single enumerator value.
00225 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
00226   assert(!Name.empty() && "Unable to create enumerator without name");
00227   Value *Elts[] = {
00228     GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
00229     MDString::get(VMContext, Name),
00230     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
00231   };
00232   return DIEnumerator(MDNode::get(VMContext, Elts));
00233 }
00234 
00235 /// \brief Create a DWARF unspecified type.
00236 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
00237   assert(!Name.empty() && "Unable to create type without name");
00238   // Unspecified types are encoded in DIBasicType format. Line number, filename,
00239   // size, alignment, offset and flags are always empty here.
00240   Value *Elts[] = {
00241     GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
00242     nullptr, // Filename
00243     nullptr, // Unused
00244     MDString::get(VMContext, Name),
00245     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00246     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00247     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00248     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00249     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
00250     ConstantInt::get(Type::getInt32Ty(VMContext), 0)  // Encoding
00251   };
00252   return DIBasicType(MDNode::get(VMContext, Elts));
00253 }
00254 
00255 /// \brief Create C++11 nullptr type.
00256 DIBasicType DIBuilder::createNullPtrType() {
00257   return createUnspecifiedType("decltype(nullptr)");
00258 }
00259 
00260 /// createBasicType - Create debugging information entry for a basic
00261 /// type, e.g 'char'.
00262 DIBasicType
00263 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
00264                            uint64_t AlignInBits, unsigned Encoding) {
00265   assert(!Name.empty() && "Unable to create type without name");
00266   // Basic types are encoded in DIBasicType format. Line number, filename,
00267   // offset and flags are always empty here.
00268   Value *Elts[] = {
00269     GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
00270     nullptr, // File/directory name
00271     nullptr, // Unused
00272     MDString::get(VMContext, Name),
00273     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00274     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00275     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00276     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00277     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
00278     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
00279   };
00280   return DIBasicType(MDNode::get(VMContext, Elts));
00281 }
00282 
00283 /// createQualifiedType - Create debugging information entry for a qualified
00284 /// type, e.g. 'const int'.
00285 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
00286   // Qualified types are encoded in DIDerivedType format.
00287   Value *Elts[] = {
00288     GetTagConstant(VMContext, Tag),
00289     nullptr, // Filename
00290     nullptr, // Unused
00291     MDString::get(VMContext, StringRef()), // Empty name.
00292     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00293     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00294     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00295     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00296     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00297     FromTy.getRef()
00298   };
00299   return DIDerivedType(MDNode::get(VMContext, Elts));
00300 }
00301 
00302 /// createPointerType - Create debugging information entry for a pointer.
00303 DIDerivedType
00304 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
00305                              uint64_t AlignInBits, StringRef Name) {
00306   // Pointer types are encoded in DIDerivedType format.
00307   Value *Elts[] = {
00308     GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
00309     nullptr, // Filename
00310     nullptr, // Unused
00311     MDString::get(VMContext, Name),
00312     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00313     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00314     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00315     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00316     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00317     PointeeTy.getRef()
00318   };
00319   return DIDerivedType(MDNode::get(VMContext, Elts));
00320 }
00321 
00322 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
00323                                                  DIType Base) {
00324   // Pointer types are encoded in DIDerivedType format.
00325   Value *Elts[] = {
00326     GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
00327     nullptr, // Filename
00328     nullptr, // Unused
00329     nullptr,
00330     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00331     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00332     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00333     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00334     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00335     PointeeTy.getRef(),
00336     Base.getRef()
00337   };
00338   return DIDerivedType(MDNode::get(VMContext, Elts));
00339 }
00340 
00341 /// createReferenceType - Create debugging information entry for a reference
00342 /// type.
00343 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
00344   assert(RTy.isType() && "Unable to create reference type");
00345   // References are encoded in DIDerivedType format.
00346   Value *Elts[] = {
00347     GetTagConstant(VMContext, Tag),
00348     nullptr, // Filename
00349     nullptr, // TheCU,
00350     nullptr, // Name
00351     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00352     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00353     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00354     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00355     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00356     RTy.getRef()
00357   };
00358   return DIDerivedType(MDNode::get(VMContext, Elts));
00359 }
00360 
00361 /// createTypedef - Create debugging information entry for a typedef.
00362 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
00363                                        unsigned LineNo, DIDescriptor Context) {
00364   // typedefs are encoded in DIDerivedType format.
00365   Value *Elts[] = {
00366     GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
00367     File.getFileNode(),
00368     DIScope(getNonCompileUnitScope(Context)).getRef(),
00369     MDString::get(VMContext, Name),
00370     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00371     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00372     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00373     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00374     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00375     Ty.getRef()
00376   };
00377   return DIDerivedType(MDNode::get(VMContext, Elts));
00378 }
00379 
00380 /// createFriend - Create debugging information entry for a 'friend'.
00381 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
00382   // typedefs are encoded in DIDerivedType format.
00383   assert(Ty.isType() && "Invalid type!");
00384   assert(FriendTy.isType() && "Invalid friend type!");
00385   Value *Elts[] = {
00386     GetTagConstant(VMContext, dwarf::DW_TAG_friend),
00387     nullptr,
00388     Ty.getRef(),
00389     nullptr, // Name
00390     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00391     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00392     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00393     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00394     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00395     FriendTy.getRef()
00396   };
00397   return DIDerivedType(MDNode::get(VMContext, Elts));
00398 }
00399 
00400 /// createInheritance - Create debugging information entry to establish
00401 /// inheritance relationship between two types.
00402 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
00403                                            uint64_t BaseOffset,
00404                                            unsigned Flags) {
00405   assert(Ty.isType() && "Unable to create inheritance");
00406   // TAG_inheritance is encoded in DIDerivedType format.
00407   Value *Elts[] = {
00408     GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
00409     nullptr,
00410     Ty.getRef(),
00411     nullptr, // Name
00412     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00413     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00414     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00415     ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
00416     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00417     BaseTy.getRef()
00418   };
00419   return DIDerivedType(MDNode::get(VMContext, Elts));
00420 }
00421 
00422 /// createMemberType - Create debugging information entry for a member.
00423 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
00424                                           DIFile File, unsigned LineNumber,
00425                                           uint64_t SizeInBits,
00426                                           uint64_t AlignInBits,
00427                                           uint64_t OffsetInBits, unsigned Flags,
00428                                           DIType Ty) {
00429   // TAG_member is encoded in DIDerivedType format.
00430   Value *Elts[] = {
00431     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00432     File.getFileNode(),
00433     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00434     MDString::get(VMContext, Name),
00435     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00436     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00437     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00438     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00439     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00440     Ty.getRef()
00441   };
00442   return DIDerivedType(MDNode::get(VMContext, Elts));
00443 }
00444 
00445 /// createStaticMemberType - Create debugging information entry for a
00446 /// C++ static data member.
00447 DIDerivedType
00448 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
00449                                   DIFile File, unsigned LineNumber,
00450                                   DIType Ty, unsigned Flags,
00451                                   llvm::Value *Val) {
00452   // TAG_member is encoded in DIDerivedType format.
00453   Flags |= DIDescriptor::FlagStaticMember;
00454   Value *Elts[] = {
00455     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00456     File.getFileNode(),
00457     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00458     MDString::get(VMContext, Name),
00459     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00460     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00461     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00462     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00463     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00464     Ty.getRef(),
00465     Val
00466   };
00467   return DIDerivedType(MDNode::get(VMContext, Elts));
00468 }
00469 
00470 /// createObjCIVar - Create debugging information entry for Objective-C
00471 /// instance variable.
00472 DIDerivedType
00473 DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber,
00474                           uint64_t SizeInBits, uint64_t AlignInBits,
00475                           uint64_t OffsetInBits, unsigned Flags, DIType Ty,
00476                           StringRef PropertyName, StringRef GetterName,
00477                           StringRef SetterName, unsigned PropertyAttributes) {
00478   // TAG_member is encoded in DIDerivedType format.
00479   Value *Elts[] = {
00480     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00481     File.getFileNode(),
00482     getNonCompileUnitScope(File),
00483     MDString::get(VMContext, Name),
00484     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00485     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00486     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00487     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00488     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00489     Ty,
00490     MDString::get(VMContext, PropertyName),
00491     MDString::get(VMContext, GetterName),
00492     MDString::get(VMContext, SetterName),
00493     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
00494   };
00495   return DIDerivedType(MDNode::get(VMContext, Elts));
00496 }
00497 
00498 /// createObjCIVar - Create debugging information entry for Objective-C
00499 /// instance variable.
00500 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
00501                                         unsigned LineNumber,
00502                                         uint64_t SizeInBits,
00503                                         uint64_t AlignInBits,
00504                                         uint64_t OffsetInBits, unsigned Flags,
00505                                         DIType Ty, MDNode *PropertyNode) {
00506   // TAG_member is encoded in DIDerivedType format.
00507   Value *Elts[] = {
00508     GetTagConstant(VMContext, dwarf::DW_TAG_member),
00509     File.getFileNode(),
00510     getNonCompileUnitScope(File),
00511     MDString::get(VMContext, Name),
00512     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00513     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00514     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00515     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
00516     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00517     Ty,
00518     PropertyNode
00519   };
00520   return DIDerivedType(MDNode::get(VMContext, Elts));
00521 }
00522 
00523 /// createObjCProperty - Create debugging information entry for Objective-C
00524 /// property.
00525 DIObjCProperty
00526 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
00527                               StringRef GetterName, StringRef SetterName,
00528                               unsigned PropertyAttributes, DIType Ty) {
00529   Value *Elts[] = {
00530     GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
00531     MDString::get(VMContext, Name),
00532     File,
00533     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00534     MDString::get(VMContext, GetterName),
00535     MDString::get(VMContext, SetterName),
00536     ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
00537     Ty
00538   };
00539   return DIObjCProperty(MDNode::get(VMContext, Elts));
00540 }
00541 
00542 /// createTemplateTypeParameter - Create debugging information for template
00543 /// type parameter.
00544 DITemplateTypeParameter
00545 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
00546                                        DIType Ty, MDNode *File, unsigned LineNo,
00547                                        unsigned ColumnNo) {
00548   Value *Elts[] = {
00549     GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
00550     DIScope(getNonCompileUnitScope(Context)).getRef(),
00551     MDString::get(VMContext, Name),
00552     Ty.getRef(),
00553     File,
00554     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00555     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00556   };
00557   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
00558 }
00559 
00560 DITemplateValueParameter
00561 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
00562                                         StringRef Name, DIType Ty,
00563                                         Value *Val, MDNode *File,
00564                                         unsigned LineNo,
00565                                         unsigned ColumnNo) {
00566   Value *Elts[] = {
00567     GetTagConstant(VMContext, Tag),
00568     DIScope(getNonCompileUnitScope(Context)).getRef(),
00569     MDString::get(VMContext, Name),
00570     Ty.getRef(),
00571     Val,
00572     File,
00573     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
00574     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
00575   };
00576   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
00577 }
00578 
00579 /// createTemplateValueParameter - Create debugging information for template
00580 /// value parameter.
00581 DITemplateValueParameter
00582 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
00583                                         DIType Ty, Value *Val,
00584                                         MDNode *File, unsigned LineNo,
00585                                         unsigned ColumnNo) {
00586   return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
00587                                       Context, Name, Ty, Val, File, LineNo,
00588                                       ColumnNo);
00589 }
00590 
00591 DITemplateValueParameter
00592 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
00593                                            DIType Ty, StringRef Val,
00594                                            MDNode *File, unsigned LineNo,
00595                                            unsigned ColumnNo) {
00596   return createTemplateValueParameter(
00597       dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
00598       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
00599 }
00600 
00601 DITemplateValueParameter
00602 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
00603                                        DIType Ty, DIArray Val,
00604                                        MDNode *File, unsigned LineNo,
00605                                        unsigned ColumnNo) {
00606   return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
00607                                       Context, Name, Ty, Val, File, LineNo,
00608                                       ColumnNo);
00609 }
00610 
00611 /// createClassType - Create debugging information entry for a class.
00612 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
00613                                            DIFile File, unsigned LineNumber,
00614                                            uint64_t SizeInBits,
00615                                            uint64_t AlignInBits,
00616                                            uint64_t OffsetInBits,
00617                                            unsigned Flags, DIType DerivedFrom,
00618                                            DIArray Elements,
00619                                            DIType VTableHolder,
00620                                            MDNode *TemplateParams,
00621                                            StringRef UniqueIdentifier) {
00622   assert((!Context || Context.isScope() || Context.isType()) &&
00623          "createClassType should be called with a valid Context");
00624   // TAG_class_type is encoded in DICompositeType format.
00625   Value *Elts[] = {
00626     GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
00627     File.getFileNode(),
00628     DIScope(getNonCompileUnitScope(Context)).getRef(),
00629     MDString::get(VMContext, Name),
00630     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00631     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00632     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00633     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
00634     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00635     DerivedFrom.getRef(),
00636     Elements,
00637     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00638     VTableHolder.getRef(),
00639     TemplateParams,
00640     UniqueIdentifier.empty() ? nullptr
00641                              : MDString::get(VMContext, UniqueIdentifier)
00642   };
00643   DICompositeType R(MDNode::get(VMContext, Elts));
00644   assert(R.isCompositeType() &&
00645          "createClassType should return a DICompositeType");
00646   if (!UniqueIdentifier.empty())
00647     retainType(R);
00648   return R;
00649 }
00650 
00651 /// createStructType - Create debugging information entry for a struct.
00652 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
00653                                             StringRef Name, DIFile File,
00654                                             unsigned LineNumber,
00655                                             uint64_t SizeInBits,
00656                                             uint64_t AlignInBits,
00657                                             unsigned Flags, DIType DerivedFrom,
00658                                             DIArray Elements,
00659                                             unsigned RunTimeLang,
00660                                             DIType VTableHolder,
00661                                             StringRef UniqueIdentifier) {
00662  // TAG_structure_type is encoded in DICompositeType format.
00663   Value *Elts[] = {
00664     GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
00665     File.getFileNode(),
00666     DIScope(getNonCompileUnitScope(Context)).getRef(),
00667     MDString::get(VMContext, Name),
00668     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00669     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00670     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00671     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00672     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00673     DerivedFrom.getRef(),
00674     Elements,
00675     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00676     VTableHolder.getRef(),
00677     nullptr,
00678     UniqueIdentifier.empty() ? nullptr
00679                              : MDString::get(VMContext, UniqueIdentifier)
00680   };
00681   DICompositeType R(MDNode::get(VMContext, Elts));
00682   assert(R.isCompositeType() &&
00683          "createStructType should return a DICompositeType");
00684   if (!UniqueIdentifier.empty())
00685     retainType(R);
00686   return R;
00687 }
00688 
00689 /// createUnionType - Create debugging information entry for an union.
00690 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
00691                                            DIFile File, unsigned LineNumber,
00692                                            uint64_t SizeInBits,
00693                                            uint64_t AlignInBits, unsigned Flags,
00694                                            DIArray Elements,
00695                                            unsigned RunTimeLang,
00696                                            StringRef UniqueIdentifier) {
00697   // TAG_union_type is encoded in DICompositeType format.
00698   Value *Elts[] = {
00699     GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
00700     File.getFileNode(),
00701     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00702     MDString::get(VMContext, Name),
00703     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00704     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00705     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00706     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00707     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
00708     nullptr,
00709     Elements,
00710     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
00711     nullptr,
00712     nullptr,
00713     UniqueIdentifier.empty() ? nullptr
00714                              : MDString::get(VMContext, UniqueIdentifier)
00715   };
00716   DICompositeType R(MDNode::get(VMContext, Elts));
00717   if (!UniqueIdentifier.empty())
00718     retainType(R);
00719   return R;
00720 }
00721 
00722 /// createSubroutineType - Create subroutine type.
00723 DISubroutineType DIBuilder::createSubroutineType(DIFile File,
00724                                                  DITypeArray ParameterTypes,
00725                                                  unsigned Flags) {
00726   // TAG_subroutine_type is encoded in DICompositeType format.
00727   Value *Elts[] = {
00728     GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
00729     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00730     nullptr,
00731     MDString::get(VMContext, ""),
00732     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00733     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
00734     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
00735     ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
00736     ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
00737     nullptr,
00738     ParameterTypes,
00739     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00740     nullptr,
00741     nullptr,
00742     nullptr  // Type Identifer
00743   };
00744   return DISubroutineType(MDNode::get(VMContext, Elts));
00745 }
00746 
00747 /// createEnumerationType - Create debugging information entry for an
00748 /// enumeration.
00749 DICompositeType DIBuilder::createEnumerationType(
00750     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
00751     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
00752     DIType UnderlyingType, StringRef UniqueIdentifier) {
00753   // TAG_enumeration_type is encoded in DICompositeType format.
00754   Value *Elts[] = {
00755     GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
00756     File.getFileNode(),
00757     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00758     MDString::get(VMContext, Name),
00759     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00760     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00761     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00762     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00763     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00764     UnderlyingType.getRef(),
00765     Elements,
00766     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00767     nullptr,
00768     nullptr,
00769     UniqueIdentifier.empty() ? nullptr
00770                              : MDString::get(VMContext, UniqueIdentifier)
00771   };
00772   DICompositeType CTy(MDNode::get(VMContext, Elts));
00773   AllEnumTypes.push_back(CTy);
00774   if (!UniqueIdentifier.empty())
00775     retainType(CTy);
00776   return CTy;
00777 }
00778 
00779 /// createArrayType - Create debugging information entry for an array.
00780 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
00781                                            DIType Ty, DIArray Subscripts) {
00782   // TAG_array_type is encoded in DICompositeType format.
00783   Value *Elts[] = {
00784     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00785     nullptr, // Filename/Directory,
00786     nullptr, // Unused
00787     MDString::get(VMContext, ""),
00788     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00789     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00790     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00791     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00792     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
00793     Ty.getRef(),
00794     Subscripts,
00795     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00796     nullptr,
00797     nullptr,
00798     nullptr  // Type Identifer
00799   };
00800   return DICompositeType(MDNode::get(VMContext, Elts));
00801 }
00802 
00803 /// createVectorType - Create debugging information entry for a vector.
00804 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
00805                                             DIType Ty, DIArray Subscripts) {
00806   // A vector is an array type with the FlagVector flag applied.
00807   Value *Elts[] = {
00808     GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
00809     nullptr, // Filename/Directory,
00810     nullptr, // Unused
00811     MDString::get(VMContext, ""),
00812     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
00813     ConstantInt::get(Type::getInt64Ty(VMContext), Size),
00814     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00815     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00816     ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
00817     Ty.getRef(),
00818     Subscripts,
00819     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
00820     nullptr,
00821     nullptr,
00822     nullptr  // Type Identifer
00823   };
00824   return DICompositeType(MDNode::get(VMContext, Elts));
00825 }
00826 
00827 /// createArtificialType - Create a new DIType with "artificial" flag set.
00828 DIType DIBuilder::createArtificialType(DIType Ty) {
00829   if (Ty.isArtificial())
00830     return Ty;
00831 
00832   SmallVector<Value *, 9> Elts;
00833   MDNode *N = Ty;
00834   assert (N && "Unexpected input DIType!");
00835   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
00836     Elts.push_back(N->getOperand(i));
00837 
00838   unsigned CurFlags = Ty.getFlags();
00839   CurFlags = CurFlags | DIType::FlagArtificial;
00840 
00841   // Flags are stored at this slot.
00842   // FIXME: Add an enum for this magic value.
00843   Elts[8] =  ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
00844 
00845   return DIType(MDNode::get(VMContext, Elts));
00846 }
00847 
00848 /// createObjectPointerType - Create a new type with both the object pointer
00849 /// and artificial flags set.
00850 DIType DIBuilder::createObjectPointerType(DIType Ty) {
00851   if (Ty.isObjectPointer())
00852     return Ty;
00853 
00854   SmallVector<Value *, 9> Elts;
00855   MDNode *N = Ty;
00856   assert (N && "Unexpected input DIType!");
00857   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
00858     Elts.push_back(N->getOperand(i));
00859 
00860   unsigned CurFlags = Ty.getFlags();
00861   CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
00862 
00863   // Flags are stored at this slot.
00864   // FIXME: Add an enum for this magic value.
00865   Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
00866 
00867   return DIType(MDNode::get(VMContext, Elts));
00868 }
00869 
00870 /// retainType - Retain DIType in a module even if it is not referenced
00871 /// through debug info anchors.
00872 void DIBuilder::retainType(DIType T) {
00873   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
00874 }
00875 
00876 /// createUnspecifiedParameter - Create unspeicified type descriptor
00877 /// for the subroutine type.
00878 DIBasicType DIBuilder::createUnspecifiedParameter() {
00879   return DIBasicType();
00880 }
00881 
00882 /// createForwardDecl - Create a permanent forward-declared type.
00883 DICompositeType
00884 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
00885                              DIFile F, unsigned Line, unsigned RuntimeLang,
00886                              uint64_t SizeInBits, uint64_t AlignInBits,
00887                              StringRef UniqueIdentifier) {
00888   // Create a temporary MDNode.
00889   Value *Elts[] = {
00890     GetTagConstant(VMContext, Tag),
00891     F.getFileNode(),
00892     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00893     MDString::get(VMContext, Name),
00894     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00895     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00896     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00897     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00898     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00899     nullptr,
00900     DIArray(),
00901     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00902     nullptr,
00903     nullptr, //TemplateParams
00904     UniqueIdentifier.empty() ? nullptr
00905                              : MDString::get(VMContext, UniqueIdentifier)
00906   };
00907   MDNode *Node = MDNode::get(VMContext, Elts);
00908   DICompositeType RetTy(Node);
00909   assert(RetTy.isCompositeType() &&
00910          "createForwardDecl result should be a DIType");
00911   if (!UniqueIdentifier.empty())
00912     retainType(RetTy);
00913   return RetTy;
00914 }
00915 
00916 /// createReplaceableForwardDecl - Create a temporary forward-declared type that
00917 /// can be RAUW'd if the full type is seen.
00918 DICompositeType DIBuilder::createReplaceableForwardDecl(
00919     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
00920     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
00921     StringRef UniqueIdentifier) {
00922   // Create a temporary MDNode.
00923   Value *Elts[] = {
00924     GetTagConstant(VMContext, Tag),
00925     F.getFileNode(),
00926     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00927     MDString::get(VMContext, Name),
00928     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00929     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00930     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00931     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00932     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00933     nullptr,
00934     DIArray(),
00935     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00936     nullptr,
00937     nullptr, //TemplateParams
00938     UniqueIdentifier.empty() ? nullptr
00939                              : MDString::get(VMContext, UniqueIdentifier)
00940   };
00941   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
00942   DICompositeType RetTy(Node);
00943   assert(RetTy.isCompositeType() &&
00944          "createReplaceableForwardDecl result should be a DIType");
00945   if (!UniqueIdentifier.empty())
00946     retainType(RetTy);
00947   return RetTy;
00948 }
00949 
00950 /// getOrCreateArray - Get a DIArray, create one if required.
00951 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
00952   return DIArray(MDNode::get(VMContext, Elements));
00953 }
00954 
00955 /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
00956 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
00957   SmallVector<llvm::Value *, 16> Elts; 
00958   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
00959     if (Elements[i] && isa<MDNode>(Elements[i]))
00960       Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
00961     else
00962       Elts.push_back(Elements[i]);
00963   }
00964   return DITypeArray(MDNode::get(VMContext, Elts));
00965 }
00966 
00967 /// getOrCreateSubrange - Create a descriptor for a value range.  This
00968 /// implicitly uniques the values returned.
00969 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
00970   Value *Elts[] = {
00971     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
00972     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
00973     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
00974   };
00975 
00976   return DISubrange(MDNode::get(VMContext, Elts));
00977 }
00978 
00979 /// \brief Create a new descriptor for the specified global.
00980 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
00981                                                  StringRef LinkageName,
00982                                                  DIFile F, unsigned LineNumber,
00983                                                  DITypeRef Ty, bool isLocalToUnit,
00984                                                  Value *Val) {
00985   Value *Elts[] = {
00986     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00987     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00988     nullptr, // TheCU,
00989     MDString::get(VMContext, Name),
00990     MDString::get(VMContext, Name),
00991     MDString::get(VMContext, LinkageName),
00992     F,
00993     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00994     Ty,
00995     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
00996     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
00997     Val,
00998     DIDescriptor()
00999   };
01000   MDNode *Node = MDNode::get(VMContext, Elts);
01001   AllGVs.push_back(Node);
01002   return DIGlobalVariable(Node);
01003 }
01004 
01005 /// \brief Create a new descriptor for the specified global.
01006 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
01007                                                  unsigned LineNumber,
01008                                                  DITypeRef Ty,
01009                                                  bool isLocalToUnit,
01010                                                  Value *Val) {
01011   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
01012                               Val);
01013 }
01014 
01015 static DIGlobalVariable
01016 createStaticVariableHelper(LLVMContext &VMContext, DIDescriptor Context,
01017                            StringRef Name, StringRef LinkageName, DIFile F,
01018                            unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit,
01019                            Value *Val, MDNode *Decl, bool isDefinition,
01020                            std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
01021   Value *Elts[] = {
01022     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
01023     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01024     getNonCompileUnitScope(Context),
01025     MDString::get(VMContext, Name),
01026     MDString::get(VMContext, Name),
01027     MDString::get(VMContext, LinkageName),
01028     F,
01029     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
01030     Ty,
01031     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
01032     ConstantInt::get(Type::getInt32Ty(VMContext), isDefinition),
01033     Val,
01034     DIDescriptor(Decl)
01035   };
01036 
01037   return DIGlobalVariable(CreateFunc(Elts));
01038 }
01039 
01040 /// createStaticVariable - Create a new descriptor for the specified
01041 /// variable.
01042 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
01043                                                  StringRef Name,
01044                                                  StringRef LinkageName,
01045                                                  DIFile F, unsigned LineNumber,
01046                                                  DITypeRef Ty,
01047                                                  bool isLocalToUnit,
01048                                                  Value *Val, MDNode *Decl) {
01049   return createStaticVariableHelper(VMContext, Context, Name, LinkageName, F,
01050                                     LineNumber, Ty, isLocalToUnit, Val, Decl, true,
01051                                     [&] (ArrayRef<Value *> Elts) -> MDNode * {
01052                                       MDNode *Node = MDNode::get(VMContext, Elts);
01053                                       AllGVs.push_back(Node);
01054                                       return Node;
01055                                     });
01056 }
01057 
01058 /// createTempStaticVariableFwdDecl - Create a new temporary descriptor for the
01059 /// specified variable declarartion.
01060 DIGlobalVariable
01061 DIBuilder::createTempStaticVariableFwdDecl(DIDescriptor Context,
01062                                            StringRef Name,
01063                                            StringRef LinkageName,
01064                                            DIFile F, unsigned LineNumber,
01065                                            DITypeRef Ty,
01066                                            bool isLocalToUnit,
01067                                            Value *Val, MDNode *Decl) {
01068   return createStaticVariableHelper(VMContext, Context, Name, LinkageName, F,
01069                                     LineNumber, Ty, isLocalToUnit, Val, Decl, false,
01070                                     [&] (ArrayRef<Value *> Elts) {
01071                                       return MDNode::getTemporary(VMContext, Elts);
01072                                     });
01073 }
01074 
01075 /// createVariable - Create a new descriptor for the specified variable.
01076 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
01077                                           StringRef Name, DIFile File,
01078                                           unsigned LineNo, DITypeRef Ty,
01079                                           bool AlwaysPreserve, unsigned Flags,
01080                                           unsigned ArgNo) {
01081   DIDescriptor Context(getNonCompileUnitScope(Scope));
01082   assert((!Context || Context.isScope()) &&
01083          "createLocalVariable should be called with a valid Context");
01084   Value *Elts[] = {
01085     GetTagConstant(VMContext, Tag),
01086     getNonCompileUnitScope(Scope),
01087     MDString::get(VMContext, Name),
01088     File,
01089     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
01090     Ty,
01091     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01092     Constant::getNullValue(Type::getInt32Ty(VMContext))
01093   };
01094   MDNode *Node = MDNode::get(VMContext, Elts);
01095   if (AlwaysPreserve) {
01096     // The optimizer may remove local variable. If there is an interest
01097     // to preserve variable info in such situation then stash it in a
01098     // named mdnode.
01099     DISubprogram Fn(getDISubprogram(Scope));
01100     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
01101     FnLocals->addOperand(Node);
01102   }
01103   DIVariable RetVar(Node);
01104   assert(RetVar.isVariable() &&
01105          "createLocalVariable should return a valid DIVariable");
01106   return RetVar;
01107 }
01108 
01109 /// createComplexVariable - Create a new descriptor for the specified variable
01110 /// which has a complex address expression for its address.
01111 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
01112                                             StringRef Name, DIFile F,
01113                                             unsigned LineNo,
01114                                             DITypeRef Ty,
01115                                             ArrayRef<Value *> Addr,
01116                                             unsigned ArgNo) {
01117   assert(Addr.size() > 0 && "complex address is empty");
01118   Value *Elts[] = {
01119     GetTagConstant(VMContext, Tag),
01120     getNonCompileUnitScope(Scope),
01121     MDString::get(VMContext, Name),
01122     F,
01123     ConstantInt::get(Type::getInt32Ty(VMContext),
01124                      (LineNo | (ArgNo << 24))),
01125     Ty,
01126     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01127     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01128     MDNode::get(VMContext, Addr)
01129   };
01130   return DIVariable(MDNode::get(VMContext, Elts));
01131 }
01132 
01133 /// createVariablePiece - Create a descriptor to describe one part
01134 /// of aggregate variable that is fragmented across multiple Values.
01135 DIVariable DIBuilder::createVariablePiece(DIVariable Variable,
01136                                           unsigned OffsetInBytes,
01137                                           unsigned SizeInBytes) {
01138   assert(SizeInBytes > 0 && "zero-size piece");
01139   Value *Addr[] = {
01140     ConstantInt::get(Type::getInt32Ty(VMContext), OpPiece),
01141     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBytes),
01142     ConstantInt::get(Type::getInt32Ty(VMContext), SizeInBytes)
01143   };
01144 
01145   assert((Variable->getNumOperands() == 8 || Variable.isVariablePiece()) &&
01146          "variable already has a complex address");
01147   SmallVector<Value *, 9> Elts;
01148   for (unsigned i = 0; i < 8; ++i)
01149     Elts.push_back(Variable->getOperand(i));
01150 
01151   Elts.push_back(MDNode::get(VMContext, Addr));
01152   return DIVariable(MDNode::get(VMContext, Elts));
01153 }
01154 
01155 /// createFunction - Create a new descriptor for the specified function.
01156 /// FIXME: this is added for dragonegg. Once we update dragonegg
01157 /// to call resolve function, this will be removed.
01158 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
01159                                        StringRef LinkageName, DIFile File,
01160                                        unsigned LineNo, DICompositeType Ty,
01161                                        bool isLocalToUnit, bool isDefinition,
01162                                        unsigned ScopeLine, unsigned Flags,
01163                                        bool isOptimized, Function *Fn,
01164                                        MDNode *TParams, MDNode *Decl) {
01165   // dragonegg does not generate identifier for types, so using an empty map
01166   // to resolve the context should be fine.
01167   DITypeIdentifierMap EmptyMap;
01168   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
01169                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01170                         Flags, isOptimized, Fn, TParams, Decl);
01171 }
01172 
01173 static DISubprogram
01174 createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
01175                      StringRef LinkageName, DIFile File, unsigned LineNo,
01176                      DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
01177                      unsigned ScopeLine, unsigned Flags, bool isOptimized,
01178                      Function *Fn, MDNode *TParams, MDNode *Decl,
01179                      std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
01180   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01181          "function types should be subroutines");
01182   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
01183   Value *Elts[] = {
01184     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01185     File.getFileNode(),
01186     DIScope(getNonCompileUnitScope(Context)).getRef(),
01187     MDString::get(VMContext, Name),
01188     MDString::get(VMContext, Name),
01189     MDString::get(VMContext, LinkageName),
01190     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01191     Ty,
01192     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01193     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01194     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01195     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01196     nullptr,
01197     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01198     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01199     Fn,
01200     TParams,
01201     Decl,
01202     MDNode::getTemporary(VMContext, TElts),
01203     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
01204   };
01205 
01206   DISubprogram S(CreateFunc(Elts));
01207   assert(S.isSubprogram() &&
01208          "createFunction should return a valid DISubprogram");
01209   return S;
01210 }
01211 
01212 
01213 /// createFunction - Create a new descriptor for the specified function.
01214 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
01215                                        StringRef LinkageName, DIFile File,
01216                                        unsigned LineNo, DICompositeType Ty,
01217                                        bool isLocalToUnit, bool isDefinition,
01218                                        unsigned ScopeLine, unsigned Flags,
01219                                        bool isOptimized, Function *Fn,
01220                                        MDNode *TParams, MDNode *Decl) {
01221   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
01222                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01223                               Flags, isOptimized, Fn, TParams, Decl,
01224                               [&] (ArrayRef<Value *> Elts) -> MDNode *{
01225                                 MDNode *Node = MDNode::get(VMContext, Elts);
01226                                 // Create a named metadata so that we
01227                                 // do not lose this mdnode.
01228                                 if (isDefinition)
01229                                   AllSubprograms.push_back(Node);
01230                                 return Node;
01231                               });
01232 }
01233 
01234 /// createTempFunctionFwdDecl - Create a new temporary descriptor for
01235 /// the specified function declaration.
01236 DISubprogram
01237 DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
01238                                      StringRef LinkageName, DIFile File,
01239                                      unsigned LineNo, DICompositeType Ty,
01240                                      bool isLocalToUnit, bool isDefinition,
01241                                      unsigned ScopeLine, unsigned Flags,
01242                                      bool isOptimized, Function *Fn,
01243                                      MDNode *TParams, MDNode *Decl) {
01244   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
01245                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01246                               Flags, isOptimized, Fn, TParams, Decl,
01247                               [&] (ArrayRef<Value *> Elts) {
01248                                 return MDNode::getTemporary(VMContext, Elts);
01249                               });
01250 }
01251 
01252 /// createMethod - Create a new descriptor for the specified C++ method.
01253 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
01254                                      StringRef LinkageName, DIFile F,
01255                                      unsigned LineNo, DICompositeType Ty,
01256                                      bool isLocalToUnit, bool isDefinition,
01257                                      unsigned VK, unsigned VIndex,
01258                                      DIType VTableHolder, unsigned Flags,
01259                                      bool isOptimized, Function *Fn,
01260                                      MDNode *TParam) {
01261   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01262          "function types should be subroutines");
01263   assert(getNonCompileUnitScope(Context) &&
01264          "Methods should have both a Context and a context that isn't "
01265          "the compile unit.");
01266   Value *Elts[] = {
01267     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01268     F.getFileNode(),
01269     DIScope(Context).getRef(),
01270     MDString::get(VMContext, Name),
01271     MDString::get(VMContext, Name),
01272     MDString::get(VMContext, LinkageName),
01273     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01274     Ty,
01275     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01276     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01277     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
01278     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
01279     VTableHolder.getRef(),
01280     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01281     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01282     Fn,
01283     TParam,
01284     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01285     nullptr,
01286     // FIXME: Do we want to use different scope/lines?
01287     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01288   };
01289   MDNode *Node = MDNode::get(VMContext, Elts);
01290   if (isDefinition)
01291     AllSubprograms.push_back(Node);
01292   DISubprogram S(Node);
01293   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
01294   return S;
01295 }
01296 
01297 /// createNameSpace - This creates new descriptor for a namespace
01298 /// with the specified parent scope.
01299 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
01300                                        DIFile File, unsigned LineNo) {
01301   Value *Elts[] = {
01302     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
01303     File.getFileNode(),
01304     getNonCompileUnitScope(Scope),
01305     MDString::get(VMContext, Name),
01306     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01307   };
01308   DINameSpace R(MDNode::get(VMContext, Elts));
01309   assert(R.Verify() &&
01310          "createNameSpace should return a verifiable DINameSpace");
01311   return R;
01312 }
01313 
01314 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
01315 /// an existing scope with a new filename.
01316 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
01317                                                      DIFile File,
01318                                                      unsigned Discriminator) {
01319   Value *Elts[] = {
01320     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01321     File.getFileNode(),
01322     Scope,
01323     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
01324   };
01325   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
01326   assert(
01327       R.Verify() &&
01328       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
01329   return R;
01330 }
01331 
01332 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
01333                                              unsigned Line, unsigned Col) {
01334   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
01335   // I believe the right way is to have a self-referential element in the node.
01336   // Also: why do we bother with line/column - they're not used and the
01337   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
01338   // for uniquing, yet then we have this other solution (because line/col were
01339   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
01340 
01341   // Defeat MDNode uniquing for lexical blocks by using unique id.
01342   static unsigned int unique_id = 0;
01343   Value *Elts[] = {
01344     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01345     File.getFileNode(),
01346     getNonCompileUnitScope(Scope),
01347     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
01348     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
01349     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
01350   };
01351   DILexicalBlock R(MDNode::get(VMContext, Elts));
01352   assert(R.Verify() &&
01353          "createLexicalBlock should return a verifiable DILexicalBlock");
01354   return R;
01355 }
01356 
01357 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01358 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01359                                       Instruction *InsertBefore) {
01360   assert(Storage && "no storage passed to dbg.declare");
01361   assert(VarInfo.isVariable() &&
01362          "empty or invalid DIVariable passed to dbg.declare");
01363   if (!DeclareFn)
01364     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01365 
01366   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01367   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
01368 }
01369 
01370 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01371 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01372                                       BasicBlock *InsertAtEnd) {
01373   assert(Storage && "no storage passed to dbg.declare");
01374   assert(VarInfo.isVariable() &&
01375          "empty or invalid DIVariable passed to dbg.declare");
01376   if (!DeclareFn)
01377     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01378 
01379   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01380 
01381   // If this block already has a terminator then insert this intrinsic
01382   // before the terminator.
01383   if (TerminatorInst *T = InsertAtEnd->getTerminator())
01384     return CallInst::Create(DeclareFn, Args, "", T);
01385   else
01386     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
01387 }
01388 
01389 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01390 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01391                                                 DIVariable VarInfo,
01392                                                 Instruction *InsertBefore) {
01393   assert(V && "no value passed to dbg.value");
01394   assert(VarInfo.isVariable() &&
01395          "empty or invalid DIVariable passed to dbg.value");
01396   if (!ValueFn)
01397     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01398 
01399   Value *Args[] = { MDNode::get(V->getContext(), V),
01400                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01401                     VarInfo };
01402   return CallInst::Create(ValueFn, Args, "", InsertBefore);
01403 }
01404 
01405 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01406 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01407                                                 DIVariable VarInfo,
01408                                                 BasicBlock *InsertAtEnd) {
01409   assert(V && "no value passed to dbg.value");
01410   assert(VarInfo.isVariable() &&
01411          "empty or invalid DIVariable passed to dbg.value");
01412   if (!ValueFn)
01413     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01414 
01415   Value *Args[] = { MDNode::get(V->getContext(), V),
01416                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01417                     VarInfo };
01418   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
01419 }