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" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 28 | #include "llvm/IR/GVMaterializer.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | using namespace llvm; |
| 35 | using namespace llvm::dwarf; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // DIDescriptor |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Duncan P. N. Exon Smith | c22a5c2 | 2015-02-21 00:43:09 +0000 | [diff] [blame] | 41 | unsigned DIDescriptor::getFlag(StringRef Flag) { |
| 42 | return StringSwitch<unsigned>(Flag) |
| 43 | #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) |
| 44 | #include "llvm/IR/DebugInfoFlags.def" |
| 45 | .Default(0); |
| 46 | } |
| 47 | |
| 48 | const char *DIDescriptor::getFlagString(unsigned Flag) { |
| 49 | switch (Flag) { |
| 50 | default: |
| 51 | return ""; |
| 52 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 53 | case Flag##NAME: \ |
| 54 | return "DIFlag" #NAME; |
| 55 | #include "llvm/IR/DebugInfoFlags.def" |
| 56 | } |
| 57 | } |
| 58 | |
Duncan P. N. Exon Smith | 269e38d | 2015-02-21 00:45:26 +0000 | [diff] [blame] | 59 | unsigned DIDescriptor::splitFlags(unsigned Flags, |
| 60 | SmallVectorImpl<unsigned> &SplitFlags) { |
| 61 | // Accessibility flags need to be specially handled, since they're packed |
| 62 | // together. |
| 63 | if (unsigned A = Flags & FlagAccessibility) { |
| 64 | if (A == FlagPrivate) |
| 65 | SplitFlags.push_back(FlagPrivate); |
| 66 | else if (A == FlagProtected) |
| 67 | SplitFlags.push_back(FlagProtected); |
| 68 | else |
| 69 | SplitFlags.push_back(FlagPublic); |
| 70 | Flags &= ~A; |
| 71 | } |
| 72 | |
| 73 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 74 | if (unsigned Bit = Flags & ID) { \ |
| 75 | SplitFlags.push_back(Bit); \ |
| 76 | Flags &= ~Bit; \ |
| 77 | } |
| 78 | #include "llvm/IR/DebugInfoFlags.def" |
| 79 | |
| 80 | return Flags; |
| 81 | } |
| 82 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 83 | static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 84 | if (!DbgNode || Elt >= DbgNode->getNumOperands()) |
| 85 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 86 | return DbgNode->getOperand(Elt); |
| 87 | } |
| 88 | |
| 89 | static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { |
| 90 | return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); |
| 91 | } |
| 92 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 93 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 94 | MDNode *Field = getNodeField(DbgNode, Elt); |
| 95 | return DIDescriptor(Field); |
| 96 | } |
| 97 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 98 | /// \brief Return the size reported by the variable's type. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 99 | unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) { |
| 100 | DIType Ty = getType().resolve(Map); |
| 101 | // Follow derived types until we reach a type that |
| 102 | // reports back a size. |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 103 | while (isa<MDDerivedType>(Ty) && !Ty.getSizeInBits()) { |
| 104 | DIDerivedType DT = cast<MDDerivedType>(Ty); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 105 | Ty = DT.getTypeDerivedFrom().resolve(Map); |
| 106 | } |
| 107 | assert(Ty.getSizeInBits() && "type with size 0"); |
| 108 | return Ty.getSizeInBits(); |
| 109 | } |
| 110 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 111 | bool DIExpression::isBitPiece() const { |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 112 | unsigned N = getNumElements(); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 113 | return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 116 | uint64_t DIExpression::getBitPieceOffset() const { |
| 117 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 118 | return getElement(getNumElements()-2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 121 | uint64_t DIExpression::getBitPieceSize() const { |
| 122 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 123 | return getElement(getNumElements()-1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 124 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 125 | |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 126 | DIExpression::iterator DIExpression::Operand::getNext() const { |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 127 | iterator it(I); |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 128 | return ++it; |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 131 | //===----------------------------------------------------------------------===// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 132 | // Simple Descriptor Constructors and other Methods |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 135 | void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 136 | assert(DbgNode && "Trying to replace an unverified type!"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 137 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 138 | TempMDNode Temp(get()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 139 | |
| 140 | // Since we use a TrackingVH for the node, its easy for clients to manufacture |
| 141 | // legitimate situations where they want to replaceAllUsesWith() on something |
| 142 | // which, due to uniquing, has merged with the source. We shield clients from |
| 143 | // this detail by allowing a value to be replaced with replaceAllUsesWith() |
| 144 | // itself. |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 145 | if (Temp.get() == D.get()) { |
| 146 | DbgNode = MDNode::replaceWithUniqued(std::move(Temp)); |
| 147 | return; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 148 | } |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 149 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 150 | Temp->replaceAllUsesWith(D.get()); |
| 151 | DbgNode = D.get(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 154 | void DIDescriptor::replaceAllUsesWith(MDNode *D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 155 | assert(DbgNode && "Trying to replace an unverified type!"); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 156 | assert(DbgNode != D && "This replacement should always happen"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 157 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 158 | TempMDNode Node(get()); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 159 | Node->replaceAllUsesWith(D); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Duncan P. N. Exon Smith | 3b960c9 | 2015-03-31 01:47:55 +0000 | [diff] [blame] | 162 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 163 | /// \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] | 164 | static bool isTypeRef(const Metadata *MD) { |
| 165 | if (!MD) |
| 166 | return true; |
| 167 | if (auto *S = dyn_cast<MDString>(MD)) |
| 168 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 169 | return isa<MDType>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 172 | /// \brief Check if a value can be a ScopeRef. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 173 | static bool isScopeRef(const Metadata *MD) { |
| 174 | if (!MD) |
| 175 | return true; |
| 176 | if (auto *S = dyn_cast<MDString>(MD)) |
| 177 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 178 | return isa<MDScope>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 181 | /// \brief Check if a value can be a DescriptorRef. |
| 182 | static bool isDescriptorRef(const Metadata *MD) { |
| 183 | if (!MD) |
| 184 | return true; |
| 185 | if (auto *S = dyn_cast<MDString>(MD)) |
| 186 | return !S->getString().empty(); |
| 187 | return isa<MDNode>(MD); |
| 188 | } |
Duncan P. N. Exon Smith | e445014 | 2015-02-18 19:56:50 +0000 | [diff] [blame] | 189 | #endif |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 190 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 191 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 192 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 193 | if (Elements) |
| 194 | N->replaceElements(cast<MDTuple>(Elements)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 195 | if (TParams) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 196 | N->replaceTemplateParams(cast<MDTuple>(TParams)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 197 | DbgNode = N; |
| 198 | } |
| 199 | |
Duncan P. N. Exon Smith | 930f388 | 2015-04-06 18:02:43 +0000 | [diff] [blame] | 200 | DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 201 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 202 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 203 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | 3ec5fa6 | 2015-04-06 19:03:45 +0000 | [diff] [blame] | 204 | N->replaceVTableHolder(MDTypeRef::get(ContainingType)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 205 | DbgNode = N; |
| 206 | } |
| 207 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 208 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 209 | assert(CurFn && "Invalid function"); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 210 | DISubprogram SP = dyn_cast<MDSubprogram>(getContext()); |
| 211 | if (!SP) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 212 | return false; |
| 213 | // This variable is not inlined function argument if its scope |
| 214 | // does not describe current function. |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 215 | return !SP.describes(CurFn); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 218 | Function *DISubprogram::getFunction() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 219 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 220 | if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction())) |
| 221 | return dyn_cast<Function>(C->getValue()); |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 225 | bool DISubprogram::describes(const Function *F) { |
| 226 | assert(F && "Invalid function"); |
| 227 | if (F == getFunction()) |
| 228 | return true; |
| 229 | StringRef Name = getLinkageName(); |
| 230 | if (Name.empty()) |
| 231 | Name = getName(); |
| 232 | if (F->getName() == Name) |
| 233 | return true; |
| 234 | return false; |
| 235 | } |
| 236 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 237 | GlobalVariable *DIGlobalVariable::getGlobal() const { |
| 238 | return dyn_cast_or_null<GlobalVariable>(getConstant()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 241 | DIScopeRef DIScope::getContext() const { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 242 | if (DIType T = dyn_cast<MDType>(*this)) |
| 243 | return T.getContext(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 244 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 245 | if (DISubprogram SP = dyn_cast<MDSubprogram>(*this)) |
| 246 | return DIScopeRef(SP.getContext()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 247 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 248 | if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(*this)) |
| 249 | return DIScopeRef(LB.getContext()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 251 | if (DINameSpace NS = dyn_cast<MDNamespace>(*this)) |
| 252 | return DIScopeRef(NS.getContext()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 253 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 254 | assert((isa<MDFile>(*this) || isa<MDCompileUnit>(*this)) && |
| 255 | "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 256 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 259 | StringRef DIScope::getName() const { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 260 | if (DIType T = dyn_cast<MDType>(*this)) |
| 261 | return T.getName(); |
| 262 | if (DISubprogram SP = dyn_cast<MDSubprogram>(*this)) |
| 263 | return SP.getName(); |
| 264 | if (DINameSpace NS = dyn_cast<MDNamespace>(*this)) |
| 265 | return NS.getName(); |
| 266 | assert((isa<MDLexicalBlockBase>(*this) || isa<MDFile>(*this) || |
| 267 | isa<MDCompileUnit>(*this)) && |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 268 | "Unhandled type of scope."); |
| 269 | return StringRef(); |
| 270 | } |
| 271 | |
| 272 | StringRef DIScope::getFilename() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 273 | if (auto *N = get()) |
Duncan P. N. Exon Smith | 897030c | 2015-04-06 16:43:40 +0000 | [diff] [blame] | 274 | if (auto *F = N->getFile()) |
| 275 | return F->getFilename(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 276 | return ""; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | StringRef DIScope::getDirectory() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 280 | if (auto *N = get()) |
Duncan P. N. Exon Smith | 897030c | 2015-04-06 16:43:40 +0000 | [diff] [blame] | 281 | if (auto *F = N->getFile()) |
| 282 | return F->getDirectory(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 283 | return ""; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 287 | get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get())); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 291 | get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get())); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 294 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 295 | DILexicalBlockFile NewScope) { |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 296 | assert(NewScope && "Expected valid scope"); |
| 297 | |
| 298 | const auto *Old = cast<MDLocation>(DbgNode); |
| 299 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 300 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 303 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 304 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 305 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 306 | } |
| 307 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 308 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 309 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 310 | return cast<MDLocalVariable>(DV) |
| 311 | ->withInline(cast_or_null<MDLocation>(InlinedScope)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 314 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 315 | return cast<MDLocalVariable>(DV)->withoutInline(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 318 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 319 | if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope)) |
| 320 | return LocalScope->getSubprogram(); |
| 321 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 324 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 325 | // 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] | 326 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 327 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 328 | return Inst.getDebugLoc(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 329 | }); |
| 330 | if (Inst == BB.end()) |
| 331 | continue; |
| 332 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 333 | const MDNode *Scope = DLoc.getInlinedAtScope(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 334 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 335 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return DISubprogram(); |
| 339 | } |
| 340 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 341 | DICompositeType llvm::getDICompositeType(DIType T) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 342 | if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T)) |
| 343 | return C; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 344 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 345 | if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 346 | // This function is currently used by dragonegg and dragonegg does |
| 347 | // not generate identifier for types, so using an empty map to resolve |
| 348 | // DerivedFrom should be fine. |
| 349 | DITypeIdentifierMap EmptyMap; |
| 350 | return getDICompositeType( |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 351 | DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 354 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 357 | DITypeIdentifierMap |
| 358 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 359 | DITypeIdentifierMap Map; |
| 360 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 361 | DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 362 | DIArray Retain = CU.getRetainedTypes(); |
| 363 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 364 | if (!isa<MDCompositeType>(Retain.getElement(Ti))) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 365 | continue; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 366 | DICompositeType Ty = cast<MDCompositeType>(Retain.getElement(Ti)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 367 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 368 | // Definition has priority over declaration. |
| 369 | // Try to insert (TypeId, Ty) to Map. |
| 370 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 371 | Map.insert(std::make_pair(TypeId, Ty)); |
| 372 | // If TypeId already exists in Map and this is a definition, replace |
| 373 | // whatever we had (declaration or definition) with the definition. |
| 374 | if (!P.second && !Ty.isForwardDecl()) |
| 375 | P.first->second = Ty; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | return Map; |
| 380 | } |
| 381 | |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | // DebugInfoFinder implementations. |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
| 386 | void DebugInfoFinder::reset() { |
| 387 | CUs.clear(); |
| 388 | SPs.clear(); |
| 389 | GVs.clear(); |
| 390 | TYs.clear(); |
| 391 | Scopes.clear(); |
| 392 | NodesSeen.clear(); |
| 393 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 394 | TypeMapInitialized = false; |
| 395 | } |
| 396 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 397 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 398 | if (!TypeMapInitialized) |
| 399 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 400 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 401 | TypeMapInitialized = true; |
| 402 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 405 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 406 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 407 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 408 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 409 | DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 410 | addCompileUnit(CU); |
| 411 | DIArray GVs = CU.getGlobalVariables(); |
| 412 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 413 | DIGlobalVariable DIG = cast<MDGlobalVariable>(GVs.getElement(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 414 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 415 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 416 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | DIArray SPs = CU.getSubprograms(); |
| 420 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 421 | processSubprogram(cast<MDSubprogram>(SPs.getElement(i))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 422 | DIArray EnumTypes = CU.getEnumTypes(); |
| 423 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 424 | processType(cast<MDType>(EnumTypes.getElement(i))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 425 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 426 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 427 | processType(cast<MDType>(RetainedTypes.getElement(i))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 428 | DIArray Imports = CU.getImportedEntities(); |
| 429 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 430 | DIImportedEntity Import = cast<MDImportedEntity>(Imports.getElement(i)); |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 431 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 432 | if (auto *T = dyn_cast<MDType>(Entity)) |
| 433 | processType(T); |
| 434 | else if (auto *SP = dyn_cast<MDSubprogram>(Entity)) |
| 435 | processSubprogram(SP); |
| 436 | else if (auto *NS = dyn_cast<MDNamespace>(Entity)) |
| 437 | processScope(NS->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 443 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 444 | if (!Loc) |
| 445 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 446 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 447 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 448 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 451 | void DebugInfoFinder::processType(DIType DT) { |
| 452 | if (!addType(DT)) |
| 453 | return; |
| 454 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 455 | if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 456 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 457 | if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) { |
| 458 | DITypeArray DTA = ST.getTypeArray(); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 459 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 460 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 461 | return; |
| 462 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 463 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 464 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 465 | DIDescriptor D = DA.getElement(i); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 466 | if (DIType T = dyn_cast<MDType>(D)) |
| 467 | processType(T); |
| 468 | else if (DISubprogram SP = dyn_cast<MDSubprogram>(D)) |
| 469 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 470 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 471 | } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 472 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void DebugInfoFinder::processScope(DIScope Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 477 | if (!Scope) |
| 478 | return; |
| 479 | if (DIType Ty = dyn_cast<MDType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 480 | processType(Ty); |
| 481 | return; |
| 482 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 483 | if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) { |
| 484 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 485 | return; |
| 486 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 487 | if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) { |
| 488 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | if (!addScope(Scope)) |
| 492 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 493 | if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 494 | processScope(LB.getContext()); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 495 | } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 496 | processScope(NS.getContext()); |
| 497 | } |
| 498 | } |
| 499 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 500 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 501 | if (!addSubprogram(SP)) |
| 502 | return; |
| 503 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 504 | processType(SP.getType()); |
| 505 | DIArray TParams = SP.getTemplateParams(); |
| 506 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 507 | DIDescriptor Element = TParams.getElement(I); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 508 | if (DITemplateTypeParameter TType = |
| 509 | dyn_cast<MDTemplateTypeParameter>(Element)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 510 | processType(TType.getType().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 511 | } else if (DITemplateValueParameter TVal = |
| 512 | dyn_cast<MDTemplateValueParameter>(Element)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 513 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 518 | void DebugInfoFinder::processDeclare(const Module &M, |
| 519 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 520 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 521 | if (!N) |
| 522 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 523 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 524 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 525 | DIVariable DV = dyn_cast<MDLocalVariable>(N); |
| 526 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 527 | return; |
| 528 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 529 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 530 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 531 | processScope(DV.getContext()); |
| 532 | processType(DV.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 535 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 536 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 537 | if (!N) |
| 538 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 539 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 540 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 541 | DIVariable DV = dyn_cast<MDLocalVariable>(N); |
| 542 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 543 | return; |
| 544 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 545 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 546 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 547 | processScope(DV.getContext()); |
| 548 | processType(DV.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 551 | bool DebugInfoFinder::addType(DIType DT) { |
| 552 | if (!DT) |
| 553 | return false; |
| 554 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 555 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 556 | return false; |
| 557 | |
| 558 | TYs.push_back(DT); |
| 559 | return true; |
| 560 | } |
| 561 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 562 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 563 | if (!CU) |
| 564 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 565 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 566 | return false; |
| 567 | |
| 568 | CUs.push_back(CU); |
| 569 | return true; |
| 570 | } |
| 571 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 572 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 573 | if (!DIG) |
| 574 | return false; |
| 575 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 576 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 577 | return false; |
| 578 | |
| 579 | GVs.push_back(DIG); |
| 580 | return true; |
| 581 | } |
| 582 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 583 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 584 | if (!SP) |
| 585 | return false; |
| 586 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 587 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 588 | return false; |
| 589 | |
| 590 | SPs.push_back(SP); |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 595 | if (!Scope) |
| 596 | return false; |
| 597 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 598 | // as null for now. |
| 599 | if (Scope->getNumOperands() == 0) |
| 600 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 601 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 602 | return false; |
| 603 | Scopes.push_back(Scope); |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | //===----------------------------------------------------------------------===// |
| 608 | // DIDescriptor: dump routines for all descriptors. |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 611 | void DIDescriptor::dump() const { |
| 612 | print(dbgs()); |
| 613 | dbgs() << '\n'; |
| 614 | } |
| 615 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 616 | void DIDescriptor::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 617 | if (!get()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 618 | return; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 619 | get()->print(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 623 | const LLVMContext &Ctx) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 624 | if (!DL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 625 | return; |
| 626 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 627 | DIScope Scope = cast<MDScope>(DL.getScope()); |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 628 | // Omit the directory, because it's likely to be long and uninteresting. |
| 629 | CommentOS << Scope.getFilename(); |
| 630 | CommentOS << ':' << DL.getLine(); |
| 631 | if (DL.getCol() != 0) |
| 632 | CommentOS << ':' << DL.getCol(); |
| 633 | |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 634 | DebugLoc InlinedAtDL = DL.getInlinedAt(); |
| 635 | if (!InlinedAtDL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 636 | return; |
| 637 | |
| 638 | CommentOS << " @[ "; |
| 639 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 640 | CommentOS << " ]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 644 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 645 | StringRef Res = getName(); |
| 646 | if (!Res.empty()) |
| 647 | OS << Res << "," << getLineNumber(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 648 | if (auto *InlinedAt = get()->getInlinedAt()) { |
| 649 | if (DebugLoc InlinedAtDL = InlinedAt) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 650 | OS << " @["; |
| 651 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 652 | OS << "]"; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 657 | template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) { |
| 658 | assert(isDescriptorRef(V) && |
| 659 | "DIDescriptorRef should be a MDString or MDNode"); |
| 660 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 661 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 662 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 663 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 664 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 665 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 666 | } |
| 667 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 668 | template <> |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 669 | DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const { |
| 670 | return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
| 671 | } |
| 672 | template <> |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 673 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 674 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 675 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 676 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 677 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 678 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 679 | |
Duncan P. N. Exon Smith | 5bf3cdc | 2015-04-06 22:27:37 +0000 | [diff] [blame] | 680 | template <> |
| 681 | DIDescriptor |
| 682 | DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const { |
| 683 | return DIDescriptor(DebugNodeRef(Val).resolve(Map)); |
| 684 | } |
| 685 | template <> |
| 686 | DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const { |
| 687 | return MDScopeRef(Val).resolve(Map); |
| 688 | } |
| 689 | template <> |
| 690 | DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const { |
| 691 | return MDTypeRef(Val).resolve(Map); |
| 692 | } |
| 693 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 694 | bool llvm::stripDebugInfo(Function &F) { |
| 695 | bool Changed = false; |
| 696 | for (BasicBlock &BB : F) { |
| 697 | for (Instruction &I : BB) { |
| 698 | if (I.getDebugLoc()) { |
| 699 | Changed = true; |
| 700 | I.setDebugLoc(DebugLoc()); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | return Changed; |
| 705 | } |
| 706 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 707 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 708 | bool Changed = false; |
| 709 | |
| 710 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 711 | // the module. |
| 712 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 713 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 714 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 715 | CI->eraseFromParent(); |
| 716 | } |
| 717 | Declare->eraseFromParent(); |
| 718 | Changed = true; |
| 719 | } |
| 720 | |
| 721 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 722 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 723 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 724 | CI->eraseFromParent(); |
| 725 | } |
| 726 | DbgVal->eraseFromParent(); |
| 727 | Changed = true; |
| 728 | } |
| 729 | |
| 730 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 731 | NME = M.named_metadata_end(); NMI != NME;) { |
| 732 | NamedMDNode *NMD = NMI; |
| 733 | ++NMI; |
| 734 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 735 | NMD->eraseFromParent(); |
| 736 | Changed = true; |
| 737 | } |
| 738 | } |
| 739 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 740 | for (Function &F : M) |
| 741 | Changed |= stripDebugInfo(F); |
| 742 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 743 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 744 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 745 | |
| 746 | return Changed; |
| 747 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 748 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 749 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 750 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 751 | M.getModuleFlag("Debug Info Version"))) |
| 752 | return Val->getZExtValue(); |
| 753 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 754 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 755 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 756 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 757 | llvm::makeSubprogramMap(const Module &M) { |
| 758 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 759 | |
| 760 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 761 | if (!CU_Nodes) |
| 762 | return R; |
| 763 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 764 | for (MDNode *N : CU_Nodes->operands()) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 765 | DICompileUnit CUNode = cast<MDCompileUnit>(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 766 | DIArray SPs = CUNode.getSubprograms(); |
| 767 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame^] | 768 | DISubprogram SP = cast<MDSubprogram>(SPs.getElement(i)); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 769 | if (Function *F = SP.getFunction()) |
| 770 | R.insert(std::make_pair(F, SP)); |
| 771 | } |
| 772 | } |
| 773 | return R; |
| 774 | } |