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