@@ -2489,6 +2489,82 @@ are listed below.
24892489
24902490 $ clang -fuse-ld=lld -Oz -Wl,--icf=safe -fcodegen-data-use code.cc
24912491
2492+ .. _strict_aliasing :
2493+
2494+ Strict Aliasing
2495+ ---------------
2496+
2497+ The C and C++ standards require accesses to objects in memory to use l-values of
2498+ an appropriate type for the object. This is called *strict aliasing * or
2499+ *type-based alias analysis *. Strict aliasing enhances a variety of powerful
2500+ memory optimizations, including reordering, combining, and eliminating memory
2501+ accesses. These optimizations can lead to unexpected behavior in code that
2502+ violates the strict aliasing rules. For example:
2503+
2504+ .. code-block :: c++
2505+
2506+ void advance(size_t *index, double *data) {
2507+ double value = data[*index];
2508+ / * Clang may assume that this store does not change the contents of `data `. */
2509+ *index += 1;
2510+ / * Clang may assume that this store does not change the contents of `index `. */
2511+ data[*index] = value;
2512+ / * Either of these facts may create significant optimization opportunities
2513+ if Clang is able to inline this function. */
2514+ }
2515+
2516+ Strict aliasing can be explicitly enabled with ``-fstrict-aliasing `` and
2517+ disabled with ``-fno-strict-aliasing ``. ``clang-cl `` defaults to
2518+ ``-fno-strict-aliasing ``; see . Otherwise, Clang defaults to ``-fstrict-aliasing ``.
2519+
2520+ C and C++ specify slightly different rules for strict aliasing. To improve
2521+ language interoperability, Clang allows two types to alias if either language
2522+ would permit it. This includes applying the C++ similar types rule to C,
2523+ allowing ``int ** `` to alias ``int const * const * ``. Clang also relaxes the
2524+ standard aliasing rules in the following ways:
2525+
2526+ * All integer types of the same size are permitted to alias each other,
2527+ including signed and unsigned types.
2528+ * ``void* `` is permitted to alias any pointer type, ``void** `` is permitted to
2529+ alias any pointer to pointer type, and so on.
2530+
2531+ Code which violates strict aliasing has undefined behavior. A program that
2532+ works in one version of Clang may not work in another because of changes to the
2533+ optimizer. Clang provides a :doc: `TypeSanitizer ` to help detect
2534+ violations of the strict aliasing rules, but it is currently still experimental.
2535+ Code that is known to violate strict aliasing should generally be built with
2536+ ``-fno-strict-aliasing `` if the violation cannot be fixed.
2537+
2538+ Clang supports several ways to fix a violation of strict aliasing:
2539+
2540+ * L-values of the character types ``char `` and ``unsigned char `` (as well as
2541+ other types, depending on the standard) are permitted to access objects of
2542+ any type.
2543+
2544+ * Library functions such as ``memcpy `` and ``memset `` are specified as treating
2545+ memory as characters and therefore are not limited by strict aliasing. If a
2546+ value of one type must be reinterpreted as another (e.g. to read the bits of a
2547+ floating-point number), use ``memcpy `` to copy the representation to an object
2548+ of the destination type. This has no overhead over a direct l-value access
2549+ because Clang should reliably optimize calls to these functions to use simple
2550+ loads and stores when they are used with small constant sizes.
2551+
2552+ * The attribute ``may_alias `` can be added to a ``typedef `` to give l-values of
2553+ that type the same aliasing power as the character types.
2554+
2555+ Clang makes a best effort to avoid obvious miscompilations from strict aliasing
2556+ by only considering type information when it cannot prove that two accesses must
2557+ refer to the same memory. However, it is not recommended that programmers
2558+ intentionally rely on this instead of using one of the solutions above because
2559+ it is too easy for the compiler's analysis to be blocked in surprising ways.
2560+
2561+ In Clang 20, Clang strengthened its implementation of strict aliasing for
2562+ accesses of pointer type. Previously, all accesses of pointer type were
2563+ permitted to alias each other, but Clang now distinguishes different pointers
2564+ by their pointee type, except as limited by the relaxations around qualifiers
2565+ and ``void* `` described above. The previous behavior of treating all pointers as
2566+ aliasing can be restored using ``-fno-pointer-tbaa ``.
2567+
24922568Profile Guided Optimization
24932569---------------------------
24942570
@@ -5272,12 +5348,6 @@ The Visual C++ Toolset has a slightly more elaborate mechanism for detection.
52725348Restrictions and Limitations compared to Clang
52735349----------------------------------------------
52745350
5275- Strict Aliasing
5276- ^^^^^^^^^^^^^^^
5277-
5278- Strict aliasing (TBAA) is always off by default in clang-cl. Whereas in clang,
5279- strict aliasing is turned on by default for all optimization levels.
5280-
5281- To enable LLVM optimizations based on strict aliasing rules (e.g., optimizations
5282- based on type of expressions in C/C++), user will need to explicitly pass
5283- `-fstrict-aliasing ` to clang-cl.
5351+ Strict aliasing (TBAA) is always off by default in clang-cl whereas in clang,
5352+ strict aliasing is turned on by default for all optimization levels. For more
5353+ details, see :ref: `Strict aliasing <strict_aliasing >`.
0 commit comments