Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 02474a3

Browse files
committed
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! Note: I accidentally committed a bogus older version of this patch previously. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218787 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0ad623b commit 02474a3

File tree

257 files changed

+1535
-1378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+1535
-1378
lines changed

docs/SourceLevelDebugging.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ Local variables
571571
metadata, ;; Reference to the type descriptor
572572
i32, ;; flags
573573
metadata ;; (optional) Reference to inline location
574-
metadata ;; (optional) Reference to a complex expression (see below)
574+
metadata ;; (optional) Reference to a complex expression.
575575
}
576576
577577
These descriptors are used to define variables local to a sub program. The
@@ -590,7 +590,20 @@ The context is either the subprogram or block where the variable is defined.
590590
Name the source variable name. Context and line indicate where the variable
591591
was defined. Type descriptor defines the declared type of the variable.
592592

593-
The ``OpPiece`` operator is used for (typically larger aggregate)
593+
Complex Expressions
594+
^^^^^^^^^^^^^^^^^^^
595+
.. code-block:: llvm
596+
597+
!8 = metadata !{
598+
i32, ;; DW_TAG_expression
599+
...
600+
}
601+
602+
Complex expressions describe variable storage locations in terms of
603+
prefix-notated DWARF expressions. Currently the only supported
604+
operators are ``DW_OP_plus``, ``DW_OP_deref``, and ``DW_OP_piece``.
605+
606+
The ``DW_OP_piece`` operator is used for (typically larger aggregate)
594607
variables that are fragmented across several locations. It takes two
595608
i32 arguments, an offset and a size in bytes to describe which piece
596609
of the variable is at this location.

include/llvm/CodeGen/MachineInstr.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,22 @@ class MachineInstr : public ilist_node<MachineInstr> {
244244
///
245245
DebugLoc getDebugLoc() const { return debugLoc; }
246246

247-
/// getDebugVariable() - Return the debug variable referenced by
247+
/// \brief Return the debug variable referenced by
248248
/// this DBG_VALUE instruction.
249249
DIVariable getDebugVariable() const {
250250
assert(isDebugValue() && "not a DBG_VALUE");
251-
const MDNode *Var = getOperand(getNumOperands() - 1).getMetadata();
252-
return DIVariable(Var);
251+
DIVariable Var(getOperand(2).getMetadata());
252+
assert(Var.Verify() && "not a DIVariable");
253+
return Var;
254+
}
255+
256+
/// \brief Return the complex address expression referenced by
257+
/// this DBG_VALUE instruction.
258+
DIExpression getDebugExpression() const {
259+
assert(isDebugValue() && "not a DBG_VALUE");
260+
DIExpression Expr(getOperand(3).getMetadata());
261+
assert(Expr.Verify() && "not a DIExpression");
262+
return Expr;
253263
}
254264

255265
/// emitError - Emit an error referring to the source location of this

include/llvm/CodeGen/MachineInstrBuilder.h

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class MachineInstrBuilder {
170170

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

@@ -345,24 +347,25 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
345347
/// address. The convention is that a DBG_VALUE is indirect iff the
346348
/// second operand is an immediate.
347349
///
348-
inline MachineInstrBuilder BuildMI(MachineFunction &MF,
349-
DebugLoc DL,
350-
const MCInstrDesc &MCID,
351-
bool IsIndirect,
352-
unsigned Reg,
353-
unsigned Offset,
354-
const MDNode *MD) {
350+
inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
351+
const MCInstrDesc &MCID, bool IsIndirect,
352+
unsigned Reg, unsigned Offset,
353+
const MDNode *Variable, const MDNode *Expr) {
354+
assert(DIVariable(Variable).Verify() && "not a DIVariable");
355+
assert(DIExpression(Expr).Verify() && "not a DIExpression");
355356
if (IsIndirect)
356357
return BuildMI(MF, DL, MCID)
357-
.addReg(Reg, RegState::Debug)
358-
.addImm(Offset)
359-
.addMetadata(MD);
358+
.addReg(Reg, RegState::Debug)
359+
.addImm(Offset)
360+
.addMetadata(Variable)
361+
.addMetadata(Expr);
360362
else {
361363
assert(Offset == 0 && "A direct address cannot have an offset.");
362364
return BuildMI(MF, DL, MCID)
363-
.addReg(Reg, RegState::Debug)
364-
.addReg(0U, RegState::Debug)
365-
.addMetadata(MD);
365+
.addReg(Reg, RegState::Debug)
366+
.addReg(0U, RegState::Debug)
367+
.addMetadata(Variable)
368+
.addMetadata(Expr);
366369
}
367370
}
368371

@@ -371,15 +374,15 @@ inline MachineInstrBuilder BuildMI(MachineFunction &MF,
371374
/// address and inserts it at position I.
372375
///
373376
inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
374-
MachineBasicBlock::iterator I,
375-
DebugLoc DL,
376-
const MCInstrDesc &MCID,
377-
bool IsIndirect,
378-
unsigned Reg,
379-
unsigned Offset,
380-
const MDNode *MD) {
377+
MachineBasicBlock::iterator I, DebugLoc DL,
378+
const MCInstrDesc &MCID, bool IsIndirect,
379+
unsigned Reg, unsigned Offset,
380+
const MDNode *Variable, const MDNode *Expr) {
381+
assert(DIVariable(Variable).Verify() && "not a DIVariable");
382+
assert(DIExpression(Expr).Verify() && "not a DIExpression");
381383
MachineFunction &MF = *BB.getParent();
382-
MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, MD);
384+
MachineInstr *MI =
385+
BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
383386
BB.insert(I, MI);
384387
return MachineInstrBuilder(MF, MI);
385388
}

include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class MachineModuleInfo : public ImmutablePass {
166166

167167
struct VariableDbgInfo {
168168
TrackingVH<MDNode> Var;
169+
TrackingVH<MDNode> Expr;
169170
unsigned Slot;
170171
DebugLoc Loc;
171172
};
@@ -390,8 +391,9 @@ class MachineModuleInfo : public ImmutablePass {
390391

391392
/// setVariableDbgInfo - Collect information used to emit debugging
392393
/// information of a variable.
393-
void setVariableDbgInfo(MDNode *N, unsigned Slot, DebugLoc Loc) {
394-
VariableDbgInfo Info = { N, Slot, Loc };
394+
void setVariableDbgInfo(MDNode *Var, MDNode *Expr, unsigned Slot,
395+
DebugLoc Loc) {
396+
VariableDbgInfo Info = {Var, Expr, Slot, Loc};
395397
VariableDbgInfos.push_back(std::move(Info));
396398
}
397399

include/llvm/CodeGen/SelectionDAG.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -984,15 +984,18 @@ class SelectionDAG {
984984

985985
/// getDbgValue - Creates a SDDbgValue node.
986986
///
987-
SDDbgValue *getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R,
988-
bool IsIndirect, uint64_t Off,
989-
DebugLoc DL, unsigned O);
990-
/// Constant.
991-
SDDbgValue *getConstantDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
992-
DebugLoc DL, unsigned O);
993-
/// Frame index.
994-
SDDbgValue *getFrameIndexDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
995-
DebugLoc DL, unsigned O);
987+
/// SDNode
988+
SDDbgValue *getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N, unsigned R,
989+
bool IsIndirect, uint64_t Off, DebugLoc DL,
990+
unsigned O);
991+
992+
/// Constant
993+
SDDbgValue *getConstantDbgValue(MDNode *Var, MDNode *Expr, const Value *C,
994+
uint64_t Off, DebugLoc DL, unsigned O);
995+
996+
/// FrameIndex
997+
SDDbgValue *getFrameIndexDbgValue(MDNode *Var, MDNode *Expr, unsigned FI,
998+
uint64_t Off, DebugLoc DL, unsigned O);
996999

9971000
/// RemoveDeadNode - Remove the specified node from the system. If any of its
9981001
/// operands then becomes dead, remove them as well. Inform UpdateListener

include/llvm/IR/DIBuilder.h

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ namespace llvm {
8585

8686
public:
8787
explicit DIBuilder(Module &M);
88-
enum ComplexAddrKind { OpPlus=1, OpDeref, OpPiece };
8988
enum DebugEmissionKind { FullDebug=1, LineTablesOnly };
9089

9190
/// finalize - Construct any deferred debug info descriptors.
@@ -501,33 +500,18 @@ namespace llvm {
501500
unsigned Flags = 0,
502501
unsigned ArgNo = 0);
503502

504-
505-
/// createComplexVariable - Create a new descriptor for the specified
503+
/// createExpression - Create a new descriptor for the specified
506504
/// variable which has a complex address expression for its address.
507-
/// @param Tag Dwarf TAG. Usually DW_TAG_auto_variable or
508-
/// DW_TAG_arg_variable.
509-
/// @param Scope Variable scope.
510-
/// @param Name Variable name.
511-
/// @param F File where this variable is defined.
512-
/// @param LineNo Line number.
513-
/// @param Ty Variable Type
514505
/// @param Addr An array of complex address operations.
515-
/// @param ArgNo If this variable is an argument then this argument's
516-
/// number. 1 indicates 1st argument.
517-
DIVariable createComplexVariable(unsigned Tag, DIDescriptor Scope,
518-
StringRef Name, DIFile F, unsigned LineNo,
519-
DITypeRef Ty, ArrayRef<Value *> Addr,
520-
unsigned ArgNo = 0);
506+
DIExpression createExpression(ArrayRef<Value *> Addr = None);
521507

522-
/// createVariablePiece - Create a descriptor to describe one part
508+
/// createPieceExpression - Create a descriptor to describe one part
523509
/// of aggregate variable that is fragmented across multiple Values.
524510
///
525-
/// @param Variable Variable that is partially represented by this.
526511
/// @param OffsetInBytes Offset of the piece in bytes.
527512
/// @param SizeInBytes Size of the piece in bytes.
528-
DIVariable createVariablePiece(DIVariable Variable,
529-
unsigned OffsetInBytes,
530-
unsigned SizeInBytes);
513+
DIExpression createPieceExpression(unsigned OffsetInBytes,
514+
unsigned SizeInBytes);
531515

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

682667
/// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
683668
/// @param Storage llvm::Value of the variable
684669
/// @param VarInfo Variable's debug info descriptor.
670+
/// @param Expr A complex location expression.
685671
/// @param InsertBefore Location for the new intrinsic.
686672
Instruction *insertDeclare(llvm::Value *Storage, DIVariable VarInfo,
687-
Instruction *InsertBefore);
688-
673+
DIExpression Expr, Instruction *InsertBefore);
689674

690675
/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
691676
/// @param Val llvm::Value of the variable
692677
/// @param Offset Offset
693678
/// @param VarInfo Variable's debug info descriptor.
679+
/// @param Expr A complex location expression.
694680
/// @param InsertAtEnd Location for the new intrinsic.
695681
Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
696-
DIVariable VarInfo,
682+
DIVariable VarInfo, DIExpression Expr,
697683
BasicBlock *InsertAtEnd);
698684

699685
/// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
700686
/// @param Val llvm::Value of the variable
701687
/// @param Offset Offset
702688
/// @param VarInfo Variable's debug info descriptor.
689+
/// @param Expr A complex location expression.
703690
/// @param InsertBefore Location for the new intrinsic.
704691
Instruction *insertDbgValueIntrinsic(llvm::Value *Val, uint64_t Offset,
705-
DIVariable VarInfo,
692+
DIVariable VarInfo, DIExpression Expr,
706693
Instruction *InsertBefore);
707694
};
708695
} // end namespace llvm

include/llvm/IR/DebugInfo.h

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class DIDescriptor {
150150
bool isTemplateValueParameter() const;
151151
bool isObjCProperty() const;
152152
bool isImportedEntity() const;
153+
bool isExpression() const;
153154

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

723-
/// HasComplexAddr - Return true if the variable has a complex address.
724-
bool hasComplexAddress() const { return getNumAddrElements() > 0; }
725-
726-
/// \brief Return the size of this variable's complex address or
727-
/// zero if there is none.
728-
unsigned getNumAddrElements() const {
729-
if (DbgNode->getNumOperands() < 9)
730-
return 0;
731-
return getDescriptorField(8)->getNumOperands();
732-
}
733-
734-
/// \brief return the Idx'th complex address element.
735-
uint64_t getAddrElement(unsigned Idx) const;
736-
737724
/// isBlockByrefVariable - Return true if the variable was declared as
738725
/// a "__block" variable (Apple Blocks).
739726
bool isBlockByrefVariable(const DITypeIdentifierMap &Map) const {
@@ -744,18 +731,42 @@ class DIVariable : public DIDescriptor {
744731
/// information for an inlined function arguments.
745732
bool isInlinedFnArgument(const Function *CurFn);
746733

734+
/// Return the size reported by the variable's type.
735+
unsigned getSizeInBits(const DITypeIdentifierMap &Map);
736+
737+
void printExtendedName(raw_ostream &OS) const;
738+
};
739+
740+
/// DIExpression - A complex location expression.
741+
class DIExpression : public DIDescriptor {
742+
friend class DIDescriptor;
743+
void printInternal(raw_ostream &OS) const;
744+
745+
public:
746+
explicit DIExpression(const MDNode *N = nullptr) : DIDescriptor(N) {}
747+
748+
/// Verify - Verify that a variable descriptor is well formed.
749+
bool Verify() const;
750+
751+
/// \brief Return the number of elements in the complex expression.
752+
unsigned getNumElements() const {
753+
if (!DbgNode)
754+
return 0;
755+
unsigned N = DbgNode->getNumOperands();
756+
assert(N > 0 && "missing tag");
757+
return N - 1;
758+
}
759+
760+
/// \brief return the Idx'th complex address element.
761+
uint64_t getElement(unsigned Idx) const;
762+
747763
/// isVariablePiece - Return whether this is a piece of an aggregate
748764
/// variable.
749765
bool isVariablePiece() const;
750766
/// getPieceOffset - Return the offset of this piece in bytes.
751767
uint64_t getPieceOffset() const;
752768
/// getPieceSize - Return the size of this piece in bytes.
753769
uint64_t getPieceSize() const;
754-
755-
/// Return the size reported by the variable's type.
756-
unsigned getSizeInBits(const DITypeIdentifierMap &Map);
757-
758-
void printExtendedName(raw_ostream &OS) const;
759770
};
760771

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

875-
/// getEntireVariable - Remove OpPiece exprs from the variable.
876-
DIVariable getEntireVariable(DIVariable DV);
877-
878886
/// Construct DITypeIdentifierMap by going through retained types of each CU.
879887
DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
880888

include/llvm/IR/IntrinsicInst.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace llvm {
8282
public:
8383
Value *getAddress() const;
8484
MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
85+
MDNode *getExpression() const { return cast<MDNode>(getArgOperand(2)); }
8586

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

107109
// Methods for support type inquiry through isa, cast, and dyn_cast:
108110
static inline bool classof(const IntrinsicInst *I) {

include/llvm/IR/Intrinsics.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,12 @@ let Properties = [IntrNoMem] in {
373373
// places.
374374
let Properties = [IntrNoMem] in {
375375
def int_dbg_declare : Intrinsic<[],
376-
[llvm_metadata_ty, llvm_metadata_ty]>;
376+
[llvm_metadata_ty,
377+
llvm_metadata_ty,
378+
llvm_metadata_ty]>;
377379
def int_dbg_value : Intrinsic<[],
378380
[llvm_metadata_ty, llvm_i64_ty,
381+
llvm_metadata_ty,
379382
llvm_metadata_ty]>;
380383
}
381384

include/llvm/Support/Dwarf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum LLVMConstants : uint32_t {
5353

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

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

0 commit comments

Comments
 (0)