public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/aoliva/heads/testme)] Optimize nonstandard boolean conversions
@ 2022-01-27  9:49 Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2022-01-27  9:49 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9ef0e43f0308c518fc4bab7bde9478715dae122e

commit 9ef0e43f0308c518fc4bab7bde9478715dae122e
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Wed Dec 29 08:44:56 2021 -0300

    Optimize nonstandard boolean conversions
    
    This patch improves the generated code for nonstandard boolean types.
    
    One of the improvements extends the code that avoids converting back
    to the nonstandard boolean type an expression computed as standard
    boolean, when it will be converted to a(nother) nonstandard boolean
    type.
    
    The other improvement involves using the literal representation
    constants in an If_Expression instead of dereferencing the T'Val array
    when converting to a (nonstandard) boolean type. Avoiding the array
    dereference enables the compiler middle-end to propagate the constants
    and perform optimizations based on them, to the point of obviating the
    improvement above.
    
    Unfortunately, the code generated with this alternate expansion tends
    to be slightly larger if it turns out to not enable any further
    optimization, though it's most certainly faster, especially on targets
    with conditional moves, more so if "store flag" is slow, as on x86.
    Still, the array dereference is more straightforward and shorter, so
    I've arranged for this alternate expansion to be used only when
    optimizing for speed.
    
    
    [changelog]
    * exp_util.adb (Adjust_Result_Type): Leave result in Standard.Boolean
    if it's going to be converted to another boolean type.
    * exp_ch4.adb (Expand_N_Type_Conversion): When optimizing, convert to
    nonstandard booleans with an if_expression with boolean literals.

Diff:
---
 gcc/ada/exp_ch4.adb  | 37 +++++++++++++++++++++++++++----------
 gcc/ada/exp_util.adb |  3 +++
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index 662f975291e..f9ea2540ed0 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -12760,18 +12760,35 @@ package body Exp_Ch4 is
          if not Has_Compatible_Representation (Target_Type, Operand_Type)
            and then not Conversion_OK (N)
          then
+            if Optimization_Level > 0
+              and then Is_Boolean_Type (Target_Type)
+            then
+               --  Convert x(y) to (if y then x'(True) else x'(False)).
+               --  Use literals, instead of indexing x'val, to enable
+               --  further optimizations in the middle-end.
 
-            --  Convert: x(y) to x'val (ytyp'pos (y))
+               Rewrite (N,
+                 Make_If_Expression (Loc,
+                   Expressions => New_List (
+                     Operand,
+                     Convert_To (Target_Type,
+                                 New_Occurrence_Of (Standard_True, Loc)),
+                     Convert_To (Target_Type,
+                                 New_Occurrence_Of (Standard_False, Loc)))));
 
-            Rewrite (N,
-              Make_Attribute_Reference (Loc,
-                Prefix         => New_Occurrence_Of (Target_Type, Loc),
-                Attribute_Name => Name_Val,
-                Expressions    => New_List (
-                  Make_Attribute_Reference (Loc,
-                    Prefix         => New_Occurrence_Of (Operand_Type, Loc),
-                    Attribute_Name => Name_Pos,
-                    Expressions    => New_List (Operand)))));
+            else
+               --  Convert: x(y) to x'val (ytyp'pos (y))
+
+               Rewrite (N,
+                 Make_Attribute_Reference (Loc,
+                   Prefix         => New_Occurrence_Of (Target_Type, Loc),
+                   Attribute_Name => Name_Val,
+                   Expressions    => New_List (
+                     Make_Attribute_Reference (Loc,
+                       Prefix         => New_Occurrence_Of (Operand_Type, Loc),
+                       Attribute_Name => Name_Pos,
+                       Expressions    => New_List (Operand)))));
+            end if;
 
             Analyze_And_Resolve (N, Target_Type);
          end if;
diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index 64324bfcb72..e64929fa4cc 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -424,6 +424,9 @@ package body Exp_Util is
             elsif     KP in N_Op_Boolean
               or else KP in N_Short_Circuit
               or else KP = N_Op_Not
+              or else (KP in N_Type_Conversion
+                           | N_Unchecked_Type_Conversion
+                        and then Is_Boolean_Type (Etype (Parent (N))))
             then
                return;


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gcc(refs/users/aoliva/heads/testme)] Optimize nonstandard boolean conversions
@ 2022-05-06  7:18 Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 2022-05-06  7:18 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2de5a9685f284dbc7a31a9f6a70a92e03795e205

commit 2de5a9685f284dbc7a31a9f6a70a92e03795e205
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Thu Feb 17 03:12:02 2022 -0300

    Optimize nonstandard boolean conversions
    
    This patch improves the generated code for nonstandard boolean types.
    
    One of the improvements extends the code that avoids converting back
    to the nonstandard boolean type an expression computed as standard
    boolean, when it will be converted to a(nother) nonstandard boolean
    type.
    
    The other improvement involves using the literal representation
    constants in an If_Expression instead of dereferencing the T'Val array
    when converting to a (nonstandard) boolean type. Avoiding the array
    dereference enables the compiler middle-end to propagate the constants
    and perform optimizations based on them, to the point of obviating the
    improvement above.
    
    Unfortunately, the code generated with this alternate expansion tends
    to be slightly larger if it turns out to not enable any further
    optimization, though it's most certainly faster, especially on targets
    with conditional moves, more so if "store flag" is slow, as on x86.
    Still, the array dereference is more straightforward and shorter, so
    I've arranged for this alternate expansion to be used only when
    optimizing for speed.
    
    
    [changelog]
    * exp_util.adb (Adjust_Result_Type): Leave result in Standard.Boolean
    if it's going to be converted to another boolean type.
    * exp_ch4.adb (Expand_N_Type_Conversion): When optimizing, convert to
    nonstandard booleans with an if_expression with boolean literals.

Diff:
---
 gcc/ada/exp_ch4.adb  | 37 +++++++++++++++++++++++++++----------
 gcc/ada/exp_util.adb |  3 +++
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index 08dbd9bd8bf..92ac4226d9c 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -12760,18 +12760,35 @@ package body Exp_Ch4 is
          if not Has_Compatible_Representation (Target_Type, Operand_Type)
            and then not Conversion_OK (N)
          then
+            if Optimization_Level > 0
+              and then Is_Boolean_Type (Target_Type)
+            then
+               --  Convert x(y) to (if y then x'(True) else x'(False)).
+               --  Use literals, instead of indexing x'val, to enable
+               --  further optimizations in the middle-end.
 
-            --  Convert: x(y) to x'val (ytyp'pos (y))
+               Rewrite (N,
+                 Make_If_Expression (Loc,
+                   Expressions => New_List (
+                     Operand,
+                     Convert_To (Target_Type,
+                                 New_Occurrence_Of (Standard_True, Loc)),
+                     Convert_To (Target_Type,
+                                 New_Occurrence_Of (Standard_False, Loc)))));
 
-            Rewrite (N,
-              Make_Attribute_Reference (Loc,
-                Prefix         => New_Occurrence_Of (Target_Type, Loc),
-                Attribute_Name => Name_Val,
-                Expressions    => New_List (
-                  Make_Attribute_Reference (Loc,
-                    Prefix         => New_Occurrence_Of (Operand_Type, Loc),
-                    Attribute_Name => Name_Pos,
-                    Expressions    => New_List (Operand)))));
+            else
+               --  Convert: x(y) to x'val (ytyp'pos (y))
+
+               Rewrite (N,
+                 Make_Attribute_Reference (Loc,
+                   Prefix         => New_Occurrence_Of (Target_Type, Loc),
+                   Attribute_Name => Name_Val,
+                   Expressions    => New_List (
+                     Make_Attribute_Reference (Loc,
+                       Prefix         => New_Occurrence_Of (Operand_Type, Loc),
+                       Attribute_Name => Name_Pos,
+                       Expressions    => New_List (Operand)))));
+            end if;
 
             Analyze_And_Resolve (N, Target_Type);
          end if;
diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb
index 64324bfcb72..e64929fa4cc 100644
--- a/gcc/ada/exp_util.adb
+++ b/gcc/ada/exp_util.adb
@@ -424,6 +424,9 @@ package body Exp_Util is
             elsif     KP in N_Op_Boolean
               or else KP in N_Short_Circuit
               or else KP = N_Op_Not
+              or else (KP in N_Type_Conversion
+                           | N_Unchecked_Type_Conversion
+                        and then Is_Boolean_Type (Etype (Parent (N))))
             then
                return;


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-06  7:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27  9:49 [gcc(refs/users/aoliva/heads/testme)] Optimize nonstandard boolean conversions Alexandre Oliva
2022-05-06  7:18 Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).