This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Improve generation of checks


Tested on i686-linux, committed on HEAD

This patch deletes unnecessary divide-by-zero and access checks based
on analysis of short-circuited forms. For exanple, in the following
program:

with P; use P;
function k (P : IP; I : Integer) return Boolean is
begin
    if P = null or else P.all = 13 then
       return True;
    elsif P /= null and then P.all = 12 then
       return True;
    elsif I = 0 or else 14 / I = 2 then
       return True;
    elsif I /= 0 and then 254 / I = 7 then
       return True;
    else
       return False;
    end if;
end K;

all four run-time checks are now so suppressed, so the following
script generates no output:

gcc -c k.adb -gnatG >log
grep constraint_error log

In addition, the same circuit generates warnings for obvious cases of
failure to use short-circuited forms.

The test program:

with P; use P;
function kw (P : IP; I : Integer) return Boolean is
begin
    if P = null or P.all = 13 then
       return True;
    elsif P /= null and P.all = 12 then
       return True;
    elsif I = 0 or 14 / I = 2 then
       return True;
    elsif I /= 0 and 254 / I = 7 then
       return True;
    else
       return False;
    end if;
end kw;

generates the following compiled with -gnatl

     1. with P; use P;
     2. function kw (P : IP; I : Integer) return Boolean is
     3. begin
     4.     if P = null or P.all = 13 then
                        1    2
        >>> warning: use "or else" instead of "or"
        >>> warning: Constraint_Error may be raised (access check)

     5.        return True;
     6.     elsif P /= null and P.all = 12 then
                            1     2
        >>> warning: use "and then" instead of "and"
        >>> warning: Constraint_Error may be raised (access check)

     7.        return True;
     8.     elsif I = 0 or 14 / I = 2 then
                        1     2
        >>> warning: use "or else" instead of "or"
        >>> warning: Constraint_Error may be raised (zero divide)

     9.        return True;
    10.     elsif I /= 0 and 254 / I = 7 then
                         1       2
        >>> warning: use "and then" instead of "and"
        >>> warning: Constraint_Error may be raised (zero divide)

    11.        return True;
    12.     else
    13.        return False;
    14.     end if;
    15. end kw;

2005-09-01  Robert Dewar  <dewar@adacore.com>

	* checks.adb (Check_Needed): New procedure, deals with removing checks
	based on analysis of short-circuited forms. Also generates warnings for
	improper use of non-short-circuited forms.
	Code clean ups.

Attachment: difs.11
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]