[Bug c++/98641] Feature request: implement pointer alignment builtins

Alexander.Richardson at cl dot cam.ac.uk gcc-bugzilla@gcc.gnu.org
Wed Jan 13 12:53:32 GMT 2021


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

--- Comment #6 from Alex Richardson <Alexander.Richardson at cl dot cam.ac.uk> ---
(In reply to Jakub Jelinek from comment #3)
> assume_aligned is something different, I guess __builtin_is_aligned expands
> to an actual runtime check (perhaps optimized away if the pointer can't be
> misaligned).
> So I guess __builtin_is_aligned (__builtin_assume_aligned (ptr, 32), 16)
> should optimize to true...
> 
> I guess I have big questions on what exactly will it do during constexpr
> evaluation - if it is on a pointer to object with certain alignment, for
> smaller alignment arguments guess it is just like pointer arithmetics then,
> but if it asks for larger alignment, shall it make it non-constant
> expression?

Regarding your second question: If folding is possible during constant
evaluation, clang will fold it even at -O0. If it's unknown, the LLVM IR
optimization passes will attempt to fold it (taking into account
__builtin_assume_aligned metadata if present).

For example, in:

static inline _Bool is_aligned_wrapper(void* x) {
    return __builtin_is_aligned(x, 4);
}

_Bool check_unknown(void* x) {
    return is_aligned_wrapper(x);
}

_Bool check_known_4_byte_alignment(void) {
    int i;
    return is_aligned_wrapper(&i);
}

_Bool check_assume_4_byte_alignment(void* x) {
    return is_aligned_wrapper(__builtin_assume_aligned(x, 4));
}

The check_unknown() function will perform a run-time check, the other two
simply return true.

https://godbolt.org/z/v4Gaaj


More information about the Gcc-bugs mailing list