From patchwork Wed Jun 14 13:52:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 107957 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1273861vqr; Wed, 14 Jun 2023 06:53:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6qFAn3bPpluQs4DcmVCi955mXUXCgzUHrwEz5Sc7A3j+vaBMe34t4XnbjD0PLva0Ra+A90 X-Received: by 2002:a05:6512:521:b0:4f4:c6ab:f11b with SMTP id o1-20020a056512052100b004f4c6abf11bmr9029795lfc.39.1686750833057; Wed, 14 Jun 2023 06:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686750833; cv=none; d=google.com; s=arc-20160816; b=xHor/zrEtlosAhnd+shj6NKOSry2URT7r4oqa4M4G3M9LQj5LHFmbWpcX4FO66obW6 rmrQO4Vg9F2YCppccI9ZDwyCeZaBWRbz7GVPBhtYIM9SyQiIah35Zh4wudriEIEECJRI 6Xv4Kq1fRninBW6GVU6CiQMyOaW0m2+XqabpX1xIXIJ3Bx/lms9zLIbAOKInXKrVVUaT Qnw4NpfNNLDcsfF9Xm/TJiWgNQGalYFJXXCSmx4IEiKtuaRDUGHf7IDVJ9/RhRCNZLfg 3I/xFodWpfsIY2ZbSMnSpUMBVy2IM2/DBH4iYXTKQubXxW3/huBuxllgTAAAKWzk8mFD Amkw== 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-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=yujLyhZGWzH9+492jfZQe0NbgEloI9Q8ntOgiUIVo+I=; b=OkCVoozihQK5oKL7+8r6nGXNgbo3m3L1sWvJB7jcx3Kb4L78dZixsDqNVKUZJsDCHr PwBkrUuz6eTZJT8N5dyVcwWrXgg1BCJnkPEU0lw+Q2s9HbuBCMJyu+ZIMMUjepw7bdbo goUlYj3RBiZYrZ506vr3oqegsFhWqOpj4XJ35LWfczjuL/Mf5thtR7o9vhYfF3Pr3Vfy hYxECCxVVeSOjTcqceUnSloIaAwq0dv7fqXwl2X/Sek+HVmbn6UOvIDRZGqhSwuiljaQ lc0oTZKs9xmhGNCUwCHlFmE2et6fc4bzAMF4KY6fDYTz0F/7VZHtwq3gqiMdd2rYEQ5Y 1ozw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=npUuiHAv; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id le9-20020a170907170900b0097738977c35si5077276ejc.850.2023.06.14.06.53.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 06:53:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=npUuiHAv; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 34AD738582BD for ; Wed, 14 Jun 2023 13:53:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34AD738582BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686750831; bh=yujLyhZGWzH9+492jfZQe0NbgEloI9Q8ntOgiUIVo+I=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=npUuiHAvFMqc/zHiluxhm6+4zPlMTb3cPmMpyvtPHoC7AzCGjW6cTgq5S1ZdzMidq CV3HXnoxHW2MIpR1aGpGKRYl3O3YyJvJlm8tIHsDILTGcX5F7Jb2AQultBAcOTJzOO UjKSFptONacgzDBd7cT8KEc/pBt8h4M1SHdmAqzs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id B5C063858D1E for ; Wed, 14 Jun 2023 13:53:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B5C063858D1E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-516-bGtABzp3Pfavo6_VJwwp-Q-1; Wed, 14 Jun 2023 09:53:07 -0400 X-MC-Unique: bGtABzp3Pfavo6_VJwwp-Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DD81A196F58D; Wed, 14 Jun 2023 13:52:59 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A13A1C1604C; Wed, 14 Jun 2023 13:52:59 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 35EDqumk1149341 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 14 Jun 2023 15:52:57 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 35EDquir1149340; Wed, 14 Jun 2023 15:52:56 +0200 Date: Wed, 14 Jun 2023 15:52:55 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] middle-end: Move constant args folding of .UBSAN_CHECK_* and .*_OVERFLOW into fold-const-call.cc Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek 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?1768686441400998727?= X-GMAIL-MSGID: =?utf-8?q?1768686441400998727?= On Wed, Jun 14, 2023 at 12:25:46PM +0000, Richard Biener wrote: > I think that's still very much desirable so this followup looks OK. > Maybe you can re-base it as prerequesite though? Rebased then (of course with the UADDC/USUBC handling removed from this first patch, will be added in the second one). Ok for trunk if it passes bootstrap/regtest? 2023-06-14 Jakub Jelinek * gimple-fold.cc (gimple_fold_call): Move handling of arg0 as well as arg1 INTEGER_CSTs for .UBSAN_CHECK_{ADD,SUB,MUL} and .{ADD,SUB,MUL}_OVERFLOW calls from here... * fold-const-call.cc (fold_const_call): ... here. Jakub --- gcc/gimple-fold.cc.jj 2023-06-13 18:23:37.199793275 +0200 +++ gcc/gimple-fold.cc 2023-06-14 15:41:51.090987708 +0200 @@ -5702,22 +5702,6 @@ gimple_fold_call (gimple_stmt_iterator * result = arg0; else if (subcode == MULT_EXPR && integer_onep (arg0)) result = arg1; - else if (TREE_CODE (arg0) == INTEGER_CST - && TREE_CODE (arg1) == INTEGER_CST) - { - if (cplx_result) - result = int_const_binop (subcode, fold_convert (type, arg0), - fold_convert (type, arg1)); - else - result = int_const_binop (subcode, arg0, arg1); - if (result && arith_overflowed_p (subcode, type, arg0, arg1)) - { - if (cplx_result) - overflow = build_one_cst (type); - else - result = NULL_TREE; - } - } if (result) { if (result == integer_zero_node) --- gcc/fold-const-call.cc.jj 2023-06-02 10:36:43.096967505 +0200 +++ gcc/fold-const-call.cc 2023-06-14 15:40:34.388064498 +0200 @@ -1669,6 +1669,7 @@ fold_const_call (combined_fn fn, tree ty { const char *p0, *p1; char c; + tree_code subcode; switch (fn) { case CFN_BUILT_IN_STRSPN: @@ -1738,6 +1739,46 @@ fold_const_call (combined_fn fn, tree ty case CFN_FOLD_LEFT_PLUS: return fold_const_fold_left (type, arg0, arg1, PLUS_EXPR); + case CFN_UBSAN_CHECK_ADD: + case CFN_ADD_OVERFLOW: + subcode = PLUS_EXPR; + goto arith_overflow; + + case CFN_UBSAN_CHECK_SUB: + case CFN_SUB_OVERFLOW: + subcode = MINUS_EXPR; + goto arith_overflow; + + case CFN_UBSAN_CHECK_MUL: + case CFN_MUL_OVERFLOW: + subcode = MULT_EXPR; + goto arith_overflow; + + arith_overflow: + if (integer_cst_p (arg0) && integer_cst_p (arg1)) + { + tree itype + = TREE_CODE (type) == COMPLEX_TYPE ? TREE_TYPE (type) : type; + bool ovf = false; + tree r = int_const_binop (subcode, fold_convert (itype, arg0), + fold_convert (itype, arg1)); + if (!r || TREE_CODE (r) != INTEGER_CST) + return NULL_TREE; + if (arith_overflowed_p (subcode, itype, arg0, arg1)) + ovf = true; + if (TREE_OVERFLOW (r)) + r = drop_tree_overflow (r); + if (itype == type) + { + if (ovf) + return NULL_TREE; + return r; + } + else + return build_complex (type, r, build_int_cst (itype, ovf)); + } + return NULL_TREE; + default: return fold_const_call_1 (fn, type, arg0, arg1); }