Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 1 | //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/None.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/IR/BasicBlock.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 23 | #include "llvm/IR/DebugInfo.h" |
| 24 | #include "llvm/IR/DebugInfoMetadata.h" |
| 25 | #include "llvm/IR/DebugLoc.h" |
| 26 | #include "llvm/IR/Function.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GVMaterializer.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 28 | #include "llvm/IR/Instruction.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 29 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/Metadata.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 33 | #include "llvm/Support/Casting.h" |
| 34 | #include <algorithm> |
| 35 | #include <cassert> |
| 36 | #include <utility> |
| 37 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | using namespace llvm::dwarf; |
| 40 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 41 | DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { |
| 42 | if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 43 | return LocalScope->getSubprogram(); |
| 44 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // DebugInfoFinder implementations. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | void DebugInfoFinder::reset() { |
| 52 | CUs.clear(); |
| 53 | SPs.clear(); |
| 54 | GVs.clear(); |
| 55 | TYs.clear(); |
| 56 | Scopes.clear(); |
| 57 | NodesSeen.clear(); |
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 | void DebugInfoFinder::processModule(const Module &M) { |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 61 | for (auto *CU : M.debug_compile_units()) { |
| 62 | addCompileUnit(CU); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 63 | for (auto DIG : CU->getGlobalVariables()) { |
| 64 | if (!addGlobalVariable(DIG)) |
| 65 | continue; |
| 66 | auto *GV = DIG->getVariable(); |
| 67 | processScope(GV->getScope()); |
| 68 | processType(GV->getType().resolve()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 69 | } |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 70 | for (auto *ET : CU->getEnumTypes()) |
| 71 | processType(ET); |
| 72 | for (auto *RT : CU->getRetainedTypes()) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 73 | if (auto *T = dyn_cast<DIType>(RT)) |
| 74 | processType(T); |
| 75 | else |
| 76 | processSubprogram(cast<DISubprogram>(RT)); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 77 | for (auto *Import : CU->getImportedEntities()) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 78 | auto *Entity = Import->getEntity().resolve(); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 79 | if (auto *T = dyn_cast<DIType>(Entity)) |
| 80 | processType(T); |
| 81 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 82 | processSubprogram(SP); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 83 | else if (auto *NS = dyn_cast<DINamespace>(Entity)) |
| 84 | processScope(NS->getScope()); |
| 85 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 86 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
Keno Fischer | 3077977 | 2017-04-11 13:32:11 +0000 | [diff] [blame] | 89 | for (auto &F : M.functions()) { |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 90 | if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) |
| 91 | processSubprogram(SP); |
Keno Fischer | 3077977 | 2017-04-11 13:32:11 +0000 | [diff] [blame] | 92 | // There could be subprograms from inlined functions referenced from |
| 93 | // instructions only. Walk the function to find them. |
| 94 | for (const BasicBlock &BB : F) { |
| 95 | for (const Instruction &I : BB) { |
| 96 | if (!I.getDebugLoc()) |
| 97 | continue; |
| 98 | processLocation(M, I.getDebugLoc().get()); |
| 99 | } |
| 100 | } |
| 101 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 104 | void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 105 | if (!Loc) |
| 106 | return; |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 107 | processScope(Loc->getScope()); |
| 108 | processLocation(M, Loc->getInlinedAt()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 111 | void DebugInfoFinder::processType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 112 | if (!addType(DT)) |
| 113 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 114 | processScope(DT->getScope().resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 115 | if (auto *ST = dyn_cast<DISubroutineType>(DT)) { |
| 116 | for (DITypeRef Ref : ST->getTypeArray()) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 117 | processType(Ref.resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | if (auto *DCT = dyn_cast<DICompositeType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 121 | processType(DCT->getBaseType().resolve()); |
Anders Waldenborg | 1433fd4 | 2015-04-14 09:18:17 +0000 | [diff] [blame] | 122 | for (Metadata *D : DCT->getElements()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 123 | if (auto *T = dyn_cast<DIType>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 124 | processType(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 125 | else if (auto *SP = dyn_cast<DISubprogram>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 126 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 127 | } |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 131 | processType(DDT->getBaseType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 135 | void DebugInfoFinder::processScope(DIScope *Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 136 | if (!Scope) |
| 137 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 138 | if (auto *Ty = dyn_cast<DIType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 139 | processType(Ty); |
| 140 | return; |
| 141 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 142 | if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 143 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 144 | return; |
| 145 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 146 | if (auto *SP = dyn_cast<DISubprogram>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 147 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | if (!addScope(Scope)) |
| 151 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 152 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 153 | processScope(LB->getScope()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 154 | } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 155 | processScope(NS->getScope()); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 156 | } else if (auto *M = dyn_cast<DIModule>(Scope)) { |
| 157 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 161 | void DebugInfoFinder::processSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 162 | if (!addSubprogram(SP)) |
| 163 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 164 | processScope(SP->getScope().resolve()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 165 | processType(SP->getType()); |
| 166 | for (auto *Element : SP->getTemplateParams()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 167 | if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 168 | processType(TType->getType().resolve()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 169 | } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 170 | processType(TVal->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 175 | void DebugInfoFinder::processDeclare(const Module &M, |
| 176 | const DbgDeclareInst *DDI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 177 | auto *N = dyn_cast<MDNode>(DDI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 178 | if (!N) |
| 179 | return; |
| 180 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 181 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 182 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 183 | return; |
| 184 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 185 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 186 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 187 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 188 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 191 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 192 | auto *N = dyn_cast<MDNode>(DVI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 193 | if (!N) |
| 194 | return; |
| 195 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 196 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 197 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 198 | return; |
| 199 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 200 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 201 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 202 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 203 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 206 | bool DebugInfoFinder::addType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 207 | if (!DT) |
| 208 | return false; |
| 209 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 210 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 211 | return false; |
| 212 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 213 | TYs.push_back(const_cast<DIType *>(DT)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 217 | bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 218 | if (!CU) |
| 219 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 220 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 221 | return false; |
| 222 | |
| 223 | CUs.push_back(CU); |
| 224 | return true; |
| 225 | } |
| 226 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 227 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 228 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 229 | return false; |
| 230 | |
| 231 | GVs.push_back(DIG); |
| 232 | return true; |
| 233 | } |
| 234 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 235 | bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 236 | if (!SP) |
| 237 | return false; |
| 238 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 239 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 240 | return false; |
| 241 | |
| 242 | SPs.push_back(SP); |
| 243 | return true; |
| 244 | } |
| 245 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 246 | bool DebugInfoFinder::addScope(DIScope *Scope) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 247 | if (!Scope) |
| 248 | return false; |
| 249 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 250 | // as null for now. |
| 251 | if (Scope->getNumOperands() == 0) |
| 252 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 253 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 254 | return false; |
| 255 | Scopes.push_back(Scope); |
| 256 | return true; |
| 257 | } |
| 258 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 259 | static MDNode *stripDebugLocFromLoopID(MDNode *N) { |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 260 | assert(N->op_begin() != N->op_end() && "Missing self reference?"); |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 261 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 262 | // if there is no debug location, we do not have to rewrite this MDNode. |
| 263 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 264 | return isa<DILocation>(Op.get()); |
| 265 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 266 | return N; |
| 267 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 268 | // If there is only the debug location without any actual loop metadata, we |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 269 | // can remove the metadata. |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 270 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 271 | return !isa<DILocation>(Op.get()); |
| 272 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 273 | return nullptr; |
| 274 | |
| 275 | SmallVector<Metadata *, 4> Args; |
| 276 | // Reserve operand 0 for loop id self reference. |
| 277 | auto TempNode = MDNode::getTemporary(N->getContext(), None); |
| 278 | Args.push_back(TempNode.get()); |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 279 | // Add all non-debug location operands back. |
| 280 | for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) { |
| 281 | if (!isa<DILocation>(*Op)) |
| 282 | Args.push_back(*Op); |
| 283 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 284 | |
| 285 | // Set the first operand to itself. |
| 286 | MDNode *LoopID = MDNode::get(N->getContext(), Args); |
| 287 | LoopID->replaceOperandWith(0, LoopID); |
| 288 | return LoopID; |
| 289 | } |
| 290 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 291 | bool llvm::stripDebugInfo(Function &F) { |
| 292 | bool Changed = false; |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 293 | if (F.getSubprogram()) { |
| 294 | Changed = true; |
| 295 | F.setSubprogram(nullptr); |
| 296 | } |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 297 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 298 | DenseMap<MDNode*, MDNode*> LoopIDsMap; |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 299 | for (BasicBlock &BB : F) { |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 300 | for (auto II = BB.begin(), End = BB.end(); II != End;) { |
| 301 | Instruction &I = *II++; // We may delete the instruction, increment now. |
Mehdi Amini | db8dd55 | 2016-05-14 04:58:35 +0000 | [diff] [blame] | 302 | if (isa<DbgInfoIntrinsic>(&I)) { |
| 303 | I.eraseFromParent(); |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 304 | Changed = true; |
Mehdi Amini | bbedb14 | 2016-05-07 05:07:47 +0000 | [diff] [blame] | 305 | continue; |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 306 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 307 | if (I.getDebugLoc()) { |
| 308 | Changed = true; |
| 309 | I.setDebugLoc(DebugLoc()); |
| 310 | } |
| 311 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 312 | |
| 313 | auto *TermInst = BB.getTerminator(); |
| 314 | if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { |
| 315 | auto *NewLoopID = LoopIDsMap.lookup(LoopID); |
| 316 | if (!NewLoopID) |
| 317 | NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); |
| 318 | if (NewLoopID != LoopID) |
| 319 | TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); |
| 320 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 321 | } |
| 322 | return Changed; |
| 323 | } |
| 324 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 325 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 326 | bool Changed = false; |
| 327 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 328 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 329 | NME = M.named_metadata_end(); NMI != NME;) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 330 | NamedMDNode *NMD = &*NMI; |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 331 | ++NMI; |
Davide Italiano | 84bd58e | 2016-10-17 20:05:35 +0000 | [diff] [blame] | 332 | |
| 333 | // We're stripping debug info, and without them, coverage information |
| 334 | // doesn't quite make sense. |
| 335 | if (NMD->getName().startswith("llvm.dbg.") || |
| 336 | NMD->getName() == "llvm.gcov") { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 337 | NMD->eraseFromParent(); |
| 338 | Changed = true; |
| 339 | } |
| 340 | } |
| 341 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 342 | for (Function &F : M) |
| 343 | Changed |= stripDebugInfo(F); |
| 344 | |
Adrian Prantl | 3bfe109 | 2016-10-10 17:53:33 +0000 | [diff] [blame] | 345 | for (auto &GV : M.globals()) { |
| 346 | SmallVector<MDNode *, 1> MDs; |
| 347 | GV.getMetadata(LLVMContext::MD_dbg, MDs); |
| 348 | if (!MDs.empty()) { |
| 349 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 350 | Changed = true; |
| 351 | } |
| 352 | } |
| 353 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 354 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 355 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 356 | |
| 357 | return Changed; |
| 358 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 359 | |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 360 | namespace { |
| 361 | |
| 362 | /// Helper class to downgrade -g metadata to -gline-tables-only metadata. |
| 363 | class DebugTypeInfoRemoval { |
| 364 | DenseMap<Metadata *, Metadata *> Replacements; |
| 365 | |
| 366 | public: |
| 367 | /// The (void)() type. |
| 368 | MDNode *EmptySubroutineType; |
| 369 | |
| 370 | private: |
| 371 | /// Remember what linkage name we originally had before stripping. If we end |
| 372 | /// up making two subprograms identical who originally had different linkage |
| 373 | /// names, then we need to make one of them distinct, to avoid them getting |
| 374 | /// uniqued. Maps the new node to the old linkage name. |
| 375 | DenseMap<DISubprogram *, StringRef> NewToLinkageName; |
| 376 | |
| 377 | // TODO: Remember the distinct subprogram we created for a given linkage name, |
| 378 | // so that we can continue to unique whenever possible. Map <newly created |
| 379 | // node, old linkage name> to the first (possibly distinct) mdsubprogram |
| 380 | // created for that combination. This is not strictly needed for correctness, |
| 381 | // but can cut down on the number of MDNodes and let us diff cleanly with the |
| 382 | // output of -gline-tables-only. |
| 383 | |
| 384 | public: |
| 385 | DebugTypeInfoRemoval(LLVMContext &C) |
| 386 | : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, |
| 387 | MDNode::get(C, {}))) {} |
| 388 | |
| 389 | Metadata *map(Metadata *M) { |
| 390 | if (!M) |
| 391 | return nullptr; |
| 392 | auto Replacement = Replacements.find(M); |
| 393 | if (Replacement != Replacements.end()) |
| 394 | return Replacement->second; |
| 395 | |
| 396 | return M; |
| 397 | } |
| 398 | MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } |
| 399 | |
| 400 | /// Recursively remap N and all its referenced children. Does a DF post-order |
| 401 | /// traversal, so as to remap bottoms up. |
| 402 | void traverseAndRemap(MDNode *N) { traverse(N); } |
| 403 | |
| 404 | private: |
| 405 | // Create a new DISubprogram, to replace the one given. |
| 406 | DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { |
| 407 | auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); |
| 408 | StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; |
| 409 | DISubprogram *Declaration = nullptr; |
| 410 | auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); |
| 411 | DITypeRef ContainingType(map(MDS->getContainingType())); |
| 412 | auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); |
| 413 | auto Variables = nullptr; |
| 414 | auto TemplateParams = nullptr; |
| 415 | |
| 416 | // Make a distinct DISubprogram, for situations that warrent it. |
| 417 | auto distinctMDSubprogram = [&]() { |
| 418 | return DISubprogram::getDistinct( |
| 419 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 420 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 421 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 422 | MDS->getVirtuality(), MDS->getVirtualIndex(), |
| 423 | MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit, |
| 424 | TemplateParams, Declaration, Variables); |
| 425 | }; |
| 426 | |
| 427 | if (MDS->isDistinct()) |
| 428 | return distinctMDSubprogram(); |
| 429 | |
| 430 | auto *NewMDS = DISubprogram::get( |
| 431 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 432 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 433 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 434 | MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(), |
| 435 | MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration, |
| 436 | Variables); |
| 437 | |
| 438 | StringRef OldLinkageName = MDS->getLinkageName(); |
| 439 | |
| 440 | // See if we need to make a distinct one. |
| 441 | auto OrigLinkage = NewToLinkageName.find(NewMDS); |
| 442 | if (OrigLinkage != NewToLinkageName.end()) { |
| 443 | if (OrigLinkage->second == OldLinkageName) |
| 444 | // We're good. |
| 445 | return NewMDS; |
| 446 | |
| 447 | // Otherwise, need to make a distinct one. |
| 448 | // TODO: Query the map to see if we already have one. |
| 449 | return distinctMDSubprogram(); |
| 450 | } |
| 451 | |
| 452 | NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); |
| 453 | return NewMDS; |
| 454 | } |
| 455 | |
| 456 | /// Create a new compile unit, to replace the one given |
| 457 | DICompileUnit *getReplacementCU(DICompileUnit *CU) { |
| 458 | // Drop skeleton CUs. |
| 459 | if (CU->getDWOId()) |
| 460 | return nullptr; |
| 461 | |
| 462 | auto *File = cast_or_null<DIFile>(map(CU->getFile())); |
| 463 | MDTuple *EnumTypes = nullptr; |
| 464 | MDTuple *RetainedTypes = nullptr; |
| 465 | MDTuple *GlobalVariables = nullptr; |
| 466 | MDTuple *ImportedEntities = nullptr; |
| 467 | return DICompileUnit::getDistinct( |
| 468 | CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), |
| 469 | CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), |
| 470 | CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, |
| 471 | RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 472 | CU->getDWOId(), CU->getSplitDebugInlining(), |
| 473 | CU->getDebugInfoForProfiling()); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | DILocation *getReplacementMDLocation(DILocation *MLD) { |
| 477 | auto *Scope = map(MLD->getScope()); |
| 478 | auto *InlinedAt = map(MLD->getInlinedAt()); |
| 479 | if (MLD->isDistinct()) |
| 480 | return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), |
| 481 | MLD->getColumn(), Scope, InlinedAt); |
| 482 | return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), |
| 483 | Scope, InlinedAt); |
| 484 | } |
| 485 | |
| 486 | /// Create a new generic MDNode, to replace the one given |
| 487 | MDNode *getReplacementMDNode(MDNode *N) { |
| 488 | SmallVector<Metadata *, 8> Ops; |
| 489 | Ops.reserve(N->getNumOperands()); |
| 490 | for (auto &I : N->operands()) |
| 491 | if (I) |
| 492 | Ops.push_back(map(I)); |
| 493 | auto *Ret = MDNode::get(N->getContext(), Ops); |
| 494 | return Ret; |
| 495 | } |
| 496 | |
| 497 | /// Attempt to re-map N to a newly created node. |
| 498 | void remap(MDNode *N) { |
| 499 | if (Replacements.count(N)) |
| 500 | return; |
| 501 | |
| 502 | auto doRemap = [&](MDNode *N) -> MDNode * { |
| 503 | if (!N) |
| 504 | return nullptr; |
| 505 | if (auto *MDSub = dyn_cast<DISubprogram>(N)) { |
| 506 | remap(MDSub->getUnit()); |
| 507 | return getReplacementSubprogram(MDSub); |
| 508 | } |
| 509 | if (isa<DISubroutineType>(N)) |
| 510 | return EmptySubroutineType; |
| 511 | if (auto *CU = dyn_cast<DICompileUnit>(N)) |
| 512 | return getReplacementCU(CU); |
| 513 | if (isa<DIFile>(N)) |
| 514 | return N; |
| 515 | if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) |
| 516 | // Remap to our referenced scope (recursively). |
| 517 | return mapNode(MDLB->getScope()); |
| 518 | if (auto *MLD = dyn_cast<DILocation>(N)) |
| 519 | return getReplacementMDLocation(MLD); |
| 520 | |
| 521 | // Otherwise, if we see these, just drop them now. Not strictly necessary, |
| 522 | // but this speeds things up a little. |
| 523 | if (isa<DINode>(N)) |
| 524 | return nullptr; |
| 525 | |
| 526 | return getReplacementMDNode(N); |
| 527 | }; |
| 528 | Replacements[N] = doRemap(N); |
| 529 | } |
| 530 | |
| 531 | /// Do the remapping traversal. |
| 532 | void traverse(MDNode *); |
| 533 | }; |
| 534 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 535 | } // end anonymous namespace |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 536 | |
| 537 | void DebugTypeInfoRemoval::traverse(MDNode *N) { |
| 538 | if (!N || Replacements.count(N)) |
| 539 | return; |
| 540 | |
| 541 | // To avoid cycles, as well as for efficiency sake, we will sometimes prune |
| 542 | // parts of the graph. |
| 543 | auto prune = [](MDNode *Parent, MDNode *Child) { |
| 544 | if (auto *MDS = dyn_cast<DISubprogram>(Parent)) |
| 545 | return Child == MDS->getVariables().get(); |
| 546 | return false; |
| 547 | }; |
| 548 | |
| 549 | SmallVector<MDNode *, 16> ToVisit; |
| 550 | DenseSet<MDNode *> Opened; |
| 551 | |
| 552 | // Visit each node starting at N in post order, and map them. |
| 553 | ToVisit.push_back(N); |
| 554 | while (!ToVisit.empty()) { |
| 555 | auto *N = ToVisit.back(); |
| 556 | if (!Opened.insert(N).second) { |
| 557 | // Close it. |
| 558 | remap(N); |
| 559 | ToVisit.pop_back(); |
| 560 | continue; |
| 561 | } |
| 562 | for (auto &I : N->operands()) |
| 563 | if (auto *MDN = dyn_cast_or_null<MDNode>(I)) |
| 564 | if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && |
| 565 | !isa<DICompileUnit>(MDN)) |
| 566 | ToVisit.push_back(MDN); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | bool llvm::stripNonLineTableDebugInfo(Module &M) { |
| 571 | bool Changed = false; |
| 572 | |
| 573 | // First off, delete the debug intrinsics. |
| 574 | auto RemoveUses = [&](StringRef Name) { |
| 575 | if (auto *DbgVal = M.getFunction(Name)) { |
| 576 | while (!DbgVal->use_empty()) |
| 577 | cast<Instruction>(DbgVal->user_back())->eraseFromParent(); |
| 578 | DbgVal->eraseFromParent(); |
| 579 | Changed = true; |
| 580 | } |
| 581 | }; |
| 582 | RemoveUses("llvm.dbg.declare"); |
| 583 | RemoveUses("llvm.dbg.value"); |
| 584 | |
| 585 | // Delete non-CU debug info named metadata nodes. |
| 586 | for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); |
| 587 | NMI != NME;) { |
| 588 | NamedMDNode *NMD = &*NMI; |
| 589 | ++NMI; |
| 590 | // Specifically keep dbg.cu around. |
| 591 | if (NMD->getName() == "llvm.dbg.cu") |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | // Drop all dbg attachments from global variables. |
| 596 | for (auto &GV : M.globals()) |
| 597 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 598 | |
| 599 | DebugTypeInfoRemoval Mapper(M.getContext()); |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame^] | 600 | auto remap = [&](MDNode *Node) -> MDNode * { |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 601 | if (!Node) |
| 602 | return nullptr; |
| 603 | Mapper.traverseAndRemap(Node); |
| 604 | auto *NewNode = Mapper.mapNode(Node); |
| 605 | Changed |= Node != NewNode; |
| 606 | Node = NewNode; |
| 607 | return NewNode; |
| 608 | }; |
| 609 | |
| 610 | // Rewrite the DebugLocs to be equivalent to what |
| 611 | // -gline-tables-only would have created. |
| 612 | for (auto &F : M) { |
| 613 | if (auto *SP = F.getSubprogram()) { |
| 614 | Mapper.traverseAndRemap(SP); |
| 615 | auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); |
| 616 | Changed |= SP != NewSP; |
| 617 | F.setSubprogram(NewSP); |
| 618 | } |
| 619 | for (auto &BB : F) { |
| 620 | for (auto &I : BB) { |
Adrian Prantl | 346dcaf | 2017-03-30 20:10:56 +0000 | [diff] [blame] | 621 | auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc { |
| 622 | auto *Scope = DL.getScope(); |
| 623 | MDNode *InlinedAt = DL.getInlinedAt(); |
| 624 | Scope = remap(Scope); |
| 625 | InlinedAt = remap(InlinedAt); |
| 626 | return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt); |
| 627 | }; |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 628 | |
Adrian Prantl | 346dcaf | 2017-03-30 20:10:56 +0000 | [diff] [blame] | 629 | if (I.getDebugLoc() != DebugLoc()) |
| 630 | I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); |
| 631 | |
| 632 | // Remap DILocations in untyped MDNodes (e.g., llvm.loop). |
| 633 | SmallVector<std::pair<unsigned, MDNode *>, 2> MDs; |
| 634 | I.getAllMetadata(MDs); |
| 635 | for (auto Attachment : MDs) |
| 636 | if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second)) |
| 637 | for (unsigned N = 0; N < T->getNumOperands(); ++N) |
| 638 | if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N))) |
| 639 | if (Loc != DebugLoc()) |
| 640 | T->replaceOperandWith(N, remapDebugLoc(Loc)); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // Create a new llvm.dbg.cu, which is equivalent to the one |
| 646 | // -gline-tables-only would have created. |
| 647 | for (auto &NMD : M.getNamedMDList()) { |
| 648 | SmallVector<MDNode *, 8> Ops; |
| 649 | for (MDNode *Op : NMD.operands()) |
| 650 | Ops.push_back(remap(Op)); |
| 651 | |
| 652 | if (!Changed) |
| 653 | continue; |
| 654 | |
| 655 | NMD.clearOperands(); |
| 656 | for (auto *Op : Ops) |
| 657 | if (Op) |
| 658 | NMD.addOperand(Op); |
| 659 | } |
| 660 | return Changed; |
| 661 | } |
| 662 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 663 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 664 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 665 | M.getModuleFlag("Debug Info Version"))) |
| 666 | return Val->getZExtValue(); |
| 667 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 668 | } |