Skip to content

Commit

Permalink
Move the complex address expression out of DIVariable and into an extra
Browse files Browse the repository at this point in the history
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!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218778 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
adrian-prantl committed Oct 1, 2014
1 parent 56077f5 commit 076fd5d
Show file tree
Hide file tree
Showing 257 changed files with 1,530 additions and 1,378 deletions.
17 changes: 15 additions & 2 deletions docs/SourceLevelDebugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ Local variables
metadata, ;; Reference to the type descriptor
i32, ;; flags
metadata ;; (optional) Reference to inline location
metadata ;; (optional) Reference to a complex expression (see below)
metadata ;; (optional) Reference to a complex expression.
}
These descriptors are used to define variables local to a sub program. The
Expand All @@ -590,7 +590,20 @@ The context is either the subprogram or block where the variable is defined.
Name the source variable name. Context and line indicate where the variable
was defined. Type descriptor defines the declared type of the variable.

The ``OpPiece`` operator is used for (typically larger aggregate)
Complex Expressions
^^^^^^^^^^^^^^^^^^^
.. code-block:: llvm
!8 = metadata !{
i32, ;; DW_TAG_expression
...
}
Complex expressions describe variable storage locations in terms of
prefix-notated DWARF expressions. Currently the only supported
operators are ``DW_OP_plus``, ``DW_OP_deref``, and ``DW_OP_piece``.

The ``DW_OP_piece`` operator is used for (typically larger aggregate)
variables that are fragmented across several locations. It takes two
i32 arguments, an offset and a size in bytes to describe which piece
of the variable is at this location.
Expand Down
16 changes: 13 additions & 3 deletions include/llvm/CodeGen/MachineInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,22 @@ class MachineInstr : public ilist_node<MachineInstr> {
///
DebugLoc getDebugLoc() const { return debugLoc; }

/// getDebugVariable() - Return the debug variable referenced by
/// \brief Return the debug variable referenced by
/// this DBG_VALUE instruction.
DIVariable getDebugVariable() const {
assert(isDebugValue() && "not a DBG_VALUE");
const MDNode *Var = getOperand(getNumOperands() - 1).getMetadata();
return DIVariable(Var);
DIVariable Var(getOperand(2).getMetadata());
assert(Var.Verify() && "not a DIVariable");
return Var;
}

/// \brief Return the complex address expression referenced by
/// this DBG_VALUE instruction.
DIExpression getDebugExpression() const {
assert(isDebugValue() && "not a DBG_VALUE");
DIExpression Expr(getOperand(3).getMetadata());
assert(Expr.Verify() && "not a DIExpression");
return Expr;
}

/// emitError - Emit an error referring to the source location of this
Expand Down
45 changes: 24 additions & 21 deletions include/llvm/CodeGen/MachineInstrBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class MachineInstrBuilder {

const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
assert((MI->isDebugValue() ? MI->getDebugVariable().Verify() : true) &&
"first MDNode argument of a DBG_VALUE not a DIVariable");
return *this;
}

Expand Down Expand Up @@ -345,24 +347,25 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
/// address. The convention is that a DBG_VALUE is indirect iff the
/// second operand is an immediate.
///
inline MachineInstrBuilder BuildMI(MachineFunction &MF,
DebugLoc DL,
const MCInstrDesc &MCID,
bool IsIndirect,
unsigned Reg,
unsigned Offset,
const MDNode *MD) {
inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
const MCInstrDesc &MCID, bool IsIndirect,
unsigned Reg, unsigned Offset,
const MDNode *Variable, const MDNode *Expr) {
assert(DIVariable(Variable).Verify() && "not a DIVariable");
assert(DIExpression(Expr).Verify() && "not a DIExpression");
if (IsIndirect)
return BuildMI(MF, DL, MCID)
.addReg(Reg, RegState::Debug)
.addImm(Offset)
.addMetadata(MD);
.addReg(Reg, RegState::Debug)
.addImm(Offset)
.addMetadata(Variable)
.addMetadata(Expr);
else {
assert(Offset == 0 && "A direct address cannot have an offset.");
return BuildMI(MF, DL, MCID)
.addReg(Reg, RegState::Debug)
.addReg(0U, RegState::Debug)
.addMetadata(MD);
.addReg(Reg, RegState::Debug)
.addReg(0U, RegState::Debug)
.addMetadata(Variable)
.addMetadata(Expr);
}
}

Expand All @@ -371,15 +374,15 @@ inline MachineInstrBuilder BuildMI(MachineFunction &MF,
/// address and inserts it at position I.
///
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
MachineBasicBlock::iterator I,
DebugLoc DL,
const MCInstrDesc &MCID,
bool IsIndirect,
unsigned Reg,
unsigned Offset,
const MDNode *MD) {
MachineBasicBlock::iterator I, DebugLoc DL,
const MCInstrDesc &MCID, bool IsIndirect,
unsigned Reg, unsigned Offset,
const MDNode *Variable, const MDNode *Expr) {
assert(DIVariable(Variable).Verify() && "not a DIVariable");
assert(DIExpression(Expr).Verify() && "not a DIExpression");
MachineFunction &MF = *BB.getParent();
MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, MD);
MachineInstr *MI =
BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
BB.insert(I, MI);
return MachineInstrBuilder(MF, MI);
}
Expand Down
6 changes: 4 additions & 2 deletions include/llvm/CodeGen/MachineModuleInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class MachineModuleInfo : public ImmutablePass {

struct VariableDbgInfo {
TrackingVH<MDNode> Var;
TrackingVH<MDNode> Expr;
unsigned Slot;
DebugLoc Loc;
};
Expand Down Expand Up @@ -390,8 +391,9 @@ class MachineModuleInfo : public ImmutablePass {

/// setVariableDbgInfo - Collect information used to emit debugging
/// information of a variable.
void setVariableDbgInfo(MDNode *N, unsigned Slot, DebugLoc Loc) {
VariableDbgInfo Info = { N, Slot, Loc };
void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot,
DebugLoc Loc) {
VariableDbgInfo Info = {Var, Expr, Slot, Loc};
VariableDbgInfos.push_back(std::move(Info));
}

Expand Down
21 changes: 12 additions & 9 deletions include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,15 +984,18 @@ class SelectionDAG {

/// getDbgValue - Creates a SDDbgValue node.
///
SDDbgValue *getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
bool IsIndirect, uint64_t Off,
DebugLoc DL, unsigned O);
/// Constant.
SDDbgValue *getConstantDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
DebugLoc DL, unsigned O);
/// Frame index.
SDDbgValue *getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
DebugLoc DL, unsigned O);
/// SDNode
SDDbgValue *getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N, unsigned R,
bool IsIndirect, uint64_t Off, DebugLoc DL,
unsigned O);

/// Constant
SDDbgValue *getConstantDbgValue(MDNode *Var, MDNode *Expr, const Value *C,
uint64_t Off, DebugLoc DL, unsigned O);

/// FrameIndex
SDDbgValue *getFrameIndexDbgValue(MDNode *Var, MDNode *Expr, unsigned FI,
uint64_t Off, DebugLoc DL, unsigned O);

/// RemoveDeadNode - Remove the specified node from the system. If any of its
/// operands then becomes dead, remove them as well. Inform UpdateListener
Expand Down
39 changes: 13 additions & 26 deletions include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ namespace llvm {

public:
explicit DIBuilder(Module &M);
enum ComplexAddrKind { OpPlus=1, OpDeref, OpPiece };
enum DebugEmissionKind { FullDebug=1, LineTablesOnly };

/// finalize - Construct any deferred debug info descriptors.
Expand Down Expand Up @@ -501,33 +500,18 @@ namespace llvm {
unsigned Flags = 0,
unsigned ArgNo = 0);


/// createComplexVariable - Create a new descriptor for the specified
/// createExpression - Create a new descriptor for the specified
/// variable which has a complex address expression for its address.
/// @param Tag Dwarf TAG. Usually DW_TAG_auto_variable or
/// DW_TAG_arg_variable.
/// @param Scope Variable scope.
/// @param Name Variable name.
/// @param F File where this variable is defined.
/// @param LineNo Line number.
/// @param Ty Variable Type
/// @param Addr An array of complex address operations.
/// @param ArgNo If this variable is an argument then this argument's
/// number. 1 indicates 1st argument.
DIVariable createComplexVariable(unsigned Tag, DIDescriptor Scope,
StringRef Name, DIFile F, unsigned LineNo,
DITypeRef Ty, ArrayRef<Value *> Addr,
unsigned ArgNo = 0);
DIExpression createExpression(ArrayRef<Value *> Addr = None);

/// createVariablePiece - Create a descriptor to describe one part
/// createPieceExpression - Create a descriptor to describe one part
/// of aggregate variable that is fragmented across multiple Values.
///
/// @param Variable Variable that is partially represented by this.
/// @param OffsetInBytes Offset of the piece in bytes.
/// @param SizeInBytes Size of the piece in bytes.
DIVariable createVariablePiece(DIVariable Variable,
unsigned OffsetInBytes,
unsigned SizeInBytes);
DIExpression createPieceExpression(unsigned OffsetInBytes,
unsigned SizeInBytes);

/// createFunction - Create a new descriptor for the specified subprogram.
/// See comments in DISubprogram for descriptions of these fields.
Expand Down Expand Up @@ -675,34 +659,37 @@ namespace llvm {
/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
/// @param Storage llvm::Value of the variable
/// @param VarInfo Variable's debug info descriptor.
/// @param Expr A complex location expression.
/// @param InsertAtEnd Location for the new intrinsic.
Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
BasicBlock *InsertAtEnd);
DIExpression Expr, BasicBlock *InsertAtEnd);

/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
/// @param Storage llvm::Value of the variable
/// @param VarInfo Variable's debug info descriptor.
/// @param Expr A complex location expression.
/// @param InsertBefore Location for the new intrinsic.
Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
Instruction *InsertBefore);

DIExpression Expr, Instruction *InsertBefore);

/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
/// @param Val llvm::Value of the variable
/// @param Offset Offset
/// @param VarInfo Variable's debug info descriptor.
/// @param Expr A complex location expression.
/// @param InsertAtEnd Location for the new intrinsic.
Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
DIVariable VarInfo,
DIVariable VarInfo, DIExpression Expr,
BasicBlock *InsertAtEnd);

/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
/// @param Val llvm::Value of the variable
/// @param Offset Offset
/// @param VarInfo Variable's debug info descriptor.
/// @param Expr A complex location expression.
/// @param InsertBefore Location for the new intrinsic.
Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
DIVariable VarInfo,
DIVariable VarInfo, DIExpression Expr,
Instruction *InsertBefore);
};
} // end namespace llvm
Expand Down
52 changes: 30 additions & 22 deletions include/llvm/IR/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class DIDescriptor {
bool isTemplateValueParameter() const;
bool isObjCProperty() const;
bool isImportedEntity() const;
bool isExpression() const;

/// print - print descriptor.
void print(raw_ostream &OS) const;
Expand Down Expand Up @@ -720,20 +721,6 @@ class DIVariable : public DIDescriptor {
/// Verify - Verify that a variable descriptor is well formed.
bool Verify() const;

/// HasComplexAddr - Return true if the variable has a complex address.
bool hasComplexAddress() const { return getNumAddrElements() > 0; }

/// \brief Return the size of this variable's complex address or
/// zero if there is none.
unsigned getNumAddrElements() const {
if (DbgNode->getNumOperands() < 9)
return 0;
return getDescriptorField(8)->getNumOperands();
}

/// \brief return the Idx'th complex address element.
uint64_t getAddrElement(unsigned Idx) const;

/// isBlockByrefVariable - Return true if the variable was declared as
/// a "__block" variable (Apple Blocks).
bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
Expand All @@ -744,18 +731,42 @@ class DIVariable : public DIDescriptor {
/// information for an inlined function arguments.
bool isInlinedFnArgument(const Function *CurFn);

/// Return the size reported by the variable's type.
unsigned getSizeInBits(const DITypeIdentifierMap &Map);

void printExtendedName(raw_ostream &OS) const;
};

/// DIExpression - A complex location expression.
class DIExpression : public DIDescriptor {
friend class DIDescriptor;
void printInternal(raw_ostream &OS) const;

public:
explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}

/// Verify - Verify that a variable descriptor is well formed.
bool Verify() const;

/// \brief Return the number of elements in the complex expression.
unsigned getNumElements() const {
if (!DbgNode)
return 0;
unsigned N = DbgNode->getNumOperands();
assert(N > 0 && "missing tag");
return N - 1;
}

/// \brief return the Idx'th complex address element.
uint64_t getElement(unsigned Idx) const;

/// isVariablePiece - Return whether this is a piece of an aggregate
/// variable.
bool isVariablePiece() const;
/// getPieceOffset - Return the offset of this piece in bytes.
uint64_t getPieceOffset() const;
/// getPieceSize - Return the size of this piece in bytes.
uint64_t getPieceSize() const;

/// Return the size reported by the variable's type.
unsigned getSizeInBits(const DITypeIdentifierMap &Map);

void printExtendedName(raw_ostream &OS) const;
};

/// DILocation - This object holds location information. This object
Expand Down Expand Up @@ -872,9 +883,6 @@ DIVariable createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
/// cleanseInlinedVariable - Remove inlined scope from the variable.
DIVariable cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext);

/// getEntireVariable - Remove OpPiece exprs from the variable.
DIVariable getEntireVariable(DIVariable DV);

/// Construct DITypeIdentifierMap by going through retained types of each CU.
DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);

Expand Down
2 changes: 2 additions & 0 deletions include/llvm/IR/IntrinsicInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace llvm {
public:
Value *getAddress() const;
MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
MDNode *getExpression() const { return cast<MDNode>(getArgOperand(2)); }

// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const IntrinsicInst *I) {
Expand All @@ -103,6 +104,7 @@ namespace llvm {
const_cast<Value*>(getArgOperand(1)))->getZExtValue();
}
MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
MDNode *getExpression() const { return cast<MDNode>(getArgOperand(3)); }

// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const IntrinsicInst *I) {
Expand Down
5 changes: 4 additions & 1 deletion include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,12 @@ let Properties = [IntrNoMem] in {
// places.
let Properties = [IntrNoMem] in {
def int_dbg_declare : Intrinsic<[],
[llvm_metadata_ty, llvm_metadata_ty]>;
[llvm_metadata_ty,
llvm_metadata_ty,
llvm_metadata_ty]>;
def int_dbg_value : Intrinsic<[],
[llvm_metadata_ty, llvm_i64_ty,
llvm_metadata_ty,
llvm_metadata_ty]>;
}

Expand Down
1 change: 1 addition & 0 deletions include/llvm/Support/Dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum LLVMConstants : uint32_t {

DW_TAG_auto_variable = 0x100, // Tag for local (auto) variables.
DW_TAG_arg_variable = 0x101, // Tag for argument variables.
DW_TAG_expression = 0x102, // Tag for complex address expressions.

DW_TAG_user_base = 0x1000, // Recommended base for user tags.

Expand Down
Loading

0 comments on commit 076fd5d

Please sign in to comment.