From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id E68903858D35 for ; Tue, 30 Jun 2020 18:46:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E68903858D35 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 05UIVNV7121057; Tue, 30 Jun 2020 14:46:11 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3204h45yq7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Jun 2020 14:46:11 -0400 Received: from m0098421.ppops.net (m0098421.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 05UIedU0150172; Tue, 30 Jun 2020 14:46:10 -0400 Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 3204h45ypc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Jun 2020 14:46:10 -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 05UIjlDf002450; Tue, 30 Jun 2020 18:46:08 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 31wwr8c1ce-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 30 Jun 2020 18:46:08 +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 05UIk65x64094434 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 Jun 2020 18:46:06 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 00AAE11C052; Tue, 30 Jun 2020 18:46:06 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3E91911C04A; Tue, 30 Jun 2020 18:46:05 +0000 (GMT) Received: from localhost.localdomain (unknown [9.145.174.225]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 30 Jun 2020 18:46:05 +0000 (GMT) From: Ilya Leoshkevich To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Marc Glisse , Richard Biener , Jakub Jelinek , Ilya Leoshkevich Subject: [PATCH v2] tree-ssa-threadbackward.c (profitable_jump_thread_path): Do not allow __builtin_constant_p () before IPA. Date: Tue, 30 Jun 2020 20:46:02 +0200 Message-Id: <20200630184602.1228644-1-iii@linux.ibm.com> X-Mailer: git-send-email 2.25.4 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-06-30_06:2020-06-30, 2020-06-30 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 impostorscore=0 cotscore=-2147483648 mlxscore=0 clxscore=1011 lowpriorityscore=0 adultscore=0 suspectscore=0 priorityscore=1501 mlxlogscore=999 phishscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006300124 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, 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: Tue, 30 Jun 2020 18:46:13 -0000 v1: https://gcc.gnu.org/pipermail/gcc-patches/2020-June/547236.html This is the implementation of Jakub's suggestion: allow __builtin_constant_p () after IPA, but fold it into 0. Smoke test passed on s390x-redhat-linux, full regtest and bootstrap are running on x86_64-redhat-linux. --- Linux Kernel (specifically, drivers/leds/trigger/ledtrig-cpu.c) build with GCC 10 fails on s390 with "impossible constraint". The problem is that jump threading makes __builtin_constant_p () lie when it splits a path containing a non-constant expression in a way that on each of the resulting paths this expression is constant. Fix by disallowing __builtin_constant_p () on threading paths before IPA and fold it into 0 after IPA. gcc/ChangeLog: 2020-06-30 Ilya Leoshkevich * tree-ssa-threadbackward.c (thread_jumps::m_allow_bcp_p): New member. (thread_jumps::profitable_jump_thread_path): Do not allow __builtin_constant_p () on threading paths unless m_allow_bcp_p is set. (thread_jumps::find_jump_threads_backwards): Set m_allow_bcp_p. (pass_thread_jumps::execute): Allow __builtin_constant_p () on threading paths after IPA. (pass_early_thread_jumps::execute): Do not allow __builtin_constant_p () on threading paths before IPA. * tree-ssa-threadupdate.c (duplicate_thread_path): Fold __builtin_constant_p () on threading paths into 0. gcc/testsuite/ChangeLog: 2020-06-30 Ilya Leoshkevich * gcc.target/s390/builtin-constant-p-threading.c: New test. --- .../s390/builtin-constant-p-threading.c | 46 +++++++++++++++++++ gcc/tree-ssa-threadbackward.c | 22 ++++++--- gcc/tree-ssa-threadupdate.c | 18 ++++++++ 3 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.target/s390/builtin-constant-p-threading.c diff --git a/gcc/testsuite/gcc.target/s390/builtin-constant-p-threading.c b/gcc/testsuite/gcc.target/s390/builtin-constant-p-threading.c new file mode 100644 index 00000000000..5f0acdce0b0 --- /dev/null +++ b/gcc/testsuite/gcc.target/s390/builtin-constant-p-threading.c @@ -0,0 +1,46 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=z196 -mzarch" } */ + +typedef struct +{ + int counter; +} atomic_t; + +static inline __attribute__ ((__gnu_inline__)) int +__atomic_add (int val, int *ptr) +{ + int old; + asm volatile("laa %[old],%[val],%[ptr]\n" + : [old] "=d" (old), [ptr] "+Q"(*ptr) + : [val] "d" (val) + : "cc", "memory"); + return old; +} + +static inline __attribute__ ((__gnu_inline__)) void +__atomic_add_const (int val, int *ptr) +{ + asm volatile("asi %[ptr],%[val]\n" + : [ptr] "+Q" (*ptr) + : [val] "i" (val) + : "cc", "memory"); +} + +static inline __attribute__ ((__gnu_inline__)) void +atomic_add (int i, atomic_t *v) +{ + if (__builtin_constant_p (i) && (i > -129) && (i < 128)) + { + __atomic_add_const (i, &v->counter); + return; + } + __atomic_add (i, &v->counter); +} + +static atomic_t num_active_cpus = { (0) }; + +void +ledtrig_cpu (_Bool is_active) +{ + atomic_add (is_active ? 1 : -1, &num_active_cpus); +} diff --git a/gcc/tree-ssa-threadbackward.c b/gcc/tree-ssa-threadbackward.c index 327628f1662..446cb1b2e21 100644 --- a/gcc/tree-ssa-threadbackward.c +++ b/gcc/tree-ssa-threadbackward.c @@ -40,7 +40,9 @@ along with GCC; see the file COPYING3. If not see class thread_jumps { public: - void find_jump_threads_backwards (basic_block bb, bool speed_p); + void find_jump_threads_backwards (basic_block bb, bool speed_p, + bool allow_bcp_p); + private: edge profitable_jump_thread_path (basic_block bbi, tree name, tree arg, bool *creates_irreducible_loop); @@ -65,6 +67,8 @@ class thread_jumps /* Indicate that we could increase code size to improve the code path. */ bool m_speed_p; + /* Whether to allow __builtin_constant_p () on threading paths. */ + bool m_allow_bcp_p; }; /* Simple helper to get the last statement from BB, which is assumed @@ -260,7 +264,9 @@ thread_jumps::profitable_jump_thread_path (basic_block bbi, tree name, gsi_next_nondebug (&gsi)) { gimple *stmt = gsi_stmt (gsi); - if (gimple_call_internal_p (stmt, IFN_UNIQUE)) + if (gimple_call_internal_p (stmt, IFN_UNIQUE) + || (!m_allow_bcp_p + && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P))) { m_path.pop (); return NULL; @@ -737,10 +743,13 @@ thread_jumps::fsm_find_control_statement_thread_paths (tree name) It is assumed that BB ends with a control statement and that by finding a path where NAME is a constant, we can thread the path. SPEED_P indicates that we could increase code size to improve the - code path. */ + code path. ALLOW_BCP_P specifies whether to allow __builtin_constant_p () + on threading paths and fold it into 0, or whether to disallow it + altogether. */ void -thread_jumps::find_jump_threads_backwards (basic_block bb, bool speed_p) +thread_jumps::find_jump_threads_backwards (basic_block bb, bool speed_p, + bool allow_bcp_p) { gimple *stmt = get_gimple_control_stmt (bb); if (!stmt) @@ -770,6 +779,7 @@ thread_jumps::find_jump_threads_backwards (basic_block bb, bool speed_p) m_visited_bbs.empty (); m_seen_loop_phi = false; m_speed_p = speed_p; + m_allow_bcp_p = allow_bcp_p; m_max_threaded_paths = param_max_fsm_thread_paths; fsm_find_control_statement_thread_paths (name); @@ -820,7 +830,7 @@ pass_thread_jumps::execute (function *fun) FOR_EACH_BB_FN (bb, fun) { if (EDGE_COUNT (bb->succs) > 1) - threader.find_jump_threads_backwards (bb, true); + threader.find_jump_threads_backwards (bb, true, true); } bool changed = thread_through_all_blocks (true); @@ -881,7 +891,7 @@ pass_early_thread_jumps::execute (function *fun) FOR_EACH_BB_FN (bb, fun) { if (EDGE_COUNT (bb->succs) > 1) - threader.find_jump_threads_backwards (bb, false); + threader.find_jump_threads_backwards (bb, false, false); } thread_through_all_blocks (true); diff --git a/gcc/tree-ssa-threadupdate.c b/gcc/tree-ssa-threadupdate.c index 5abecf6c295..4b39e6d76e5 100644 --- a/gcc/tree-ssa-threadupdate.c +++ b/gcc/tree-ssa-threadupdate.c @@ -2365,6 +2365,24 @@ duplicate_thread_path (edge entry, edge exit, basic_block *region, edge_iterator ei; basic_block bb = region_copy[i]; + /* Fold __builtin_constant_p () calls into 0. We act so conservatively, + because threading could have split a non-constant expression into + multiple constant ones, and we don't know whether corresponding paths + converge before all statements that depends on __builtin_constant_p () + result. */ + for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi); + gsi_next_nondebug (&gsi)) + { + gimple *stmt = gsi_stmt (gsi); + if (gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)) + { + tree lhs = gimple_call_lhs (stmt); + tree zero = build_int_cst (TREE_TYPE (lhs), 0); + gimple *repl = gimple_build_assign (lhs, zero); + gsi_replace (&gsi, repl, false); + } + } + /* Watch inconsistent profile. */ if (curr_count > region[i]->count) curr_count = region[i]->count; -- 2.25.4