public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "absoler at smail dot nju.edu.cn" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/107050] New: duplicate load of return value when facing multiple branches
Date: Tue, 27 Sep 2022 08:53:41 +0000	[thread overview]
Message-ID: <bug-107050-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 107050
           Summary: duplicate load of return value when facing multiple
                    branches
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: absoler at smail dot nju.edu.cn
  Target Milestone: ---

given this code:

int g_286 = (-5L);
int p;
int f = 1;

void func_58();

func_31(int c, int d) {
        if (c) {
                if (f){
                        if (d)
                                func_58();
                        return g_286;
                }
                g_286 = 0;
        }
        return 0;
}
func_58() {
        int arr[30];
        p = arr[0];
}

when compiled with gcc-12.1.0 (-O1), it will generate:

0000000000401186 <func_58>:
  401186:       48 83 ec 10             sub    $0x10,%rsp
  40118a:       8b 44 24 88             mov    -0x78(%rsp),%eax
  40118e:       89 05 f4 8c 00 00       mov    %eax,0x8cf4(%rip)        #
409e88 <p>
  401194:       48 83 c4 10             add    $0x10,%rsp
  401198:       c3                      retq   

0000000000401199 <func_31>:
  401199:       89 f8                   mov    %edi,%eax
  40119b:       85 ff                   test   %edi,%edi
  40119d:       74 1f                   je     4011be <func_31+0x25>
  40119f:       8b 05 bb 2e 00 00       mov    0x2ebb(%rip),%eax        #
404060 <f>
  4011a5:       85 c0                   test   %eax,%eax
  4011a7:       75 0b                   jne    4011b4 <func_31+0x1b>
  4011a9:       c7 05 b1 2e 00 00 00    movl   $0x0,0x2eb1(%rip)        #
404064 <g_286>
  4011b0:       00 00 00 
  4011b3:       c3                      retq   
  4011b4:       8b 05 aa 2e 00 00       mov    0x2eaa(%rip),%eax        #
404064 <g_286>
  4011ba:       85 f6                   test   %esi,%esi
  4011bc:       75 01                   jne    4011bf <func_31+0x26>
  4011be:       c3                      retq   
  4011bf:       48 83 ec 08             sub    $0x8,%rsp
  4011c3:       b8 00 00 00 00          mov    $0x0,%eax
  4011c8:       e8 b9 ff ff ff          callq  401186 <func_58>
  4011cd:       8b 05 91 2e 00 00       mov    0x2e91(%rip),%eax        #
404064 <g_286>
  4011d3:       48 83 c4 08             add    $0x8,%rsp
  4011d7:       c3                      retq 

we can see in the func_31, compiler choose to load g_286 before judge whether d
!= 0, and if it's true, %eax will be used and func_58 is called. Before return,
g_286 will be loaded again to %eax

             reply	other threads:[~2022-09-27  8:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-27  8:53 absoler at smail dot nju.edu.cn [this message]
2022-09-27  9:20 ` [Bug rtl-optimization/107050] " rguenth at gcc dot gnu.org
2022-09-27 19:53 ` segher at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107050-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).