Revert "Revert "DI: Fold constant arguments into a single MDString""
[opencl/llvm.git] / lib / IR / DIBuilder.cpp
1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the DIBuilder.
11 //
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
23 using namespace llvm;
24 using namespace llvm::dwarf;
26 namespace {
27 class HeaderBuilder {
28   SmallVector<char, 256> Chars;
30 public:
31   explicit HeaderBuilder(Twine T) { T.toVector(Chars); }
32   HeaderBuilder(const HeaderBuilder &X) : Chars(X.Chars) {}
33   HeaderBuilder(HeaderBuilder &&X) : Chars(std::move(X.Chars)) {}
35   template <class Twineable> HeaderBuilder &concat(Twineable &&X) {
36     Chars.push_back(0);
37     Twine(X).toVector(Chars);
38     return *this;
39   }
41   MDString *get(LLVMContext &Context) const {
42     return MDString::get(Context, StringRef(Chars.begin(), Chars.size()));
43   }
45   static HeaderBuilder get(unsigned Tag) {
46     return HeaderBuilder("0x" + Twine::utohexstr(Tag));
47   }
48 };
49 }
51 DIBuilder::DIBuilder(Module &m)
52     : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr),
53       TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
54       DeclareFn(nullptr), ValueFn(nullptr) {}
56 void DIBuilder::finalize() {
57   DIArray Enums = getOrCreateArray(AllEnumTypes);
58   DIType(TempEnumTypes).replaceAllUsesWith(Enums);
60   SmallVector<Value *, 16> RetainValues;
61   // Declarations and definitions of the same type may be retained. Some
62   // clients RAUW these pairs, leaving duplicates in the retained types
63   // list. Use a set to remove the duplicates while we transform the
64   // TrackingVHs back into Values.
65   SmallPtrSet<Value *, 16> RetainSet;
66   for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
67     if (RetainSet.insert(AllRetainTypes[I]))
68       RetainValues.push_back(AllRetainTypes[I]);
69   DIArray RetainTypes = getOrCreateArray(RetainValues);
70   DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
72   DIArray SPs = getOrCreateArray(AllSubprograms);
73   DIType(TempSubprograms).replaceAllUsesWith(SPs);
74   for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
75     DISubprogram SP(SPs.getElement(i));
76     SmallVector<Value *, 4> Variables;
77     if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
78       for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
79         Variables.push_back(NMD->getOperand(ii));
80       NMD->eraseFromParent();
81     }
82     if (MDNode *Temp = SP.getVariablesNodes()) {
83       DIArray AV = getOrCreateArray(Variables);
84       DIType(Temp).replaceAllUsesWith(AV);
85     }
86   }
88   DIArray GVs = getOrCreateArray(AllGVs);
89   DIType(TempGVs).replaceAllUsesWith(GVs);
91   SmallVector<Value *, 16> RetainValuesI;
92   for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
93     RetainValuesI.push_back(AllImportedModules[I]);
94   DIArray IMs = getOrCreateArray(RetainValuesI);
95   DIType(TempImportedModules).replaceAllUsesWith(IMs);
96 }
98 /// If N is compile unit return NULL otherwise return N.
99 static MDNode *getNonCompileUnitScope(MDNode *N) {
100   if (DIDescriptor(N).isCompileUnit())
101     return nullptr;
102   return N;
105 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
106                                   StringRef Directory) {
107   assert(!Filename.empty() && "Unable to create file without name");
108   Value *Pair[] = {
109     MDString::get(VMContext, Filename),
110     MDString::get(VMContext, Directory)
111   };
112   return MDNode::get(VMContext, Pair);
115 DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
116                                            StringRef Directory,
117                                            StringRef Producer, bool isOptimized,
118                                            StringRef Flags, unsigned RunTimeVer,
119                                            StringRef SplitName,
120                                            DebugEmissionKind Kind,
121                                            bool EmitDebugInfo) {
123   assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
124           (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
125          "Invalid Language tag");
126   assert(!Filename.empty() &&
127          "Unable to create compile unit without filename");
128   Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
129   TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
131   TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
133   TempSubprograms = MDNode::getTemporary(VMContext, TElts);
135   TempGVs = MDNode::getTemporary(VMContext, TElts);
137   TempImportedModules = MDNode::getTemporary(VMContext, TElts);
139   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
140                        .concat(Lang)
141                        .concat(Producer)
142                        .concat(isOptimized)
143                        .concat(Flags)
144                        .concat(RunTimeVer)
145                        .concat(SplitName)
146                        .concat(Kind)
147                        .get(VMContext),
148                    createFilePathPair(VMContext, Filename, Directory),
149                    TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs,
150                    TempImportedModules};
152   MDNode *CUNode = MDNode::get(VMContext, Elts);
154   // Create a named metadata so that it is easier to find cu in a module.
155   // Note that we only generate this when the caller wants to actually
156   // emit debug information. When we are only interested in tracking
157   // source line locations throughout the backend, we prevent codegen from
158   // emitting debug info in the final output by not generating llvm.dbg.cu.
159   if (EmitDebugInfo) {
160     NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
161     NMD->addOperand(CUNode);
162   }
164   return DICompileUnit(CUNode);
167 static DIImportedEntity
168 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
169                      Value *NS, unsigned Line, StringRef Name,
170                      SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
171   const MDNode *R;
172   Value *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C),
173                    Context, NS};
174   R = MDNode::get(C, Elts);
175   DIImportedEntity M(R);
176   assert(M.Verify() && "Imported module should be valid");
177   AllImportedModules.push_back(TrackingVH<MDNode>(M));
178   return M;
181 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
182                                                  DINameSpace NS,
183                                                  unsigned Line) {
184   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
185                                 Context, NS, Line, StringRef(), AllImportedModules);
188 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
189                                                  DIImportedEntity NS,
190                                                  unsigned Line) {
191   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
192                                 Context, NS, Line, StringRef(), AllImportedModules);
195 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
196                                                       DIScope Decl,
197                                                       unsigned Line, StringRef Name) {
198   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
199                                 Context, Decl.getRef(), Line, Name,
200                                 AllImportedModules);
203 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
204                                                       DIImportedEntity Imp,
205                                                       unsigned Line, StringRef Name) {
206   return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
207                                 Context, Imp, Line, Name, AllImportedModules);
210 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
211   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext),
212                    createFilePathPair(VMContext, Filename, Directory)};
213   return DIFile(MDNode::get(VMContext, Elts));
216 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
217   assert(!Name.empty() && "Unable to create enumerator without name");
218   Value *Elts[] = {
219       HeaderBuilder::get(dwarf::DW_TAG_enumerator).concat(Name).concat(Val).get(
220           VMContext)};
221   return DIEnumerator(MDNode::get(VMContext, Elts));
224 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
225   assert(!Name.empty() && "Unable to create type without name");
226   // Unspecified types are encoded in DIBasicType format. Line number, filename,
227   // size, alignment, offset and flags are always empty here.
228   Value *Elts[] = {
229       HeaderBuilder::get(dwarf::DW_TAG_unspecified_type)
230           .concat(Name)
231           .concat(0)
232           .concat(0)
233           .concat(0)
234           .concat(0)
235           .concat(0)
236           .concat(0)
237           .get(VMContext),
238       nullptr, // Filename
239       nullptr  // Unused
240   };
241   return DIBasicType(MDNode::get(VMContext, Elts));
244 DIBasicType DIBuilder::createNullPtrType() {
245   return createUnspecifiedType("decltype(nullptr)");
248 DIBasicType
249 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
250                            uint64_t AlignInBits, unsigned Encoding) {
251   assert(!Name.empty() && "Unable to create type without name");
252   // Basic types are encoded in DIBasicType format. Line number, filename,
253   // offset and flags are always empty here.
254   Value *Elts[] = {
255       HeaderBuilder::get(dwarf::DW_TAG_base_type)
256           .concat(Name)
257           .concat(0) // Line
258           .concat(SizeInBits)
259           .concat(AlignInBits)
260           .concat(0) // Offset
261           .concat(0) // Flags
262           .concat(Encoding)
263           .get(VMContext),
264       nullptr, // Filename
265       nullptr  // Unused
266   };
267   return DIBasicType(MDNode::get(VMContext, Elts));
270 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
271   // Qualified types are encoded in DIDerivedType format.
272   Value *Elts[] = {HeaderBuilder::get(Tag)
273                        .concat(StringRef()) // Name
274                        .concat(0)           // Line
275                        .concat(0)           // Size
276                        .concat(0)           // Align
277                        .concat(0)           // Offset
278                        .concat(0)           // Flags
279                        .get(VMContext),
280                    nullptr, // Filename
281                    nullptr, // Unused
282                    FromTy.getRef()};
283   return DIDerivedType(MDNode::get(VMContext, Elts));
286 DIDerivedType
287 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
288                              uint64_t AlignInBits, StringRef Name) {
289   // Pointer types are encoded in DIDerivedType format.
290   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type)
291                        .concat(Name)
292                        .concat(0) // Line
293                        .concat(SizeInBits)
294                        .concat(AlignInBits)
295                        .concat(0) // Offset
296                        .concat(0) // Flags
297                        .get(VMContext),
298                    nullptr, // Filename
299                    nullptr, // Unused
300                    PointeeTy.getRef()};
301   return DIDerivedType(MDNode::get(VMContext, Elts));
304 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
305                                                  DIType Base) {
306   // Pointer types are encoded in DIDerivedType format.
307   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type)
308                        .concat(StringRef())
309                        .concat(0) // Line
310                        .concat(0) // Size
311                        .concat(0) // Align
312                        .concat(0) // Offset
313                        .concat(0) // Flags
314                        .get(VMContext),
315                    nullptr, // Filename
316                    nullptr, // Unused
317                    PointeeTy.getRef(), Base.getRef()};
318   return DIDerivedType(MDNode::get(VMContext, Elts));
321 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
322   assert(RTy.isType() && "Unable to create reference type");
323   // References are encoded in DIDerivedType format.
324   Value *Elts[] = {HeaderBuilder::get(Tag)
325                        .concat(StringRef()) // Name
326                        .concat(0)           // Line
327                        .concat(0)           // Size
328                        .concat(0)           // Align
329                        .concat(0)           // Offset
330                        .concat(0)           // Flags
331                        .get(VMContext),
332                    nullptr, // Filename
333                    nullptr, // TheCU,
334                    RTy.getRef()};
335   return DIDerivedType(MDNode::get(VMContext, Elts));
338 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
339                                        unsigned LineNo, DIDescriptor Context) {
340   // typedefs are encoded in DIDerivedType format.
341   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef)
342                        .concat(Name)
343                        .concat(LineNo)
344                        .concat(0) // Size
345                        .concat(0) // Align
346                        .concat(0) // Offset
347                        .concat(0) // Flags
348                        .get(VMContext),
349                    File.getFileNode(),
350                    DIScope(getNonCompileUnitScope(Context)).getRef(),
351                    Ty.getRef()};
352   return DIDerivedType(MDNode::get(VMContext, Elts));
355 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
356   // typedefs are encoded in DIDerivedType format.
357   assert(Ty.isType() && "Invalid type!");
358   assert(FriendTy.isType() && "Invalid friend type!");
359   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend)
360                        .concat(StringRef()) // Name
361                        .concat(0)           // Line
362                        .concat(0)           // Size
363                        .concat(0)           // Align
364                        .concat(0)           // Offset
365                        .concat(0)           // Flags
366                        .get(VMContext),
367                    nullptr, Ty.getRef(), FriendTy.getRef()};
368   return DIDerivedType(MDNode::get(VMContext, Elts));
371 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
372                                            uint64_t BaseOffset,
373                                            unsigned Flags) {
374   assert(Ty.isType() && "Unable to create inheritance");
375   // TAG_inheritance is encoded in DIDerivedType format.
376   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance)
377                        .concat(StringRef()) // Name
378                        .concat(0)           // Line
379                        .concat(0)           // Size
380                        .concat(0)           // Align
381                        .concat(BaseOffset)
382                        .concat(Flags)
383                        .get(VMContext),
384                    nullptr, Ty.getRef(), BaseTy.getRef()};
385   return DIDerivedType(MDNode::get(VMContext, Elts));
388 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
389                                           DIFile File, unsigned LineNumber,
390                                           uint64_t SizeInBits,
391                                           uint64_t AlignInBits,
392                                           uint64_t OffsetInBits, unsigned Flags,
393                                           DIType Ty) {
394   // TAG_member is encoded in DIDerivedType format.
395   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
396                        .concat(Name)
397                        .concat(LineNumber)
398                        .concat(SizeInBits)
399                        .concat(AlignInBits)
400                        .concat(OffsetInBits)
401                        .concat(Flags)
402                        .get(VMContext),
403                    File.getFileNode(),
404                    DIScope(getNonCompileUnitScope(Scope)).getRef(),
405                    Ty.getRef()};
406   return DIDerivedType(MDNode::get(VMContext, Elts));
409 DIDerivedType
410 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
411                                   DIFile File, unsigned LineNumber,
412                                   DIType Ty, unsigned Flags,
413                                   llvm::Value *Val) {
414   // TAG_member is encoded in DIDerivedType format.
415   Flags |= DIDescriptor::FlagStaticMember;
416   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
417                        .concat(Name)
418                        .concat(LineNumber)
419                        .concat(0) // Size
420                        .concat(0) // Align
421                        .concat(0) // Offset
422                        .concat(Flags)
423                        .get(VMContext),
424                    File.getFileNode(),
425                    DIScope(getNonCompileUnitScope(Scope)).getRef(), Ty.getRef(),
426                    Val};
427   return DIDerivedType(MDNode::get(VMContext, Elts));
430 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
431                                         unsigned LineNumber,
432                                         uint64_t SizeInBits,
433                                         uint64_t AlignInBits,
434                                         uint64_t OffsetInBits, unsigned Flags,
435                                         DIType Ty, MDNode *PropertyNode) {
436   // TAG_member is encoded in DIDerivedType format.
437   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member)
438                        .concat(Name)
439                        .concat(LineNumber)
440                        .concat(SizeInBits)
441                        .concat(AlignInBits)
442                        .concat(OffsetInBits)
443                        .concat(Flags)
444                        .get(VMContext),
445                    File.getFileNode(), getNonCompileUnitScope(File), Ty,
446                    PropertyNode};
447   return DIDerivedType(MDNode::get(VMContext, Elts));
450 DIObjCProperty
451 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
452                               StringRef GetterName, StringRef SetterName,
453                               unsigned PropertyAttributes, DIType Ty) {
454   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property)
455                        .concat(Name)
456                        .concat(LineNumber)
457                        .concat(GetterName)
458                        .concat(SetterName)
459                        .concat(PropertyAttributes)
460                        .get(VMContext),
461                    File, Ty};
462   return DIObjCProperty(MDNode::get(VMContext, Elts));
465 DITemplateTypeParameter
466 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
467                                        DIType Ty, MDNode *File, unsigned LineNo,
468                                        unsigned ColumnNo) {
469   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter)
470                        .concat(Name)
471                        .concat(LineNo)
472                        .concat(ColumnNo)
473                        .get(VMContext),
474                    DIScope(getNonCompileUnitScope(Context)).getRef(),
475                    Ty.getRef(), File};
476   return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
479 DITemplateValueParameter
480 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
481                                         StringRef Name, DIType Ty,
482                                         Value *Val, MDNode *File,
483                                         unsigned LineNo,
484                                         unsigned ColumnNo) {
485   Value *Elts[] = {
486       HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get(
487           VMContext),
488       DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), Val,
489       File};
490   return DITemplateValueParameter(MDNode::get(VMContext, Elts));
493 DITemplateValueParameter
494 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
495                                         DIType Ty, Value *Val,
496                                         MDNode *File, unsigned LineNo,
497                                         unsigned ColumnNo) {
498   return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
499                                       Context, Name, Ty, Val, File, LineNo,
500                                       ColumnNo);
503 DITemplateValueParameter
504 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
505                                            DIType Ty, StringRef Val,
506                                            MDNode *File, unsigned LineNo,
507                                            unsigned ColumnNo) {
508   return createTemplateValueParameter(
509       dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
510       MDString::get(VMContext, Val), File, LineNo, ColumnNo);
513 DITemplateValueParameter
514 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
515                                        DIType Ty, DIArray Val,
516                                        MDNode *File, unsigned LineNo,
517                                        unsigned ColumnNo) {
518   return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
519                                       Context, Name, Ty, Val, File, LineNo,
520                                       ColumnNo);
523 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
524                                            DIFile File, unsigned LineNumber,
525                                            uint64_t SizeInBits,
526                                            uint64_t AlignInBits,
527                                            uint64_t OffsetInBits,
528                                            unsigned Flags, DIType DerivedFrom,
529                                            DIArray Elements,
530                                            DIType VTableHolder,
531                                            MDNode *TemplateParams,
532                                            StringRef UniqueIdentifier) {
533   assert((!Context || Context.isScope() || Context.isType()) &&
534          "createClassType should be called with a valid Context");
535   // TAG_class_type is encoded in DICompositeType format.
536   Value *Elts[] = {
537       HeaderBuilder::get(dwarf::DW_TAG_class_type)
538           .concat(Name)
539           .concat(LineNumber)
540           .concat(SizeInBits)
541           .concat(AlignInBits)
542           .concat(OffsetInBits)
543           .concat(Flags)
544           .concat(0)
545           .get(VMContext),
546       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
547       DerivedFrom.getRef(), Elements, VTableHolder.getRef(), TemplateParams,
548       UniqueIdentifier.empty() ? nullptr
549                                : MDString::get(VMContext, UniqueIdentifier)};
550   DICompositeType R(MDNode::get(VMContext, Elts));
551   assert(R.isCompositeType() &&
552          "createClassType should return a DICompositeType");
553   if (!UniqueIdentifier.empty())
554     retainType(R);
555   return R;
558 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
559                                             StringRef Name, DIFile File,
560                                             unsigned LineNumber,
561                                             uint64_t SizeInBits,
562                                             uint64_t AlignInBits,
563                                             unsigned Flags, DIType DerivedFrom,
564                                             DIArray Elements,
565                                             unsigned RunTimeLang,
566                                             DIType VTableHolder,
567                                             StringRef UniqueIdentifier) {
568  // TAG_structure_type is encoded in DICompositeType format.
569   Value *Elts[] = {
570       HeaderBuilder::get(dwarf::DW_TAG_structure_type)
571           .concat(Name)
572           .concat(LineNumber)
573           .concat(SizeInBits)
574           .concat(AlignInBits)
575           .concat(0)
576           .concat(Flags)
577           .concat(RunTimeLang)
578           .get(VMContext),
579       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(),
580       DerivedFrom.getRef(), Elements, VTableHolder.getRef(), nullptr,
581       UniqueIdentifier.empty() ? nullptr
582                                : MDString::get(VMContext, UniqueIdentifier)};
583   DICompositeType R(MDNode::get(VMContext, Elts));
584   assert(R.isCompositeType() &&
585          "createStructType should return a DICompositeType");
586   if (!UniqueIdentifier.empty())
587     retainType(R);
588   return R;
591 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
592                                            DIFile File, unsigned LineNumber,
593                                            uint64_t SizeInBits,
594                                            uint64_t AlignInBits, unsigned Flags,
595                                            DIArray Elements,
596                                            unsigned RunTimeLang,
597                                            StringRef UniqueIdentifier) {
598   // TAG_union_type is encoded in DICompositeType format.
599   Value *Elts[] = {
600       HeaderBuilder::get(dwarf::DW_TAG_union_type)
601           .concat(Name)
602           .concat(LineNumber)
603           .concat(SizeInBits)
604           .concat(AlignInBits)
605           .concat(0) // Offset
606           .concat(Flags)
607           .concat(RunTimeLang)
608           .get(VMContext),
609       File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
610       nullptr, Elements, nullptr, nullptr,
611       UniqueIdentifier.empty() ? nullptr
612                                : MDString::get(VMContext, UniqueIdentifier)};
613   DICompositeType R(MDNode::get(VMContext, Elts));
614   if (!UniqueIdentifier.empty())
615     retainType(R);
616   return R;
619 DISubroutineType DIBuilder::createSubroutineType(DIFile File,
620                                                  DITypeArray ParameterTypes,
621                                                  unsigned Flags) {
622   // TAG_subroutine_type is encoded in DICompositeType format.
623   Value *Elts[] = {
624       HeaderBuilder::get(dwarf::DW_TAG_subroutine_type)
625           .concat(StringRef())
626           .concat(0)     // Line
627           .concat(0)     // Size
628           .concat(0)     // Align
629           .concat(0)     // Offset
630           .concat(Flags) // Flags
631           .concat(0)
632           .get(VMContext),
633       nullptr, nullptr, nullptr, ParameterTypes, nullptr, nullptr,
634       nullptr // Type Identifer
635   };
636   return DISubroutineType(MDNode::get(VMContext, Elts));
639 DICompositeType DIBuilder::createEnumerationType(
640     DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
641     uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
642     DIType UnderlyingType, StringRef UniqueIdentifier) {
643   // TAG_enumeration_type is encoded in DICompositeType format.
644   Value *Elts[] = {
645       HeaderBuilder::get(dwarf::DW_TAG_enumeration_type)
646           .concat(Name)
647           .concat(LineNumber)
648           .concat(SizeInBits)
649           .concat(AlignInBits)
650           .concat(0) // Offset
651           .concat(0) // Flags
652           .concat(0)
653           .get(VMContext),
654       File.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(),
655       UnderlyingType.getRef(), Elements, nullptr, nullptr,
656       UniqueIdentifier.empty() ? nullptr
657                                : MDString::get(VMContext, UniqueIdentifier)};
658   DICompositeType CTy(MDNode::get(VMContext, Elts));
659   AllEnumTypes.push_back(CTy);
660   if (!UniqueIdentifier.empty())
661     retainType(CTy);
662   return CTy;
665 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
666                                            DIType Ty, DIArray Subscripts) {
667   // TAG_array_type is encoded in DICompositeType format.
668   Value *Elts[] = {
669       HeaderBuilder::get(dwarf::DW_TAG_array_type)
670           .concat(StringRef())
671           .concat(0) // Line
672           .concat(Size)
673           .concat(AlignInBits)
674           .concat(0) // Offset
675           .concat(0) // Flags
676           .concat(0)
677           .get(VMContext),
678       nullptr, // Filename/Directory,
679       nullptr, // Unused
680       Ty.getRef(), Subscripts, nullptr, nullptr,
681       nullptr // Type Identifer
682   };
683   return DICompositeType(MDNode::get(VMContext, Elts));
686 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
687                                             DIType Ty, DIArray Subscripts) {
688   // A vector is an array type with the FlagVector flag applied.
689   Value *Elts[] = {
690       HeaderBuilder::get(dwarf::DW_TAG_array_type)
691           .concat("")
692           .concat(0) // Line
693           .concat(Size)
694           .concat(AlignInBits)
695           .concat(0) // Offset
696           .concat(DIType::FlagVector)
697           .concat(0)
698           .get(VMContext),
699       nullptr, // Filename/Directory,
700       nullptr, // Unused
701       Ty.getRef(), Subscripts, nullptr, nullptr,
702       nullptr // Type Identifer
703   };
704   return DICompositeType(MDNode::get(VMContext, Elts));
707 static HeaderBuilder setTypeFlagsInHeader(StringRef Header,
708                                           unsigned FlagsToSet) {
709   DIHeaderFieldIterator I(Header);
710   std::advance(I, 6);
712   unsigned Flags;
713   if (I->getAsInteger(0, Flags))
714     Flags = 0;
715   Flags |= FlagsToSet;
717   return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat(
718       I.getSuffix());
721 static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
722                                   unsigned FlagsToSet) {
723   SmallVector<Value *, 9> Elts;
724   MDNode *N = Ty;
725   assert(N && "Unexpected input DIType!");
726   // Update header field.
727   Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context));
728   for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I)
729     Elts.push_back(N->getOperand(I));
731   return DIType(MDNode::get(Context, Elts));
734 DIType DIBuilder::createArtificialType(DIType Ty) {
735   if (Ty.isArtificial())
736     return Ty;
737   return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
740 DIType DIBuilder::createObjectPointerType(DIType Ty) {
741   if (Ty.isObjectPointer())
742     return Ty;
743   unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
744   return createTypeWithFlags(VMContext, Ty, Flags);
747 void DIBuilder::retainType(DIType T) {
748   AllRetainTypes.push_back(TrackingVH<MDNode>(T));
751 DIBasicType DIBuilder::createUnspecifiedParameter() {
752   return DIBasicType();
755 DICompositeType
756 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
757                              DIFile F, unsigned Line, unsigned RuntimeLang,
758                              uint64_t SizeInBits, uint64_t AlignInBits,
759                              StringRef UniqueIdentifier) {
760   // Create a temporary MDNode.
761   Value *Elts[] = {
762       HeaderBuilder::get(Tag)
763           .concat(Name)
764           .concat(Line)
765           .concat(SizeInBits)
766           .concat(AlignInBits)
767           .concat(0) // Offset
768           .concat(DIDescriptor::FlagFwdDecl)
769           .concat(RuntimeLang)
770           .get(VMContext),
771       F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
772       DIArray(), nullptr,
773       nullptr, // TemplateParams
774       UniqueIdentifier.empty() ? nullptr
775                                : MDString::get(VMContext, UniqueIdentifier)};
776   MDNode *Node = MDNode::get(VMContext, Elts);
777   DICompositeType RetTy(Node);
778   assert(RetTy.isCompositeType() &&
779          "createForwardDecl result should be a DIType");
780   if (!UniqueIdentifier.empty())
781     retainType(RetTy);
782   return RetTy;
785 DICompositeType DIBuilder::createReplaceableForwardDecl(
786     unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
787     unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
788     StringRef UniqueIdentifier) {
789   // Create a temporary MDNode.
790   Value *Elts[] = {
791       HeaderBuilder::get(Tag)
792           .concat(Name)
793           .concat(Line)
794           .concat(SizeInBits)
795           .concat(AlignInBits)
796           .concat(0) // Offset
797           .concat(DIDescriptor::FlagFwdDecl)
798           .concat(RuntimeLang)
799           .get(VMContext),
800       F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr,
801       DIArray(), nullptr,
802       nullptr, // TemplateParams
803       UniqueIdentifier.empty() ? nullptr
804                                : MDString::get(VMContext, UniqueIdentifier)};
805   MDNode *Node = MDNode::getTemporary(VMContext, Elts);
806   DICompositeType RetTy(Node);
807   assert(RetTy.isCompositeType() &&
808          "createReplaceableForwardDecl result should be a DIType");
809   if (!UniqueIdentifier.empty())
810     retainType(RetTy);
811   return RetTy;
814 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
815   return DIArray(MDNode::get(VMContext, Elements));
818 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
819   SmallVector<llvm::Value *, 16> Elts; 
820   for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
821     if (Elements[i] && isa<MDNode>(Elements[i]))
822       Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
823     else
824       Elts.push_back(Elements[i]);
825   }
826   return DITypeArray(MDNode::get(VMContext, Elts));
829 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
830   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type)
831                        .concat(Lo)
832                        .concat(Count)
833                        .get(VMContext)};
835   return DISubrange(MDNode::get(VMContext, Elts));
838 static DIGlobalVariable
839 createGlobalVariableHelper(LLVMContext &VMContext, DIDescriptor Context,
840                            StringRef Name, StringRef LinkageName, DIFile F,
841                            unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit,
842                            Value *Val, MDNode *Decl, bool isDefinition,
843                            std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
844   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable)
845                        .concat(Name)
846                        .concat(Name)
847                        .concat(LinkageName)
848                        .concat(LineNumber)
849                        .concat(isLocalToUnit)
850                        .concat(isDefinition)
851                        .get(VMContext),
852                    getNonCompileUnitScope(Context), F, Ty, Val,
853                    DIDescriptor(Decl)};
855   return DIGlobalVariable(CreateFunc(Elts));
858 DIGlobalVariable DIBuilder::createGlobalVariable(DIDescriptor Context,
859                                                  StringRef Name,
860                                                  StringRef LinkageName,
861                                                  DIFile F, unsigned LineNumber,
862                                                  DITypeRef Ty,
863                                                  bool isLocalToUnit,
864                                                  Value *Val, MDNode *Decl) {
865   return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
866                                     LineNumber, Ty, isLocalToUnit, Val, Decl, true,
867                                     [&] (ArrayRef<Value *> Elts) -> MDNode * {
868                                       MDNode *Node = MDNode::get(VMContext, Elts);
869                                       AllGVs.push_back(Node);
870                                       return Node;
871                                     });
874 DIGlobalVariable
875 DIBuilder::createTempGlobalVariableFwdDecl(DIDescriptor Context,
876                                            StringRef Name,
877                                            StringRef LinkageName,
878                                            DIFile F, unsigned LineNumber,
879                                            DITypeRef Ty,
880                                            bool isLocalToUnit,
881                                            Value *Val, MDNode *Decl) {
882   return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
883                                     LineNumber, Ty, isLocalToUnit, Val, Decl, false,
884                                     [&] (ArrayRef<Value *> Elts) {
885                                       return MDNode::getTemporary(VMContext, Elts);
886                                     });
889 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
890                                           StringRef Name, DIFile File,
891                                           unsigned LineNo, DITypeRef Ty,
892                                           bool AlwaysPreserve, unsigned Flags,
893                                           unsigned ArgNo) {
894   DIDescriptor Context(getNonCompileUnitScope(Scope));
895   assert((!Context || Context.isScope()) &&
896          "createLocalVariable should be called with a valid Context");
897   Value *Elts[] = {HeaderBuilder::get(Tag)
898                        .concat(Name)
899                        .concat(LineNo | (ArgNo << 24))
900                        .concat(Flags)
901                        .get(VMContext),
902                    getNonCompileUnitScope(Scope), File, Ty};
903   MDNode *Node = MDNode::get(VMContext, Elts);
904   if (AlwaysPreserve) {
905     // The optimizer may remove local variable. If there is an interest
906     // to preserve variable info in such situation then stash it in a
907     // named mdnode.
908     DISubprogram Fn(getDISubprogram(Scope));
909     NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
910     FnLocals->addOperand(Node);
911   }
912   DIVariable RetVar(Node);
913   assert(RetVar.isVariable() &&
914          "createLocalVariable should return a valid DIVariable");
915   return RetVar;
918 DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) {
919   auto Header = HeaderBuilder::get(DW_TAG_expression);
920   for (int64_t I : Addr)
921     Header.concat(I);
922   Value *Elts[] = {Header.get(VMContext)};
923   return DIExpression(MDNode::get(VMContext, Elts));
926 DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes,
927                                               unsigned SizeInBytes) {
928   int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes};
929   return createExpression(Addr);
932 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
933                                        StringRef LinkageName, DIFile File,
934                                        unsigned LineNo, DICompositeType Ty,
935                                        bool isLocalToUnit, bool isDefinition,
936                                        unsigned ScopeLine, unsigned Flags,
937                                        bool isOptimized, Function *Fn,
938                                        MDNode *TParams, MDNode *Decl) {
939   // dragonegg does not generate identifier for types, so using an empty map
940   // to resolve the context should be fine.
941   DITypeIdentifierMap EmptyMap;
942   return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
943                         LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
944                         Flags, isOptimized, Fn, TParams, Decl);
947 static DISubprogram
948 createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
949                      StringRef LinkageName, DIFile File, unsigned LineNo,
950                      DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
951                      unsigned ScopeLine, unsigned Flags, bool isOptimized,
952                      Function *Fn, MDNode *TParams, MDNode *Decl,
953                      std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
954   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
955          "function types should be subroutines");
956   Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
957   Value *Elts[] = {
958       HeaderBuilder::get(dwarf::DW_TAG_subprogram)
959           .concat(Name)
960           .concat(Name)
961           .concat(LinkageName)
962           .concat(LineNo)
963           .concat(isLocalToUnit)
964           .concat(isDefinition)
965           .concat(0)
966           .concat(0)
967           .concat(Flags)
968           .concat(isOptimized)
969           .concat(ScopeLine)
970           .get(VMContext),
971       File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), Ty,
972       nullptr, Fn, TParams, Decl, MDNode::getTemporary(VMContext, TElts)};
974   DISubprogram S(CreateFunc(Elts));
975   assert(S.isSubprogram() &&
976          "createFunction should return a valid DISubprogram");
977   return S;
981 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
982                                        StringRef LinkageName, DIFile File,
983                                        unsigned LineNo, DICompositeType Ty,
984                                        bool isLocalToUnit, bool isDefinition,
985                                        unsigned ScopeLine, unsigned Flags,
986                                        bool isOptimized, Function *Fn,
987                                        MDNode *TParams, MDNode *Decl) {
988   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
989                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
990                               Flags, isOptimized, Fn, TParams, Decl,
991                               [&] (ArrayRef<Value *> Elts) -> MDNode *{
992                                 MDNode *Node = MDNode::get(VMContext, Elts);
993                                 // Create a named metadata so that we
994                                 // do not lose this mdnode.
995                                 if (isDefinition)
996                                   AllSubprograms.push_back(Node);
997                                 return Node;
998                               });
1001 DISubprogram
1002 DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1003                                      StringRef LinkageName, DIFile File,
1004                                      unsigned LineNo, DICompositeType Ty,
1005                                      bool isLocalToUnit, bool isDefinition,
1006                                      unsigned ScopeLine, unsigned Flags,
1007                                      bool isOptimized, Function *Fn,
1008                                      MDNode *TParams, MDNode *Decl) {
1009   return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
1010                               LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1011                               Flags, isOptimized, Fn, TParams, Decl,
1012                               [&] (ArrayRef<Value *> Elts) {
1013                                 return MDNode::getTemporary(VMContext, Elts);
1014                               });
1017 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1018                                      StringRef LinkageName, DIFile F,
1019                                      unsigned LineNo, DICompositeType Ty,
1020                                      bool isLocalToUnit, bool isDefinition,
1021                                      unsigned VK, unsigned VIndex,
1022                                      DIType VTableHolder, unsigned Flags,
1023                                      bool isOptimized, Function *Fn,
1024                                      MDNode *TParam) {
1025   assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1026          "function types should be subroutines");
1027   assert(getNonCompileUnitScope(Context) &&
1028          "Methods should have both a Context and a context that isn't "
1029          "the compile unit.");
1030   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram)
1031                        .concat(Name)
1032                        .concat(Name)
1033                        .concat(LinkageName)
1034                        .concat(LineNo)
1035                        .concat(isLocalToUnit)
1036                        .concat(isDefinition)
1037                        .concat(VK)
1038                        .concat(VIndex)
1039                        .concat(Flags)
1040                        .concat(isOptimized)
1041                        .concat(LineNo)
1042                        // FIXME: Do we want to use different scope/lines?
1043                        .get(VMContext),
1044                    F.getFileNode(), DIScope(Context).getRef(), Ty,
1045                    VTableHolder.getRef(), Fn, TParam, nullptr, nullptr};
1046   MDNode *Node = MDNode::get(VMContext, Elts);
1047   if (isDefinition)
1048     AllSubprograms.push_back(Node);
1049   DISubprogram S(Node);
1050   assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1051   return S;
1054 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1055                                        DIFile File, unsigned LineNo) {
1056   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace)
1057                        .concat(Name)
1058                        .concat(LineNo)
1059                        .get(VMContext),
1060                    File.getFileNode(), getNonCompileUnitScope(Scope)};
1061   DINameSpace R(MDNode::get(VMContext, Elts));
1062   assert(R.Verify() &&
1063          "createNameSpace should return a verifiable DINameSpace");
1064   return R;
1067 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1068                                                      DIFile File,
1069                                                      unsigned Discriminator) {
1070   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1071                        .concat(Discriminator)
1072                        .get(VMContext),
1073                    File.getFileNode(), Scope};
1074   DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1075   assert(
1076       R.Verify() &&
1077       "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1078   return R;
1081 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1082                                              unsigned Line, unsigned Col) {
1083   // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1084   // I believe the right way is to have a self-referential element in the node.
1085   // Also: why do we bother with line/column - they're not used and the
1086   // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1087   // for uniquing, yet then we have this other solution (because line/col were
1088   // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1090   // Defeat MDNode uniquing for lexical blocks by using unique id.
1091   static unsigned int unique_id = 0;
1092   Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block)
1093                        .concat(Line)
1094                        .concat(Col)
1095                        .concat(unique_id++)
1096                        .get(VMContext),
1097                    File.getFileNode(), getNonCompileUnitScope(Scope)};
1098   DILexicalBlock R(MDNode::get(VMContext, Elts));
1099   assert(R.Verify() &&
1100          "createLexicalBlock should return a verifiable DILexicalBlock");
1101   return R;
1104 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1105                                       DIExpression Expr,
1106                                       Instruction *InsertBefore) {
1107   assert(Storage && "no storage passed to dbg.declare");
1108   assert(VarInfo.isVariable() &&
1109          "empty or invalid DIVariable passed to dbg.declare");
1110   if (!DeclareFn)
1111     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1113   Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
1114   return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1117 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1118                                       DIExpression Expr,
1119                                       BasicBlock *InsertAtEnd) {
1120   assert(Storage && "no storage passed to dbg.declare");
1121   assert(VarInfo.isVariable() &&
1122          "empty or invalid DIVariable passed to dbg.declare");
1123   if (!DeclareFn)
1124     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1126   Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
1128   // If this block already has a terminator then insert this intrinsic
1129   // before the terminator.
1130   if (TerminatorInst *T = InsertAtEnd->getTerminator())
1131     return CallInst::Create(DeclareFn, Args, "", T);
1132   else
1133     return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1136 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1137                                                 DIVariable VarInfo,
1138                                                 DIExpression Expr,
1139                                                 Instruction *InsertBefore) {
1140   assert(V && "no value passed to dbg.value");
1141   assert(VarInfo.isVariable() &&
1142          "empty or invalid DIVariable passed to dbg.value");
1143   if (!ValueFn)
1144     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1146   Value *Args[] = {MDNode::get(V->getContext(), V),
1147                    ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1148                    VarInfo, Expr};
1149   return CallInst::Create(ValueFn, Args, "", InsertBefore);
1152 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1153                                                 DIVariable VarInfo,
1154                                                 DIExpression Expr,
1155                                                 BasicBlock *InsertAtEnd) {
1156   assert(V && "no value passed to dbg.value");
1157   assert(VarInfo.isVariable() &&
1158          "empty or invalid DIVariable passed to dbg.value");
1159   if (!ValueFn)
1160     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1162   Value *Args[] = {MDNode::get(V->getContext(), V),
1163                    ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1164                    VarInfo, Expr};
1165   return CallInst::Create(ValueFn, Args, "", InsertAtEnd);