Commit 9ead6ee5 by Eric Botcazou Committed by Pierre-Marie de Rodat

[Ada] Improve support for tuning branch probability heuristics

This adds a new GNAT.Branch_Prediction package to make it possible to
tune the branch probability heuristics more finely.  This package
contains the equivalent of __builtin_expect in C/C++ plus a couple of
specializations.

The following program gives a summary of the usage:

package Q is

  I : Integer;
  pragma Volatile (I);

end Q;

with GNAT.Branch_Prediction; use GNAT.Branch_Prediction;
with Text_IO; use Text_IO;
with Q; use Q;

procedure P is
begin
  if Unlikely (I = 0) then
    Put_Line ("Zero was passed");
    return;
  end if;

  if Likely (I > 0) then
    Put_Line ("A positive number was passed");
  else
    Put_Line ("A negative number was passed");
  end if;

  if Expect ((I rem 2) = 0, False) then
    Put_Line ("An even number was passed");
  else
    Put_Line ("An odd number was passed");
  end if;
end;

2019-07-10  Eric Botcazou  <ebotcazou@adacore.com>

gcc/ada/

	* Makefile.rtl (GNATRTL_NONTASKING_OBJS): Add g-brapre.
	* libgnat/g-brapre.ads: New package specification.
	* doc/gnat_rm/the_gnat_library.rst: Document it.
	* gnat_rm.texi: Regenerate.

From-SVN: r273340
parent 27572ba3
2019-07-10 Eric Botcazou <ebotcazou@adacore.com>
* Makefile.rtl (GNATRTL_NONTASKING_OBJS): Add g-brapre.
* libgnat/g-brapre.ads: New package specification.
* doc/gnat_rm/the_gnat_library.rst: Document it.
* gnat_rm.texi: Regenerate.
2019-07-10 Yannick Moy <moy@adacore.com>
* osint-c.adb (Set_File_Name): Always add extension for multiple
......
......@@ -387,6 +387,7 @@ GNATRTL_NONTASKING_OBJS= \
g-arrspl$(objext) \
g-awk$(objext) \
g-binenv$(objext) \
g-brapre$(objext) \
g-bubsor$(objext) \
g-busora$(objext) \
g-busorg$(objext) \
......
......@@ -722,6 +722,17 @@ Provides access to key=value associations captured at bind time.
These associations can be specified using the :switch:`-V` binder command
line switch.
.. _`GNAT.Branch_Prediction_(g-brapre.ads)`:
``GNAT.Branch_Prediction`` (:file:`g-brapre.ads`)
=================================================
.. index:: GNAT.Branch_Prediction (g-brapre.ads)
.. index:: Branch Prediction
Provides routines giving hints to the branch predictor of the code generator.
.. _`GNAT.Bounded_Buffers_(g-boubuf.ads)`:
``GNAT.Bounded_Buffers`` (:file:`g-boubuf.ads`)
......
------------------------------------------------------------------------------
-- --
-- GNAT RUN-TIME COMPONENTS --
-- --
-- G N A T . B R A N C H _ P R E D I C T I O N --
-- --
-- S p e c --
-- --
-- Copyright (C) 2019, AdaCore --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
-- This package provides routines giving hints to the branch predictor of the
-- code generator. These hints are useful when optimization is enabled and the
-- branch probability heuristics are used (which is the default), but they are
-- overridden when profile feedback-directed optimization is used instead.
-- The canonical pattern is to use them as the condition of an If statement:
--
-- if Likely (X > 0) then
-- Do_Something;
-- end if;
--
-- when it is not obvious that one outcome of the condition is more likely
-- than the other, or else to reverse the prediction made by the heuristics
-- in very peculiar cases. In the other cases, it is better not to use them,
-- because predicting how programs actually perform is notoriously hard.
package GNAT.Branch_Prediction is
pragma Pure;
function Expect (Condition : Boolean; Outcome : Boolean) return Boolean;
pragma Import (Intrinsic, Expect, "__builtin_expect");
-- This function returns the value of its first parameter Condition and
-- tells the branch predictor that this value is expected to be Outcome.
function Likely (Condition : Boolean) return Boolean;
pragma Import (Intrinsic, Likely, "__builtin_likely");
-- This function returns the value of its parameter Condition and tells
-- the branch predictor that this value is expected to be True. Calling
-- it is strictly equivalent to calling Expect with Outcome set to True.
function Unlikely (Condition : Boolean) return Boolean;
pragma Import (Intrinsic, Unlikely, "__builtin_unlikely");
-- This function returns the value of its parameter Condition and tells
-- the branch predictor that this value is expected to be False. Calling
-- it is strictly equivalent to calling Expect with Outcome set to False.
end GNAT.Branch_Prediction;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment