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