Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +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 | |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
Duncan P. N. Exon Smith | c22a5c2 | 2015-02-21 00:43:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSwitch.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
| 22 | #include "llvm/IR/Constants.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 29 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | using namespace llvm; |
| 34 | using namespace llvm::dwarf; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // DIDescriptor |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Duncan P. N. Exon Smith | c22a5c2 | 2015-02-21 00:43:09 +0000 | [diff] [blame] | 40 | unsigned DIDescriptor::getFlag(StringRef Flag) { |
| 41 | return StringSwitch<unsigned>(Flag) |
| 42 | #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) |
| 43 | #include "llvm/IR/DebugInfoFlags.def" |
| 44 | .Default(0); |
| 45 | } |
| 46 | |
| 47 | const char *DIDescriptor::getFlagString(unsigned Flag) { |
| 48 | switch (Flag) { |
| 49 | default: |
| 50 | return ""; |
| 51 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 52 | case Flag##NAME: \ |
| 53 | return "DIFlag" #NAME; |
| 54 | #include "llvm/IR/DebugInfoFlags.def" |
| 55 | } |
| 56 | } |
| 57 | |
Duncan P. N. Exon Smith | 269e38d | 2015-02-21 00:45:26 +0000 | [diff] [blame] | 58 | unsigned DIDescriptor::splitFlags(unsigned Flags, |
| 59 | SmallVectorImpl<unsigned> &SplitFlags) { |
| 60 | // Accessibility flags need to be specially handled, since they're packed |
| 61 | // together. |
| 62 | if (unsigned A = Flags & FlagAccessibility) { |
| 63 | if (A == FlagPrivate) |
| 64 | SplitFlags.push_back(FlagPrivate); |
| 65 | else if (A == FlagProtected) |
| 66 | SplitFlags.push_back(FlagProtected); |
| 67 | else |
| 68 | SplitFlags.push_back(FlagPublic); |
| 69 | Flags &= ~A; |
| 70 | } |
| 71 | |
| 72 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 73 | if (unsigned Bit = Flags & ID) { \ |
| 74 | SplitFlags.push_back(Bit); \ |
| 75 | Flags &= ~Bit; \ |
| 76 | } |
| 77 | #include "llvm/IR/DebugInfoFlags.def" |
| 78 | |
| 79 | return Flags; |
| 80 | } |
| 81 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 82 | bool DIDescriptor::Verify() const { |
| 83 | return DbgNode && |
| 84 | (DIDerivedType(DbgNode).Verify() || |
| 85 | DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() || |
| 86 | DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() || |
| 87 | DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() || |
| 88 | DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() || |
| 89 | DILexicalBlock(DbgNode).Verify() || |
| 90 | DILexicalBlockFile(DbgNode).Verify() || |
| 91 | DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() || |
| 92 | DIObjCProperty(DbgNode).Verify() || |
| 93 | DITemplateTypeParameter(DbgNode).Verify() || |
| 94 | DITemplateValueParameter(DbgNode).Verify() || |
Duncan P. N. Exon Smith | e9d379c | 2015-03-16 21:03:55 +0000 | [diff] [blame] | 95 | DIImportedEntity(DbgNode).Verify()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 98 | static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 99 | if (!DbgNode || Elt >= DbgNode->getNumOperands()) |
| 100 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 101 | return DbgNode->getOperand(Elt); |
| 102 | } |
| 103 | |
| 104 | static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { |
| 105 | return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); |
| 106 | } |
| 107 | |
| 108 | static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) { |
| 109 | if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt))) |
| 110 | return MDS->getString(); |
| 111 | return StringRef(); |
| 112 | } |
| 113 | |
| 114 | StringRef DIDescriptor::getStringField(unsigned Elt) const { |
| 115 | return ::getStringField(DbgNode, Elt); |
| 116 | } |
| 117 | |
| 118 | uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 119 | if (auto *C = getConstantField(Elt)) |
| 120 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 121 | return CI->getZExtValue(); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int64_t DIDescriptor::getInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 127 | if (auto *C = getConstantField(Elt)) |
| 128 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 129 | return CI->getZExtValue(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 135 | MDNode *Field = getNodeField(DbgNode, Elt); |
| 136 | return DIDescriptor(Field); |
| 137 | } |
| 138 | |
| 139 | GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 140 | return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | Constant *DIDescriptor::getConstantField(unsigned Elt) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 144 | if (!DbgNode) |
| 145 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 146 | |
| 147 | if (Elt < DbgNode->getNumOperands()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 148 | if (auto *C = |
| 149 | dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt))) |
| 150 | return C->getValue(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 151 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | Function *DIDescriptor::getFunctionField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 155 | return dyn_cast_or_null<Function>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 158 | /// \brief Return the size reported by the variable's type. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 159 | unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) { |
| 160 | DIType Ty = getType().resolve(Map); |
| 161 | // Follow derived types until we reach a type that |
| 162 | // reports back a size. |
| 163 | while (Ty.isDerivedType() && !Ty.getSizeInBits()) { |
| 164 | DIDerivedType DT(&*Ty); |
| 165 | Ty = DT.getTypeDerivedFrom().resolve(Map); |
| 166 | } |
| 167 | assert(Ty.getSizeInBits() && "type with size 0"); |
| 168 | return Ty.getSizeInBits(); |
| 169 | } |
| 170 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 171 | bool DIExpression::isBitPiece() const { |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 172 | unsigned N = getNumElements(); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 173 | return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 176 | uint64_t DIExpression::getBitPieceOffset() const { |
| 177 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 178 | return getElement(getNumElements()-2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 181 | uint64_t DIExpression::getBitPieceSize() const { |
| 182 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 183 | return getElement(getNumElements()-1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 184 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 185 | |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 186 | DIExpression::iterator DIExpression::Operand::getNext() const { |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 187 | iterator it(I); |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 188 | return ++it; |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 192 | // Simple Descriptor Constructors and other Methods |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 195 | void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 196 | assert(DbgNode && "Trying to replace an unverified type!"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 197 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 198 | TempMDNode Temp(get()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 199 | |
| 200 | // Since we use a TrackingVH for the node, its easy for clients to manufacture |
| 201 | // legitimate situations where they want to replaceAllUsesWith() on something |
| 202 | // which, due to uniquing, has merged with the source. We shield clients from |
| 203 | // this detail by allowing a value to be replaced with replaceAllUsesWith() |
| 204 | // itself. |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 205 | if (Temp.get() == D.get()) { |
| 206 | DbgNode = MDNode::replaceWithUniqued(std::move(Temp)); |
| 207 | return; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 208 | } |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 209 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 210 | Temp->replaceAllUsesWith(D.get()); |
| 211 | DbgNode = D.get(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 214 | void DIDescriptor::replaceAllUsesWith(MDNode *D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 215 | assert(DbgNode && "Trying to replace an unverified type!"); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 216 | assert(DbgNode != D && "This replacement should always happen"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 217 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 218 | TempMDNode Node(get()); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 219 | Node->replaceAllUsesWith(D); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 222 | bool DICompileUnit::Verify() const { |
| 223 | if (!isCompileUnit()) |
| 224 | return false; |
| 225 | |
| 226 | // Don't bother verifying the compilation directory or producer string |
| 227 | // as those could be empty. |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 228 | return !getFilename().empty(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 231 | bool DIObjCProperty::Verify() const { return isObjCProperty(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 232 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 233 | /// \brief Check if a value can be a reference to a type. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 234 | static bool isTypeRef(const Metadata *MD) { |
| 235 | if (!MD) |
| 236 | return true; |
| 237 | if (auto *S = dyn_cast<MDString>(MD)) |
| 238 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 239 | return isa<MDType>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 242 | /// \brief Check if a value can be a ScopeRef. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 243 | static bool isScopeRef(const Metadata *MD) { |
| 244 | if (!MD) |
| 245 | return true; |
| 246 | if (auto *S = dyn_cast<MDString>(MD)) |
| 247 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 248 | return isa<MDScope>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Duncan P. N. Exon Smith | e445014 | 2015-02-18 19:56:50 +0000 | [diff] [blame] | 251 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 252 | /// \brief Check if a value can be a DescriptorRef. |
| 253 | static bool isDescriptorRef(const Metadata *MD) { |
| 254 | if (!MD) |
| 255 | return true; |
| 256 | if (auto *S = dyn_cast<MDString>(MD)) |
| 257 | return !S->getString().empty(); |
| 258 | return isa<MDNode>(MD); |
| 259 | } |
Duncan P. N. Exon Smith | e445014 | 2015-02-18 19:56:50 +0000 | [diff] [blame] | 260 | #endif |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 261 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 262 | bool DIType::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 263 | auto *N = dyn_cast_or_null<MDType>(DbgNode); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 264 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 265 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 266 | if (!isScopeRef(N->getScope())) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 267 | return false; |
| 268 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 269 | // DIType is abstract, it should be a BasicType, a DerivedType or |
| 270 | // a CompositeType. |
| 271 | if (isBasicType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 272 | return DIBasicType(DbgNode).Verify(); |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 273 | |
| 274 | // FIXME: Sink this into the various subclass verifies. |
| 275 | if (getFilename().empty()) { |
| 276 | // Check whether the filename is allowed to be empty. |
| 277 | uint16_t Tag = getTag(); |
| 278 | if (Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && |
| 279 | Tag != dwarf::DW_TAG_pointer_type && |
| 280 | Tag != dwarf::DW_TAG_ptr_to_member_type && |
| 281 | Tag != dwarf::DW_TAG_reference_type && |
| 282 | Tag != dwarf::DW_TAG_rvalue_reference_type && |
| 283 | Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type && |
| 284 | Tag != dwarf::DW_TAG_enumeration_type && |
| 285 | Tag != dwarf::DW_TAG_subroutine_type && |
Peter Collingbourne | b736065 | 2015-03-25 17:44:49 +0000 | [diff] [blame] | 286 | Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && |
| 287 | Tag != dwarf::DW_TAG_structure_type && Tag != dwarf::DW_TAG_member && |
| 288 | Tag != dwarf::DW_TAG_typedef) |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
| 292 | if (isCompositeType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 293 | return DICompositeType(DbgNode).Verify(); |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 294 | if (isDerivedType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 295 | return DIDerivedType(DbgNode).Verify(); |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 296 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 299 | bool DIBasicType::Verify() const { |
| 300 | return dyn_cast_or_null<MDBasicType>(DbgNode); |
| 301 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 302 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 303 | bool DIDerivedType::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 304 | auto *N = dyn_cast_or_null<MDDerivedTypeBase>(DbgNode); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 305 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 306 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 307 | if (getTag() == dwarf::DW_TAG_ptr_to_member_type) { |
| 308 | auto *D = dyn_cast<MDDerivedType>(N); |
| 309 | if (!D) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 310 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 311 | if (!isTypeRef(D->getExtraData())) |
| 312 | return false; |
| 313 | } |
| 314 | return isTypeRef(N->getBaseType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 317 | bool DICompositeType::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 318 | auto *N = dyn_cast_or_null<MDCompositeTypeBase>(DbgNode); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 319 | return N && isTypeRef(N->getBaseType()) && isTypeRef(N->getVTableHolder()) && |
| 320 | !(isLValueReference() && isRValueReference()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 323 | bool DISubprogram::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 324 | auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 325 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 328 | if (!isScopeRef(N->getScope())) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 329 | return false; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 330 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 331 | if (auto *Op = N->getType()) |
| 332 | if (!isa<MDNode>(Op)) |
| 333 | return false; |
| 334 | |
| 335 | if (!isTypeRef(getContainingType())) |
| 336 | return false; |
| 337 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 338 | if (isLValueReference() && isRValueReference()) |
| 339 | return false; |
| 340 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 341 | // If a DISubprogram has an llvm::Function*, then scope chains from all |
| 342 | // instructions within the function should lead to this DISubprogram. |
| 343 | if (auto *F = getFunction()) { |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 344 | for (auto &BB : *F) { |
| 345 | for (auto &I : BB) { |
Duncan P. N. Exon Smith | 215e7ed | 2015-03-30 17:06:38 +0000 | [diff] [blame] | 346 | MDLocation *DL = |
| 347 | cast_or_null<MDLocation>(I.getDebugLoc().getAsMDNode()); |
| 348 | if (!DL) |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 349 | continue; |
| 350 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 351 | // walk the inlined-at scopes |
Duncan P. N. Exon Smith | 8f7bc79 | 2015-03-30 17:41:24 +0000 | [diff] [blame] | 352 | MDScope *Scope = DL->getInlinedAtScope(); |
Adrian Prantl | de200df | 2015-01-20 22:37:25 +0000 | [diff] [blame] | 353 | if (!Scope) |
| 354 | return false; |
Duncan P. N. Exon Smith | 215e7ed | 2015-03-30 17:06:38 +0000 | [diff] [blame] | 355 | while (!isa<MDSubprogram>(Scope)) { |
| 356 | Scope = cast<MDLexicalBlockBase>(Scope)->getScope(); |
Adrian Prantl | 1292e24 | 2015-01-21 18:32:56 +0000 | [diff] [blame] | 357 | if (!Scope) |
| 358 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 359 | } |
| 360 | if (!DISubprogram(Scope).describes(F)) |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 365 | |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 366 | return true; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 369 | bool DIGlobalVariable::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 370 | auto *N = dyn_cast_or_null<MDGlobalVariable>(DbgNode); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 371 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 372 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 373 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 374 | |
| 375 | if (N->getDisplayName().empty()) |
| 376 | return false; |
| 377 | |
| 378 | if (auto *Op = N->getScope()) |
| 379 | if (!isa<MDNode>(Op)) |
| 380 | return false; |
| 381 | |
| 382 | if (auto *Op = N->getStaticDataMemberDeclaration()) |
| 383 | if (!isa<MDNode>(Op)) |
| 384 | return false; |
| 385 | |
| 386 | return isTypeRef(N->getType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 389 | bool DIVariable::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 390 | auto *N = dyn_cast_or_null<MDLocalVariable>(DbgNode); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 391 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 392 | if (!N) |
| 393 | return false; |
| 394 | |
| 395 | if (auto *Op = N->getScope()) |
| 396 | if (!isa<MDNode>(Op)) |
| 397 | return false; |
| 398 | |
| 399 | return isTypeRef(N->getType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 402 | bool DILocation::Verify() const { |
| 403 | return dyn_cast_or_null<MDLocation>(DbgNode); |
| 404 | } |
| 405 | bool DINameSpace::Verify() const { |
| 406 | return dyn_cast_or_null<MDNamespace>(DbgNode); |
| 407 | } |
| 408 | bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); } |
| 409 | bool DIEnumerator::Verify() const { |
| 410 | return dyn_cast_or_null<MDEnumerator>(DbgNode); |
| 411 | } |
| 412 | bool DISubrange::Verify() const { |
| 413 | return dyn_cast_or_null<MDSubrange>(DbgNode); |
| 414 | } |
| 415 | bool DILexicalBlock::Verify() const { |
| 416 | return dyn_cast_or_null<MDLexicalBlock>(DbgNode); |
| 417 | } |
| 418 | bool DILexicalBlockFile::Verify() const { |
| 419 | return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode); |
| 420 | } |
| 421 | bool DITemplateTypeParameter::Verify() const { |
| 422 | return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode); |
| 423 | } |
| 424 | bool DITemplateValueParameter::Verify() const { |
| 425 | return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode); |
| 426 | } |
| 427 | bool DIImportedEntity::Verify() const { |
| 428 | return dyn_cast_or_null<MDImportedEntity>(DbgNode); |
| 429 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 430 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 431 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 432 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 433 | if (Elements) |
| 434 | N->replaceElements(cast<MDTuple>(Elements)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 435 | if (TParams) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 436 | N->replaceTemplateParams(cast<MDTuple>(TParams)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 437 | DbgNode = N; |
| 438 | } |
| 439 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 440 | DIScopeRef DIScope::getRef() const { |
| 441 | if (!isCompositeType()) |
| 442 | return DIScopeRef(*this); |
| 443 | DICompositeType DTy(DbgNode); |
| 444 | if (!DTy.getIdentifier()) |
| 445 | return DIScopeRef(*this); |
| 446 | return DIScopeRef(DTy.getIdentifier()); |
| 447 | } |
| 448 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 449 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 450 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 451 | N->replaceVTableHolder(ContainingType.getRef()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 452 | DbgNode = N; |
| 453 | } |
| 454 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 455 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 456 | assert(CurFn && "Invalid function"); |
| 457 | if (!getContext().isSubprogram()) |
| 458 | return false; |
| 459 | // This variable is not inlined function argument if its scope |
| 460 | // does not describe current function. |
| 461 | return !DISubprogram(getContext()).describes(CurFn); |
| 462 | } |
| 463 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 464 | Function *DISubprogram::getFunction() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 465 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 466 | if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction())) |
| 467 | return dyn_cast<Function>(C->getValue()); |
| 468 | return nullptr; |
| 469 | } |
| 470 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 471 | bool DISubprogram::describes(const Function *F) { |
| 472 | assert(F && "Invalid function"); |
| 473 | if (F == getFunction()) |
| 474 | return true; |
| 475 | StringRef Name = getLinkageName(); |
| 476 | if (Name.empty()) |
| 477 | Name = getName(); |
| 478 | if (F->getName() == Name) |
| 479 | return true; |
| 480 | return false; |
| 481 | } |
| 482 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 483 | GlobalVariable *DIGlobalVariable::getGlobal() const { |
| 484 | return dyn_cast_or_null<GlobalVariable>(getConstant()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 487 | DIScopeRef DIScope::getContext() const { |
| 488 | |
| 489 | if (isType()) |
| 490 | return DIType(DbgNode).getContext(); |
| 491 | |
| 492 | if (isSubprogram()) |
| 493 | return DIScopeRef(DISubprogram(DbgNode).getContext()); |
| 494 | |
| 495 | if (isLexicalBlock()) |
| 496 | return DIScopeRef(DILexicalBlock(DbgNode).getContext()); |
| 497 | |
| 498 | if (isLexicalBlockFile()) |
| 499 | return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); |
| 500 | |
| 501 | if (isNameSpace()) |
| 502 | return DIScopeRef(DINameSpace(DbgNode).getContext()); |
| 503 | |
| 504 | assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 505 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 508 | StringRef DIScope::getName() const { |
| 509 | if (isType()) |
| 510 | return DIType(DbgNode).getName(); |
| 511 | if (isSubprogram()) |
| 512 | return DISubprogram(DbgNode).getName(); |
| 513 | if (isNameSpace()) |
| 514 | return DINameSpace(DbgNode).getName(); |
| 515 | assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || |
| 516 | isCompileUnit()) && |
| 517 | "Unhandled type of scope."); |
| 518 | return StringRef(); |
| 519 | } |
| 520 | |
| 521 | StringRef DIScope::getFilename() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 522 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 523 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0); |
| 524 | return ""; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | StringRef DIScope::getDirectory() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 528 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 529 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1); |
| 530 | return ""; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
| 534 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 535 | get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get())); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
| 539 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 540 | get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get())); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 543 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 544 | DILexicalBlockFile NewScope) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 545 | assert(Verify()); |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 546 | assert(NewScope && "Expected valid scope"); |
| 547 | |
| 548 | const auto *Old = cast<MDLocation>(DbgNode); |
| 549 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 550 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 553 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 554 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 555 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 556 | } |
| 557 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 558 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 559 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 560 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 561 | return cast<MDLocalVariable>(DV) |
| 562 | ->withInline(cast_or_null<MDLocation>(InlinedScope)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 565 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 566 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 567 | return cast<MDLocalVariable>(DV)->withoutInline(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 570 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
| 571 | DIDescriptor D(Scope); |
| 572 | if (D.isSubprogram()) |
| 573 | return DISubprogram(Scope); |
| 574 | |
| 575 | if (D.isLexicalBlockFile()) |
| 576 | return getDISubprogram(DILexicalBlockFile(Scope).getContext()); |
| 577 | |
| 578 | if (D.isLexicalBlock()) |
| 579 | return getDISubprogram(DILexicalBlock(Scope).getContext()); |
| 580 | |
| 581 | return DISubprogram(); |
| 582 | } |
| 583 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 584 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 585 | // We look for the first instr that has a debug annotation leading back to F. |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 586 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 587 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
| 588 | return !Inst.getDebugLoc().isUnknown(); |
| 589 | }); |
| 590 | if (Inst == BB.end()) |
| 591 | continue; |
| 592 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 593 | const MDNode *Scope = DLoc.getScopeNode(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 594 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 595 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return DISubprogram(); |
| 599 | } |
| 600 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 601 | DICompositeType llvm::getDICompositeType(DIType T) { |
| 602 | if (T.isCompositeType()) |
| 603 | return DICompositeType(T); |
| 604 | |
| 605 | if (T.isDerivedType()) { |
| 606 | // This function is currently used by dragonegg and dragonegg does |
| 607 | // not generate identifier for types, so using an empty map to resolve |
| 608 | // DerivedFrom should be fine. |
| 609 | DITypeIdentifierMap EmptyMap; |
| 610 | return getDICompositeType( |
| 611 | DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); |
| 612 | } |
| 613 | |
| 614 | return DICompositeType(); |
| 615 | } |
| 616 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 617 | DITypeIdentifierMap |
| 618 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 619 | DITypeIdentifierMap Map; |
| 620 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 621 | DICompileUnit CU(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 622 | DIArray Retain = CU.getRetainedTypes(); |
| 623 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
| 624 | if (!Retain.getElement(Ti).isCompositeType()) |
| 625 | continue; |
| 626 | DICompositeType Ty(Retain.getElement(Ti)); |
| 627 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 628 | // Definition has priority over declaration. |
| 629 | // Try to insert (TypeId, Ty) to Map. |
| 630 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 631 | Map.insert(std::make_pair(TypeId, Ty)); |
| 632 | // If TypeId already exists in Map and this is a definition, replace |
| 633 | // whatever we had (declaration or definition) with the definition. |
| 634 | if (!P.second && !Ty.isForwardDecl()) |
| 635 | P.first->second = Ty; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | return Map; |
| 640 | } |
| 641 | |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | // DebugInfoFinder implementations. |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
| 646 | void DebugInfoFinder::reset() { |
| 647 | CUs.clear(); |
| 648 | SPs.clear(); |
| 649 | GVs.clear(); |
| 650 | TYs.clear(); |
| 651 | Scopes.clear(); |
| 652 | NodesSeen.clear(); |
| 653 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 654 | TypeMapInitialized = false; |
| 655 | } |
| 656 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 657 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 658 | if (!TypeMapInitialized) |
| 659 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 660 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 661 | TypeMapInitialized = true; |
| 662 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 665 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 666 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 667 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 668 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 669 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 670 | addCompileUnit(CU); |
| 671 | DIArray GVs = CU.getGlobalVariables(); |
| 672 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
| 673 | DIGlobalVariable DIG(GVs.getElement(i)); |
| 674 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 675 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 676 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | DIArray SPs = CU.getSubprograms(); |
| 680 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
| 681 | processSubprogram(DISubprogram(SPs.getElement(i))); |
| 682 | DIArray EnumTypes = CU.getEnumTypes(); |
| 683 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
| 684 | processType(DIType(EnumTypes.getElement(i))); |
| 685 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 686 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
| 687 | processType(DIType(RetainedTypes.getElement(i))); |
| 688 | DIArray Imports = CU.getImportedEntities(); |
| 689 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
| 690 | DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); |
Duncan P. N. Exon Smith | d4e07c9 | 2015-03-20 19:13:53 +0000 | [diff] [blame] | 691 | if (!Import) |
| 692 | continue; |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 693 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 694 | if (Entity.isType()) |
| 695 | processType(DIType(Entity)); |
| 696 | else if (Entity.isSubprogram()) |
| 697 | processSubprogram(DISubprogram(Entity)); |
| 698 | else if (Entity.isNameSpace()) |
| 699 | processScope(DINameSpace(Entity).getContext()); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 705 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 706 | if (!Loc) |
| 707 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 708 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 709 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 710 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 713 | void DebugInfoFinder::processType(DIType DT) { |
| 714 | if (!addType(DT)) |
| 715 | return; |
| 716 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
| 717 | if (DT.isCompositeType()) { |
| 718 | DICompositeType DCT(DT); |
| 719 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 720 | if (DT.isSubroutineType()) { |
| 721 | DITypeArray DTA = DISubroutineType(DT).getTypeArray(); |
| 722 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 723 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 724 | return; |
| 725 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 726 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 727 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 728 | DIDescriptor D = DA.getElement(i); |
| 729 | if (D.isType()) |
| 730 | processType(DIType(D)); |
| 731 | else if (D.isSubprogram()) |
| 732 | processSubprogram(DISubprogram(D)); |
| 733 | } |
| 734 | } else if (DT.isDerivedType()) { |
| 735 | DIDerivedType DDT(DT); |
| 736 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void DebugInfoFinder::processScope(DIScope Scope) { |
| 741 | if (Scope.isType()) { |
| 742 | DIType Ty(Scope); |
| 743 | processType(Ty); |
| 744 | return; |
| 745 | } |
| 746 | if (Scope.isCompileUnit()) { |
| 747 | addCompileUnit(DICompileUnit(Scope)); |
| 748 | return; |
| 749 | } |
| 750 | if (Scope.isSubprogram()) { |
| 751 | processSubprogram(DISubprogram(Scope)); |
| 752 | return; |
| 753 | } |
| 754 | if (!addScope(Scope)) |
| 755 | return; |
| 756 | if (Scope.isLexicalBlock()) { |
| 757 | DILexicalBlock LB(Scope); |
| 758 | processScope(LB.getContext()); |
| 759 | } else if (Scope.isLexicalBlockFile()) { |
| 760 | DILexicalBlockFile LBF = DILexicalBlockFile(Scope); |
| 761 | processScope(LBF.getScope()); |
| 762 | } else if (Scope.isNameSpace()) { |
| 763 | DINameSpace NS(Scope); |
| 764 | processScope(NS.getContext()); |
| 765 | } |
| 766 | } |
| 767 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 768 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 769 | if (!addSubprogram(SP)) |
| 770 | return; |
| 771 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 772 | processType(SP.getType()); |
| 773 | DIArray TParams = SP.getTemplateParams(); |
| 774 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 775 | DIDescriptor Element = TParams.getElement(I); |
| 776 | if (Element.isTemplateTypeParameter()) { |
| 777 | DITemplateTypeParameter TType(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 778 | processType(TType.getType().resolve(TypeIdentifierMap)); |
| 779 | } else if (Element.isTemplateValueParameter()) { |
| 780 | DITemplateValueParameter TVal(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 781 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 786 | void DebugInfoFinder::processDeclare(const Module &M, |
| 787 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 788 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 789 | if (!N) |
| 790 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 791 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 792 | |
| 793 | DIDescriptor DV(N); |
| 794 | if (!DV.isVariable()) |
| 795 | return; |
| 796 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 797 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 798 | return; |
| 799 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 800 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 803 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 804 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 805 | if (!N) |
| 806 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 807 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 808 | |
| 809 | DIDescriptor DV(N); |
| 810 | if (!DV.isVariable()) |
| 811 | return; |
| 812 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 813 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 814 | return; |
| 815 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 816 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 819 | bool DebugInfoFinder::addType(DIType DT) { |
| 820 | if (!DT) |
| 821 | return false; |
| 822 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 823 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 824 | return false; |
| 825 | |
| 826 | TYs.push_back(DT); |
| 827 | return true; |
| 828 | } |
| 829 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 830 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 831 | if (!CU) |
| 832 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 833 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 834 | return false; |
| 835 | |
| 836 | CUs.push_back(CU); |
| 837 | return true; |
| 838 | } |
| 839 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 840 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 841 | if (!DIG) |
| 842 | return false; |
| 843 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 844 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 845 | return false; |
| 846 | |
| 847 | GVs.push_back(DIG); |
| 848 | return true; |
| 849 | } |
| 850 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 851 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 852 | if (!SP) |
| 853 | return false; |
| 854 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 855 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 856 | return false; |
| 857 | |
| 858 | SPs.push_back(SP); |
| 859 | return true; |
| 860 | } |
| 861 | |
| 862 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 863 | if (!Scope) |
| 864 | return false; |
| 865 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 866 | // as null for now. |
| 867 | if (Scope->getNumOperands() == 0) |
| 868 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 869 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 870 | return false; |
| 871 | Scopes.push_back(Scope); |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | //===----------------------------------------------------------------------===// |
| 876 | // DIDescriptor: dump routines for all descriptors. |
| 877 | //===----------------------------------------------------------------------===// |
| 878 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 879 | void DIDescriptor::dump() const { |
| 880 | print(dbgs()); |
| 881 | dbgs() << '\n'; |
| 882 | } |
| 883 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 884 | void DIDescriptor::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 885 | if (!get()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 886 | return; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 887 | get()->print(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 891 | const LLVMContext &Ctx) { |
| 892 | if (!DL.isUnknown()) { // Print source line info. |
| 893 | DIScope Scope(DL.getScope(Ctx)); |
| 894 | assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); |
| 895 | // Omit the directory, because it's likely to be long and uninteresting. |
| 896 | CommentOS << Scope.getFilename(); |
| 897 | CommentOS << ':' << DL.getLine(); |
| 898 | if (DL.getCol() != 0) |
| 899 | CommentOS << ':' << DL.getCol(); |
| 900 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); |
| 901 | if (!InlinedAtDL.isUnknown()) { |
| 902 | CommentOS << " @[ "; |
| 903 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 904 | CommentOS << " ]"; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 910 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 911 | StringRef Res = getName(); |
| 912 | if (!Res.empty()) |
| 913 | OS << Res << "," << getLineNumber(); |
| 914 | if (MDNode *InlinedAt = getInlinedAt()) { |
| 915 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); |
| 916 | if (!InlinedAtDL.isUnknown()) { |
| 917 | OS << " @["; |
| 918 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 919 | OS << "]"; |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 924 | template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) { |
| 925 | assert(isDescriptorRef(V) && |
| 926 | "DIDescriptorRef should be a MDString or MDNode"); |
| 927 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 928 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 929 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 930 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 931 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 932 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 933 | } |
| 934 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 935 | template <> |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 936 | DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const { |
| 937 | return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
| 938 | } |
| 939 | template <> |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 940 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 941 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 942 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 943 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 944 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 945 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 946 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 947 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 948 | bool Changed = false; |
| 949 | |
| 950 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 951 | // the module. |
| 952 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 953 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 954 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 955 | CI->eraseFromParent(); |
| 956 | } |
| 957 | Declare->eraseFromParent(); |
| 958 | Changed = true; |
| 959 | } |
| 960 | |
| 961 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 962 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 963 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 964 | CI->eraseFromParent(); |
| 965 | } |
| 966 | DbgVal->eraseFromParent(); |
| 967 | Changed = true; |
| 968 | } |
| 969 | |
| 970 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 971 | NME = M.named_metadata_end(); NMI != NME;) { |
| 972 | NamedMDNode *NMD = NMI; |
| 973 | ++NMI; |
| 974 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 975 | NMD->eraseFromParent(); |
| 976 | Changed = true; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 981 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; |
| 982 | ++FI) |
| 983 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; |
| 984 | ++BI) { |
| 985 | if (!BI->getDebugLoc().isUnknown()) { |
| 986 | Changed = true; |
| 987 | BI->setDebugLoc(DebugLoc()); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | return Changed; |
| 992 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 993 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 994 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 995 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 996 | M.getModuleFlag("Debug Info Version"))) |
| 997 | return Val->getZExtValue(); |
| 998 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 999 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1000 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 1001 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 1002 | llvm::makeSubprogramMap(const Module &M) { |
| 1003 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1004 | |
| 1005 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 1006 | if (!CU_Nodes) |
| 1007 | return R; |
| 1008 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1009 | for (MDNode *N : CU_Nodes->operands()) { |
| 1010 | DICompileUnit CUNode(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1011 | DIArray SPs = CUNode.getSubprograms(); |
| 1012 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 1013 | DISubprogram SP(SPs.getElement(i)); |
| 1014 | if (Function *F = SP.getFunction()) |
| 1015 | R.insert(std::make_pair(F, SP)); |
| 1016 | } |
| 1017 | } |
| 1018 | return R; |
| 1019 | } |