with Instrument;
use Instrument;

--------------------------
-- Reduce Constant Expression
--------------------------

--  This test evaluates a code  improvement  potential  that  results
--  from  executing  a  less  expensive arithmetic operation when the
--  opportunity is offered to replace a multiplication, that  uses  a
--  loop parameter and a constant, with an addition.

--  The  test  procedures  contain  a  redefinition  of   two   local
--  variables, Local_1 and Local_2, that are enclosed by a loop.  The
--  loop  parameter  has  a  nonstatic  range  that   is   adequately
--  camouflaged  to prevent loop optimization.  In the optimized test
--  procedure the redefinition of  Local_1  evaluates  an  expression
--  that multiplies the loop parameter by a local constant whereas in
--  the   nonoptimized   test   procedure   the   operands   of   the
--  multiplication are the loop parameter and a local variable.  This
--  local variable is iteratively redefined within the loop  to  void
--  the  optimization.   A  redefinition that evaluates an expression
--  which uses the  division  operation  is  included  to  provide  a
--  comparable reference to this local variable in the optimized test
--  procedure.  The optimization is evidenced by a difference in  the
--  execution time of the two procedures.

--  The camouflaged redefinition of Global_1  using  Local_2  ensures
--  that   Local_2   is   referenced   and   that   other  unexpected
--  optimizations do not invalidate the benchmark.

procedure Aocea2 is

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

    Constant_Init : constant T := Init / Init;
    Local_Init : T;
    Local_1, Local_2 : T;

begin

    Start ("AOCEA2", "Arith. Optimization, Const. Elim. (test)");
    for I in 1 .. 1000 loop

        Let (Global, Ident (Init));

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

            Let (Local_Init, Ident (Init / Init));
            ------optimize------
            Local_1 := T (T (I) * Constant_Init);   -- included in test version
            --     Local_1 := T(T(I) * Local_Init);      -- included in control version

            Let (Local_2,
                 Ident (T (Local_1 / Local_Init)));    -- inc. in test ver.
            --     Let(Local_2, Ident(T(Local_1 / Constant_Init))); -- inc. in control ver.

        end loop;

        Let (Global, Ident (Init + Local_2 - T'Val (1)));
        Let (Global, Ident (Init));

    end loop;
    Stop;

end Aocea2;