From patchwork Sat Jan 26 11:36:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 1031432 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-494771-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="YprQoqL9"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43mv4b3RcLz9sD9 for ; Sat, 26 Jan 2019 22:37:00 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=h+Wg8GjVINa7u1GI i/xX6hCq5VjfiA3WZ1nzIE3YzJMRsLBoJ9LcaL0p1Bu9JgCmhlKD88syQByEXYV9 Cy6dlvP+HuDVCfpdqSwRxkmrI4PfayUWfFI8Bd70yUDNa5SNu2osEdC87zyQZ0oe LImiCHGwYDRBuBmqWuV9RMgmU3s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=aCUSiGf9iRtmkol7AIKrWA zhwyg=; b=YprQoqL9zWOxYQZMDvnlKr9BcA3i4rE+5vTbCjo/O/KBqV6oGAg1Oa bT5Yevnd+d6r4OFKak+DNK+dYuivvKXOrWJE7Ut3m3WOUuOUR3FtBSu7C6dYXO4i DNqk9TmVCuqH+Yy7hHiHUSUGO1fwHJE7bUxH7Vv5vdzE4W/ZICH9A= Received: (qmail 101499 invoked by alias); 26 Jan 2019 11:36:51 -0000 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 Received: (qmail 101489 invoked by uid 89); 26 Jan 2019 11:36:50 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=rec, H*r:10024 X-HELO: smtp.eu.adacore.com Received: from mel.act-europe.fr (HELO smtp.eu.adacore.com) (194.98.77.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 26 Jan 2019 11:36:49 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id E7E5C81393 for ; Sat, 26 Jan 2019 12:36:46 +0100 (CET) Received: from smtp.eu.adacore.com ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XtRmSoDDbpht for ; Sat, 26 Jan 2019 12:36:46 +0100 (CET) Received: from polaris.localnet (bon31-6-88-161-99-133.fbx.proxad.net [88.161.99.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.eu.adacore.com (Postfix) with ESMTPSA id C413F81392 for ; Sat, 26 Jan 2019 12:36:46 +0100 (CET) From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix suboptimal -gnatR3 output for discriminated record type Date: Sat, 26 Jan 2019 12:36:44 +0100 Message-ID: <5913410.4aAUJiCpL2@polaris> MIME-Version: 1.0 This is a regression present on all active branches: the output of -gnatR3 is suboptimal in some cases, for example: package P is type Arr is array (Short_Integer range <>) of Integer; type Rec (D : Short_Integer) is record A : Arr (2 .. D); end record; end P; for Rec'Object_Size use 1048544; for Rec'Value_Size use (((#1 max 1) + 0) * 32); for Rec'Alignment use 4; for Rec use record D at 0 range 0 .. 15; A at 4 range 0 .. ??; end record; Tested on x86_64-suse-linux, applied on all active branches. 2019-01-26 Eric Botcazou * gcc-interface/decl.c (annotate_value) : Use test on the sign bit instead of on the sign of the value. : Turn addition of negative constant into subtraction. : Add test for degenerate case. : Simplify. Index: gcc-interface/decl.c =================================================================== --- gcc-interface/decl.c (revision 268182) +++ gcc-interface/decl.c (working copy) @@ -8250,8 +8250,9 @@ annotate_value (tree gnu_size) { case INTEGER_CST: /* For negative values, build NEGATE_EXPR of the opposite. Such values - can appear for discriminants in expressions for variants. */ - if (tree_int_cst_sgn (gnu_size) < 0) + can appear for discriminants in expressions for variants. Note that, + sizetype being unsigned, we don't directly use tree_int_cst_sgn. */ + if (tree_int_cst_sign_bit (gnu_size)) { tree t = wide_int_to_tree (sizetype, -wi::to_wide (gnu_size)); tcode = Negate_Expr; @@ -8323,8 +8324,21 @@ annotate_value (tree gnu_size) case EQ_EXPR: tcode = Eq_Expr; break; case NE_EXPR: tcode = Ne_Expr; break; - case MULT_EXPR: case PLUS_EXPR: + /* Turn addition of negative constant into subtraction. */ + if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST + && tree_int_cst_sign_bit (TREE_OPERAND (gnu_size, 1))) + { + tcode = Minus_Expr; + ops[0] = annotate_value (TREE_OPERAND (gnu_size, 0)); + wide_int op1 = -wi::to_wide (TREE_OPERAND (gnu_size, 1)); + ops[1] = annotate_value (wide_int_to_tree (sizetype, op1)); + break; + } + + /* ... fall through ... */ + + case MULT_EXPR: tcode = (TREE_CODE (gnu_size) == MULT_EXPR ? Mult_Expr : Plus_Expr); /* Fold conversions from bytes to bits into inner operations. */ if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST @@ -8334,6 +8348,7 @@ annotate_value (tree gnu_size) if (TREE_CODE (inner_op) == TREE_CODE (gnu_size) && TREE_CODE (TREE_OPERAND (inner_op, 1)) == INTEGER_CST) { + ops[0] = annotate_value (TREE_OPERAND (inner_op, 0)); tree inner_op_op1 = TREE_OPERAND (inner_op, 1); tree gnu_size_op1 = TREE_OPERAND (gnu_size, 1); widest_int op1; @@ -8341,10 +8356,13 @@ annotate_value (tree gnu_size) op1 = (wi::to_widest (inner_op_op1) * wi::to_widest (gnu_size_op1)); else - op1 = (wi::to_widest (inner_op_op1) - + wi::to_widest (gnu_size_op1)); - ops[1] = UI_From_gnu (wide_int_to_tree (sizetype, op1)); - ops[0] = annotate_value (TREE_OPERAND (inner_op, 0)); + { + op1 = (wi::to_widest (inner_op_op1) + + wi::to_widest (gnu_size_op1)); + if (wi::zext (op1, TYPE_PRECISION (sizetype)) == 0) + return ops[0]; + } + ops[1] = annotate_value (wide_int_to_tree (sizetype, op1)); } } break; @@ -8352,18 +8370,12 @@ annotate_value (tree gnu_size) case BIT_AND_EXPR: tcode = Bit_And_Expr; /* For negative values in sizetype, build NEGATE_EXPR of the opposite. - Such values appear in expressions with aligning patterns. Note that, - since sizetype is unsigned, we have to jump through some hoops. */ + Such values can appear in expressions with aligning patterns. */ if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST) { - tree op1 = TREE_OPERAND (gnu_size, 1); - wide_int signed_op1 = wi::sext (wi::to_wide (op1), - TYPE_PRECISION (sizetype)); - if (wi::neg_p (signed_op1)) - { - op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1)); - ops[1] = annotate_value (build1 (NEGATE_EXPR, sizetype, op1)); - } + wide_int op1 = wi::sext (wi::to_wide (TREE_OPERAND (gnu_size, 1)), + TYPE_PRECISION (sizetype)); + ops[1] = annotate_value (wide_int_to_tree (sizetype, op1)); } break;