@@ -2125,43 +2125,104 @@ implemented directly in terms of :ref:`extended vector support
21252125<langext-vectors>` instead of builtins, in order to reduce the number of
21262126builtins that we need to implement.
21272127
2128- .. _langext-__builtin_assume :
2129-
2130- ``__builtin_assume ``
2131- ------------------------------
2128+ ``__builtin_alloca ``
2129+ --------------------
21322130
2133- ``__builtin_assume `` is used to provide the optimizer with a boolean
2134- invariant that is defined to be true .
2131+ ``__builtin_alloca `` is used to dynamically allocate memory on the stack. Memory
2132+ is automatically freed upon function termination .
21352133
21362134**Syntax **:
21372135
21382136.. code-block :: c++
21392137
2140- __builtin_assume (bool )
2138+ __builtin_alloca (size_t n )
21412139
21422140**Example of Use **:
21432141
21442142.. code-block :: c++
21452143
2146- int foo (int x) {
2147- __builtin_assume (x != 0);
2144+ void init (float* data, size_t nbelems);
2145+ void process (float* data, size_t nbelems);
2146+ int foo (size_t n) {
2147+ auto mem = (float*)__builtin_alloca (n * sizeof (float));
2148+ init (mem, n);
2149+ process (mem, n);
2150+ /* mem is automatically freed at this point */
2151+ }
2152+
2153+ **Description **:
2154+
2155+ ``__builtin_alloca `` is meant to be used to allocate a dynamic amount of memory
2156+ on the stack. This amount is subject to stack allocation limits.
2157+
2158+ Query for this feature with ``__has_builtin(__builtin_alloca) ``.
2159+
2160+ ``__builtin_alloca_with_align ``
2161+ -------------------------------
21482162
2149- // The optimizer may short-circuit this check using the invariant.
2150- if (x == 0)
2151- return do_something ();
2163+ `` __builtin_alloca_with_align `` is used to dynamically allocate memory on the
2164+ stack while controlling its alignment. Memory is automatically freed upon
2165+ function termination.
21522166
2153- return do_something_else ();
2167+
2168+ **Syntax **:
2169+
2170+ .. code-block :: c++
2171+
2172+ __builtin_alloca_with_align (size_t n, size_t align)
2173+
2174+ **Example of Use **:
2175+
2176+ .. code-block :: c++
2177+
2178+ void init (float* data, size_t nbelems);
2179+ void process (float* data, size_t nbelems);
2180+ int foo (size_t n) {
2181+ auto mem = (float*)__builtin_alloca_with_align (
2182+ n * sizeof (float),
2183+ CHAR_BIT * alignof (float));
2184+ init (mem, n);
2185+ process (mem, n);
2186+ /* mem is automatically freed at this point */
21542187 }
21552188
21562189**Description **:
21572190
2158- The boolean argument to this function is defined to be true. The optimizer may
2159- analyze the form of the expression provided as the argument and deduce from
2160- that information used to optimize the program. If the condition is violated
2161- during execution, the behavior is undefined. The argument itself is never
2162- evaluated, so any side effects of the expression will be discarded .
2191+ `` __builtin_alloca_with_align `` is meant to be used to allocate a dynamic amount of memory
2192+ on the stack. It is similar to `` __builtin_alloca `` but accepts a second
2193+ argument whose value is the alignment constraint, as a power of 2 in * bits *.
2194+
2195+ Query for this feature with `` __has_builtin(__builtin_alloca_with_align) `` .
21632196
2164- Query for this feature with ``__has_builtin(__builtin_assume) ``.
2197+ .. _langext-__builtin_assume :
2198+
2199+ ``__builtin_call_with_static_chain ``
2200+ ------------------------------------
2201+
2202+ ``__builtin_call_with_static_chain `` is used to perform a static call while
2203+ setting updating the static chain register.
2204+
2205+ **Syntax **:
2206+
2207+ .. code-block :: c++
2208+
2209+ T __builtin_call_with_static_chain (T expr, void* ptr)
2210+
2211+ **Example of Use **:
2212+
2213+ .. code-block :: c++
2214+
2215+ auto v = __builtin_call_with_static_chain (foo (3), foo);
2216+
2217+ **Description **:
2218+
2219+ This builtin returns ``expr `` after checking that ``expr `` is a non-member
2220+ static call expression. The call to that expression is made while using ``ptr ``
2221+ as a function pointer stored in a dedicated register to implement *static chain *
2222+ calling convention, as used by some language to implement closures or nested
2223+ functions.
2224+
2225+ Query for this feature with ``__has_builtin(__builtin_call_with_static_chain) ``.
21652226
21662227``__builtin_readcyclecounter ``
21672228------------------------------
@@ -2501,6 +2562,98 @@ flow conditions such as in ``if`` and ``switch`` statements.
25012562
25022563Query for this feature with ``__has_builtin(__builtin_unpredictable) ``.
25032564
2565+
2566+ ``__builtin_expect ``
2567+ --------------------
2568+
2569+ ``__builtin_expect `` is used to indicate that the value of an expression is
2570+ anticipated to be the same as a statically known result.
2571+
2572+ **Syntax **:
2573+
2574+ .. code-block :: c++
2575+
2576+ long __builtin_expect (long expr, long val)
2577+
2578+ **Example of use **:
2579+
2580+ .. code-block :: c++
2581+
2582+ if (__builtin_expect (x, 0)) {
2583+ bar ();
2584+ }
2585+
2586+ **Description **:
2587+
2588+ The ``__builtin_expect() `` builtin is typically used with control flow
2589+ conditions such as in ``if `` and ``switch `` statements to help branch
2590+ prediction. It means that its first argument ``expr `` is expected to take the
2591+ value of its second argument ``val ``. It always returns ``expr ``.
2592+
2593+ Query for this feature with ``__has_builtin(__builtin_expect) ``.
2594+
2595+ ``__builtin_expect_with_probability ``
2596+ -------------------------------------
2597+
2598+ ``__builtin_expect_with_probability `` is similar to ``__builtin_expect `` but it
2599+ takes a probability as third argument.
2600+
2601+ **Syntax **:
2602+
2603+ .. code-block :: c++
2604+
2605+ long __builtin_expect_with_probability (long expr, long val, double p)
2606+
2607+ **Example of use **:
2608+
2609+ .. code-block :: c++
2610+
2611+ if (__builtin_expect_with_probability (x, 0, .3)) {
2612+ bar ();
2613+ }
2614+
2615+ **Description **:
2616+
2617+ The ``__builtin_expect_with_probability() `` builtin is typically used with
2618+ control flow conditions such as in ``if `` and ``switch `` statements to help
2619+ branch prediction. It means that its first argument ``expr `` is expected to take
2620+ the value of its second argument ``val `` with probability ``p ``. ``p `` must be
2621+ within ``[0.0 ; 1.0] `` bounds. This builtin always returns the value of ``expr ``.
2622+
2623+ Query for this feature with ``__has_builtin(__builtin_expect_with_probability) ``.
2624+
2625+ ``__builtin_prefetch ``
2626+ ----------------------
2627+
2628+ ``__builtin_prefetch `` is used to communicate with the cache handler to bring
2629+ data into the cache before it gets used.
2630+
2631+ **Syntax **:
2632+
2633+ .. code-block :: c++
2634+
2635+ void __builtin_prefetch (const void *addr, int rw=0, int locality=3)
2636+
2637+ **Example of use **:
2638+
2639+ .. code-block :: c++
2640+
2641+ __builtin_prefetch (a + i);
2642+
2643+ **Description **:
2644+
2645+ The ``__builtin_prefetch(addr, rw, locality) `` builtin is expected to be used to
2646+ avoid cache misses when the developper has a good understanding of which data
2647+ are going to be used next. ``addr `` is the address that needs to be brought into
2648+ the cache. ``rw `` indicates the expected access mode: ``0 `` for *read * and ``1 ``
2649+ for *write *. In case of *read write * access, ``1 `` is to be used. ``locality ``
2650+ indicates the expected persistance of data in cache, from ``0 `` which means that
2651+ data can be discarded from cache after its next use to ``3 `` which means that
2652+ data is going to be reused a lot once in cache. ``1 `` and ``2 `` provide
2653+ intermediate behavior between these two extremes.
2654+
2655+ Query for this feature with ``__has_builtin(__builtin_prefetch) ``.
2656+
25042657``__sync_swap ``
25052658---------------
25062659
0 commit comments