Issue 184762
Summary Pessimistic code generation for `next_multiple_of` for powers of two
Labels new issue
Assignees
Reporter coastalwhite
    LLVM is unable to optimize `a.div_ceil(64) * 64` to `(a + 63) & ~0x3F`. This seems to apply to all powers of two. The result is worse code.

This was originally noticed in Rust, but it also applies in `C`. Here is a Godbolt link to illustrate the problem.

<https://godbolt.org/z/vE6nqKqer>

```c
#include <stdint.h>

typedef uint32_t u32;

static inline u32 next_multiple_of(u32 a, u32 b) {
    u32 r = a % b;
    if (r == 0) {
        return a;
    } else {
        return a + (b - r);
    }
}

static inline u32 div_ceil(u32 a, u32 b) {
    u32 d = a / b;
    u32 r = a % b;
    if (r > 0) {
        return d + 1;
    } else {
        return d;
    }
}

// Generates worse code.
u32 next_multiple_of_64_div_ceil(u32 v) {
    return div_ceil(v, 64) * 64;
}

// Generates correct code.
u32 next_multiple_of_64(u32 v) {
    return next_multiple_of(v, 64);
}

// Generates correct code.
u32 next_multiple_of_64_raw(u32 v) {
    return (v + 63) & ~((u32)0x3F);
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to