with Instrument;
use Instrument;

-----------------------
-- Identity Simplification
-----------------------

--  This test evaluates the code improvement potential  that  results
--  from  the elimination of arithmetic operations in the computation
--  of an expression when the  opportunity  is  provided  to  perform
--  identity simplification.

--  The test procedures define  global  information.   A  conditional
--  statement encloses an optimizable and nonoptimizable redefinition
--  of Global_1 and Global_3 using an expression  that  evaluates  to
--  the  value  Init.   In the optimized test procedure the evaluated
--  expression contains a subexpression that can be simplified, while
--  in  the  nonoptimized  test  procedure  the  evaluated expression
--  cannot be simplified.  Consequently, no difference in the size of
--  the  procedures  is  expected  but the effect of the optimization
--  should be evident from the difference in execution  time  of  the
--  two  procedures  by  reducing the simplified subexpression to the
--  value 1 at compile-time.

--  The constant values used to implement the  test  are  defined  so
--  that  the  expressions  are  evaluated consistently for different
--  numeric types.  Camouflaging the conditional expression  prevents
--  other   unexpected   optimizations   that  would  invalidate  the
--  benchmark.

procedure Opisa1 is

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

    Zero : constant T := Init - Init;
    One : constant T := T (Init / Init);
    Two : constant T := One + One;
    Three : constant T := One + Two;
    Value : constant T := Init / Init;

    Global_1 : T;
    Global_2 : T;
    Global_3 : T;
    Global_4 : T;
    Global_5 : T;

begin

    Start ("OPISA1", "Optimization Perf., Identity Simp. (control)");
    for I in 1 .. 1000 loop

        Let (Global_1, Ident (Init / Init));
        Let (Global_2, Ident (Init / Init));
        Let (Global_3, Ident (Init / Init));
        Let (Global_4, Ident (Init / Init));
        Let (Global_5, Ident (Init / Init));

        if Ident (Init) = Init then

            --------------optimize--------------
            Global_1 :=
               Ident (Init / Init) +
--          One - T(T((Global_2 + Zero) * One) / T(Global_2 * One));
                  -- included in test version
                  Two -
                  T (T ((Global_4 + Value) * Three) / T (Global_5 * Three));
            -- included in control version
        else

            Global_3 :=
               Ident (Init / Init) +
--          Two - T(T((Global_4 + Value) * Three)/T(Global_5 * Three));
                  -- included in test version
                  One - T (T ((Global_2 + Zero) * One) / T (Global_2 * One));
            -- included in control version
        end if;

        Let (Global_1, Ident (Init / Init));
        Let (Global_2, Ident (Init / Init));
        Let (Global_3, Ident (Init / Init));
        Let (Global_4, Ident (Init / Init));
        Let (Global_5, Ident (Init / Init));

    end loop;
    Stop;

end Opisa1;