From patchwork Tue Jul 24 18:11:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 948723 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-482219-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="smgGkO79"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41Zmdv2VdQz9s0n for ; Wed, 25 Jul 2018 04:11:47 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=OxSaPGG9xFHQMldeqGoI3GX2SsLss8kf/in/NcJVoDwJRxfyIYUbP tsyvrsAaXcMBeu6toUXsud2i1bJHqTi5Ufb6xz3mOFxL2FWlHz7ljABsvFvjfLyu nTjxls5kbpUh3Uh5dNX75Y3aePSRWV+3TF5J4dRkeDlbahpYc7mNt8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=yl8QB+cAzbbc9G75RiZlUikVaks=; b=smgGkO79jfPSIbpuHmij bKwPIYbmHUZAJutHCYy9wKJM2665qX9uSaxBWJz1NdX6i+9MExnuBt0w4RJ9PzjL viMjXG6W1CKnPf7Ng2PrNkOdiSz8PCWp3poHMrIB4WDm0yh4rJIwKZkDU1bI5bs/ n0YMU74k+z0UT5kBEtydNDM= Received: (qmail 100406 invoked by alias); 24 Jul 2018 18:11:40 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 100379 invoked by uid 89); 24 Jul 2018 18:11:39 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: foss.arm.com Received: from usa-sjc-mx-foss1.foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 24 Jul 2018 18:11:37 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8DD377A9 for ; Tue, 24 Jul 2018 11:11:36 -0700 (PDT) Received: from localhost (unknown [10.32.99.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 156C13F575 for ; Tue, 24 Jul 2018 11:11:35 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Fix ceil_log2(0) (PR 86644) Date: Tue, 24 Jul 2018 19:11:34 +0100 Message-ID: <876014ld0p.fsf@arm.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 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. Tested on aarch64-linux-gnu, aarch64_be-elf and x86_64-linux-gnu. OK to install? Richard 2018-07-24 Richard Sandiford gcc/ * hwint.c (ceil_log2): Fix comment. Return 0 for 0. Index: gcc/hwint.c =================================================================== --- gcc/hwint.c 2018-05-02 08:38:14.433364094 +0100 +++ gcc/hwint.c 2018-07-24 19:09:03.522774662 +0100 @@ -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,