Skip to content

Commit f29e07c

Browse files
Use DIExpression:foldConstantMath() at the result of an append()
1 parent d0df230 commit f29e07c

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ DIExpression *DIExpression::append(const DIExpression *Expr,
18601860
NewOps.append(Ops.begin(), Ops.end());
18611861
auto *result = DIExpression::get(Expr->getContext(), NewOps);
18621862
assert(result->isValid() && "concatenated expression is not valid");
1863-
return result;
1863+
return result->foldConstantMath();
18641864
}
18651865

18661866
DIExpression *DIExpression::appendToStack(const DIExpression *Expr,
@@ -2157,7 +2157,8 @@ DIExpression *DIExpression::foldConstantMath() {
21572157
if (Op1Raw == dwarf::DW_OP_plus_uconst && isConstantVal(Op2Raw) &&
21582158
Op3Raw == dwarf::DW_OP_plus) {
21592159
auto Result = Op1Arg + Op2Arg;
2160-
WorkingOps.erase(WorkingOps.begin() + Loc + 2, WorkingOps.begin() + 5);
2160+
WorkingOps.erase(WorkingOps.begin() + Loc + 2,
2161+
WorkingOps.begin() + Loc + 5);
21612162
WorkingOps[Loc + 1] = Result;
21622163
Cursor.assignNewExpr(WorkingOps);
21632164
Loc = 0;
@@ -2170,7 +2171,8 @@ DIExpression *DIExpression::foldConstantMath() {
21702171
if (isConstantVal(Op1Raw) && Op2Raw == dwarf::DW_OP_plus &&
21712172
Op3Raw == dwarf::DW_OP_plus_uconst) {
21722173
auto Result = Op1Arg + Op3Arg;
2173-
WorkingOps.erase(WorkingOps.begin() + Loc + 2, WorkingOps.begin() + 5);
2174+
WorkingOps.erase(WorkingOps.begin() + Loc + 2,
2175+
WorkingOps.begin() + Loc + 5);
21742176
WorkingOps[Loc] = dwarf::DW_OP_plus_uconst;
21752177
WorkingOps[Loc + 1] = Result;
21762178
Cursor.assignNewExpr(WorkingOps);

llvm/test/Bitcode/upgrade-dbg-addr.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ entry:
88
%num.addr = alloca i32, align 4
99
store i32 %num, ptr %num.addr, align 4
1010
; CHECK-NOT: call void @llvm.dbg.addr
11-
; CHECK: call void @llvm.dbg.value(metadata ptr %num.addr, metadata ![[#]], metadata !DIExpression(DW_OP_plus_uconst, 0, DW_OP_deref))
11+
; CHECK: call void @llvm.dbg.value(metadata ptr %num.addr, metadata ![[#]], metadata !DIExpression(DW_OP_deref))
1212
call void @llvm.dbg.addr(metadata ptr %num.addr, metadata !16, metadata !DIExpression(DW_OP_plus_uconst, 0)), !dbg !17
1313
%0 = load i32, ptr %num.addr, align 4
1414
ret i32 %0

llvm/test/DebugInfo/MIR/AArch64/dbgcall-site-expr-chain.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ body: |
105105

106106
# CHECK: DW_TAG_GNU_call_site_parameter
107107
# CHECK-NEXT: DW_AT_location (DW_OP_reg2 W2)
108-
# CHECK-NEXT: DW_AT_GNU_call_site_value (DW_OP_breg19 W19+700, DW_OP_plus_uconst 0x9, DW_OP_plus_uconst 0x50)
108+
# CHECK-NEXT: DW_AT_GNU_call_site_value (DW_OP_breg19 W19+789)
109109

110110
# CHECK: DW_TAG_GNU_call_site_parameter
111111
# CHECK-NEXT: DW_AT_location (DW_OP_reg1 W1)
112112
# CHECK-NEXT: DW_AT_GNU_call_site_value (DW_OP_breg19 W19-406, DW_OP_constu 0x32, DW_OP_minus)
113113

114114
# CHECK: DW_TAG_GNU_call_site_parameter
115115
# CHECK-NEXT: DW_AT_location (DW_OP_reg0 W0)
116-
# CHECK-NEXT: DW_AT_GNU_call_site_value (DW_OP_breg19 W19+100, DW_OP_plus_uconst 0x17)
116+
# CHECK-NEXT: DW_AT_GNU_call_site_value (DW_OP_breg19 W19+123)

llvm/unittests/IR/MetadataTest.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,92 @@ TEST_F(DIExpressionTest, Fold) {
33973397
EXPECT_EQ(E, ResExpr);
33983398
}
33993399

3400+
TEST_F(DIExpressionTest, Append) {
3401+
// Test appending a {dwarf::DW_OP_constu, <const>, DW_OP_plus} to a DW_OP_plus expression
3402+
SmallVector<uint64_t, 8> Ops = {dwarf::DW_OP_LLVM_arg, 0,
3403+
dwarf::DW_OP_constu, 2, dwarf::DW_OP_plus};
3404+
auto *Expr = DIExpression::get(Context, Ops);
3405+
SmallVector<uint64_t, 8> AppendOps = {dwarf::DW_OP_constu, 3,
3406+
dwarf::DW_OP_plus};
3407+
auto *AppendExpr = DIExpression::append(Expr, AppendOps);
3408+
SmallVector<uint64_t, 8> OpsRes = {
3409+
dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_constu, 5, dwarf::DW_OP_plus};
3410+
auto *ResExpr = DIExpression::get(Context, OpsRes);
3411+
EXPECT_EQ(ResExpr, AppendExpr);
3412+
3413+
// Test appending a {dwarf::DW_OP_plus_uconst, <const>} to a DW_OP_plus
3414+
// expression uint64_t PlusUConstOps[] = {dwarf::DW_OP_plus_uconst, 3};
3415+
AppendOps.clear();
3416+
AppendOps.push_back(dwarf::DW_OP_plus_uconst);
3417+
AppendOps.push_back(3);
3418+
AppendExpr = DIExpression::append(Expr, AppendOps);
3419+
OpsRes.clear();
3420+
OpsRes.push_back(dwarf::DW_OP_LLVM_arg);
3421+
OpsRes.push_back(0);
3422+
OpsRes.push_back(dwarf::DW_OP_plus_uconst);
3423+
OpsRes.push_back(5);
3424+
ResExpr = DIExpression::get(Context, OpsRes);
3425+
EXPECT_EQ(ResExpr, AppendExpr);
3426+
3427+
// Test appending a {dwarf::DW_OP_constu, 0, DW_OP_plus} to an expression
3428+
AppendOps[0] = dwarf::DW_OP_constu;
3429+
AppendOps[1] = 0;
3430+
AppendOps.push_back(dwarf::DW_OP_plus);
3431+
AppendExpr = DIExpression::append(Expr, AppendOps);
3432+
OpsRes[2] = dwarf::DW_OP_constu;
3433+
OpsRes[3] = Ops[3];
3434+
OpsRes.push_back(dwarf::DW_OP_plus);
3435+
ResExpr = DIExpression::get(Context, OpsRes);
3436+
EXPECT_EQ(ResExpr, AppendExpr);
3437+
3438+
// Test appending a {dwarf::DW_OP_constu, 0, DW_OP_minus} to an expression
3439+
AppendOps[2] = dwarf::DW_OP_minus;
3440+
AppendExpr = DIExpression::append(Expr, AppendOps);
3441+
OpsRes[3] = Ops[3];
3442+
ResExpr = DIExpression::get(Context, OpsRes);
3443+
EXPECT_EQ(ResExpr, AppendExpr);
3444+
3445+
// Test appending a {dwarf::DW_OP_constu, 0, DW_OP_shl} to an expression
3446+
AppendOps[2] = dwarf::DW_OP_shl;
3447+
AppendExpr = DIExpression::append(Expr, AppendOps);
3448+
OpsRes[3] = Ops[3];
3449+
ResExpr = DIExpression::get(Context, OpsRes);
3450+
EXPECT_EQ(ResExpr, AppendExpr);
3451+
3452+
// Test appending a {dwarf::DW_OP_constu, 0, DW_OP_shr} to an expression
3453+
AppendOps[2] = dwarf::DW_OP_shr;
3454+
AppendExpr = DIExpression::append(Expr, AppendOps);
3455+
OpsRes[3] = Ops[3];
3456+
ResExpr = DIExpression::get(Context, OpsRes);
3457+
EXPECT_EQ(ResExpr, AppendExpr);
3458+
3459+
// Test appending a {dwarf::DW_OP_constu, <const>, DW_OP_mul} to a DW_OP_mul expression
3460+
Ops[4] = dwarf::DW_OP_mul;
3461+
Expr = DIExpression::get(Context, Ops);
3462+
AppendOps[1] = 3;
3463+
AppendOps[2] = dwarf::DW_OP_mul;
3464+
AppendExpr = DIExpression::append(Expr, AppendOps);
3465+
OpsRes[3] = 6;
3466+
OpsRes[4] = dwarf::DW_OP_mul;
3467+
ResExpr = DIExpression::get(Context, OpsRes);
3468+
EXPECT_EQ(ResExpr, AppendExpr);
3469+
3470+
// Test appending a {dwarf::DW_OP_constu, 1, DW_OP_mul} to an expression
3471+
AppendOps[1] = 1;
3472+
AppendExpr = DIExpression::append(Expr, AppendOps);
3473+
OpsRes[3] = Ops[3];
3474+
ResExpr = DIExpression::get(Context, OpsRes);
3475+
EXPECT_EQ(ResExpr, AppendExpr);
3476+
3477+
// Test appending a {dwarf::DW_OP_constu, 1, DW_OP_div} to an expression
3478+
AppendOps[1] = 1;
3479+
AppendOps[2] = dwarf::DW_OP_div;
3480+
AppendExpr = DIExpression::append(Expr, AppendOps);
3481+
OpsRes[3] = Ops[3];
3482+
ResExpr = DIExpression::get(Context, OpsRes);
3483+
EXPECT_EQ(ResExpr, AppendExpr);
3484+
}
3485+
34003486
TEST_F(DIExpressionTest, isValid) {
34013487
#define EXPECT_VALID(...) \
34023488
do { \

0 commit comments

Comments
 (0)