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 temporary forward-declared type that
00883 /// can be RAUW'd if the full type is seen.
00884 DICompositeType
00885 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
00886                              DIFile F, unsigned Line, unsigned RuntimeLang,
00887                              uint64_t SizeInBits, uint64_t AlignInBits,
00888                              StringRef UniqueIdentifier) {
00889   // Create a temporary MDNode.
00890   Value *Elts[] = {
00891     GetTagConstant(VMContext, Tag),
00892     F.getFileNode(),
00893     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00894     MDString::get(VMContext, Name),
00895     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00896     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00897     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00898     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00899     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00900     nullptr,
00901     DIArray(),
00902     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00903     nullptr,
00904     nullptr, //TemplateParams
00905     UniqueIdentifier.empty() ? nullptr
00906                              : MDString::get(VMContext, UniqueIdentifier)
00907   };
00908   MDNode *Node = MDNode::get(VMContext, Elts);
00909   DICompositeType RetTy(Node);
00910   assert(RetTy.isCompositeType() &&
00911          "createForwardDecl result should be a DIType");
00912   if (!UniqueIdentifier.empty())
00913     retainType(RetTy);
00914   return RetTy;
00915 }
00916 
00917 /// createForwardDecl - Create a temporary forward-declared type that
00918 /// can be RAUW'd if the full type is seen.
00919 DICompositeType DIBuilder::createReplaceableForwardDecl(
00920     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
00921     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
00922     StringRef UniqueIdentifier) {
00923   // Create a temporary MDNode.
00924   Value *Elts[] = {
00925     GetTagConstant(VMContext, Tag),
00926     F.getFileNode(),
00927     DIScope(getNonCompileUnitScope(Scope)).getRef(),
00928     MDString::get(VMContext, Name),
00929     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
00930     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
00931     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
00932     ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
00933     ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
00934     nullptr,
00935     DIArray(),
00936     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
00937     nullptr,
00938     nullptr, //TemplateParams
00939     UniqueIdentifier.empty() ? nullptr
00940                              : MDString::get(VMContext, UniqueIdentifier)
00941   };
00942   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
00943   DICompositeType RetTy(Node);
00944   assert(RetTy.isCompositeType() &&
00945          "createForwardDecl result should be a DIType");
00946   if (!UniqueIdentifier.empty())
00947     retainType(RetTy);
00948   return RetTy;
00949 }
00950 
00951 /// getOrCreateArray - Get a DIArray, create one if required.
00952 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
00953   return DIArray(MDNode::get(VMContext, Elements));
00954 }
00955 
00956 /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
00957 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
00958   SmallVector<llvm::Value *, 16> Elts; 
00959   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
00960     if (Elements[i] && isa<MDNode>(Elements[i]))
00961       Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
00962     else
00963       Elts.push_back(Elements[i]);
00964   }
00965   return DITypeArray(MDNode::get(VMContext, Elts));
00966 }
00967 
00968 /// getOrCreateSubrange - Create a descriptor for a value range.  This
00969 /// implicitly uniques the values returned.
00970 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
00971   Value *Elts[] = {
00972     GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
00973     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
00974     ConstantInt::get(Type::getInt64Ty(VMContext), Count)
00975   };
00976 
00977   return DISubrange(MDNode::get(VMContext, Elts));
00978 }
00979 
00980 /// \brief Create a new descriptor for the specified global.
00981 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
00982                                                  StringRef LinkageName,
00983                                                  DIFile F, unsigned LineNumber,
00984                                                  DITypeRef Ty, bool isLocalToUnit,
00985                                                  Value *Val) {
00986   Value *Elts[] = {
00987     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
00988     Constant::getNullValue(Type::getInt32Ty(VMContext)),
00989     nullptr, // TheCU,
00990     MDString::get(VMContext, Name),
00991     MDString::get(VMContext, Name),
00992     MDString::get(VMContext, LinkageName),
00993     F,
00994     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
00995     Ty,
00996     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
00997     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
00998     Val,
00999     DIDescriptor()
01000   };
01001   MDNode *Node = MDNode::get(VMContext, Elts);
01002   AllGVs.push_back(Node);
01003   return DIGlobalVariable(Node);
01004 }
01005 
01006 /// \brief Create a new descriptor for the specified global.
01007 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
01008                                                  unsigned LineNumber,
01009                                                  DITypeRef Ty,
01010                                                  bool isLocalToUnit,
01011                                                  Value *Val) {
01012   return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
01013                               Val);
01014 }
01015 
01016 /// createStaticVariable - Create a new descriptor for the specified static
01017 /// variable.
01018 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
01019                                                  StringRef Name,
01020                                                  StringRef LinkageName,
01021                                                  DIFile F, unsigned LineNumber,
01022                                                  DITypeRef Ty,
01023                                                  bool isLocalToUnit,
01024                                                  Value *Val, MDNode *Decl) {
01025   Value *Elts[] = {
01026     GetTagConstant(VMContext, dwarf::DW_TAG_variable),
01027     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01028     getNonCompileUnitScope(Context),
01029     MDString::get(VMContext, Name),
01030     MDString::get(VMContext, Name),
01031     MDString::get(VMContext, LinkageName),
01032     F,
01033     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
01034     Ty,
01035     ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
01036     ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
01037     Val,
01038     DIDescriptor(Decl)
01039   };
01040   MDNode *Node = MDNode::get(VMContext, Elts);
01041   AllGVs.push_back(Node);
01042   return DIGlobalVariable(Node);
01043 }
01044 
01045 /// createVariable - Create a new descriptor for the specified variable.
01046 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
01047                                           StringRef Name, DIFile File,
01048                                           unsigned LineNo, DITypeRef Ty,
01049                                           bool AlwaysPreserve, unsigned Flags,
01050                                           unsigned ArgNo) {
01051   DIDescriptor Context(getNonCompileUnitScope(Scope));
01052   assert((!Context || Context.isScope()) &&
01053          "createLocalVariable should be called with a valid Context");
01054   Value *Elts[] = {
01055     GetTagConstant(VMContext, Tag),
01056     getNonCompileUnitScope(Scope),
01057     MDString::get(VMContext, Name),
01058     File,
01059     ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
01060     Ty,
01061     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01062     Constant::getNullValue(Type::getInt32Ty(VMContext))
01063   };
01064   MDNode *Node = MDNode::get(VMContext, Elts);
01065   if (AlwaysPreserve) {
01066     // The optimizer may remove local variable. If there is an interest
01067     // to preserve variable info in such situation then stash it in a
01068     // named mdnode.
01069     DISubprogram Fn(getDISubprogram(Scope));
01070     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
01071     FnLocals->addOperand(Node);
01072   }
01073   DIVariable RetVar(Node);
01074   assert(RetVar.isVariable() &&
01075          "createLocalVariable should return a valid DIVariable");
01076   return RetVar;
01077 }
01078 
01079 /// createComplexVariable - Create a new descriptor for the specified variable
01080 /// which has a complex address expression for its address.
01081 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
01082                                             StringRef Name, DIFile F,
01083                                             unsigned LineNo,
01084                                             DITypeRef Ty,
01085                                             ArrayRef<Value *> Addr,
01086                                             unsigned ArgNo) {
01087   assert(Addr.size() > 0 && "complex address is empty");
01088   Value *Elts[] = {
01089     GetTagConstant(VMContext, Tag),
01090     getNonCompileUnitScope(Scope),
01091     MDString::get(VMContext, Name),
01092     F,
01093     ConstantInt::get(Type::getInt32Ty(VMContext),
01094                      (LineNo | (ArgNo << 24))),
01095     Ty,
01096     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01097     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01098     MDNode::get(VMContext, Addr)
01099   };
01100   return DIVariable(MDNode::get(VMContext, Elts));
01101 }
01102 
01103 /// createVariablePiece - Create a descriptor to describe one part
01104 /// of aggregate variable that is fragmented across multiple Values.
01105 DIVariable DIBuilder::createVariablePiece(DIVariable Variable,
01106                                           unsigned OffsetInBytes,
01107                                           unsigned SizeInBytes) {
01108   assert(SizeInBytes > 0 && "zero-size piece");
01109   Value *Addr[] = {
01110     ConstantInt::get(Type::getInt32Ty(VMContext), OpPiece),
01111     ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBytes),
01112     ConstantInt::get(Type::getInt32Ty(VMContext), SizeInBytes)
01113   };
01114 
01115   assert((Variable->getNumOperands() == 8 || Variable.isVariablePiece()) &&
01116          "variable already has a complex address");
01117   SmallVector<Value *, 9> Elts;
01118   for (unsigned i = 0; i < 8; ++i)
01119     Elts.push_back(Variable->getOperand(i));
01120 
01121   Elts.push_back(MDNode::get(VMContext, Addr));
01122   return DIVariable(MDNode::get(VMContext, Elts));
01123 }
01124 
01125 /// createFunction - Create a new descriptor for the specified function.
01126 /// FIXME: this is added for dragonegg. Once we update dragonegg
01127 /// to call resolve function, this will be removed.
01128 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
01129                                        StringRef LinkageName, DIFile File,
01130                                        unsigned LineNo, DICompositeType Ty,
01131                                        bool isLocalToUnit, bool isDefinition,
01132                                        unsigned ScopeLine, unsigned Flags,
01133                                        bool isOptimized, Function *Fn,
01134                                        MDNode *TParams, MDNode *Decl) {
01135   // dragonegg does not generate identifier for types, so using an empty map
01136   // to resolve the context should be fine.
01137   DITypeIdentifierMap EmptyMap;
01138   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
01139                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
01140                         Flags, isOptimized, Fn, TParams, Decl);
01141 }
01142 
01143 /// createFunction - Create a new descriptor for the specified function.
01144 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
01145                                        StringRef LinkageName, DIFile File,
01146                                        unsigned LineNo, DICompositeType Ty,
01147                                        bool isLocalToUnit, bool isDefinition,
01148                                        unsigned ScopeLine, unsigned Flags,
01149                                        bool isOptimized, Function *Fn,
01150                                        MDNode *TParams, MDNode *Decl) {
01151   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01152          "function types should be subroutines");
01153   Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
01154   Value *Elts[] = {
01155     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01156     File.getFileNode(),
01157     DIScope(getNonCompileUnitScope(Context)).getRef(),
01158     MDString::get(VMContext, Name),
01159     MDString::get(VMContext, Name),
01160     MDString::get(VMContext, LinkageName),
01161     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01162     Ty,
01163     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01164     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01165     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01166     ConstantInt::get(Type::getInt32Ty(VMContext), 0),
01167     nullptr,
01168     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01169     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01170     Fn,
01171     TParams,
01172     Decl,
01173     MDNode::getTemporary(VMContext, TElts),
01174     ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
01175   };
01176   MDNode *Node = MDNode::get(VMContext, Elts);
01177 
01178   // Create a named metadata so that we do not lose this mdnode.
01179   if (isDefinition)
01180     AllSubprograms.push_back(Node);
01181   DISubprogram S(Node);
01182   assert(S.isSubprogram() &&
01183          "createFunction should return a valid DISubprogram");
01184   return S;
01185 }
01186 
01187 /// createMethod - Create a new descriptor for the specified C++ method.
01188 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
01189                                      StringRef LinkageName, DIFile F,
01190                                      unsigned LineNo, DICompositeType Ty,
01191                                      bool isLocalToUnit, bool isDefinition,
01192                                      unsigned VK, unsigned VIndex,
01193                                      DIType VTableHolder, unsigned Flags,
01194                                      bool isOptimized, Function *Fn,
01195                                      MDNode *TParam) {
01196   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01197          "function types should be subroutines");
01198   assert(getNonCompileUnitScope(Context) &&
01199          "Methods should have both a Context and a context that isn't "
01200          "the compile unit.");
01201   Value *Elts[] = {
01202     GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
01203     F.getFileNode(),
01204     DIScope(Context).getRef(),
01205     MDString::get(VMContext, Name),
01206     MDString::get(VMContext, Name),
01207     MDString::get(VMContext, LinkageName),
01208     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
01209     Ty,
01210     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
01211     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
01212     ConstantInt::get(Type::getInt32Ty(VMContext), VK),
01213     ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
01214     VTableHolder.getRef(),
01215     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
01216     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
01217     Fn,
01218     TParam,
01219     Constant::getNullValue(Type::getInt32Ty(VMContext)),
01220     nullptr,
01221     // FIXME: Do we want to use different scope/lines?
01222     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01223   };
01224   MDNode *Node = MDNode::get(VMContext, Elts);
01225   if (isDefinition)
01226     AllSubprograms.push_back(Node);
01227   DISubprogram S(Node);
01228   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
01229   return S;
01230 }
01231 
01232 /// createNameSpace - This creates new descriptor for a namespace
01233 /// with the specified parent scope.
01234 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
01235                                        DIFile File, unsigned LineNo) {
01236   Value *Elts[] = {
01237     GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
01238     File.getFileNode(),
01239     getNonCompileUnitScope(Scope),
01240     MDString::get(VMContext, Name),
01241     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
01242   };
01243   DINameSpace R(MDNode::get(VMContext, Elts));
01244   assert(R.Verify() &&
01245          "createNameSpace should return a verifiable DINameSpace");
01246   return R;
01247 }
01248 
01249 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
01250 /// an existing scope with a new filename.
01251 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
01252                                                      DIFile File,
01253                                                      unsigned Discriminator) {
01254   Value *Elts[] = {
01255     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01256     File.getFileNode(),
01257     Scope,
01258     ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
01259   };
01260   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
01261   assert(
01262       R.Verify() &&
01263       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
01264   return R;
01265 }
01266 
01267 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
01268                                              unsigned Line, unsigned Col) {
01269   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
01270   // I believe the right way is to have a self-referential element in the node.
01271   // Also: why do we bother with line/column - they're not used and the
01272   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
01273   // for uniquing, yet then we have this other solution (because line/col were
01274   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
01275 
01276   // Defeat MDNode uniquing for lexical blocks by using unique id.
01277   static unsigned int unique_id = 0;
01278   Value *Elts[] = {
01279     GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
01280     File.getFileNode(),
01281     getNonCompileUnitScope(Scope),
01282     ConstantInt::get(Type::getInt32Ty(VMContext), Line),
01283     ConstantInt::get(Type::getInt32Ty(VMContext), Col),
01284     ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
01285   };
01286   DILexicalBlock R(MDNode::get(VMContext, Elts));
01287   assert(R.Verify() &&
01288          "createLexicalBlock should return a verifiable DILexicalBlock");
01289   return R;
01290 }
01291 
01292 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01293 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01294                                       Instruction *InsertBefore) {
01295   assert(Storage && "no storage passed to dbg.declare");
01296   assert(VarInfo.isVariable() &&
01297          "empty or invalid DIVariable passed to dbg.declare");
01298   if (!DeclareFn)
01299     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01300 
01301   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01302   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
01303 }
01304 
01305 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
01306 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01307                                       BasicBlock *InsertAtEnd) {
01308   assert(Storage && "no storage passed to dbg.declare");
01309   assert(VarInfo.isVariable() &&
01310          "empty or invalid DIVariable passed to dbg.declare");
01311   if (!DeclareFn)
01312     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01313 
01314   Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
01315 
01316   // If this block already has a terminator then insert this intrinsic
01317   // before the terminator.
01318   if (TerminatorInst *T = InsertAtEnd->getTerminator())
01319     return CallInst::Create(DeclareFn, Args, "", T);
01320   else
01321     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
01322 }
01323 
01324 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01325 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01326                                                 DIVariable VarInfo,
01327                                                 Instruction *InsertBefore) {
01328   assert(V && "no value passed to dbg.value");
01329   assert(VarInfo.isVariable() &&
01330          "empty or invalid DIVariable passed to dbg.value");
01331   if (!ValueFn)
01332     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01333 
01334   Value *Args[] = { MDNode::get(V->getContext(), V),
01335                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01336                     VarInfo };
01337   return CallInst::Create(ValueFn, Args, "", InsertBefore);
01338 }
01339 
01340 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
01341 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01342                                                 DIVariable VarInfo,
01343                                                 BasicBlock *InsertAtEnd) {
01344   assert(V && "no value passed to dbg.value");
01345   assert(VarInfo.isVariable() &&
01346          "empty or invalid DIVariable passed to dbg.value");
01347   if (!ValueFn)
01348     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01349 
01350   Value *Args[] = { MDNode::get(V->getContext(), V),
01351                     ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01352                     VarInfo };
01353   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
01354 }