diff mbox series

[Ada] Optimize away certain elaboration checks

Message ID 20210707162539.GA2543209@adacore.com
State New
Headers show
Series [Ada] Optimize away certain elaboration checks | expand

Commit Message

Pierre-Marie de Rodat July 7, 2021, 4:25 p.m. UTC
The body of every primitive subprogram contains an elaboration check, in
case a dispatching call is made. These checks happen even in the static
model. This patch removes the checks if pragma Pure or Preelaborate is
present, because they cannot fail in that case.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* checks.adb (Install_Primitive_Elaboration_Check): Do not
	generate elaboration checks for primitives if pragma Pure or
	Preelaborate is present.  Misc comment fixes, including
	referring to the correct kind of check (elaboration, not
	accessibility).
	* checks.ads, restrict.adb, sem_cat.ads, sinfo.ads: Minor
	reformatting and comment fixes.
diff mbox series

Patch

diff --git a/gcc/ada/checks.adb b/gcc/ada/checks.adb
--- a/gcc/ada/checks.adb
+++ b/gcc/ada/checks.adb
@@ -48,6 +48,7 @@  with Sem;            use Sem;
 with Sem_Aux;        use Sem_Aux;
 with Sem_Ch3;        use Sem_Ch3;
 with Sem_Ch8;        use Sem_Ch8;
+with Sem_Cat;        use Sem_Cat;
 with Sem_Disp;       use Sem_Disp;
 with Sem_Eval;       use Sem_Eval;
 with Sem_Mech;       use Sem_Mech;
@@ -84,7 +85,7 @@  package body Checks is
    --  such as Apply_Scalar_Range_Check that do not insert any code can be
    --  safely called even when the Expander is inactive (but Errors_Detected
    --  is 0). The benefit of executing this code when expansion is off, is
-   --  the ability to emit constraint error warning for static expressions
+   --  the ability to emit constraint error warnings for static expressions
    --  even when we are not generating code.
 
    --  The above is modified in gnatprove mode to ensure that proper check
@@ -8634,7 +8635,7 @@  package body Checks is
          return;
 
       --  Do not generate an elaboration check if the related subprogram is
-      --  not subjected to accessibility checks.
+      --  not subject to elaboration checks.
 
       elsif Elaboration_Checks_Suppressed (Subp_Id) then
          return;
@@ -8644,14 +8645,20 @@  package body Checks is
       elsif Restriction_Active (No_Elaboration_Code) then
          return;
 
+      --  If pragma Pure or Preelaborate applies, then these elaboration checks
+      --  cannot fail, so do not generate them.
+
+      elsif In_Preelaborated_Unit then
+         return;
+
       --  Do not generate an elaboration check if exceptions cannot be used,
       --  caught, or propagated.
 
       elsif not Exceptions_OK then
          return;
 
-      --  Do not consider subprograms which act as compilation units, because
-      --  they cannot be the target of a dispatching call.
+      --  Do not consider subprograms that are compilation units, because they
+      --  cannot be the target of a dispatching call.
 
       elsif Nkind (Context) = N_Compilation_Unit then
          return;
@@ -8681,10 +8688,10 @@  package body Checks is
       elsif Analyzed (Subp_Body) then
          return;
 
-      --  Do not consider primitives which occur within an instance that acts
-      --  as a compilation unit. Such an instance defines its spec and body out
-      --  of order (body is first) within the tree, which causes the reference
-      --  to the elaboration flag to appear as an undefined symbol.
+      --  Do not consider primitives that occur within an instance that is a
+      --  compilation unit. Such an instance defines its spec and body out of
+      --  order (body is first) within the tree, which causes the reference to
+      --  the elaboration flag to appear as an undefined symbol.
 
       elsif Within_Compilation_Unit_Instance (Subp_Id) then
          return;


diff --git a/gcc/ada/checks.ads b/gcc/ada/checks.ads
--- a/gcc/ada/checks.ads
+++ b/gcc/ada/checks.ads
@@ -357,7 +357,7 @@  package Checks is
    --  if so inserts the appropriate run-time check.
 
    procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id);
-   --  Insert a check which ensures that subprogram body Subp_Body has been
+   --  Insert a check to ensure that subprogram body Subp_Body has been
    --  properly elaborated. The check is installed only when Subp_Body is the
    --  body of a nonabstract library-level primitive of a tagged type. Further
    --  restrictions may apply, see the body for details.


diff --git a/gcc/ada/restrict.adb b/gcc/ada/restrict.adb
--- a/gcc/ada/restrict.adb
+++ b/gcc/ada/restrict.adb
@@ -396,10 +396,9 @@  package body Restrict is
       N : Node_Id;
       V : Uint := Uint_Minus_1)
    is
-      Msg_Issued : Boolean;
-      pragma Unreferenced (Msg_Issued);
+      Ignore_Msg_Issued : Boolean;
    begin
-      Check_Restriction (Msg_Issued, R, N, V);
+      Check_Restriction (Ignore_Msg_Issued, R, N, V);
    end Check_Restriction;
 
    procedure Check_Restriction


diff --git a/gcc/ada/sem_cat.ads b/gcc/ada/sem_cat.ads
--- a/gcc/ada/sem_cat.ads
+++ b/gcc/ada/sem_cat.ads
@@ -27,7 +27,7 @@ 
 --  the semantic restrictions required for the categorization pragmas:
 --
 --    Preelaborate
---    Pure,
+--    Pure
 --    Remote_Call_Interface
 --    Remote_Types
 --    Shared_Passive


diff --git a/gcc/ada/sinfo.ads b/gcc/ada/sinfo.ads
--- a/gcc/ada/sinfo.ads
+++ b/gcc/ada/sinfo.ads
@@ -2082,7 +2082,7 @@  package Sinfo is
    --    that no elaboration check is needed on the call, because it appears in
    --    the context of a local Suppress pragma. This is used on calls within
    --    task bodies, where the actual elaboration checks are applied after
-   --    analysis, when the local scope stack is not present
+   --    analysis, when the local scope stack is not present.
 
    --  No_Entities_Ref_In_Spec
    --    Present in N_With_Clause nodes. Set if the with clause is on the