From patchwork Sun Aug 5 23:31:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 175219 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]) by ozlabs.org (Postfix) with SMTP id 26E4B2C008D for ; Mon, 6 Aug 2012 09:32:23 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1344814344; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC: Subject:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=cmQrfh7xtRSEL75hdFXbo7k7/Mo=; b=X1uZIzZeBtkI7Q2 dmhrmmkkUxeeYHnJUOhMwL7Ei7XPeKbHAZlg6V+TmrJjaJsrOwdOAuJXs7hmnHri ArTJrLx+dkYQvNOQAiGkpsdd5snxUIDaXUSbMRQPzV4YgkECzxouq1szcid4CFhm bB7eQ86fSKvANSMWxeUZzm636ydc= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Vb20CckA0wOSe0pAb4GyuFVZEk12j7MBkhjtuJM0IWGH+lXnwzHKyB9y8OW0t1 FZGC1XxzO51ggnSxwrXmXyJ0iMEpUt90g7pR+rw/aVQfQhJJ/BSpdxLpZDwvL080 QHmjFMOuF1rBBvWOPAs3WtRybDJU2OrKAiLqXiyD3tZkg=; Received: (qmail 23967 invoked by alias); 5 Aug 2012 23:32:21 -0000 Received: (qmail 23958 invoked by uid 22791); 5 Aug 2012 23:32:20 -0000 X-SWARE-Spam-Status: No, hits=-3.7 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL X-Spam-Check-By: sourceware.org Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 05 Aug 2012 23:31:57 +0000 Received: from svr-orw-exc-10.mgc.mentorg.com ([147.34.98.58]) by relay1.mentorg.com with esmtp id 1SyAIe-0004UT-35 from Tom_deVries@mentor.com ; Sun, 05 Aug 2012 16:31:56 -0700 Received: from SVR-IES-FEM-01.mgc.mentorg.com ([137.202.0.104]) by SVR-ORW-EXC-10.mgc.mentorg.com with Microsoft SMTPSVC(6.0.3790.4675); Sun, 5 Aug 2012 16:31:55 -0700 Received: from [127.0.0.1] (137.202.0.76) by SVR-IES-FEM-01.mgc.mentorg.com (137.202.0.104) with Microsoft SMTP Server id 14.1.289.1; Mon, 6 Aug 2012 00:31:53 +0100 Message-ID: <501F0265.9090300@mentor.com> Date: Mon, 6 Aug 2012 01:31:49 +0200 From: Tom de Vries User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Richard Guenther CC: "gcc-patches@gcc.gnu.org" , Steven Bosscher Subject: [PATCH] Remove introduction of undefined overflow in emit_case_bit_test. 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 Richard, the code in emit_case_bit_tests currently introduces a MINUS_EXPR in signed type (without checking if signed types wrap or not), which could mean introduction of undefined overflow. This patch fixes this problem by performing the MINUS_EXPR in an unsigned type. Doing so also enables the vrp optimization for the test-case. Bootstrapped and reg-tested (ada inclusive) on x86_64. OK for trunk? Thanks, - Tom 2012-08-06 Tom de Vries * tree-switch-conversion.c (emit_case_bit_tests): Generate MINUS_EXPR in unsigned type. * gcc.dg/tree-ssa/vrp78.c: New test. Index: gcc/tree-switch-conversion.c =================================================================== --- gcc/tree-switch-conversion.c (revision 190139) +++ gcc/tree-switch-conversion.c (working copy) @@ -384,10 +384,10 @@ emit_case_bit_tests (gimple swtch, tree gsi = gsi_last_bb (switch_bb); - /* idx = (unsigned) (x - minval) */ - idx = fold_build2 (MINUS_EXPR, index_type, index_expr, - fold_convert (index_type, minval)); - idx = fold_convert (unsigned_index_type, idx); + /* idx = (unsigned)x - minval */ + idx = fold_convert (unsigned_index_type, index_expr); + idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx, + fold_convert (unsigned_index_type, minval)); idx = force_gimple_operand_gsi (&gsi, idx, /*simple=*/true, NULL_TREE, /*before=*/true, GSI_SAME_STMT); Index: gcc/testsuite/gcc.dg/tree-ssa/vrp78.c =================================================================== --- /dev/null (new file) +++ gcc/testsuite/gcc.dg/tree-ssa/vrp78.c (revision 0) @@ -0,0 +1,34 @@ +/* { dg-do link } */ +/* { dg-options "-O2" } */ + +/* Based on f3 from vrp63.c, but with switch instead of if-chain. */ + +extern void link_error (void); + +void +f3 (int s) +{ + if (s >> 3 == -2) + /* s in range [ -16, -9]. */ + ; + else + { + /* s in range ~[-16, -9], so none of the case labels can be taken. */ + switch (s) + { + case -16: + case -12: + case -9: + link_error (); + break; + default: + break; + } + } +} + +int +main () +{ + return 0; +}