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 namespace {
00027 class HeaderBuilder {
00028   SmallVector<char, 256> Chars;
00029 
00030 public:
00031   explicit HeaderBuilder(Twine T) { T.toVector(Chars); }
00032   HeaderBuilder(const HeaderBuilder &X) : Chars(X.Chars) {}
00033   HeaderBuilder(HeaderBuilder &&X) : Chars(std::move(X.Chars)) {}
00034 
00035   template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
00036     Chars.push_back(0);
00037     Twine(X).toVector(Chars);
00038     return *this;
00039   }
00040 
00041   MDString *get(LLVMContext &Context) const {
00042     return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
00043   }
00044 
00045   static HeaderBuilder get(unsigned Tag) {
00046     return HeaderBuilder("0x" + Twine::utohexstr(Tag));
00047   }
00048 };
00049 }
00050 
00051 DIBuilder::DIBuilder(Module &m)
00052     : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
00053       TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
00054       DeclareFn(nullptr), ValueFn(nullptr) {}
00055 
00056 void DIBuilder::finalize() {
00057   DIArray Enums = getOrCreateArray(AllEnumTypes);
00058   DIType(TempEnumTypes).replaceAllUsesWith(Enums);
00059 
00060   SmallVector<Value *, 16> RetainValues;
00061   // Declarations and definitions of the same type may be retained. Some
00062   // clients RAUW these pairs, leaving duplicates in the retained types
00063   // list. Use a set to remove the duplicates while we transform the
00064   // TrackingVHs back into Values.
00065   SmallPtrSet<Value *, 16> RetainSet;
00066   for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
00067     if (RetainSet.insert(AllRetainTypes[I]).second)
00068       RetainValues.push_back(AllRetainTypes[I]);
00069   DIArray RetainTypes = getOrCreateArray(RetainValues);
00070   DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
00071 
00072   DIArray SPs = getOrCreateArray(AllSubprograms);
00073   DIType(TempSubprograms).replaceAllUsesWith(SPs);
00074   for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
00075     DISubprogram SP(SPs.getElement(i));
00076     if (MDNode *Temp = SP.getVariablesNodes()) {
00077       SmallVector<Value *, 4> Variables;
00078       for (Value *V : PreservedVariables.lookup(SP))
00079         Variables.push_back(V);
00080       DIArray AV = getOrCreateArray(Variables);
00081       DIType(Temp).replaceAllUsesWith(AV);
00082     }
00083   }
00084 
00085   DIArray GVs = getOrCreateArray(AllGVs);
00086   DIType(TempGVs).replaceAllUsesWith(GVs);
00087 
00088   SmallVector<Value *, 16> RetainValuesI;
00089   for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
00090     RetainValuesI.push_back(AllImportedModules[I]);
00091   DIArray IMs = getOrCreateArray(RetainValuesI);
00092   DIType(TempImportedModules).replaceAllUsesWith(IMs);
00093 }
00094 
00095 /// If N is compile unit return NULL otherwise return N.
00096 static MDNode *getNonCompileUnitScope(MDNode *N) {
00097   if (DIDescriptor(N).isCompileUnit())
00098     return nullptr;
00099   return N;
00100 }
00101 
00102 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
00103                                   StringRef Directory) {
00104   assert(!Filename.empty() && "Unable to create file without name");
00105   Value *Pair[] = {
00106     MDString::get(VMContext, Filename),
00107     MDString::get(VMContext, Directory)
00108   };
00109   return MDNode::get(VMContext, Pair);
00110 }
00111 
00112 DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
00113                                            StringRef Directory,
00114                                            StringRef Producer, bool isOptimized,
00115                                            StringRef Flags, unsigned RunTimeVer,
00116                                            StringRef SplitName,
00117                                            DebugEmissionKind Kind,
00118                                            bool EmitDebugInfo) {
00119 
00120   assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
00121           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
00122          "Invalid Language tag");
00123   assert(!Filename.empty() &&
00124          "Unable to create compile unit without filename");
00125   Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
00126   TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
00127 
00128   TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
00129 
00130   TempSubprograms = MDNode::getTemporary(VMContext, TElts);
00131 
00132   TempGVs = MDNode::getTemporary(VMContext, TElts);
00133 
00134   TempImportedModules = MDNode::getTemporary(VMContext, TElts);
00135 
00136   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
00137                        .concat(Lang)
00138                        .concat(Producer)
00139                        .concat(isOptimized)
00140                        .concat(Flags)
00141                        .concat(RunTimeVer)
00142                        .concat(SplitName)
00143                        .concat(Kind)
00144                        .get(VMContext),
00145                    createFilePathPair(VMContext, Filename, Directory),
00146                    TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
00147                    TempImportedModules};
00148 
00149   MDNode *CUNode = MDNode::get(VMContext, Elts);
00150 
00151   // Create a named metadata so that it is easier to find cu in a module.
00152   // Note that we only generate this when the caller wants to actually
00153   // emit debug information. When we are only interested in tracking
00154   // source line locations throughout the backend, we prevent codegen from
00155   // emitting debug info in the final output by not generating llvm.dbg.cu.
00156   if (EmitDebugInfo) {
00157     NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
00158     NMD->addOperand(CUNode);
00159   }
00160 
00161   return DICompileUnit(CUNode);
00162 }
00163 
00164 static DIImportedEntity
00165 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
00166                      Value *NS, unsigned Line, StringRef Name,
00167                      SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
00168   const MDNode *R;
00169   Value *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C),
00170                    Context, NS};
00171   R = MDNode::get(C, Elts);
00172   DIImportedEntity M(R);
00173   assert(M.Verify() && "Imported module should be valid");
00174   AllImportedModules.push_back(TrackingVH<MDNode>(M));
00175   return M;
00176 }
00177 
00178 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00179                                                  DINameSpace NS,
00180                                                  unsigned Line) {
00181   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
00182                                 Context, NS, Line, StringRef(), AllImportedModules);
00183 }
00184 
00185 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
00186                                                  DIImportedEntity NS,
00187                                                  unsigned Line) {
00188   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
00189                                 Context, NS, Line, StringRef(), AllImportedModules);
00190 }
00191 
00192 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
00193                                                       DIDescriptor Decl,
00194                                                       unsigned Line, StringRef Name) {
00195   // Make sure to use the unique identifier based metadata reference for
00196   // types that have one.
00197   Value *V = Decl.isType() ? static_cast<Value*>(DIType(Decl).getRef()) : Decl;
00198   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
00199                                 Context, V, Line, Name,
00200                                 AllImportedModules);
00201 }
00202 
00203 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
00204                                                       DIImportedEntity Imp,
00205                                                       unsigned Line, StringRef Name) {
00206   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
00207                                 Context, Imp, Line, Name, AllImportedModules);
00208 }
00209 
00210 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
00211   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext),
00212                    createFilePathPair(VMContext, Filename, Directory)};
00213   return DIFile(MDNode::get(VMContext, Elts));
00214 }
00215 
00216 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
00217   assert(!Name.empty() && "Unable to create enumerator without name");
00218   Value *Elts[] = {
00219       HeaderBuilder::get(dwarf::DW_TAG_enumerator).concat(Name).concat(Val).get(
00220           VMContext)};
00221   return DIEnumerator(MDNode::get(VMContext, Elts));
00222 }
00223 
00224 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
00225   assert(!Name.empty() && "Unable to create type without name");
00226   // Unspecified types are encoded in DIBasicType format. Line number, filename,
00227   // size, alignment, offset and flags are always empty here.
00228   Value *Elts[] = {
00229       HeaderBuilder::get(dwarf::DW_TAG_unspecified_type)
00230           .concat(Name)
00231           .concat(0)
00232           .concat(0)
00233           .concat(0)
00234           .concat(0)
00235           .concat(0)
00236           .concat(0)
00237           .get(VMContext),
00238       nullptr, // Filename
00239       nullptr  // Unused
00240   };
00241   return DIBasicType(MDNode::get(VMContext, Elts));
00242 }
00243 
00244 DIBasicType DIBuilder::createNullPtrType() {
00245   return createUnspecifiedType("decltype(nullptr)");
00246 }
00247 
00248 DIBasicType
00249 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
00250                            uint64_t AlignInBits, unsigned Encoding) {
00251   assert(!Name.empty() && "Unable to create type without name");
00252   // Basic types are encoded in DIBasicType format. Line number, filename,
00253   // offset and flags are always empty here.
00254   Value *Elts[] = {
00255       HeaderBuilder::get(dwarf::DW_TAG_base_type)
00256           .concat(Name)
00257           .concat(0) // Line
00258           .concat(SizeInBits)
00259           .concat(AlignInBits)
00260           .concat(0) // Offset
00261           .concat(0) // Flags
00262           .concat(Encoding)
00263           .get(VMContext),
00264       nullptr, // Filename
00265       nullptr  // Unused
00266   };
00267   return DIBasicType(MDNode::get(VMContext, Elts));
00268 }
00269 
00270 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
00271   // Qualified types are encoded in DIDerivedType format.
00272   Value *Elts[] = {HeaderBuilder::get(Tag)
00273                        .concat(StringRef()) // Name
00274                        .concat(0)           // Line
00275                        .concat(0)           // Size
00276                        .concat(0)           // Align
00277                        .concat(0)           // Offset
00278                        .concat(0)           // Flags
00279                        .get(VMContext),
00280                    nullptr, // Filename
00281                    nullptr, // Unused
00282                    FromTy.getRef()};
00283   return DIDerivedType(MDNode::get(VMContext, Elts));
00284 }
00285 
00286 DIDerivedType
00287 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
00288                              uint64_t AlignInBits, StringRef Name) {
00289   // Pointer types are encoded in DIDerivedType format.
00290   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type)
00291                        .concat(Name)
00292                        .concat(0) // Line
00293                        .concat(SizeInBits)
00294                        .concat(AlignInBits)
00295                        .concat(0) // Offset
00296                        .concat(0) // Flags
00297                        .get(VMContext),
00298                    nullptr, // Filename
00299                    nullptr, // Unused
00300                    PointeeTy.getRef()};
00301   return DIDerivedType(MDNode::get(VMContext, Elts));
00302 }
00303 
00304 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
00305                                                  DIType Base) {
00306   // Pointer types are encoded in DIDerivedType format.
00307   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type)
00308                        .concat(StringRef())
00309                        .concat(0) // Line
00310                        .concat(0) // Size
00311                        .concat(0) // Align
00312                        .concat(0) // Offset
00313                        .concat(0) // Flags
00314                        .get(VMContext),
00315                    nullptr, // Filename
00316                    nullptr, // Unused
00317                    PointeeTy.getRef(), Base.getRef()};
00318   return DIDerivedType(MDNode::get(VMContext, Elts));
00319 }
00320 
00321 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
00322   assert(RTy.isType() && "Unable to create reference type");
00323   // References are encoded in DIDerivedType format.
00324   Value *Elts[] = {HeaderBuilder::get(Tag)
00325                        .concat(StringRef()) // Name
00326                        .concat(0)           // Line
00327                        .concat(0)           // Size
00328                        .concat(0)           // Align
00329                        .concat(0)           // Offset
00330                        .concat(0)           // Flags
00331                        .get(VMContext),
00332                    nullptr, // Filename
00333                    nullptr, // TheCU,
00334                    RTy.getRef()};
00335   return DIDerivedType(MDNode::get(VMContext, Elts));
00336 }
00337 
00338 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
00339                                        unsigned LineNo, DIDescriptor Context) {
00340   // typedefs are encoded in DIDerivedType format.
00341   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef)
00342                        .concat(Name)
00343                        .concat(LineNo)
00344                        .concat(0) // Size
00345                        .concat(0) // Align
00346                        .concat(0) // Offset
00347                        .concat(0) // Flags
00348                        .get(VMContext),
00349                    File.getFileNode(),
00350                    DIScope(getNonCompileUnitScope(Context)).getRef(),
00351                    Ty.getRef()};
00352   return DIDerivedType(MDNode::get(VMContext, Elts));
00353 }
00354 
00355 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
00356   // typedefs are encoded in DIDerivedType format.
00357   assert(Ty.isType() && "Invalid type!");
00358   assert(FriendTy.isType() && "Invalid friend type!");
00359   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend)
00360                        .concat(StringRef()) // Name
00361                        .concat(0)           // Line
00362                        .concat(0)           // Size
00363                        .concat(0)           // Align
00364                        .concat(0)           // Offset
00365                        .concat(0)           // Flags
00366                        .get(VMContext),
00367                    nullptr, Ty.getRef(), FriendTy.getRef()};
00368   return DIDerivedType(MDNode::get(VMContext, Elts));
00369 }
00370 
00371 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
00372                                            uint64_t BaseOffset,
00373                                            unsigned Flags) {
00374   assert(Ty.isType() && "Unable to create inheritance");
00375   // TAG_inheritance is encoded in DIDerivedType format.
00376   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance)
00377                        .concat(StringRef()) // Name
00378                        .concat(0)           // Line
00379                        .concat(0)           // Size
00380                        .concat(0)           // Align
00381                        .concat(BaseOffset)
00382                        .concat(Flags)
00383                        .get(VMContext),
00384                    nullptr, Ty.getRef(), BaseTy.getRef()};
00385   return DIDerivedType(MDNode::get(VMContext, Elts));
00386 }
00387 
00388 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
00389                                           DIFile File, unsigned LineNumber,
00390                                           uint64_t SizeInBits,
00391                                           uint64_t AlignInBits,
00392                                           uint64_t OffsetInBits, unsigned Flags,
00393                                           DIType Ty) {
00394   // TAG_member is encoded in DIDerivedType format.
00395   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
00396                        .concat(Name)
00397                        .concat(LineNumber)
00398                        .concat(SizeInBits)
00399                        .concat(AlignInBits)
00400                        .concat(OffsetInBits)
00401                        .concat(Flags)
00402                        .get(VMContext),
00403                    File.getFileNode(),
00404                    DIScope(getNonCompileUnitScope(Scope)).getRef(),
00405                    Ty.getRef()};
00406   return DIDerivedType(MDNode::get(VMContext, Elts));
00407 }
00408 
00409 DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope,
00410                                                 StringRef Name, DIFile File,
00411                                                 unsigned LineNumber, DIType Ty,
00412                                                 unsigned Flags,
00413                                                 llvm::Constant *Val) {
00414   // TAG_member is encoded in DIDerivedType format.
00415   Flags |= DIDescriptor::FlagStaticMember;
00416   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
00417                        .concat(Name)
00418                        .concat(LineNumber)
00419                        .concat(0) // Size
00420                        .concat(0) // Align
00421                        .concat(0) // Offset
00422                        .concat(Flags)
00423                        .get(VMContext),
00424                    File.getFileNode(),
00425                    DIScope(getNonCompileUnitScope(Scope)).getRef(), Ty.getRef(),
00426                    Val};
00427   return DIDerivedType(MDNode::get(VMContext, Elts));
00428 }
00429 
00430 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
00431                                         unsigned LineNumber,
00432                                         uint64_t SizeInBits,
00433                                         uint64_t AlignInBits,
00434                                         uint64_t OffsetInBits, unsigned Flags,
00435                                         DIType Ty, MDNode *PropertyNode) {
00436   // TAG_member is encoded in DIDerivedType format.
00437   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
00438                        .concat(Name)
00439                        .concat(LineNumber)
00440                        .concat(SizeInBits)
00441                        .concat(AlignInBits)
00442                        .concat(OffsetInBits)
00443                        .concat(Flags)
00444                        .get(VMContext),
00445                    File.getFileNode(), getNonCompileUnitScope(File), Ty,
00446                    PropertyNode};
00447   return DIDerivedType(MDNode::get(VMContext, Elts));
00448 }
00449 
00450 DIObjCProperty
00451 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
00452                               StringRef GetterName, StringRef SetterName,
00453                               unsigned PropertyAttributes, DIType Ty) {
00454   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
00455                        .concat(Name)
00456                        .concat(LineNumber)
00457                        .concat(GetterName)
00458                        .concat(SetterName)
00459                        .concat(PropertyAttributes)
00460                        .get(VMContext),
00461                    File, Ty};
00462   return DIObjCProperty(MDNode::get(VMContext, Elts));
00463 }
00464 
00465 DITemplateTypeParameter
00466 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
00467                                        DIType Ty, MDNode *File, unsigned LineNo,
00468                                        unsigned ColumnNo) {
00469   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
00470                        .concat(Name)
00471                        .concat(LineNo)
00472                        .concat(ColumnNo)
00473                        .get(VMContext),
00474                    DIScope(getNonCompileUnitScope(Context)).getRef(),
00475                    Ty.getRef(), File};
00476   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
00477 }
00478 
00479 static DITemplateValueParameter createTemplateValueParameterHelper(
00480     LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name,
00481     DIType Ty, Value *Val, MDNode *File, unsigned LineNo, unsigned ColumnNo) {
00482   Value *Elts[] = {
00483       HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get(
00484           VMContext),
00485       DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), Val,
00486       File};
00487   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
00488 }
00489 
00490 DITemplateValueParameter
00491 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
00492                                         DIType Ty, Constant *Val, MDNode *File,
00493                                         unsigned LineNo, unsigned ColumnNo) {
00494   return createTemplateValueParameterHelper(
00495       VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, Val,
00496       File, LineNo, ColumnNo);
00497 }
00498 
00499 DITemplateValueParameter
00500 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
00501                                            DIType Ty, StringRef Val,
00502                                            MDNode *File, unsigned LineNo,
00503                                            unsigned ColumnNo) {
00504   return createTemplateValueParameterHelper(
00505       VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
00506       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
00507 }
00508 
00509 DITemplateValueParameter
00510 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
00511                                        DIType Ty, DIArray Val,
00512                                        MDNode *File, unsigned LineNo,
00513                                        unsigned ColumnNo) {
00514   return createTemplateValueParameterHelper(
00515       VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
00516       Val, File, LineNo, ColumnNo);
00517 }
00518 
00519 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
00520                                            DIFile File, unsigned LineNumber,
00521                                            uint64_t SizeInBits,
00522                                            uint64_t AlignInBits,
00523                                            uint64_t OffsetInBits,
00524                                            unsigned Flags, DIType DerivedFrom,
00525                                            DIArray Elements,
00526                                            DIType VTableHolder,
00527                                            MDNode *TemplateParams,
00528                                            StringRef UniqueIdentifier) {
00529   assert((!Context || Context.isScope() || Context.isType()) &&
00530          "createClassType should be called with a valid Context");
00531   // TAG_class_type is encoded in DICompositeType format.
00532   Value *Elts[] = {
00533       HeaderBuilder::get(dwarf::DW_TAG_class_type)
00534           .concat(Name)
00535           .concat(LineNumber)
00536           .concat(SizeInBits)
00537           .concat(AlignInBits)
00538           .concat(OffsetInBits)
00539           .concat(Flags)
00540           .concat(0)
00541           .get(VMContext),
00542       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
00543       DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
00544       UniqueIdentifier.empty() ? nullptr
00545                                : MDString::get(VMContext, UniqueIdentifier)};
00546   DICompositeType R(MDNode::get(VMContext, Elts));
00547   assert(R.isCompositeType() &&
00548          "createClassType should return a DICompositeType");
00549   if (!UniqueIdentifier.empty())
00550     retainType(R);
00551   return R;
00552 }
00553 
00554 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
00555                                             StringRef Name, DIFile File,
00556                                             unsigned LineNumber,
00557                                             uint64_t SizeInBits,
00558                                             uint64_t AlignInBits,
00559                                             unsigned Flags, DIType DerivedFrom,
00560                                             DIArray Elements,
00561                                             unsigned RunTimeLang,
00562                                             DIType VTableHolder,
00563                                             StringRef UniqueIdentifier) {
00564  // TAG_structure_type is encoded in DICompositeType format.
00565   Value *Elts[] = {
00566       HeaderBuilder::get(dwarf::DW_TAG_structure_type)
00567           .concat(Name)
00568           .concat(LineNumber)
00569           .concat(SizeInBits)
00570           .concat(AlignInBits)
00571           .concat(0)
00572           .concat(Flags)
00573           .concat(RunTimeLang)
00574           .get(VMContext),
00575       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
00576       DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
00577       UniqueIdentifier.empty() ? nullptr
00578                                : MDString::get(VMContext, UniqueIdentifier)};
00579   DICompositeType R(MDNode::get(VMContext, Elts));
00580   assert(R.isCompositeType() &&
00581          "createStructType should return a DICompositeType");
00582   if (!UniqueIdentifier.empty())
00583     retainType(R);
00584   return R;
00585 }
00586 
00587 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
00588                                            DIFile File, unsigned LineNumber,
00589                                            uint64_t SizeInBits,
00590                                            uint64_t AlignInBits, unsigned Flags,
00591                                            DIArray Elements,
00592                                            unsigned RunTimeLang,
00593                                            StringRef UniqueIdentifier) {
00594   // TAG_union_type is encoded in DICompositeType format.
00595   Value *Elts[] = {
00596       HeaderBuilder::get(dwarf::DW_TAG_union_type)
00597           .concat(Name)
00598           .concat(LineNumber)
00599           .concat(SizeInBits)
00600           .concat(AlignInBits)
00601           .concat(0) // Offset
00602           .concat(Flags)
00603           .concat(RunTimeLang)
00604           .get(VMContext),
00605       File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
00606       nullptr, Elements, nullptr, nullptr,
00607       UniqueIdentifier.empty() ? nullptr
00608                                : MDString::get(VMContext, UniqueIdentifier)};
00609   DICompositeType R(MDNode::get(VMContext, Elts));
00610   if (!UniqueIdentifier.empty())
00611     retainType(R);
00612   return R;
00613 }
00614 
00615 DISubroutineType DIBuilder::createSubroutineType(DIFile File,
00616                                                  DITypeArray ParameterTypes,
00617                                                  unsigned Flags) {
00618   // TAG_subroutine_type is encoded in DICompositeType format.
00619   Value *Elts[] = {
00620       HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
00621           .concat(StringRef())
00622           .concat(0)     // Line
00623           .concat(0)     // Size
00624           .concat(0)     // Align
00625           .concat(0)     // Offset
00626           .concat(Flags) // Flags
00627           .concat(0)
00628           .get(VMContext),
00629       nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
00630       nullptr // Type Identifer
00631   };
00632   return DISubroutineType(MDNode::get(VMContext, Elts));
00633 }
00634 
00635 DICompositeType DIBuilder::createEnumerationType(
00636     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
00637     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
00638     DIType UnderlyingType, StringRef UniqueIdentifier) {
00639   // TAG_enumeration_type is encoded in DICompositeType format.
00640   Value *Elts[] = {
00641       HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
00642           .concat(Name)
00643           .concat(LineNumber)
00644           .concat(SizeInBits)
00645           .concat(AlignInBits)
00646           .concat(0) // Offset
00647           .concat(0) // Flags
00648           .concat(0)
00649           .get(VMContext),
00650       File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
00651       UnderlyingType.getRef(), Elements, nullptr, nullptr,
00652       UniqueIdentifier.empty() ? nullptr
00653                                : MDString::get(VMContext, UniqueIdentifier)};
00654   DICompositeType CTy(MDNode::get(VMContext, Elts));
00655   AllEnumTypes.push_back(CTy);
00656   if (!UniqueIdentifier.empty())
00657     retainType(CTy);
00658   return CTy;
00659 }
00660 
00661 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
00662                                            DIType Ty, DIArray Subscripts) {
00663   // TAG_array_type is encoded in DICompositeType format.
00664   Value *Elts[] = {
00665       HeaderBuilder::get(dwarf::DW_TAG_array_type)
00666           .concat(StringRef())
00667           .concat(0) // Line
00668           .concat(Size)
00669           .concat(AlignInBits)
00670           .concat(0) // Offset
00671           .concat(0) // Flags
00672           .concat(0)
00673           .get(VMContext),
00674       nullptr, // Filename/Directory,
00675       nullptr, // Unused
00676       Ty.getRef(), Subscripts, nullptr, nullptr,
00677       nullptr // Type Identifer
00678   };
00679   return DICompositeType(MDNode::get(VMContext, Elts));
00680 }
00681 
00682 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
00683                                             DIType Ty, DIArray Subscripts) {
00684   // A vector is an array type with the FlagVector flag applied.
00685   Value *Elts[] = {
00686       HeaderBuilder::get(dwarf::DW_TAG_array_type)
00687           .concat("")
00688           .concat(0) // Line
00689           .concat(Size)
00690           .concat(AlignInBits)
00691           .concat(0) // Offset
00692           .concat(DIType::FlagVector)
00693           .concat(0)
00694           .get(VMContext),
00695       nullptr, // Filename/Directory,
00696       nullptr, // Unused
00697       Ty.getRef(), Subscripts, nullptr, nullptr,
00698       nullptr // Type Identifer
00699   };
00700   return DICompositeType(MDNode::get(VMContext, Elts));
00701 }
00702 
00703 static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
00704                                           unsigned FlagsToSet) {
00705   DIHeaderFieldIterator I(Header);
00706   std::advance(I, 6);
00707 
00708   unsigned Flags;
00709   if (I->getAsInteger(0, Flags))
00710     Flags = 0;
00711   Flags |= FlagsToSet;
00712 
00713   return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat(
00714       I.getSuffix());
00715 }
00716 
00717 static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
00718                                   unsigned FlagsToSet) {
00719   SmallVector<Value *, 9> Elts;
00720   MDNode *N = Ty;
00721   assert(N && "Unexpected input DIType!");
00722   // Update header field.
00723   Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
00724   for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
00725     Elts.push_back(N->getOperand(I));
00726 
00727   return DIType(MDNode::get(Context, Elts));
00728 }
00729 
00730 DIType DIBuilder::createArtificialType(DIType Ty) {
00731   if (Ty.isArtificial())
00732     return Ty;
00733   return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
00734 }
00735 
00736 DIType DIBuilder::createObjectPointerType(DIType Ty) {
00737   if (Ty.isObjectPointer())
00738     return Ty;
00739   unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
00740   return createTypeWithFlags(VMContext, Ty, Flags);
00741 }
00742 
00743 void DIBuilder::retainType(DIType T) {
00744   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
00745 }
00746 
00747 DIBasicType DIBuilder::createUnspecifiedParameter() {
00748   return DIBasicType();
00749 }
00750 
00751 DICompositeType
00752 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
00753                              DIFile F, unsigned Line, unsigned RuntimeLang,
00754                              uint64_t SizeInBits, uint64_t AlignInBits,
00755                              StringRef UniqueIdentifier) {
00756   // Create a temporary MDNode.
00757   Value *Elts[] = {
00758       HeaderBuilder::get(Tag)
00759           .concat(Name)
00760           .concat(Line)
00761           .concat(SizeInBits)
00762           .concat(AlignInBits)
00763           .concat(0) // Offset
00764           .concat(DIDescriptor::FlagFwdDecl)
00765           .concat(RuntimeLang)
00766           .get(VMContext),
00767       F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
00768       DIArray(), nullptr,
00769       nullptr, // TemplateParams
00770       UniqueIdentifier.empty() ? nullptr
00771                                : MDString::get(VMContext, UniqueIdentifier)};
00772   MDNode *Node = MDNode::get(VMContext, Elts);
00773   DICompositeType RetTy(Node);
00774   assert(RetTy.isCompositeType() &&
00775          "createForwardDecl result should be a DIType");
00776   if (!UniqueIdentifier.empty())
00777     retainType(RetTy);
00778   return RetTy;
00779 }
00780 
00781 DICompositeType DIBuilder::createReplaceableForwardDecl(
00782     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
00783     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
00784     StringRef UniqueIdentifier) {
00785   // Create a temporary MDNode.
00786   Value *Elts[] = {
00787       HeaderBuilder::get(Tag)
00788           .concat(Name)
00789           .concat(Line)
00790           .concat(SizeInBits)
00791           .concat(AlignInBits)
00792           .concat(0) // Offset
00793           .concat(DIDescriptor::FlagFwdDecl)
00794           .concat(RuntimeLang)
00795           .get(VMContext),
00796       F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
00797       DIArray(), nullptr,
00798       nullptr, // TemplateParams
00799       UniqueIdentifier.empty() ? nullptr
00800                                : MDString::get(VMContext, UniqueIdentifier)};
00801   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
00802   DICompositeType RetTy(Node);
00803   assert(RetTy.isCompositeType() &&
00804          "createReplaceableForwardDecl result should be a DIType");
00805   if (!UniqueIdentifier.empty())
00806     retainType(RetTy);
00807   return RetTy;
00808 }
00809 
00810 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
00811   return DIArray(MDNode::get(VMContext, Elements));
00812 }
00813 
00814 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
00815   SmallVector<llvm::Value *, 16> Elts; 
00816   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
00817     if (Elements[i] && isa<MDNode>(Elements[i]))
00818       Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
00819     else
00820       Elts.push_back(Elements[i]);
00821   }
00822   return DITypeArray(MDNode::get(VMContext, Elts));
00823 }
00824 
00825 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
00826   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
00827                        .concat(Lo)
00828                        .concat(Count)
00829                        .get(VMContext)};
00830 
00831   return DISubrange(MDNode::get(VMContext, Elts));
00832 }
00833 
00834 static DIGlobalVariable createGlobalVariableHelper(
00835     LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
00836     StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty,
00837     bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition,
00838     std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
00839   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
00840                        .concat(Name)
00841                        .concat(Name)
00842                        .concat(LinkageName)
00843                        .concat(LineNumber)
00844                        .concat(isLocalToUnit)
00845                        .concat(isDefinition)
00846                        .get(VMContext),
00847                    DIScope(getNonCompileUnitScope(Context)).getRef(), F, Ty, Val,
00848                    DIDescriptor(Decl)};
00849 
00850   return DIGlobalVariable(CreateFunc(Elts));
00851 }
00852 
00853 DIGlobalVariable DIBuilder::createGlobalVariable(
00854     DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
00855     unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
00856     MDNode *Decl) {
00857   return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
00858                                     LineNumber, Ty, isLocalToUnit, Val, Decl, true,
00859                                     [&] (ArrayRef<Value *> Elts) -> MDNode * {
00860                                       MDNode *Node = MDNode::get(VMContext, Elts);
00861                                       AllGVs.push_back(Node);
00862                                       return Node;
00863                                     });
00864 }
00865 
00866 DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
00867     DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F,
00868     unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val,
00869     MDNode *Decl) {
00870   return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
00871                                     LineNumber, Ty, isLocalToUnit, Val, Decl, false,
00872                                     [&] (ArrayRef<Value *> Elts) {
00873                                       return MDNode::getTemporary(VMContext, Elts);
00874                                     });
00875 }
00876 
00877 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
00878                                           StringRef Name, DIFile File,
00879                                           unsigned LineNo, DITypeRef Ty,
00880                                           bool AlwaysPreserve, unsigned Flags,
00881                                           unsigned ArgNo) {
00882   DIDescriptor Context(getNonCompileUnitScope(Scope));
00883   assert((!Context || Context.isScope()) &&
00884          "createLocalVariable should be called with a valid Context");
00885   Value *Elts[] = {HeaderBuilder::get(Tag)
00886                        .concat(Name)
00887                        .concat(LineNo | (ArgNo << 24))
00888                        .concat(Flags)
00889                        .get(VMContext),
00890                    getNonCompileUnitScope(Scope), File, Ty};
00891   MDNode *Node = MDNode::get(VMContext, Elts);
00892   if (AlwaysPreserve) {
00893     // The optimizer may remove local variable. If there is an interest
00894     // to preserve variable info in such situation then stash it in a
00895     // named mdnode.
00896     DISubprogram Fn(getDISubprogram(Scope));
00897     assert(Fn && "Missing subprogram for local variable");
00898     PreservedVariables[Fn].push_back(Node);
00899   }
00900   DIVariable RetVar(Node);
00901   assert(RetVar.isVariable() &&
00902          "createLocalVariable should return a valid DIVariable");
00903   return RetVar;
00904 }
00905 
00906 DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) {
00907   auto Header = HeaderBuilder::get(DW_TAG_expression);
00908   for (int64_t I : Addr)
00909     Header.concat(I);
00910   Value *Elts[] = {Header.get(VMContext)};
00911   return DIExpression(MDNode::get(VMContext, Elts));
00912 }
00913 
00914 DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes,
00915                                               unsigned SizeInBytes) {
00916   int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes};
00917   return createExpression(Addr);
00918 }
00919 
00920 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
00921                                        StringRef LinkageName, DIFile File,
00922                                        unsigned LineNo, DICompositeType Ty,
00923                                        bool isLocalToUnit, bool isDefinition,
00924                                        unsigned ScopeLine, unsigned Flags,
00925                                        bool isOptimized, Function *Fn,
00926                                        MDNode *TParams, MDNode *Decl) {
00927   // dragonegg does not generate identifier for types, so using an empty map
00928   // to resolve the context should be fine.
00929   DITypeIdentifierMap EmptyMap;
00930   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
00931                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
00932                         Flags, isOptimized, Fn, TParams, Decl);
00933 }
00934 
00935 static DISubprogram
00936 createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
00937                      StringRef LinkageName, DIFile File, unsigned LineNo,
00938                      DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
00939                      unsigned ScopeLine, unsigned Flags, bool isOptimized,
00940                      Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars,
00941                      std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
00942   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
00943          "function types should be subroutines");
00944   Value *Elts[] = {
00945       HeaderBuilder::get(dwarf::DW_TAG_subprogram)
00946           .concat(Name)
00947           .concat(Name)
00948           .concat(LinkageName)
00949           .concat(LineNo)
00950           .concat(isLocalToUnit)
00951           .concat(isDefinition)
00952           .concat(0)
00953           .concat(0)
00954           .concat(Flags)
00955           .concat(isOptimized)
00956           .concat(ScopeLine)
00957           .get(VMContext),
00958       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
00959       nullptr, Fn, TParams, Decl, Vars};
00960 
00961   DISubprogram S(CreateFunc(Elts));
00962   assert(S.isSubprogram() &&
00963          "createFunction should return a valid DISubprogram");
00964   return S;
00965 }
00966 
00967 
00968 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
00969                                        StringRef LinkageName, DIFile File,
00970                                        unsigned LineNo, DICompositeType Ty,
00971                                        bool isLocalToUnit, bool isDefinition,
00972                                        unsigned ScopeLine, unsigned Flags,
00973                                        bool isOptimized, Function *Fn,
00974                                        MDNode *TParams, MDNode *Decl) {
00975   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
00976                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
00977                               Flags, isOptimized, Fn, TParams, Decl,
00978                               MDNode::getTemporary(VMContext, None),
00979                               [&] (ArrayRef<Value *> Elts) -> MDNode *{
00980                                 MDNode *Node = MDNode::get(VMContext, Elts);
00981                                 // Create a named metadata so that we
00982                                 // do not lose this mdnode.
00983                                 if (isDefinition)
00984                                   AllSubprograms.push_back(Node);
00985                                 return Node;
00986                               });
00987 }
00988 
00989 DISubprogram
00990 DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
00991                                      StringRef LinkageName, DIFile File,
00992                                      unsigned LineNo, DICompositeType Ty,
00993                                      bool isLocalToUnit, bool isDefinition,
00994                                      unsigned ScopeLine, unsigned Flags,
00995                                      bool isOptimized, Function *Fn,
00996                                      MDNode *TParams, MDNode *Decl) {
00997   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
00998                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
00999                               Flags, isOptimized, Fn, TParams, Decl, nullptr,
01000                               [&] (ArrayRef<Value *> Elts) {
01001                                 return MDNode::getTemporary(VMContext, Elts);
01002                               });
01003 }
01004 
01005 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
01006                                      StringRef LinkageName, DIFile F,
01007                                      unsigned LineNo, DICompositeType Ty,
01008                                      bool isLocalToUnit, bool isDefinition,
01009                                      unsigned VK, unsigned VIndex,
01010                                      DIType VTableHolder, unsigned Flags,
01011                                      bool isOptimized, Function *Fn,
01012                                      MDNode *TParam) {
01013   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
01014          "function types should be subroutines");
01015   assert(getNonCompileUnitScope(Context) &&
01016          "Methods should have both a Context and a context that isn't "
01017          "the compile unit.");
01018   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
01019                        .concat(Name)
01020                        .concat(Name)
01021                        .concat(LinkageName)
01022                        .concat(LineNo)
01023                        .concat(isLocalToUnit)
01024                        .concat(isDefinition)
01025                        .concat(VK)
01026                        .concat(VIndex)
01027                        .concat(Flags)
01028                        .concat(isOptimized)
01029                        .concat(LineNo)
01030                        // FIXME: Do we want to use different scope/lines?
01031                        .get(VMContext),
01032                    F.getFileNode(), DIScope(Context).getRef(), Ty,
01033                    VTableHolder.getRef(), Fn, TParam, nullptr, nullptr};
01034   MDNode *Node = MDNode::get(VMContext, Elts);
01035   if (isDefinition)
01036     AllSubprograms.push_back(Node);
01037   DISubprogram S(Node);
01038   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
01039   return S;
01040 }
01041 
01042 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
01043                                        DIFile File, unsigned LineNo) {
01044   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
01045                        .concat(Name)
01046                        .concat(LineNo)
01047                        .get(VMContext),
01048                    File.getFileNode(), getNonCompileUnitScope(Scope)};
01049   DINameSpace R(MDNode::get(VMContext, Elts));
01050   assert(R.Verify() &&
01051          "createNameSpace should return a verifiable DINameSpace");
01052   return R;
01053 }
01054 
01055 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
01056                                                      DIFile File,
01057                                                      unsigned Discriminator) {
01058   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
01059                        .concat(Discriminator)
01060                        .get(VMContext),
01061                    File.getFileNode(), Scope};
01062   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
01063   assert(
01064       R.Verify() &&
01065       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
01066   return R;
01067 }
01068 
01069 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
01070                                              unsigned Line, unsigned Col) {
01071   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
01072   // I believe the right way is to have a self-referential element in the node.
01073   // Also: why do we bother with line/column - they're not used and the
01074   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
01075   // for uniquing, yet then we have this other solution (because line/col were
01076   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
01077 
01078   // Defeat MDNode uniquing for lexical blocks by using unique id.
01079   static unsigned int unique_id = 0;
01080   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
01081                        .concat(Line)
01082                        .concat(Col)
01083                        .concat(unique_id++)
01084                        .get(VMContext),
01085                    File.getFileNode(), getNonCompileUnitScope(Scope)};
01086   DILexicalBlock R(MDNode::get(VMContext, Elts));
01087   assert(R.Verify() &&
01088          "createLexicalBlock should return a verifiable DILexicalBlock");
01089   return R;
01090 }
01091 
01092 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01093                                       DIExpression Expr,
01094                                       Instruction *InsertBefore) {
01095   assert(Storage && "no storage passed to dbg.declare");
01096   assert(VarInfo.isVariable() &&
01097          "empty or invalid DIVariable passed to dbg.declare");
01098   if (!DeclareFn)
01099     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01100 
01101   Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
01102   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
01103 }
01104 
01105 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
01106                                       DIExpression Expr,
01107                                       BasicBlock *InsertAtEnd) {
01108   assert(Storage && "no storage passed to dbg.declare");
01109   assert(VarInfo.isVariable() &&
01110          "empty or invalid DIVariable passed to dbg.declare");
01111   if (!DeclareFn)
01112     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
01113 
01114   Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
01115 
01116   // If this block already has a terminator then insert this intrinsic
01117   // before the terminator.
01118   if (TerminatorInst *T = InsertAtEnd->getTerminator())
01119     return CallInst::Create(DeclareFn, Args, "", T);
01120   else
01121     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
01122 }
01123 
01124 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01125                                                 DIVariable VarInfo,
01126                                                 DIExpression Expr,
01127                                                 Instruction *InsertBefore) {
01128   assert(V && "no value passed to dbg.value");
01129   assert(VarInfo.isVariable() &&
01130          "empty or invalid DIVariable passed to dbg.value");
01131   if (!ValueFn)
01132     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01133 
01134   Value *Args[] = {MDNode::get(V->getContext(), V),
01135                    ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01136                    VarInfo, Expr};
01137   return CallInst::Create(ValueFn, Args, "", InsertBefore);
01138 }
01139 
01140 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
01141                                                 DIVariable VarInfo,
01142                                                 DIExpression Expr,
01143                                                 BasicBlock *InsertAtEnd) {
01144   assert(V && "no value passed to dbg.value");
01145   assert(VarInfo.isVariable() &&
01146          "empty or invalid DIVariable passed to dbg.value");
01147   if (!ValueFn)
01148     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
01149 
01150   Value *Args[] = {MDNode::get(V->getContext(), V),
01151                    ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
01152                    VarInfo, Expr};
01153   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
01154 }