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 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 37 | // Simple Descriptor Constructors and other Methods |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Duncan P. N. Exon Smith | 930f388 | 2015-04-06 18:02:43 +0000 | [diff] [blame] | 40 | DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 41 | |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 42 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
Duncan P. N. Exon Smith | 1134473 | 2015-04-07 16:50:39 +0000 | [diff] [blame] | 43 | get()->replaceSubprograms(MDSubprogramArray(Subprograms)); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
Duncan P. N. Exon Smith | 1134473 | 2015-04-07 16:50:39 +0000 | [diff] [blame] | 47 | get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 50 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 51 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 52 | return cast<MDLocalVariable>(DV) |
| 53 | ->withInline(cast_or_null<MDLocation>(InlinedScope)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 56 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 57 | return cast<MDLocalVariable>(DV)->withoutInline(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 60 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 61 | if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope)) |
| 62 | return LocalScope->getSubprogram(); |
| 63 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 66 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 67 | // 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] | 68 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 69 | 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] | 70 | return Inst.getDebugLoc(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 71 | }); |
| 72 | if (Inst == BB.end()) |
| 73 | continue; |
| 74 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 75 | const MDNode *Scope = DLoc.getInlinedAtScope(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 76 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 77 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return DISubprogram(); |
| 81 | } |
| 82 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 83 | DICompositeType llvm::getDICompositeType(DIType T) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 84 | if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T)) |
| 85 | return C; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 86 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 87 | if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 88 | // This function is currently used by dragonegg and dragonegg does |
| 89 | // not generate identifier for types, so using an empty map to resolve |
| 90 | // DerivedFrom should be fine. |
| 91 | DITypeIdentifierMap EmptyMap; |
| 92 | return getDICompositeType( |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 93 | DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 96 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 99 | DITypeIdentifierMap |
| 100 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 101 | DITypeIdentifierMap Map; |
| 102 | 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] | 103 | DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 104 | DIArray Retain = CU.getRetainedTypes(); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 105 | for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) { |
| 106 | if (!isa<MDCompositeType>(Retain[Ti])) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 107 | continue; |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 108 | DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 109 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 110 | // Definition has priority over declaration. |
| 111 | // Try to insert (TypeId, Ty) to Map. |
| 112 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 113 | Map.insert(std::make_pair(TypeId, Ty)); |
| 114 | // If TypeId already exists in Map and this is a definition, replace |
| 115 | // whatever we had (declaration or definition) with the definition. |
| 116 | if (!P.second && !Ty.isForwardDecl()) |
| 117 | P.first->second = Ty; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return Map; |
| 122 | } |
| 123 | |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | // DebugInfoFinder implementations. |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | |
| 128 | void DebugInfoFinder::reset() { |
| 129 | CUs.clear(); |
| 130 | SPs.clear(); |
| 131 | GVs.clear(); |
| 132 | TYs.clear(); |
| 133 | Scopes.clear(); |
| 134 | NodesSeen.clear(); |
| 135 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 136 | TypeMapInitialized = false; |
| 137 | } |
| 138 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 139 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 140 | if (!TypeMapInitialized) |
| 141 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 142 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 143 | TypeMapInitialized = true; |
| 144 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 147 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 148 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 149 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 150 | 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] | 151 | DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 152 | addCompileUnit(CU); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 153 | for (DIGlobalVariable DIG : CU->getGlobalVariables()) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 154 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 155 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 156 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 159 | for (auto *SP : CU->getSubprograms()) |
| 160 | processSubprogram(SP); |
| 161 | for (auto *ET : CU->getEnumTypes()) |
| 162 | processType(ET); |
| 163 | for (auto *RT : CU->getRetainedTypes()) |
| 164 | processType(RT); |
| 165 | for (DIImportedEntity Import : CU->getImportedEntities()) { |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame^] | 166 | auto *Entity = Import->getEntity().resolve(TypeIdentifierMap); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 167 | if (auto *T = dyn_cast<MDType>(Entity)) |
| 168 | processType(T); |
| 169 | else if (auto *SP = dyn_cast<MDSubprogram>(Entity)) |
| 170 | processSubprogram(SP); |
| 171 | else if (auto *NS = dyn_cast<MDNamespace>(Entity)) |
| 172 | processScope(NS->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 178 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 179 | if (!Loc) |
| 180 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 181 | InitializeTypeMap(M); |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 182 | processScope(Loc->getScope()); |
| 183 | processLocation(M, Loc->getInlinedAt()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 186 | void DebugInfoFinder::processType(DIType DT) { |
| 187 | if (!addType(DT)) |
| 188 | return; |
| 189 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 190 | if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 191 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 192 | if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) { |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 193 | for (MDTypeRef Ref : ST->getTypeArray()) |
| 194 | processType(Ref.resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 197 | for (Metadata *D : DCT->getElements()->operands()) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 198 | if (DIType T = dyn_cast<MDType>(D)) |
| 199 | processType(T); |
| 200 | else if (DISubprogram SP = dyn_cast<MDSubprogram>(D)) |
| 201 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 202 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 203 | } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 204 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void DebugInfoFinder::processScope(DIScope Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 209 | if (!Scope) |
| 210 | return; |
| 211 | if (DIType Ty = dyn_cast<MDType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 212 | processType(Ty); |
| 213 | return; |
| 214 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 215 | if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) { |
| 216 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 217 | return; |
| 218 | } |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 219 | if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) { |
| 220 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | if (!addScope(Scope)) |
| 224 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 225 | if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 226 | processScope(LB.getContext()); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 227 | } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 228 | processScope(NS.getContext()); |
| 229 | } |
| 230 | } |
| 231 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 232 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 233 | if (!addSubprogram(SP)) |
| 234 | return; |
| 235 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 236 | processType(SP.getType()); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 237 | for (auto *Element : SP.getTemplateParams()) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 238 | if (DITemplateTypeParameter TType = |
| 239 | dyn_cast<MDTemplateTypeParameter>(Element)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 240 | processType(TType.getType().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 241 | } else if (DITemplateValueParameter TVal = |
| 242 | dyn_cast<MDTemplateValueParameter>(Element)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 243 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 248 | void DebugInfoFinder::processDeclare(const Module &M, |
| 249 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 251 | if (!N) |
| 252 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 253 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 254 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 255 | DIVariable DV = dyn_cast<MDLocalVariable>(N); |
| 256 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 257 | return; |
| 258 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 259 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 260 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 261 | processScope(DV.getContext()); |
| 262 | processType(DV.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 265 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 266 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 267 | if (!N) |
| 268 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 269 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 270 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 271 | DIVariable DV = dyn_cast<MDLocalVariable>(N); |
| 272 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 273 | return; |
| 274 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 275 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 276 | return; |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 277 | processScope(DV.getContext()); |
| 278 | processType(DV.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 281 | bool DebugInfoFinder::addType(DIType DT) { |
| 282 | if (!DT) |
| 283 | return false; |
| 284 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 285 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 286 | return false; |
| 287 | |
| 288 | TYs.push_back(DT); |
| 289 | return true; |
| 290 | } |
| 291 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 292 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 293 | if (!CU) |
| 294 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 295 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 296 | return false; |
| 297 | |
| 298 | CUs.push_back(CU); |
| 299 | return true; |
| 300 | } |
| 301 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 302 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 303 | if (!DIG) |
| 304 | return false; |
| 305 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 306 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 307 | return false; |
| 308 | |
| 309 | GVs.push_back(DIG); |
| 310 | return true; |
| 311 | } |
| 312 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 313 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 314 | if (!SP) |
| 315 | return false; |
| 316 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 317 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 318 | return false; |
| 319 | |
| 320 | SPs.push_back(SP); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 325 | if (!Scope) |
| 326 | return false; |
| 327 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 328 | // as null for now. |
| 329 | if (Scope->getNumOperands() == 0) |
| 330 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 331 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 332 | return false; |
| 333 | Scopes.push_back(Scope); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | //===----------------------------------------------------------------------===// |
| 338 | // DIDescriptor: dump routines for all descriptors. |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 341 | void DIDescriptor::dump() const { |
| 342 | print(dbgs()); |
| 343 | dbgs() << '\n'; |
| 344 | } |
| 345 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 346 | void DIDescriptor::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 347 | if (!get()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 348 | return; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 349 | get()->print(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 353 | const LLVMContext &Ctx) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 354 | if (!DL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 355 | return; |
| 356 | |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 357 | DIScope Scope = cast<MDScope>(DL.getScope()); |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 358 | // Omit the directory, because it's likely to be long and uninteresting. |
| 359 | CommentOS << Scope.getFilename(); |
| 360 | CommentOS << ':' << DL.getLine(); |
| 361 | if (DL.getCol() != 0) |
| 362 | CommentOS << ':' << DL.getCol(); |
| 363 | |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 364 | DebugLoc InlinedAtDL = DL.getInlinedAt(); |
| 365 | if (!InlinedAtDL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 366 | return; |
| 367 | |
| 368 | CommentOS << " @[ "; |
| 369 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 370 | CommentOS << " ]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 374 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 375 | StringRef Res = getName(); |
| 376 | if (!Res.empty()) |
| 377 | OS << Res << "," << getLineNumber(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 378 | if (auto *InlinedAt = get()->getInlinedAt()) { |
| 379 | if (DebugLoc InlinedAtDL = InlinedAt) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 380 | OS << " @["; |
| 381 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 382 | OS << "]"; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Duncan P. N. Exon Smith | 5bf3cdc | 2015-04-06 22:27:37 +0000 | [diff] [blame] | 387 | template <> |
| 388 | DIDescriptor |
| 389 | DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const { |
| 390 | return DIDescriptor(DebugNodeRef(Val).resolve(Map)); |
| 391 | } |
| 392 | template <> |
| 393 | DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const { |
| 394 | return MDScopeRef(Val).resolve(Map); |
| 395 | } |
| 396 | template <> |
| 397 | DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const { |
| 398 | return MDTypeRef(Val).resolve(Map); |
| 399 | } |
| 400 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 401 | bool llvm::stripDebugInfo(Function &F) { |
| 402 | bool Changed = false; |
| 403 | for (BasicBlock &BB : F) { |
| 404 | for (Instruction &I : BB) { |
| 405 | if (I.getDebugLoc()) { |
| 406 | Changed = true; |
| 407 | I.setDebugLoc(DebugLoc()); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return Changed; |
| 412 | } |
| 413 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 414 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 415 | bool Changed = false; |
| 416 | |
| 417 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 418 | // the module. |
| 419 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 420 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 421 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 422 | CI->eraseFromParent(); |
| 423 | } |
| 424 | Declare->eraseFromParent(); |
| 425 | Changed = true; |
| 426 | } |
| 427 | |
| 428 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 429 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 430 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 431 | CI->eraseFromParent(); |
| 432 | } |
| 433 | DbgVal->eraseFromParent(); |
| 434 | Changed = true; |
| 435 | } |
| 436 | |
| 437 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 438 | NME = M.named_metadata_end(); NMI != NME;) { |
| 439 | NamedMDNode *NMD = NMI; |
| 440 | ++NMI; |
| 441 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 442 | NMD->eraseFromParent(); |
| 443 | Changed = true; |
| 444 | } |
| 445 | } |
| 446 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 447 | for (Function &F : M) |
| 448 | Changed |= stripDebugInfo(F); |
| 449 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 450 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 451 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 452 | |
| 453 | return Changed; |
| 454 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 455 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 456 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 457 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 458 | M.getModuleFlag("Debug Info Version"))) |
| 459 | return Val->getZExtValue(); |
| 460 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 461 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 462 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 463 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 464 | llvm::makeSubprogramMap(const Module &M) { |
| 465 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 466 | |
| 467 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 468 | if (!CU_Nodes) |
| 469 | return R; |
| 470 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 471 | for (MDNode *N : CU_Nodes->operands()) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 472 | DICompileUnit CUNode = cast<MDCompileUnit>(N); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 473 | for (DISubprogram SP : CUNode->getSubprograms()) { |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 474 | if (Function *F = SP.getFunction()) |
| 475 | R.insert(std::make_pair(F, SP)); |
| 476 | } |
| 477 | } |
| 478 | return R; |
| 479 | } |