Add a function for profiling to run at shutdown. Unlike the existing API, this
[llvm/stm8.git] / lib / Analysis / DIBuilder.cpp
blob108d2d2ec2b1137603a66800faf5ec506ed5c059
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.
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"
22 using namespace llvm;
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) {
40 Value *Elts[] = {
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
57 /// for a file.
58 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
59 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
60 Value *Elts[] = {
61 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
62 MDString::get(VMContext, Filename),
63 MDString::get(VMContext, Directory),
64 TheCU
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) {
71 Value *Elts[] = {
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
80 /// type, e.g 'char'.
81 DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
82 uint64_t AlignInBits,
83 unsigned Encoding) {
84 // Basic types are encoded in DIBasicType format. Line number, filename,
85 // offset and flags are always empty here.
86 Value *Elts[] = {
87 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
88 TheCU,
89 MDString::get(VMContext, Name),
90 NULL, // Filename
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.
105 Value *Elts[] = {
106 GetTagConstant(VMContext, Tag),
107 TheCU,
108 MDString::get(VMContext, StringRef()), // Empty name.
109 NULL, // Filename
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
115 FromTy
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.
124 Value *Elts[] = {
125 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
126 TheCU,
127 MDString::get(VMContext, Name),
128 NULL, // Filename
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
134 PointeeTy
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.
142 Value *Elts[] = {
143 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
144 TheCU,
145 NULL, // Name
146 NULL, // Filename
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,
159 unsigned LineNo) {
160 // typedefs are encoded in DIDerivedType format.
161 assert(Ty.Verify() && "Invalid typedef type!");
162 Value *Elts[] = {
163 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
164 Ty.getContext(),
165 MDString::get(VMContext, Name),
166 File,
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!");
182 Value *Elts[] = {
183 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
185 NULL, // Name
186 Ty.getFile(),
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
192 FriendTy
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.
202 Value *Elts[] = {
203 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
205 NULL, // Name
206 Ty.getFile(),
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),
212 BaseTy
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,
222 DIType Ty) {
223 // TAG_member is encoded in DIDerivedType format.
224 Value *Elts[] = {
225 GetTagConstant(VMContext, dwarf::DW_TAG_member),
226 File, // Or TheCU ? Ty ?
227 MDString::get(VMContext, Name),
228 File,
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 /// createClassType - Create debugging information entry for a class.
240 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
241 DIFile File, unsigned LineNumber,
242 uint64_t SizeInBits, uint64_t AlignInBits,
243 uint64_t OffsetInBits, unsigned Flags,
244 DIType DerivedFrom, DIArray Elements,
245 MDNode *VTableHoder, MDNode *TemplateParams) {
246 // TAG_class_type is encoded in DICompositeType format.
247 Value *Elts[] = {
248 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
249 Context,
250 MDString::get(VMContext, Name),
251 File,
252 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
253 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
254 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
255 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
256 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
257 DerivedFrom,
258 Elements,
259 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
260 VTableHoder,
261 TemplateParams
263 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
266 /// createTemplateTypeParameter - Create debugging information for template
267 /// type parameter.
268 DITemplateTypeParameter
269 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
270 DIType Ty, MDNode *File, unsigned LineNo,
271 unsigned ColumnNo) {
272 Value *Elts[] = {
273 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
274 Context,
275 MDString::get(VMContext, Name),
277 File,
278 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
279 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
281 return DITemplateTypeParameter(MDNode::get(VMContext, &Elts[0],
282 array_lengthof(Elts)));
285 /// createTemplateValueParameter - Create debugging information for template
286 /// value parameter.
287 DITemplateValueParameter
288 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
289 DIType Ty, uint64_t Val,
290 MDNode *File, unsigned LineNo,
291 unsigned ColumnNo) {
292 Value *Elts[] = {
293 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
294 Context,
295 MDString::get(VMContext, Name),
297 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
298 File,
299 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
300 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
302 return DITemplateValueParameter(MDNode::get(VMContext, &Elts[0],
303 array_lengthof(Elts)));
306 /// createStructType - Create debugging information entry for a struct.
307 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
308 DIFile File, unsigned LineNumber,
309 uint64_t SizeInBits, uint64_t AlignInBits,
310 unsigned Flags, DIArray Elements,
311 unsigned RunTimeLang) {
312 // TAG_structure_type is encoded in DICompositeType format.
313 Value *Elts[] = {
314 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
315 Context,
316 MDString::get(VMContext, Name),
317 File,
318 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
319 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
320 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
321 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
322 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
323 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
324 Elements,
325 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
326 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
328 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
331 /// createUnionType - Create debugging information entry for an union.
332 DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
333 DIFile File,
334 unsigned LineNumber, uint64_t SizeInBits,
335 uint64_t AlignInBits, unsigned Flags,
336 DIArray Elements, unsigned RunTimeLang) {
337 // TAG_union_type is encoded in DICompositeType format.
338 Value *Elts[] = {
339 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
340 Scope,
341 MDString::get(VMContext, Name),
342 File,
343 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
344 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
345 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
346 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
347 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
348 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
349 Elements,
350 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
351 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
353 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
356 /// createSubroutineType - Create subroutine type.
357 DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
358 // TAG_subroutine_type is encoded in DICompositeType format.
359 Value *Elts[] = {
360 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
361 File,
362 MDString::get(VMContext, ""),
363 File,
364 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
365 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
366 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
367 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
368 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
369 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
370 ParameterTypes,
371 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
372 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
374 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
377 /// createEnumerationType - Create debugging information entry for an
378 /// enumeration.
379 DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
380 DIFile File, unsigned LineNumber,
381 uint64_t SizeInBits,
382 uint64_t AlignInBits, DIArray Elements) {
383 // TAG_enumeration_type is encoded in DICompositeType format.
384 Value *Elts[] = {
385 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
386 Scope,
387 MDString::get(VMContext, Name),
388 File,
389 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
390 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
391 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
392 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
393 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
394 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
395 Elements,
396 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
397 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
399 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
400 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
401 NMD->addOperand(Node);
402 return DIType(Node);
405 /// createArrayType - Create debugging information entry for an array.
406 DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
407 DIType Ty, DIArray Subscripts) {
408 // TAG_array_type is encoded in DICompositeType format.
409 Value *Elts[] = {
410 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
411 TheCU,
412 MDString::get(VMContext, ""),
413 TheCU,
414 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
415 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
416 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
417 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
418 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
420 Subscripts,
421 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
422 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
424 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
427 /// createVectorType - Create debugging information entry for a vector.
428 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
429 DIType Ty, DIArray Subscripts) {
430 // TAG_vector_type is encoded in DICompositeType format.
431 Value *Elts[] = {
432 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
433 TheCU,
434 MDString::get(VMContext, ""),
435 TheCU,
436 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
437 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
438 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
439 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
440 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
442 Subscripts,
443 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
444 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
446 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
449 /// createArtificialType - Create a new DIType with "artificial" flag set.
450 DIType DIBuilder::createArtificialType(DIType Ty) {
451 if (Ty.isArtificial())
452 return Ty;
454 SmallVector<Value *, 9> Elts;
455 MDNode *N = Ty;
456 assert (N && "Unexpected input DIType!");
457 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
458 if (Value *V = N->getOperand(i))
459 Elts.push_back(V);
460 else
461 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
464 unsigned CurFlags = Ty.getFlags();
465 CurFlags = CurFlags | DIType::FlagArtificial;
467 // Flags are stored at this slot.
468 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
470 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
473 /// retainType - Retain DIType in a module even if it is not referenced
474 /// through debug info anchors.
475 void DIBuilder::retainType(DIType T) {
476 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
477 NMD->addOperand(T);
480 /// createUnspecifiedParameter - Create unspeicified type descriptor
481 /// for the subroutine type.
482 DIDescriptor DIBuilder::createUnspecifiedParameter() {
483 Value *Elts[] = {
484 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
486 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
489 /// createTemporaryType - Create a temporary forward-declared type.
490 DIType DIBuilder::createTemporaryType() {
491 // Give the temporary MDNode a tag. It doesn't matter what tag we
492 // use here as long as DIType accepts it.
493 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
494 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
495 return DIType(Node);
498 /// createTemporaryType - Create a temporary forward-declared type.
499 DIType DIBuilder::createTemporaryType(DIFile F) {
500 // Give the temporary MDNode a tag. It doesn't matter what tag we
501 // use here as long as DIType accepts it.
502 Value *Elts[] = {
503 GetTagConstant(VMContext, DW_TAG_base_type),
504 F.getCompileUnit(),
505 NULL,
508 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
509 return DIType(Node);
512 /// getOrCreateArray - Get a DIArray, create one if required.
513 DIArray DIBuilder::getOrCreateArray(Value *const *Elements, unsigned NumElements) {
514 if (NumElements == 0) {
515 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
516 return DIArray(MDNode::get(VMContext, &Null, 1));
518 return DIArray(MDNode::get(VMContext, Elements, NumElements));
521 /// getOrCreateSubrange - Create a descriptor for a value range. This
522 /// implicitly uniques the values returned.
523 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Hi) {
524 Value *Elts[] = {
525 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
526 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
527 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
530 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
533 /// createGlobalVariable - Create a new descriptor for the specified global.
534 DIGlobalVariable DIBuilder::
535 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
536 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
537 Value *Elts[] = {
538 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
539 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
540 TheCU,
541 MDString::get(VMContext, Name),
542 MDString::get(VMContext, Name),
543 MDString::get(VMContext, Name),
545 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
547 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
548 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
551 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
552 // Create a named metadata so that we do not lose this mdnode.
553 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
554 NMD->addOperand(Node);
555 return DIGlobalVariable(Node);
558 /// createStaticVariable - Create a new descriptor for the specified static
559 /// variable.
560 DIGlobalVariable DIBuilder::
561 createStaticVariable(DIDescriptor Context, StringRef Name,
562 StringRef LinkageName, DIFile F, unsigned LineNumber,
563 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
564 Value *Elts[] = {
565 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
566 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
567 Context,
568 MDString::get(VMContext, Name),
569 MDString::get(VMContext, Name),
570 MDString::get(VMContext, LinkageName),
572 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
574 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
575 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
578 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
579 // Create a named metadata so that we do not lose this mdnode.
580 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
581 NMD->addOperand(Node);
582 return DIGlobalVariable(Node);
585 /// createVariable - Create a new descriptor for the specified variable.
586 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
587 StringRef Name, DIFile File,
588 unsigned LineNo, DIType Ty,
589 bool AlwaysPreserve, unsigned Flags,
590 unsigned ArgNo) {
591 Value *Elts[] = {
592 GetTagConstant(VMContext, Tag),
593 Scope,
594 MDString::get(VMContext, Name),
595 File,
596 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
598 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
600 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
601 if (AlwaysPreserve) {
602 // The optimizer may remove local variable. If there is an interest
603 // to preserve variable info in such situation then stash it in a
604 // named mdnode.
605 DISubprogram Fn(getDISubprogram(Scope));
606 StringRef FName = "fn";
607 if (Fn.getFunction())
608 FName = Fn.getFunction()->getName();
609 char One = '\1';
610 if (FName.startswith(StringRef(&One, 1)))
611 FName = FName.substr(1);
612 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
613 FnLocals->addOperand(Node);
615 return DIVariable(Node);
618 /// createComplexVariable - Create a new descriptor for the specified variable
619 /// which has a complex address expression for its address.
620 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
621 StringRef Name, DIFile F,
622 unsigned LineNo,
623 DIType Ty, Value *const *Addr,
624 unsigned NumAddr, unsigned ArgNo) {
625 SmallVector<Value *, 15> Elts;
626 Elts.push_back(GetTagConstant(VMContext, Tag));
627 Elts.push_back(Scope);
628 Elts.push_back(MDString::get(VMContext, Name));
629 Elts.push_back(F);
630 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))));
631 Elts.push_back(Ty);
632 Elts.append(Addr, Addr+NumAddr);
634 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
637 /// createFunction - Create a new descriptor for the specified function.
638 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
639 StringRef Name,
640 StringRef LinkageName,
641 DIFile File, unsigned LineNo,
642 DIType Ty,
643 bool isLocalToUnit, bool isDefinition,
644 unsigned Flags, bool isOptimized,
645 Function *Fn,
646 MDNode *TParams) {
647 Value *Elts[] = {
648 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
649 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
650 Context,
651 MDString::get(VMContext, Name),
652 MDString::get(VMContext, Name),
653 MDString::get(VMContext, LinkageName),
654 File,
655 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
657 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
658 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
659 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
660 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
661 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
662 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
663 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
665 TParams
667 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
669 // Create a named metadata so that we do not lose this mdnode.
670 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
671 NMD->addOperand(Node);
672 return DISubprogram(Node);
675 /// createMethod - Create a new descriptor for the specified C++ method.
676 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
677 StringRef Name,
678 StringRef LinkageName,
679 DIFile F,
680 unsigned LineNo, DIType Ty,
681 bool isLocalToUnit,
682 bool isDefinition,
683 unsigned VK, unsigned VIndex,
684 MDNode *VTableHolder,
685 unsigned Flags,
686 bool isOptimized,
687 Function *Fn,
688 MDNode *TParam) {
689 Value *Elts[] = {
690 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
691 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
692 Context,
693 MDString::get(VMContext, Name),
694 MDString::get(VMContext, Name),
695 MDString::get(VMContext, LinkageName),
697 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
699 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
700 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
701 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
702 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
703 VTableHolder,
704 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
705 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
707 TParam,
709 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
711 // Create a named metadata so that we do not lose this mdnode.
712 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
713 NMD->addOperand(Node);
714 return DISubprogram(Node);
717 /// createNameSpace - This creates new descriptor for a namespace
718 /// with the specified parent scope.
719 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
720 DIFile File, unsigned LineNo) {
721 Value *Elts[] = {
722 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
723 Scope,
724 MDString::get(VMContext, Name),
725 File,
726 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
728 return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
731 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
732 unsigned Line, unsigned Col) {
733 // Defeat MDNode uniqing for lexical blocks by using unique id.
734 static unsigned int unique_id = 0;
735 Value *Elts[] = {
736 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
737 Scope,
738 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
739 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
740 File,
741 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
743 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
746 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
747 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
748 Instruction *InsertBefore) {
749 assert(Storage && "no storage passed to dbg.declare");
750 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
751 if (!DeclareFn)
752 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
754 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
755 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
758 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
759 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
760 BasicBlock *InsertAtEnd) {
761 assert(Storage && "no storage passed to dbg.declare");
762 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
763 if (!DeclareFn)
764 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
766 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
768 // If this block already has a terminator then insert this intrinsic
769 // before the terminator.
770 if (TerminatorInst *T = InsertAtEnd->getTerminator())
771 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
772 else
773 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
776 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
777 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
778 DIVariable VarInfo,
779 Instruction *InsertBefore) {
780 assert(V && "no value passed to dbg.value");
781 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
782 if (!ValueFn)
783 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
785 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
786 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
787 VarInfo };
788 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
791 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
792 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
793 DIVariable VarInfo,
794 BasicBlock *InsertAtEnd) {
795 assert(V && "no value passed to dbg.value");
796 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
797 if (!ValueFn)
798 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
800 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
801 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
802 VarInfo };
803 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);