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