From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7901) id D84F2385841C; Tue, 9 Apr 2024 07:58:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D84F2385841C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712649484; bh=j6+nnj1KNCaNsomkLfWFTashH+/d+sdHej8TriRnWt0=; h=From:To:Subject:Date:From; b=n9i6zGSAsRdt+v8cuOKHITKg/isgpQRUGVyVpkRscfCf+NmgtuXqISMf6NE0KAxq0 N/D2fZ6kvZEWf8rQx2IyVFrTUiC8Z7lpb9r+pQgs4Iq7WD3pee+e9pdSo6z8OwLBaY c/Kj5qb7NSXOHLPKRYktHd4/gg6Z/HElUpjwxAjE= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: J?rgen Kvalsvik To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9864] Generate constant at start of loop, without UB X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?J=C3=B8rgen_Kvalsvik?= X-Git-Refname: refs/heads/master X-Git-Oldrev: 2daeb89d6f025d6daf7e560575863b3280120be8 X-Git-Newrev: a2447556a5405d2cde20afc134b90cd1d199ce04 Message-Id: <20240409075804.D84F2385841C@sourceware.org> Date: Tue, 9 Apr 2024 07:58:04 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a2447556a5405d2cde20afc134b90cd1d199ce04 commit r14-9864-ga2447556a5405d2cde20afc134b90cd1d199ce04 Author: Jørgen Kvalsvik Date: Mon Apr 8 15:19:55 2024 +0200 Generate constant at start of loop, without UB 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 middle-end/114627 gcc/ChangeLog: * tree-profile.cc (instrument_decisions): Generate constant at the start of loop. Diff: --- 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 f9d727b3111..b85111624fe 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);