[COMMITTED,14/26] ada: Clean up handling of inlining of finalizer procedures

Message ID 20240802071210.413366-14-poulhies@adacore.com
State Committed
Commit 3e64ad035302af0cd8e329bb508aa9ef1eb27597
Headers
Series [COMMITTED,01/26] ada: Fix detection of suspicious loop patterns |

Commit Message

Marc Poulhiès Aug. 2, 2024, 7:11 a.m. UTC
  From: Richard Kenner <kenner@adacore.com>

Change Is_Finalizer from synthesized attribute into flag. Remove duplicate
Is_Finalizer_Proc. Add new Try_Inline_Always for backend usage.

gcc/ada/

	* einfo-utils.ads (Is_Finalizer): Delete.
	* einfo-utils.adb (Is_Finalizer): Delete.
	* einfo.ads: Adjust comment.
	* gen_il-fields.ads, gen_il-gen-gen_entities.adb: Add Is_Finalizer
	flag.
	* exp_ch3.adb (Build_Init_Procedure): Set it.
	* exp_ch7.adb (Create_Finalizer): Likewise.
	* exp_util.adb (Try_Inline_Always): New function.
	* exp_util.ads (Try_Inline_Always): New function.
	* sem_elab.adb (Is_Finalizer_Proc): Replace with Is_Finalizer.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/einfo-utils.adb             |  9 ---------
 gcc/ada/einfo-utils.ads             |  1 -
 gcc/ada/einfo.ads                   |  2 +-
 gcc/ada/exp_ch3.adb                 |  1 +
 gcc/ada/exp_ch7.adb                 |  1 +
 gcc/ada/exp_util.adb                | 10 ++++++++++
 gcc/ada/exp_util.ads                |  5 +++++
 gcc/ada/gen_il-fields.ads           |  1 +
 gcc/ada/gen_il-gen-gen_entities.adb |  1 +
 gcc/ada/sem_elab.adb                | 26 +++++---------------------
 10 files changed, 25 insertions(+), 32 deletions(-)
  

Patch

diff --git a/gcc/ada/einfo-utils.adb b/gcc/ada/einfo-utils.adb
index c0c79f92e13..3dc25b36ad4 100644
--- a/gcc/ada/einfo-utils.adb
+++ b/gcc/ada/einfo-utils.adb
@@ -1567,15 +1567,6 @@  package body Einfo.Utils is
                     Has_Option (Id, Name_Synchronous));
    end Is_External_State;
 
-   ------------------
-   -- Is_Finalizer --
-   ------------------
-
-   function Is_Finalizer (Id : E) return B is
-   begin
-      return Ekind (Id) = E_Procedure and then Chars (Id) = Name_uFinalizer;
-   end Is_Finalizer;
-
    ----------------------
    -- Is_Full_Access --
    ----------------------
diff --git a/gcc/ada/einfo-utils.ads b/gcc/ada/einfo-utils.ads
index 8207576fb89..c0480c71cb8 100644
--- a/gcc/ada/einfo-utils.ads
+++ b/gcc/ada/einfo-utils.ads
@@ -190,7 +190,6 @@  package Einfo.Utils is
    function Is_Dynamic_Scope (Id : E) return B;
    function Is_Elaboration_Target (Id : E) return B;
    function Is_External_State (Id : E) return B;
-   function Is_Finalizer (Id : E) return B with Inline;
    function Is_Full_Access (Id : E) return B with Inline;
    function Is_Null_State (Id : E) return B;
    function Is_Package_Or_Generic_Package (Id : E) return B with Inline;
diff --git a/gcc/ada/einfo.ads b/gcc/ada/einfo.ads
index 9d0f2ee3c02..4486ab3636f 100644
--- a/gcc/ada/einfo.ads
+++ b/gcc/ada/einfo.ads
@@ -2647,7 +2647,7 @@  package Einfo is
 --       the transient finalization mechanisms. The flag prevents the double
 --       finalization of the object.
 
---    Is_Finalizer (synthesized)
+--    Is_Finalizer
 --       Applies to all entities, true for procedures containing finalization
 --       code to process local or library level objects.
 
diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb
index 6fee2b41bac..bf04ea9d70a 100644
--- a/gcc/ada/exp_ch3.adb
+++ b/gcc/ada/exp_ch3.adb
@@ -3532,6 +3532,7 @@  package body Exp_Ch3 is
                DF_Id :=
                  Make_Defining_Identifier (Loc,
                    Chars => New_External_Name (Name_uFinalizer));
+               Set_Is_Finalizer (DF_Id);
 
                Append_To (Decls, Make_Local_Deep_Finalize (Rec_Type, DF_Id));
 
diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb
index b545a58448d..72f0b539c2e 100644
--- a/gcc/ada/exp_ch7.adb
+++ b/gcc/ada/exp_ch7.adb
@@ -1979,6 +1979,7 @@  package body Exp_Ch7 is
             Fin_Id :=
               Make_Defining_Identifier (Loc,
                 Chars => New_External_Name (Name_uFinalizer));
+            Set_Is_Finalizer (Fin_Id);
 
             --  The visibility semantics of At_End handlers force a strange
             --  separation of spec and body for stack-related finalizers:
diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index c5d3af7545e..bd8bbb39d9c 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -14525,6 +14525,16 @@  package body Exp_Util is
       return Target;
    end Thunk_Target;
 
+   -----------------------
+   -- Try_Inline_Always --
+   -----------------------
+
+   function Try_Inline_Always (Subp : Entity_Id) return Boolean is
+     ((Is_Expression_Function (Subp) or else Is_Finalizer (Subp))
+       and then not Debug_Flag_Dot_8);
+   --  We want to inline expression functions and finalizers as much as
+   --  practical unless -gnatd.8.
+
    -------------------
    -- Type_Map_Hash --
    -------------------
diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads
index 7fbbe5fc9fd..14d9e345b53 100644
--- a/gcc/ada/exp_util.ads
+++ b/gcc/ada/exp_util.ads
@@ -1260,6 +1260,11 @@  package Exp_Util is
 
    --  WARNING: There is a matching C declaration of this subprogram in fe.h
 
+   function Try_Inline_Always (Subp : Entity_Id) return Boolean;
+   --  Determines if the backend should try hard to inline Subp. This is
+   --  similar to Subp having a pragma Inline_Always, but doesn't cause an
+   --  error if Subp can't actually be inlined.
+
    function Type_May_Have_Bit_Aligned_Components
      (Typ : Entity_Id) return Boolean;
    --  Determines if Typ is a composite type that has within it (looking down
diff --git a/gcc/ada/gen_il-fields.ads b/gcc/ada/gen_il-fields.ads
index 9b85401eadc..22fd1e372f6 100644
--- a/gcc/ada/gen_il-fields.ads
+++ b/gcc/ada/gen_il-fields.ads
@@ -708,6 +708,7 @@  package Gen_IL.Fields is
       Is_Exception_Handler,
       Is_Exported,
       Is_Finalized_Transient,
+      Is_Finalizer,
       Is_First_Subtype,
       Is_Fixed_Lower_Bound_Array_Subtype,
       Is_Fixed_Lower_Bound_Index_Subtype,
diff --git a/gcc/ada/gen_il-gen-gen_entities.adb b/gcc/ada/gen_il-gen-gen_entities.adb
index 86701cf9e31..29b22c62587 100644
--- a/gcc/ada/gen_il-gen-gen_entities.adb
+++ b/gcc/ada/gen_il-gen-gen_entities.adb
@@ -146,6 +146,7 @@  begin -- Gen_IL.Gen.Gen_Entities
         Sm (Is_Entry_Formal, Flag),
         Sm (Is_Entry_Wrapper, Flag),
         Sm (Is_Exported, Flag),
+        Sm (Is_Finalizer, Flag),
         Sm (Is_First_Subtype, Flag),
         Sm (Is_Formal_Subprogram, Flag),
         Sm (Is_Frozen, Flag),
diff --git a/gcc/ada/sem_elab.adb b/gcc/ada/sem_elab.adb
index a030d6b06f1..0b5f87bd828 100644
--- a/gcc/ada/sem_elab.adb
+++ b/gcc/ada/sem_elab.adb
@@ -1778,11 +1778,6 @@  package body Sem_Elab is
       --  Determine whether arbitrary entity Id denotes internally generated
       --  routine Default_Initial_Condition.
 
-      function Is_Finalizer_Proc (Id : Entity_Id) return Boolean;
-      pragma Inline (Is_Finalizer_Proc);
-      --  Determine whether arbitrary entity Id denotes internally generated
-      --  routine _Finalizer.
-
       function Is_Initial_Condition_Proc (Id : Entity_Id) return Boolean;
       pragma Inline (Is_Initial_Condition_Proc);
       --  Determine whether arbitrary entity Id denotes internally generated
@@ -2438,7 +2433,7 @@  package body Sem_Elab is
          --  Calls to _Finalizer procedures must not appear in the output
          --  because this creates confusing noise.
 
-         elsif Is_Finalizer_Proc (Subp_Id) then
+         elsif Is_Finalizer (Subp_Id) then
             null;
 
          --  Initial_Condition
@@ -5338,7 +5333,7 @@  package body Sem_Elab is
 
             return
               (Is_Controlled_Procedure (Subp_Id, Name_Finalize)
-                 or else Is_Finalizer_Proc (Subp_Id)
+                 or else Is_Finalizer (Subp_Id)
                  or else Is_TSS (Subp_Id, TSS_Deep_Finalize))
                and then In_Initialization_Context (Call);
          end Is_Partial_Finalization_Proc;
@@ -6607,7 +6602,7 @@  package body Sem_Elab is
             --  Calls to _Finalizer procedures must not appear in the output
             --  because this creates confusing noise.
 
-            elsif Is_Finalizer_Proc (Subp_Id) then
+            elsif Is_Finalizer (Subp_Id) then
                null;
 
             --  Initial_Condition
@@ -13098,7 +13093,7 @@  package body Sem_Elab is
             --  Controlled finalization actions
 
             elsif Is_Controlled_Procedure (Targ_Id, Name_Finalize)
-              or else Is_Finalizer_Proc (Targ_Id)
+              or else Is_Finalizer (Targ_Id)
             then
                Extra := First_Formal_Type (Targ_Id);
                Kind  := Controlled_Finalization;
@@ -14467,7 +14462,7 @@  package body Sem_Elab is
       begin
          return
            Is_Accept_Alternative_Proc (Id)
-             or else Is_Finalizer_Proc (Id)
+             or else Is_Finalizer (Id)
              or else Is_Partial_Invariant_Proc (Id)
              or else Is_TSS (Id, TSS_Deep_Adjust)
              or else Is_TSS (Id, TSS_Deep_Finalize)
@@ -14488,17 +14483,6 @@  package body Sem_Elab is
          return Ekind (Id) = E_Procedure and then Is_DIC_Procedure (Id);
       end Is_Default_Initial_Condition_Proc;
 
-      -----------------------
-      -- Is_Finalizer_Proc --
-      -----------------------
-
-      function Is_Finalizer_Proc (Id : Entity_Id) return Boolean is
-      begin
-         --  To qualify, the entity must denote a _Finalizer procedure
-
-         return Ekind (Id) = E_Procedure and then Chars (Id) = Name_uFinalizer;
-      end Is_Finalizer_Proc;
-
       -------------------------------
       -- Is_Initial_Condition_Proc --
       -------------------------------