From patchwork Tue Nov 28 07:43:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 1869140 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=qXVL9d3D; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SfZFq42C8z1yRW for ; Tue, 28 Nov 2023 18:43:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D57203858039 for ; Tue, 28 Nov 2023 07:43:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id AB396385829F for ; Tue, 28 Nov 2023 07:43:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB396385829F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AB396385829F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701157409; cv=none; b=uqre5WpwgByL1TMO/gE8g9QEqmKDiaMyqVKzai4e6vA8vphJSBUDVCdi8bYAisRxLKqBsJaHojey2MSqe3W20iNUyWVm46vIdFCISFoU3FdM28EeUKOnuWBYamND7lphrlDVoUSAt4JGwLTgX/iKMF6MqDMsnNKelLglwbUQ8Io= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701157409; c=relaxed/simple; bh=I1iqgHHICAUrWDoTJLpKWaLpG6IY2+aE+ww2mi4nXWU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=XQIhP3aoLRFmgmmZKldu0wJILSIJ/u7klg/nedPknPihR9Hlgil6KXbO6WSVhWVrg83Qv+lmNNISIsE4pikQPhUcG68LTaw+dERBKfWx+j63bBujZcrzEiRiAMFxylXv/UyNfi6cp0ce14iMFUgq7SeM+RmsK4KdKLNX4tT2wwE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0353727.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3AS7d5Xm020444; Tue, 28 Nov 2023 07:43:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : to : cc : from : subject : content-type : content-transfer-encoding; s=pp1; bh=/0HP5mdk4l4YLiL678PFFdQGIzpNy9dlPeZ6KhAY/K0=; b=qXVL9d3Dyvd8N5wk/ys30uKMffvfPr9m4gCDykxy50Wlvq9SUhy1JHuCXOVe0a9sCQfA yglg8fzzP0SDx1UJER/R68CqjoxXlQqzPRpkQnsc7VH4/fqjoWVPOle7YQRXsEHQo6UN THBHqvAUdTbfLQ39ukG72tjHatWNkX6s4Qb4fafOBEgM5t0Anr1LPYAfeIGEkbbCzN4a bb3W88lmbLfqhx4uwYTQDCx+vOOlSP+vVSPyEbyTjipX4KmE+J6XBRxCFGUhr/HIOp4R +b0hq82Vx3m3xntXsiVkOFeAwOnM3rpG4DGZnzCdpHUH36OSocyqIcYyq3wYr5XivAgv 3Q== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3unb891gca-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 28 Nov 2023 07:43:26 +0000 Received: from m0353727.ppops.net (m0353727.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 3AS6nG98000831; Tue, 28 Nov 2023 07:43:25 GMT Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3unb891gby-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 28 Nov 2023 07:43:25 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3AS7QFea031012; Tue, 28 Nov 2023 07:43:24 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3uku8sxf3w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 28 Nov 2023 07:43:24 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 3AS7hLQm13304490 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Nov 2023 07:43:21 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D334520040; Tue, 28 Nov 2023 07:43:21 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 102EE20043; Tue, 28 Nov 2023 07:43:20 +0000 (GMT) Received: from [9.197.249.13] (unknown [9.197.249.13]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 28 Nov 2023 07:43:19 +0000 (GMT) Message-ID: Date: Tue, 28 Nov 2023 15:43:21 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches Cc: Segher Boessenkool , David , "Kewen.Lin" , Peter Bergner From: HAO CHEN GUI Subject: [PATCH] Expand: Pass down equality only flag to cmpmem expand X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: sEC1FX9i31E3JE4ywwF8L8TAyCaUbdX_ X-Proofpoint-GUID: M-fgH5uRfUOMiimMYSfLTYm4bFaXId9M X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-28_06,2023-11-27_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=999 spamscore=0 adultscore=0 suspectscore=0 mlxscore=0 phishscore=0 lowpriorityscore=0 impostorscore=0 malwarescore=0 bulkscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2311280059 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, This patch passes down the equality only flags from emit_block_cmp_hints to cmpmem optab so that the target specific expand can generate optimized insns for equality only compare. Targets (e.g. rs6000) can generate more efficient insn sequence if the block compare is equality only. Bootstrapped and tested on x86 and powerpc64-linux BE and LE with no regressions. Is this OK for trunk? Thanks Gui Haochen ChangeLog Expand: Pass down equality only flag to cmpmem expand Targets (e.g. rs6000) can generate more efficient insn sequence if the block compare is equality only. This patch passes down the equality only flags from emit_block_cmp_hints to cmpmem optab so that the target specific expand can generate optimized insns for equality only compare. gcc/ * expr.cc (expand_cmpstrn_or_cmpmem): Rename to... (expand_cmpstrn): ...this. (expand_cmpmem): New function. Pass down equality only flag to cmpmem expand. (emit_block_cmp_via_cmpmem): Add an argument for equality only flag and call expand_cmpmem instead of expand_cmpstrn_or_cmpmem. (emit_block_cmp_hints): Call emit_block_cmp_via_cmpmem with equality only flag. * expr.h (expand_cmpstrn, expand_cmpmem): Declare. * builtins.cc (expand_builtin_strcmp, expand_builtin_strncmp): Call expand_cmpstrn instead of expand_cmpstrn_or_cmpmem. * config/i386/i386.md (cmpmemsi): Add the sixth operand for equality only flag. * config/rs6000/rs6000.md (cmpmemsi): Likewise. * config/s390/s390.md (cmpmemsi): Likewise. * doc/md.texi (cmpmem): Modify the document and add an operand for equality only flag. patch.diff diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 5ece0d23eb9..c2dbc25433d 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -4819,7 +4819,7 @@ expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target) if (len && !TREE_SIDE_EFFECTS (len)) { arg3_rtx = expand_normal (len); - result = expand_cmpstrn_or_cmpmem + result = expand_cmpstrn (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len), arg3_rtx, MIN (arg1_align, arg2_align)); } @@ -4929,9 +4929,9 @@ expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target, rtx arg1_rtx = get_memory_rtx (arg1, len); rtx arg2_rtx = get_memory_rtx (arg2, len); rtx arg3_rtx = expand_normal (len); - result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx, - arg2_rtx, TREE_TYPE (len), arg3_rtx, - MIN (arg1_align, arg2_align)); + result = expand_cmpstrn (cmpstrn_icode, target, arg1_rtx, arg2_rtx, + TREE_TYPE (len), arg3_rtx, + MIN (arg1_align, arg2_align)); tree fndecl = get_callee_fndecl (exp); if (result) diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 1b5a794b9e5..775cba5d93d 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -23195,7 +23195,8 @@ (define_expand "cmpmemsi" (compare:SI (match_operand:BLK 1 "memory_operand" "") (match_operand:BLK 2 "memory_operand" "") ) ) (use (match_operand 3 "general_operand")) - (use (match_operand 4 "immediate_operand"))] + (use (match_operand 4 "immediate_operand")) + (use (match_operand 5 ""))] "" { if (ix86_expand_cmpstrn_or_cmpmem (operands[0], operands[1], diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md index 2a1b5ecfaee..e66330f876e 100644 --- a/gcc/config/rs6000/rs6000.md +++ b/gcc/config/rs6000/rs6000.md @@ -10097,7 +10097,8 @@ (define_expand "cmpmemsi" (compare:SI (match_operand:BLK 1) (match_operand:BLK 2))) (use (match_operand:SI 3)) - (use (match_operand:SI 4))])] + (use (match_operand:SI 4)) + (use (match_operand:SI 5))])] "TARGET_POPCNTD" { if (expand_block_compare (operands)) diff --git a/gcc/config/s390/s390.md b/gcc/config/s390/s390.md index 4bdb679daf2..506e79fb035 100644 --- a/gcc/config/s390/s390.md +++ b/gcc/config/s390/s390.md @@ -3790,7 +3790,8 @@ (define_expand "cmpmemsi" (compare:SI (match_operand:BLK 1 "memory_operand" "") (match_operand:BLK 2 "memory_operand" "") ) ) (use (match_operand:SI 3 "general_operand" "")) - (use (match_operand:SI 4 "" ""))] + (use (match_operand:SI 4 "" "")) + (use (match_operand:SI 5 "" ""))] "" { if (s390_expand_cmpmem (operands[0], operands[1], diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi index e01cdcbe22c..06955cd7e78 100644 --- a/gcc/doc/md.texi +++ b/gcc/doc/md.texi @@ -6992,14 +6992,19 @@ result of the comparison. @cindex @code{cmpmem@var{m}} instruction pattern @item @samp{cmpmem@var{m}} -Block compare instruction, with five operands like the operands -of @samp{cmpstr@var{m}}. The two memory blocks specified are compared -byte by byte in lexicographic order starting at the beginning of each -block. Unlike @samp{cmpstr@var{m}} the instruction can prefetch -any bytes in the two memory blocks. Also unlike @samp{cmpstr@var{m}} -the comparison will not stop if both bytes are zero. The effect of -the instruction is to store a value in operand 0 whose sign indicates -the result of the comparison. +Block compare instruction, with six operands. The first five operands are +like the operands of @samp{cmpstr@var{m}}. The last operand indicates +whether the comparison is equality or not. Value one means it's an +equality only compare and zero means it's a non-equality compare. + +The two memory blocks specified are compared byte by byte in lexicographic +order starting at the beginning of each block. Unlike @samp{cmpstr@var{m}} +the instruction can prefetch any bytes in the two memory blocks. Also +unlike @samp{cmpstr@var{m}} the comparison will not stop if both bytes are +zero. When last operand is zero, the effect of the instruction is to store +a value in operand 0 whose sign indicates the result of the comparison. +When last operand is one, zero in operand 0 indicates two blocks are equal. +All other values in operand 0 indicate two blocks are not equal. @cindex @code{strlen@var{m}} instruction pattern @item @samp{strlen@var{m}} diff --git a/gcc/expr.cc b/gcc/expr.cc index 6dd9b8f2ce6..3cdc5181bd3 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -2381,14 +2381,13 @@ emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src, return expand_call (call_expr, NULL_RTX, false); } -/* Try to expand cmpstrn or cmpmem operation ICODE with the given operands. +/* Try to expand cmpstrn operation ICODE with the given operands. ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success, otherwise return null. */ rtx -expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, - rtx arg2_rtx, tree arg3_type, rtx arg3_rtx, - HOST_WIDE_INT align) +expand_cmpstrn (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx, + tree arg3_type, rtx arg3_rtx, HOST_WIDE_INT align) { machine_mode insn_mode = insn_data[icode].operand[0].mode; @@ -2407,6 +2406,34 @@ expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, return NULL_RTX; } +/* Similar as expand_cmpstrn, the last operand indicates whether it is a + equality comparison or not. */ +rtx +expand_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx, + tree arg3_type, rtx arg3_rtx, HOST_WIDE_INT align, + bool equality_only) +{ + machine_mode insn_mode = insn_data[icode].operand[0].mode; + + if (target && (!REG_P (target) || HARD_REGISTER_P (target))) + target = NULL_RTX; + + class expand_operand ops[6]; + create_output_operand (&ops[0], target, insn_mode); + create_fixed_operand (&ops[1], arg1_rtx); + create_fixed_operand (&ops[2], arg2_rtx); + create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type), + TYPE_UNSIGNED (arg3_type)); + create_integer_operand (&ops[4], align); + if (equality_only) + create_integer_operand (&ops[5], 1); + else + create_integer_operand (&ops[5], 0); + if (maybe_expand_insn (icode, 6, ops)) + return ops[0].value; + return NULL_RTX; +} + /* Expand a block compare between X and Y with length LEN using the cmpmem optab, placing the result in TARGET. LEN_TYPE is the type of the expression that was used to calculate the length. ALIGN @@ -2414,7 +2441,7 @@ expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx, static rtx emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target, - unsigned align) + unsigned align, bool equality_only) { /* Note: The cmpstrnsi pattern, if it exists, is not suitable for implementing memcmp because it will stop if it encounters two @@ -2424,7 +2451,8 @@ emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target, if (icode == CODE_FOR_nothing) return NULL_RTX; - return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align); + return expand_cmpmem (icode, target, x, y, len_type, len, align, + equality_only); } /* Emit code to compare a block Y to a block X. This may be done with @@ -2469,7 +2497,8 @@ emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target, result = compare_by_pieces (x, y, INTVAL (len), target, align, y_cfn, y_cfndata); else - result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align); + result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align, + equality_only); return result; } diff --git a/gcc/expr.h b/gcc/expr.h index 2a172867fdb..64dbbcfcaad 100644 --- a/gcc/expr.h +++ b/gcc/expr.h @@ -199,8 +199,9 @@ extern void use_regs (rtx *, int, int); extern void use_group_regs (rtx *, rtx); #ifdef GCC_INSN_CODES_H -extern rtx expand_cmpstrn_or_cmpmem (insn_code, rtx, rtx, rtx, tree, rtx, - HOST_WIDE_INT); +extern rtx expand_cmpstrn (insn_code, rtx, rtx, rtx, tree, rtx, HOST_WIDE_INT); +extern rtx expand_cmpmem (insn_code, rtx, rtx, rtx, tree, rtx, HOST_WIDE_INT, + bool); #endif /* Write zeros through the storage of OBJECT.