Move the complex address expression out of DIVariable and into an extra
argument of the llvm.dbg.declare/llvm.dbg.value intrinsics.
Previously, DIVariable was a variable-length field that has an optional
reference to a Metadata array consisting of a variable number of
complex address expressions. In the case of OpPiece expressions this is
wasting a lot of storage in IR, because when an aggregate type is, e.g.,
SROA'd into all of its n individual members, the IR will contain n copies
of the DIVariable, all alike, only differing in the complex address
reference at the end.
By making the complex address into an extra argument of the
dbg.value/dbg.declare intrinsics, all of the pieces can reference the
same variable and the complex address expressions can be uniqued across
the CU, too.
Down the road, this will allow us to move other flags, such as
"indirection" out of the DIVariable, too.
The new intrinsics look like this:
declare void @llvm.dbg.declare(metadata %storage, metadata %var, metadata %expr)
declare void @llvm.dbg.value(metadata %storage, i64 %offset, metadata %var, metadata %expr)
This patch adds a new LLVM-local tag to DIExpressions, so we can detect
and pretty-print DIExpression metadata nodes.
What this patch doesn't do:
This patch does not touch the "Indirect" field in DIVariable; but moving
that into the expression would be a natural next step.
http://reviews.llvm.org/D4919
rdar://problem/17994491
Thanks to dblaikie and dexonsmith for reviewing this patch!
llvm-svn: 218778
diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index 4274d82..4e36a8d 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -49,7 +49,7 @@
DIObjCProperty(DbgNode).Verify() ||
DITemplateTypeParameter(DbgNode).Verify() ||
DITemplateValueParameter(DbgNode).Verify() ||
- DIImportedEntity(DbgNode).Verify());
+ DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
}
static Value *getField(const MDNode *DbgNode, unsigned Elt) {
@@ -138,33 +138,9 @@
}
}
-uint64_t DIVariable::getAddrElement(unsigned Idx) const {
- DIDescriptor ComplexExpr = getDescriptorField(8);
- if (Idx < ComplexExpr->getNumOperands())
- if (auto *CI = dyn_cast_or_null<ConstantInt>(ComplexExpr->getOperand(Idx)))
- return CI->getZExtValue();
-
- assert(false && "non-existing complex address element requested");
- return 0;
-}
-
/// getInlinedAt - If this variable is inlined then return inline location.
MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
-bool DIVariable::isVariablePiece() const {
- return hasComplexAddress() && getAddrElement(0) == DIBuilder::OpPiece;
-}
-
-uint64_t DIVariable::getPieceOffset() const {
- assert(isVariablePiece());
- return getAddrElement(1);
-}
-
-uint64_t DIVariable::getPieceSize() const {
- assert(isVariablePiece());
- return getAddrElement(2);
-}
-
/// Return the size reported by the variable's type.
unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
DIType Ty = getType().resolve(Map);
@@ -178,8 +154,29 @@
return Ty.getSizeInBits();
}
+uint64_t DIExpression::getElement(unsigned Idx) const {
+ unsigned I = Idx + 1;
+ if (I < DbgNode->getNumOperands())
+ if (auto *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(I)))
+ return CI->getZExtValue();
+ assert(false && "non-existing complex address element requested");
+ return 0;
+}
+bool DIExpression::isVariablePiece() const {
+ return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
+}
+
+uint64_t DIExpression::getPieceOffset() const {
+ assert(isVariablePiece());
+ return getElement(1);
+}
+
+uint64_t DIExpression::getPieceSize() const {
+ assert(isVariablePiece());
+ return getElement(2);
+}
//===----------------------------------------------------------------------===//
// Predicates
@@ -357,6 +354,12 @@
getTag() == dwarf::DW_TAG_imported_declaration);
}
+/// \brief Return true if the specified tag is DW_TAG_imported_module or
+/// DW_TAG_imported_declaration.
+bool DIDescriptor::isExpression() const {
+ return DbgNode && (getTag() == dwarf::DW_TAG_expression);
+}
+
//===----------------------------------------------------------------------===//
// Simple Descriptor Constructors and other Methods
//===----------------------------------------------------------------------===//
@@ -596,14 +599,16 @@
if (!fieldIsTypeRef(DbgNode, 5))
return false;
- // Variable without a complex expression.
- if (DbgNode->getNumOperands() == 8)
+ // Variable without an inline location.
+ if (DbgNode->getNumOperands() == 7)
return true;
- // Make sure the complex expression is an MDNode.
- return (DbgNode->getNumOperands() == 9 && fieldIsMDNode(DbgNode, 8));
+ return DbgNode->getNumOperands() == 8;
}
+/// Verify - Verify that a variable descriptor is well formed.
+bool DIExpression::Verify() const { return isExpression(); }
+
/// Verify - Verify that a location descriptor is well formed.
bool DILocation::Verify() const {
if (!DbgNode)
@@ -936,19 +941,6 @@
return DIVariable(MDNode::get(VMContext, Elts));
}
-
-/// getEntireVariable - Remove OpPiece exprs from the variable.
-DIVariable llvm::getEntireVariable(DIVariable DV) {
- if (!DV.isVariablePiece())
- return DV;
-
- SmallVector<Value *, 8> Elts;
- for (unsigned i = 0; i < 8; ++i)
- Elts.push_back(DV->getOperand(i));
-
- return DIVariable(MDNode::get(DV->getContext(), Elts));
-}
-
/// getDISubprogram - Find subprogram that is enclosing this scope.
DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
DIDescriptor D(Scope);
@@ -1293,6 +1285,8 @@
DINameSpace(DbgNode).printInternal(OS);
} else if (this->isScope()) {
DIScope(DbgNode).printInternal(OS);
+ } else if (this->isExpression()) {
+ DIExpression(DbgNode).printInternal(OS);
}
}
@@ -1442,10 +1436,28 @@
OS << " [" << Res << ']';
OS << " [line " << getLineNumber() << ']';
+}
- if (isVariablePiece())
- OS << " [piece, size " << getPieceSize()
- << ", offset " << getPieceOffset() << ']';
+void DIExpression::printInternal(raw_ostream &OS) const {
+ for (unsigned I = 0; I < getNumElements(); ++I) {
+ uint64_t OpCode = getElement(I);
+ OS << " [" << OperationEncodingString(OpCode);
+ switch (OpCode) {
+ case DW_OP_plus: {
+ OS << " " << getElement(++I);
+ break;
+ }
+ case DW_OP_piece: {
+ unsigned Offset = getElement(++I);
+ unsigned Size = getElement(++I);
+ OS << " offset=" << Offset << ", size= " << Size;
+ break;
+ }
+ default:
+ break;
+ }
+ OS << "]";
+ }
}
void DIObjCProperty::printInternal(raw_ostream &OS) const {