From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id BD97F384F016 for ; Fri, 13 May 2022 07:55:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BD97F384F016 Received: by mail-wr1-x434.google.com with SMTP id f2so3280531wrc.0 for ; Fri, 13 May 2022 00:55:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=7UjnlI4wkUEqHVDkvaCnDQcBFBtSV4qZtU3OI01CjOE=; b=zPt7oSjU4xLqOmFXhX55kFhYnDgfigLyLOmUbxf9icfeLKHoqJxwk8jXN/rN/Di/f9 SLM0UciYbzOh7N/BPrt25i/a6/DSaUXQZ31OX8YNJZQqSZ0lF7zbeCxkFObT+QYu2lIs Cy4e20/xFefyUKkI5b+Px54DCv0rQLzIQo80nhvkbnWbEuENgpOnGlOjxrH2mEtds9pZ 1ut3A5Tk4wL9PgtHmIOaXhRqdKiukvUnG/lU8zHGhCjK0YcD8zov1vCtq6r2JFl4he7d q2YnORKZE5M7r8d6DqmH1PKtxXiDSSGvo+u1fp/PMfO0GaoNdlOFNDaAZT52BHaTHIHt sNww== X-Gm-Message-State: AOAM5333FoChvqdWxry19HHNhm8jVHGgHomxyuXYmeCe27DkSdmRSoX5 3j368pykbyJQ6qH2lBR2n0b9zTFaOoHnuXGW X-Google-Smtp-Source: ABdhPJwqbC/MHhRp2ZFcaQUMv09cb9cOntrxO1nmW3S5Cl3F0MppSpGvjZgghX6dXkiaHv7pXzbYsg== X-Received: by 2002:a5d:4148:0:b0:20a:d2de:d960 with SMTP id c8-20020a5d4148000000b0020ad2ded960mr2801369wrq.61.1652428551433; Fri, 13 May 2022 00:55:51 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:bf38:f508:6f40:de1d]) by smtp.gmail.com with ESMTPSA id t9-20020a1c7709000000b003942a244f4dsm4708799wmi.38.2022.05.13.00.55.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 00:55:50 -0700 (PDT) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix wrong SRA with VIEW_CONVERT_EXPR and reverse SSO Date: Fri, 13 May 2022 09:54:35 +0200 Message-ID: <2183209.iZASKD2KPV@fomalhaut> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart1893875.PYKUYFuaPT" Content-Transfer-Encoding: 7Bit X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 May 2022 07:55:56 -0000 This is a multi-part message in MIME format. --nextPart1893875.PYKUYFuaPT Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Hi, most cases of VIEW_CONVERT_EXPRs involving reverse scalar storage order are disqualified for SRA because they are storage_order_barrier_p, but you can still have a VIEW_CONVERT_EXPR to a regular composite type being applied to a component of a record type with reverse scalar storage order, although this is pretty rare even in Ada (the only idiomatic way I know of is to use 'Valid on a floating-point component). In this case, the bypass for !useless_type_conversion_p in sra_modify_assign, albeit already heavily guarded, triggers and may generate wrong code, e.g. on the attached testcase, so the patch makes sure that it does only when the SSO is the same on both side. Bootstrapped/regtested on x86-64/Linux, OK for the mainline? 2022-05-13 Eric Botcazou * tree-sra.c (sra_modify_assign): Check that the scalar storage order is the same on the LHS and RHS before rewriting one with the model of the other. 2022-05-13 Eric Botcazou * gnat.dg/sso17.adb: New test. -- Eric Botcazou --nextPart1893875.PYKUYFuaPT Content-Disposition: attachment; filename="p.diff" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="utf-8"; name="p.diff" diff --git a/gcc/tree-sra.cc b/gcc/tree-sra.cc index a86f8c01346..081c51b58a4 100644 --- a/gcc/tree-sra.cc +++ b/gcc/tree-sra.cc @@ -4270,32 +4270,31 @@ sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi) sra_stats.exprs++; } - if (modify_this_stmt) - { - if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) + if (modify_this_stmt + && !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) + { + /* If we can avoid creating a VIEW_CONVERT_EXPR, then do so. + ??? This should move to fold_stmt which we simply should + call after building a VIEW_CONVERT_EXPR here. */ + if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) + && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (lhs)) == racc->reverse + && !contains_bitfld_component_ref_p (lhs)) { - /* If we can avoid creating a VIEW_CONVERT_EXPR do so. - ??? This should move to fold_stmt which we simply should - call after building a VIEW_CONVERT_EXPR here. */ - if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) - && !contains_bitfld_component_ref_p (lhs)) - { - lhs = build_ref_for_model (loc, lhs, 0, racc, gsi, false); - gimple_assign_set_lhs (stmt, lhs); - } - else if (lacc - && AGGREGATE_TYPE_P (TREE_TYPE (rhs)) - && !contains_vce_or_bfcref_p (rhs)) - rhs = build_ref_for_model (loc, rhs, 0, lacc, gsi, false); + lhs = build_ref_for_model (loc, lhs, 0, racc, gsi, false); + gimple_assign_set_lhs (stmt, lhs); + } + else if (lacc + && AGGREGATE_TYPE_P (TREE_TYPE (rhs)) + && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (rhs)) == lacc->reverse + && !contains_vce_or_bfcref_p (rhs)) + rhs = build_ref_for_model (loc, rhs, 0, lacc, gsi, false); - if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) - { - rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), - rhs); - if (is_gimple_reg_type (TREE_TYPE (lhs)) - && TREE_CODE (lhs) != SSA_NAME) - force_gimple_rhs = true; - } + if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs))) + { + rhs = fold_build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (lhs), rhs); + if (is_gimple_reg_type (TREE_TYPE (lhs)) + && TREE_CODE (lhs) != SSA_NAME) + force_gimple_rhs = true; } } --nextPart1893875.PYKUYFuaPT Content-Disposition: attachment; filename="sso17.adb" Content-Transfer-Encoding: 7Bit Content-Type: text/x-adasrc; charset="UTF-8"; name="sso17.adb" -- { dg-do run } -- { dg-options "-gnatws -O" } with System; procedure SSO17 is type My_Float is new Float range 0.0 .. 359.99; type Rec is record Az : My_Float; El : My_Float; end record; for Rec'Bit_Order use System.High_Order_First; for Rec'Scalar_Storage_Order use System.High_Order_First; R : Rec; procedure Is_True (B : Boolean); pragma No_Inline (Is_True); procedure Is_True (B : Boolean) is begin if not B then raise Program_Error; end if; end; begin R := (Az => 1.1, El => 2.2); Is_True (R.Az'Valid); R := (Az => 3.3, El => 4.4); Is_True (R.Az'Valid); end; --nextPart1893875.PYKUYFuaPT--