https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89681

            Bug ID: 89681
           Summary: Incorrect source positions on O1 for simple code case
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alahay01 at gcc dot gnu.org
  Target Milestone: ---

Created attachment 45949
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45949&action=edit
source code

Compiling a simple test case with O1 gives incorrect line numbers for two of
the lines of assembly.

Tested this on HEAD, 6.4.0, 7.2.0, using both X86-64 and AArch64. Debug results
are the same for all.

$ gcc --version
gcc (Ubuntu 7.2.0-1ubuntu1~16.04) 7.2.0

$ cat -n consume.c
     1  extern void consume(int);
     2  void testfunc(int **p) {
     3    int *a = *p;
     4    int b = *a;
     5    consume(b);
     6  }

$ gcc -O1 -g consume.c -c -fdump-rtl-all
$ objdump -S -l -d consume.o

consume.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <testfunc>:
testfunc():
/home/alahay01/consume.c:2
extern void consume(int);
void testfunc(int **p) {
   0:   48 83 ec 08             sub    $0x8,%rsp
/home/alahay01/consume.c:4
  int *a = *p;
  int b = *a;
   4:   48 8b 07                mov    (%rdi),%rax
/home/alahay01/consume.c:5
  consume(b);
   7:   8b 38                   mov    (%rax),%edi
   9:   e8 00 00 00 00          callq  e <testfunc+0xe>
/home/alahay01/consume.c:6
}
   e:   48 83 c4 08             add    $0x8,%rsp
  12:   c3                      retq


>The two MOV instructions are on the wrong line. They are given lines 4 and 5. 
>They should have lines 3 and 4.



$ cat consume.c.229r.expand

;; Function testfunc (testfunc, funcdef_no=0, decl_uid=1795, cgraph_uid=0,
symbol_order=0)


;; Generating RTL for gimple basic block 2


try_optimize_cfg iteration 1

Merging block 3 into block 2...
Merged blocks 2 and 3.
Merged 2 and 3 without moving.
Merging block 4 into block 2...
Merged blocks 2 and 4.
Merged 2 and 4 without moving.


try_optimize_cfg iteration 2



;;
;; Full RTL generated for this function:
;;
(note 1 0 4 NOTE_INSN_DELETED)
(note 4 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
(insn 2 4 3 2 (set (reg/v/f:DI 89 [ p ])
        (reg:DI 5 di [ p ])) "consume.c":2 -1
     (nil))
(note 3 2 6 2 NOTE_INSN_FUNCTION_BEG)
(debug_insn 6 3 7 2 (var_location:DI a (mem/f:DI (reg/v/f:DI 89 [ p ]) [1
*p_2(D)+0 S8 A64])) "consume.c":3 -1
     (nil))
(debug_insn 7 6 8 2 (var_location:SI b (mem:SI (mem/f:DI (reg/v/f:DI 89 [ p ])
[1 *p_2(D)+0 S8 A64]) [2 *a_3+0 S4 A32])) "consume.c":4 -1
     (nil))
(insn 8 7 9 2 (set (reg/f:DI 90)
        (mem/f:DI (reg/v/f:DI 89 [ p ]) [1 *p_2(D)+0 S8 A64])) "consume.c":4 -1
     (nil))
(insn 9 8 10 2 (set (reg:SI 5 di)
        (mem:SI (reg/f:DI 90) [2 *a_3+0 S4 A32])) "consume.c":5 -1
     (nil))
(call_insn 10 9 0 2 (call (mem:QI (symbol_ref:DI ("consume") [flags 0x41] 
<function_decl 0x7faaba359e00 consume>) [0 consume S1 A8])
        (const_int 0 [0])) "consume.c":5 -1
     (nil)
    (expr_list:SI (use (reg:SI 5 di))
        (nil)))


>Looks to me like it goes wrong in the expand phase. There are two debug_insn's 
>for lines 3 and 4. But, then there are two insn's for line 4 and 5. I think 
>these are incorrect, and should be lines 3 and 4.



>With O0 the results are correct:

$ gcc -O0 -g consume.c -c -fdump-rtl-all
$ objdump -S -l -d consume.o

consume.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <testfunc>:
testfunc():
/home/alahay01/consume.c:2
extern void consume(int);
void testfunc(int **p) {
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 20             sub    $0x20,%rsp
   8:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
/home/alahay01/consume.c:3
  int *a = *p;
   c:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  10:   48 8b 00                mov    (%rax),%rax
  13:   48 89 45 f8             mov    %rax,-0x8(%rbp)
/home/alahay01/consume.c:4
  int b = *a;
  17:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  1b:   8b 00                   mov    (%rax),%eax
  1d:   89 45 f4                mov    %eax,-0xc(%rbp)
/home/alahay01/consume.c:5
  consume(b);
  20:   8b 45 f4                mov    -0xc(%rbp),%eax
  23:   89 c7                   mov    %eax,%edi
  25:   e8 00 00 00 00          callq  2a <testfunc+0x2a>
/home/alahay01/consume.c:6
}
  2a:   90                      nop
  2b:   c9                      leaveq
  2c:   c3                      retq


$ cat consume.c.229r.expand

;; Function testfunc (testfunc, funcdef_no=0, decl_uid=1795, cgraph_uid=0,
symbol_order=0)

Partition 0: size 8 align 8
        a_3
Partition 1: size 4 align 4
        b_4

;; Generating RTL for gimple basic block 2


try_optimize_cfg iteration 1

Merging block 3 into block 2...
Merged blocks 2 and 3.
Merged 2 and 3 without moving.
Merging block 4 into block 2...
Merged blocks 2 and 4.
Merged 2 and 4 without moving.


try_optimize_cfg iteration 2



;;
;; Full RTL generated for this function:
;;
(note 1 0 4 NOTE_INSN_DELETED)
(note 4 1 2 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
(insn 2 4 3 2 (set (mem/f/c:DI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -24 [0xffffffffffffffe8])) [3 p+0 S8 A64])
        (reg:DI 5 di [ p ])) "consume.c":2 -1
     (nil))
(note 3 2 6 2 NOTE_INSN_FUNCTION_BEG)
(insn 6 3 7 2 (set (reg/f:DI 87)
        (mem/f/c:DI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -24 [0xffffffffffffffe8])) [3 p+0 S8 A64]))
"consume.c":3 -1
     (nil))
(insn 7 6 8 2 (set (reg/f:DI 88)
        (mem/f:DI (reg/f:DI 87) [1 *p_2(D)+0 S8 A64])) "consume.c":3 -1
     (nil))
(insn 8 7 9 2 (set (mem/f/c:DI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -8 [0xfffffffffffffff8])) [1 a+0 S8 A64])
        (reg/f:DI 88)) "consume.c":3 -1
     (nil))
(insn 9 8 10 2 (set (reg/f:DI 89)
        (mem/f/c:DI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -8 [0xfffffffffffffff8])) [1 a+0 S8 A64]))
"consume.c":4 -1
     (nil))
(insn 10 9 11 2 (set (reg:SI 90)
        (mem:SI (reg/f:DI 89) [2 *a_3+0 S4 A32])) "consume.c":4 -1
     (nil))
(insn 11 10 12 2 (set (mem/c:SI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -12 [0xfffffffffffffff4])) [2 b+0 S4 A32])
        (reg:SI 90)) "consume.c":4 -1
     (nil))
(insn 12 11 13 2 (set (reg:SI 91)
        (mem/c:SI (plus:DI (reg/f:DI 82 virtual-stack-vars)
                (const_int -12 [0xfffffffffffffff4])) [2 b+0 S4 A32]))
"consume.c":5 -1
     (nil))
(insn 13 12 14 2 (set (reg:SI 5 di)
        (reg:SI 91)) "consume.c":5 -1
     (nil))
(call_insn 14 13 17 2 (call (mem:QI (symbol_ref:DI ("consume") [flags 0x41] 
<function_decl 0x7f517cca5e00 consume>) [0 consume S1 A8])
        (const_int 0 [0])) "consume.c":5 -1
     (nil)
    (expr_list:SI (use (reg:SI 5 di))
        (nil)))
(insn 17 14 0 2 (const_int 0 [0]) "consume.c":6 -1
     (nil))

Reply via email to