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