with Instrument;
use Instrument;

----------------------
-- Expression Computation
----------------------

--  This test evaluates a code  improvement  potential  that  results
--  from  minimizing  the execution of an arithmetic computation when
--  the opportunity is offered to relocate  an  invariant  expression
--  outside the region of code for a loop body.

--  The test  procedures  contain  a  single  loop  that  encloses  a
--  camouflaged  redefinition  of  a  local  variable,  Local_1.  The
--  redefinition requires the evaluation of an  expression  which  in
--  the  optimized  test  procedure includes an invariant computation
--  using local variables.  In the nonoptimized test procedure one of
--  the  local  variables  in the invariant expression is replaced by
--  the  redefined  local  variable.   Consequently,  only   in   the
--  optimized  test  procedure is there an invariant computation that
--  can be relocated.  This relocation should  be  evident  from  the
--  difference  in  the  execution  time  of  the  two procedures and
--  possibly by a  difference  in  space  requirements  for  the  two
--  procedure bodies.

--  Local variables are used in the invariant expression to  increase
--  the  likelihood of detecting the optimization, however the values
--  of these local variables are  determined  at  execution  time  to
--  ensure   the  maximum  benefit  from  the  code  improvement.   A
--  camouflaged assignment of Local_1 to a  global  variable  ensures
--  that   its   redefinition   is   not   eliminated  as  a  further
--  optimization.

procedure Loeca2 is

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

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

begin

    Start ("LOECA2", "Loop Optimization, Expr. Calc. (test)");
    for I in 1 .. 10000 loop

        Let (Global, Ident (Init));

        for J in Ident (T'Val (1)) .. Ident (T'Val (10)) loop
            -----optimize----
            Let (Local_1, Ident (T (T (Local_2 * Local_3) / Local_1)));
            -- included in test version
            --     Let(Local_2, Ident(T(T(Local_2 * Local_3)/Local_1)));
            -- included in control version

        end loop;

        Let (Global, Ident (Local_1));
        -- included in test version
        --  Let(Global, Ident(Local_2));
        -- included in control version

        Let (Global, Ident (Init));

    end loop;
    Stop;

end Loeca2;