From patchwork Tue Mar 25 07:28:00 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 333301 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 4361914008E for ; Tue, 25 Mar 2014 18:39:51 +1100 (EST) 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; q=dns; s=default; b=F9/VenpHHlQ/cYBxsMop+YW4NURuX HOBeNgKVdIiLpmGk9UuCA+umDtaeExly/SGJM9IzQVDC1l9109p8uMIUe+F8qeQ5 AnAGplC5JPWjt66nyWos3JES8gwDHb1ukC0MK5X69qLMBc5L7Ca4OgoGsjQa8Loj zk/EFwgX5TO9CM= 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; s=default; bh=rO4T0hHSR/MvFpgc+CIQeOQ7MOg=; b=ACR 4w/yLJrEjQaOk3u0CMxjJe9lQJUj/mAjRxV6x8JCiJE4nziWu9YRULJBeicQPai7 6N8YRMpVdYhgOntUk1oHX/DmVQcK9TyIU5eWuDmxZ2lIr4Qa3t2fTWv4OTobRa9v yAc2bCkhwXfoqvlvZlB7/T0DKS1/+D+yFOrAHJkM= Received: (qmail 23829 invoked by alias); 25 Mar 2014 07:39:35 -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 23759 invoked by uid 89); 25 Mar 2014 07:39:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.8 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Mar 2014 07:39:33 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2P7dTZF021814 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 25 Mar 2014 03:39:30 -0400 Received: from tucnak.zalov.cz (ovpn-116-51.ams2.redhat.com [10.36.116.51]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s2P7dRnS021162 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 25 Mar 2014 03:39:28 -0400 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.14.8/8.14.7) with ESMTP id s2P7S1ub017778; Tue, 25 Mar 2014 08:28:01 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.14.8/8.14.8/Submit) id s2P7S08t017777; Tue, 25 Mar 2014 08:28:00 +0100 Date: Tue, 25 Mar 2014 08:28:00 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix folding of UBSAN_CHECK_SUB (x, 0) etc. Message-ID: <20140325072800.GL1817@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes Hi! While working on previous patch, I've noticed a severe omission in the -fsanitize=signed-integer-overflow support. Nothing ever folds addition/subtraction of 0 and multiplication by [0,1], none of these may ever overflow and thus we can fold them to normal operations and let those be folded as normal operations. Bootstrapped/regtested on x86_64-linux and i686-linux, tested with --with-build-config=bootstrap-ubsan bootstrap on i686-linux, ok for trunk? 2014-03-25 Jakub Jelinek * tree-vrp.c (simplify_internal_call_using_ranges): If only one range is range_int_cst_p, but not both, at least optimize addition/subtraction of 0 and multiplication by 0 or 1. Jakub --- gcc/tree-vrp.c.jj 2014-03-12 10:14:18.000000000 +0100 +++ gcc/tree-vrp.c 2014-03-24 16:14:50.822674164 +0100 @@ -9336,31 +9336,58 @@ simplify_internal_call_using_ranges (gim else if (TREE_CODE (op0) == INTEGER_CST) set_value_range_to_value (&vr0, op0, NULL); else - return false; + set_value_range_to_varying (&vr0); if (TREE_CODE (op1) == SSA_NAME) vr1 = *get_value_range (op1); else if (TREE_CODE (op1) == INTEGER_CST) set_value_range_to_value (&vr1, op1, NULL); else - return false; + set_value_range_to_varying (&vr1); - if (!range_int_cst_p (&vr0) || !range_int_cst_p (&vr1)) - return false; - - tree r1 = int_const_binop (subcode, vr0.min, vr1.min); - tree r2 = int_const_binop (subcode, vr0.max, vr1.max); - if (r1 == NULL_TREE || TREE_OVERFLOW (r1) - || r2 == NULL_TREE || TREE_OVERFLOW (r2)) - return false; - if (subcode == MULT_EXPR) + if (!range_int_cst_p (&vr0)) + { + /* If one range is VR_ANTI_RANGE, VR_VARYING etc., + optimize at least x = y + 0; x = y - 0; x = y * 0; + and x = y * 1; which never overflow. */ + if (!range_int_cst_p (&vr1)) + return false; + if (tree_int_cst_sgn (vr1.min) == -1) + return false; + if (compare_tree_int (vr1.max, subcode == MULT_EXPR) == 1) + return false; + } + else if (!range_int_cst_p (&vr1)) + { + /* If one range is VR_ANTI_RANGE, VR_VARYING etc., + optimize at least x = 0 + y; x = 0 * y; and x = 1 * y; + which never overflow. */ + if (subcode == MINUS_EXPR) + return false; + if (!range_int_cst_p (&vr0)) + return false; + if (tree_int_cst_sgn (vr0.min) == -1) + return false; + if (compare_tree_int (vr0.max, subcode == MULT_EXPR) == 1) + return false; + } + else { - tree r3 = int_const_binop (subcode, vr0.min, vr1.max); - tree r4 = int_const_binop (subcode, vr0.max, vr1.min); - if (r3 == NULL_TREE || TREE_OVERFLOW (r3) - || r4 == NULL_TREE || TREE_OVERFLOW (r4)) + tree r1 = int_const_binop (subcode, vr0.min, vr1.min); + tree r2 = int_const_binop (subcode, vr0.max, vr1.max); + if (r1 == NULL_TREE || TREE_OVERFLOW (r1) + || r2 == NULL_TREE || TREE_OVERFLOW (r2)) return false; + if (subcode == MULT_EXPR) + { + tree r3 = int_const_binop (subcode, vr0.min, vr1.max); + tree r4 = int_const_binop (subcode, vr0.max, vr1.min); + if (r3 == NULL_TREE || TREE_OVERFLOW (r3) + || r4 == NULL_TREE || TREE_OVERFLOW (r4)) + return false; + } } + gimple g = gimple_build_assign_with_ops (subcode, gimple_call_lhs (stmt), op0, op1); gsi_replace (gsi, g, false);