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" |
Duncan P. N. Exon Smith | c22a5c2 | 2015-02-21 00:43:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSwitch.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
| 22 | #include "llvm/IR/Constants.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DerivedTypes.h" |
| 25 | #include "llvm/IR/Instructions.h" |
| 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 28 | #include "llvm/IR/GVMaterializer.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | using namespace llvm; |
| 35 | using namespace llvm::dwarf; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // DIDescriptor |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Duncan P. N. Exon Smith | c22a5c2 | 2015-02-21 00:43:09 +0000 | [diff] [blame] | 41 | unsigned DIDescriptor::getFlag(StringRef Flag) { |
| 42 | return StringSwitch<unsigned>(Flag) |
| 43 | #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) |
| 44 | #include "llvm/IR/DebugInfoFlags.def" |
| 45 | .Default(0); |
| 46 | } |
| 47 | |
| 48 | const char *DIDescriptor::getFlagString(unsigned Flag) { |
| 49 | switch (Flag) { |
| 50 | default: |
| 51 | return ""; |
| 52 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 53 | case Flag##NAME: \ |
| 54 | return "DIFlag" #NAME; |
| 55 | #include "llvm/IR/DebugInfoFlags.def" |
| 56 | } |
| 57 | } |
| 58 | |
Duncan P. N. Exon Smith | 269e38d | 2015-02-21 00:45:26 +0000 | [diff] [blame] | 59 | unsigned DIDescriptor::splitFlags(unsigned Flags, |
| 60 | SmallVectorImpl<unsigned> &SplitFlags) { |
| 61 | // Accessibility flags need to be specially handled, since they're packed |
| 62 | // together. |
| 63 | if (unsigned A = Flags & FlagAccessibility) { |
| 64 | if (A == FlagPrivate) |
| 65 | SplitFlags.push_back(FlagPrivate); |
| 66 | else if (A == FlagProtected) |
| 67 | SplitFlags.push_back(FlagProtected); |
| 68 | else |
| 69 | SplitFlags.push_back(FlagPublic); |
| 70 | Flags &= ~A; |
| 71 | } |
| 72 | |
| 73 | #define HANDLE_DI_FLAG(ID, NAME) \ |
| 74 | if (unsigned Bit = Flags & ID) { \ |
| 75 | SplitFlags.push_back(Bit); \ |
| 76 | Flags &= ~Bit; \ |
| 77 | } |
| 78 | #include "llvm/IR/DebugInfoFlags.def" |
| 79 | |
| 80 | return Flags; |
| 81 | } |
| 82 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 83 | bool DIDescriptor::Verify() const { |
| 84 | return DbgNode && |
| 85 | (DIDerivedType(DbgNode).Verify() || |
| 86 | DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() || |
| 87 | DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() || |
| 88 | DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() || |
| 89 | DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() || |
| 90 | DILexicalBlock(DbgNode).Verify() || |
| 91 | DILexicalBlockFile(DbgNode).Verify() || |
| 92 | DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() || |
| 93 | DIObjCProperty(DbgNode).Verify() || |
| 94 | DITemplateTypeParameter(DbgNode).Verify() || |
| 95 | DITemplateValueParameter(DbgNode).Verify() || |
Duncan P. N. Exon Smith | e9d379c | 2015-03-16 21:03:55 +0000 | [diff] [blame] | 96 | DIImportedEntity(DbgNode).Verify()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 99 | static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 100 | if (!DbgNode || Elt >= DbgNode->getNumOperands()) |
| 101 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 102 | return DbgNode->getOperand(Elt); |
| 103 | } |
| 104 | |
| 105 | static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { |
| 106 | return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); |
| 107 | } |
| 108 | |
| 109 | static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) { |
| 110 | if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt))) |
| 111 | return MDS->getString(); |
| 112 | return StringRef(); |
| 113 | } |
| 114 | |
| 115 | StringRef DIDescriptor::getStringField(unsigned Elt) const { |
| 116 | return ::getStringField(DbgNode, Elt); |
| 117 | } |
| 118 | |
| 119 | uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 120 | if (auto *C = getConstantField(Elt)) |
| 121 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 122 | return CI->getZExtValue(); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int64_t DIDescriptor::getInt64Field(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 128 | if (auto *C = getConstantField(Elt)) |
| 129 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 130 | return CI->getZExtValue(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { |
| 136 | MDNode *Field = getNodeField(DbgNode, Elt); |
| 137 | return DIDescriptor(Field); |
| 138 | } |
| 139 | |
| 140 | GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 141 | return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | Constant *DIDescriptor::getConstantField(unsigned Elt) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 145 | if (!DbgNode) |
| 146 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 147 | |
| 148 | if (Elt < DbgNode->getNumOperands()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 149 | if (auto *C = |
| 150 | dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt))) |
| 151 | return C->getValue(); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 152 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | Function *DIDescriptor::getFunctionField(unsigned Elt) const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 156 | return dyn_cast_or_null<Function>(getConstantField(Elt)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 159 | /// \brief Return the size reported by the variable's type. |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 160 | unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) { |
| 161 | DIType Ty = getType().resolve(Map); |
| 162 | // Follow derived types until we reach a type that |
| 163 | // reports back a size. |
| 164 | while (Ty.isDerivedType() && !Ty.getSizeInBits()) { |
| 165 | DIDerivedType DT(&*Ty); |
| 166 | Ty = DT.getTypeDerivedFrom().resolve(Map); |
| 167 | } |
| 168 | assert(Ty.getSizeInBits() && "type with size 0"); |
| 169 | return Ty.getSizeInBits(); |
| 170 | } |
| 171 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 172 | bool DIExpression::isBitPiece() const { |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 173 | unsigned N = getNumElements(); |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 174 | return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 177 | uint64_t DIExpression::getBitPieceOffset() const { |
| 178 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 179 | return getElement(getNumElements()-2); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Adrian Prantl | 27bd01f | 2015-02-09 23:57:15 +0000 | [diff] [blame] | 182 | uint64_t DIExpression::getBitPieceSize() const { |
| 183 | assert(isBitPiece() && "not a piece"); |
Adrian Prantl | 34bcbee | 2015-01-21 00:59:20 +0000 | [diff] [blame] | 184 | return getElement(getNumElements()-1); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 185 | } |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 186 | |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 187 | DIExpression::iterator DIExpression::Operand::getNext() const { |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 188 | iterator it(I); |
Adrian Prantl | 0f61579 | 2015-03-04 17:39:33 +0000 | [diff] [blame] | 189 | return ++it; |
Adrian Prantl | 70f2a73 | 2015-01-23 23:40:47 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 192 | //===----------------------------------------------------------------------===// |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 193 | // Simple Descriptor Constructors and other Methods |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 196 | void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 197 | assert(DbgNode && "Trying to replace an unverified type!"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 198 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
| 199 | TempMDNode Temp(get()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 200 | |
| 201 | // Since we use a TrackingVH for the node, its easy for clients to manufacture |
| 202 | // legitimate situations where they want to replaceAllUsesWith() on something |
| 203 | // which, due to uniquing, has merged with the source. We shield clients from |
| 204 | // this detail by allowing a value to be replaced with replaceAllUsesWith() |
| 205 | // itself. |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 206 | if (Temp.get() == D.get()) { |
| 207 | DbgNode = MDNode::replaceWithUniqued(std::move(Temp)); |
| 208 | return; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 209 | } |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 210 | |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 211 | Temp->replaceAllUsesWith(D.get()); |
| 212 | DbgNode = D.get(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Frederic Riss | 36acf0f | 2014-09-15 07:50:36 +0000 | [diff] [blame] | 215 | void DIDescriptor::replaceAllUsesWith(MDNode *D) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 216 | assert(DbgNode && "Trying to replace an unverified type!"); |
David Blaikie | d3f094a | 2014-05-06 03:41:57 +0000 | [diff] [blame] | 217 | assert(DbgNode != D && "This replacement should always happen"); |
Duncan P. N. Exon Smith | 946fdcc | 2015-01-19 20:36:39 +0000 | [diff] [blame] | 218 | assert(DbgNode->isTemporary() && "Expected temporary node"); |
Duncan P. N. Exon Smith | 9c3b894 | 2015-02-28 23:48:02 +0000 | [diff] [blame] | 219 | TempMDNode Node(get()); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 220 | Node->replaceAllUsesWith(D); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Duncan P. N. Exon Smith | cd07efa1 | 2015-03-31 00:47:15 +0000 | [diff] [blame] | 223 | bool DICompileUnit::Verify() const { return isCompileUnit(); } |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 224 | bool DIObjCProperty::Verify() const { return isObjCProperty(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 225 | |
Duncan P. N. Exon Smith | 3b960c9 | 2015-03-31 01:47:55 +0000 | [diff] [blame] | 226 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 227 | /// \brief Check if a value can be a reference to a type. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 228 | static bool isTypeRef(const Metadata *MD) { |
| 229 | if (!MD) |
| 230 | return true; |
| 231 | if (auto *S = dyn_cast<MDString>(MD)) |
| 232 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 233 | return isa<MDType>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Duncan P. N. Exon Smith | 7f637a9 | 2014-10-15 17:01:28 +0000 | [diff] [blame] | 236 | /// \brief Check if a value can be a ScopeRef. |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 237 | static bool isScopeRef(const Metadata *MD) { |
| 238 | if (!MD) |
| 239 | return true; |
| 240 | if (auto *S = dyn_cast<MDString>(MD)) |
| 241 | return !S->getString().empty(); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 242 | return isa<MDScope>(MD); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 245 | /// \brief Check if a value can be a DescriptorRef. |
| 246 | static bool isDescriptorRef(const Metadata *MD) { |
| 247 | if (!MD) |
| 248 | return true; |
| 249 | if (auto *S = dyn_cast<MDString>(MD)) |
| 250 | return !S->getString().empty(); |
| 251 | return isa<MDNode>(MD); |
| 252 | } |
Duncan P. N. Exon Smith | e445014 | 2015-02-18 19:56:50 +0000 | [diff] [blame] | 253 | #endif |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 254 | |
Duncan P. N. Exon Smith | 85866b2a | 2015-03-31 01:28:58 +0000 | [diff] [blame] | 255 | bool DIType::Verify() const { return isType(); } |
Duncan P. N. Exon Smith | cd07efa1 | 2015-03-31 00:47:15 +0000 | [diff] [blame] | 256 | bool DIBasicType::Verify() const { return isBasicType(); } |
| 257 | bool DIDerivedType::Verify() const { return isDerivedType(); } |
Duncan P. N. Exon Smith | 85866b2a | 2015-03-31 01:28:58 +0000 | [diff] [blame] | 258 | bool DICompositeType::Verify() const { return isCompositeType(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 259 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 260 | bool DISubprogram::Verify() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 261 | auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 262 | if (!N) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 263 | return false; |
| 264 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 265 | // If a DISubprogram has an llvm::Function*, then scope chains from all |
| 266 | // instructions within the function should lead to this DISubprogram. |
| 267 | if (auto *F = getFunction()) { |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 268 | for (auto &BB : *F) { |
| 269 | for (auto &I : BB) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 270 | MDLocation *DL = I.getDebugLoc(); |
Duncan P. N. Exon Smith | 215e7ed | 2015-03-30 17:06:38 +0000 | [diff] [blame] | 271 | if (!DL) |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 272 | continue; |
| 273 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 274 | // walk the inlined-at scopes |
Duncan P. N. Exon Smith | 8f7bc79 | 2015-03-30 17:41:24 +0000 | [diff] [blame] | 275 | MDScope *Scope = DL->getInlinedAtScope(); |
Adrian Prantl | de200df | 2015-01-20 22:37:25 +0000 | [diff] [blame] | 276 | if (!Scope) |
| 277 | return false; |
Duncan P. N. Exon Smith | 215e7ed | 2015-03-30 17:06:38 +0000 | [diff] [blame] | 278 | while (!isa<MDSubprogram>(Scope)) { |
| 279 | Scope = cast<MDLexicalBlockBase>(Scope)->getScope(); |
Adrian Prantl | 1292e24 | 2015-01-21 18:32:56 +0000 | [diff] [blame] | 280 | if (!Scope) |
| 281 | return false; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 282 | } |
| 283 | if (!DISubprogram(Scope).describes(F)) |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 288 | |
Adrian Prantl | 9260cca | 2015-01-22 00:00:52 +0000 | [diff] [blame] | 289 | return true; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Duncan P. N. Exon Smith | 94d58f8 | 2015-03-31 01:28:22 +0000 | [diff] [blame] | 292 | bool DIGlobalVariable::Verify() const { return isGlobalVariable(); } |
| 293 | bool DIVariable::Verify() const { return isVariable(); } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 294 | |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 295 | bool DILocation::Verify() const { |
| 296 | return dyn_cast_or_null<MDLocation>(DbgNode); |
| 297 | } |
| 298 | bool DINameSpace::Verify() const { |
| 299 | return dyn_cast_or_null<MDNamespace>(DbgNode); |
| 300 | } |
| 301 | bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); } |
| 302 | bool DIEnumerator::Verify() const { |
| 303 | return dyn_cast_or_null<MDEnumerator>(DbgNode); |
| 304 | } |
| 305 | bool DISubrange::Verify() const { |
| 306 | return dyn_cast_or_null<MDSubrange>(DbgNode); |
| 307 | } |
| 308 | bool DILexicalBlock::Verify() const { |
| 309 | return dyn_cast_or_null<MDLexicalBlock>(DbgNode); |
| 310 | } |
| 311 | bool DILexicalBlockFile::Verify() const { |
| 312 | return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode); |
| 313 | } |
| 314 | bool DITemplateTypeParameter::Verify() const { |
| 315 | return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode); |
| 316 | } |
| 317 | bool DITemplateValueParameter::Verify() const { |
| 318 | return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode); |
| 319 | } |
| 320 | bool DIImportedEntity::Verify() const { |
| 321 | return dyn_cast_or_null<MDImportedEntity>(DbgNode); |
| 322 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 323 | |
Manman Ren | 1a125c9 | 2014-07-28 19:33:20 +0000 | [diff] [blame] | 324 | void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 325 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 326 | if (Elements) |
| 327 | N->replaceElements(cast<MDTuple>(Elements)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 328 | if (TParams) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 329 | N->replaceTemplateParams(cast<MDTuple>(TParams)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 330 | DbgNode = N; |
| 331 | } |
| 332 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 333 | DIScopeRef DIScope::getRef() const { |
| 334 | if (!isCompositeType()) |
| 335 | return DIScopeRef(*this); |
| 336 | DICompositeType DTy(DbgNode); |
| 337 | if (!DTy.getIdentifier()) |
| 338 | return DIScopeRef(*this); |
| 339 | return DIScopeRef(DTy.getIdentifier()); |
| 340 | } |
| 341 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 342 | void DICompositeType::setContainingType(DICompositeType ContainingType) { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 343 | TypedTrackingMDRef<MDCompositeTypeBase> N(get()); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 344 | N->replaceVTableHolder(ContainingType.getRef()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 345 | DbgNode = N; |
| 346 | } |
| 347 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 348 | bool DIVariable::isInlinedFnArgument(const Function *CurFn) { |
| 349 | assert(CurFn && "Invalid function"); |
| 350 | if (!getContext().isSubprogram()) |
| 351 | return false; |
| 352 | // This variable is not inlined function argument if its scope |
| 353 | // does not describe current function. |
| 354 | return !DISubprogram(getContext()).describes(CurFn); |
| 355 | } |
| 356 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 357 | Function *DISubprogram::getFunction() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 358 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 359 | if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction())) |
| 360 | return dyn_cast<Function>(C->getValue()); |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 364 | bool DISubprogram::describes(const Function *F) { |
| 365 | assert(F && "Invalid function"); |
| 366 | if (F == getFunction()) |
| 367 | return true; |
| 368 | StringRef Name = getLinkageName(); |
| 369 | if (Name.empty()) |
| 370 | Name = getName(); |
| 371 | if (F->getName() == Name) |
| 372 | return true; |
| 373 | return false; |
| 374 | } |
| 375 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 376 | GlobalVariable *DIGlobalVariable::getGlobal() const { |
| 377 | return dyn_cast_or_null<GlobalVariable>(getConstant()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 380 | DIScopeRef DIScope::getContext() const { |
| 381 | |
| 382 | if (isType()) |
| 383 | return DIType(DbgNode).getContext(); |
| 384 | |
| 385 | if (isSubprogram()) |
| 386 | return DIScopeRef(DISubprogram(DbgNode).getContext()); |
| 387 | |
| 388 | if (isLexicalBlock()) |
| 389 | return DIScopeRef(DILexicalBlock(DbgNode).getContext()); |
| 390 | |
| 391 | if (isLexicalBlockFile()) |
| 392 | return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); |
| 393 | |
| 394 | if (isNameSpace()) |
| 395 | return DIScopeRef(DINameSpace(DbgNode).getContext()); |
| 396 | |
| 397 | assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 398 | return DIScopeRef(nullptr); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 401 | StringRef DIScope::getName() const { |
| 402 | if (isType()) |
| 403 | return DIType(DbgNode).getName(); |
| 404 | if (isSubprogram()) |
| 405 | return DISubprogram(DbgNode).getName(); |
| 406 | if (isNameSpace()) |
| 407 | return DINameSpace(DbgNode).getName(); |
| 408 | assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || |
| 409 | isCompileUnit()) && |
| 410 | "Unhandled type of scope."); |
| 411 | return StringRef(); |
| 412 | } |
| 413 | |
| 414 | StringRef DIScope::getFilename() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 415 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 416 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0); |
| 417 | return ""; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | StringRef DIScope::getDirectory() const { |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 421 | if (auto *N = get()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 422 | return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1); |
| 423 | return ""; |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void DICompileUnit::replaceSubprograms(DIArray Subprograms) { |
| 427 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 428 | get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get())); |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { |
| 432 | assert(Verify() && "Expected compile unit"); |
Duncan P. N. Exon Smith | 9b9cc2d | 2015-03-23 21:54:07 +0000 | [diff] [blame] | 433 | get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get())); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 436 | DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, |
David Blaikie | 2f3f76f | 2014-08-21 22:45:21 +0000 | [diff] [blame] | 437 | DILexicalBlockFile NewScope) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 438 | assert(Verify()); |
Duncan P. N. Exon Smith | 9885469 | 2015-01-14 22:27:36 +0000 | [diff] [blame] | 439 | assert(NewScope && "Expected valid scope"); |
| 440 | |
| 441 | const auto *Old = cast<MDLocation>(DbgNode); |
| 442 | return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), |
| 443 | NewScope, Old->getInlinedAt())); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 446 | unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { |
| 447 | std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); |
| 448 | return ++Ctx.pImpl->DiscriminatorTable[Key]; |
| 449 | } |
| 450 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 451 | DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, |
| 452 | LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 453 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 454 | return cast<MDLocalVariable>(DV) |
| 455 | ->withInline(cast_or_null<MDLocation>(InlinedScope)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 458 | DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { |
Duncan P. N. Exon Smith | 176b691 | 2014-10-03 20:01:09 +0000 | [diff] [blame] | 459 | assert(DIVariable(DV).Verify() && "Expected a DIVariable"); |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 460 | return cast<MDLocalVariable>(DV)->withoutInline(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 463 | DISubprogram llvm::getDISubprogram(const MDNode *Scope) { |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame^] | 464 | if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope)) |
| 465 | return LocalScope->getSubprogram(); |
| 466 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 469 | DISubprogram llvm::getDISubprogram(const Function *F) { |
| 470 | // 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] | 471 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 472 | 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] | 473 | return Inst.getDebugLoc(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 474 | }); |
| 475 | if (Inst == BB.end()) |
| 476 | continue; |
| 477 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 478 | const MDNode *Scope = DLoc.getInlinedAtScope(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 479 | DISubprogram Subprogram = getDISubprogram(Scope); |
| 480 | return Subprogram.describes(F) ? Subprogram : DISubprogram(); |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return DISubprogram(); |
| 484 | } |
| 485 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 486 | DICompositeType llvm::getDICompositeType(DIType T) { |
| 487 | if (T.isCompositeType()) |
| 488 | return DICompositeType(T); |
| 489 | |
| 490 | if (T.isDerivedType()) { |
| 491 | // This function is currently used by dragonegg and dragonegg does |
| 492 | // not generate identifier for types, so using an empty map to resolve |
| 493 | // DerivedFrom should be fine. |
| 494 | DITypeIdentifierMap EmptyMap; |
| 495 | return getDICompositeType( |
| 496 | DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); |
| 497 | } |
| 498 | |
| 499 | return DICompositeType(); |
| 500 | } |
| 501 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 502 | DITypeIdentifierMap |
| 503 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 504 | DITypeIdentifierMap Map; |
| 505 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 506 | DICompileUnit CU(CU_Nodes->getOperand(CUi)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 507 | DIArray Retain = CU.getRetainedTypes(); |
| 508 | for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { |
| 509 | if (!Retain.getElement(Ti).isCompositeType()) |
| 510 | continue; |
| 511 | DICompositeType Ty(Retain.getElement(Ti)); |
| 512 | if (MDString *TypeId = Ty.getIdentifier()) { |
| 513 | // Definition has priority over declaration. |
| 514 | // Try to insert (TypeId, Ty) to Map. |
| 515 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 516 | Map.insert(std::make_pair(TypeId, Ty)); |
| 517 | // If TypeId already exists in Map and this is a definition, replace |
| 518 | // whatever we had (declaration or definition) with the definition. |
| 519 | if (!P.second && !Ty.isForwardDecl()) |
| 520 | P.first->second = Ty; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return Map; |
| 525 | } |
| 526 | |
| 527 | //===----------------------------------------------------------------------===// |
| 528 | // DebugInfoFinder implementations. |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | |
| 531 | void DebugInfoFinder::reset() { |
| 532 | CUs.clear(); |
| 533 | SPs.clear(); |
| 534 | GVs.clear(); |
| 535 | TYs.clear(); |
| 536 | Scopes.clear(); |
| 537 | NodesSeen.clear(); |
| 538 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 539 | TypeMapInitialized = false; |
| 540 | } |
| 541 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 542 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 543 | if (!TypeMapInitialized) |
| 544 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 545 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 546 | TypeMapInitialized = true; |
| 547 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 550 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 551 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 552 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 553 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 554 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 555 | addCompileUnit(CU); |
| 556 | DIArray GVs = CU.getGlobalVariables(); |
| 557 | for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { |
| 558 | DIGlobalVariable DIG(GVs.getElement(i)); |
| 559 | if (addGlobalVariable(DIG)) { |
Manman Ren | f0a582b | 2014-11-21 19:55:23 +0000 | [diff] [blame] | 560 | processScope(DIG.getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 561 | processType(DIG.getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | DIArray SPs = CU.getSubprograms(); |
| 565 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) |
| 566 | processSubprogram(DISubprogram(SPs.getElement(i))); |
| 567 | DIArray EnumTypes = CU.getEnumTypes(); |
| 568 | for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) |
| 569 | processType(DIType(EnumTypes.getElement(i))); |
| 570 | DIArray RetainedTypes = CU.getRetainedTypes(); |
| 571 | for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) |
| 572 | processType(DIType(RetainedTypes.getElement(i))); |
| 573 | DIArray Imports = CU.getImportedEntities(); |
| 574 | for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { |
| 575 | DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); |
Duncan P. N. Exon Smith | d4e07c9 | 2015-03-20 19:13:53 +0000 | [diff] [blame] | 576 | if (!Import) |
| 577 | continue; |
Adrian Prantl | d09ba23 | 2014-04-01 03:41:04 +0000 | [diff] [blame] | 578 | DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 579 | if (Entity.isType()) |
| 580 | processType(DIType(Entity)); |
| 581 | else if (Entity.isSubprogram()) |
| 582 | processSubprogram(DISubprogram(Entity)); |
| 583 | else if (Entity.isNameSpace()) |
| 584 | processScope(DINameSpace(Entity).getContext()); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 590 | void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 591 | if (!Loc) |
| 592 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 593 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 594 | processScope(Loc.getScope()); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 595 | processLocation(M, Loc.getOrigLocation()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 598 | void DebugInfoFinder::processType(DIType DT) { |
| 599 | if (!addType(DT)) |
| 600 | return; |
| 601 | processScope(DT.getContext().resolve(TypeIdentifierMap)); |
| 602 | if (DT.isCompositeType()) { |
| 603 | DICompositeType DCT(DT); |
| 604 | processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
Manman Ren | f8a1967 | 2014-07-28 22:24:06 +0000 | [diff] [blame] | 605 | if (DT.isSubroutineType()) { |
| 606 | DITypeArray DTA = DISubroutineType(DT).getTypeArray(); |
| 607 | for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i) |
| 608 | processType(DTA.getElement(i).resolve(TypeIdentifierMap)); |
| 609 | return; |
| 610 | } |
Manman Ren | ab8ffba | 2014-07-28 19:14:13 +0000 | [diff] [blame] | 611 | DIArray DA = DCT.getElements(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 612 | for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { |
| 613 | DIDescriptor D = DA.getElement(i); |
| 614 | if (D.isType()) |
| 615 | processType(DIType(D)); |
| 616 | else if (D.isSubprogram()) |
| 617 | processSubprogram(DISubprogram(D)); |
| 618 | } |
| 619 | } else if (DT.isDerivedType()) { |
| 620 | DIDerivedType DDT(DT); |
| 621 | processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | void DebugInfoFinder::processScope(DIScope Scope) { |
| 626 | if (Scope.isType()) { |
| 627 | DIType Ty(Scope); |
| 628 | processType(Ty); |
| 629 | return; |
| 630 | } |
| 631 | if (Scope.isCompileUnit()) { |
| 632 | addCompileUnit(DICompileUnit(Scope)); |
| 633 | return; |
| 634 | } |
| 635 | if (Scope.isSubprogram()) { |
| 636 | processSubprogram(DISubprogram(Scope)); |
| 637 | return; |
| 638 | } |
| 639 | if (!addScope(Scope)) |
| 640 | return; |
| 641 | if (Scope.isLexicalBlock()) { |
| 642 | DILexicalBlock LB(Scope); |
| 643 | processScope(LB.getContext()); |
| 644 | } else if (Scope.isLexicalBlockFile()) { |
| 645 | DILexicalBlockFile LBF = DILexicalBlockFile(Scope); |
| 646 | processScope(LBF.getScope()); |
| 647 | } else if (Scope.isNameSpace()) { |
| 648 | DINameSpace NS(Scope); |
| 649 | processScope(NS.getContext()); |
| 650 | } |
| 651 | } |
| 652 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 653 | void DebugInfoFinder::processSubprogram(DISubprogram SP) { |
| 654 | if (!addSubprogram(SP)) |
| 655 | return; |
| 656 | processScope(SP.getContext().resolve(TypeIdentifierMap)); |
| 657 | processType(SP.getType()); |
| 658 | DIArray TParams = SP.getTemplateParams(); |
| 659 | for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { |
| 660 | DIDescriptor Element = TParams.getElement(I); |
| 661 | if (Element.isTemplateTypeParameter()) { |
| 662 | DITemplateTypeParameter TType(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 663 | processType(TType.getType().resolve(TypeIdentifierMap)); |
| 664 | } else if (Element.isTemplateValueParameter()) { |
| 665 | DITemplateValueParameter TVal(Element); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 666 | processType(TVal.getType().resolve(TypeIdentifierMap)); |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 671 | void DebugInfoFinder::processDeclare(const Module &M, |
| 672 | const DbgDeclareInst *DDI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 673 | MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); |
| 674 | if (!N) |
| 675 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 676 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 677 | |
| 678 | DIDescriptor DV(N); |
| 679 | if (!DV.isVariable()) |
| 680 | return; |
| 681 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 682 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 683 | return; |
| 684 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 685 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 688 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 689 | MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); |
| 690 | if (!N) |
| 691 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 692 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 693 | |
| 694 | DIDescriptor DV(N); |
| 695 | if (!DV.isVariable()) |
| 696 | return; |
| 697 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 698 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 699 | return; |
| 700 | processScope(DIVariable(N).getContext()); |
Adrian Prantl | 1a1647c | 2014-03-18 02:34:58 +0000 | [diff] [blame] | 701 | processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 704 | bool DebugInfoFinder::addType(DIType DT) { |
| 705 | if (!DT) |
| 706 | return false; |
| 707 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 708 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 709 | return false; |
| 710 | |
| 711 | TYs.push_back(DT); |
| 712 | return true; |
| 713 | } |
| 714 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 715 | bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { |
| 716 | if (!CU) |
| 717 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 718 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 719 | return false; |
| 720 | |
| 721 | CUs.push_back(CU); |
| 722 | return true; |
| 723 | } |
| 724 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 725 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { |
| 726 | if (!DIG) |
| 727 | return false; |
| 728 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 729 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 730 | return false; |
| 731 | |
| 732 | GVs.push_back(DIG); |
| 733 | return true; |
| 734 | } |
| 735 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 736 | bool DebugInfoFinder::addSubprogram(DISubprogram SP) { |
| 737 | if (!SP) |
| 738 | return false; |
| 739 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 740 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 741 | return false; |
| 742 | |
| 743 | SPs.push_back(SP); |
| 744 | return true; |
| 745 | } |
| 746 | |
| 747 | bool DebugInfoFinder::addScope(DIScope Scope) { |
| 748 | if (!Scope) |
| 749 | return false; |
| 750 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 751 | // as null for now. |
| 752 | if (Scope->getNumOperands() == 0) |
| 753 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 754 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 755 | return false; |
| 756 | Scopes.push_back(Scope); |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | //===----------------------------------------------------------------------===// |
| 761 | // DIDescriptor: dump routines for all descriptors. |
| 762 | //===----------------------------------------------------------------------===// |
| 763 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 764 | void DIDescriptor::dump() const { |
| 765 | print(dbgs()); |
| 766 | dbgs() << '\n'; |
| 767 | } |
| 768 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 769 | void DIDescriptor::print(raw_ostream &OS) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 770 | if (!get()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 771 | return; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 772 | get()->print(OS); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, |
| 776 | const LLVMContext &Ctx) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 777 | if (!DL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 778 | return; |
| 779 | |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 780 | DIScope Scope(DL.getScope()); |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 781 | assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); |
| 782 | // Omit the directory, because it's likely to be long and uninteresting. |
| 783 | CommentOS << Scope.getFilename(); |
| 784 | CommentOS << ':' << DL.getLine(); |
| 785 | if (DL.getCol() != 0) |
| 786 | CommentOS << ':' << DL.getCol(); |
| 787 | |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 788 | DebugLoc InlinedAtDL = DL.getInlinedAt(); |
| 789 | if (!InlinedAtDL) |
Duncan P. N. Exon Smith | 51306ef | 2015-03-30 18:45:11 +0000 | [diff] [blame] | 790 | return; |
| 791 | |
| 792 | CommentOS << " @[ "; |
| 793 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 794 | CommentOS << " ]"; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | void DIVariable::printExtendedName(raw_ostream &OS) const { |
| 798 | const LLVMContext &Ctx = DbgNode->getContext(); |
| 799 | StringRef Res = getName(); |
| 800 | if (!Res.empty()) |
| 801 | OS << Res << "," << getLineNumber(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 802 | if (auto *InlinedAt = get()->getInlinedAt()) { |
| 803 | if (DebugLoc InlinedAtDL = InlinedAt) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 804 | OS << " @["; |
| 805 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 806 | OS << "]"; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 811 | template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) { |
| 812 | assert(isDescriptorRef(V) && |
| 813 | "DIDescriptorRef should be a MDString or MDNode"); |
| 814 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 815 | template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 816 | assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); |
| 817 | } |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 818 | template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 819 | assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); |
| 820 | } |
| 821 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 822 | template <> |
Duncan P. N. Exon Smith | 2a78e9b | 2015-02-18 19:39:36 +0000 | [diff] [blame] | 823 | DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const { |
| 824 | return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
| 825 | } |
| 826 | template <> |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 827 | DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 828 | return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 829 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 830 | template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { |
Duncan P. N. Exon Smith | c81307a | 2014-11-14 23:55:03 +0000 | [diff] [blame] | 831 | return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt))); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 832 | } |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 833 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 834 | bool llvm::stripDebugInfo(Function &F) { |
| 835 | bool Changed = false; |
| 836 | for (BasicBlock &BB : F) { |
| 837 | for (Instruction &I : BB) { |
| 838 | if (I.getDebugLoc()) { |
| 839 | Changed = true; |
| 840 | I.setDebugLoc(DebugLoc()); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | return Changed; |
| 845 | } |
| 846 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 847 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 848 | bool Changed = false; |
| 849 | |
| 850 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 851 | // the module. |
| 852 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 853 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 854 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 855 | CI->eraseFromParent(); |
| 856 | } |
| 857 | Declare->eraseFromParent(); |
| 858 | Changed = true; |
| 859 | } |
| 860 | |
| 861 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 862 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 863 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 864 | CI->eraseFromParent(); |
| 865 | } |
| 866 | DbgVal->eraseFromParent(); |
| 867 | Changed = true; |
| 868 | } |
| 869 | |
| 870 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 871 | NME = M.named_metadata_end(); NMI != NME;) { |
| 872 | NamedMDNode *NMD = NMI; |
| 873 | ++NMI; |
| 874 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 875 | NMD->eraseFromParent(); |
| 876 | Changed = true; |
| 877 | } |
| 878 | } |
| 879 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 880 | for (Function &F : M) |
| 881 | Changed |= stripDebugInfo(F); |
| 882 | |
| 883 | if ( GVMaterializer *Materializer = M.getMaterializer()) |
| 884 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 885 | |
| 886 | return Changed; |
| 887 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 888 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 889 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 890 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 891 | M.getModuleFlag("Debug Info Version"))) |
| 892 | return Val->getZExtValue(); |
| 893 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 894 | } |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 895 | |
David Blaikie | a8c3509 | 2014-07-02 18:30:05 +0000 | [diff] [blame] | 896 | llvm::DenseMap<const llvm::Function *, llvm::DISubprogram> |
| 897 | llvm::makeSubprogramMap(const Module &M) { |
| 898 | DenseMap<const Function *, DISubprogram> R; |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 899 | |
| 900 | NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu"); |
| 901 | if (!CU_Nodes) |
| 902 | return R; |
| 903 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 904 | for (MDNode *N : CU_Nodes->operands()) { |
| 905 | DICompileUnit CUNode(N); |
David Blaikie | 6876b3b | 2014-07-01 20:05:26 +0000 | [diff] [blame] | 906 | DIArray SPs = CUNode.getSubprograms(); |
| 907 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 908 | DISubprogram SP(SPs.getElement(i)); |
| 909 | if (Function *F = SP.getFunction()) |
| 910 | R.insert(std::make_pair(F, SP)); |
| 911 | } |
| 912 | } |
| 913 | return R; |
| 914 | } |