From patchwork Tue Nov 19 08:42:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1197288 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-513990-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="TIkKcuee"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="PkVtLbZS"; 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 47HK813mwmz9s4Y for ; Tue, 19 Nov 2019 19:42:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type:content-transfer-encoding; q=dns; s=default; b=YIY SAUycvtLDBi8Zd0waQyNSOxIyo4vwdhaxKuikwi5p/rWynGDhzZE0lcFem/BxzRL y6G4aEynVHvWcBoIx8l8bgcBrRQj7+3nrA/+McGP5w0a508mvt5gSktR4Q8/ClPA rq4qLs5HzlTgPi/7kSkRo7L3hXNwVgKO29Q/HFKw= 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:date :from:to:cc:subject:message-id:reply-to:mime-version :content-type:content-transfer-encoding; s=default; bh=1hBtl6QvI h0w1dwKJlLSL7dcJZo=; b=TIkKcueetZJefCwH3LsxfPwdg2rHVD18OYW4/7ivE 5rBaIR+MdRPTDcScNt0Hp7nUAKlIeBdaxmOG2lWH5CAtGaFwQYCh8LIOzdVUDAWL E25Ze4Fu0tOOUrFGT/ZwX2W5ZHg8YVtUwOnexoRu2w4PDd4UD9IRSOOB1Ij496hU uc= Received: (qmail 66022 invoked by alias); 19 Nov 2019 08:42:17 -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 66012 invoked by uid 89); 19 Nov 2019 08:42:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 19 Nov 2019 08:42:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574152933; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6ddsJXiDKxYUvSpFlzov90Ejw3rWH52RadSkHAtHQiE=; b=PkVtLbZSu4EV7Sems5tTzqUzPEd806RxSYP0CgDrUxXj+pvFZ+PX2CikISUdPH6zNVLjwN HWxvRexywIpTC0fRkVFliCqxxjebJixmAASOgwcAkAJIeWiPRAPC7Pw3Y8JRzlZBd9dy/3 SrwJ6UKN/efwkHpUbuJ7shV2EOUy6Ho= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-257-Y89dvR51Pz6wb5a72l1J1g-1; Tue, 19 Nov 2019 03:42:10 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 28DC01005500; Tue, 19 Nov 2019 08:42:09 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-21.ams2.redhat.com [10.36.116.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B706B627DE; Tue, 19 Nov 2019 08:42:08 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id xAJ8g6Fr019738; Tue, 19 Nov 2019 09:42:06 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id xAJ8g37e019737; Tue, 19 Nov 2019 09:42:03 +0100 Date: Tue, 19 Nov 2019 09:42:03 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix up __builtin_mul_overflow for signed * signed -> unsigned (PR middle-end/91450) Message-ID: <20191119084203.GB4650@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 User-Agent: Mutt/1.11.3 (2019-02-01) X-Mimecast-Spam-Score: 0 Content-Disposition: inline X-IsSubscribed: yes Hi! This case is correctly described in the comment in expand_mul_overflow: s1 * s2 -> ur t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1) t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2) res = t1 * t2 ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true) */ where if one of the operands is negative and the other non-negative, the overflow is whenever both operands are non-zero, so s1 && s2, but unfortunately the code has been implementing s1 & s2 instead, which e.g. for the -2 and 1 where (-2 & 1) == 0, but (-2 && 1) != 0 is incorrect. The second hunk is when we know from VRP that one of the operand (and which one) is negative and which one is non-negative, obviously the negative one can't be zero, so we can just compare the non-negative one against zero. The third hunk is when we don't know this precisely from VRP. In that case we used to do essentially if ((s1 & s2) == 0) goto main_label; ovf = 1; main_label: ovf |= __builtin_mul_overflow ((unsigned) s1, (unsigned) s2, &r); (the fallthrough is ok, because mul_overflow when one of the operands is zero will just compute zero), this patch changes it to: if (s1 == 0) goto main_label; if (s2 == 0) goto main_label; ovf = 1; main_label: ovf |= __builtin_mul_overflow ((unsigned) s1, (unsigned) s2, &r); but as an optimization, if we know from VRP that one of the operands is negative, we don't bother comparing that operand to 0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and release branches? 2019-11-19 Jakub Jelinek PR middle-end/91450 * internal-fn.c (expand_mul_overflow): For s1 * s2 -> ur, if one operand is negative and one non-negative, compare the non-negative one against 0 rather than comparing s1 & s2 against 0. Otherwise, don't compare (s1 & s2) == 0, but compare separately both s1 == 0 and s2 == 0, unless one of them is known to be negative. Remove tem2 variable, use tem where tem2 has been used before. * gcc.c-torture/execute/pr91450-1.c: New test. * gcc.c-torture/execute/pr91450-2.c: New test. Jakub --- gcc/internal-fn.c.jj 2019-11-08 11:33:48.000000000 +0100 +++ gcc/internal-fn.c 2019-11-18 14:41:48.247381649 +0100 @@ -1408,7 +1408,7 @@ expand_mul_overflow (location_t loc, tre /* s1 * s2 -> ur */ if (!uns0_p && !uns1_p && unsr_p) { - rtx tem, tem2; + rtx tem; switch (pos_neg0 | pos_neg1) { case 1: /* Both operands known to be non-negative. */ @@ -1438,10 +1438,8 @@ expand_mul_overflow (location_t loc, tre ops.op2 = NULL_TREE; ops.location = loc; res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); - tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false, - OPTAB_LIB_WIDEN); - do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, - NULL_RTX, NULL, done_label, + do_compare_rtx_and_jump (pos_neg0 == 1 ? op0 : op1, const0_rtx, EQ, + true, mode, NULL_RTX, NULL, done_label, profile_probability::very_likely ()); goto do_error_label; } @@ -1472,16 +1470,23 @@ expand_mul_overflow (location_t loc, tre arg1 = error_mark_node; emit_jump (do_main_label); emit_label (after_negate_label); - tem2 = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false, - OPTAB_LIB_WIDEN); - do_compare_rtx_and_jump (tem2, const0_rtx, GE, false, mode, NULL_RTX, - NULL, do_main_label, profile_probability::very_likely ()); + tem = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false, + OPTAB_LIB_WIDEN); + do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX, + NULL, do_main_label, + profile_probability::very_likely ()); /* One argument is negative here, the other positive. This overflows always, unless one of the arguments is 0. But if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1 is, thus we can keep do_main code oring in overflow as is. */ - do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, NULL_RTX, - NULL, do_main_label, profile_probability::very_likely ()); + if (pos_neg0 != 2) + do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX, + NULL, do_main_label, + profile_probability::very_unlikely ()); + if (pos_neg1 != 2) + do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX, + NULL, do_main_label, + profile_probability::very_unlikely ()); expand_arith_set_overflow (lhs, target); emit_label (do_main_label); goto do_main; --- gcc/testsuite/gcc.c-torture/execute/pr91450-1.c.jj 2019-11-18 14:32:04.156108293 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr91450-1.c 2019-11-18 14:39:28.693466446 +0100 @@ -0,0 +1,88 @@ +/* PR middle-end/91450 */ + +__attribute__((noipa)) unsigned long long +foo (int a, int b) +{ + unsigned long long r; + if (!__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + return r; +} + +__attribute__((noipa)) unsigned long long +bar (int a, int b) +{ + unsigned long long r; + if (a >= 0) + return 0; + if (!__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + return r; +} + +__attribute__((noipa)) unsigned long long +baz (int a, int b) +{ + unsigned long long r; + if (b >= 0) + return 0; + if (!__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + return r; +} + +__attribute__((noipa)) unsigned long long +qux (int a, int b) +{ + unsigned long long r; + if (a >= 0) + return 0; + if (b < 0) + return 0; + if (!__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + return r; +} + +__attribute__((noipa)) unsigned long long +quux (int a, int b) +{ + unsigned long long r; + if (a < 0) + return 0; + if (b >= 0) + return 0; + if (!__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + return r; +} + +int +main () +{ + if (foo (-4, 2) != -8ULL) + __builtin_abort (); + if (foo (2, -4) != -8ULL) + __builtin_abort (); + if (bar (-4, 2) != -8ULL) + __builtin_abort (); + if (baz (2, -4) != -8ULL) + __builtin_abort (); + if (qux (-4, 2) != -8ULL) + __builtin_abort (); + if (quux (2, -4) != -8ULL) + __builtin_abort (); + if (foo (-2, 1) != -2ULL) + __builtin_abort (); + if (foo (1, -2) != -2ULL) + __builtin_abort (); + if (bar (-2, 1) != -2ULL) + __builtin_abort (); + if (baz (1, -2) != -2ULL) + __builtin_abort (); + if (qux (-2, 1) != -2ULL) + __builtin_abort (); + if (quux (1, -2) != -2ULL) + __builtin_abort (); + return 0; +} --- gcc/testsuite/gcc.c-torture/execute/pr91450-2.c.jj 2019-11-18 14:32:12.204987999 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr91450-2.c 2019-11-18 14:33:37.881707491 +0100 @@ -0,0 +1,76 @@ +/* PR middle-end/91450 */ + +__attribute__((noipa)) void +foo (int a, int b) +{ + unsigned long long r; + if (__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + if (r != 0) + __builtin_abort (); +} + +__attribute__((noipa)) void +bar (int a, int b) +{ + unsigned long long r; + if (a >= 0) + return; + if (__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + if (r != 0) + __builtin_abort (); +} + +__attribute__((noipa)) void +baz (int a, int b) +{ + unsigned long long r; + if (b >= 0) + return; + if (__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + if (r != 0) + __builtin_abort (); +} + +__attribute__((noipa)) void +qux (int a, int b) +{ + unsigned long long r; + if (a >= 0) + return; + if (b < 0) + return; + if (__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + if (r != 0) + __builtin_abort (); +} + +__attribute__((noipa)) void +quux (int a, int b) +{ + unsigned long long r; + if (a < 0) + return; + if (b >= 0) + return; + if (__builtin_mul_overflow (a, b, &r)) + __builtin_abort (); + if (r != 0) + __builtin_abort (); +} + +int +main () +{ + foo (-4, 0); + foo (0, -4); + foo (0, 0); + bar (-4, 0); + baz (0, -4); + qux (-4, 0); + quux (0, -4); + return 0; +}