From patchwork Tue Feb 20 19:46:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Faust X-Patchwork-Id: 1901667 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=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=Gb84zifM; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=v2okBzzM; 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 4TfVKm4KkHz1yP6 for ; Wed, 21 Feb 2024 06:47:08 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 43C0B3858C41 for ; Tue, 20 Feb 2024 19:47:06 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 4B0B53858C31 for ; Tue, 20 Feb 2024 19:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B0B53858C31 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4B0B53858C31 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708458405; cv=pass; b=BipdGs802S01JUZAFM/6oL1SLpLGo/Fbgxp1b8EVVaR4qo8ec66bVs44Dz6sQ2eM5eA+YByZAsdj2XhJ+s63jbxVqhdrX8mi8qncQw72TRJ++bZHwBmxWo9GgEk0R9m/NOLKgdSBGs8oDzjYasZzRoExgKMzPXCj/IpNG5itn/8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708458405; c=relaxed/simple; bh=lObnGrydyTZfFETFKgthhPW0EtSO5K5sPNwEzQbdFDQ=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=nn4X3c1A4+sNNT195uqNq/LaqqmanRAe4st/Mq8qHvTk1in8KRWBfe8ZXA1f3A+gHA7VW/0SNAf2b/3cpnPYeHFC9a3Zl+Qs0u7dWHCg5a66Kz4d2at6DhQlLX1yWr508xUZZtifqRA4f4VnCxOJ3nacEBtguR2+HBtveoXPFQk= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41KJk57Z001839 for ; Tue, 20 Feb 2024 19:46:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=OcIXuT3Nn4qdRQijW1qKUSnGteZ7xXycB063Ymcu600=; b=Gb84zifM0cOUowVULs7goflaLYunILA+CKXymRPL5QGTU+MrrxQfzYW+LnrcBjk1PVr0 1eB5OEHJUabh+AJfhx0w1HzXPCsXgJrqSjmB8IdgjMBkbxTzLwirB4Nvt3AlR6eyIg2x 2QsB05jD0iygOEzP3FblQRx3GsinqUTevLglhAHxEJcekOqIYW63UIa9iAiu1CuywHn9 ++g1P63yt+cHnE0IshxCea+YJFxMYN8o8glhh8aVRQ2mQWhpX7AMtrUrvePui0UCJFma yb/HdhBVBlzUnes3wnWVMhGQ4QJcO7fY+45dD1n0wuQvHIgf0kZEbk5FSe1zhNKdWfum 5w== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3wakk3yw6w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 20 Feb 2024 19:46:40 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 41KIatPJ037777 for ; Tue, 20 Feb 2024 19:46:39 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3wak87suyx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 20 Feb 2024 19:46:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IrFoTiELWQcjFt24PJPUfvNQs1CxTlBiRaUutpSk1RI4zIabuwzewRgj6IoIwThC6ALh77SQ9N4/Cj/HuFt27YQlJbyAgmmFYUtxLq6q6O21kTF22VyF1UAtDDRwl37Js3koficJYdwOlxMc+WzgyPZCQ9vWNjH4atUmztAc81vm8keK0NCuvEXdLIZYw+eZia8qUjhU/uyEcO6/QiLsIoP3k8cdOzqP4KCj1Xdltj5FhetkIrcUHjek+2VCBfl2kMV23JXI30C3EORyfUVzDoy3ldZvu557h5uPvPJXFdN1Es/bhTC7ai86s5xahkpr7pjVDe53w93F8R4+9Qhxdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OcIXuT3Nn4qdRQijW1qKUSnGteZ7xXycB063Ymcu600=; b=Ev8QLUyT1zSHywav47nx2JkFifrrVsbCf4RmNPdH0YlNJYwV04usn1IV8+BFlK+gDAMNsF77rBQ4j+HKLpvghlA3/FtrEc54TD0ZTvVukrtTmCAdeXkoO5Qr3sOoBnvLBKwIjWEoCsnDlVy2FgjsWpJmE87JkDW3epR1G116HJyzTUMJE86EVSlX53uzw6Y1eJfZtTnTAy9VuzCBHVspsm0R5X42x8AhS/LaiykSQrZor3EIZlzIs54oqqM+2q9H+n2wA3kkoCbXPVs4H5F34fR2ucZQiW5k8yf4LlWIbZuMCdvYzS7jg3bNZObDLQ0blH8akVBhQ/df7o+daxmcnw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OcIXuT3Nn4qdRQijW1qKUSnGteZ7xXycB063Ymcu600=; b=v2okBzzMbi+fdTZk8czleSLhPr6Qhmsc8Fd1FIxJXcueTxaZnYHPfq/IRo814DF79h6dHhNcEn9fskcsVEQo9RMPQ4onYyh1ilRNhuDKw/tE/mkUm8sJ4+j534CPK0/YLJKmH6R7EDd7qKwWaKfZqcudJC88Am5GGzGajjw19Hc= Received: from DS0PR10MB7953.namprd10.prod.outlook.com (2603:10b6:8:1a1::22) by LV3PR10MB7819.namprd10.prod.outlook.com (2603:10b6:408:1b0::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.38; Tue, 20 Feb 2024 19:46:37 +0000 Received: from DS0PR10MB7953.namprd10.prod.outlook.com ([fe80::9722:fdc5:e685:633]) by DS0PR10MB7953.namprd10.prod.outlook.com ([fe80::9722:fdc5:e685:633%7]) with mapi id 15.20.7292.036; Tue, 20 Feb 2024 19:46:37 +0000 From: David Faust To: gcc-patches@gcc.gnu.org Cc: jose.marchesi@oracle.com Subject: [PATCH v2] bpf: add inline memmove and memcpy expansion Date: Tue, 20 Feb 2024 11:46:31 -0800 Message-ID: <20240220194631.20557-1-david.faust@oracle.com> X-Mailer: git-send-email 2.41.0 X-ClientProxiedBy: MN2PR16CA0053.namprd16.prod.outlook.com (2603:10b6:208:234::22) To DS0PR10MB7953.namprd10.prod.outlook.com (2603:10b6:8:1a1::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR10MB7953:EE_|LV3PR10MB7819:EE_ X-MS-Office365-Filtering-Correlation-Id: e4a37f3d-6baa-4ace-ba92-08dc324ca66d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2xbGHPSgZbtzeIcyknuAC6ZwnRL/tnYMWhIU0g9WEfB7G9JDzB/wIo1PbVw3nVhtiLB5eg2j/aM/d0A3YXqe/Fy40dYOCFuWNvvtq09fniGUN/a4bgoIpU3ZS6sbrRKWADZaVucAQMf14DMR0D/jA3Ye4dF6tdMAseCS29hZpmFmF1YlPpVub0wWQo51vtQTwzl+2hCoyQvaZuGf9Vi6E5f0SaZBw4cPNf6AT8QT3/GEAWwcBWc5HHp8SJdRcgFE56zc/JyuDK03dVhQpuVng/kMF69hQRtwl5zJjv9pto1whew5gwWHvViWJ73U5R+xf/2Th5l20RRUGEUxQHs5+5FdPSfvW0c755AdpNHEMs9mIIaZh8NbFjgJARcovOJVOiULSv6JAZYethyzdHbImj2ngaitAja6G/nUKYQLeQfM5RjyAunZBfH5aPP7na5+bN8VxeM5jEfhelwMoQpitT8Dhqnf09W5j1bDf7nHAhIDFIl7+MhXI0s6w2ELML52rOtauUluxaGamoPaULCsMrG9r8cqY0U5VtD24y2Kysi35uhwShhwKbgpYkvzfu3n X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DS0PR10MB7953.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(230273577357003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PnfxtaOF17wvC3MSzcUNFKugqjXlKk4VbbQfzmSUHrSx62yXjCo3fRqEspz7S3QhGSxO+SzVCMhzdB5hpxtUXxw4v2FRcYU/EBsLP33obbQscBKesTjtdG+b2Izu3RJZMiRgh0/HuHyK38xzHmw4o3HW9Jt3nzdb3Dr0pZ+iOFJ5ZnPQDXKxq3imGJ4c0Ir5TXyPfG8CTUrE6C++RmifreAiTH13Tjcgy4zv/pif6axmPvWIKMZSaLO2X29H0+hvT6lNB9lDW/f4Pa1u0JSSV405Hkirba68b80bvN5Q74KzZ0mow1i+bMWGlAAO56QW4G4RtilmJbQkHknn7pzDNZjQQKIz7zuhUiK2qM+JAzmDqvIOfYtQYZLVfFeabCuML3CFytSHs2/2Drts8E3XIm8gePocPr5o8OGVWrAeYXfbaKVFd8+FR71bZM1yPYrL4ndTK9nVSMrOFkdOOaJi+hp70J0tPo1IVKjMnX5vL0IiTjb2mL5KMzXe6mBdRuCRyc9Wz03DzhrrBUh3T52oQFdXD76C/EqrfO0XBamhqNS9X8+rec4/LTVFA49V4aV45+XUc6gT82kyusDvTBUReCfkVKLbe3MBeI7RWcfWMbRnZFcRntowQPIe+tOvoRG83ay0IykY4oYCPapl4ECvIqbP6meP948Q7y8L/dy1BJkl77IuCjjamoIHt5GdllZZL6PflCQPTllP2sfPkbksVIC26Xm2wP2lxCZoP7sWS3IXnWhkEYjDjsUw1+qzKNYr+yplxfCL/Tgpeuelc4P0gUbzEx5T7wXXmjSIqbYKqcH4E6ucNKZPK6oX9ocYlco9zc5gvGPKunw6JCsBKxDDpaHcRzp5Ju9K4Z9nr4zNy/Im2+TLOR/s5xkqckKbawk7117MOL8AyzOCHxWeaFwbKsRk3Ohng271eLhoGfm8u/HtdlautnXMurduY/9wRqgpoyTqKSiJDky2w1wwHJQr2OA2chVmJXaQc9QKFR8RMPK8vFTY0e6jT7+BoVHwZfFGWBmFH4dJtyatVzak65aMguBbsjDEsLi1D3dS9wRJXlJ5DA8WtfZ0eJexhLJhOb8L8RAxOWYwGhiwQr5cTtayONbHsec8mGq9GNxtfTEOhR4j+3ISaD7tr7Q8N6jPfFh7o0LSODbNbxzoHK9fJtYHN6bSA2y2HFFTEAxnrOEFyIZkTSb9igwMN7GgSyJvyRE5UzBUHnHfgxIbw7bIbHhX1EQpxJ+P0REvov29IMc1Ming3Q/LhDsZo75phvMekE+RMTqO6VZZ9pFC6HXcsUnT/suk2KJlyY275xGp3gpmkEhrTHu8eu/aoFa1MZGzq6lagxlXUmavnI1x5+1Qdm6Kz+xhuevzsUe6p8K5HEa3+n0lLJYKXIXW2hAaLDS0/mF+BjyJ+uTQsOXDr8FP/Kdf3emvhHiUUzEfG9UnnioWt2As0X2lZaHf3jU2rrF6ZGZxKqOPYlAtxgsPLFfTlND4/drKcclWiUOIUtdU13clRMih9ibK7ifXBHpZMJDkKvhwkpta8EHXMKwiR+FF6PMf7eN16cw8Lh2kr3WWzO4woWS54hRv7zDkXyHM8duZfqDA X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: rwtHemr6AKUbl1E6fPSvHW+xyRbq+3x8RHICibFNcfraZ/B5zzUBaD81fxDpuQHOtfryD8hXEBWwjPlPcgYN0Jv40BoVDxkaljWoeA7dXxDSFjZ5VDLa84D/RRxRSq0ChSX9OekNWhPg7C+ie5cN+cipGOuTABjvVbmivYqeu4FKLIJRT/hV9boX/P/jUuRCQJiV7g+5JUAQsddiWe7uJjwVsTo9QL4Ng9jis6QgDtnesxX9tLL7K/Z2UCRF4nV/HTwwx0bWnAOqE4GV/TQ/yocarZur5O6R8+1/RBsiO9zqbogwEDcbYSQ+STQJ8meovZeVM/gOxwV4ipbosq7sdllwSKRADmoJcNzRg4BsEIh/qskKXZmF82oANz36eB9HFfiTbMHekVPZ92MSRHJKte6jJBfCvZivnuFuAbA3G1++sFfCYBUjn+oE1yfwTbYdOSe75isz8liuUnYAxUI9Vb9IvE0JcNS6AIt5KqYd+UJXpAdLdZ1n0a5UJSodVGZZMEeH4EOuJuNNlI2Pt2/9lX/xo6mJj5e1QHtJhpuSuuOBO/AOUyqOrrdlxcaxScy2P9UZ9J53h65sLOR2YQv9tgXJBHw3YQOvcWDn897lgvk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e4a37f3d-6baa-4ace-ba92-08dc324ca66d X-MS-Exchange-CrossTenant-AuthSource: DS0PR10MB7953.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2024 19:46:37.2365 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: S6dIt3HLXN+JM+GGTsLEYue+n0xMYDAVeHny2L4o8dOA2TyMBxCCwX2DeM0CmfE9LeOXowPyQcAhZtlLlDqy0w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR10MB7819 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-20_06,2024-02-20_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402200141 X-Proofpoint-ORIG-GUID: i8StFW2cwsYVuxj7XZSJaFsjnDwjme2j X-Proofpoint-GUID: i8StFW2cwsYVuxj7XZSJaFsjnDwjme2j X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 [Changes from v1: Jose's review comments, all of which I agree with. - Fix 'implments' typo in commit message. - Change check that alignment is CONST_INT to gcc_assert (). - Change default case in alignment switch to gcc_unreachable (). - Reword error message for non-constant size memmove/memcpy, and update test for the error accordingly. - Delete CPYMEM_EXPAND_ERR macro, since it was now only used in one place. ] BPF programs are not typically linked, which means we cannot fall back on library calls to implement __builtin_{memmove,memcpy} and should always expand them inline if possible. GCC already successfully expands these builtins inline in many cases, but failed to do so for a few for simple cases involving overlapping memmove in the kernel BPF selftests and was instead emitting a libcall. This patch implements a simple inline expansion of memcpy and memmove in the BPF backend in a verifier-friendly way, with the caveat that the size must be an integer constant, which is also required by clang. Tested for bpf-unknown-none on x86_64-linux-gnu host. Also tested against the BPF verifier by compiling and loading a test program with overlapping memmove (essentially the memmove-1.c test) which failed before due to a libcall, and now successfully loads and passes the verifier. gcc/ * config/bpf/bpf-protos.h (bpf_expand_cpymem): New. * config/bpf/bpf.cc: (emit_move_loop, bpf_expand_cpymem): New. * config/bpf/bpf.md: (cpymemdi, movmemdi): New define_expands. gcc/testsuite/ * gcc.target/bpf/memcpy-1.c: New test. * gcc.target/bpf/memmove-1.c: New test. * gcc.target/bpf/memmove-2.c: New test. --- gcc/config/bpf/bpf-protos.h | 2 + gcc/config/bpf/bpf.cc | 116 +++++++++++++++++++++++ gcc/config/bpf/bpf.md | 36 +++++++ gcc/testsuite/gcc.target/bpf/memcpy-1.c | 26 +++++ gcc/testsuite/gcc.target/bpf/memmove-1.c | 46 +++++++++ gcc/testsuite/gcc.target/bpf/memmove-2.c | 23 +++++ 6 files changed, 249 insertions(+) create mode 100644 gcc/testsuite/gcc.target/bpf/memcpy-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/memmove-1.c create mode 100644 gcc/testsuite/gcc.target/bpf/memmove-2.c diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h index 46d950bd990..366acb87ae4 100644 --- a/gcc/config/bpf/bpf-protos.h +++ b/gcc/config/bpf/bpf-protos.h @@ -35,4 +35,6 @@ const char *bpf_add_core_reloc (rtx *operands, const char *templ); class gimple_opt_pass; gimple_opt_pass *make_pass_lower_bpf_core (gcc::context *ctxt); +bool bpf_expand_cpymem (rtx *, bool); + #endif /* ! GCC_BPF_PROTOS_H */ diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index d6ca47eeecb..5db031a4551 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -1184,6 +1184,122 @@ bpf_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size, #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \ bpf_use_by_pieces_infrastructure_p +/* Helper for bpf_expand_cpymem. Emit an unrolled loop moving the bytes + from SRC to DST. */ + +static void +emit_move_loop (rtx src, rtx dst, machine_mode mode, int offset, int inc, + unsigned iters, unsigned remainder) +{ + rtx reg = gen_reg_rtx (mode); + + /* First copy in chunks as large as alignment permits. */ + for (unsigned int i = 0; i < iters; i++) + { + emit_move_insn (reg, adjust_address (src, mode, offset)); + emit_move_insn (adjust_address (dst, mode, offset), reg); + offset += inc; + } + + /* Handle remaining bytes which might be smaller than the chunks + used above. */ + if (remainder & 4) + { + emit_move_insn (reg, adjust_address (src, SImode, offset)); + emit_move_insn (adjust_address (dst, SImode, offset), reg); + offset += (inc < 0 ? -4 : 4); + remainder -= 4; + } + if (remainder & 2) + { + emit_move_insn (reg, adjust_address (src, HImode, offset)); + emit_move_insn (adjust_address (dst, HImode, offset), reg); + offset += (inc < 0 ? -2 : 2); + remainder -= 2; + } + if (remainder & 1) + { + emit_move_insn (reg, adjust_address (src, QImode, offset)); + emit_move_insn (adjust_address (dst, QImode, offset), reg); + } +} + +/* Expand cpymem/movmem, as from __builtin_memcpy/memmove. + OPERANDS are the same as the cpymem/movmem patterns. + IS_MOVE is true if this is a memmove, false for memcpy. + Return true if we successfully expanded, or false if we cannot + and must punt to a libcall. */ + +bool +bpf_expand_cpymem (rtx *operands, bool is_move) +{ + /* Size must be constant for this expansion to work. */ + if (!CONST_INT_P (operands[2])) + { + const char *name = is_move ? "memmove" : "memcpy"; + if (flag_building_libgcc) + warning (RTL_LOCATION (operands[2]), + "could not expand call to %<__builtin_%s%> inline: " + "size must be constant", name); + else + error ("could not expand call to %<__builtin_%s%> inline: " + "size must be constant", name); + return false; + } + + /* Alignment is a CONST_INT. */ + gcc_assert (CONST_INT_P (operands[3])); + + rtx dst = operands[0]; + rtx src = operands[1]; + rtx size = operands[2]; + unsigned HOST_WIDE_INT size_bytes = UINTVAL (size); + unsigned align = UINTVAL (operands[3]); + enum machine_mode mode; + switch (align) + { + case 1: mode = QImode; break; + case 2: mode = HImode; break; + case 4: mode = SImode; break; + case 8: mode = DImode; break; + default: + gcc_unreachable (); + } + + unsigned iters = size_bytes >> ceil_log2 (align); + unsigned remainder = size_bytes & (align - 1); + + int inc = GET_MODE_SIZE (mode); + rtx_code_label *fwd_label, *done_label; + if (is_move) + { + /* For memmove, be careful of overlap. It is not a concern for memcpy. + To handle overlap, we check (at runtime) if SRC < DST, and if so do + the move "backwards" starting from SRC + SIZE. */ + fwd_label = gen_label_rtx (); + done_label = gen_label_rtx (); + + rtx dst_addr = copy_to_mode_reg (Pmode, XEXP (dst, 0)); + rtx src_addr = copy_to_mode_reg (Pmode, XEXP (src, 0)); + emit_cmp_and_jump_insns (src_addr, dst_addr, GEU, NULL_RTX, Pmode, + true, fwd_label, profile_probability::even ()); + + /* Emit the "backwards" unrolled loop. */ + emit_move_loop (src, dst, mode, size_bytes, -inc, iters, remainder); + emit_jump_insn (gen_jump (done_label)); + emit_barrier (); + + emit_label (fwd_label); + } + + emit_move_loop (src, dst, mode, 0, inc, iters, remainder); + + if (is_move) + emit_label (done_label); + + return true; +} + /* Finally, build the GCC target. */ struct gcc_target targetm = TARGET_INITIALIZER; diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md index 50df1aaa3e2..ca677bc6b50 100644 --- a/gcc/config/bpf/bpf.md +++ b/gcc/config/bpf/bpf.md @@ -627,4 +627,40 @@ (define_insn "ldabs" "{ldabs\t%0|r0 = *( *) skb[%0]}" [(set_attr "type" "ld")]) +;;; memmove and memcopy + +;; 0 is dst +;; 1 is src +;; 2 is size of copy in bytes +;; 3 is alignment + +(define_expand "cpymemdi" + [(match_operand:BLK 0 "memory_operand") + (match_operand:BLK 1 "memory_operand") + (match_operand:DI 2 "general_operand") + (match_operand:DI 3 "immediate_operand")] + "" +{ + if (bpf_expand_cpymem (operands, false)) + DONE; + FAIL; +}) + +;; 0 is dst +;; 1 is src +;; 2 is size of copy in bytes +;; 3 is alignment + +(define_expand "movmemdi" + [(match_operand:BLK 0 "memory_operand") + (match_operand:BLK 1 "memory_operand") + (match_operand:DI 2 "general_operand") + (match_operand:DI 3 "immediate_operand")] + "" +{ + if (bpf_expand_cpymem (operands, true)) + DONE; + FAIL; +}) + (include "atomic.md") diff --git a/gcc/testsuite/gcc.target/bpf/memcpy-1.c b/gcc/testsuite/gcc.target/bpf/memcpy-1.c new file mode 100644 index 00000000000..6c9707f24e8 --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/memcpy-1.c @@ -0,0 +1,26 @@ +/* Ensure memcpy is expanded inline rather than emitting a libcall. */ + +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +struct context { + unsigned int data; + unsigned int data_end; + unsigned int data_meta; + unsigned int ingress; + unsigned int queue_index; + unsigned int egress; +}; + +void +cpy_1(struct context *ctx) +{ + void *data = (void *)(long)ctx->data; + char *dest; + dest = data; + dest += 16; + + __builtin_memcpy (dest, data, 8); +} + +/* { dg-final { scan-assembler-times "call" 0 } } */ diff --git a/gcc/testsuite/gcc.target/bpf/memmove-1.c b/gcc/testsuite/gcc.target/bpf/memmove-1.c new file mode 100644 index 00000000000..3b8ba82639e --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/memmove-1.c @@ -0,0 +1,46 @@ +/* Ensure memmove is expanded inline rather than emitting a libcall. */ + +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +struct context { + unsigned int data; + unsigned int data_end; + unsigned int data_meta; + unsigned int ingress; + unsigned int queue_index; + unsigned int egress; +}; + +void +mov_1_nooverlap (struct context *ctx) +{ + void *data = (void *)(long)ctx->data; + char *dest; + dest = data; + dest += 16; + + __builtin_memmove (dest, data, 12); +} + +void +mov_1_overlap (struct context *ctx) +{ + void *data = (void *)(long)ctx->data; + char *dest; + dest = data; + dest += 4; + + __builtin_memmove (dest, data, 12); +} + +void +mov_1_arbitrary (struct context *ctx_a, struct context *ctx_b) +{ + void *src = (void *)(long)ctx_a->data; + void *dst = (void *)(long)ctx_b->data; + + __builtin_memmove (dst, src, 12); +} + +/* { dg-final { scan-assembler-times "call" 0 } } */ diff --git a/gcc/testsuite/gcc.target/bpf/memmove-2.c b/gcc/testsuite/gcc.target/bpf/memmove-2.c new file mode 100644 index 00000000000..7fae96095a3 --- /dev/null +++ b/gcc/testsuite/gcc.target/bpf/memmove-2.c @@ -0,0 +1,23 @@ +/* Test that we error if memmove cannot be expanded inline. */ + +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +typedef unsigned int __u32; + +struct context { + unsigned int data; + unsigned int data_end; + unsigned int data_meta; +}; + +void +mov_2_unsupported (struct context *ctx) +{ + void *data = (void *)(long)ctx->data; + char *dest; + dest = data; + dest += 4; + + __builtin_memmove (dest, data, ctx->data_meta); /* { dg-error "could not expand" } */ +}