From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id BD5243858D38 for ; Thu, 20 Aug 2020 04:34:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BD5243858D38 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07K4XkHH103034; Thu, 20 Aug 2020 00:34:52 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 330ucp1wqg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Aug 2020 00:34:52 -0400 Received: from m0098414.ppops.net (m0098414.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 07K4Ypb8105264; Thu, 20 Aug 2020 00:34:52 -0400 Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0b-001b2d01.pphosted.com with ESMTP id 330ucp1wq5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Aug 2020 00:34:51 -0400 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 07K4TogA003109; Thu, 20 Aug 2020 04:34:50 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma03ams.nl.ibm.com with ESMTP id 3304um2ntu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Aug 2020 04:34:50 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 07K4YlVR24248576 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Aug 2020 04:34:47 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3537B11C058; Thu, 20 Aug 2020 04:34:47 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 59F5711C05C; Thu, 20 Aug 2020 04:34:46 +0000 (GMT) Received: from genoa.aus.stglabs.ibm.com (unknown [9.40.192.157]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 20 Aug 2020 04:34:46 +0000 (GMT) From: guojiufu To: gcc-patches@gcc.gnu.org Cc: guojiufu@linux.ibm.com, wschmidt@linux.ibm.com, segher@kernel.crashing.org, dje.gcc@gmail.com Subject: [PATCH] Check calls before loop unrolling Date: Thu, 20 Aug 2020 12:34:45 +0800 Message-Id: <20200820043445.2216872-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235, 18.0.687 definitions=2020-08-19_13:2020-08-19, 2020-08-19 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 priorityscore=1501 adultscore=0 malwarescore=0 impostorscore=0 suspectscore=3 mlxscore=0 mlxlogscore=768 clxscore=1015 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008200034 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Thu, 20 Aug 2020 04:34:55 -0000 Hi, When unroll loops, if there are calls inside the loop, those calls may raise negative impacts for unrolling. This patch adds a param param_max_unrolled_calls, and checks if the number of calls inside the loop bigger than this param, loop is prevent from unrolling. This patch is checking the _average_ number of calls which is the summary of call numbers multiply the possibility of the call maybe executed. The _average_ number could be a fraction, to keep the precision, the param is the threshold number multiply 10000. Bootstrap and regtest pass on powerpc64le. Is this ok for trunk? gcc/ChangeLog 2020-08-19 Jiufu Guo * params.opt (param_max_unrolled_average_calls_x10000): New param. * cfgloop.h (average_num_loop_calls): New declare. * cfgloopanal.c (average_num_loop_calls): New function. * loop-unroll.c (decide_unroll_constant_iteration, decide_unroll_runtime_iterations, decide_unroll_stupid): Check average_num_loop_calls and param_max_unrolled_average_calls_x10000. --- gcc/cfgloop.h | 2 ++ gcc/cfgloopanal.c | 25 +++++++++++++++++++++++++ gcc/loop-unroll.c | 10 ++++++++++ gcc/params.opt | 4 ++++ 4 files changed, 41 insertions(+) diff --git a/gcc/cfgloop.h b/gcc/cfgloop.h index 18b404e292f..dab933da150 100644 --- a/gcc/cfgloop.h +++ b/gcc/cfgloop.h @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #define GCC_CFGLOOP_H #include "cfgloopmanip.h" +#include "sreal.h" /* Structure to hold decision about unrolling/peeling. */ enum lpt_dec @@ -387,6 +388,7 @@ extern vec get_loop_exit_edges (const class loop *, basic_block * = NULL); extern edge single_exit (const class loop *); extern edge single_likely_exit (class loop *loop, vec); extern unsigned num_loop_branches (const class loop *); +extern sreal average_num_loop_calls (const class loop *); extern edge loop_preheader_edge (const class loop *); extern edge loop_latch_edge (const class loop *); diff --git a/gcc/cfgloopanal.c b/gcc/cfgloopanal.c index 0b33e8272a7..a314db4e0c0 100644 --- a/gcc/cfgloopanal.c +++ b/gcc/cfgloopanal.c @@ -233,6 +233,31 @@ average_num_loop_insns (const class loop *loop) return ret; } +/* Count the number of call insns in LOOP. */ +sreal +average_num_loop_calls (const class loop *loop) +{ + basic_block *bbs; + rtx_insn *insn; + unsigned int i, bncalls; + sreal ncalls = 0; + + bbs = get_loop_body (loop); + for (i = 0; i < loop->num_nodes; i++) + { + bncalls = 0; + FOR_BB_INSNS (bbs[i], insn) + if (CALL_P (insn)) + bncalls++; + + ncalls += (sreal) bncalls + * bbs[i]->count.to_sreal_scale (loop->header->count); + } + free (bbs); + + return ncalls; +} + /* Returns expected number of iterations of LOOP, according to measured or guessed profile. diff --git a/gcc/loop-unroll.c b/gcc/loop-unroll.c index 693c7768868..56b8fb37d2a 100644 --- a/gcc/loop-unroll.c +++ b/gcc/loop-unroll.c @@ -370,6 +370,10 @@ decide_unroll_constant_iterations (class loop *loop, int flags) nunroll = nunroll_by_av; if (nunroll > (unsigned) param_max_unroll_times) nunroll = param_max_unroll_times; + if (!loop->unroll + && (average_num_loop_calls (loop) * (sreal) 10000).to_int () + > (unsigned) param_max_unrolled_average_calls_x10000) + nunroll = 0; if (targetm.loop_unroll_adjust) nunroll = targetm.loop_unroll_adjust (nunroll, loop); @@ -689,6 +693,9 @@ decide_unroll_runtime_iterations (class loop *loop, int flags) nunroll = nunroll_by_av; if (nunroll > (unsigned) param_max_unroll_times) nunroll = param_max_unroll_times; + if ((average_num_loop_calls (loop) * (sreal) 10000).to_int () + > (unsigned) param_max_unrolled_average_calls_x10000) + nunroll = 0; if (targetm.loop_unroll_adjust) nunroll = targetm.loop_unroll_adjust (nunroll, loop); @@ -1173,6 +1180,9 @@ decide_unroll_stupid (class loop *loop, int flags) nunroll = nunroll_by_av; if (nunroll > (unsigned) param_max_unroll_times) nunroll = param_max_unroll_times; + if ((average_num_loop_calls (loop) * (sreal) 10000).to_int () + > (unsigned) param_max_unrolled_average_calls_x10000) + nunroll = 0; if (targetm.loop_unroll_adjust) nunroll = targetm.loop_unroll_adjust (nunroll, loop); diff --git a/gcc/params.opt b/gcc/params.opt index f39e5d1a012..80605861223 100644 --- a/gcc/params.opt +++ b/gcc/params.opt @@ -634,6 +634,10 @@ The maximum number of unrollings of a single loop. Common Joined UInteger Var(param_max_unrolled_insns) Init(200) Param Optimization The maximum number of instructions to consider to unroll in a loop. +-param=max-unrolled-average-calls-x10000= +Common Joined UInteger Var(param_max_unrolled_average_calls_x10000) Init(0) Param Optimization +The maximum number of calls to consider to unroll in a loop on average and multiply 10000. + -param=max-unswitch-insns= Common Joined UInteger Var(param_max_unswitch_insns) Init(50) Param Optimization The maximum number of insns of an unswitched loop. -- 2.25.1