This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Fix double stack use from new concatenation circuit


Concatenation was building a temporary variable, and then copying it to
the object if the concatenation appeared in the initializing expression
of the declared object (common usage). In the old concatenation circuit
one copy was on the secondary stack, one on the primary stack. In the
new concatenation circuit we get two copies on the primary stack.

This patch avoids the duplicate copy by replacing the object declaration
with an object renaming in this situation. A new flag OK_To_Rename is
introduced to control this optimization, which right now is only used
for concatenation, but there may be other similar cases.

A test program is:

procedure ConcatOpt (x : string) is
   y : string := x & "ABC";
begin
   null;
end ConcatOpt;

Compiled with -gnatG, this used to give:

Source recreated from tree for Concatopt (body)

with system.system__concat_2;

procedure concatopt (x : string) is
   subtype concatopt__S1b is string (x'first(1) .. x'last(1));
   L3b : constant integer := x'length;
   L4b : constant integer := L3b + 3;
   L5b : constant integer := (if L3b /= 0 then x'first else 1);
   subtype concatopt__TS6bS is string (L5b .. L5b + (L4b - 1));
   S6b : string (L5b .. L5b + (L4b - 1));
   $system__concat_2__str_concat_2 (S6b, x, "ABC");
   [subtype concatopt__T7b is concatopt__TS6bS]
   subtype concatopt__T7b is concatopt__TS6bS;
   y : concatopt__T7b := S6b;
begin
   null;
   return;
end concatopt;

But with the patch, we get:

Source recreated from tree for Concatopt (body)

with system.system__concat_2;

procedure concatopt (x : string) is
   subtype concatopt__S1b is string (x'first(1) .. x'last(1));
   L3b : constant integer := x'length;
   L4b : constant integer := L3b + 3;
   L5b : constant integer := (if L3b /= 0 then x'first else 1);
   subtype concatopt__TS6bS is string (L5b .. L5b + (L4b - 1));
   S6b : string (L5b .. L5b + (L4b - 1));
   $system__concat_2__str_concat_2 (S6b, x, "ABC");
   [subtype concatopt__T7b is concatopt__TS6bS]
   subtype concatopt__T7b is concatopt__TS6bS;
   y : concatopt__T7b renames S6b;
begin
   null;
   return;
end concatopt;

Note that y has been changed into a renaming statement

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

2009-04-24  Robert Dewar  <dewar@adacore.com>

	* einfo.adb (OK_To_Rename): New flag

	* einfo.ads (OK_To_Rename): New flag

	* exp_ch3.adb (Expand_N_Object_Declaration): Rewrite as renames if
	OK_To_Rename set.

	* exp_ch4.adb (Expand_Concatenate): Mark temp variable OK_To_Rename

	* sem_ch7.adb (Uninstall_Declarations): Allow for renames from
	OK_To_Rename.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]