------- Comment #23 from baldrick at free dot fr 2007-02-23 15:40 ------- Subject: Re: VRP fails to eliminate range checks in Ada code
> if not To'Valid then I'm pretty sure that this is an example of PR26797: the front-end should generate 'Valid by doing a VIEW_CONVERT_EXPR of To to its base type, and then checking whether that is in the right range. But what it often does right now is simply check whether To is in TYPE_MIN_VALUE .. TYPE_MAX_VALUE using an "if" statement. Of course VRP eliminates that. This is exactly PR26797, and needs to be fixed in the Ada f-e. That said, maybe I should explain about 'Valid. How can a variable have a value that is not in range? One answer is: because it is uninitialized. Another way is provided by the language: there is a technique for assigning from one variable to another without a range check, called "unchecked conversion". If the result is out of range, then the behaviour you get if you use it is undefined. Finally, there are some other funky situations which can be thought of as equivalent to a use of unchecked conversion. Anyway, the language provides exactly one way to check if a variable is in range: using 'Valid. So the 'Valid check must stop the optimizers from optimizing away the check based on the "known" range. The technique that seems to have been agreed on is to use VIEW_CONVERT_EXPR, but unfortunately the front-end doesn't seem to always use that right now. So this is not a problem with your patch as such. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30911