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