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() || |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 95 | DIImportedEntity(DbgNode).Verify() || DIExpression(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 | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 263 | auto *N = getRaw(); |
| 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 && |
| 286 | Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend) |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | if (isCompositeType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 291 | return DICompositeType(DbgNode).Verify(); |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 292 | if (isDerivedType()) |
Renato Golin | 47f46fd | 2013-11-26 16:47:00 +0000 | [diff] [blame] | 293 | return DIDerivedType(DbgNode).Verify(); |
Duncan P. N. Exon Smith | f374040 | 2015-03-16 20:46:27 +0000 | [diff] [blame] | 294 | return false; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 297 | bool DIBasicType::Verify() const { return getRaw(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 298 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 299 | bool DIDerivedType::Verify() const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 300 | auto *N = getRaw(); |
| 301 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 302 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 303 | if (getTag() == dwarf::DW_TAG_ptr_to_member_type) { |
| 304 | auto *D = dyn_cast<MDDerivedType>(N); |
| 305 | if (!D) |
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 (!isTypeRef(D->getExtraData())) |
| 308 | return false; |
| 309 | } |
| 310 | return isTypeRef(N->getBaseType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 313 | bool DICompositeType::Verify() const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 314 | auto *N = getRaw(); |
| 315 | return N && isTypeRef(N->getBaseType()) && isTypeRef(N->getVTableHolder()) && |
| 316 | !(isLValueReference() && isRValueReference()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 319 | bool DISubprogram::Verify() const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 320 | auto *N = getRaw(); |
| 321 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 324 | if (!isScopeRef(N->getScope())) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 325 | return false; |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 326 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 327 | if (auto *Op = N->getType()) |
| 328 | if (!isa<MDNode>(Op)) |
| 329 | return false; |
| 330 | |
| 331 | if (!isTypeRef(getContainingType())) |
| 332 | return false; |
| 333 | |
Adrian Prantl | 99c7af2 | 2013-12-18 21:48:19 +0000 | [diff] [blame] | 334 | if (isLValueReference() && isRValueReference()) |
| 335 | return false; |
| 336 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 337 | // If a DISubprogram has an llvm::Function*, then scope chains from all |
| 338 | // instructions within the function should lead to this DISubprogram. |
| 339 | if (auto *F = getFunction()) { |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 340 | for (auto &BB : *F) { |
| 341 | for (auto &I : BB) { |
| 342 | DebugLoc DL = I.getDebugLoc(); |
| 343 | if (DL.isUnknown()) |
| 344 | continue; |
| 345 | |
| 346 | MDNode *Scope = nullptr; |
| 347 | MDNode *IA = nullptr; |
| 348 | // walk the inlined-at scopes |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 349 | while ((IA = DL.getInlinedAt())) |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 350 | DL = DebugLoc::getFromDILocation(IA); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 351 | DL.getScopeAndInlinedAt(Scope, IA); |
Adrian Prantl | de200df | 2015-01-20 22:37:25 +0000 | [diff] [blame] | 352 | if (!Scope) |
| 353 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 354 | assert(!IA); |
| 355 | while (!DIDescriptor(Scope).isSubprogram()) { |
| 356 | DILexicalBlockFile D(Scope); |
| 357 | Scope = D.isLexicalBlockFile() |
| 358 | ? D.getScope() |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 359 | : DebugLoc::getFromDILexicalBlock(Scope).getScope(); |
Adrian Prantl | 1292e24 | 2015-01-21 18:32:56 +0000 | [diff] [blame] | 360 | if (!Scope) |
| 361 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 362 | } |
| 363 | if (!DISubprogram(Scope).describes(F)) |
| 364 | return false; |
| 365 | } |
| 366 | } |
| 367 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 368 | |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 369 | return true; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 372 | bool DIGlobalVariable::Verify() const { |
| 373 | auto *N = getRaw(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 374 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 375 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 376 | return false; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 377 | |
| 378 | if (N->getDisplayName().empty()) |
| 379 | return false; |
| 380 | |
| 381 | if (auto *Op = N->getScope()) |
| 382 | if (!isa<MDNode>(Op)) |
| 383 | return false; |
| 384 | |
| 385 | if (auto *Op = N->getStaticDataMemberDeclaration()) |
| 386 | if (!isa<MDNode>(Op)) |
| 387 | return false; |
| 388 | |
| 389 | return isTypeRef(N->getType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 392 | bool DIVariable::Verify() const { |
| 393 | auto *N = getRaw(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 394 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 395 | if (!N) |
| 396 | return false; |
| 397 | |
| 398 | if (auto *Op = N->getScope()) |
| 399 | if (!isa<MDNode>(Op)) |
| 400 | return false; |
| 401 | |
| 402 | return isTypeRef(N->getType()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 405 | bool DIExpression::Verify() const { |
| 406 | // FIXME: This should return false if it's null! |
| 407 | auto *N = getRaw(); |
| 408 | return !N || N->isValid(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 411 | bool DILocation::Verify() const { return getRaw(); } |
| 412 | bool DINameSpace::Verify() const { return getRaw(); } |
| 413 | bool DIFile::Verify() const { return getRaw(); } |
| 414 | bool DIEnumerator::Verify() const { return getRaw(); } |
| 415 | bool DISubrange::Verify() const { return getRaw(); } |
| 416 | bool DILexicalBlock::Verify() const { return getRaw(); } |
| 417 | bool DILexicalBlockFile::Verify() const { return getRaw(); } |
| 418 | bool DITemplateTypeParameter::Verify() const { return getRaw(); } |
| 419 | bool DITemplateValueParameter::Verify() const { return getRaw(); } |
| 420 | bool DIImportedEntity::Verify() const { return getRaw(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 421 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 422 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 423 | TypedTrackingMDRef<MDCompositeTypeBase> N(getRaw()); |
| 424 | if (Elements) |
| 425 | N->replaceElements(cast<MDTuple>(Elements)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 426 | if (TParams) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 427 | N->replaceTemplateParams(cast<MDTuple>(TParams)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 428 | DbgNode = N; |
| 429 | } |
| 430 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 431 | DIScopeRef DIScope::getRef() const { |
| 432 | if (!isCompositeType()) |
| 433 | return DIScopeRef(*this); |
| 434 | DICompositeType DTy(DbgNode); |
| 435 | if (!DTy.getIdentifier()) |
| 436 | return DIScopeRef(*this); |
| 437 | return DIScopeRef(DTy.getIdentifier()); |
| 438 | } |
| 439 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 440 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 441 | TypedTrackingMDRef<MDCompositeTypeBase> N(getRaw()); |
| 442 | N->replaceVTableHolder(ContainingType.getRef()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 443 | DbgNode = N; |
| 444 | } |
| 445 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 446 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 447 | assert(CurFn && "Invalid function"); |
| 448 | if (!getContext().isSubprogram()) |
| 449 | return false; |
| 450 | // This variable is not inlined function argument if its scope |
| 451 | // does not describe current function. |
| 452 | return !DISubprogram(getContext()).describes(CurFn); |
| 453 | } |
| 454 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 455 | Function *DISubprogram::getFunction() const { |
| 456 | if (auto *N = getRaw()) |
| 457 | if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction())) |
| 458 | return dyn_cast<Function>(C->getValue()); |
| 459 | return nullptr; |
| 460 | } |
| 461 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 462 | bool DISubprogram::describes(const Function *F) { |
| 463 | assert(F && "Invalid function"); |
| 464 | if (F == getFunction()) |
| 465 | return true; |
| 466 | StringRef Name = getLinkageName(); |
| 467 | if (Name.empty()) |
| 468 | Name = getName(); |
| 469 | if (F->getName() == Name) |
| 470 | return true; |
| 471 | return false; |
| 472 | } |
| 473 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 474 | GlobalVariable *DIGlobalVariable::getGlobal() const { |
| 475 | return dyn_cast_or_null<GlobalVariable>(getConstant()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 478 | DIScopeRef DIScope::getContext() const { |
| 479 | |
| 480 | if (isType()) |
| 481 | return DIType(DbgNode).getContext(); |
| 482 | |
| 483 | if (isSubprogram()) |
| 484 | return DIScopeRef(DISubprogram(DbgNode).getContext()); |
| 485 | |
| 486 | if (isLexicalBlock()) |
| 487 | return DIScopeRef(DILexicalBlock(DbgNode).getContext()); |
| 488 | |
| 489 | if (isLexicalBlockFile()) |
| 490 | return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); |
| 491 | |
| 492 | if (isNameSpace()) |
| 493 | return DIScopeRef(DINameSpace(DbgNode).getContext()); |
| 494 | |
| 495 | assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 496 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 499 | StringRef DIScope::getName() const { |
| 500 | if (isType()) |
| 501 | return DIType(DbgNode).getName(); |
| 502 | if (isSubprogram()) |
| 503 | return DISubprogram(DbgNode).getName(); |
| 504 | if (isNameSpace()) |
| 505 | return DINameSpace(DbgNode).getName(); |
| 506 | assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || |
| 507 | isCompileUnit()) && |
| 508 | "Unhandled type of scope."); |
| 509 | return StringRef(); |
| 510 | } |
| 511 | |
| 512 | StringRef DIScope::getFilename() const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 513 | if (auto *N = getRaw()) |
| 514 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0); |
| 515 | return ""; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | StringRef DIScope::getDirectory() const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 519 | if (auto *N = getRaw()) |
| 520 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1); |
| 521 | return ""; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
| 525 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 526 | getRaw()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get())); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
| 530 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 531 | getRaw()->replaceGlobalVariables( |
| 532 | cast_or_null<MDTuple>(GlobalVariables.get())); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 535 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 536 | DILexicalBlockFile NewScope) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 537 | assert(Verify()); |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 538 | assert(NewScope && "Expected valid scope"); |
| 539 | |
| 540 | const auto *Old = cast<MDLocation>(DbgNode); |
| 541 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 542 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 545 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 546 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 547 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 548 | } |
| 549 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 550 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 551 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 552 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 553 | return cast<MDLocalVariable>(DV) |
| 554 | ->withInline(cast_or_null<MDLocation>(InlinedScope)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 557 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 558 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 559 | return cast<MDLocalVariable>(DV)->withoutInline(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 562 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
| 563 | DIDescriptor D(Scope); |
| 564 | if (D.isSubprogram()) |
| 565 | return DISubprogram(Scope); |
| 566 | |
| 567 | if (D.isLexicalBlockFile()) |
| 568 | return getDISubprogram(DILexicalBlockFile(Scope).getContext()); |
| 569 | |
| 570 | if (D.isLexicalBlock()) |
| 571 | return getDISubprogram(DILexicalBlock(Scope).getContext()); |
| 572 | |
| 573 | return DISubprogram(); |
| 574 | } |
| 575 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 576 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 577 | // 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] | 578 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 579 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
| 580 | return !Inst.getDebugLoc().isUnknown(); |
| 581 | }); |
| 582 | if (Inst == BB.end()) |
| 583 | continue; |
| 584 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 585 | const MDNode *Scope = DLoc.getScopeNode(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 586 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 587 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | return DISubprogram(); |
| 591 | } |
| 592 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 593 | DICompositeType llvm::getDICompositeType(DIType T) { |
| 594 | if (T.isCompositeType()) |
| 595 | return DICompositeType(T); |
| 596 | |
| 597 | if (T.isDerivedType()) { |
| 598 | // This function is currently used by dragonegg and dragonegg does |
| 599 | // not generate identifier for types, so using an empty map to resolve |
| 600 | // DerivedFrom should be fine. |
| 601 | DITypeIdentifierMap EmptyMap; |
| 602 | return getDICompositeType( |
| 603 | DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); |
| 604 | } |
| 605 | |
| 606 | return DICompositeType(); |
| 607 | } |
| 608 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 609 | DITypeIdentifierMap |
| 610 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 611 | DITypeIdentifierMap Map; |
| 612 | 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] | 613 | DICompileUnit CU(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 614 | DIArray Retain = CU.getRetainedTypes(); |
| 615 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
| 616 | if (!Retain.getElement(Ti).isCompositeType()) |
| 617 | continue; |
| 618 | DICompositeType Ty(Retain.getElement(Ti)); |
| 619 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 620 | // Definition has priority over declaration. |
| 621 | // Try to insert (TypeId, Ty) to Map. |
| 622 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 623 | Map.insert(std::make_pair(TypeId, Ty)); |
| 624 | // If TypeId already exists in Map and this is a definition, replace |
| 625 | // whatever we had (declaration or definition) with the definition. |
| 626 | if (!P.second && !Ty.isForwardDecl()) |
| 627 | P.first->second = Ty; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | return Map; |
| 632 | } |
| 633 | |
| 634 | //===----------------------------------------------------------------------===// |
| 635 | // DebugInfoFinder implementations. |
| 636 | //===----------------------------------------------------------------------===// |
| 637 | |
| 638 | void DebugInfoFinder::reset() { |
| 639 | CUs.clear(); |
| 640 | SPs.clear(); |
| 641 | GVs.clear(); |
| 642 | TYs.clear(); |
| 643 | Scopes.clear(); |
| 644 | NodesSeen.clear(); |
| 645 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 646 | TypeMapInitialized = false; |
| 647 | } |
| 648 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 649 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 650 | if (!TypeMapInitialized) |
| 651 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 652 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 653 | TypeMapInitialized = true; |
| 654 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 657 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 658 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 659 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 660 | 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] | 661 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 662 | addCompileUnit(CU); |
| 663 | DIArray GVs = CU.getGlobalVariables(); |
| 664 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
| 665 | DIGlobalVariable DIG(GVs.getElement(i)); |
| 666 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 667 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 668 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | DIArray SPs = CU.getSubprograms(); |
| 672 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
| 673 | processSubprogram(DISubprogram(SPs.getElement(i))); |
| 674 | DIArray EnumTypes = CU.getEnumTypes(); |
| 675 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
| 676 | processType(DIType(EnumTypes.getElement(i))); |
| 677 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 678 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
| 679 | processType(DIType(RetainedTypes.getElement(i))); |
| 680 | DIArray Imports = CU.getImportedEntities(); |
| 681 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
| 682 | DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 683 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 684 | if (Entity.isType()) |
| 685 | processType(DIType(Entity)); |
| 686 | else if (Entity.isSubprogram()) |
| 687 | processSubprogram(DISubprogram(Entity)); |
| 688 | else if (Entity.isNameSpace()) |
| 689 | processScope(DINameSpace(Entity).getContext()); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 695 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 696 | if (!Loc) |
| 697 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 698 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 699 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 700 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 703 | void DebugInfoFinder::processType(DIType DT) { |
| 704 | if (!addType(DT)) |
| 705 | return; |
| 706 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
| 707 | if (DT.isCompositeType()) { |
| 708 | DICompositeType DCT(DT); |
| 709 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 710 | if (DT.isSubroutineType()) { |
| 711 | DITypeArray DTA = DISubroutineType(DT).getTypeArray(); |
| 712 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 713 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 714 | return; |
| 715 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 716 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 717 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 718 | DIDescriptor D = DA.getElement(i); |
| 719 | if (D.isType()) |
| 720 | processType(DIType(D)); |
| 721 | else if (D.isSubprogram()) |
| 722 | processSubprogram(DISubprogram(D)); |
| 723 | } |
| 724 | } else if (DT.isDerivedType()) { |
| 725 | DIDerivedType DDT(DT); |
| 726 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | void DebugInfoFinder::processScope(DIScope Scope) { |
| 731 | if (Scope.isType()) { |
| 732 | DIType Ty(Scope); |
| 733 | processType(Ty); |
| 734 | return; |
| 735 | } |
| 736 | if (Scope.isCompileUnit()) { |
| 737 | addCompileUnit(DICompileUnit(Scope)); |
| 738 | return; |
| 739 | } |
| 740 | if (Scope.isSubprogram()) { |
| 741 | processSubprogram(DISubprogram(Scope)); |
| 742 | return; |
| 743 | } |
| 744 | if (!addScope(Scope)) |
| 745 | return; |
| 746 | if (Scope.isLexicalBlock()) { |
| 747 | DILexicalBlock LB(Scope); |
| 748 | processScope(LB.getContext()); |
| 749 | } else if (Scope.isLexicalBlockFile()) { |
| 750 | DILexicalBlockFile LBF = DILexicalBlockFile(Scope); |
| 751 | processScope(LBF.getScope()); |
| 752 | } else if (Scope.isNameSpace()) { |
| 753 | DINameSpace NS(Scope); |
| 754 | processScope(NS.getContext()); |
| 755 | } |
| 756 | } |
| 757 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 758 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 759 | if (!addSubprogram(SP)) |
| 760 | return; |
| 761 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 762 | processType(SP.getType()); |
| 763 | DIArray TParams = SP.getTemplateParams(); |
| 764 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 765 | DIDescriptor Element = TParams.getElement(I); |
| 766 | if (Element.isTemplateTypeParameter()) { |
| 767 | DITemplateTypeParameter TType(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 768 | processType(TType.getType().resolve(TypeIdentifierMap)); |
| 769 | } else if (Element.isTemplateValueParameter()) { |
| 770 | DITemplateValueParameter TVal(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 771 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 776 | void DebugInfoFinder::processDeclare(const Module &M, |
| 777 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 778 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 779 | if (!N) |
| 780 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 781 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 782 | |
| 783 | DIDescriptor DV(N); |
| 784 | if (!DV.isVariable()) |
| 785 | return; |
| 786 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 787 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 788 | return; |
| 789 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 790 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 793 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 794 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 795 | if (!N) |
| 796 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 797 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 798 | |
| 799 | DIDescriptor DV(N); |
| 800 | if (!DV.isVariable()) |
| 801 | return; |
| 802 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 803 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 804 | return; |
| 805 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 806 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 809 | bool DebugInfoFinder::addType(DIType DT) { |
| 810 | if (!DT) |
| 811 | return false; |
| 812 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 813 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 814 | return false; |
| 815 | |
| 816 | TYs.push_back(DT); |
| 817 | return true; |
| 818 | } |
| 819 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 820 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 821 | if (!CU) |
| 822 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 823 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 824 | return false; |
| 825 | |
| 826 | CUs.push_back(CU); |
| 827 | return true; |
| 828 | } |
| 829 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 830 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 831 | if (!DIG) |
| 832 | return false; |
| 833 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 834 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 835 | return false; |
| 836 | |
| 837 | GVs.push_back(DIG); |
| 838 | return true; |
| 839 | } |
| 840 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 841 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 842 | if (!SP) |
| 843 | return false; |
| 844 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 845 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 846 | return false; |
| 847 | |
| 848 | SPs.push_back(SP); |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 853 | if (!Scope) |
| 854 | return false; |
| 855 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 856 | // as null for now. |
| 857 | if (Scope->getNumOperands() == 0) |
| 858 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 859 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 860 | return false; |
| 861 | Scopes.push_back(Scope); |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | //===----------------------------------------------------------------------===// |
| 866 | // DIDescriptor: dump routines for all descriptors. |
| 867 | //===----------------------------------------------------------------------===// |
| 868 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 869 | void DIDescriptor::dump() const { |
| 870 | print(dbgs()); |
| 871 | dbgs() << '\n'; |
| 872 | } |
| 873 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 874 | void DIDescriptor::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 875 | if (!get()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 876 | return; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 877 | get()->print(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 881 | const LLVMContext &Ctx) { |
| 882 | if (!DL.isUnknown()) { // Print source line info. |
| 883 | DIScope Scope(DL.getScope(Ctx)); |
| 884 | assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); |
| 885 | // Omit the directory, because it's likely to be long and uninteresting. |
| 886 | CommentOS << Scope.getFilename(); |
| 887 | CommentOS << ':' << DL.getLine(); |
| 888 | if (DL.getCol() != 0) |
| 889 | CommentOS << ':' << DL.getCol(); |
| 890 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); |
| 891 | if (!InlinedAtDL.isUnknown()) { |
| 892 | CommentOS << " @[ "; |
| 893 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 894 | CommentOS << " ]"; |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 900 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 901 | StringRef Res = getName(); |
| 902 | if (!Res.empty()) |
| 903 | OS << Res << "," << getLineNumber(); |
| 904 | if (MDNode *InlinedAt = getInlinedAt()) { |
| 905 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); |
| 906 | if (!InlinedAtDL.isUnknown()) { |
| 907 | OS << " @["; |
| 908 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 909 | OS << "]"; |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 914 | template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) { |
| 915 | assert(isDescriptorRef(V) && |
| 916 | "DIDescriptorRef should be a MDString or MDNode"); |
| 917 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 918 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 919 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 920 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 921 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 922 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 923 | } |
| 924 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 925 | template <> |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 926 | DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const { |
| 927 | return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
| 928 | } |
| 929 | template <> |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 930 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 931 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 932 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 933 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 934 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 935 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 936 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 937 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 938 | bool Changed = false; |
| 939 | |
| 940 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 941 | // the module. |
| 942 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 943 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 944 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 945 | CI->eraseFromParent(); |
| 946 | } |
| 947 | Declare->eraseFromParent(); |
| 948 | Changed = true; |
| 949 | } |
| 950 | |
| 951 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 952 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 953 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 954 | CI->eraseFromParent(); |
| 955 | } |
| 956 | DbgVal->eraseFromParent(); |
| 957 | Changed = true; |
| 958 | } |
| 959 | |
| 960 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 961 | NME = M.named_metadata_end(); NMI != NME;) { |
| 962 | NamedMDNode *NMD = NMI; |
| 963 | ++NMI; |
| 964 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 965 | NMD->eraseFromParent(); |
| 966 | Changed = true; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 971 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; |
| 972 | ++FI) |
| 973 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; |
| 974 | ++BI) { |
| 975 | if (!BI->getDebugLoc().isUnknown()) { |
| 976 | Changed = true; |
| 977 | BI->setDebugLoc(DebugLoc()); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | return Changed; |
| 982 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 983 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 984 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 985 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 986 | M.getModuleFlag("Debug Info Version"))) |
| 987 | return Val->getZExtValue(); |
| 988 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 989 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 990 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 991 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 992 | llvm::makeSubprogramMap(const Module &M) { |
| 993 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 994 | |
| 995 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 996 | if (!CU_Nodes) |
| 997 | return R; |
| 998 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 999 | for (MDNode *N : CU_Nodes->operands()) { |
| 1000 | DICompileUnit CUNode(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 1001 | DIArray SPs = CUNode.getSubprograms(); |
| 1002 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 1003 | DISubprogram SP(SPs.getElement(i)); |
| 1004 | if (Function *F = SP.getFunction()) |
| 1005 | R.insert(std::make_pair(F, SP)); |
| 1006 | } |
| 1007 | } |
| 1008 | return R; |
| 1009 | } |