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 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 15 | #include "llvm-c/DebugInfo.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | #include "llvm/ADT/DenseSet.h" |
| 18 | #include "llvm/ADT/None.h" |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/IR/BasicBlock.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfoMetadata.h" |
| 26 | #include "llvm/IR/DebugLoc.h" |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DebugInfo.h" |
| 28 | #include "llvm/IR/DIBuilder.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Function.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GVMaterializer.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Instruction.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/Metadata.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Casting.h" |
| 37 | #include <algorithm> |
| 38 | #include <cassert> |
| 39 | #include <utility> |
| 40 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | using namespace llvm::dwarf; |
| 43 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 44 | DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { |
| 45 | if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 46 | return LocalScope->getSubprogram(); |
| 47 | return nullptr; |
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 | //===----------------------------------------------------------------------===// |
| 51 | // DebugInfoFinder implementations. |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
| 54 | void DebugInfoFinder::reset() { |
| 55 | CUs.clear(); |
| 56 | SPs.clear(); |
| 57 | GVs.clear(); |
| 58 | TYs.clear(); |
| 59 | Scopes.clear(); |
| 60 | NodesSeen.clear(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 63 | void DebugInfoFinder::processModule(const Module &M) { |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 64 | for (auto *CU : M.debug_compile_units()) { |
| 65 | addCompileUnit(CU); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 66 | for (auto DIG : CU->getGlobalVariables()) { |
| 67 | if (!addGlobalVariable(DIG)) |
| 68 | continue; |
| 69 | auto *GV = DIG->getVariable(); |
| 70 | processScope(GV->getScope()); |
| 71 | processType(GV->getType().resolve()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 72 | } |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 73 | for (auto *ET : CU->getEnumTypes()) |
| 74 | processType(ET); |
| 75 | for (auto *RT : CU->getRetainedTypes()) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 76 | if (auto *T = dyn_cast<DIType>(RT)) |
| 77 | processType(T); |
| 78 | else |
| 79 | processSubprogram(cast<DISubprogram>(RT)); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 80 | for (auto *Import : CU->getImportedEntities()) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 81 | auto *Entity = Import->getEntity().resolve(); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 82 | if (auto *T = dyn_cast<DIType>(Entity)) |
| 83 | processType(T); |
| 84 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 85 | processSubprogram(SP); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 86 | else if (auto *NS = dyn_cast<DINamespace>(Entity)) |
| 87 | processScope(NS->getScope()); |
| 88 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 89 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
Keno Fischer | 3077977 | 2017-04-11 13:32:11 +0000 | [diff] [blame] | 92 | for (auto &F : M.functions()) { |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 93 | if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) |
| 94 | processSubprogram(SP); |
Keno Fischer | 3077977 | 2017-04-11 13:32:11 +0000 | [diff] [blame] | 95 | // There could be subprograms from inlined functions referenced from |
| 96 | // instructions only. Walk the function to find them. |
| 97 | for (const BasicBlock &BB : F) { |
| 98 | for (const Instruction &I : BB) { |
| 99 | if (!I.getDebugLoc()) |
| 100 | continue; |
| 101 | processLocation(M, I.getDebugLoc().get()); |
| 102 | } |
| 103 | } |
| 104 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 107 | void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 108 | if (!Loc) |
| 109 | return; |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 110 | processScope(Loc->getScope()); |
| 111 | processLocation(M, Loc->getInlinedAt()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 114 | void DebugInfoFinder::processType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 115 | if (!addType(DT)) |
| 116 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 117 | processScope(DT->getScope().resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 118 | if (auto *ST = dyn_cast<DISubroutineType>(DT)) { |
| 119 | for (DITypeRef Ref : ST->getTypeArray()) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 120 | processType(Ref.resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | if (auto *DCT = dyn_cast<DICompositeType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 124 | processType(DCT->getBaseType().resolve()); |
Anders Waldenborg | 1433fd4 | 2015-04-14 09:18:17 +0000 | [diff] [blame] | 125 | for (Metadata *D : DCT->getElements()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 126 | if (auto *T = dyn_cast<DIType>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 127 | processType(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 128 | else if (auto *SP = dyn_cast<DISubprogram>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 129 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 130 | } |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 134 | processType(DDT->getBaseType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 138 | void DebugInfoFinder::processScope(DIScope *Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 139 | if (!Scope) |
| 140 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 141 | if (auto *Ty = dyn_cast<DIType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 142 | processType(Ty); |
| 143 | return; |
| 144 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 145 | if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 146 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 149 | if (auto *SP = dyn_cast<DISubprogram>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 150 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | if (!addScope(Scope)) |
| 154 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 155 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 156 | processScope(LB->getScope()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 157 | } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 158 | processScope(NS->getScope()); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 159 | } else if (auto *M = dyn_cast<DIModule>(Scope)) { |
| 160 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 164 | void DebugInfoFinder::processSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 165 | if (!addSubprogram(SP)) |
| 166 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 167 | processScope(SP->getScope().resolve()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 168 | processType(SP->getType()); |
| 169 | for (auto *Element : SP->getTemplateParams()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 170 | if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 171 | processType(TType->getType().resolve()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 172 | } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 173 | processType(TVal->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 178 | void DebugInfoFinder::processDeclare(const Module &M, |
| 179 | const DbgDeclareInst *DDI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 180 | auto *N = dyn_cast<MDNode>(DDI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 181 | if (!N) |
| 182 | return; |
| 183 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 184 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 185 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 186 | return; |
| 187 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 188 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 189 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 190 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 191 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 194 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 195 | auto *N = dyn_cast<MDNode>(DVI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 196 | if (!N) |
| 197 | return; |
| 198 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 199 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 200 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 201 | return; |
| 202 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 203 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 204 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 205 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 206 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 209 | bool DebugInfoFinder::addType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 210 | if (!DT) |
| 211 | return false; |
| 212 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 213 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 214 | return false; |
| 215 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 216 | TYs.push_back(const_cast<DIType *>(DT)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 220 | bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 221 | if (!CU) |
| 222 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 223 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 224 | return false; |
| 225 | |
| 226 | CUs.push_back(CU); |
| 227 | return true; |
| 228 | } |
| 229 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 230 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 231 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 232 | return false; |
| 233 | |
| 234 | GVs.push_back(DIG); |
| 235 | return true; |
| 236 | } |
| 237 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 238 | bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 239 | if (!SP) |
| 240 | return false; |
| 241 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 242 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 243 | return false; |
| 244 | |
| 245 | SPs.push_back(SP); |
| 246 | return true; |
| 247 | } |
| 248 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 249 | bool DebugInfoFinder::addScope(DIScope *Scope) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | if (!Scope) |
| 251 | return false; |
| 252 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 253 | // as null for now. |
| 254 | if (Scope->getNumOperands() == 0) |
| 255 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 256 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 257 | return false; |
| 258 | Scopes.push_back(Scope); |
| 259 | return true; |
| 260 | } |
| 261 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 262 | static MDNode *stripDebugLocFromLoopID(MDNode *N) { |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 263 | assert(N->op_begin() != N->op_end() && "Missing self reference?"); |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 264 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 265 | // if there is no debug location, we do not have to rewrite this MDNode. |
| 266 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 267 | return isa<DILocation>(Op.get()); |
| 268 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 269 | return N; |
| 270 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 271 | // 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] | 272 | // can remove the metadata. |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 273 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 274 | return !isa<DILocation>(Op.get()); |
| 275 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 276 | return nullptr; |
| 277 | |
| 278 | SmallVector<Metadata *, 4> Args; |
| 279 | // Reserve operand 0 for loop id self reference. |
| 280 | auto TempNode = MDNode::getTemporary(N->getContext(), None); |
| 281 | Args.push_back(TempNode.get()); |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame] | 282 | // Add all non-debug location operands back. |
| 283 | for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) { |
| 284 | if (!isa<DILocation>(*Op)) |
| 285 | Args.push_back(*Op); |
| 286 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 287 | |
| 288 | // Set the first operand to itself. |
| 289 | MDNode *LoopID = MDNode::get(N->getContext(), Args); |
| 290 | LoopID->replaceOperandWith(0, LoopID); |
| 291 | return LoopID; |
| 292 | } |
| 293 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 294 | bool llvm::stripDebugInfo(Function &F) { |
| 295 | bool Changed = false; |
Adrian Prantl | a8b2ddb | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 296 | if (F.getMetadata(LLVMContext::MD_dbg)) { |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 297 | Changed = true; |
| 298 | F.setSubprogram(nullptr); |
| 299 | } |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 300 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 301 | DenseMap<MDNode*, MDNode*> LoopIDsMap; |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 302 | for (BasicBlock &BB : F) { |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 303 | for (auto II = BB.begin(), End = BB.end(); II != End;) { |
| 304 | Instruction &I = *II++; // We may delete the instruction, increment now. |
Mehdi Amini | db8dd55 | 2016-05-14 04:58:35 +0000 | [diff] [blame] | 305 | if (isa<DbgInfoIntrinsic>(&I)) { |
| 306 | I.eraseFromParent(); |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 307 | Changed = true; |
Mehdi Amini | bbedb14 | 2016-05-07 05:07:47 +0000 | [diff] [blame] | 308 | continue; |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 309 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 310 | if (I.getDebugLoc()) { |
| 311 | Changed = true; |
| 312 | I.setDebugLoc(DebugLoc()); |
| 313 | } |
| 314 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 315 | |
| 316 | auto *TermInst = BB.getTerminator(); |
Justin Bogner | b29bebe | 2017-08-18 21:38:03 +0000 | [diff] [blame] | 317 | if (!TermInst) |
| 318 | // This is invalid IR, but we may not have run the verifier yet |
| 319 | continue; |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 320 | if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { |
| 321 | auto *NewLoopID = LoopIDsMap.lookup(LoopID); |
| 322 | if (!NewLoopID) |
| 323 | NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); |
| 324 | if (NewLoopID != LoopID) |
| 325 | TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); |
| 326 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 327 | } |
| 328 | return Changed; |
| 329 | } |
| 330 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 331 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 332 | bool Changed = false; |
| 333 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 334 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 335 | NME = M.named_metadata_end(); NMI != NME;) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 336 | NamedMDNode *NMD = &*NMI; |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 337 | ++NMI; |
Davide Italiano | 84bd58e | 2016-10-17 20:05:35 +0000 | [diff] [blame] | 338 | |
| 339 | // We're stripping debug info, and without them, coverage information |
| 340 | // doesn't quite make sense. |
| 341 | if (NMD->getName().startswith("llvm.dbg.") || |
| 342 | NMD->getName() == "llvm.gcov") { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 343 | NMD->eraseFromParent(); |
| 344 | Changed = true; |
| 345 | } |
| 346 | } |
| 347 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 348 | for (Function &F : M) |
| 349 | Changed |= stripDebugInfo(F); |
| 350 | |
Adrian Prantl | 3bfe109 | 2016-10-10 17:53:33 +0000 | [diff] [blame] | 351 | for (auto &GV : M.globals()) { |
| 352 | SmallVector<MDNode *, 1> MDs; |
| 353 | GV.getMetadata(LLVMContext::MD_dbg, MDs); |
| 354 | if (!MDs.empty()) { |
| 355 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 356 | Changed = true; |
| 357 | } |
| 358 | } |
| 359 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 360 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 361 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 362 | |
| 363 | return Changed; |
| 364 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 365 | |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 366 | namespace { |
| 367 | |
| 368 | /// Helper class to downgrade -g metadata to -gline-tables-only metadata. |
| 369 | class DebugTypeInfoRemoval { |
| 370 | DenseMap<Metadata *, Metadata *> Replacements; |
| 371 | |
| 372 | public: |
| 373 | /// The (void)() type. |
| 374 | MDNode *EmptySubroutineType; |
| 375 | |
| 376 | private: |
| 377 | /// Remember what linkage name we originally had before stripping. If we end |
| 378 | /// up making two subprograms identical who originally had different linkage |
| 379 | /// names, then we need to make one of them distinct, to avoid them getting |
| 380 | /// uniqued. Maps the new node to the old linkage name. |
| 381 | DenseMap<DISubprogram *, StringRef> NewToLinkageName; |
| 382 | |
| 383 | // TODO: Remember the distinct subprogram we created for a given linkage name, |
| 384 | // so that we can continue to unique whenever possible. Map <newly created |
| 385 | // node, old linkage name> to the first (possibly distinct) mdsubprogram |
| 386 | // created for that combination. This is not strictly needed for correctness, |
| 387 | // but can cut down on the number of MDNodes and let us diff cleanly with the |
| 388 | // output of -gline-tables-only. |
| 389 | |
| 390 | public: |
| 391 | DebugTypeInfoRemoval(LLVMContext &C) |
| 392 | : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, |
| 393 | MDNode::get(C, {}))) {} |
| 394 | |
| 395 | Metadata *map(Metadata *M) { |
| 396 | if (!M) |
| 397 | return nullptr; |
| 398 | auto Replacement = Replacements.find(M); |
| 399 | if (Replacement != Replacements.end()) |
| 400 | return Replacement->second; |
| 401 | |
| 402 | return M; |
| 403 | } |
| 404 | MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } |
| 405 | |
| 406 | /// Recursively remap N and all its referenced children. Does a DF post-order |
| 407 | /// traversal, so as to remap bottoms up. |
| 408 | void traverseAndRemap(MDNode *N) { traverse(N); } |
| 409 | |
| 410 | private: |
| 411 | // Create a new DISubprogram, to replace the one given. |
| 412 | DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { |
| 413 | auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); |
| 414 | StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; |
| 415 | DISubprogram *Declaration = nullptr; |
| 416 | auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); |
| 417 | DITypeRef ContainingType(map(MDS->getContainingType())); |
| 418 | auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); |
| 419 | auto Variables = nullptr; |
| 420 | auto TemplateParams = nullptr; |
| 421 | |
| 422 | // Make a distinct DISubprogram, for situations that warrent it. |
| 423 | auto distinctMDSubprogram = [&]() { |
| 424 | return DISubprogram::getDistinct( |
| 425 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 426 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 427 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 428 | MDS->getVirtuality(), MDS->getVirtualIndex(), |
| 429 | MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit, |
| 430 | TemplateParams, Declaration, Variables); |
| 431 | }; |
| 432 | |
| 433 | if (MDS->isDistinct()) |
| 434 | return distinctMDSubprogram(); |
| 435 | |
| 436 | auto *NewMDS = DISubprogram::get( |
| 437 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 438 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 439 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 440 | MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(), |
| 441 | MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration, |
| 442 | Variables); |
| 443 | |
| 444 | StringRef OldLinkageName = MDS->getLinkageName(); |
| 445 | |
| 446 | // See if we need to make a distinct one. |
| 447 | auto OrigLinkage = NewToLinkageName.find(NewMDS); |
| 448 | if (OrigLinkage != NewToLinkageName.end()) { |
| 449 | if (OrigLinkage->second == OldLinkageName) |
| 450 | // We're good. |
| 451 | return NewMDS; |
| 452 | |
| 453 | // Otherwise, need to make a distinct one. |
| 454 | // TODO: Query the map to see if we already have one. |
| 455 | return distinctMDSubprogram(); |
| 456 | } |
| 457 | |
| 458 | NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); |
| 459 | return NewMDS; |
| 460 | } |
| 461 | |
| 462 | /// Create a new compile unit, to replace the one given |
| 463 | DICompileUnit *getReplacementCU(DICompileUnit *CU) { |
| 464 | // Drop skeleton CUs. |
| 465 | if (CU->getDWOId()) |
| 466 | return nullptr; |
| 467 | |
| 468 | auto *File = cast_or_null<DIFile>(map(CU->getFile())); |
| 469 | MDTuple *EnumTypes = nullptr; |
| 470 | MDTuple *RetainedTypes = nullptr; |
| 471 | MDTuple *GlobalVariables = nullptr; |
| 472 | MDTuple *ImportedEntities = nullptr; |
| 473 | return DICompileUnit::getDistinct( |
| 474 | CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), |
| 475 | CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), |
| 476 | CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, |
| 477 | RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 478 | CU->getDWOId(), CU->getSplitDebugInlining(), |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 479 | CU->getDebugInfoForProfiling(), CU->getGnuPubnames()); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | DILocation *getReplacementMDLocation(DILocation *MLD) { |
| 483 | auto *Scope = map(MLD->getScope()); |
| 484 | auto *InlinedAt = map(MLD->getInlinedAt()); |
| 485 | if (MLD->isDistinct()) |
| 486 | return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), |
| 487 | MLD->getColumn(), Scope, InlinedAt); |
| 488 | return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), |
| 489 | Scope, InlinedAt); |
| 490 | } |
| 491 | |
| 492 | /// Create a new generic MDNode, to replace the one given |
| 493 | MDNode *getReplacementMDNode(MDNode *N) { |
| 494 | SmallVector<Metadata *, 8> Ops; |
| 495 | Ops.reserve(N->getNumOperands()); |
| 496 | for (auto &I : N->operands()) |
| 497 | if (I) |
| 498 | Ops.push_back(map(I)); |
| 499 | auto *Ret = MDNode::get(N->getContext(), Ops); |
| 500 | return Ret; |
| 501 | } |
| 502 | |
| 503 | /// Attempt to re-map N to a newly created node. |
| 504 | void remap(MDNode *N) { |
| 505 | if (Replacements.count(N)) |
| 506 | return; |
| 507 | |
| 508 | auto doRemap = [&](MDNode *N) -> MDNode * { |
| 509 | if (!N) |
| 510 | return nullptr; |
| 511 | if (auto *MDSub = dyn_cast<DISubprogram>(N)) { |
| 512 | remap(MDSub->getUnit()); |
| 513 | return getReplacementSubprogram(MDSub); |
| 514 | } |
| 515 | if (isa<DISubroutineType>(N)) |
| 516 | return EmptySubroutineType; |
| 517 | if (auto *CU = dyn_cast<DICompileUnit>(N)) |
| 518 | return getReplacementCU(CU); |
| 519 | if (isa<DIFile>(N)) |
| 520 | return N; |
| 521 | if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) |
| 522 | // Remap to our referenced scope (recursively). |
| 523 | return mapNode(MDLB->getScope()); |
| 524 | if (auto *MLD = dyn_cast<DILocation>(N)) |
| 525 | return getReplacementMDLocation(MLD); |
| 526 | |
| 527 | // Otherwise, if we see these, just drop them now. Not strictly necessary, |
| 528 | // but this speeds things up a little. |
| 529 | if (isa<DINode>(N)) |
| 530 | return nullptr; |
| 531 | |
| 532 | return getReplacementMDNode(N); |
| 533 | }; |
| 534 | Replacements[N] = doRemap(N); |
| 535 | } |
| 536 | |
| 537 | /// Do the remapping traversal. |
| 538 | void traverse(MDNode *); |
| 539 | }; |
| 540 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 541 | } // end anonymous namespace |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 542 | |
| 543 | void DebugTypeInfoRemoval::traverse(MDNode *N) { |
| 544 | if (!N || Replacements.count(N)) |
| 545 | return; |
| 546 | |
| 547 | // To avoid cycles, as well as for efficiency sake, we will sometimes prune |
| 548 | // parts of the graph. |
| 549 | auto prune = [](MDNode *Parent, MDNode *Child) { |
| 550 | if (auto *MDS = dyn_cast<DISubprogram>(Parent)) |
| 551 | return Child == MDS->getVariables().get(); |
| 552 | return false; |
| 553 | }; |
| 554 | |
| 555 | SmallVector<MDNode *, 16> ToVisit; |
| 556 | DenseSet<MDNode *> Opened; |
| 557 | |
| 558 | // Visit each node starting at N in post order, and map them. |
| 559 | ToVisit.push_back(N); |
| 560 | while (!ToVisit.empty()) { |
| 561 | auto *N = ToVisit.back(); |
| 562 | if (!Opened.insert(N).second) { |
| 563 | // Close it. |
| 564 | remap(N); |
| 565 | ToVisit.pop_back(); |
| 566 | continue; |
| 567 | } |
| 568 | for (auto &I : N->operands()) |
| 569 | if (auto *MDN = dyn_cast_or_null<MDNode>(I)) |
| 570 | if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && |
| 571 | !isa<DICompileUnit>(MDN)) |
| 572 | ToVisit.push_back(MDN); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | bool llvm::stripNonLineTableDebugInfo(Module &M) { |
| 577 | bool Changed = false; |
| 578 | |
| 579 | // First off, delete the debug intrinsics. |
| 580 | auto RemoveUses = [&](StringRef Name) { |
| 581 | if (auto *DbgVal = M.getFunction(Name)) { |
| 582 | while (!DbgVal->use_empty()) |
| 583 | cast<Instruction>(DbgVal->user_back())->eraseFromParent(); |
| 584 | DbgVal->eraseFromParent(); |
| 585 | Changed = true; |
| 586 | } |
| 587 | }; |
| 588 | RemoveUses("llvm.dbg.declare"); |
| 589 | RemoveUses("llvm.dbg.value"); |
| 590 | |
| 591 | // Delete non-CU debug info named metadata nodes. |
| 592 | for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); |
| 593 | NMI != NME;) { |
| 594 | NamedMDNode *NMD = &*NMI; |
| 595 | ++NMI; |
| 596 | // Specifically keep dbg.cu around. |
| 597 | if (NMD->getName() == "llvm.dbg.cu") |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | // Drop all dbg attachments from global variables. |
| 602 | for (auto &GV : M.globals()) |
| 603 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 604 | |
| 605 | DebugTypeInfoRemoval Mapper(M.getContext()); |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 606 | auto remap = [&](MDNode *Node) -> MDNode * { |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 607 | if (!Node) |
| 608 | return nullptr; |
| 609 | Mapper.traverseAndRemap(Node); |
| 610 | auto *NewNode = Mapper.mapNode(Node); |
| 611 | Changed |= Node != NewNode; |
| 612 | Node = NewNode; |
| 613 | return NewNode; |
| 614 | }; |
| 615 | |
| 616 | // Rewrite the DebugLocs to be equivalent to what |
| 617 | // -gline-tables-only would have created. |
| 618 | for (auto &F : M) { |
| 619 | if (auto *SP = F.getSubprogram()) { |
| 620 | Mapper.traverseAndRemap(SP); |
| 621 | auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); |
| 622 | Changed |= SP != NewSP; |
| 623 | F.setSubprogram(NewSP); |
| 624 | } |
| 625 | for (auto &BB : F) { |
| 626 | for (auto &I : BB) { |
Adrian Prantl | 346dcaf | 2017-03-30 20:10:56 +0000 | [diff] [blame] | 627 | auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc { |
| 628 | auto *Scope = DL.getScope(); |
| 629 | MDNode *InlinedAt = DL.getInlinedAt(); |
| 630 | Scope = remap(Scope); |
| 631 | InlinedAt = remap(InlinedAt); |
| 632 | return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt); |
| 633 | }; |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 634 | |
Adrian Prantl | 346dcaf | 2017-03-30 20:10:56 +0000 | [diff] [blame] | 635 | if (I.getDebugLoc() != DebugLoc()) |
| 636 | I.setDebugLoc(remapDebugLoc(I.getDebugLoc())); |
| 637 | |
| 638 | // Remap DILocations in untyped MDNodes (e.g., llvm.loop). |
| 639 | SmallVector<std::pair<unsigned, MDNode *>, 2> MDs; |
| 640 | I.getAllMetadata(MDs); |
| 641 | for (auto Attachment : MDs) |
| 642 | if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second)) |
| 643 | for (unsigned N = 0; N < T->getNumOperands(); ++N) |
| 644 | if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N))) |
| 645 | if (Loc != DebugLoc()) |
| 646 | T->replaceOperandWith(N, remapDebugLoc(Loc)); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Create a new llvm.dbg.cu, which is equivalent to the one |
| 652 | // -gline-tables-only would have created. |
| 653 | for (auto &NMD : M.getNamedMDList()) { |
| 654 | SmallVector<MDNode *, 8> Ops; |
| 655 | for (MDNode *Op : NMD.operands()) |
| 656 | Ops.push_back(remap(Op)); |
| 657 | |
| 658 | if (!Changed) |
| 659 | continue; |
| 660 | |
| 661 | NMD.clearOperands(); |
| 662 | for (auto *Op : Ops) |
| 663 | if (Op) |
| 664 | NMD.addOperand(Op); |
| 665 | } |
| 666 | return Changed; |
| 667 | } |
| 668 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 669 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 670 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 671 | M.getModuleFlag("Debug Info Version"))) |
| 672 | return Val->getZExtValue(); |
| 673 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 674 | } |
Dehao Chen | f464627 | 2017-10-02 18:13:14 +0000 | [diff] [blame] | 675 | |
| 676 | void Instruction::applyMergedLocation(const DILocation *LocA, |
| 677 | const DILocation *LocB) { |
Vedant Kumar | 65b0d4d | 2018-04-12 20:58:24 +0000 | [diff] [blame^] | 678 | setDebugLoc(DILocation::getMergedLocation(LocA, LocB, |
| 679 | DILocation::WithGeneratedLocation)); |
Dehao Chen | f464627 | 2017-10-02 18:13:14 +0000 | [diff] [blame] | 680 | } |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 681 | |
| 682 | //===----------------------------------------------------------------------===// |
| 683 | // LLVM C API implementations. |
| 684 | //===----------------------------------------------------------------------===// |
| 685 | |
| 686 | static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) { |
| 687 | switch (lang) { |
| 688 | #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \ |
| 689 | case LLVMDWARFSourceLanguage##NAME: return ID; |
| 690 | #include "llvm/BinaryFormat/Dwarf.def" |
| 691 | #undef HANDLE_DW_LANG |
| 692 | } |
| 693 | llvm_unreachable("Unhandled Tag"); |
| 694 | } |
| 695 | |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 696 | template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) { |
| 697 | return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr); |
| 698 | } |
| 699 | |
| 700 | static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) { |
| 701 | return static_cast<DINode::DIFlags>(Flags); |
| 702 | } |
| 703 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 704 | unsigned LLVMDebugMetadataVersion() { |
| 705 | return DEBUG_METADATA_VERSION; |
| 706 | } |
| 707 | |
| 708 | LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) { |
| 709 | return wrap(new DIBuilder(*unwrap(M), false)); |
| 710 | } |
| 711 | |
| 712 | LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { |
| 713 | return wrap(new DIBuilder(*unwrap(M))); |
| 714 | } |
| 715 | |
| 716 | unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) { |
| 717 | return getDebugMetadataVersionFromModule(*unwrap(M)); |
| 718 | } |
| 719 | |
| 720 | LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) { |
| 721 | return StripDebugInfo(*unwrap(M)); |
| 722 | } |
| 723 | |
| 724 | void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) { |
| 725 | delete unwrap(Builder); |
| 726 | } |
| 727 | |
| 728 | void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) { |
| 729 | unwrap(Builder)->finalize(); |
| 730 | } |
| 731 | |
| 732 | LLVMMetadataRef LLVMDIBuilderCreateCompileUnit( |
| 733 | LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang, |
| 734 | LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen, |
| 735 | LLVMBool isOptimized, const char *Flags, size_t FlagsLen, |
| 736 | unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen, |
| 737 | LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining, |
| 738 | LLVMBool DebugInfoForProfiling) { |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 739 | auto File = unwrapDI<DIFile>(FileRef); |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 740 | |
| 741 | return wrap(unwrap(Builder)->createCompileUnit( |
| 742 | map_from_llvmDWARFsourcelanguage(Lang), File, |
| 743 | StringRef(Producer, ProducerLen), isOptimized, |
| 744 | StringRef(Flags, FlagsLen), RuntimeVer, |
| 745 | StringRef(SplitName, SplitNameLen), |
| 746 | static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId, |
| 747 | SplitDebugInlining, DebugInfoForProfiling)); |
| 748 | } |
| 749 | |
| 750 | LLVMMetadataRef |
| 751 | LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename, |
| 752 | size_t FilenameLen, const char *Directory, |
| 753 | size_t DirectoryLen) { |
| 754 | return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen), |
| 755 | StringRef(Directory, DirectoryLen))); |
| 756 | } |
| 757 | |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 758 | LLVMMetadataRef LLVMDIBuilderCreateFunction( |
| 759 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 760 | size_t NameLen, const char *LinkageName, size_t LinkageNameLen, |
| 761 | LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty, |
| 762 | LLVMBool IsLocalToUnit, LLVMBool IsDefinition, |
| 763 | unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) { |
| 764 | return wrap(unwrap(Builder)->createFunction( |
| 765 | unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen}, |
| 766 | unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), |
| 767 | IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags), |
| 768 | IsOptimized, nullptr, nullptr, nullptr)); |
| 769 | } |
| 770 | |
| 771 | |
| 772 | LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock( |
| 773 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, |
| 774 | LLVMMetadataRef File, unsigned Line, unsigned Col) { |
| 775 | return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope), |
| 776 | unwrapDI<DIFile>(File), |
| 777 | Line, Col)); |
| 778 | } |
| 779 | |
| 780 | LLVMMetadataRef |
| 781 | LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder, |
| 782 | LLVMMetadataRef Scope, |
| 783 | LLVMMetadataRef File, |
| 784 | unsigned Discriminator) { |
| 785 | return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope), |
| 786 | unwrapDI<DIFile>(File), |
| 787 | Discriminator)); |
| 788 | } |
| 789 | |
whitequark | 789164d | 2017-11-01 22:18:52 +0000 | [diff] [blame] | 790 | LLVMMetadataRef |
| 791 | LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line, |
| 792 | unsigned Column, LLVMMetadataRef Scope, |
| 793 | LLVMMetadataRef InlinedAt) { |
| 794 | return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope), |
| 795 | unwrap(InlinedAt))); |
| 796 | } |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 797 | |
| 798 | LLVMMetadataRef LLVMDIBuilderCreateEnumerationType( |
| 799 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 800 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 801 | unsigned SizeInBits, unsigned AlignInBits, LLVMMetadataRef *Elements, |
| 802 | unsigned NumElements, LLVMMetadataRef ClassTy) { |
| 803 | auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), |
| 804 | NumElements}); |
| 805 | return wrap(unwrap(Builder)->createEnumerationType( |
| 806 | unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), |
| 807 | LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy))); |
| 808 | } |
| 809 | |
| 810 | LLVMMetadataRef LLVMDIBuilderCreateUnionType( |
| 811 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 812 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 813 | unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags, |
| 814 | LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang, |
| 815 | const char *UniqueId, size_t UniqueIdLen) { |
| 816 | auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), |
| 817 | NumElements}); |
| 818 | return wrap(unwrap(Builder)->createUnionType( |
| 819 | unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), |
| 820 | LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), |
| 821 | Elts, RunTimeLang, {UniqueId, UniqueIdLen})); |
| 822 | } |
| 823 | |
| 824 | |
| 825 | LLVMMetadataRef |
| 826 | LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, unsigned Size, |
| 827 | unsigned AlignInBits, LLVMMetadataRef Ty, |
| 828 | LLVMMetadataRef *Subscripts, |
| 829 | unsigned NumSubscripts) { |
| 830 | auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), |
| 831 | NumSubscripts}); |
| 832 | return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits, |
| 833 | unwrapDI<DIType>(Ty), Subs)); |
| 834 | } |
| 835 | |
| 836 | LLVMMetadataRef |
| 837 | LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, unsigned Size, |
| 838 | unsigned AlignInBits, LLVMMetadataRef Ty, |
| 839 | LLVMMetadataRef *Subscripts, |
| 840 | unsigned NumSubscripts) { |
| 841 | auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts), |
| 842 | NumSubscripts}); |
| 843 | return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits, |
| 844 | unwrapDI<DIType>(Ty), Subs)); |
| 845 | } |
| 846 | |
| 847 | LLVMMetadataRef |
| 848 | LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name, |
| 849 | size_t NameLen, unsigned SizeInBits, |
| 850 | LLVMDWARFTypeEncoding Encoding) { |
| 851 | return wrap(unwrap(Builder)->createBasicType({Name, NameLen}, |
| 852 | SizeInBits, Encoding)); |
| 853 | } |
| 854 | |
| 855 | LLVMMetadataRef LLVMDIBuilderCreatePointerType( |
| 856 | LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy, |
| 857 | unsigned SizeInBits, unsigned AlignInBits, unsigned AddressSpace, |
| 858 | const char *Name, size_t NameLen) { |
| 859 | return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy), |
| 860 | SizeInBits, AlignInBits, |
| 861 | AddressSpace, {Name, NameLen})); |
| 862 | } |
| 863 | |
| 864 | LLVMMetadataRef LLVMDIBuilderCreateStructType( |
| 865 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 866 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 867 | unsigned SizeInBits, unsigned AlignInBits, LLVMDIFlags Flags, |
| 868 | LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements, |
| 869 | unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder, |
| 870 | const char *UniqueId, size_t UniqueIdLen) { |
| 871 | auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements), |
| 872 | NumElements}); |
| 873 | return wrap(unwrap(Builder)->createStructType( |
| 874 | unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File), |
| 875 | LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags), |
| 876 | unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang, |
| 877 | unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen})); |
| 878 | } |
| 879 | |
| 880 | LLVMMetadataRef LLVMDIBuilderCreateMemberType( |
| 881 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
Harlan Haskins | bee4b58 | 2018-04-02 19:11:44 +0000 | [diff] [blame] | 882 | size_t NameLen, LLVMMetadataRef File, unsigned LineNo, unsigned SizeInBits, |
| 883 | unsigned AlignInBits, unsigned OffsetInBits, LLVMDIFlags Flags, |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 884 | LLVMMetadataRef Ty) { |
| 885 | return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope), |
| 886 | {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits, |
| 887 | OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty))); |
| 888 | } |
| 889 | |
| 890 | LLVMMetadataRef |
| 891 | LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name, |
| 892 | size_t NameLen) { |
| 893 | return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen})); |
| 894 | } |
| 895 | |
| 896 | LLVMMetadataRef |
| 897 | LLVMDIBuilderCreateStaticMemberType( |
| 898 | LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name, |
| 899 | size_t NameLen, LLVMMetadataRef File, unsigned LineNumber, |
| 900 | LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal, |
| 901 | unsigned AlignInBits) { |
| 902 | return wrap(unwrap(Builder)->createStaticMemberType( |
| 903 | unwrapDI<DIScope>(Scope), {Name, NameLen}, |
| 904 | unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type), |
| 905 | map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal), |
| 906 | AlignInBits)); |
| 907 | } |
| 908 | |
| 909 | LLVMMetadataRef |
| 910 | LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder, |
| 911 | LLVMMetadataRef Type) { |
| 912 | return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type))); |
| 913 | } |
| 914 | |
| 915 | LLVMMetadataRef |
| 916 | LLVMDIBuilderCreateReplaceableCompositeType( |
Harlan Haskins | bee4b58 | 2018-04-02 19:11:44 +0000 | [diff] [blame] | 917 | LLVMDIBuilderRef Builder, unsigned Tag, const char *Name, |
| 918 | size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line, |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 919 | unsigned RuntimeLang, unsigned SizeInBits, unsigned AlignInBits, |
| 920 | LLVMDIFlags Flags, const char *UniqueIdentifier, |
Harlan Haskins | bee4b58 | 2018-04-02 19:11:44 +0000 | [diff] [blame] | 921 | size_t UniqueIdentifierLen) { |
Harlan Haskins | b7881bb | 2018-04-02 00:17:40 +0000 | [diff] [blame] | 922 | return wrap(unwrap(Builder)->createReplaceableCompositeType( |
| 923 | Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope), |
| 924 | unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits, |
| 925 | AlignInBits, map_from_llvmDIFlags(Flags), |
| 926 | {UniqueIdentifier, UniqueIdentifierLen})); |
| 927 | } |
| 928 | |
| 929 | LLVMMetadataRef |
| 930 | LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag, |
| 931 | LLVMMetadataRef Type) { |
| 932 | return wrap(unwrap(Builder)->createQualifiedType(Tag, |
| 933 | unwrapDI<DIType>(Type))); |
| 934 | } |
| 935 | |
| 936 | LLVMMetadataRef |
| 937 | LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag, |
| 938 | LLVMMetadataRef Type) { |
| 939 | return wrap(unwrap(Builder)->createReferenceType(Tag, |
| 940 | unwrapDI<DIType>(Type))); |
| 941 | } |
| 942 | |
| 943 | LLVMMetadataRef |
| 944 | LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) { |
| 945 | return wrap(unwrap(Builder)->createNullPtrType()); |
| 946 | } |
| 947 | |
| 948 | LLVMMetadataRef |
| 949 | LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder, |
| 950 | LLVMMetadataRef PointeeType, |
| 951 | LLVMMetadataRef ClassType, |
| 952 | unsigned SizeInBits, |
| 953 | unsigned AlignInBits, |
| 954 | LLVMDIFlags Flags) { |
| 955 | return wrap(unwrap(Builder)->createMemberPointerType( |
| 956 | unwrapDI<DIType>(PointeeType), |
| 957 | unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits, |
| 958 | map_from_llvmDIFlags(Flags))); |
| 959 | } |
| 960 | |
| 961 | LLVMMetadataRef |
| 962 | LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder, |
| 963 | LLVMMetadataRef Type) { |
| 964 | return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type))); |
| 965 | } |
| 966 | |
| 967 | LLVMMetadataRef |
| 968 | LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder, |
| 969 | LLVMMetadataRef File, |
| 970 | LLVMMetadataRef *ParameterTypes, |
| 971 | unsigned NumParameterTypes, |
| 972 | LLVMDIFlags Flags) { |
| 973 | auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes), |
| 974 | NumParameterTypes}); |
| 975 | return wrap(unwrap(Builder)->createSubroutineType( |
| 976 | Elts, map_from_llvmDIFlags(Flags))); |
| 977 | } |
Robert Widmann | f53050f | 2018-04-07 06:07:55 +0000 | [diff] [blame] | 978 | |
| 979 | LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) { |
| 980 | return wrap(unwrap<Function>(Func)->getSubprogram()); |
| 981 | } |
| 982 | |
| 983 | void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) { |
| 984 | unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP)); |
| 985 | } |