]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
Expose clang's alignment builtins and use them for roundup2/rounddown2
authorAlex Richardson <arichardson@FreeBSD.org>
Wed, 3 Feb 2021 15:27:17 +0000 (15:27 +0000)
committerDimitry Andric <dim@FreeBSD.org>
Tue, 31 Aug 2021 19:24:52 +0000 (21:24 +0200)
commit3b2ae1c2bbfba2173c2b7f9c8f6a1382dcb4f081
treee35f9be0f723b47965b49c0d56bdb7508f30114e
parentbfa89a5dd1055447ee64e0a4cf77898ee970a3b1
Expose clang's alignment builtins and use them for roundup2/rounddown2

This makes roundup2/rounddown2 type- and const-preserving and allows
using it on pointer types without casting to uintptr_t first. Not
performing pointer-to-integer conversions also helps the compiler's
optimization passes and can therefore result in better code generation.
When using it with integer values there should be no change other than
the compiler checking that the alignment value is a valid power-of-two.

I originally implemented these builtins for CHERI a few years ago and
they have been very useful for CheriBSD. However, they are also useful
for non-CHERI code so I was able to upstream them for Clang 10.0.

Rationale from the clang documentation:
Clang provides builtins to support checking and adjusting alignment
of pointers and integers. These builtins can be used to avoid relying
on implementation-defined behavior of arithmetic on integers derived
from pointers. Additionally, these builtins retain type information
and, unlike bitwise arithmetic, they can perform semantic checking on
the alignment value.

There is also a feature request for GCC, so GCC may also support it in
the future: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98641

Reviewed By: brooks, jhb, imp
Differential Revision: https://reviews.freebsd.org/D28332

(cherry picked from commit 8fa6abb6f4f64f4f23e2920e2aea7996566851a4)
sys/sys/cdefs.h
sys/sys/param.h