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

            Bug ID: 87692
           Summary: Reuse guard variable for multiple initializations
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the following example:

int produce1();
int produce2(int ) noexcept;


auto test() {
    static int val1 = produce1();
    static int val2 = produce2(val1); // noexcept

    return val2 + val2;
}

For the above example two guard variables are generated: for `val1` and `val2`.
However the `val2` initialization always succeeds and may not happen before
`val1` initialization.

So instead of having two variables, `val2` could reuse the guard variable for
`val1`.

Using one variable has advantages: less code is generated and no need in
multiple guard variables checks.

Such optimization will reduce the code size and improve overall performance.

Reply via email to