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