Bug 24647

Summary: two copies of a constant in two different registers
Product: gcc Reporter: Andrew Pinski <pinskia>
Component: targetAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED WORKSFORME    
Severity: normal CC: gcc-bugs, pawel_sikora, steven
Priority: P3 Keywords: missed-optimization
Version: 4.1.0   
Target Milestone: ---   
Host: Target: i686-pc-linux-gnu
Build: Known to work:
Known to fail: Last reconfirmed: 2007-07-01 00:38:11
Bug Depends on:    
Bug Blocks: 16996    

Description Andrew Pinski 2005-11-03 01:15:13 UTC
Take the following code:
int f(void)
{
  static int i;
  int i1;
  i1 = i;
  if (i1 == 0)
    i = i1 = 2;
  return i1;
}
----
Currently we generate:
f:
    movl i.1285, %eax
    pushl   %ebp
    movl %esp, %ebp
    testl   %eax, %eax
    jne .L7
    movl $2, %ecx
    movl $2, %eax
    movl %ecx, i.1285
.L7:
    popl %ebp
    ret


Why is 2 in two different registers (I think the issue here is really a target issue)?  But should postreload CSE detect this or postreload GCSE?
Comment 1 Pawel Sikora 2006-02-02 14:15:06 UTC
on x86-64 I get:

f:      movl    i.0(%rip), %eax
        testl   %eax, %eax
        jne     .L2
        movb    $2, %al
        movl    $2, i.0(%rip)
.L2:    rep ; ret

$ pr24647.c.t97.final_cleanup
f() {
  static int i;
  int i1;
  static int i;
<bb 0>:
  i1 = i;
  if (i1 == 0) goto <L0>; else goto <L1>;
<L0>:;
  i = 2;
  i1 = 2;
<L1>:;
  return i1;
}
Comment 2 Andrew Pinski 2006-08-21 06:22:12 UTC
(In reply to comment #1)
> on x86-64 I get:

That is what I get on x86 also now:
f:
        movl    i.1523, %eax
        pushl   %ebp
        movl    %esp, %ebp
        testl   %eax, %eax
        jne     .L2
        movl    $2, i.1523
        movb    $2, %al
.L2:
        popl    %ebp
        ret

But just change "== 0" to "!= 0" and you get the problem still.  Also this is a code size issue for -Os as movl with a constant takes up too much space.
Comment 3 Steven Bosscher 2019-03-06 08:43:27 UTC
Trunk today:

f():
        movl    f()::i, %eax
        testl   %eax, %eax
        jne     .L1		# or je for the != case
        movl    $2, f()::i
        movl    $2, %eax
.L1:
        ret