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

            Bug ID: 114489
           Summary: introduced redundant load facing different branches
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: absoler at smail dot nju.edu.cn
  Target Milestone: ---

similar with https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107050, but without
function call.

```
int f = 1;
int g1, g2, arr[10];

int func_1() {
    int a = 4;
    int *p1 = &g1;
    if (f) {
        arr[0] = g2;
        int *p2 = &a;
        a = 1;
    }
    *p1 &= (a > 9) - 5;
    return a;
}

int main() {
    func_1();
}
```
gcc-13.2.0 -O1 generates:
```
func_1():
  401106:       mov    0x2f80(%rip),%eax        # 40408c <g1>  # first load
  40110c:       mov    $0x4,%edx
  401111:       cmpl   $0x0,0x2f10(%rip)        # 404028 <f>
  401118:       je     401131 <func_1+0x2b>
  40111a:       mov    0x2f68(%rip),%eax        # 404088 <g2>
  401120:       mov    %eax,0x2f3a(%rip)        # 404060 <arr>
  401126:       mov    0x2f60(%rip),%eax        # 40408c <g1>  # second load
  40112c:       mov    $0x1,%edx
  401131:       and    $0xfffffffb,%eax
  401134:       mov    %eax,0x2f52(%rip)        # 40408c <g1>
  40113a:       mov    %edx,%eax
  40113c:       retq   
```

I wonder why compiler won't use another register to hold `g2`, so that the
second load is unnecessary.

Reply via email to