Skip to content

Commit e18ad47

Browse files
[SelectionDAG]Salvage debuginfo when combining load and sext instrs.
SelectionDAG uses the DAGCombiner to fold a load followed by a sext to a load and sext instruction. For example, in x86 we will see that %1 = load i32, ptr @GloBaRR #dbg_value(i32 %1, !43, !DIExpression(), !52) %2 = sext i32 %1 to i64, !dbg !53 is converted to: %0:gr64_nosp = MOVSX64rm32 $rip, 1, $noreg, @GloBaRR, $noreg, debug-instr-number 1, debug-location !51 DBG_VALUE $noreg, $noreg, !"Idx", !DIExpression(), debug-location !52 The DBG_VALUE needs to be transferred correctly to the new combined instruction, and it needs to be appended with a DIExpression which contains a DW_OP_LLVM_fragment, describing that the lower bits of the virtual register contain the value. This patch fixes the above described problem.
1 parent 23a22d0 commit e18ad47

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/Analysis/TargetLibraryInfo.h"
3333
#include "llvm/Analysis/ValueTracking.h"
3434
#include "llvm/Analysis/VectorUtils.h"
35+
#include "llvm/BinaryFormat/Dwarf.h"
3536
#include "llvm/CodeGen/ByteProvider.h"
3637
#include "llvm/CodeGen/DAGCombine.h"
3738
#include "llvm/CodeGen/ISDOpcodes.h"
@@ -51,6 +52,7 @@
5152
#include "llvm/IR/Attributes.h"
5253
#include "llvm/IR/Constant.h"
5354
#include "llvm/IR/DataLayout.h"
55+
#include "llvm/IR/DebugInfoMetadata.h"
5456
#include "llvm/IR/DerivedTypes.h"
5557
#include "llvm/IR/Function.h"
5658
#include "llvm/IR/Metadata.h"
@@ -78,6 +80,7 @@
7880
#include <variant>
7981

8082
#include "MatchContext.h"
83+
#include "SDNodeDbgValue.h"
8184

8285
using namespace llvm;
8386
using namespace llvm::SDPatternMatch;
@@ -14444,14 +14447,51 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
1444414447
LN0->getMemOperand());
1444514448
Combiner.ExtendSetCCUses(SetCCs, N0, ExtLoad, ExtOpc);
1444614449
// If the load value is used only by N, replace it via CombineTo N.
14447-
bool NoReplaceTrunc = SDValue(LN0, 0).hasOneUse();
14450+
SDValue OldLoadVal(LN0, 0);
14451+
SDValue OldSextValue(N, 0);
14452+
bool NoReplaceTrunc = OldLoadVal.hasOneUse();
1444814453
Combiner.CombineTo(N, ExtLoad);
14454+
14455+
// Because we are replacing a load and a sext with a load-sext instruction,
14456+
// the dbg_value attached to the load will be of a smaller bit width, and we
14457+
// have to add a DW_OP_LLVM_fragment to the DIExpression.
14458+
auto SalvageToOldLoadSize = [&](SDValue From, SDValue To64) {
14459+
for (SDDbgValue *Dbg : DAG.GetDbgValues(From.getNode())) {
14460+
unsigned VarBits = From->getValueSizeInBits(0);
14461+
14462+
// Build/append a fragment expression [0, VarBits]
14463+
const DIExpression *OldE = Dbg->getExpression();
14464+
auto NewE = DIExpression::createFragmentExpression(OldE, 0, VarBits);
14465+
14466+
// Create a new SDDbgValue that points at the widened node with the
14467+
// fragment.
14468+
if (!NewE)
14469+
continue;
14470+
SDDbgValue *NewDV = DAG.getDbgValue(
14471+
Dbg->getVariable(), *NewE, To64.getNode(), To64.getResNo(),
14472+
Dbg->isIndirect(), Dbg->getDebugLoc(), Dbg->getOrder());
14473+
DAG.AddDbgValue(NewDV, /*isParametet*/ false);
14474+
}
14475+
};
14476+
1444914477
if (NoReplaceTrunc) {
14478+
if (LN0->getHasDebugValue()) {
14479+
DAG.transferDbgValues(OldLoadVal, ExtLoad);
14480+
SalvageToOldLoadSize(OldLoadVal, ExtLoad);
14481+
}
14482+
if (N->getHasDebugValue())
14483+
DAG.transferDbgValues(OldSextValue, ExtLoad);
1445014484
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
1445114485
Combiner.recursivelyDeleteUnusedNodes(LN0);
1445214486
} else {
1445314487
SDValue Trunc =
1445414488
DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(), ExtLoad);
14489+
if (LN0->getHasDebugValue()) {
14490+
DAG.transferDbgValues(OldLoadVal, Trunc);
14491+
SalvageToOldLoadSize(OldLoadVal, Trunc);
14492+
}
14493+
if (N->getHasDebugValue())
14494+
DAG.transferDbgValues(OldSextValue, Trunc);
1445514495
Combiner.CombineTo(LN0, Trunc, ExtLoad.getValue(1));
1445614496
}
1445714497
return SDValue(N, 0); // Return N so it doesn't get rechecked!
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
# RUN: llc -O2 %s -mtriple=x86_64-unkown-linux -start-before=x86-isel -stop-after=x86-isel -o - | FileCheck %s --check-prefix=MIR
3+
# RUN: llc -O2 %s -start-before=x86-isel -mtriple=x86_64-unkown-linux --filetype=obj -o %t.o
4+
# RUN: llvm-dwarfdump %t.o --name Idx | FileCheck %s --check-prefix=DUMP
5+
6+
# MIR: ![[IDX:[0-9]+]] = !DILocalVariable(name: "Idx"
7+
# MIR-LABEL: bb.0
8+
# MIR: %{{[0-9a-zA-Z]+}}{{.*}} = MOVSX64rm32 ${{.*}}, 1, $noreg, @GlobArr, $noreg, debug-instr-number [[INSTR_NUM:[0-9]+]]
9+
# MIR-NEXT: DBG_INSTR_REF ![[IDX]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref([[INSTR_NUM]], 0)
10+
# MIR-NEXT DBG_INSTR_REF ![[IDX]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_fragment, 0, 32), dbg-instr-ref([[INSTR_NUM]], 0)
11+
12+
# DUMP: DW_AT_location (indexed ({{[0-9]+}}x{{[0-9]+}}) loclist = 0x{{[0-9]+}}:
13+
# DUMP-NEXT: [0x{{[0-9]+}}, 0x{{[0-9]+}}): {{.*}}, DW_OP_piece 0x4
14+
15+
--- |
16+
@GlobArr = dso_local local_unnamed_addr global [5 x i32] [i32 1, i32 1, i32 2, i32 3, i32 5], align 16, !dbg !0
17+
@__const.main.Data = private unnamed_addr constant [7 x i32] [i32 10, i32 20, i32 30, i32 40, i32 50, i32 60, i32 70], align 16
18+
define dso_local void @_Z8useValuei(i32 noundef %0) local_unnamed_addr #0 !dbg !22 {
19+
ret void, !dbg !28
20+
}
21+
define dso_local noundef i32 @main() local_unnamed_addr #1 !dbg !29 {
22+
%1 = load i32, ptr @GlobArr
23+
#dbg_value(i32 %1, !43, !DIExpression(), !52)
24+
%2 = sext i32 %1 to i64
25+
%3 = getelementptr inbounds i32, ptr @__const.main.Data, i64 %2
26+
%4 = load i32, ptr %3
27+
tail call void @_Z8useValuei(i32 noundef %4), !dbg !56
28+
ret i32 0
29+
}
30+
!llvm.dbg.cu = !{!2}
31+
!llvm.module.flags = !{!10, !11, !16}
32+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
33+
!1 = distinct !DIGlobalVariable(type: !6, isDefinition: true)
34+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, emissionKind: FullDebug, nameTableKind: None)
35+
!3 = !DIFile(filename: "/tmp/test.cpp", directory: "/Users/srastogi/Development/llvm-project/build_ninja", checksumkind: CSK_MD5, checksum: "0fe735937e606b4db3e3b2e9253eff90")
36+
!6 = !DICompositeType(tag: DW_TAG_array_type, elements: !8)
37+
!7 = !DIBasicType()
38+
!8 = !{}
39+
!10 = !{i32 7, !"Dwarf Version", i32 5}
40+
!11 = !{i32 2, !"Debug Info Version", i32 3}
41+
!16 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
42+
!22 = distinct !DISubprogram(type: !23, unit: !2, keyInstructions: true)
43+
!23 = !DISubroutineType(types: !24)
44+
!24 = !{}
45+
!28 = !DILocation(scope: !22, atomRank: 1)
46+
!29 = distinct !DISubprogram(type: !30, unit: !2, keyInstructions: true)
47+
!30 = !DISubroutineType(types: !31)
48+
!31 = !{}
49+
!38 = distinct !DILexicalBlock(scope: !29, line: 5, column: 3)
50+
!43 = !DILocalVariable(name: "Idx", scope: !44, type: !7)
51+
!44 = distinct !DILexicalBlock(scope: !38, line: 5, column: 3)
52+
!46 = distinct !DILexicalBlock(scope: !44, line: 5, column: 27)
53+
!52 = !DILocation(scope: !44)
54+
!56 = !DILocation(scope: !46)

0 commit comments

Comments
 (0)