1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DIBuilder.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/DIBuilder.h"
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Dwarf.h"
23 using namespace llvm::dwarf;
25 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
26 assert((Tag & LLVMDebugVersionMask) == 0 &&
27 "Tag too large for debug encoding!");
28 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
31 DIBuilder::DIBuilder(Module &m)
32 : M(m), VMContext(M.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {}
34 /// CreateCompileUnit - A CompileUnit provides an anchor for all debugging
35 /// information generated during this instance of compilation.
36 void DIBuilder::CreateCompileUnit(unsigned Lang, StringRef Filename,
37 StringRef Directory, StringRef Producer,
38 bool isOptimized, StringRef Flags,
39 unsigned RunTimeVer) {
41 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
42 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
43 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
44 MDString::get(VMContext, Filename),
45 MDString::get(VMContext, Directory),
46 MDString::get(VMContext, Producer),
47 // Deprecate isMain field.
48 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
49 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
50 MDString::get(VMContext, Flags),
51 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
53 TheCU = DICompileUnit(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
56 /// CreateFile - Create a file descriptor to hold debugging information
58 DIFile DIBuilder::CreateFile(StringRef Filename, StringRef Directory) {
59 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
61 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
62 MDString::get(VMContext, Filename),
63 MDString::get(VMContext, Directory),
66 return DIFile(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
69 /// CreateEnumerator - Create a single enumerator value.
70 DIEnumerator DIBuilder::CreateEnumerator(StringRef Name, uint64_t Val) {
72 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
73 MDString::get(VMContext, Name),
74 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
76 return DIEnumerator(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
79 /// CreateBasicType - Create debugging information entry for a basic
81 DIType DIBuilder::CreateBasicType(StringRef Name, uint64_t SizeInBits,
84 // Basic types are encoded in DIBasicType format. Line number, filename,
85 // offset and flags are always empty here.
87 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
89 MDString::get(VMContext, Name),
91 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
92 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
93 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
94 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
95 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
96 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
98 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
101 /// CreateQaulifiedType - Create debugging information entry for a qualified
102 /// type, e.g. 'const int'.
103 DIType DIBuilder::CreateQualifiedType(unsigned Tag, DIType FromTy) {
104 // Qualified types are encoded in DIDerivedType format.
106 GetTagConstant(VMContext, Tag),
108 MDString::get(VMContext, StringRef()), // Empty name.
110 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
111 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
112 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
113 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
114 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
117 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
120 /// CreatePointerType - Create debugging information entry for a pointer.
121 DIType DIBuilder::CreatePointerType(DIType PointeeTy, uint64_t SizeInBits,
122 uint64_t AlignInBits, StringRef Name) {
123 // Pointer types are encoded in DIDerivedType format.
125 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
127 MDString::get(VMContext, Name),
129 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
130 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
131 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
132 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
133 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
136 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
139 /// CreateReferenceType - Create debugging information entry for a reference.
140 DIType DIBuilder::CreateReferenceType(DIType RTy) {
141 // References are encoded in DIDerivedType format.
143 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
147 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
148 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
149 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
150 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
151 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
154 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
157 /// CreateTypedef - Create debugging information entry for a typedef.
158 DIType DIBuilder::CreateTypedef(DIType Ty, StringRef Name, DIFile File,
160 // typedefs are encoded in DIDerivedType format.
161 assert(Ty.Verify() && "Invalid typedef type!");
163 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
165 MDString::get(VMContext, Name),
167 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
168 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
169 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
170 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
171 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
174 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
177 /// CreateFriend - Create debugging information entry for a 'friend'.
178 DIType DIBuilder::CreateFriend(DIType Ty, DIType FriendTy) {
179 // typedefs are encoded in DIDerivedType format.
180 assert(Ty.Verify() && "Invalid type!");
181 assert(FriendTy.Verify() && "Invalid friend type!");
183 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
187 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
188 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
189 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
190 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
191 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
194 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
197 /// CreateInheritance - Create debugging information entry to establish
198 /// inheritnace relationship between two types.
199 DIType DIBuilder::CreateInheritance(DIType Ty, DIType BaseTy,
200 uint64_t BaseOffset, unsigned Flags) {
201 // TAG_inheritance is encoded in DIDerivedType format.
203 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
207 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
208 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
209 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
210 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
211 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
214 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
217 /// CreateMemberType - Create debugging information entry for a member.
218 DIType DIBuilder::CreateMemberType(StringRef Name,
219 DIFile File, unsigned LineNumber,
220 uint64_t SizeInBits, uint64_t AlignInBits,
221 uint64_t OffsetInBits, unsigned Flags,
223 // TAG_member is encoded in DIDerivedType format.
225 GetTagConstant(VMContext, dwarf::DW_TAG_member),
226 File, // Or TheCU ? Ty ?
227 MDString::get(VMContext, Name),
229 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
230 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
231 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
232 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
233 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
236 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
239 /// CreateStructType - Create debugging information entry for a struct.
240 DIType DIBuilder::CreateStructType(DIDescriptor Context, StringRef Name,
241 DIFile File, unsigned LineNumber,
242 uint64_t SizeInBits, uint64_t AlignInBits,
243 unsigned Flags, DIArray Elements,
244 unsigned RunTimeLang) {
245 // TAG_structure_type is encoded in DICompositeType format.
247 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
249 MDString::get(VMContext, Name),
251 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
252 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
253 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
254 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
255 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
256 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
258 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
259 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
261 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
264 /// CreateUnionType - Create debugging information entry for an union.
265 DIType DIBuilder::CreateUnionType(DIDescriptor Scope, StringRef Name,
267 unsigned LineNumber, uint64_t SizeInBits,
268 uint64_t AlignInBits, unsigned Flags,
269 DIArray Elements, unsigned RunTimeLang) {
270 // TAG_union_type is encoded in DICompositeType format.
272 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
274 MDString::get(VMContext, Name),
276 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
277 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
278 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
279 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
280 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
281 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
283 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
284 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
286 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
289 /// CreateSubroutineType - Create subroutine type.
290 DIType DIBuilder::CreateSubroutineType(DIFile File, DIArray ParameterTypes) {
291 // TAG_subroutine_type is encoded in DICompositeType format.
293 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
295 MDString::get(VMContext, ""),
297 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
298 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
299 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
300 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
301 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
302 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
304 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
305 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
307 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
310 /// CreateEnumerationType - Create debugging information entry for an
312 DIType DIBuilder::CreateEnumerationType(DIDescriptor Scope, StringRef Name,
313 DIFile File, unsigned LineNumber,
315 uint64_t AlignInBits, DIArray Elements) {
316 // TAG_enumeration_type is encoded in DICompositeType format.
318 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
320 MDString::get(VMContext, Name),
322 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
323 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
324 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
325 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
326 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
327 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
329 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
330 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
332 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
333 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
334 NMD->addOperand(Node);
338 /// CreateArrayType - Create debugging information entry for an array.
339 DIType DIBuilder::CreateArrayType(uint64_t Size, uint64_t AlignInBits,
340 DIType Ty, DIArray Subscripts) {
341 // TAG_array_type is encoded in DICompositeType format.
343 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
345 MDString::get(VMContext, ""),
347 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
348 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
349 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
350 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
351 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
354 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
355 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
357 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
360 /// CreateVectorType - Create debugging information entry for a vector.
361 DIType DIBuilder::CreateVectorType(uint64_t Size, uint64_t AlignInBits,
362 DIType Ty, DIArray Subscripts) {
363 // TAG_vector_type is encoded in DICompositeType format.
365 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
367 MDString::get(VMContext, ""),
369 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
370 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
371 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
372 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
373 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
376 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
377 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
379 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
382 /// CreateArtificialType - Create a new DIType with "artificial" flag set.
383 DIType DIBuilder::CreateArtificialType(DIType Ty) {
384 if (Ty.isArtificial())
387 SmallVector<Value *, 9> Elts;
389 assert (N && "Unexpected input DIType!");
390 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
391 if (Value *V = N->getOperand(i))
394 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
397 unsigned CurFlags = Ty.getFlags();
398 CurFlags = CurFlags | DIType::FlagArtificial;
400 // Flags are stored at this slot.
401 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
403 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
406 /// RetainType - Retain DIType in a module even if it is not referenced
407 /// through debug info anchors.
408 void DIBuilder::RetainType(DIType T) {
409 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
413 /// CreateUnspecifiedParameter - Create unspeicified type descriptor
414 /// for the subroutine type.
415 DIDescriptor DIBuilder::CreateUnspecifiedParameter() {
417 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
419 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
422 /// CreateTemporaryType - Create a temporary forward-declared type.
423 DIType DIBuilder::CreateTemporaryType() {
424 // Give the temporary MDNode a tag. It doesn't matter what tag we
425 // use here as long as DIType accepts it.
426 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
427 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
431 /// CreateTemporaryType - Create a temporary forward-declared type.
432 DIType DIBuilder::CreateTemporaryType(DIFile F) {
433 // Give the temporary MDNode a tag. It doesn't matter what tag we
434 // use here as long as DIType accepts it.
436 GetTagConstant(VMContext, DW_TAG_base_type),
441 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
445 /// GetOrCreateArray - Get a DIArray, create one if required.
446 DIArray DIBuilder::GetOrCreateArray(Value *const *Elements, unsigned NumElements) {
447 if (NumElements == 0) {
448 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
449 return DIArray(MDNode::get(VMContext, &Null, 1));
451 return DIArray(MDNode::get(VMContext, Elements, NumElements));
454 /// GetOrCreateSubrange - Create a descriptor for a value range. This
455 /// implicitly uniques the values returned.
456 DISubrange DIBuilder::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
458 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
459 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
460 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
463 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
466 /// CreateGlobalVariable - Create a new descriptor for the specified global.
467 DIGlobalVariable DIBuilder::
468 CreateGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
469 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
471 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
472 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
474 MDString::get(VMContext, Name),
475 MDString::get(VMContext, Name),
476 MDString::get(VMContext, Name),
478 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
480 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
481 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
484 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
485 // Create a named metadata so that we do not lose this mdnode.
486 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
487 NMD->addOperand(Node);
488 return DIGlobalVariable(Node);
491 /// CreateStaticVariable - Create a new descriptor for the specified static
493 DIGlobalVariable DIBuilder::
494 CreateStaticVariable(DIDescriptor Context, StringRef Name,
495 StringRef LinkageName, DIFile F, unsigned LineNumber,
496 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
498 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
499 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
501 MDString::get(VMContext, Name),
502 MDString::get(VMContext, Name),
503 MDString::get(VMContext, LinkageName),
505 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
507 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
508 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
511 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
512 // Create a named metadata so that we do not lose this mdnode.
513 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
514 NMD->addOperand(Node);
515 return DIGlobalVariable(Node);
518 /// CreateVariable - Create a new descriptor for the specified variable.
519 DIVariable DIBuilder::CreateLocalVariable(unsigned Tag, DIDescriptor Scope,
520 StringRef Name, DIFile File,
521 unsigned LineNo, DIType Ty,
522 bool AlwaysPreserve, unsigned Flags) {
524 GetTagConstant(VMContext, Tag),
526 MDString::get(VMContext, Name),
528 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
530 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
532 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
533 if (AlwaysPreserve) {
534 // The optimizer may remove local variable. If there is an interest
535 // to preserve variable info in such situation then stash it in a
537 DISubprogram Fn(getDISubprogram(Scope));
538 StringRef FName = "fn";
539 if (Fn.getFunction())
540 FName = Fn.getFunction()->getName();
542 if (FName.startswith(StringRef(&One, 1)))
543 FName = FName.substr(1);
544 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
545 FnLocals->addOperand(Node);
547 return DIVariable(Node);
550 /// CreateComplexVariable - Create a new descriptor for the specified variable
551 /// which has a complex address expression for its address.
552 DIVariable DIBuilder::CreateComplexVariable(unsigned Tag, DIDescriptor Scope,
553 StringRef Name, DIFile F,
555 DIType Ty, Value *const *Addr,
557 SmallVector<Value *, 15> Elts;
558 Elts.push_back(GetTagConstant(VMContext, Tag));
559 Elts.push_back(Scope);
560 Elts.push_back(MDString::get(VMContext, Name));
562 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
564 Elts.append(Addr, Addr+NumAddr);
566 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
569 /// CreateNameSpace - This creates new descriptor for a namespace
570 /// with the specified parent scope.
571 DINameSpace DIBuilder::CreateNameSpace(DIDescriptor Scope, StringRef Name,
572 DIFile File, unsigned LineNo) {
574 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
576 MDString::get(VMContext, Name),
578 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
580 return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
583 DILexicalBlock DIBuilder::CreateLexicalBlock(DIDescriptor Scope, DIFile File,
584 unsigned Line, unsigned Col) {
585 // Defeat MDNode uniqing for lexical blocks by using unique id.
586 static unsigned int unique_id = 0;
588 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
590 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
591 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
593 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
595 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
598 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
599 Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
600 Instruction *InsertBefore) {
601 assert(Storage && "no storage passed to dbg.declare");
602 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
604 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
606 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
607 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
610 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
611 Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
612 BasicBlock *InsertAtEnd) {
613 assert(Storage && "no storage passed to dbg.declare");
614 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
616 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
618 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
620 // If this block already has a terminator then insert this intrinsic
621 // before the terminator.
622 if (TerminatorInst *T = InsertAtEnd->getTerminator())
623 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
625 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
628 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
629 Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
631 Instruction *InsertBefore) {
632 assert(V && "no value passed to dbg.value");
633 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
635 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
637 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
638 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
640 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
643 /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
644 Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
646 BasicBlock *InsertAtEnd) {
647 assert(V && "no value passed to dbg.value");
648 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
650 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
652 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
653 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
655 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);