From e8dce850a7ffcffff4690f62f85e0ed4ede4d82a Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Wed, 25 Jul 2018 08:41:35 +0000 Subject: [PATCH] Fix ceil_log2(0) (PR 86644) This PR shows a pathological case in which we try SLP vectorisation on dead code. We record that 0 bits of the result are enough to satisfy all users (which is true), and that led to precision being 0 in: static unsigned int vect_element_precision (unsigned int precision) { precision = 1 << ceil_log2 (precision); return MAX (precision, BITS_PER_UNIT); } ceil_log2 (0) returned 64 rather than 0, leading to 1 << 64, which is UB. 2018-07-25 Richard Sandiford gcc/ * hwint.c (ceil_log2): Fix comment. Return 0 for 0. From-SVN: r262961 --- gcc/ChangeLog | 4 ++++ gcc/hwint.c | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3a2780455de..bb9f50670ad 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2018-07-25 Richard Sandiford + + * hwint.c (ceil_log2): Fix comment. Return 0 for 0. + 2018-07-25 Martin Liska PR middle-end/86645 diff --git a/gcc/hwint.c b/gcc/hwint.c index 7258df71ed6..b10b5e409b9 100644 --- a/gcc/hwint.c +++ b/gcc/hwint.c @@ -60,12 +60,12 @@ floor_log2 (unsigned HOST_WIDE_INT x) return t; } -/* Given X, an unsigned number, return the largest Y such that 2**Y >= X. */ +/* Given X, an unsigned number, return the least Y such that 2**Y >= X. */ int ceil_log2 (unsigned HOST_WIDE_INT x) { - return floor_log2 (x - 1) + 1; + return x == 0 ? 0 : floor_log2 (x - 1) + 1; } /* Return the logarithm of X, base 2, considering X unsigned, -- 2.30.2