From patchwork Fri Apr 29 09:10:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 53345 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 CB5483857344 for ; Fri, 29 Apr 2022 09:12:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CB5483857344 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651223528; bh=ZyMZOLgQuPovDoAv3FPPSEee+/sHDEWbOIVR/XO7Uvg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=dw6G5W885WHFXnLyo6lVSjVVlHxTOytu/wtdx0H+LymQRkSe6eh494VGuumAoX72U PHPR2aKAz1aGmxtwPwy96rNoTfz+rzAfHip/00YJfWfCYkOaie3pRv+5QsrhU7cyXG jwn6b5UIwzJPZT4n0FwQnmO/SXnVce1LaWXTyOq4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id BA1823857376 for ; Fri, 29 Apr 2022 09:10:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BA1823857376 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 76928210E1 for ; Fri, 29 Apr 2022 09:10:15 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 62A1E13446 for ; Fri, 29 Apr 2022 09:10:15 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id WPTiFnera2LreAAAMHmgww (envelope-from ) for ; Fri, 29 Apr 2022 09:10:15 +0000 Date: Fri, 29 Apr 2022 11:10:14 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/105431 - another overflow in powi handling MIME-Version: 1.0 Message-Id: <20220429091015.62A1E13446@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This avoids undefined signed overflow when calling powi_as_mults_1. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. 2022-04-29 Richard Biener PR tree-optimization/105431 * tree-ssa-math-opts.cc (powi_as_mults_1): Make n unsigned. (powi_as_mults): Use absu_hwi. (gimple_expand_builtin_powi): Remove now pointless n != -n check. --- gcc/tree-ssa-math-opts.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc index 7555793948e..232e903b0d2 100644 --- a/gcc/tree-ssa-math-opts.cc +++ b/gcc/tree-ssa-math-opts.cc @@ -1495,7 +1495,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; @@ -1548,7 +1548,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; @@ -1572,11 +1572,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;