From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 88C8A383E81F; Thu, 19 May 2022 14:03:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88C8A383E81F MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10020] tree-optimization/105431 - another overflow in powi handling X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 3e6ae76385c60ad1ef63270ba969729a40ca1245 X-Git-Newrev: 16a2fcfe247364088ca9d6d058d870bdfa28b345 Message-Id: <20220519140335.88C8A383E81F@sourceware.org> Date: Thu, 19 May 2022 14:03:35 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 May 2022 14:03:35 -0000 https://gcc.gnu.org/g:16a2fcfe247364088ca9d6d058d870bdfa28b345 commit r11-10020-g16a2fcfe247364088ca9d6d058d870bdfa28b345 Author: Richard Biener Date: Fri Apr 29 08:45:48 2022 +0200 tree-optimization/105431 - another overflow in powi handling This avoids undefined signed overflow when calling powi_as_mults_1. 2022-04-29 Richard Biener PR tree-optimization/105431 * tree-ssa-math-opts.c (powi_as_mults_1): Make n unsigned. (powi_as_mults): Use absu_hwi. (gimple_expand_builtin_powi): Remove now pointless n != -n check. (cherry picked from commit 44b09adb9bad99dd7e3017c5ecefed7f7c9a1590) Diff: --- gcc/tree-ssa-math-opts.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c index 8b5d4aa4f4a..adeb70fd635 100644 --- a/gcc/tree-ssa-math-opts.c +++ b/gcc/tree-ssa-math-opts.c @@ -1492,7 +1492,7 @@ powi_cost (HOST_WIDE_INT n) static tree powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type, - HOST_WIDE_INT n, tree *cache) + unsigned HOST_WIDE_INT n, tree *cache) { tree op0, op1, ssa_target; unsigned HOST_WIDE_INT digit; @@ -1545,7 +1545,7 @@ powi_as_mults (gimple_stmt_iterator *gsi, location_t loc, memset (cache, 0, sizeof (cache)); cache[1] = arg0; - result = powi_as_mults_1 (gsi, loc, type, (n < 0) ? -n : n, cache); + result = powi_as_mults_1 (gsi, loc, type, absu_hwi (n), cache); if (n >= 0) return result; @@ -1569,11 +1569,9 @@ static tree gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc, tree arg0, HOST_WIDE_INT n) { - /* Avoid largest negative number. */ - if (n != -n - && ((n >= -1 && n <= 2) - || (optimize_function_for_speed_p (cfun) - && powi_cost (n) <= POWI_MAX_MULTS))) + if ((n >= -1 && n <= 2) + || (optimize_function_for_speed_p (cfun) + && powi_cost (n) <= POWI_MAX_MULTS)) return powi_as_mults (gsi, loc, arg0, n); return NULL_TREE;