Skip to content

Commit aae995a

Browse files
Use DIExpression:foldConstantMath() at the result of an appendOpsToArg()
1 parent 03885c1 commit aae995a

File tree

11 files changed

+158
-61
lines changed

11 files changed

+158
-61
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,17 +1779,55 @@ DIExpression *DIExpression::appendOpsToArg(const DIExpression *Expr,
17791779
unsigned ArgNo, bool StackValue) {
17801780
assert(Expr && "Can't add ops to this expression");
17811781

1782+
unsigned OpCount = 0;
1783+
17821784
// Handle non-variadic intrinsics by prepending the opcodes.
17831785
if (!any_of(Expr->expr_ops(),
17841786
[](auto Op) { return Op.getOp() == dwarf::DW_OP_LLVM_arg; })) {
17851787
assert(ArgNo == 0 &&
17861788
"Location Index must be 0 for a non-variadic expression.");
1789+
1790+
for (auto It = Expr->expr_op_begin(); It != Expr->expr_op_end(); It++)
1791+
OpCount++;
17871792
SmallVector<uint64_t, 8> NewOps(Ops.begin(), Ops.end());
1788-
return DIExpression::prependOpcodes(Expr, NewOps, StackValue);
1793+
auto *Result = DIExpression::prependOpcodes(Expr, NewOps, StackValue);
1794+
1795+
// Instead of applying a foldConstantMath on the entire expression, apply it
1796+
// only on operations that are preppended + the succeeding operations
1797+
// totalling to the maximum foldable pattern.
1798+
uint64_t NewOpCount = 0;
1799+
for (auto It = Result->expr_op_begin(); It != Result->expr_op_end(); It++)
1800+
NewOpCount++;
1801+
1802+
// This is the number of new operations that were added to the expression,
1803+
// i.e. the number of operations in the Ops argument.
1804+
unsigned NumOfAddedOps = NewOpCount - OpCount;
1805+
// This is the number of operations that DIExpression::foldConstantMath
1806+
// should be applied upon, which is the number of new operations added + the
1807+
// succeeding operations totalling to the longest foldable pattern.
1808+
unsigned NumOfOpsToBeFolded = NumOfAddedOps + DIExpression::MaxRuleOpSize;
1809+
unsigned Count = 0;
1810+
SmallVector<uint64_t> OpsToBeFolded;
1811+
SmallVector<uint64_t> FoldedOps;
1812+
1813+
for (auto Op : Result->expr_ops()) {
1814+
if (Count < NumOfOpsToBeFolded) {
1815+
Op.appendToVector(OpsToBeFolded);
1816+
Count++;
1817+
} else {
1818+
Op.appendToVector(FoldedOps);
1819+
}
1820+
}
1821+
1822+
OpsToBeFolded = Result->foldConstantMath(OpsToBeFolded);
1823+
OpsToBeFolded.append(FoldedOps);
1824+
return DIExpression::get(Result->getContext(), OpsToBeFolded);
17891825
}
17901826

1827+
unsigned ArgPos;
17911828
SmallVector<uint64_t, 8> NewOps;
17921829
for (auto Op : Expr->expr_ops()) {
1830+
OpCount++;
17931831
// A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
17941832
if (StackValue) {
17951833
if (Op.getOp() == dwarf::DW_OP_stack_value)
@@ -1800,13 +1838,54 @@ DIExpression *DIExpression::appendOpsToArg(const DIExpression *Expr,
18001838
}
18011839
}
18021840
Op.appendToVector(NewOps);
1803-
if (Op.getOp() == dwarf::DW_OP_LLVM_arg && Op.getArg(0) == ArgNo)
1841+
if (Op.getOp() == dwarf::DW_OP_LLVM_arg && Op.getArg(0) == ArgNo) {
18041842
NewOps.insert(NewOps.end(), Ops.begin(), Ops.end());
1843+
ArgPos = OpCount;
1844+
}
18051845
}
18061846
if (StackValue)
18071847
NewOps.push_back(dwarf::DW_OP_stack_value);
18081848

1809-
return DIExpression::get(Expr->getContext(), NewOps);
1849+
auto *Result = DIExpression::get(Expr->getContext(), NewOps);
1850+
1851+
// Instead of applying a foldConstantMath on the entire expression, apply it
1852+
// only on operations that are inserted to the ArgNo + the preceeding
1853+
// operations totalling to the maximum foldable pattern.
1854+
unsigned NewOpCount = 0;
1855+
for (auto It = Result->expr_op_begin(); It != Result->expr_op_end(); It++)
1856+
NewOpCount++;
1857+
1858+
unsigned Count = 0;
1859+
SmallVector<uint64_t> OpsToBeFolded;
1860+
SmallVector<uint64_t> FoldedOps;
1861+
SmallVector<uint64_t> RemainingOps;
1862+
1863+
// This is the number of operations we can consider already folded in the
1864+
// expression, which precede the ArgNo that the Ops are appended to.
1865+
unsigned FoldedOpCount = ArgPos > DIExpression::MaxRuleOpSize
1866+
? ArgPos - DIExpression::MaxRuleOpSize
1867+
: 0;
1868+
// This is the number of operations that DIExpression::foldConstantMath should
1869+
// be applied upon, which is the number of new operations added + the
1870+
// preceeding operations totalling to the maximum foldable pattern.
1871+
unsigned OpsToBeFoldedCount =
1872+
NewOpCount - OpCount + DIExpression::MaxRuleOpSize;
1873+
1874+
for (auto Op : Result->expr_ops()) {
1875+
if (Count < FoldedOpCount) {
1876+
Op.appendToVector(FoldedOps);
1877+
Count++;
1878+
} else if (Count >= FoldedOpCount && Count < OpsToBeFoldedCount) {
1879+
Op.appendToVector(OpsToBeFolded);
1880+
Count++;
1881+
} else
1882+
Op.appendToVector(RemainingOps);
1883+
}
1884+
1885+
OpsToBeFolded = Result->foldConstantMath(OpsToBeFolded);
1886+
FoldedOps.append(OpsToBeFolded);
1887+
FoldedOps.append(RemainingOps);
1888+
return DIExpression::get(Result->getContext(), FoldedOps);
18101889
}
18111890

18121891
DIExpression *DIExpression::replaceArg(const DIExpression *Expr,

llvm/test/DebugInfo/MIR/InstrRef/deref-spills-with-size.mir

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
# DWARF-NEXT: DW_OP_breg7 RSP-8
8484
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_stack_value
8585
## scalar / stack value with various sizes.
86-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x8, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value
87-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value
88-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value)
86+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x8, DW_OP_plus_uconst 0x1, DW_OP_stack_value
87+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref, DW_OP_plus_uconst 0x1, DW_OP_stack_value
88+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_plus_uconst 0x1, DW_OP_stack_value)
8989
# DWARF: DW_AT_name ("flannel")
9090

9191
# Variable with fragments.
@@ -96,9 +96,9 @@
9696
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_piece 0x4
9797
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_stack_value, DW_OP_piece 0x4
9898
## Scalar / stack value with various sizes.
99-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x8, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value, DW_OP_piece 0x4
100-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x4, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value, DW_OP_piece 0x4
101-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value, DW_OP_piece 0x4)
99+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x8, DW_OP_plus_uconst 0x1, DW_OP_stack_value, DW_OP_piece 0x4
100+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x4, DW_OP_plus_uconst 0x1, DW_OP_stack_value, DW_OP_piece 0x4
101+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref_size 0x1, DW_OP_plus_uconst 0x1, DW_OP_stack_value, DW_OP_piece 0x4)
102102
# DWARF: DW_AT_name ("shoes")
103103

104104
--- |
@@ -308,7 +308,7 @@ body: |
308308
$rax = MOV64ri 0, debug-location !7
309309
DBG_INSTR_REF !8, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), dbg-instr-ref(16, 0), debug-location !7
310310
; CHECK: DBG_VALUE_LIST ![[VARNUM]],
311-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 8, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), $rsp
311+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 8, DW_OP_plus_uconst, 1, DW_OP_stack_value), $rsp
312312
$eax = MOV32ri 0, debug-location !7
313313
DBG_VALUE $noreg, $noreg, !8, !DIExpression(), debug-location !7
314314
; CHECK: DBG_VALUE $noreg, $noreg
@@ -319,7 +319,7 @@ body: |
319319
$rax = MOV64ri 0, debug-location !7
320320
DBG_INSTR_REF !8, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), dbg-instr-ref(17, 0), debug-location !7
321321
; CHECK: DBG_VALUE_LIST ![[VARNUM]],
322-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), $rsp
322+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref, DW_OP_plus_uconst, 1, DW_OP_stack_value), $rsp
323323
$eax = MOV32ri 0, debug-location !7
324324
DBG_VALUE $noreg, $noreg, !8, !DIExpression(), debug-location !7
325325
; CHECK: DBG_VALUE $noreg, $noreg
@@ -330,7 +330,7 @@ body: |
330330
$rax = MOV64ri 0, debug-location !7
331331
DBG_INSTR_REF !8, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), dbg-instr-ref(18, 0), debug-location !7
332332
; CHECK: DBG_VALUE_LIST ![[VARNUM]],
333-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 1, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value), $rsp
333+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 1, DW_OP_plus_uconst, 1, DW_OP_stack_value), $rsp
334334
$eax = MOV32ri 0, debug-location !7
335335
DBG_VALUE $noreg, $noreg, !8, !DIExpression(), debug-location !7
336336
; CHECK: DBG_VALUE $noreg, $noreg
@@ -341,7 +341,7 @@ body: |
341341
$rax = MOV64ri 0, debug-location !7
342342
DBG_INSTR_REF !10, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), dbg-instr-ref(19, 0), debug-location !7
343343
; CHECK: DBG_VALUE_LIST ![[VARNUM2]],
344-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 8, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
344+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 8, DW_OP_plus_uconst, 1, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
345345
$eax = MOV32ri 0, debug-location !7
346346
DBG_VALUE $noreg, $noreg, !10, !DIExpression(DW_OP_LLVM_fragment, 0, 32), debug-location !7
347347
; CHECK: DBG_VALUE $noreg, $noreg
@@ -352,7 +352,7 @@ body: |
352352
$rax = MOV64ri 0, debug-location !7
353353
DBG_INSTR_REF !10, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), dbg-instr-ref(20, 0), debug-location !7
354354
; CHECK: DBG_VALUE_LIST ![[VARNUM2]],
355-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 4, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
355+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 4, DW_OP_plus_uconst, 1, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
356356
$eax = MOV32ri 0, debug-location !7
357357
DBG_VALUE $noreg, $noreg, !10, !DIExpression(DW_OP_LLVM_fragment, 0, 32), debug-location !7
358358
; CHECK: DBG_VALUE $noreg, $noreg
@@ -363,7 +363,7 @@ body: |
363363
$rax = MOV64ri 0, debug-location !7
364364
DBG_INSTR_REF !10, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), dbg-instr-ref(21, 0), debug-location !7
365365
; CHECK: DBG_VALUE_LIST ![[VARNUM2]],
366-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 1, DW_OP_constu, 1, DW_OP_plus, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
366+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_constu, 8, DW_OP_minus, DW_OP_deref_size, 1, DW_OP_plus_uconst, 1, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 32), $rsp
367367
$eax = MOV32ri 0, debug-location !7
368368
DBG_VALUE $noreg, $noreg, !10, !DIExpression(DW_OP_LLVM_fragment, 0, 32), debug-location !7
369369
; CHECK: DBG_VALUE $noreg, $noreg

llvm/test/DebugInfo/MIR/InstrRef/follow-spill-of-indir-value.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
##
2828
## Second location, variable pointed to by the register value plus eight.
2929
##
30-
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref, DW_OP_lit8, DW_OP_plus)
30+
# DWARF-NEXT: DW_OP_breg7 RSP-8, DW_OP_deref, DW_OP_plus_uconst 0x8)
3131
##
3232
## Spilt to stack: push stack location and deref the pointer onto the dwarf
3333
## expr stack. Then add eight to it, and it points to the variable.
@@ -135,7 +135,7 @@ body: |
135135
; CHECK: DBG_VALUE $rdi,
136136
MOV64mr $rsp, 1, $noreg, -8, $noreg, $rdi :: (store (s64) into %stack.0)
137137
$rdi = MOV64ri 0
138-
; CHECK: DBG_VALUE $rsp, 0, ![[VARNUM]], !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_deref, DW_OP_constu, 8, DW_OP_plus),
138+
; CHECK: DBG_VALUE $rsp, 0, ![[VARNUM]], !DIExpression(DW_OP_constu, 8, DW_OP_minus, DW_OP_deref, DW_OP_plus_uconst, 8),
139139
140140
renamable $rax = MOV64rm $rsp, 1, $noreg, -8, $noreg :: (load (s64) from %stack.0)
141141
renamable $eax = MOV32rm killed renamable $rax, 1, $noreg, 0, $noreg, debug-location !24 :: (load (s32) from %ir.i1, !tbaa !25)

llvm/test/DebugInfo/salvage-icmp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
; CHECK: call void @llvm.dbg.value(metadata i32 %a,
77
; CHECK-SAME: ![[VAR_C:[0-9]+]],
8-
; CHECK-SAME: !DIExpression(DW_OP_constu, 0, DW_OP_ne, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 0, DW_OP_eq, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 1, DW_OP_gt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551615, DW_OP_gt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 2, DW_OP_ge, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551614, DW_OP_ge, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 3, DW_OP_lt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551613, DW_OP_lt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 4, DW_OP_le, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551612, DW_OP_le, DW_OP_stack_value))
8+
; CHECK-SAME: !DIExpression(DW_OP_lit0, DW_OP_ne, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_lit0, DW_OP_eq, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 1, DW_OP_gt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551615, DW_OP_gt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 2, DW_OP_ge, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551614, DW_OP_ge, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 3, DW_OP_lt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551613, DW_OP_lt, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_constu, 4, DW_OP_le, DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_consts, 18446744073709551612, DW_OP_le, DW_OP_stack_value))
99

1010
; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i32 %a, i32 %a, i32 %a, i32 %b, i32 %a, i32 %b, i32 %b, i32 %a, i32 %a, i32 %b, i32 %b),
1111
; CHECK-SAME: ![[VAR_C:[0-9]+]],

0 commit comments

Comments
 (0)