with Instrument;
use Instrument;

---------------------------
-- Reduce Invariant Expression
---------------------------
--
--  This test evaluates a code improvement potential similar to  that
--  resulting  from  the  Reduce Constant Expression benchmark except
--  that the operands of the multiplication operation  are  the  loop
--  parameter and a loop invariant.
--
--  The test procedures contain a redefinition of a  local  variable,
--  Local_1.   In  the  optimized  test  procedure  the  redefinition
--  evaluates an expression that multiplies the loop parameter  by  a
--  loop   invariant,  Local_2,  whereas  in  the  nonoptimized  test
--  procedure the  operands  are  the  loop  parameter  and  a  local
--  variable,  Local_3, that is iteratively redefined within the loop
--  to  void  the  optimization.   The  camouflaged  redefinition  of
--  Local_4  serves  to  reference  Local_1  and to provide a similar
--  morphology with the Reduce Constant Expression benchmark.
--
--  The camouflaged redefinition of Global_1  using  Local_4  ensures
--  that   Local_4   is   referenced   and   that   other  unexpected
--  optimizations do not invalidate the benchmark.

procedure Aoiea1 is

    package New_Procs is new Procs (Integer);
    use New_Procs;

    Local_1, Local_4 : T;
    Local_2, Local_3 : T := Ident (Init / Init);

begin

    Start ("AOIEA1", "Arith. Optimization, Invariant Elim. (control)");
    for I in 1 .. 1000 loop

        Let (Global, Ident (Init / Init));

        Let (Local_2, Ident (Init / Init));

        for J in Ident (T'Val (1)) .. Ident (T'Val (10)) loop

            Let (Local_3, Ident (Local_3));
            ---optimize---
            --     Local_1 := T(Local_2 * T(J));    -- included in test version
            Local_1 := T (Local_3 * T (J));    -- included in control version
            Let (Local_4, Ident (T (Local_1 / T (J))));

        end loop;

        Let (Global, Ident (Local_4));
        Let (Global, Ident (Init / Init));

    end loop;
    Stop;

end Aoiea1;