Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 13 | |
| 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" |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Dwarf.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::dwarf; |
| 24 | |
| 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); |
| 29 | } |
| 30 | DIBuilder::DIBuilder(Module &m) |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 31 | : M(m), VMContext(M.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {} |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 32 | |
| 33 | /// CreateCompileUnit - A CompileUnit provides an anchor for all debugging |
| 34 | /// information generated during this instance of compilation. |
| 35 | void DIBuilder::CreateCompileUnit(unsigned Lang, StringRef Filename, |
| 36 | StringRef Directory, StringRef Producer, |
| 37 | bool isOptimized, StringRef Flags, |
| 38 | unsigned RunTimeVer) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 39 | Value *Elts[] = { |
| 40 | GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit), |
| 41 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 42 | ConstantInt::get(Type::getInt32Ty(VMContext), Lang), |
| 43 | MDString::get(VMContext, Filename), |
| 44 | MDString::get(VMContext, Directory), |
| 45 | MDString::get(VMContext, Producer), |
| 46 | // Deprecate isMain field. |
| 47 | ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain |
| 48 | ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized), |
| 49 | MDString::get(VMContext, Flags), |
| 50 | ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer) |
| 51 | }; |
| 52 | TheCU = DICompileUnit(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// CreateFile - Create a file descriptor to hold debugging information |
| 56 | /// for a file. |
| 57 | DIFile DIBuilder::CreateFile(StringRef Filename, StringRef Directory) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 58 | assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit"); |
| 59 | Value *Elts[] = { |
| 60 | GetTagConstant(VMContext, dwarf::DW_TAG_file_type), |
| 61 | MDString::get(VMContext, Filename), |
| 62 | MDString::get(VMContext, Directory), |
| 63 | TheCU |
| 64 | }; |
| 65 | return DIFile(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// CreateEnumerator - Create a single enumerator value. |
| 69 | DIEnumerator DIBuilder::CreateEnumerator(StringRef Name, uint64_t Val) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 70 | Value *Elts[] = { |
| 71 | GetTagConstant(VMContext, dwarf::DW_TAG_enumerator), |
| 72 | MDString::get(VMContext, Name), |
| 73 | ConstantInt::get(Type::getInt64Ty(VMContext), Val) |
| 74 | }; |
| 75 | return DIEnumerator(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /// CreateBasicType - Create debugging information entry for a basic |
| 79 | /// type, e.g 'char'. |
| 80 | DIType DIBuilder::CreateBasicType(StringRef Name, uint64_t SizeInBits, |
| 81 | uint64_t AlignInBits, |
| 82 | unsigned Encoding) { |
| 83 | // Basic types are encoded in DIBasicType format. Line number, filename, |
| 84 | // offset and flags are always empty here. |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 85 | Value *Elts[] = { |
| 86 | GetTagConstant(VMContext, dwarf::DW_TAG_base_type), |
| 87 | TheCU, |
| 88 | MDString::get(VMContext, Name), |
| 89 | NULL, // Filename |
| 90 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 91 | ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), |
| 92 | ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), |
| 93 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 94 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags; |
| 95 | ConstantInt::get(Type::getInt32Ty(VMContext), Encoding) |
| 96 | }; |
| 97 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// CreateQaulifiedType - Create debugging information entry for a qualified |
| 101 | /// type, e.g. 'const int'. |
| 102 | DIType DIBuilder::CreateQualifiedType(unsigned Tag, DIType FromTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 103 | // Qualified types are encoded in DIDerivedType format. |
| 104 | Value *Elts[] = { |
| 105 | GetTagConstant(VMContext, Tag), |
| 106 | TheCU, |
| 107 | MDString::get(VMContext, StringRef()), // Empty name. |
| 108 | NULL, // Filename |
| 109 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 110 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size |
| 111 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align |
| 112 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 113 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags |
| 114 | FromTy |
| 115 | }; |
| 116 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /// CreatePointerType - Create debugging information entry for a pointer. |
| 120 | DIType DIBuilder::CreatePointerType(DIType PointeeTy, uint64_t SizeInBits, |
| 121 | uint64_t AlignInBits, StringRef Name) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 122 | // Pointer types are encoded in DIDerivedType format. |
| 123 | Value *Elts[] = { |
| 124 | GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type), |
| 125 | TheCU, |
| 126 | MDString::get(VMContext, Name), |
| 127 | NULL, // Filename |
| 128 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 129 | ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), |
| 130 | ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), |
| 131 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 132 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags |
| 133 | PointeeTy |
| 134 | }; |
| 135 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /// CreateReferenceType - Create debugging information entry for a reference. |
| 139 | DIType DIBuilder::CreateReferenceType(DIType RTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 140 | // References are encoded in DIDerivedType format. |
| 141 | Value *Elts[] = { |
| 142 | GetTagConstant(VMContext, dwarf::DW_TAG_reference_type), |
| 143 | TheCU, |
| 144 | NULL, // Name |
| 145 | NULL, // Filename |
| 146 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 147 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size |
| 148 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align |
| 149 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 150 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags |
| 151 | RTy |
| 152 | }; |
| 153 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /// CreateTypedef - Create debugging information entry for a typedef. |
| 157 | DIType DIBuilder::CreateTypedef(DIType Ty, StringRef Name, DIFile File, |
| 158 | unsigned LineNo) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 159 | // typedefs are encoded in DIDerivedType format. |
| 160 | assert(Ty.Verify() && "Invalid typedef type!"); |
| 161 | Value *Elts[] = { |
| 162 | GetTagConstant(VMContext, dwarf::DW_TAG_typedef), |
| 163 | Ty.getContext(), |
| 164 | MDString::get(VMContext, Name), |
| 165 | File, |
| 166 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), |
| 167 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size |
| 168 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align |
| 169 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 170 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags |
| 171 | Ty |
| 172 | }; |
| 173 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /// CreateFriend - Create debugging information entry for a 'friend'. |
| 177 | DIType DIBuilder::CreateFriend(DIType Ty, DIType FriendTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 178 | // typedefs are encoded in DIDerivedType format. |
| 179 | assert(Ty.Verify() && "Invalid type!"); |
| 180 | assert(FriendTy.Verify() && "Invalid friend type!"); |
| 181 | Value *Elts[] = { |
| 182 | GetTagConstant(VMContext, dwarf::DW_TAG_friend), |
| 183 | Ty, |
| 184 | NULL, // Name |
| 185 | Ty.getFile(), |
| 186 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 187 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size |
| 188 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align |
| 189 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset |
| 190 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags |
| 191 | FriendTy |
| 192 | }; |
| 193 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /// CreateInheritance - Create debugging information entry to establish |
| 197 | /// inheritnace relationship between two types. |
| 198 | DIType DIBuilder::CreateInheritance(DIType Ty, DIType BaseTy, |
| 199 | uint64_t BaseOffset, unsigned Flags) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 200 | // TAG_inheritance is encoded in DIDerivedType format. |
| 201 | Value *Elts[] = { |
| 202 | GetTagConstant(VMContext, dwarf::DW_TAG_inheritance), |
| 203 | Ty, |
| 204 | NULL, // Name |
| 205 | NULL, // File |
| 206 | ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line |
| 207 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size |
| 208 | ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align |
| 209 | ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset), |
| 210 | ConstantInt::get(Type::getInt32Ty(VMContext), Flags), |
| 211 | BaseTy |
| 212 | }; |
| 213 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /// CreateMemberType - Create debugging information entry for a member. |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 217 | DIType DIBuilder::CreateMemberType(StringRef Name, |
| 218 | DIFile File, unsigned LineNumber, |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 219 | uint64_t SizeInBits, uint64_t AlignInBits, |
| 220 | uint64_t OffsetInBits, unsigned Flags, |
| 221 | DIType Ty) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 222 | // TAG_member is encoded in DIDerivedType format. |
| 223 | Value *Elts[] = { |
| 224 | GetTagConstant(VMContext, dwarf::DW_TAG_member), |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 225 | File, // Or TheCU ? Ty ? |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 226 | MDString::get(VMContext, Name), |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 227 | File, |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 228 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), |
| 229 | ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), |
| 230 | ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), |
| 231 | ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits), |
| 232 | ConstantInt::get(Type::getInt32Ty(VMContext), Flags), |
| 233 | Ty |
| 234 | }; |
| 235 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 238 | /// CreateStructType - Create debugging information entry for a struct. |
| 239 | DIType DIBuilder::CreateStructType(DIDescriptor Context, StringRef Name, DIFile F, |
| 240 | unsigned LineNumber, uint64_t SizeInBits, |
| 241 | uint64_t AlignInBits, unsigned Flags, |
| 242 | DIArray Elements, unsigned RunTimeLang) { |
| 243 | // TAG_structure_type is encoded in DICompositeType format. |
| 244 | Value *Elts[] = { |
| 245 | GetTagConstant(VMContext, dwarf::DW_TAG_structure_type), |
| 246 | Context, |
| 247 | MDString::get(VMContext, Name), |
| 248 | F, |
| 249 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), |
| 250 | ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), |
| 251 | ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), |
| 252 | ConstantInt::get(Type::getInt32Ty(VMContext), Flags), |
| 253 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 254 | Elements, |
| 255 | ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), |
| 256 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 257 | }; |
| 258 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
| 259 | } |
| 260 | |
| 261 | |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 262 | /// CreateArtificialType - Create a new DIType with "artificial" flag set. |
| 263 | DIType DIBuilder::CreateArtificialType(DIType Ty) { |
| 264 | if (Ty.isArtificial()) |
| 265 | return Ty; |
| 266 | |
| 267 | SmallVector<Value *, 9> Elts; |
| 268 | MDNode *N = Ty; |
| 269 | assert (N && "Unexpected input DIType!"); |
| 270 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 271 | if (Value *V = N->getOperand(i)) |
| 272 | Elts.push_back(V); |
| 273 | else |
| 274 | Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))); |
| 275 | } |
| 276 | |
| 277 | unsigned CurFlags = Ty.getFlags(); |
| 278 | CurFlags = CurFlags | DIType::FlagArtificial; |
| 279 | |
| 280 | // Flags are stored at this slot. |
| 281 | Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags); |
| 282 | |
| 283 | return DIType(MDNode::get(VMContext, Elts.data(), Elts.size())); |
| 284 | } |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame^] | 285 | |
| 286 | /// CreateTemporaryType - Create a temporary forward-declared type. |
| 287 | DIType DIBuilder::CreateTemporaryType() { |
| 288 | // Give the temporary MDNode a tag. It doesn't matter what tag we |
| 289 | // use here as long as DIType accepts it. |
| 290 | Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; |
| 291 | MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts)); |
| 292 | return DIType(Node); |
| 293 | } |
| 294 | |
| 295 | /// CreateTemporaryType - Create a temporary forward-declared type. |
| 296 | DIType DIBuilder::CreateTemporaryType(DIFile F) { |
| 297 | // Give the temporary MDNode a tag. It doesn't matter what tag we |
| 298 | // use here as long as DIType accepts it. |
| 299 | Value *Elts[] = { |
| 300 | GetTagConstant(VMContext, DW_TAG_base_type), |
| 301 | F.getCompileUnit(), |
| 302 | NULL, |
| 303 | F |
| 304 | }; |
| 305 | MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts)); |
| 306 | return DIType(Node); |
| 307 | } |
| 308 | |
| 309 | /// GetOrCreateArray - Get a DIArray, create one if required. |
| 310 | DIArray DIBuilder::GetOrCreateArray(Value *const *Elements, unsigned NumElements) { |
| 311 | if (NumElements == 0) { |
| 312 | Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)); |
| 313 | return DIArray(MDNode::get(VMContext, &Null, 1)); |
| 314 | } |
| 315 | return DIArray(MDNode::get(VMContext, Elements, NumElements)); |
| 316 | } |
| 317 | |
| 318 | /// CreateGlobalVariable - Create a new descriptor for the specified global. |
| 319 | DIGlobalVariable DIBuilder:: |
| 320 | CreateGlobalVariable(StringRef Name, |
| 321 | StringRef LinkageName, DIFile F, unsigned LineNumber, |
| 322 | DIType Ty, bool isLocalToUnit, llvm::Value *Val) { |
| 323 | Value *Elts[] = { |
| 324 | GetTagConstant(VMContext, dwarf::DW_TAG_variable), |
| 325 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 326 | TheCU, |
| 327 | MDString::get(VMContext, Name), |
| 328 | MDString::get(VMContext, Name), |
| 329 | MDString::get(VMContext, LinkageName), |
| 330 | F, |
| 331 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), |
| 332 | Ty, |
| 333 | ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), |
| 334 | ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ |
| 335 | Val |
| 336 | }; |
| 337 | MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)); |
| 338 | // Create a named metadata so that we do not lose this mdnode. |
| 339 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv"); |
| 340 | NMD->addOperand(Node); |
| 341 | return DIGlobalVariable(Node); |
| 342 | } |
| 343 | |
| 344 | /// CreateStaticVariable - Create a new descriptor for the specified static |
| 345 | /// variable. |
| 346 | DIGlobalVariable DIBuilder:: |
| 347 | CreateStaticVariable(DIDescriptor Context, StringRef Name, |
| 348 | StringRef LinkageName, DIFile F, unsigned LineNumber, |
| 349 | DIType Ty, bool isLocalToUnit, llvm::Value *Val) { |
| 350 | Value *Elts[] = { |
| 351 | GetTagConstant(VMContext, dwarf::DW_TAG_variable), |
| 352 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 353 | Context, |
| 354 | MDString::get(VMContext, Name), |
| 355 | MDString::get(VMContext, Name), |
| 356 | MDString::get(VMContext, LinkageName), |
| 357 | F, |
| 358 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), |
| 359 | Ty, |
| 360 | ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit), |
| 361 | ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/ |
| 362 | Val |
| 363 | }; |
| 364 | MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)); |
| 365 | // Create a named metadata so that we do not lose this mdnode. |
| 366 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv"); |
| 367 | NMD->addOperand(Node); |
| 368 | return DIGlobalVariable(Node); |
| 369 | } |
| 370 | |
| 371 | /// CreateComplexVariable - Create a new descriptor for the specified variable |
| 372 | /// which has a complex address expression for its address. |
| 373 | DIVariable DIBuilder::CreateComplexVariable(unsigned Tag, DIDescriptor Scope, |
| 374 | StringRef Name, DIFile F, |
| 375 | unsigned LineNo, |
| 376 | DIType Ty, Value *const *Addr, |
| 377 | unsigned NumAddr) { |
| 378 | SmallVector<Value *, 15> Elts; |
| 379 | Elts.push_back(GetTagConstant(VMContext, Tag)); |
| 380 | Elts.push_back(Scope); |
| 381 | Elts.push_back(MDString::get(VMContext, Name)); |
| 382 | Elts.push_back(F); |
| 383 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)); |
| 384 | Elts.push_back(Ty); |
| 385 | Elts.append(Addr, Addr+NumAddr); |
| 386 | |
| 387 | return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size())); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /// CreateNameSpace - This creates new descriptor for a namespace |
| 392 | /// with the specified parent scope. |
| 393 | DINameSpace DIBuilder::CreateNameSpace(DIDescriptor Scope, StringRef Name, |
| 394 | DIFile File, unsigned LineNo) { |
| 395 | Value *Elts[] = { |
| 396 | GetTagConstant(VMContext, dwarf::DW_TAG_namespace), |
| 397 | Scope, |
| 398 | MDString::get(VMContext, Name), |
| 399 | File, |
| 400 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNo) |
| 401 | }; |
| 402 | return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
| 403 | } |
| 404 | |
| 405 | /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. |
| 406 | Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo, |
| 407 | Instruction *InsertBefore) { |
| 408 | assert(Storage && "no storage passed to dbg.declare"); |
| 409 | assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare"); |
| 410 | if (!DeclareFn) |
| 411 | DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); |
| 412 | |
| 413 | Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo }; |
| 414 | return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore); |
| 415 | } |
| 416 | |
| 417 | /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. |
| 418 | Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo, |
| 419 | BasicBlock *InsertAtEnd) { |
| 420 | assert(Storage && "no storage passed to dbg.declare"); |
| 421 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare"); |
| 422 | if (!DeclareFn) |
| 423 | DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); |
| 424 | |
| 425 | Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo }; |
| 426 | |
| 427 | // If this block already has a terminator then insert this intrinsic |
| 428 | // before the terminator. |
| 429 | if (TerminatorInst *T = InsertAtEnd->getTerminator()) |
| 430 | return CallInst::Create(DeclareFn, Args, Args+2, "", T); |
| 431 | else |
| 432 | return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd); |
| 433 | } |
| 434 | |
| 435 | /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. |
| 436 | Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset, |
| 437 | DIVariable VarInfo, |
| 438 | Instruction *InsertBefore) { |
| 439 | assert(V && "no value passed to dbg.value"); |
| 440 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); |
| 441 | if (!ValueFn) |
| 442 | ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); |
| 443 | |
| 444 | Value *Args[] = { MDNode::get(V->getContext(), &V, 1), |
| 445 | ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), |
| 446 | VarInfo }; |
| 447 | return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore); |
| 448 | } |
| 449 | |
| 450 | /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. |
| 451 | Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset, |
| 452 | DIVariable VarInfo, |
| 453 | BasicBlock *InsertAtEnd) { |
| 454 | assert(V && "no value passed to dbg.value"); |
| 455 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); |
| 456 | if (!ValueFn) |
| 457 | ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); |
| 458 | |
| 459 | Value *Args[] = { MDNode::get(V->getContext(), &V, 1), |
| 460 | ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), |
| 461 | VarInfo }; |
| 462 | return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd); |
| 463 | } |
| 464 | |