From patchwork Tue Jan 16 08:17:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 84162 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 705DD3858412 for ; Tue, 16 Jan 2024 08:18:52 +0000 (GMT) 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 B7C7E3858D1E for ; Tue, 16 Jan 2024 08:18:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7C7E3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B7C7E3858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705393095; cv=none; b=V7otq6nI+yGTPjTaXQuqeeHz2m14SGb7Sw2XRWwME+f6Hv0/Sgeh4udjmpr9s0C7H1lywJ/2E5XjYDXh/C+wYVRdd3jv524QGOjJkLPQfueXIAslfUy0sgA6gWVezGqkwpFOmCx4JSyUBRdM5yM1olUU6nxo780VrhBOpY2eAUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705393095; c=relaxed/simple; bh=o5XFO+lnvpIVvppf2ThgFdHNvTF1Fs5bK3OjLc+iN9I=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=jOm0TlFdwphFC27lsPVCZ7YFNWkEnVfXRdGdFjjUsT5ny51cKphU2AVtP3cD8ttonoH0H8ev+l5Eg5HyC1QXuOWg2PZT9YnhCOVEW/ZmFo9U7XGGm9eOBU6FNn+oIm80mAj2Gwr7SNFmvJn6FqnU+dOcCDlMOTL4QcLioALYErY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705393084; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=/m07pxqqvQbmd5k/NuMel680t8xjQOLnfig+9ckU89w=; b=dOPrSOPyJVNy9MAZ0cpZl/HjoYi4C9FN4ah9fEm/A82ixCto5gVLllObFPMdYeMunpm/0j Cy/FPAkOL3OmEKxLrwHA7+K2gCDKeuguvAdWb2f6ICCjOrNwixvzEpZvx0/zUB3tETIdXb LRvP/UHcOHnYR9IodvKjsCobkJqx9r0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-272-wwgkk3ACNleTonbi06vrZg-1; Tue, 16 Jan 2024 03:18:02 -0500 X-MC-Unique: wwgkk3ACNleTonbi06vrZg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B35F5185A784; Tue, 16 Jan 2024 08:18:01 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 76BDB28AC; Tue, 16 Jan 2024 08:18:01 +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 40G8Hw5c3011961 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 16 Jan 2024 09:17:59 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40G8HwkJ3011960; Tue, 16 Jan 2024 09:17:58 +0100 Date: Tue, 16 Jan 2024 09:17:57 +0100 From: Jakub Jelinek To: Richard Biener , Michael Matz Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] cfgexpand: Workaround CSE of ADDR_EXPRs in VAR_DECL partitioning [PR113372] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.3 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_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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! The following patch adds a quick workaround to bugs in VAR_DECL partitioning. The problem is that there is no dependency between ADDR_EXPRs of local decls and CLOBBERs of those vars, so VN can CSE uses of ADDR_EXPRs (including ivopts integral variants thereof), which can break add_scope_conflicts discovery of what variables are actually used in certain region. E.g. we can have ivtmp.40_3 = (unsigned long) &MEM [(void *)&bitint.6 + 8B]; ... uses of ivtmp.40_3 ... bitint.6 ={v} {CLOBBER(eos)}; ... ivtmp.28_43 = (unsigned long) &MEM [(void *)&bitint.6 + 8B]; ... uses of ivtmp.28_43 before VN (such as dom3), which the add_scope_conflicts code identifies as 2 independent uses of bitint.6 variable (which is correct), but then VN determines ivtmp.28_43 is the same as ivtmp.40_3 and just uses ivtmp.40_3 even in the second region; at that point add_scope_conflict thinks the bitint.6 variable is not used in that region anymore. The following patch does a simple single def-stmt check for such ADDR_EXPRs (rather than say trying to do a full propagation of what SSA_NAMEs can contain ADDR_EXPRs of local variables), which seems to workaround all 4 PRs. In addition to this patch I've used the attached one to gather statistics on the total size of all variable partitions in a function and seems besides the new testcases nothing is really affected compared to no patch (I've actually just modified the patch to == OMP_SCAN instead of == ADDR_EXPR, so it looks the same except that it never triggers). The comparison wasn't perfect because I've only gathered BITS_PER_WORD, main_input_filename (did some replacement of build directories and /tmp/ccXXXXXX names of LTO to make it more similar between the two bootstraps/regtests), current_function_name and the total size of all variable partitions if any, because I didn't record e.g. the optimization options and so e.g. torture tests which iterate over options could have different partition sizes even in one compiler when BITS_PER_WORD, main_input_filename and current_function_name are all equal. So had to write an awk script to check if the first triple in the second build appeared in the first one and the quadruple in the second build appeared in the first one too, otherwise print result and that only triggered in the new tests. Also, the cc1plus binary according to objdump -dr is identical between the two builds except for the ADDR_EXPR vs. OMP_SCAN constant in the two spots. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Or should I just use INTEGRAL_TYPE_P instead of ptrofftype_p? 2024-01-15 Jakub Jelinek PR tree-optimization/113372 PR middle-end/90348 PR middle-end/110115 PR middle-end/111422 * cfgexpand.cc (add_scope_conflicts_2): New function. (add_scope_conflicts_1): Use it. * gcc.dg/torture/bitint-49.c: New test. * gcc.c-torture/execute/pr90348.c: New test. * gcc.c-torture/execute/pr110115.c: New test. * gcc.c-torture/execute/pr111422.c: New test. Jakub --- gcc/cfgexpand.cc.jj 2024-01-15 16:08:00.686573778 +0100 +++ gcc/cfgexpand.cc 2024-01-15 16:19:20.452082787 +0100 @@ -2312,6 +2312,20 @@ expand_used_vars (bitmap forced_stack_va partition_stack_vars (); if (dump_file) dump_stack_var_partition (); +size_t si, i, n = stack_vars_num; +unsigned long long sum = 0; +for (si = 0; si < n; ++si) +{ +unsigned HOST_WIDE_INT sz; +i = stack_vars_sorted[si]; +if (stack_vars[i].representative != i) +continue; +if (stack_vars[i].size.is_constant (&sz)) +sum += sz; +} +FILE *f = fopen ("/tmp/stackvars", "a"); +fprintf (f, "%d %s %s %lld\n", (int) BITS_PER_WORD, main_input_filename ? main_input_filename : "-", current_function_name (), sum); +fclose (f); } --- gcc/cfgexpand.cc.jj 2024-01-03 11:51:28.262776560 +0100 +++ gcc/cfgexpand.cc 2024-01-15 16:08:00.686573778 +0100 @@ -571,6 +571,25 @@ visit_conflict (gimple *, tree op, tree, return false; } +/* Helper function for add_scope_conflicts_1. For USE on + a stmt, if it is a SSA_NAME and in its SSA_NAME_DEF_STMT is known to be + based on some ADDR_EXPR, invoke VISIT on that ADDR_EXPR. */ + +static inline void +add_scope_conflicts_2 (tree use, bitmap work, + walk_stmt_load_store_addr_fn visit) +{ + if (TREE_CODE (use) == SSA_NAME + && (POINTER_TYPE_P (TREE_TYPE (use)) || ptrofftype_p (TREE_TYPE (use)))) + { + gimple *g = SSA_NAME_DEF_STMT (use); + if (is_gimple_assign (g)) + if (tree op = gimple_assign_rhs1 (g)) + if (TREE_CODE (op) == ADDR_EXPR) + visit (g, TREE_OPERAND (op, 0), op, work); + } +} + /* Helper routine for add_scope_conflicts, calculating the active partitions at the end of BB, leaving the result in WORK. We're called to generate conflicts when FOR_CONFLICT is true, otherwise we're just tracking @@ -583,6 +602,8 @@ add_scope_conflicts_1 (basic_block bb, b edge_iterator ei; gimple_stmt_iterator gsi; walk_stmt_load_store_addr_fn visit; + use_operand_p use_p; + ssa_op_iter iter; bitmap_clear (work); FOR_EACH_EDGE (e, ei, bb->preds) @@ -593,7 +614,10 @@ add_scope_conflicts_1 (basic_block bb, b for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple *stmt = gsi_stmt (gsi); + gphi *phi = as_a (stmt); walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit); + FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE) + add_scope_conflicts_2 (USE_FROM_PTR (use_p), work, visit); } for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { @@ -613,8 +637,7 @@ add_scope_conflicts_1 (basic_block bb, b } else if (!is_gimple_debug (stmt)) { - if (for_conflict - && visit == visit_op) + if (for_conflict && visit == visit_op) { /* If this is the first real instruction in this BB we need to add conflicts for everything live at this point now. @@ -634,6 +657,8 @@ add_scope_conflicts_1 (basic_block bb, b visit = visit_conflict; } walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit); + FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) + add_scope_conflicts_2 (USE_FROM_PTR (use_p), work, visit); } } } --- gcc/testsuite/gcc.dg/torture/bitint-49.c.jj 2024-01-15 15:13:54.590737604 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-49.c 2024-01-15 15:14:19.864387438 +0100 @@ -0,0 +1,28 @@ +/* PR tree-optimization/113372 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O1" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +_BitInt(8) a, b, c; + +#if __BITINT_MAXWIDTH__ >= 6384 +_BitInt(8) +foo (_BitInt(6384) y) +{ + _BitInt(4745) x = -(b % y) * b; + int i = __builtin_sub_overflow_p (-y, 0, 0); + c |= __builtin_add_overflow_p (i, 0, a); + return x; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 6384 + if (foo (4) != 0 || c != 0) + __builtin_abort (); +#endif + return 0; +} --- gcc/testsuite/gcc.c-torture/execute/pr90348.c.jj 2024-01-15 15:15:24.369493734 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr90348.c 2019-05-07 13:02:47.155066415 +0200 @@ -0,0 +1,38 @@ +/* PR middle-end/90348 */ + +void __attribute__ ((noipa)) +set_one (unsigned char *ptr) +{ + *ptr = 1; +} + +void __attribute__ ((noipa)) +check_zero (unsigned char const *in, unsigned int len) +{ + for (unsigned int i = 0; i < len; ++i) + if (in[i] != 0) + __builtin_abort (); +} + +static void +set_one_on_stack (void) +{ + unsigned char buf[1]; + set_one (buf); +} + +int +main () +{ + for (int i = 0; i <= 4; ++i) + { + unsigned char in[4]; + for (int j = 0; j < i; ++j) + { + in[j] = 0; + set_one_on_stack (); + } + check_zero (in, i); + } + return 0; +} --- gcc/testsuite/gcc.c-torture/execute/pr110115.c.jj 2024-01-15 15:18:42.327751055 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr110115.c 2024-01-15 15:18:33.094878974 +0100 @@ -0,0 +1,45 @@ +/* PR middle-end/110115 */ + +int a; +signed char b; + +static int +foo (signed char *e, int f) +{ + int d; + for (d = 0; d < f; d++) + e[d] = 0; + return d; +} + +int +bar (signed char e, int f) +{ + signed char h[20]; + int i = foo (h, f); + return i; +} + +int +baz () +{ + switch (a) + { + case 'f': + return 0; + default: + return ~0; + } +} + +int +main () +{ + { + signed char *k[3]; + int d; + for (d = 0; bar (8, 15) - 15 + d < 1; d++) + k[baz () + 1] = &b; + *k[0] = -*k[0]; + } +} --- gcc/testsuite/gcc.c-torture/execute/pr111422.c.jj 2024-01-15 15:21:35.205355850 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr111422.c 2024-01-15 15:21:26.866471388 +0100 @@ -0,0 +1,40 @@ +/* PR middle-end/111422 */ + +int a, b; +int *c = &b; +unsigned d; +signed char e; +int f = 1; + +int +foo (int k, signed char *l) +{ + if (k < 6) + return a; + l[0] = l[1] = l[k - 1] = 8; + return 0; +} + +int +bar (int k) +{ + signed char g[11]; + int h = foo (k, g); + return h; +} + +int +main () +{ + for (; b < 8; b = b + 1) + ; + int j; + int *n[8]; + j = 0; + for (;18446744073709551608ULL + bar (*c) + *c + j < 2; j++) + n[j] = &f; + for (; e <= 4; e++) + d = *n[0] == f; + if (d != 1) + __builtin_abort (); +}