The main goal of this change is to improve the evaluation at compile
time of the ranges of values that an expression can have at run time
and to apply it in as many cases as possible during the compilation.

It turns out that the front-end contains two separate engines that
can evaluate value ranges at compile time: the historical one is in
Determine_Range and is used in most cases, and the more recent but
more specialized one is in Minimize_Eliminate_Overflows and is used
for the -gnato2 and -gnato3 compilation modes.

The latter is more complete and more precise for arithmetic expressions
so the main change is to make it independent and use it in lieu of the
former in Determine_Range for arithmetic expressions. Two additional
improvements are made to Determine_Range: the built-in cache is made
aware of expression rewriting and more attributes are handled.

As a prerequisite of the last change, the compile-time evaluation of
some attributes is moved from the expander to the semantic analyzer.
This in particular covers Position, First_Bit and Last_Bit, which have
nontrivial semantics in Ada 2005 when a component clause with reverse
bit order is specified.

The main visible effect of the change is to prevent the compiler from
generating arithmetic checks that can never fail at low optimization.

Tested on x86_64-pc-linux-gnu, committed on trunk

2020-06-18  Eric Botcazou  <ebotca...@adacore.com>

gcc/ada/

        * checks.adb (Compute_Range_For_Arithmetic_Op): New procedure to
        compute a range for an arithmetical operation extracted from...
        (Minimize_Eliminate_Overflows): ...here.  Call it.
        (Determine_Range_Cache_O): New cache for Original_Node nodes.
        (Determine_Range): Call Compute_Range_For_Arithmetic_Op for all
        arithmetic expressions. Use Attribute_Id in lieu of Attribute_Name
        for attributes. Add handling for Range_Length alongside Length.
        Add specific handling for Alignment, Bit, First_Bit, Last_Bit,
        Max_Size_In_Storage_Elements, Position, Bit_Position,
        Component_Size, Object_Size, Size, Value_Size, Descriptor_Size.
        (Enable_Overflow_Check): Omit the check for Abs and Minus if the
        operand cannot be the largest negative number.
        (Selected_Length_Checks): Use Pos for Number_Dimensions.
        * exp_attr.adb (Expand_N_Attribute_Reference): Move compile-time
        handling of Bit_Position, Descriptor_Size, First_Bit, Last_Bit
        and Position to...
        * sem_attr.adb (Eval_Attribute): ...here.  Move up Alignment for
        objects and use Compile_Time_Known_Attribute in this case too.

Attachment: patch.diff.gz
Description: application/gzip

Reply via email to