Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 1 | //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// |
| 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 helper classes used to build and interpret debug |
| 11 | // information in LLVM IR form. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/DebugInfo.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Intrinsics.h" |
Torok Edwin | 620f280 | 2008-12-16 09:07:36 +0000 | [diff] [blame] | 19 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Analysis/ValueTracking.h" |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Streams.h" |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // DIDescriptor |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) { |
| 31 | GV = gv; |
| 32 | |
| 33 | // If this is non-null, check to see if the Tag matches. If not, set to null. |
| 34 | if (GV && getTag() != RequiredTag) |
| 35 | GV = 0; |
| 36 | } |
| 37 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 38 | const std::string & |
| 39 | DIDescriptor::getStringField(unsigned Elt, std::string &Result) const { |
| 40 | if (GV == 0) { |
| 41 | Result.clear(); |
| 42 | return Result; |
| 43 | } |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 44 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 45 | Constant *C = GV->getInitializer(); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 46 | if (C == 0 || Elt >= C->getNumOperands()) { |
| 47 | Result.clear(); |
| 48 | return Result; |
| 49 | } |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 50 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 51 | // Fills in the string if it succeeds |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 52 | if (!GetConstantStringInfo(C->getOperand(Elt), Result)) |
| 53 | Result.clear(); |
| 54 | |
| 55 | return Result; |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { |
| 59 | if (GV == 0) return 0; |
| 60 | Constant *C = GV->getInitializer(); |
| 61 | if (C == 0 || Elt >= C->getNumOperands()) |
| 62 | return 0; |
| 63 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt))) |
| 64 | return CI->getZExtValue(); |
| 65 | return 0; |
| 66 | } |
| 67 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 68 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 69 | if (GV == 0) return DIDescriptor(); |
| 70 | Constant *C = GV->getInitializer(); |
| 71 | if (C == 0 || Elt >= C->getNumOperands()) |
| 72 | return DIDescriptor(); |
| 73 | C = C->getOperand(Elt); |
| 74 | return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts())); |
| 75 | } |
| 76 | |
| 77 | GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { |
| 78 | if (GV == 0) return 0; |
| 79 | Constant *C = GV->getInitializer(); |
| 80 | if (C == 0 || Elt >= C->getNumOperands()) |
| 81 | return 0; |
| 82 | C = C->getOperand(Elt); |
| 83 | |
| 84 | return dyn_cast<GlobalVariable>(C->stripPointerCasts()); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // Simple Descriptor Constructors and other Methods |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | |
| 93 | DIAnchor::DIAnchor(GlobalVariable *GV) |
| 94 | : DIDescriptor(GV, dwarf::DW_TAG_anchor) {} |
| 95 | DIEnumerator::DIEnumerator(GlobalVariable *GV) |
| 96 | : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {} |
| 97 | DISubrange::DISubrange(GlobalVariable *GV) |
| 98 | : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {} |
| 99 | DICompileUnit::DICompileUnit(GlobalVariable *GV) |
| 100 | : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {} |
| 101 | DIBasicType::DIBasicType(GlobalVariable *GV) |
| 102 | : DIType(GV, dwarf::DW_TAG_base_type) {} |
| 103 | DISubprogram::DISubprogram(GlobalVariable *GV) |
| 104 | : DIGlobal(GV, dwarf::DW_TAG_subprogram) {} |
| 105 | DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV) |
| 106 | : DIGlobal(GV, dwarf::DW_TAG_variable) {} |
| 107 | DIBlock::DIBlock(GlobalVariable *GV) |
| 108 | : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {} |
Torok Edwin | b07fbd9 | 2008-12-13 08:25:29 +0000 | [diff] [blame] | 109 | // needed by DIVariable::getType() |
Torok Edwin | a70c68e | 2008-12-16 09:06:01 +0000 | [diff] [blame] | 110 | DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) { |
| 111 | if (!gv) return; |
Torok Edwin | b07fbd9 | 2008-12-13 08:25:29 +0000 | [diff] [blame] | 112 | unsigned tag = getTag(); |
| 113 | if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) && |
| 114 | !DICompositeType::isCompositeType(tag)) |
| 115 | GV = 0; |
| 116 | } |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 117 | |
| 118 | /// isDerivedType - Return true if the specified tag is legal for |
| 119 | /// DIDerivedType. |
Devang Patel | 486938f | 2009-01-12 21:38:43 +0000 | [diff] [blame] | 120 | bool DIType::isDerivedType(unsigned Tag) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 121 | switch (Tag) { |
| 122 | case dwarf::DW_TAG_typedef: |
| 123 | case dwarf::DW_TAG_pointer_type: |
| 124 | case dwarf::DW_TAG_reference_type: |
| 125 | case dwarf::DW_TAG_const_type: |
| 126 | case dwarf::DW_TAG_volatile_type: |
| 127 | case dwarf::DW_TAG_restrict_type: |
| 128 | case dwarf::DW_TAG_member: |
| 129 | case dwarf::DW_TAG_inheritance: |
| 130 | return true; |
| 131 | default: |
| 132 | // FIXME: Even though it doesn't make sense, CompositeTypes are current |
| 133 | // modelled as DerivedTypes, this should return true for them as well. |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) { |
| 139 | if (GV && !isDerivedType(getTag())) |
| 140 | GV = 0; |
| 141 | } |
| 142 | |
| 143 | /// isCompositeType - Return true if the specified tag is legal for |
| 144 | /// DICompositeType. |
Devang Patel | 486938f | 2009-01-12 21:38:43 +0000 | [diff] [blame] | 145 | bool DIType::isCompositeType(unsigned TAG) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 146 | switch (TAG) { |
| 147 | case dwarf::DW_TAG_array_type: |
| 148 | case dwarf::DW_TAG_structure_type: |
| 149 | case dwarf::DW_TAG_union_type: |
| 150 | case dwarf::DW_TAG_enumeration_type: |
| 151 | case dwarf::DW_TAG_vector_type: |
| 152 | case dwarf::DW_TAG_subroutine_type: |
Devang Patel | 25cb0d7 | 2009-03-25 03:52:06 +0000 | [diff] [blame] | 153 | case dwarf::DW_TAG_class_type: |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 154 | return true; |
| 155 | default: |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | DICompositeType::DICompositeType(GlobalVariable *GV) |
| 161 | : DIDerivedType(GV, true, true) { |
| 162 | if (GV && !isCompositeType(getTag())) |
| 163 | GV = 0; |
| 164 | } |
| 165 | |
| 166 | /// isVariable - Return true if the specified tag is legal for DIVariable. |
| 167 | bool DIVariable::isVariable(unsigned Tag) { |
| 168 | switch (Tag) { |
| 169 | case dwarf::DW_TAG_auto_variable: |
| 170 | case dwarf::DW_TAG_arg_variable: |
| 171 | case dwarf::DW_TAG_return_variable: |
| 172 | return true; |
| 173 | default: |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
Devang Patel | 36375ee | 2009-02-17 21:23:59 +0000 | [diff] [blame] | 178 | DIVariable::DIVariable(GlobalVariable *gv) : DIDescriptor(gv) { |
| 179 | if (gv && !isVariable(getTag())) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 180 | GV = 0; |
| 181 | } |
| 182 | |
Devang Patel | 68afdc3 | 2009-01-05 18:33:01 +0000 | [diff] [blame] | 183 | unsigned DIArray::getNumElements() const { |
| 184 | assert (GV && "Invalid DIArray"); |
| 185 | Constant *C = GV->getInitializer(); |
| 186 | assert (C && "Invalid DIArray initializer"); |
| 187 | return C->getNumOperands(); |
| 188 | } |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 189 | |
Devang Patel | b79b535 | 2009-01-19 23:21:49 +0000 | [diff] [blame] | 190 | /// Verify - Verify that a compile unit is well formed. |
| 191 | bool DICompileUnit::Verify() const { |
| 192 | if (isNull()) |
| 193 | return false; |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 194 | std::string Res; |
| 195 | if (getFilename(Res).empty()) |
| 196 | return false; |
Devang Patel | b79b535 | 2009-01-19 23:21:49 +0000 | [diff] [blame] | 197 | // It is possible that directory and produce string is empty. |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 198 | return true; |
Devang Patel | b79b535 | 2009-01-19 23:21:49 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /// Verify - Verify that a type descriptor is well formed. |
| 202 | bool DIType::Verify() const { |
| 203 | if (isNull()) |
| 204 | return false; |
| 205 | if (getContext().isNull()) |
| 206 | return false; |
| 207 | |
| 208 | DICompileUnit CU = getCompileUnit(); |
| 209 | if (!CU.isNull() && !CU.Verify()) |
| 210 | return false; |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | /// Verify - Verify that a composite type descriptor is well formed. |
| 215 | bool DICompositeType::Verify() const { |
| 216 | if (isNull()) |
| 217 | return false; |
| 218 | if (getContext().isNull()) |
| 219 | return false; |
| 220 | |
| 221 | DICompileUnit CU = getCompileUnit(); |
| 222 | if (!CU.isNull() && !CU.Verify()) |
| 223 | return false; |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /// Verify - Verify that a subprogram descriptor is well formed. |
| 228 | bool DISubprogram::Verify() const { |
| 229 | if (isNull()) |
| 230 | return false; |
| 231 | |
| 232 | if (getContext().isNull()) |
| 233 | return false; |
| 234 | |
| 235 | DICompileUnit CU = getCompileUnit(); |
| 236 | if (!CU.Verify()) |
| 237 | return false; |
| 238 | |
| 239 | DICompositeType Ty = getType(); |
| 240 | if (!Ty.isNull() && !Ty.Verify()) |
| 241 | return false; |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | /// Verify - Verify that a global variable descriptor is well formed. |
| 246 | bool DIGlobalVariable::Verify() const { |
| 247 | if (isNull()) |
| 248 | return false; |
| 249 | |
| 250 | if (getContext().isNull()) |
| 251 | return false; |
| 252 | |
| 253 | DICompileUnit CU = getCompileUnit(); |
| 254 | if (!CU.Verify()) |
| 255 | return false; |
| 256 | |
| 257 | DIType Ty = getType(); |
| 258 | if (!Ty.Verify()) |
| 259 | return false; |
| 260 | |
| 261 | if (!getGlobal()) |
| 262 | return false; |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | /// Verify - Verify that a variable descriptor is well formed. |
| 268 | bool DIVariable::Verify() const { |
| 269 | if (isNull()) |
| 270 | return false; |
| 271 | |
| 272 | if (getContext().isNull()) |
| 273 | return false; |
| 274 | |
| 275 | DIType Ty = getType(); |
| 276 | if (!Ty.Verify()) |
| 277 | return false; |
| 278 | |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
Devang Patel | 36375ee | 2009-02-17 21:23:59 +0000 | [diff] [blame] | 283 | /// getOriginalTypeSize - If this type is derived from a base type then |
| 284 | /// return base type size. |
| 285 | uint64_t DIDerivedType::getOriginalTypeSize() const { |
| 286 | if (getTag() != dwarf::DW_TAG_member) |
| 287 | return getSizeInBits(); |
| 288 | DIType BT = getTypeDerivedFrom(); |
| 289 | if (BT.getTag() != dwarf::DW_TAG_base_type) |
| 290 | return getSizeInBits(); |
| 291 | return BT.getSizeInBits(); |
| 292 | } |
Devang Patel | b79b535 | 2009-01-19 23:21:49 +0000 | [diff] [blame] | 293 | |
Devang Patel | af5b6bb | 2009-04-15 00:06:07 +0000 | [diff] [blame^] | 294 | /// describes - Return true if this subprogram provides debugging |
| 295 | /// information for the function F. |
| 296 | bool DISubprogram::describes(const Function *F) { |
| 297 | assert (F && "Invalid function"); |
| 298 | std::string Name; |
| 299 | getLinkageName(Name); |
| 300 | if (Name.empty()) |
| 301 | getName(Name); |
| 302 | if (!Name.empty() && (strcmp(Name.c_str(), F->getNameStart()) == false)) |
| 303 | return true; |
| 304 | return false; |
| 305 | } |
| 306 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
| 308 | // DIFactory: Basic Helpers |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 311 | DIFactory::DIFactory(Module &m) : M(m) { |
| 312 | StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0; |
| 313 | EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL)); |
| 314 | } |
| 315 | |
| 316 | /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'. |
| 317 | /// This is only valid when the descriptor is non-null. |
| 318 | Constant *DIFactory::getCastToEmpty(DIDescriptor D) { |
| 319 | if (D.isNull()) return Constant::getNullValue(EmptyStructPtr); |
| 320 | return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr); |
| 321 | } |
| 322 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 323 | Constant *DIFactory::GetTagConstant(unsigned TAG) { |
Devang Patel | 6906ba5 | 2009-01-20 19:22:03 +0000 | [diff] [blame] | 324 | assert((TAG & LLVMDebugVersionMask) == 0 && |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 325 | "Tag too large for debug encoding!"); |
Devang Patel | 6906ba5 | 2009-01-20 19:22:03 +0000 | [diff] [blame] | 326 | return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | Constant *DIFactory::GetStringConstant(const std::string &String) { |
| 330 | // Check string cache for previous edition. |
| 331 | Constant *&Slot = StringCache[String]; |
| 332 | |
| 333 | // Return Constant if previously defined. |
| 334 | if (Slot) return Slot; |
| 335 | |
| 336 | const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty); |
| 337 | |
| 338 | // If empty string then use a sbyte* null instead. |
| 339 | if (String.empty()) |
| 340 | return Slot = ConstantPointerNull::get(DestTy); |
| 341 | |
| 342 | // Construct string as an llvm constant. |
| 343 | Constant *ConstStr = ConstantArray::get(String); |
| 344 | |
| 345 | // Otherwise create and return a new string global. |
| 346 | GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, |
| 347 | GlobalVariable::InternalLinkage, |
| 348 | ConstStr, ".str", &M); |
| 349 | StrGV->setSection("llvm.metadata"); |
| 350 | return Slot = ConstantExpr::getBitCast(StrGV, DestTy); |
| 351 | } |
| 352 | |
| 353 | /// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it |
| 354 | /// already exists, return it. If not, create a new one and return it. |
| 355 | DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) { |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 356 | const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 357 | |
| 358 | // Otherwise, create the global or return it if already in the module. |
| 359 | Constant *C = M.getOrInsertGlobal(Name, EltTy); |
| 360 | assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?"); |
| 361 | GlobalVariable *GV = cast<GlobalVariable>(C); |
| 362 | |
| 363 | // If it has an initializer, it is already in the module. |
| 364 | if (GV->hasInitializer()) |
| 365 | return SubProgramAnchor = DIAnchor(GV); |
| 366 | |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 367 | GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 368 | GV->setSection("llvm.metadata"); |
| 369 | GV->setConstant(true); |
| 370 | M.addTypeName("llvm.dbg.anchor.type", EltTy); |
| 371 | |
| 372 | // Otherwise, set the initializer. |
| 373 | Constant *Elts[] = { |
| 374 | GetTagConstant(dwarf::DW_TAG_anchor), |
| 375 | ConstantInt::get(Type::Int32Ty, TAG) |
| 376 | }; |
| 377 | |
| 378 | GV->setInitializer(ConstantStruct::get(Elts, 2)); |
| 379 | return DIAnchor(GV); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | // DIFactory: Primary Constructors |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | |
| 388 | /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units, |
| 389 | /// creating a new one if there isn't already one in the module. |
| 390 | DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() { |
| 391 | // If we already created one, just return it. |
| 392 | if (!CompileUnitAnchor.isNull()) |
| 393 | return CompileUnitAnchor; |
| 394 | return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit, |
| 395 | "llvm.dbg.compile_units"); |
| 396 | } |
| 397 | |
| 398 | /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms, |
| 399 | /// creating a new one if there isn't already one in the module. |
| 400 | DIAnchor DIFactory::GetOrCreateSubprogramAnchor() { |
| 401 | // If we already created one, just return it. |
| 402 | if (!SubProgramAnchor.isNull()) |
| 403 | return SubProgramAnchor; |
| 404 | return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram, |
| 405 | "llvm.dbg.subprograms"); |
| 406 | } |
| 407 | |
| 408 | /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals, |
| 409 | /// creating a new one if there isn't already one in the module. |
| 410 | DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() { |
| 411 | // If we already created one, just return it. |
| 412 | if (!GlobalVariableAnchor.isNull()) |
| 413 | return GlobalVariableAnchor; |
| 414 | return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable, |
| 415 | "llvm.dbg.global_variables"); |
| 416 | } |
| 417 | |
| 418 | /// GetOrCreateArray - Create an descriptor for an array of descriptors. |
| 419 | /// This implicitly uniques the arrays created. |
| 420 | DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) { |
| 421 | SmallVector<Constant*, 16> Elts; |
| 422 | |
| 423 | for (unsigned i = 0; i != NumTys; ++i) |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 424 | Elts.push_back(getCastToEmpty(Tys[i])); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 426 | Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr, |
| 427 | Elts.size()), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 428 | &Elts[0], Elts.size()); |
| 429 | // If we already have this array, just return the uniqued version. |
| 430 | DIDescriptor &Entry = SimpleConstantCache[Init]; |
| 431 | if (!Entry.isNull()) return DIArray(Entry.getGV()); |
| 432 | |
| 433 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 434 | GlobalValue::InternalLinkage, |
| 435 | Init, "llvm.dbg.array", &M); |
| 436 | GV->setSection("llvm.metadata"); |
| 437 | Entry = DIDescriptor(GV); |
| 438 | return DIArray(GV); |
| 439 | } |
| 440 | |
| 441 | /// GetOrCreateSubrange - Create a descriptor for a value range. This |
| 442 | /// implicitly uniques the values returned. |
| 443 | DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) { |
| 444 | Constant *Elts[] = { |
| 445 | GetTagConstant(dwarf::DW_TAG_subrange_type), |
| 446 | ConstantInt::get(Type::Int64Ty, Lo), |
| 447 | ConstantInt::get(Type::Int64Ty, Hi) |
| 448 | }; |
| 449 | |
| 450 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 451 | |
| 452 | // If we already have this range, just return the uniqued version. |
| 453 | DIDescriptor &Entry = SimpleConstantCache[Init]; |
| 454 | if (!Entry.isNull()) return DISubrange(Entry.getGV()); |
| 455 | |
| 456 | M.addTypeName("llvm.dbg.subrange.type", Init->getType()); |
| 457 | |
| 458 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 459 | GlobalValue::InternalLinkage, |
| 460 | Init, "llvm.dbg.subrange", &M); |
| 461 | GV->setSection("llvm.metadata"); |
| 462 | Entry = DIDescriptor(GV); |
| 463 | return DISubrange(GV); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | /// CreateCompileUnit - Create a new descriptor for the specified compile |
| 469 | /// unit. Note that this does not unique compile units within the module. |
| 470 | DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID, |
| 471 | const std::string &Filename, |
| 472 | const std::string &Directory, |
Devang Patel | 3b64c6b | 2009-01-23 22:33:47 +0000 | [diff] [blame] | 473 | const std::string &Producer, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 474 | bool isMain, |
Devang Patel | 3b64c6b | 2009-01-23 22:33:47 +0000 | [diff] [blame] | 475 | bool isOptimized, |
Devang Patel | 13319ce | 2009-02-17 22:43:44 +0000 | [diff] [blame] | 476 | const char *Flags, |
| 477 | unsigned RunTimeVer) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 478 | Constant *Elts[] = { |
| 479 | GetTagConstant(dwarf::DW_TAG_compile_unit), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 480 | getCastToEmpty(GetOrCreateCompileUnitAnchor()), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 481 | ConstantInt::get(Type::Int32Ty, LangID), |
| 482 | GetStringConstant(Filename), |
| 483 | GetStringConstant(Directory), |
Devang Patel | 3b64c6b | 2009-01-23 22:33:47 +0000 | [diff] [blame] | 484 | GetStringConstant(Producer), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 485 | ConstantInt::get(Type::Int1Ty, isMain), |
Devang Patel | 3b64c6b | 2009-01-23 22:33:47 +0000 | [diff] [blame] | 486 | ConstantInt::get(Type::Int1Ty, isOptimized), |
Devang Patel | 13319ce | 2009-02-17 22:43:44 +0000 | [diff] [blame] | 487 | GetStringConstant(Flags), |
| 488 | ConstantInt::get(Type::Int32Ty, RunTimeVer) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 492 | |
| 493 | M.addTypeName("llvm.dbg.compile_unit.type", Init->getType()); |
| 494 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 495 | GlobalValue::InternalLinkage, |
| 496 | Init, "llvm.dbg.compile_unit", &M); |
| 497 | GV->setSection("llvm.metadata"); |
| 498 | return DICompileUnit(GV); |
| 499 | } |
| 500 | |
| 501 | /// CreateEnumerator - Create a single enumerator value. |
| 502 | DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){ |
| 503 | Constant *Elts[] = { |
| 504 | GetTagConstant(dwarf::DW_TAG_enumerator), |
| 505 | GetStringConstant(Name), |
| 506 | ConstantInt::get(Type::Int64Ty, Val) |
| 507 | }; |
| 508 | |
| 509 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 510 | |
| 511 | M.addTypeName("llvm.dbg.enumerator.type", Init->getType()); |
| 512 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 513 | GlobalValue::InternalLinkage, |
| 514 | Init, "llvm.dbg.enumerator", &M); |
| 515 | GV->setSection("llvm.metadata"); |
| 516 | return DIEnumerator(GV); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | /// CreateBasicType - Create a basic type like int, float, etc. |
| 521 | DIBasicType DIFactory::CreateBasicType(DIDescriptor Context, |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 522 | const std::string &Name, |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 523 | DICompileUnit CompileUnit, |
| 524 | unsigned LineNumber, |
| 525 | uint64_t SizeInBits, |
| 526 | uint64_t AlignInBits, |
| 527 | uint64_t OffsetInBits, unsigned Flags, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 528 | unsigned Encoding) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 529 | Constant *Elts[] = { |
| 530 | GetTagConstant(dwarf::DW_TAG_base_type), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 531 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 532 | GetStringConstant(Name), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 533 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 534 | ConstantInt::get(Type::Int32Ty, LineNumber), |
| 535 | ConstantInt::get(Type::Int64Ty, SizeInBits), |
| 536 | ConstantInt::get(Type::Int64Ty, AlignInBits), |
| 537 | ConstantInt::get(Type::Int64Ty, OffsetInBits), |
| 538 | ConstantInt::get(Type::Int32Ty, Flags), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 539 | ConstantInt::get(Type::Int32Ty, Encoding) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 543 | |
| 544 | M.addTypeName("llvm.dbg.basictype.type", Init->getType()); |
| 545 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 546 | GlobalValue::InternalLinkage, |
| 547 | Init, "llvm.dbg.basictype", &M); |
| 548 | GV->setSection("llvm.metadata"); |
| 549 | return DIBasicType(GV); |
| 550 | } |
| 551 | |
| 552 | /// CreateDerivedType - Create a derived type like const qualified type, |
| 553 | /// pointer, typedef, etc. |
| 554 | DIDerivedType DIFactory::CreateDerivedType(unsigned Tag, |
| 555 | DIDescriptor Context, |
| 556 | const std::string &Name, |
| 557 | DICompileUnit CompileUnit, |
| 558 | unsigned LineNumber, |
| 559 | uint64_t SizeInBits, |
| 560 | uint64_t AlignInBits, |
| 561 | uint64_t OffsetInBits, |
| 562 | unsigned Flags, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 563 | DIType DerivedFrom) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 564 | Constant *Elts[] = { |
| 565 | GetTagConstant(Tag), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 566 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 567 | GetStringConstant(Name), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 568 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 569 | ConstantInt::get(Type::Int32Ty, LineNumber), |
| 570 | ConstantInt::get(Type::Int64Ty, SizeInBits), |
| 571 | ConstantInt::get(Type::Int64Ty, AlignInBits), |
| 572 | ConstantInt::get(Type::Int64Ty, OffsetInBits), |
| 573 | ConstantInt::get(Type::Int32Ty, Flags), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 574 | getCastToEmpty(DerivedFrom) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 578 | |
| 579 | M.addTypeName("llvm.dbg.derivedtype.type", Init->getType()); |
| 580 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 581 | GlobalValue::InternalLinkage, |
| 582 | Init, "llvm.dbg.derivedtype", &M); |
| 583 | GV->setSection("llvm.metadata"); |
| 584 | return DIDerivedType(GV); |
| 585 | } |
| 586 | |
| 587 | /// CreateCompositeType - Create a composite type like array, struct, etc. |
| 588 | DICompositeType DIFactory::CreateCompositeType(unsigned Tag, |
| 589 | DIDescriptor Context, |
| 590 | const std::string &Name, |
| 591 | DICompileUnit CompileUnit, |
| 592 | unsigned LineNumber, |
| 593 | uint64_t SizeInBits, |
| 594 | uint64_t AlignInBits, |
| 595 | uint64_t OffsetInBits, |
| 596 | unsigned Flags, |
| 597 | DIType DerivedFrom, |
Devang Patel | 13319ce | 2009-02-17 22:43:44 +0000 | [diff] [blame] | 598 | DIArray Elements, |
| 599 | unsigned RuntimeLang) { |
Devang Patel | 854967e | 2008-12-17 22:39:29 +0000 | [diff] [blame] | 600 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 601 | Constant *Elts[] = { |
| 602 | GetTagConstant(Tag), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 603 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 604 | GetStringConstant(Name), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 605 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 606 | ConstantInt::get(Type::Int32Ty, LineNumber), |
| 607 | ConstantInt::get(Type::Int64Ty, SizeInBits), |
| 608 | ConstantInt::get(Type::Int64Ty, AlignInBits), |
| 609 | ConstantInt::get(Type::Int64Ty, OffsetInBits), |
| 610 | ConstantInt::get(Type::Int32Ty, Flags), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 611 | getCastToEmpty(DerivedFrom), |
Devang Patel | 13319ce | 2009-02-17 22:43:44 +0000 | [diff] [blame] | 612 | getCastToEmpty(Elements), |
| 613 | ConstantInt::get(Type::Int32Ty, RuntimeLang) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 617 | |
| 618 | M.addTypeName("llvm.dbg.composite.type", Init->getType()); |
| 619 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 620 | GlobalValue::InternalLinkage, |
| 621 | Init, "llvm.dbg.composite", &M); |
| 622 | GV->setSection("llvm.metadata"); |
| 623 | return DICompositeType(GV); |
| 624 | } |
| 625 | |
| 626 | |
| 627 | /// CreateSubprogram - Create a new descriptor for the specified subprogram. |
| 628 | /// See comments in DISubprogram for descriptions of these fields. This |
| 629 | /// method does not unique the generated descriptors. |
| 630 | DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context, |
| 631 | const std::string &Name, |
| 632 | const std::string &DisplayName, |
| 633 | const std::string &LinkageName, |
| 634 | DICompileUnit CompileUnit, |
| 635 | unsigned LineNo, DIType Type, |
| 636 | bool isLocalToUnit, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 637 | bool isDefinition) { |
Devang Patel | 854967e | 2008-12-17 22:39:29 +0000 | [diff] [blame] | 638 | |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 639 | Constant *Elts[] = { |
| 640 | GetTagConstant(dwarf::DW_TAG_subprogram), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 641 | getCastToEmpty(GetOrCreateSubprogramAnchor()), |
| 642 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 643 | GetStringConstant(Name), |
| 644 | GetStringConstant(DisplayName), |
| 645 | GetStringConstant(LinkageName), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 646 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 647 | ConstantInt::get(Type::Int32Ty, LineNo), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 648 | getCastToEmpty(Type), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 649 | ConstantInt::get(Type::Int1Ty, isLocalToUnit), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 650 | ConstantInt::get(Type::Int1Ty, isDefinition) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 654 | |
| 655 | M.addTypeName("llvm.dbg.subprogram.type", Init->getType()); |
| 656 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 657 | GlobalValue::InternalLinkage, |
| 658 | Init, "llvm.dbg.subprogram", &M); |
| 659 | GV->setSection("llvm.metadata"); |
| 660 | return DISubprogram(GV); |
| 661 | } |
| 662 | |
| 663 | /// CreateGlobalVariable - Create a new descriptor for the specified global. |
| 664 | DIGlobalVariable |
| 665 | DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name, |
| 666 | const std::string &DisplayName, |
| 667 | const std::string &LinkageName, |
| 668 | DICompileUnit CompileUnit, |
| 669 | unsigned LineNo, DIType Type,bool isLocalToUnit, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 670 | bool isDefinition, llvm::GlobalVariable *Val) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 671 | Constant *Elts[] = { |
| 672 | GetTagConstant(dwarf::DW_TAG_variable), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 673 | getCastToEmpty(GetOrCreateGlobalVariableAnchor()), |
| 674 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 675 | GetStringConstant(Name), |
| 676 | GetStringConstant(DisplayName), |
| 677 | GetStringConstant(LinkageName), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 678 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 679 | ConstantInt::get(Type::Int32Ty, LineNo), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 680 | getCastToEmpty(Type), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 681 | ConstantInt::get(Type::Int1Ty, isLocalToUnit), |
| 682 | ConstantInt::get(Type::Int1Ty, isDefinition), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 683 | ConstantExpr::getBitCast(Val, EmptyStructPtr) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 687 | |
| 688 | M.addTypeName("llvm.dbg.global_variable.type", Init->getType()); |
| 689 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 690 | GlobalValue::InternalLinkage, |
| 691 | Init, "llvm.dbg.global_variable", &M); |
| 692 | GV->setSection("llvm.metadata"); |
| 693 | return DIGlobalVariable(GV); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /// CreateVariable - Create a new descriptor for the specified variable. |
| 698 | DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context, |
| 699 | const std::string &Name, |
| 700 | DICompileUnit CompileUnit, unsigned LineNo, |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 701 | DIType Type) { |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 702 | Constant *Elts[] = { |
| 703 | GetTagConstant(Tag), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 704 | getCastToEmpty(Context), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 705 | GetStringConstant(Name), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 706 | getCastToEmpty(CompileUnit), |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 707 | ConstantInt::get(Type::Int32Ty, LineNo), |
Devang Patel | dd9db66 | 2009-01-30 18:20:31 +0000 | [diff] [blame] | 708 | getCastToEmpty(Type) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 712 | |
| 713 | M.addTypeName("llvm.dbg.variable.type", Init->getType()); |
| 714 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 715 | GlobalValue::InternalLinkage, |
| 716 | Init, "llvm.dbg.variable", &M); |
| 717 | GV->setSection("llvm.metadata"); |
| 718 | return DIVariable(GV); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | /// CreateBlock - This creates a descriptor for a lexical block with the |
| 723 | /// specified parent context. |
| 724 | DIBlock DIFactory::CreateBlock(DIDescriptor Context) { |
| 725 | Constant *Elts[] = { |
| 726 | GetTagConstant(dwarf::DW_TAG_lexical_block), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 727 | getCastToEmpty(Context) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 728 | }; |
| 729 | |
| 730 | Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); |
| 731 | |
| 732 | M.addTypeName("llvm.dbg.block.type", Init->getType()); |
| 733 | GlobalVariable *GV = new GlobalVariable(Init->getType(), true, |
| 734 | GlobalValue::InternalLinkage, |
| 735 | Init, "llvm.dbg.block", &M); |
| 736 | GV->setSection("llvm.metadata"); |
| 737 | return DIBlock(GV); |
| 738 | } |
| 739 | |
| 740 | |
| 741 | //===----------------------------------------------------------------------===// |
| 742 | // DIFactory: Routines for inserting code into a function |
| 743 | //===----------------------------------------------------------------------===// |
| 744 | |
| 745 | /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation, |
| 746 | /// inserting it at the end of the specified basic block. |
| 747 | void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo, |
| 748 | unsigned ColNo, BasicBlock *BB) { |
| 749 | |
| 750 | // Lazily construct llvm.dbg.stoppoint function. |
| 751 | if (!StopPointFn) |
| 752 | StopPointFn = llvm::Intrinsic::getDeclaration(&M, |
| 753 | llvm::Intrinsic::dbg_stoppoint); |
| 754 | |
| 755 | // Invoke llvm.dbg.stoppoint |
| 756 | Value *Args[] = { |
| 757 | llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo), |
| 758 | llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo), |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 759 | getCastToEmpty(CU) |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 760 | }; |
| 761 | CallInst::Create(StopPointFn, Args, Args+3, "", BB); |
| 762 | } |
| 763 | |
| 764 | /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to |
| 765 | /// mark the start of the specified subprogram. |
| 766 | void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) { |
| 767 | // Lazily construct llvm.dbg.func.start. |
| 768 | if (!FuncStartFn) |
| 769 | FuncStartFn = llvm::Intrinsic::getDeclaration(&M, |
| 770 | llvm::Intrinsic::dbg_func_start); |
| 771 | |
| 772 | // Call llvm.dbg.func.start which also implicitly sets a stoppoint. |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 773 | CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to |
| 777 | /// mark the start of a region for the specified scoping descriptor. |
| 778 | void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) { |
| 779 | // Lazily construct llvm.dbg.region.start function. |
| 780 | if (!RegionStartFn) |
| 781 | RegionStartFn = llvm::Intrinsic::getDeclaration(&M, |
| 782 | llvm::Intrinsic::dbg_region_start); |
| 783 | // Call llvm.dbg.func.start. |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 784 | CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
| 788 | /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to |
| 789 | /// mark the end of a region for the specified scoping descriptor. |
| 790 | void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) { |
| 791 | // Lazily construct llvm.dbg.region.end function. |
| 792 | if (!RegionEndFn) |
| 793 | RegionEndFn = llvm::Intrinsic::getDeclaration(&M, |
| 794 | llvm::Intrinsic::dbg_region_end); |
| 795 | |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 796 | CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. |
| 800 | void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D, |
| 801 | BasicBlock *BB) { |
| 802 | // Cast the storage to a {}* for the call to llvm.dbg.declare. |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 803 | Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB); |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 804 | |
| 805 | if (!DeclareFn) |
| 806 | DeclareFn = llvm::Intrinsic::getDeclaration(&M, |
| 807 | llvm::Intrinsic::dbg_declare); |
Chris Lattner | 497a7a8 | 2008-11-10 04:10:34 +0000 | [diff] [blame] | 808 | Value *Args[] = { Storage, getCastToEmpty(D) }; |
Chris Lattner | a45664f | 2008-11-10 02:56:27 +0000 | [diff] [blame] | 809 | CallInst::Create(DeclareFn, Args, Args+2, "", BB); |
| 810 | } |
Torok Edwin | 620f280 | 2008-12-16 09:07:36 +0000 | [diff] [blame] | 811 | |
| 812 | namespace llvm { |
| 813 | /// Finds the stoppoint coressponding to this instruction, that is the |
| 814 | /// stoppoint that dominates this instruction |
| 815 | const DbgStopPointInst *findStopPoint(const Instruction *Inst) |
| 816 | { |
| 817 | if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst)) |
| 818 | return DSI; |
| 819 | |
| 820 | const BasicBlock *BB = Inst->getParent(); |
| 821 | BasicBlock::const_iterator I = Inst, B; |
| 822 | do { |
| 823 | B = BB->begin(); |
| 824 | // A BB consisting only of a terminator can't have a stoppoint. |
| 825 | if (I != B) { |
| 826 | do { |
| 827 | --I; |
| 828 | if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I)) |
| 829 | return DSI; |
| 830 | } while (I != B); |
| 831 | } |
| 832 | // This BB didn't have a stoppoint: if there is only one |
| 833 | // predecessor, look for a stoppoint there. |
| 834 | // We could use getIDom(), but that would require dominator info. |
| 835 | BB = I->getParent()->getUniquePredecessor(); |
| 836 | if (BB) |
| 837 | I = BB->getTerminator(); |
| 838 | } while (BB != 0); |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /// Finds the stoppoint corresponding to first real (non-debug intrinsic) |
| 843 | /// instruction in this Basic Block, and returns the stoppoint for it. |
| 844 | const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) |
| 845 | { |
| 846 | for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 847 | if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I)) |
| 848 | return DSI; |
| 849 | } |
| 850 | // Fallback to looking for stoppoint of unique predecessor. |
| 851 | // Useful if this BB contains no stoppoints, but unique predecessor does. |
| 852 | BB = BB->getUniquePredecessor(); |
| 853 | if (BB) |
| 854 | return findStopPoint(BB->getTerminator()); |
| 855 | return 0; |
| 856 | } |
| 857 | |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 858 | Value *findDbgGlobalDeclare(GlobalVariable *V) |
| 859 | { |
| 860 | const Module *M = V->getParent(); |
| 861 | const Type *Ty = M->getTypeByName("llvm.dbg.global_variable.type"); |
| 862 | if (!Ty) |
| 863 | return 0; |
| 864 | Ty = PointerType::get(Ty, 0); |
| 865 | |
| 866 | Value *Val = V->stripPointerCasts(); |
| 867 | for (Value::use_iterator I = Val->use_begin(), E =Val->use_end(); |
| 868 | I != E; ++I) { |
| 869 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I)) { |
| 870 | if (CE->getOpcode() == Instruction::BitCast) { |
| 871 | Value *VV = CE; |
| 872 | while (VV->hasOneUse()) { |
| 873 | VV = *VV->use_begin(); |
| 874 | } |
| 875 | if (VV->getType() == Ty) |
| 876 | return VV; |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (Val->getType() == Ty) |
| 882 | return Val; |
| 883 | return 0; |
| 884 | } |
| 885 | |
Torok Edwin | 620f280 | 2008-12-16 09:07:36 +0000 | [diff] [blame] | 886 | /// Finds the dbg.declare intrinsic corresponding to this value if any. |
| 887 | /// It looks through pointer casts too. |
| 888 | const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) |
| 889 | { |
| 890 | if (stripCasts) { |
| 891 | V = V->stripPointerCasts(); |
| 892 | // Look for the bitcast. |
| 893 | for (Value::use_const_iterator I = V->use_begin(), E =V->use_end(); |
| 894 | I != E; ++I) { |
| 895 | if (isa<BitCastInst>(I)) |
| 896 | return findDbgDeclare(*I, false); |
| 897 | } |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | // Find dbg.declare among uses of the instruction. |
| 902 | for (Value::use_const_iterator I = V->use_begin(), E =V->use_end(); |
| 903 | I != E; ++I) { |
| 904 | if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) |
| 905 | return DDI; |
| 906 | } |
| 907 | return 0; |
| 908 | } |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 909 | |
| 910 | bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 911 | unsigned &LineNo, std::string &File, std::string &Dir) |
| 912 | { |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 913 | DICompileUnit Unit; |
| 914 | DIType TypeD; |
| 915 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) { |
| 916 | Value *DIGV = findDbgGlobalDeclare(GV); |
| 917 | if (!DIGV) |
| 918 | return false; |
| 919 | DIGlobalVariable Var(cast<GlobalVariable>(DIGV)); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 920 | Var.getDisplayName(DisplayName); |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 921 | LineNo = Var.getLineNumber(); |
| 922 | Unit = Var.getCompileUnit(); |
| 923 | TypeD = Var.getType(); |
| 924 | } else { |
| 925 | const DbgDeclareInst *DDI = findDbgDeclare(V); |
| 926 | if (!DDI) |
| 927 | return false; |
| 928 | DIVariable Var(cast<GlobalVariable>(DDI->getVariable())); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 929 | Var.getName(DisplayName); |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 930 | LineNo = Var.getLineNumber(); |
| 931 | Unit = Var.getCompileUnit(); |
| 932 | TypeD = Var.getType(); |
| 933 | } |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 934 | TypeD.getName(Type); |
| 935 | Unit.getFilename(File); |
| 936 | Unit.getDirectory(Dir); |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 937 | return true; |
| 938 | } |
Torok Edwin | 620f280 | 2008-12-16 09:07:36 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 941 | /// dump - print compile unit. |
| 942 | void DICompileUnit::dump() const { |
Devang Patel | d8e880c | 2009-02-24 23:15:09 +0000 | [diff] [blame] | 943 | if (getLanguage()) |
| 944 | cerr << " [" << dwarf::LanguageString(getLanguage()) << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 945 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 946 | std::string Res1, Res2; |
| 947 | cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]"; |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /// dump - print type. |
| 951 | void DIType::dump() const { |
| 952 | if (isNull()) return; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 953 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 954 | std::string Res; |
| 955 | if (!getName(Res).empty()) |
| 956 | cerr << " [" << Res << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 957 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 958 | unsigned Tag = getTag(); |
| 959 | cerr << " [" << dwarf::TagString(Tag) << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 960 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 961 | // TODO : Print context |
| 962 | getCompileUnit().dump(); |
| 963 | cerr << " [" |
| 964 | << getLineNumber() << ", " |
| 965 | << getSizeInBits() << ", " |
| 966 | << getAlignInBits() << ", " |
| 967 | << getOffsetInBits() |
| 968 | << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 969 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 970 | if (isPrivate()) |
| 971 | cerr << " [private] "; |
| 972 | else if (isProtected()) |
| 973 | cerr << " [protected] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 974 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 975 | if (isForwardDecl()) |
| 976 | cerr << " [fwd] "; |
| 977 | |
| 978 | if (isBasicType(Tag)) |
| 979 | DIBasicType(GV).dump(); |
| 980 | else if (isDerivedType(Tag)) |
| 981 | DIDerivedType(GV).dump(); |
| 982 | else if (isCompositeType(Tag)) |
| 983 | DICompositeType(GV).dump(); |
| 984 | else { |
| 985 | cerr << "Invalid DIType\n"; |
| 986 | return; |
| 987 | } |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 988 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 989 | cerr << "\n"; |
| 990 | } |
| 991 | |
| 992 | /// dump - print basic type. |
| 993 | void DIBasicType::dump() const { |
| 994 | cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] "; |
| 995 | |
| 996 | } |
| 997 | |
| 998 | /// dump - print derived type. |
| 999 | void DIDerivedType::dump() const { |
| 1000 | cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump(); |
| 1001 | } |
| 1002 | |
| 1003 | /// dump - print composite type. |
| 1004 | void DICompositeType::dump() const { |
| 1005 | DIArray A = getTypeArray(); |
| 1006 | if (A.isNull()) |
| 1007 | return; |
| 1008 | cerr << " [" << A.getNumElements() << " elements]"; |
| 1009 | } |
| 1010 | |
| 1011 | /// dump - print global. |
| 1012 | void DIGlobal::dump() const { |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1013 | std::string Res; |
| 1014 | if (!getName(Res).empty()) |
| 1015 | cerr << " [" << Res << "] "; |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1016 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1017 | unsigned Tag = getTag(); |
| 1018 | cerr << " [" << dwarf::TagString(Tag) << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 1019 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1020 | // TODO : Print context |
| 1021 | getCompileUnit().dump(); |
| 1022 | cerr << " [" << getLineNumber() << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 1023 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1024 | if (isLocalToUnit()) |
| 1025 | cerr << " [local] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 1026 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1027 | if (isDefinition()) |
| 1028 | cerr << " [def] "; |
| 1029 | |
| 1030 | if (isGlobalVariable(Tag)) |
| 1031 | DIGlobalVariable(GV).dump(); |
| 1032 | |
| 1033 | cerr << "\n"; |
| 1034 | } |
| 1035 | |
| 1036 | /// dump - print subprogram. |
| 1037 | void DISubprogram::dump() const { |
| 1038 | DIGlobal::dump(); |
| 1039 | } |
| 1040 | |
| 1041 | /// dump - print global variable. |
| 1042 | void DIGlobalVariable::dump() const { |
| 1043 | cerr << " ["; getGlobal()->dump(); cerr << "] "; |
| 1044 | } |
| 1045 | |
| 1046 | /// dump - print variable. |
| 1047 | void DIVariable::dump() const { |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 1048 | std::string Res; |
| 1049 | if (!getName(Res).empty()) |
| 1050 | cerr << " [" << Res << "] "; |
Bill Wendling | ccbdc7a | 2009-03-09 05:04:40 +0000 | [diff] [blame] | 1051 | |
Devang Patel | bf3f5a0 | 2009-01-30 01:03:10 +0000 | [diff] [blame] | 1052 | getCompileUnit().dump(); |
| 1053 | cerr << " [" << getLineNumber() << "] "; |
| 1054 | getType().dump(); |
| 1055 | cerr << "\n"; |
| 1056 | } |