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 | } |
Devang Patel | 48f17ba | 2010-12-07 23:58:00 +0000 | [diff] [blame] | 30 | |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 31 | DIBuilder::DIBuilder(Module &m) |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 32 | : M(m), VMContext(M.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {} |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 33 | |
| 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) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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) |
| 52 | }; |
| 53 | TheCU = DICompileUnit(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /// CreateFile - Create a file descriptor to hold debugging information |
| 57 | /// for a file. |
| 58 | DIFile DIBuilder::CreateFile(StringRef Filename, StringRef Directory) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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 |
| 65 | }; |
| 66 | return DIFile(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /// CreateEnumerator - Create a single enumerator value. |
| 70 | DIEnumerator DIBuilder::CreateEnumerator(StringRef Name, uint64_t Val) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 71 | Value *Elts[] = { |
| 72 | GetTagConstant(VMContext, dwarf::DW_TAG_enumerator), |
| 73 | MDString::get(VMContext, Name), |
| 74 | ConstantInt::get(Type::getInt64Ty(VMContext), Val) |
| 75 | }; |
| 76 | return DIEnumerator(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 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. |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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) |
| 97 | }; |
| 98 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// CreateQaulifiedType - Create debugging information entry for a qualified |
| 102 | /// type, e.g. 'const int'. |
| 103 | DIType DIBuilder::CreateQualifiedType(unsigned Tag, DIType FromTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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 |
| 116 | }; |
| 117 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// CreatePointerType - Create debugging information entry for a pointer. |
| 121 | DIType DIBuilder::CreatePointerType(DIType PointeeTy, uint64_t SizeInBits, |
| 122 | uint64_t AlignInBits, StringRef Name) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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 |
| 135 | }; |
| 136 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /// CreateReferenceType - Create debugging information entry for a reference. |
| 140 | DIType DIBuilder::CreateReferenceType(DIType RTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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 |
| 152 | RTy |
| 153 | }; |
| 154 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /// CreateTypedef - Create debugging information entry for a typedef. |
| 158 | DIType DIBuilder::CreateTypedef(DIType Ty, StringRef Name, DIFile File, |
| 159 | unsigned LineNo) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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 |
| 172 | Ty |
| 173 | }; |
| 174 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /// CreateFriend - Create debugging information entry for a 'friend'. |
| 178 | DIType DIBuilder::CreateFriend(DIType Ty, DIType FriendTy) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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), |
| 184 | Ty, |
| 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 |
| 193 | }; |
| 194 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 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) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 201 | // TAG_inheritance is encoded in DIDerivedType format. |
| 202 | Value *Elts[] = { |
| 203 | GetTagConstant(VMContext, dwarf::DW_TAG_inheritance), |
| 204 | Ty, |
| 205 | NULL, // Name |
| 206 | NULL, // File |
| 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 |
| 213 | }; |
| 214 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /// CreateMemberType - Create debugging information entry for a member. |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 218 | DIType DIBuilder::CreateMemberType(StringRef Name, |
| 219 | DIFile File, unsigned LineNumber, |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 220 | uint64_t SizeInBits, uint64_t AlignInBits, |
| 221 | uint64_t OffsetInBits, unsigned Flags, |
| 222 | DIType Ty) { |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 223 | // TAG_member is encoded in DIDerivedType format. |
| 224 | Value *Elts[] = { |
| 225 | GetTagConstant(VMContext, dwarf::DW_TAG_member), |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 226 | File, // Or TheCU ? Ty ? |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 227 | MDString::get(VMContext, Name), |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 228 | File, |
Benjamin Kramer | 42c9b25 | 2010-11-04 18:45:27 +0000 | [diff] [blame] | 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), |
| 234 | Ty |
| 235 | }; |
| 236 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 239 | /// CreateStructType - Create debugging information entry for a struct. |
| 240 | DIType DIBuilder::CreateStructType(DIDescriptor Context, StringRef Name, DIFile F, |
| 241 | unsigned LineNumber, uint64_t SizeInBits, |
| 242 | uint64_t AlignInBits, unsigned Flags, |
| 243 | DIArray Elements, unsigned RunTimeLang) { |
| 244 | // TAG_structure_type is encoded in DICompositeType format. |
| 245 | Value *Elts[] = { |
| 246 | GetTagConstant(VMContext, dwarf::DW_TAG_structure_type), |
| 247 | Context, |
| 248 | MDString::get(VMContext, Name), |
| 249 | F, |
| 250 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber), |
| 251 | ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits), |
| 252 | ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits), |
| 253 | ConstantInt::get(Type::getInt32Ty(VMContext), Flags), |
| 254 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 255 | Elements, |
| 256 | ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang), |
| 257 | llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)), |
| 258 | }; |
| 259 | return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
| 260 | } |
| 261 | |
| 262 | |
Devang Patel | 35fcd65 | 2010-11-04 15:01:38 +0000 | [diff] [blame] | 263 | /// CreateArtificialType - Create a new DIType with "artificial" flag set. |
| 264 | DIType DIBuilder::CreateArtificialType(DIType Ty) { |
| 265 | if (Ty.isArtificial()) |
| 266 | return Ty; |
| 267 | |
| 268 | SmallVector<Value *, 9> Elts; |
| 269 | MDNode *N = Ty; |
| 270 | assert (N && "Unexpected input DIType!"); |
| 271 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 272 | if (Value *V = N->getOperand(i)) |
| 273 | Elts.push_back(V); |
| 274 | else |
| 275 | Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))); |
| 276 | } |
| 277 | |
| 278 | unsigned CurFlags = Ty.getFlags(); |
| 279 | CurFlags = CurFlags | DIType::FlagArtificial; |
| 280 | |
| 281 | // Flags are stored at this slot. |
| 282 | Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags); |
| 283 | |
| 284 | return DIType(MDNode::get(VMContext, Elts.data(), Elts.size())); |
| 285 | } |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 286 | |
| 287 | /// CreateTemporaryType - Create a temporary forward-declared type. |
| 288 | DIType DIBuilder::CreateTemporaryType() { |
| 289 | // Give the temporary MDNode a tag. It doesn't matter what tag we |
| 290 | // use here as long as DIType accepts it. |
| 291 | Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) }; |
| 292 | MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts)); |
| 293 | return DIType(Node); |
| 294 | } |
| 295 | |
| 296 | /// CreateTemporaryType - Create a temporary forward-declared type. |
| 297 | DIType DIBuilder::CreateTemporaryType(DIFile F) { |
| 298 | // Give the temporary MDNode a tag. It doesn't matter what tag we |
| 299 | // use here as long as DIType accepts it. |
| 300 | Value *Elts[] = { |
| 301 | GetTagConstant(VMContext, DW_TAG_base_type), |
| 302 | F.getCompileUnit(), |
| 303 | NULL, |
| 304 | F |
| 305 | }; |
| 306 | MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts)); |
| 307 | return DIType(Node); |
| 308 | } |
| 309 | |
| 310 | /// GetOrCreateArray - Get a DIArray, create one if required. |
| 311 | DIArray DIBuilder::GetOrCreateArray(Value *const *Elements, unsigned NumElements) { |
| 312 | if (NumElements == 0) { |
| 313 | Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)); |
| 314 | return DIArray(MDNode::get(VMContext, &Null, 1)); |
| 315 | } |
| 316 | return DIArray(MDNode::get(VMContext, Elements, NumElements)); |
| 317 | } |
| 318 | |
| 319 | /// CreateGlobalVariable - Create a new descriptor for the specified global. |
| 320 | DIGlobalVariable DIBuilder:: |
Devang Patel | 94541ef | 2010-12-08 00:06:22 +0000 | [diff] [blame^] | 321 | CreateGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber, |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 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), |
Devang Patel | 94541ef | 2010-12-08 00:06:22 +0000 | [diff] [blame^] | 329 | MDString::get(VMContext, Name), |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 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 | |
Devang Patel | 48f17ba | 2010-12-07 23:58:00 +0000 | [diff] [blame] | 371 | /// CreateVariable - Create a new descriptor for the specified variable. |
| 372 | DIVariable DIBuilder::CreateLocalVariable(unsigned Tag, DIDescriptor Scope, |
| 373 | StringRef Name, DIFile File, |
| 374 | unsigned LineNo, DIType Ty, |
| 375 | bool AlwaysPreserve, unsigned Flags) { |
| 376 | Value *Elts[] = { |
| 377 | GetTagConstant(VMContext, Tag), |
| 378 | Scope, |
| 379 | MDString::get(VMContext, Name), |
| 380 | File, |
| 381 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNo), |
| 382 | Ty, |
| 383 | ConstantInt::get(Type::getInt32Ty(VMContext), Flags) |
| 384 | }; |
| 385 | MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)); |
| 386 | if (AlwaysPreserve) { |
| 387 | // The optimizer may remove local variable. If there is an interest |
| 388 | // to preserve variable info in such situation then stash it in a |
| 389 | // named mdnode. |
| 390 | DISubprogram Fn(getDISubprogram(Scope)); |
| 391 | StringRef FName = "fn"; |
| 392 | if (Fn.getFunction()) |
| 393 | FName = Fn.getFunction()->getName(); |
| 394 | char One = '\1'; |
| 395 | if (FName.startswith(StringRef(&One, 1))) |
| 396 | FName = FName.substr(1); |
| 397 | NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName); |
| 398 | FnLocals->addOperand(Node); |
| 399 | } |
| 400 | return DIVariable(Node); |
| 401 | } |
| 402 | |
Devang Patel | fe58f95 | 2010-12-07 23:25:47 +0000 | [diff] [blame] | 403 | /// CreateComplexVariable - Create a new descriptor for the specified variable |
| 404 | /// which has a complex address expression for its address. |
| 405 | DIVariable DIBuilder::CreateComplexVariable(unsigned Tag, DIDescriptor Scope, |
| 406 | StringRef Name, DIFile F, |
| 407 | unsigned LineNo, |
| 408 | DIType Ty, Value *const *Addr, |
| 409 | unsigned NumAddr) { |
| 410 | SmallVector<Value *, 15> Elts; |
| 411 | Elts.push_back(GetTagConstant(VMContext, Tag)); |
| 412 | Elts.push_back(Scope); |
| 413 | Elts.push_back(MDString::get(VMContext, Name)); |
| 414 | Elts.push_back(F); |
| 415 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)); |
| 416 | Elts.push_back(Ty); |
| 417 | Elts.append(Addr, Addr+NumAddr); |
| 418 | |
| 419 | return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size())); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /// CreateNameSpace - This creates new descriptor for a namespace |
| 424 | /// with the specified parent scope. |
| 425 | DINameSpace DIBuilder::CreateNameSpace(DIDescriptor Scope, StringRef Name, |
| 426 | DIFile File, unsigned LineNo) { |
| 427 | Value *Elts[] = { |
| 428 | GetTagConstant(VMContext, dwarf::DW_TAG_namespace), |
| 429 | Scope, |
| 430 | MDString::get(VMContext, Name), |
| 431 | File, |
| 432 | ConstantInt::get(Type::getInt32Ty(VMContext), LineNo) |
| 433 | }; |
| 434 | return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts))); |
| 435 | } |
| 436 | |
| 437 | /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. |
| 438 | Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo, |
| 439 | Instruction *InsertBefore) { |
| 440 | assert(Storage && "no storage passed to dbg.declare"); |
| 441 | assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare"); |
| 442 | if (!DeclareFn) |
| 443 | DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); |
| 444 | |
| 445 | Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo }; |
| 446 | return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore); |
| 447 | } |
| 448 | |
| 449 | /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. |
| 450 | Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo, |
| 451 | BasicBlock *InsertAtEnd) { |
| 452 | assert(Storage && "no storage passed to dbg.declare"); |
| 453 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare"); |
| 454 | if (!DeclareFn) |
| 455 | DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); |
| 456 | |
| 457 | Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo }; |
| 458 | |
| 459 | // If this block already has a terminator then insert this intrinsic |
| 460 | // before the terminator. |
| 461 | if (TerminatorInst *T = InsertAtEnd->getTerminator()) |
| 462 | return CallInst::Create(DeclareFn, Args, Args+2, "", T); |
| 463 | else |
| 464 | return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd); |
| 465 | } |
| 466 | |
| 467 | /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. |
| 468 | Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset, |
| 469 | DIVariable VarInfo, |
| 470 | Instruction *InsertBefore) { |
| 471 | assert(V && "no value passed to dbg.value"); |
| 472 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); |
| 473 | if (!ValueFn) |
| 474 | ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); |
| 475 | |
| 476 | Value *Args[] = { MDNode::get(V->getContext(), &V, 1), |
| 477 | ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), |
| 478 | VarInfo }; |
| 479 | return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore); |
| 480 | } |
| 481 | |
| 482 | /// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call. |
| 483 | Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset, |
| 484 | DIVariable VarInfo, |
| 485 | BasicBlock *InsertAtEnd) { |
| 486 | assert(V && "no value passed to dbg.value"); |
| 487 | assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value"); |
| 488 | if (!ValueFn) |
| 489 | ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); |
| 490 | |
| 491 | Value *Args[] = { MDNode::get(V->getContext(), &V, 1), |
| 492 | ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), |
| 493 | VarInfo }; |
| 494 | return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd); |
| 495 | } |
| 496 | |