From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by sourceware.org (Postfix) with ESMTPS id 58D303858D3C; Sat, 23 Dec 2023 08:59:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58D303858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 58D303858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703321957; cv=none; b=dCWrKcxDqgxFSRh6Tb1M1xGIA7AYynVAWqhUNa4jXynjH+lIhHpYI4B4B40KkJrE/1BsvPMcjmgGs6ZpxNNjgtbqhwLq1FCbwDu1SHBQJiv3P7IeGByYVN9EbdIDlf8gYHa/A7PY89yjsa/DYREQ0m+GuhArCmcy8HwgJhI3Ffo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703321957; c=relaxed/simple; bh=uOufF/xgP+wwXd+M1FWIYESVDX8BynIqZZrhV9xFQ94=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=Y1gppnPrHLdjVnLX7zhnpRar2OqcWg7FFG7uXYk73ZDPAh6tiiNBpLHifugBlHFsp5Zf+rF+6nSJTWse9EaHNRrMEc3zR8V+JTfQI0qF4OvmqtI0xDKf1k1Ufzvjz+CFr/wgS16liInjMJrp+sTHkRyI+9ETqtVBb6bTet/Lvdc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3bb802341eeso1723306b6e.3; Sat, 23 Dec 2023 00:59:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703321954; x=1703926754; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FUt9MOqvSMTClSXAEWn5zFWy0TxywEQasCBqlPDTqpY=; b=aYa+agIKcAqu+frqlehGF0oIBV/5gu3sebAHNyytOgAJXg/5/LW0qAhvOYFAVLmwnE Y5DLO8VojaSFUOQPt32L2LD1lP+O28vLnuxoapoDXvBZrGq6qc4V0qA7zgukWBZcFSu3 ofzXUNDxiW2h+mnW0+tApMt75LorD2wQx+wQ3CSMrWyQDxvwjvx3S4gHB/OwGjuMRO7O XuIc08UbB+11fNKkGD40IA7fdzK0Po5lFVGtyGiiOBqtxfDWyhmAcrjnJMzdsSn9SlyZ vK7sM34+TpAf7JfzX9I8h+bzUQlwOYWuM9tFjsSiytxF1HRiiOb+zOTbtKjNwrOwngAv psyw== X-Gm-Message-State: AOJu0YxxXiI2Khd4wtv+LYX4LJLI0PtXevBGdRgDIR89ibiwMA+VtLu5 3RD6Of1C2YzjmvY1m6LPZjzOMX1YujMjb4IR X-Google-Smtp-Source: AGHT+IF3QG0tn7pCvsWlw+AG4r8pDE9BsbcPp4n3LyJQxJFbXFQadu8hpIRV0Ft7tJ4ckQVLqZJw1Q== X-Received: by 2002:a05:6808:2388:b0:3bb:6d9b:852 with SMTP id bp8-20020a056808238800b003bb6d9b0852mr3258173oib.87.1703321954297; Sat, 23 Dec 2023 00:59:14 -0800 (PST) Received: from localhost.localdomain ([149.248.38.156]) by smtp.gmail.com with ESMTPSA id l18-20020a62be12000000b006ce95e37a40sm4592991pff.111.2023.12.23.00.59.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Dec 2023 00:59:13 -0800 (PST) From: YunQiang Su To: gcc-patches@gcc.gnu.org Cc: richard.sandiford@arm.com, pinskia@gmail.com, jeffreyalaw@gmail.com, rguenther@suse.de, YunQiang Su Subject: [PATCH v3] EXPR: Emit an truncate if 31+ bits polluted for SImode Date: Sat, 23 Dec 2023 16:58:58 +0800 Message-Id: <20231223085858.4136369-1-syq@gcc.gnu.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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 List-Id: On TRULY_NOOP_TRUNCATION_MODES_P (DImode, SImode)) == true platforms, if 31 or above bits is polluted by an bitops, we will need an truncate. Let's emit one, and mark let's use the same hardreg as in and out, the RTL may like: (insn 21 20 24 2 (set (subreg/s/u:SI (reg/v:DI 200 [ val ]) 0) (truncate:SI (reg/v:DI 200 [ val ]))) "../xx.c":7:29 -1 (nil)) We use /s/u flags to mark it as really needed, as in combine_simplify_rtx, this insn may be considered as truncated, so let's skip this combination. gcc/ChangeLog: PR: 104914. * combine.cc (try_combine): Skip combine with truncate if dest is subreg and has /u/s flags on platforms TRULY_NOOP_TRUNCATION_MODES_P (DImode, SImode)) == true. * expr.cc (expand_assignment): Emit a truncate insn, if 31+ bits is polluted for SImode. gcc/testsuite/ChangeLog: PR: 104914. * gcc.target/mips/pr104914.c: New testcase. --- gcc/combine.cc | 23 +++++++++++++++++++++- gcc/expr.cc | 17 ++++++++++++++++ gcc/testsuite/gcc.target/mips/pr104914.c | 25 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/mips/pr104914.c diff --git a/gcc/combine.cc b/gcc/combine.cc index 1cda4dd57f2..04b9c414053 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -3294,6 +3294,28 @@ try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0, n_occurrences = 0; /* `subst' counts here */ subst_low_luid = DF_INSN_LUID (i2); + /* Don't try to combine a TRUNCATE INSN, if it's DEST is SUBREG and has + FLAG /s/u. We use these 2 flags to mark this INSN as really needed: + normally, it means that the bits of 31+ of this variable is polluted + by a bitops. The reason of existing of case (subreg:SI (reg:DI)) is + that, the same hardreg may act as src and dest. */ + if (TRULY_NOOP_TRUNCATION_MODES_P (DImode, SImode) + && INSN_P (i2)) + { + rtx i2dest_o = SET_DEST (PATTERN (i2)); + rtx i2src_o = SET_SRC (PATTERN (i2)); + if (GET_CODE (i2dest_o) == SUBREG + && GET_MODE (i2dest_o) == SImode + && GET_MODE (SUBREG_REG (i2dest_o)) == DImode + && SUBREG_PROMOTED_VAR_P (i2dest_o) + && SUBREG_PROMOTED_GET (i2dest_o) == SRP_SIGNED + && GET_CODE (i2src_o) == TRUNCATE + && GET_MODE (i2src_o) == SImode + && rtx_equal_p (SUBREG_REG (i2dest_o), XEXP (i2src_o, 0)) + ) + return 0; + } + /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique copy of I2SRC each time we substitute it, in order to avoid creating self-referential RTL when we will be substituting I1SRC for I1DEST @@ -5326,7 +5348,6 @@ find_split_point (rtx *loc, rtx_insn *insn, bool set_src) UNIQUE_COPY is true if each substitution must be unique. We do this by copying if `n_occurrences' is nonzero. */ - static rtx subst (rtx x, rtx from, rtx to, bool in_dest, bool in_cond, bool unique_copy) { diff --git a/gcc/expr.cc b/gcc/expr.cc index 9fef2bf6585..f7236040a34 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -6284,6 +6284,23 @@ expand_assignment (tree to, tree from, bool nontemporal) nontemporal, reversep); convert_move (SUBREG_REG (to_rtx), to_rtx1, SUBREG_PROMOTED_SIGN (to_rtx)); + + rtx last = get_last_insn (); + if (TRULY_NOOP_TRUNCATION_MODES_P (DImode, SImode) + && known_ge (bitregion_end, 31) + && SUBREG_PROMOTED_VAR_P (to_rtx) + && SUBREG_PROMOTED_SIGN (to_rtx) == SRP_SIGNED + && GET_MODE (to_rtx) == SImode + && GET_MODE (SUBREG_REG (to_rtx)) == DImode + && GET_CODE (SET_SRC (PATTERN (last))) == SIGN_EXTEND + ) + { + insn_code icode = convert_optab_handler + (trunc_optab, SImode, DImode); + if (icode != CODE_FOR_nothing) + emit_unop_insn (icode, to_rtx, + SUBREG_REG (to_rtx), TRUNCATE); + } } } else diff --git a/gcc/testsuite/gcc.target/mips/pr104914.c b/gcc/testsuite/gcc.target/mips/pr104914.c new file mode 100644 index 00000000000..5dd10e84c17 --- /dev/null +++ b/gcc/testsuite/gcc.target/mips/pr104914.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-mabi=64" } */ + +extern void abort (void); +extern void exit (int); + +NOMIPS16 int test (const unsigned char *buf) +{ + int val; + ((unsigned char*)&val)[0] = *buf++; + ((unsigned char*)&val)[1] = *buf++; + ((unsigned char*)&val)[2] = *buf++; + ((unsigned char*)&val)[3] = *buf++; + if(val > 0) + return 1; + else + return 0; +} + +int main () +{ + if (test("\xff\xff\xff\xff") != 0) + abort(); + exit(0); +} -- 2.39.2