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

            Bug ID: 85971
           Summary: Really Simple "If" with one function call inside is
                    not optimized efficiently
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mcccs at gmx dot com
  Target Milestone: ---

GCC: 8 or 9/trunk

Optimization: O3 or Ofast

Code:

```
int PolyMod(int s);
void CreateChecksum(int isTestNet, int *mod) {
    if (isTestNet == 0) {
        *mod = PolyMod(5);
    } else {
        *mod = PolyMod(9);
    }
}
```

It is optimized very inefficiently. The
assembly has one branch.

However, if the compiler was as smart as the
people who develop it, he'd transform the code into
this:

```
int PolyMod(int s);
void CreateChecksum(int isTestNet, int *mod) {
    int a;
    if (isTestNet == 0) {
        a = 5;
    } else {
        a = 9;
    }
    *mod = PolyMod(a);
}
```

which compiles to assembly with zero branches.
Another way to reach the same efficient assembly would be

```
int PolyMod(int s);
void CreateChecksum(int isTestNet, int *mod) {
        *mod = PolyMod(isTestNet == 0 ? 5 : 9);
}
```

So, the compiler has problems seeing little powerful
argument optimizations.

Reply via email to