Bug 28306 - const / pure call with ignored argument emitted.
Summary: const / pure call with ignored argument emitted.
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 29842 28289
  Show dependency treegraph
 
Reported: 2006-07-07 16:52 UTC by Jorn Wolfgang Rennecke
Modified: 2019-03-06 10:39 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-05 00:00:00


Attachments
test case (183 bytes, text/plain)
2006-07-07 16:54 UTC, Jorn Wolfgang Rennecke
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jorn Wolfgang Rennecke 2006-07-07 16:52:16 UTC
There is code in the expanders which is supposed to avoid emitting calls
to pure functions which have their result ignored, but it doesn't appear to
work when the function called is represented as a COND_EXPR.
Comment 1 Jorn Wolfgang Rennecke 2006-07-07 16:54:27 UTC
Created attachment 11850 [details]
test case

The available source code for the functions allows the compiler to
see that the called functions are const.
This should be sufficient for the compiler to figure out the call is not needed.
Likewise, the pure attributes should also be sufficient to figure this out.
Comment 2 Richard Biener 2006-07-07 17:30:13 UTC
Confirmed.
Comment 3 Richard Biener 2008-01-13 18:22:35 UTC
This works for 'const' functions because they stick on the function type as well.
The attributes are only on the DECL and do not get merged and transfered to the
resulting function type of the COND_EXPR.

So this again asks for streamlining attributes on types vs. decls.
Comment 4 Steven Bosscher 2019-03-05 09:01:27 UTC
GCC trunk today on x86-64 produces:

gen_x86_64_shrd(int):
        xorl    %eax, %eax
        ret
ok(int):
        movl    $1, %eax
        ret
ix86_split_ashr(int):
        testl   %edi, %edi
        movl    $ok(int), %eax
        movl    $gen_x86_64_shrd(int), %edx
        cmove   %rdx, %rax
        xorl    %edi, %edi
        jmp     *%rax


FWIW clang trunk also does not optimize this, yielding:
ix86_split_ashr(int):                   # @ix86_split_ashr(int)
        testl   %edi, %edi
        movl    $gen_x86_64_shrd(int), %eax
        movl    $ok(int), %ecx
        cmoveq  %rax, %rcx
        xorl    %edi, %edi
        jmpq    *%rcx                   # TAILCALL
ok(int):                                # @ok(int)
        movl    $1, %eax
        retq
gen_x86_64_shrd(int):                  # @gen_x86_64_shrd(int)
        xorl    %eax, %eax
        retq


ICC 19.0.1 _does_ optimize, resulting in:
ix86_split_ashr(int):
        ret                                                     #24.1
_INTERNALf242c0c5::gen_x86_64_shrd(int):
        xorl      %eax, %eax                                    #6.10
        ret                                                     #6.10
_INTERNALf242c0c5::ok(int):
        movl      $1, %eax                                      #12.10
        ret                                                     #12.10