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