with Instrument;
use Instrument;

----------------
-- Constant Folding
----------------

--  This test evaluates a code  improvement  potential  that  results
--  from  the  elimination  of  operations  that  compute expressions
--  comprised of constant values.

--  The optimized test procedure redefines Global_1 and Global_2 with
--  constant   values,   Constant_1  and  Constant_2.   Global_1  and
--  Global_2 are immediately used as expression operands to  redefine
--  Global_3  and  Global_4,  thereby  allowing  their  values  to be
--  propagated  for  the  compile-time  evaluation  of  the  constant
--  expressions.

--  The nonoptimized test  procedure  inhibits  the  optimization  by
--  introducing  a  call  to  a  procedure  in  the  basic block that
--  necessitates the execution-time  evaluation  of  the  expressions
--  since the values of Global_1 and Global_2 can no longer be safely
--  propagated.

procedure Opcfa1 is

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

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

    Constant_1 : constant T := Init / Init;
    Constant_2 : constant T := Init / Init + Init / Init;

    procedure Break_Basic_Block is
    begin
        Let (Global_1, Ident (Init / Init));
        Let (Global_2, Ident (Init / Init));
        Let (Global_3, Ident (Init / Init));
        Let (Global_4, Ident (Init / Init));
    end Break_Basic_Block;

begin

    Start ("OPCFA1", "Optimization Perf., Constant Folding (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));

        Global_1 := Constant_1;
        Global_2 := Constant_2;

        Break_Basic_Block;        -- included in control version

        ------optimize-----
        Global_3 := Global_1 + Global_2;
        ------optimize-----
        Global_4 := Global_1 * Global_2;

        --  Break_Basic_Block;         -- included in test version
        Let (Global_1, Ident (Init / Init));
        Let (Global_2, Ident (Init / Init));
        Let (Global_3, Ident (Init / Init));
        Let (Global_4, Ident (Init / Init));

    end loop;
    Stop;

end Opcfa1;