Issue 98212
Summary Failure to CSE loads/stores
Labels missed-optimization
Assignees
Reporter Kmeakin
    # Loads

https://godbolt.org/z/55de88Ph6

Each `if` in `src` shares a common prefix of loads with the `if`s above it, but in the resulting assembly, the loads are repeated in the body of each `if` rather than being CSE'd as has been done manually in `tgt`. 

```c
#include <stdint.h>

typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;

u32 src(u64 len, uint8_t *xs) {
    if (len == 0) {
        return 0;
    }

    if (len == 1) {
        return xs[0];
    }

    if (len == 2) {
        return xs[0] + xs[1];
    }

    if (len == 3) {
        return xs[0] + xs[1] + xs[2];
    }

    return xs[0] + xs[1] + xs[2] + xs[3];
}

u32 tgt(u64 len, uint8_t *xs) {
    if (len == 0) {
 return 0;
    }

    u8 x0 = xs[0];
    if (len == 1) {
        return x0;
    }

    u8 x1 = xs[1];
    if (len == 2) {
        return x0 + x1;
    }

    u8 x2 = xs[2];
    if (len == 3) {
        return x0 + x1 + x2;
    }

    u8 x3 = xs[3];
    return x0 + x1 + x2 + x3;
}
```

# Stores
https://godbolt.org/z/4G171qhrW

Similarly for stores:
```c
#include <stdint.h>

typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;

void src(u64 len, uint8_t *xs, uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3) {
    if (len == 0) {
 return;
    } else if (len == 1) {
        xs[0] = x0;
    } else if (len == 2) {
        xs[0] = x0;
        xs[1] = x1;
    } else if (len == 3) {
        xs[0] = x0;
        xs[1] = x1;
 xs[2] = x2;
    } else {
        xs[0] = x0;
        xs[1] = x1;
 xs[2] = x2;
        xs[3] = x3;
    }
}

void tgt(u64 len, uint8_t *xs, uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3) {
    if (len == 0) {
        return;
    }

    xs[0] = x0;
    if (len == 1) {
        return;
    }

    xs[1] = x1;
    if (len == 2) {
        return;
    }

    xs[2] = x3;
    if (len == 3) {
        return;
    }

    xs[3] = x3;
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to