<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/97162>97162</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missed optimizazion opportunity with anonymous structures
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          fekir
      </td>
    </tr>
</table>

<pre>
    Consider following code snippet

~~~~
struct {
    int i = 42;
} const a;


auto bar(){
    return a;
}
~~~~

clang optimizes the code to 

~~~~
        mov     eax, 42
 ret
~~~~

but the MSVC compiler does better, even without optimizations enabled.
It seems that it does not generate any code for the function bar at all!

It would be nice if clang can consistently avoid generating code for functions that are not used at all.

I've collected some examples here: https://godbolt.org/z/xEj51j8E7

Note that in case of `auto foo0(){return a;}` the code is generated, but for `decltype(a) foo2( ){return a;}` not (GCC has similar issue https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115670).

The cases that clang does seem to handle optimally are


~~~~
struct {
 int i = 42;
} const a;
namespace {
    struct s{};
}
auto foo0(){
    return a;
}
s foo4(){
    return {};
}

auto foo5(){
    struct {} t;
    return t;
}

struct s2{
    s i; //s is in anonymous namespace
};

s2 foo6(){
    return {};
}
~~~~

Also the diagnostic `-Wunused-function` can be improved, as it currently does not warn for those cases.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVctyozgXfhp5cyouEDaXBYt03P6rF_1vZmpmOSXEAatHSJR0ZMdZ5NmnBNixk85UTxYQQ-k7l--C8F71BrFm2y9su1uJQAfr6g7_Vm7V2PZcP1njVYsOOqu1PSnTg7QtgjdqHJFYsmPJ43x9fX19nf_z5IIkYMWX-TcAgDIECli2gw1n2fKCFTuQ1ngC8fbs5ioCWWiEY7xkvLrDc0jBmdtzxe59H_NVamF6sCOpQb2gBzrgPANZ-Hn_sPwN9jjdUTwz_hQbn1-7y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