From patchwork Thu Sep 9 13:44:43 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 64298 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 9BFA0B70CB for ; Thu, 9 Sep 2010 23:44:59 +1000 (EST) Received: (qmail 8168 invoked by alias); 9 Sep 2010 13:44:57 -0000 Received: (qmail 8103 invoked by uid 22791); 9 Sep 2010 13:44:56 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mel.act-europe.fr (HELO mel.act-europe.fr) (212.99.106.210) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 09 Sep 2010 13:44:46 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 1DFC7CB025A; Thu, 9 Sep 2010 15:44:44 +0200 (CEST) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bVg1hHrLw6jn; Thu, 9 Sep 2010 15:44:44 +0200 (CEST) Received: from saumur.act-europe.fr (saumur.act-europe.fr [10.10.0.183]) by mel.act-europe.fr (Postfix) with ESMTP id 0BB0DCB01E7; Thu, 9 Sep 2010 15:44:44 +0200 (CEST) Received: by saumur.act-europe.fr (Postfix, from userid 525) id E3975D9BA8; Thu, 9 Sep 2010 15:44:43 +0200 (CEST) Date: Thu, 9 Sep 2010 15:44:43 +0200 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Constant-folding and discriminants Message-ID: <20100909134443.GA19720@adacore.com> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.9i X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The compiler tries to optimize comparisons involving expressions that are not compile-time known, but can be recognized as identical, for example references to the same variable. If the expressions are references to the same discriminant the expressions cannot be assumed to be equal, because they may denote the discriminant of two different instances of the same discriminated type, used for example as a component bound. The following must compile and execute quietly: procedure No_Fold is type Arr is array (integer range <>) of Integer; type Rec (Discr : Positive) is record Field : Arr (1 .. Discr); end record; R1 : Rec (3) := (3, (others => 0)); R2 : Rec (7) := (7, (others => 0)); procedure Compare (X, Y : Rec) is begin if X.Discr < Y.Discr and then not (X.Field'Last < Y.Field'Last) then raise Program_Error; end if; end Compare; begin Compare (R1, R2); end; Tested on x86_64-pc-linux-gnu, committed on trunk 2010-09-09 Ed Schonberg * sem_eval.adb (Is_Same_Value): Two occurrences of the same discriminant cannot be assumed to be the same value because they may refer to bounds of a component of two different instances of a discriminated type. Index: sem_eval.adb =================================================================== --- sem_eval.adb (revision 164000) +++ sem_eval.adb (working copy) @@ -642,9 +642,15 @@ package body Sem_Eval is -- types, since we may have two NaN values and they should never -- compare equal. + -- If the entity is a discriminant, the two expressions may be + -- bounds of components of objects of the same discriminated + -- type. The values of the discriminants are not static, and + -- therefore the result is unknown. + if Nkind_In (Lf, N_Identifier, N_Expanded_Name) and then Nkind_In (Rf, N_Identifier, N_Expanded_Name) and then Entity (Lf) = Entity (Rf) + and then Ekind (Entity (Lf)) /= E_Discriminant and then Present (Entity (Lf)) and then not Is_Floating_Point_Type (Etype (L)) and then not Is_Volatile_Reference (L)