From patchwork Mon Apr 8 14:18:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= X-Patchwork-Id: 1920870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (4096-bit key; secure) header.d=kolabnow.com header.i=@kolabnow.com header.a=rsa-sha256 header.s=dkim20160331 header.b=h2H1Ss6t; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VCrn3244sz1yYf for ; Tue, 9 Apr 2024 00:19:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B042F3861001 for ; Mon, 8 Apr 2024 14:19:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx.kolabnow.com (mx.kolabnow.com [212.103.80.155]) by sourceware.org (Postfix) with ESMTPS id 8A9723858D28 for ; Mon, 8 Apr 2024 14:18:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A9723858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=lambda.is Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=lambda.is ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8A9723858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.103.80.155 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712585919; cv=none; b=obyyow0Ds3/VjvG5ac2asjS3as4KbuipU2+DFcToymMu6SeKFBxTCKeoD2YK+kX4TvYp74yjd88y1Be5hDvAgT/D2qyNW3lZh13JeAfNZu2xNll52vVAX3+gsTZaPgwbelJOtHbraPN/na6mQ1Kqh/1noiMlCSmnLouqQr3EYLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712585919; c=relaxed/simple; bh=TMsNes9yFcADkkjXLwKiEca/denuuscAeXU+EhZam9I=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Wa4Rysu15OvxzSFLmAzS1wWZkMSfYpm6UGUMx44/W+q4qVHAYuHq0nPSxa4Nzupq/+FoDmEX6WZR7z6p3RXzK6Ta5reddh9g/0XpfHQzyv+VL3jin6WqvR28dit326sSzS10yK85n+HMU3Kkf2VpOIM0IwQsP1w+GVAnWDeEpfU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (unknown [127.0.0.1]) by mx.kolabnow.com (Postfix) with ESMTP id 2EBBB2096DEC; Mon, 8 Apr 2024 16:18:33 +0200 (CEST) Authentication-Results: ext-mx-out011.mykolab.com (amavis); dkim=pass (4096-bit key) reason="pass (just generated, assumed good)" header.d=kolabnow.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1712585912; x=1714400313; bh=qZmQNrmvIIjH0D0pFc17SH3AODwG0gG0S03/5dlLy5M=; b=h2H1Ss6tVdF8 bi8CUWUQHMrqM4Q5n4SsNRs26T6JU3qxR/X9cucP/euNoRVgrq+xP3WKzJB6wTh2 pIHsd22lA/nlRYpJJlYZ9byfekd0zp31shmfHlai6zbcI3A6Dyu0Dai46jQvAt8L vaiz6pxwRcvD6bzttarmvjbO8Ow3N68lpSq7ZbPM3VVzcvXQCsqjkvSdW/bv+ZJL jDyBytNHRalBj5AKQxi8Qmu+Nnl9gw9vSi94mApNvb7r1lTP6z7vG3yYTE0Tkwsf 51cC4FxiR+jYm3m1fwU7TB7n5H6VqOFUx3Rmuosn8cOhMEnm8OYYSGVNWs7hO4/S 9s+ZdLgrS2aPuWqHvWi360ign31rGJcD3j7Hrxry6w8LixTs9LyyjEXPgqPslJgP 3wzFw+x2FAY+c9TtXioZXFPzc/K5t5e1ZQYxKX+nB7/xyV/hTFbVqt3NmNlc5JHt jWI+dPEMxkLhqMA2Aec6H/0lrs+oDltr2lQr2f3KTboYOsVsdqwy3BZE1jBW7Yo0 SrWVoOcObFVDQ+5hvZ3nNxotqVXzoeg7F9oxmVLsQJC3HlaKgd43//6GHgHZOOJR gCUxJn8ph+dNOCLP/JvaTk5rWloo3OghfGcXAvq5P63cTAK9IVWo8I2JRi1uXCfR 770pdmwEK5nGjyHp8b1gEKCrh3GMi+I= X-Virus-Scanned: amavis at mykolab.com X-Spam-Score: -1 X-Spam-Level: X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out011.mykolab.com [127.0.0.1]) (amavis, port 10024) with ESMTP id 5furccgjumXo; Mon, 8 Apr 2024 16:18:32 +0200 (CEST) Received: from int-mx009.mykolab.com (unknown [10.9.13.9]) by mx.kolabnow.com (Postfix) with ESMTPS id E78292096DEB; Mon, 8 Apr 2024 16:18:31 +0200 (CEST) Received: from ext-subm010.mykolab.com (unknown [10.9.6.10]) by int-mx009.mykolab.com (Postfix) with ESMTPS id B23742114755; Mon, 8 Apr 2024 16:18:31 +0200 (CEST) From: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= To: gcc-patches@gcc.gnu.org Cc: hjl.tools@gmail.com, rguenther@suse.de, hubicka@ucw.cz, sam@gentoo.org, =?utf-8?q?J=C3=B8rgen_Kvalsvik?= Subject: [PATCH 2/2] Generate constant at start of loop, without UB Date: Mon, 8 Apr 2024 16:18:15 +0200 Message-Id: <20240408141815.127984-3-j@lambda.is> In-Reply-To: <20240408141815.127984-1-j@lambda.is> References: <20240408141815.127984-1-j@lambda.is> MIME-Version: 1.0 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Generating the constants used for recording the edges taken for condition coverage would trigger undefined behavior when an expression had exactly 64 (== sizeof (1ULL)) conditions, as it would generate the constant for the next iteration at the end of the loop body, even if there was never a next iteration. By moving the check and constant generation to the top of the loop and hoisting the increment flag there is no opportunity for UB. PR 114627 gcc/ChangeLog: * tree-profile.cc (instrument_decisions): Generate constant at the start of loop. --- gcc/tree-profile.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/gcc/tree-profile.cc b/gcc/tree-profile.cc index 33ff550a7bc..e58f5c83472 100644 --- a/gcc/tree-profile.cc +++ b/gcc/tree-profile.cc @@ -1049,6 +1049,7 @@ instrument_decisions (array_slice expr, size_t condno, zerocounter[2] = zero; unsigned xi = 0; + bool increment = false; tree rhs = build_int_cst (gcov_type_node, 1ULL << xi); for (basic_block current : expr) { @@ -1057,7 +1058,14 @@ instrument_decisions (array_slice expr, size_t condno, candidates.safe_push (zerocounter); counters prev = resolve_counters (candidates); - int increment = 0; + if (increment) + { + xi += 1; + gcc_checking_assert (xi < sizeof (uint64_t) * BITS_PER_UNIT); + rhs = build_int_cst (gcov_type_node, 1ULL << xi); + increment = false; + } + for (edge e : current->succs) { counters next = prev; @@ -1072,7 +1080,7 @@ instrument_decisions (array_slice expr, size_t condno, tree m = build_int_cst (gcov_type_node, masks[2*xi + k]); next[2] = emit_bitwise_op (e, prev[2], BIT_IOR_EXPR, m); } - increment = 1; + increment = true; } else if (e->flags & EDGE_COMPLEX) { @@ -1085,11 +1093,13 @@ instrument_decisions (array_slice expr, size_t condno, } table.get_or_insert (e->dest).safe_push (next); } - xi += increment; - if (increment) - rhs = build_int_cst (gcov_type_node, 1ULL << xi); } + /* Since this is also the return value, the number of conditions, make sure + to include the increment of the last basic block. */ + if (increment) + xi += 1; + gcc_assert (xi == bitmap_count_bits (core)); const tree relaxed = build_int_cst (integer_type_node, MEMMODEL_RELAXED);