From patchwork Fri Jun 30 09:28:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 114635 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10222442vqr; Fri, 30 Jun 2023 02:29:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlHS4efCKZHOx1pX1KuFmxI21pOL5Wtj1aVXrbUc9X4wPpfVj8LblPX+Q4oYpyEuVIcYYlRq X-Received: by 2002:a17:906:29ce:b0:992:76e:ccbf with SMTP id y14-20020a17090629ce00b00992076eccbfmr1481348eje.14.1688117354402; Fri, 30 Jun 2023 02:29:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688117354; cv=none; d=google.com; s=arc-20160816; b=dIC2W+QBawDupwaWfk5Pao/LCrg/BU2NEi2r5/QNgJYXG0xDA3mTZzg2DfE0l7A2EU BpNy0Z3p70c/ooL1mIpNNpfNR7aQ54RN53eZzjKhh/2plXqDIBZca1BPMHsy0f8lzHsC 4L4UNdQeIO4Sxw3ngYixJNs5PV1lQjhMEB25d7KB2iSev96/dVavTcgfHJn7uRdQEyze 6jIA2tj1r6Y7LYoHQj7kAKTeSDyKZYJ5MlRq3TZ9eFm1+71ngs8a45jwPMjVY7DhGDjU opB1/p/VEvskMvG0OSf9auLi+iRVv9OAKXPxdg1FTLaD8dHITH/6ZrdxmnxVFhivBz+X 7h/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=RGCTiRlrkD+IS3LdIEz92tBBgKffnS5wzY6qAScsORg=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=SsbkYK3HYWtyyHDiTypDR/fc3c39Nnj0pXa3P2YxEVgkiz10PDJ3M9cLGxjYLEkPx1 4xuwEHr4H02/4j0BnF2nznwOxqe/41WAC7IEL7fTpLbR1zwVPpxjJoGFfdGjiaUBLDGf XWslxh90bUh2Nii9D36drVMHAcMjCNTQKJCRIXBuaUfp0mva6NZNZ+mvJ6P3wGdmI/MX Oi2HqJ2jc8KdH5GgjRxwl6M1M5Hsif7nz9eANNYDwsMd1GTW/Ux+EN/1/iDxfT+FXgMa SkFarjGsLof4rggQkDmdA68ww1QYP51vd9mJtt27gZYLM3lYOIvG5PnAUYwZoHgs9ODJ 1+LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NnKQ08mU; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l26-20020a1709060e1a00b00988c051a9edsi37165eji.651.2023.06.30.02.29.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 02:29:14 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NnKQ08mU; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F3220385354A for ; Fri, 30 Jun 2023 09:29:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F3220385354A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688117353; bh=RGCTiRlrkD+IS3LdIEz92tBBgKffnS5wzY6qAScsORg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=NnKQ08mUxt1vq9+fBJZRd44dVV/8/hQ0zO0ATQMVms9jUH4lW3WZKQLDENxeLYZyT q01uNGA+R4fq/KM7oM/VPJcOdJSukS8vOvLCPOW4AX1QGxtmehEfaqLTfMHDKbPmz0 OEddmflx1ylQJja8GcKOpWwk6WsgeVk27eX2Mllw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 66CF7385DC05 for ; Fri, 30 Jun 2023 09:28:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66CF7385DC05 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-3141fa31c2bso904937f8f.2 for ; Fri, 30 Jun 2023 02:28:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688117307; x=1690709307; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RGCTiRlrkD+IS3LdIEz92tBBgKffnS5wzY6qAScsORg=; b=WgmsfTOKsog2g93on51l61p0/w3w6kDxEXzyZZ/XwAlvaUclSgR1zfVMLw/s77xkNl zc0DnY6xvPhajnPCO5zK7QTvE7rpuBO9LRT06ba5iJN1g0F0Uxmnzs5VzC/SprDMT3XD 0EhTTS3w+SkPm6FTZr4+xCeJsHlM00wNSeNxg9eOb3jQAebAQxpzb86JXr3O7/qzAuVq RFc2oNycblRox1pu4oHTyuoXULZG5tvYFZhrl9wCMOBT9bb07n8JSwlEp5CBoJwaxHzP PjThGElwkOsVFOVY9HMuyn6Gk9HEjPI+ILKcdIKGGaE+uRmwzOm6BHThco6K+XCWnhoN mvtA== X-Gm-Message-State: ABy/qLZycumT+WnRWH/+kmgvkgza6YLgbKtTww1SghhtvKEm+t2A7hOv umvY7PC2yi5JiVM47E45SsEi4iAiFrilm+YNDImYLg== X-Received: by 2002:adf:fe8f:0:b0:313:ee69:fb21 with SMTP id l15-20020adffe8f000000b00313ee69fb21mr1667814wrr.62.1688117306756; Fri, 30 Jun 2023 02:28:26 -0700 (PDT) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id e15-20020adfe7cf000000b0030fae360f14sm17873062wrn.68.2023.06.30.02.28.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 02:28:26 -0700 (PDT) X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix couple of endianness issues in fold_ctor_reference Date: Fri, 30 Jun 2023 11:28:25 +0200 Message-ID: <10296585.nUPlyArG6x@fomalhaut> MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 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-Patchwork-Original-From: Eric Botcazou via Gcc-patches From: Eric Botcazou Reply-To: Eric Botcazou Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770119342905350036?= X-GMAIL-MSGID: =?utf-8?q?1770119342905350036?= Hi, fold_ctor_reference attempts to use a recursive local processing in order to call native_encode_expr on the leaf nodes of the constructor, before falling back to calling native_encode_initializer if this fails. There are a couple of issues related to endianness present in it: 1) it does not specifically handle integral bit-fields; now these are left justified on big-endian platforms so cannot be treated like ordinary fields. 2) it does not check that the constructor uses the native storage order. Proposed fix attached, tested on x86-64/Linux and SPARC/Solaris, OK for the mainline and some branches? 2023-06-30 Eric Botcazou * gimple-fold.cc (fold_array_ctor_reference): Fix head comment. (fold_nonarray_ctor_reference): Likewise. Specifically deal with integral bit-fields. (fold_ctor_reference): Check that the constructor uses the native storage order. 2023-06-30 Eric Botcazou * gcc.c-torture/execute/20230630-1.c: New test. * gcc.c-torture/execute/20230630-2.c: Likewise. diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 3d46b76edeb..e80a72dfa22 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -7849,12 +7849,11 @@ get_base_constructor (tree base, poly_int64_pod *bit_offset, } } -/* CTOR is CONSTRUCTOR of an array type. Fold a reference of SIZE bits - to the memory at bit OFFSET. When non-null, TYPE is the expected - type of the reference; otherwise the type of the referenced element - is used instead. When SIZE is zero, attempt to fold a reference to - the entire element which OFFSET refers to. Increment *SUBOFF by - the bit offset of the accessed element. */ +/* CTOR is a CONSTRUCTOR of an array or vector type. Fold a reference of SIZE + bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of + the reference; otherwise the type of the referenced element is used instead. + When SIZE is zero, attempt to fold a reference to the entire element OFFSET + refers to. Increment *SUBOFF by the bit offset of the accessed element. */ static tree fold_array_ctor_reference (tree type, tree ctor, @@ -8019,13 +8018,11 @@ fold_array_ctor_reference (tree type, tree ctor, return type ? build_zero_cst (type) : NULL_TREE; } -/* CTOR is CONSTRUCTOR of an aggregate or vector. Fold a reference - of SIZE bits to the memory at bit OFFSET. When non-null, TYPE - is the expected type of the reference; otherwise the type of - the referenced member is used instead. When SIZE is zero, - attempt to fold a reference to the entire member which OFFSET - refers to; in this case. Increment *SUBOFF by the bit offset - of the accessed member. */ +/* CTOR is a CONSTRUCTOR of a record or union type. Fold a reference of SIZE + bits to the memory at bit OFFSET. If non-null, TYPE is the expected type of + the reference; otherwise the type of the referenced member is used instead. + When SIZE is zero, attempt to fold a reference to the entire member OFFSET + refers to. Increment *SUBOFF by the bit offset of the accessed member. */ static tree fold_nonarray_ctor_reference (tree type, tree ctor, @@ -8037,8 +8034,7 @@ fold_nonarray_ctor_reference (tree type, tree ctor, unsigned HOST_WIDE_INT cnt; tree cfield, cval; - FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, - cval) + FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) { tree byte_offset = DECL_FIELD_OFFSET (cfield); tree field_offset = DECL_FIELD_BIT_OFFSET (cfield); @@ -8110,6 +8106,19 @@ fold_nonarray_ctor_reference (tree type, tree ctor, return NULL_TREE; offset_int inner_offset = offset_int (offset) - bitoffset; + + /* Integral bit-fields are left-justified on big-endian targets, so + we must arrange for native_encode_int to look at the MSB. */ + if (DECL_BIT_FIELD (cfield) && INTEGRAL_TYPE_P (TREE_TYPE (cfield))) + { + if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) + return NULL_TREE; + const unsigned int encoding_size + = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (cfield))); + if (BYTES_BIG_ENDIAN) + inner_offset += encoding_size - wi::to_offset (field_size); + } + return fold_ctor_reference (type, cval, inner_offset.to_uhwi (), size, from_decl, suboff); @@ -8122,7 +8131,7 @@ fold_nonarray_ctor_reference (tree type, tree ctor, return build_zero_cst (type); } -/* CTOR is value initializing memory. Fold a reference of TYPE and +/* CTOR is a value initializing memory. Fold a reference of TYPE and bit size POLY_SIZE to the memory at bit POLY_OFFSET. When POLY_SIZE is zero, attempt to fold a reference to the entire subobject which OFFSET refers to. This is used when folding accesses to @@ -8163,7 +8172,8 @@ fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset, } return ret; } - /* For constants and byte-aligned/sized reads try to go through + + /* For constants and byte-aligned/sized reads, try to go through native_encode/interpret. */ if (CONSTANT_CLASS_P (ctor) && BITS_PER_UNIT == 8 @@ -8179,7 +8189,12 @@ fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset, if (len > 0) return native_interpret_expr (type, buf, len); } - if (TREE_CODE (ctor) == CONSTRUCTOR) + + /* For constructors, try first a recursive local processing, but in any case + this requires the native storage order. */ + if (TREE_CODE (ctor) == CONSTRUCTOR + && !(AGGREGATE_TYPE_P (TREE_TYPE (ctor)) + && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (ctor)))) { unsigned HOST_WIDE_INT dummy = 0; if (!suboff) @@ -8194,9 +8209,9 @@ fold_ctor_reference (tree type, tree ctor, const poly_uint64 &poly_offset, ret = fold_nonarray_ctor_reference (type, ctor, offset, size, from_decl, suboff); - /* Fall back to native_encode_initializer. Needs to be done - only in the outermost fold_ctor_reference call (because it itself - recurses into CONSTRUCTORs) and doesn't update suboff. */ + /* Otherwise fall back to native_encode_initializer. This may be done + only from the outermost fold_ctor_reference call (because it itself + recurses into CONSTRUCTORs and doesn't update suboff). */ if (ret == NULL_TREE && suboff == &dummy && BITS_PER_UNIT == 8