512B bb.2.for.body:
520B MOVaddr
524B MOVi32imm
DBG_VALUE $x19, $noreg, !"i", <<<<<<<<<<<<<<<
Above DBG_VALUE should be placed before MOV* instructions. Test case presented after
the problem description.
DBG_VALUE instruction is placed incorrectly because wrong debug variable range
is calculated at "COMPUTING LIVE DEBUG VARIABLES" stage. Range for debug variable
instruction (for "i" variable) is computed according to the current state of instructions
inside of loop basic block. But later - Register Allocator creates new instructions
which are not taken into account when Live Debug Variables are computed. Please check
real data(full log in the end): after COMPUTING LIVE DEBUG VARIABLES there is following
instruction range for "i" variable - [544B;848r). It is shorter than Live Interval for
corresponding register [512B,848r:1). Register Allocator inserts two instructions
before 544B. As the result Debug Value record is inserted after these additional
instructions. That is incorrect. Debug Value for the loop counter should be inserted
before any loop instruction.
related reviews: D35953, https://bugs.llvm.org/show_bug.cgi?id=33730, D46599, https://bugs.llvm.org/show_bug.cgi?id=37149.
Debug variable range become incorrect when it was trimmed
at lib/CodeGen/LiveDebugVariables.cpp:
if (I.start() < RStart) {
// Interval start overlaps range - trim to the scope range. I.setStartUnchecked(RStart); // Remember that this interval was trimmed. trimmedDefs.insert(RStart);
}
The fix is to not trim Debug Variable Range.
It does not brake live-debug-variables.ll(no new .debug_loc entries).
It does not affect compilation time for test case from https://bugs.llvm.org/show_bug.cgi?id=33730.
cat test_debug_val.cpp:
void func(int, ...);
int array[0x100];
int main( int argc, char **argv )
{
int var = 56; int a1 = array[1]; int a2 = array[2]; int a3 = array[3]; int a4 = array[4]; int a5 = array[5]; int a6 = array[6]; int a7 = array[7]; int a8 = array[8]; int a9 = array[9]; int a10 = array[10]; for( int i = 0; i < 0x100; i++ ) { array[i] = var; func(0, i, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 ); } return 0;
}
clang -O3 -g -c --target=aarch64-unknown-linux -std=gnu++14 test_debug_val.cpp -S -mllvm -print-after-all -mllvm -debug
test_debug_val.s:
.Ltmp7:
//DEBUG_VALUE: main:a2 <- $w21
.LBB0_1: // %for.body
// =>This Inner Loop Header: Depth=1 ldr w2, [sp, #44] // 4-byte Folded Reload
.Ltmp8:
.loc 1 16 17 is_stmt 1 // test_debug_val.cpp:16:17 adrp x8, array add x8, x8, :lo12:array mov w9, #56
.Ltmp9:
//DEBUG_VALUE: i <- $x19
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEBUG_VALUE is reported not in the very beginning of Loop.
It is reported after several instructions.
print-after-all data:
- COMPUTING LIVE DEBUG VARIABLES: main **
- DEBUG VARIABLES **
!"i,14" [432B;448B):0 [544B;848r):1 Loc0=0 Loc1=%35
- * IR Dump After Debug Variable Analysis *:
- Machine code for function main: NoPHIs, TracksLiveness
512B bb.2.for.body:
; predecessors: %bb.0, %bb.2 successors: %bb.1(0x04000000), %bb.2(0x7c000000); %bb.1(3.12%), %bb.2(96.88%)
544B STRWroX %28:gpr32, %27:gpr64common, %35:gpr64common, 0, 1, debug-location !54 :: (store 4 into %ir.scevgep, !tbaa !38); test_debug_val.cpp:16:17
- INTERVALS **
%35 [8r,448B:0)[512B,848r:1)[848r,944B:2) 0@8r 1@512B-phi 2@848r weight:2.289400e-01
- * IR Dump After Greedy Register Allocator *:
- Machine code for function main: NoPHIs, TracksLiveness
512B bb.2.for.body:
; predecessors: %bb.0, %bb.2 successors: %bb.1(0x04000000), %bb.2(0x7c000000); %bb.1(3.12%), %bb.2(96.88%)
520B %37:gpr64common = MOVaddr target-flags(aarch64-page) @array, target-flags(aarch64-pageoff, aarch64-nc) @array, debug-location !54; test_debug_val.cpp:16:17
524B %39:gpr32 = MOVi32imm 56, debug-location !54; test_debug_val.cpp:16:17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
544B STRWroX %39:gpr32, %37:gpr64common, %35:gpr64common, 0, 1, debug-location !54 :: (store 4 into %ir.scevgep, !tbaa !38); test_debug_val.cpp:16:17
- * IR Dump After Virtual Register Rewriter *:
- Machine code for function main: NoPHIs, TracksLiveness, NoVRegs
512B bb.2.for.body:
; predecessors: %bb.0, %bb.2 successors: %bb.1(0x04000000), %bb.2(0x7c000000); %bb.1(3.12%), %bb.2(96.88%) liveins: $w20, $w21, $w22, $w23, $w24, $w25, $w26, $w27, $w28, $x19
520B renamable $x8 = MOVaddr target-flags(aarch64-page) @array, target-flags(aarch64-pageoff, aarch64-nc) @array, debug-location !54; test_debug_val.cpp:16:17
524B renamable $w9 = MOVi32imm 56, debug-location !54; test_debug_val.cpp:16:17
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DBG_VALUE $x19, $noreg, !"i", !DIExpression(), debug-location !51; test_debug_val.cpp:0 line no:14
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
544B STRWroX killed renamable $w9, killed renamable $x8, renamable $x19, 0, 1, debug-location !54 :: (store 4 into %ir.scevgep, !tbaa !38); test_debug_val.cpp:16:17
what is the debug location attached to the MOV, the DBG_VALUE, and the instructions in between?
If I understand the original code correctly, the intention was to not move a DBG_VALUE before the lexical scope of its variable starts.