From patchwork Thu Jun 29 20:49:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 114435 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9904417vqr; Thu, 29 Jun 2023 13:50:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlFJIz5ThgpJNr+XuEtm6wvCWmNWmtNh3Bm/NIZLmTx3Cl1ua1/SCotKWMFB5m/ryTyyR3gc X-Received: by 2002:a17:906:ae85:b0:991:e849:e13b with SMTP id md5-20020a170906ae8500b00991e849e13bmr382850ejb.10.1688071808776; Thu, 29 Jun 2023 13:50:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688071808; cv=none; d=google.com; s=arc-20160816; b=id0qBmoSDxAICOkZbhi6TBLqkxD1S/3rRzEVisv608e0RcFMkTrVXNcoDOOfHlO3Jj EGNlp8UNbKogFfCJBmh5dkYUgGbfGMpx1UqD+xu3BZErAaw76yikcEyt7MyHhSnEIa/u 9wdvtvXywLmLFCGGXFqguhnOaheGBwG5WgTuELX1BWgzcGht2+K+fl9g/1kSwPs2oSZB QuZkRqWYT0XzH/goRU1G6LCEnGAOICoR8MmbNjxCk2+Qy+utoaW5/2UsrxICisQ0ckrK eg/BqEkTsn1DEsVp6sKELJ7zmSkNtkFopjLt2VfVbE/e8Rt2xgBsAE2T3EDOL0wqHhA2 f8nA== 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:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=2gcUV5jPwba0g5dI6OVweu5KAIwkU39nmQq9ewaFe1M=; fh=kBESpujLxfPHGMaeiF9gaQs/WM0XQOCqJE5Z9Fbzubs=; b=qNfwDG4wzbr+Y10Fxi3GTH85mIs7IyaBrCJVGC5UYrMHiOXPCZD4aPp08aaiaj3XpZ WfDcoqN/Fp7BpLApPmuJGhat5sdRLfn7GPi5hliZdvD6WKk/YFk1yKiGqwyHbxoKWp/g WHb44/3sOyqxX+EvmViH4e5QQuIw2/Wck9XFzI+qIaUFBPn76oCkSGWHOFhKy3Ra1pzF /aqcMFIkdy8rp+9y8xjSo18De3QCSPRb6oKXvaNIdMmGAQTrpt8M8hVr5XWhCYeAe3aU FaA+WL/QE2rwgfat7rIm0faY54r6SbIF0I00A44VtqLFr9Lua7c/2VWkyeEo5c1Onx4W KV4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OmvjIMrs; 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 b1-20020a1709065e4100b00992c195ac80si738633eju.357.2023.06.29.13.50.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jun 2023 13:50:08 -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=OmvjIMrs; 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 7A956385842B for ; Thu, 29 Jun 2023 20:50:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7A956385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688071807; bh=2gcUV5jPwba0g5dI6OVweu5KAIwkU39nmQq9ewaFe1M=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OmvjIMrsaMk5EpcUuIy1EPZ5E2tZrpA8js5fCbMTIBRt535jsmeeIpUSYruPJ+Ztd hd5n8vMCYKs7dDN1OzJ2AA5h7bwMsnvfi2nWQZyhGKRJtmM4d7GzgTR+bAv1c/G0og 5/BoTNEcaJUyF/VWfDXlbbzHfXFW/4E1GpuIG2tY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id 713593858D35 for ; Thu, 29 Jun 2023 20:49:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 713593858D35 Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id F06AC281D1F; Thu, 29 Jun 2023 22:49:21 +0200 (CEST) Date: Thu, 29 Jun 2023 22:49:21 +0200 To: gcc-patches@gcc.gnu.org, jwakely@redhat.com Subject: Extend ipa-fnsummary to skip __builtin_expect Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jan Hubicka via Gcc-patches From: Jan Hubicka Reply-To: Jan Hubicka 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?1770071585142568778?= X-GMAIL-MSGID: =?utf-8?q?1770071585142568778?= Compute ipa-predicates for conditionals involving __builtin_expect_p std::vector allocator looks as follows: __attribute__((nodiscard)) struct pair * std::__new_allocator >::allocate (struct __new_allocator * const this, size_type __n, const void * D.27753) { bool _1; long int _2; long int _3; long unsigned int _5; struct pair * _9; [local count: 1073741824]: _1 = __n_7(D) > 1152921504606846975; _2 = (long int) _1; _3 = __builtin_expect (_2, 0); if (_3 != 0) goto ; [10.00%] else goto ; [90.00%] [local count: 107374184]: if (__n_7(D) > 2305843009213693951) goto ; [50.00%] else goto ; [50.00%] [local count: 53687092]: std::__throw_bad_array_new_length (); [local count: 53687092]: std::__throw_bad_alloc (); [local count: 966367641]: _5 = __n_7(D) * 8; _9 = operator new (_5); return _9; } So there is check for allocated block size being greater than max_size which is wrapper in __builtin_expect. This makes ipa-fnsummary to give up analyzing predicates and it will miss the fact that the two different calls to __throw will be optimized out if __n is larady smaller than 1152921504606846975 which it is after _M_check_len. This patch extends ipa-fnsummary to understand functions that return their parameter. We still do not get the value range propagated sicne _M_check_len is not inlined early and ipa-prop misses return functions, but we get closer :) Bootstrapped/regtested x86_64-linux, comitted. gcc/ChangeLog: PR tree-optimization/109849 * ipa-fnsummary.cc (decompose_param_expr): Skip functions returning its parameter. (set_cond_stmt_execution_predicate): Return early if predicate was constructed. gcc/testsuite/ChangeLog: PR tree-optimization/109849 * gcc.dg/ipa/pr109849.c: New test. diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc index 78cbb60d056..a09f6305c63 100644 --- a/gcc/ipa-fnsummary.cc +++ b/gcc/ipa-fnsummary.cc @@ -1516,6 +1516,19 @@ decompose_param_expr (struct ipa_func_body_info *fbi, if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr)) break; + stmt = SSA_NAME_DEF_STMT (expr); + + if (gcall *call = dyn_cast (stmt)) + { + int flags = gimple_call_return_flags (call); + if (!(flags & ERF_RETURNS_ARG)) + goto fail; + int arg = flags & ERF_RETURN_ARG_MASK; + if (arg >= (int)gimple_call_num_args (call)) + goto fail; + expr = gimple_call_arg (stmt, arg); + continue; + } if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr))) break; @@ -1664,6 +1677,7 @@ set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi, } } vec_free (param_ops); + return; } if (TREE_CODE (op) != SSA_NAME) diff --git a/gcc/testsuite/gcc.dg/ipa/pr109849.c b/gcc/testsuite/gcc.dg/ipa/pr109849.c new file mode 100644 index 00000000000..09b62f90d70 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/pr109849.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-Os -fdump-ipa-inline-details" } */ +void bad (void); +void +test(int a) +{ + if (__builtin_expect (a>3, 0)) + { + bad (); + bad (); + bad (); + bad (); + bad (); + bad (); + bad (); + bad (); + } +} +void +foo (int a) +{ + if (a>0) + __builtin_unreachable (); + test (a); +} +/* { dg-final { scan-ipa-dump "Inlined 2 calls" "inline" } } */ +/* { dg-final { scan-ipa-dump "Inlining test" "inline" } } */