From patchwork Tue Oct 11 21:34:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 680934 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 3stqyP72tyz9sBr for ; Wed, 12 Oct 2016 08:34:36 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=VMwXz4dR; dkim-atps=neutral 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:reply-to:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; q=dns; s=default; b=u/4RfdW3RQgjsJ3b i0em2+obZzXBIEfHBohT0KJyLaq5eyHaqqvaWl7uz+RQXpBm7W19UmYeR+5/m5AG lcshx/8GK4ny6ZvAIcC0B7LSf3bHTZlxh61RsK22XqyA4J6oevPlFl1UKUlvDXiO dHPe6Dwmimj5USYfQie0obUttrs= 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:reply-to:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; s=default; bh=KDayjtcSifZxEyU2285CoQ FPOLQ=; b=VMwXz4dRgwSrTJj9MaPW+zNuoT84hry+/IlVNs1rVaq6XRA0f6xLrF dGBRZc/qtnWLtWq9ksZaVF3OsoVDc13Q+V8f3MrqRLt8Vm1P2CCULCNH7/oKYhGj ViDqu1GLmbno55qJW6Td+gewH6QEIYs4UYU3x9DKBLzL4wzHYsEGw= Received: (qmail 15589 invoked by alias); 11 Oct 2016 21:34:29 -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 15574 invoked by uid 89); 11 Oct 2016 21:34:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=surprised, U*INTEGER_CST X-HELO: mail3-relais-sop.national.inria.fr Received: from mail3-relais-sop.national.inria.fr (HELO mail3-relais-sop.national.inria.fr) (192.134.164.104) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 11 Oct 2016 21:34:18 +0000 Received: from ip-118.net-89-2-234.rev.numericable.fr (HELO laptop-mg.local) ([89.2.234.118]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Oct 2016 23:34:15 +0200 Date: Tue, 11 Oct 2016 23:34:14 +0200 (CEST) From: Marc Glisse Reply-To: gcc-patches@gcc.gnu.org To: Bin Cheng cc: "gcc-patches@gcc.gnu.org" , nd Subject: Re: [PATCH GCC]Simplify (convert)(X op const) -> (convert)X op (convert)const by match&simplify In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 On Tue, 11 Oct 2016, Bin Cheng wrote: > We missed folding (convert)(X op const) -> (convert)X op (convert)const for unsigned narrowing because of reason reported at https://gcc.gnu.org/ml/gcc/2016-07/msg00126.html > This patch fixes the issue by adding new match&simplify pattern, it also adds a test case. This is the prerequisite patch for next patch adding new vectorization pattern. Some technical comments below. I am sure Jeff and/or Richi will have more to say on the approach. I am a bit surprised to see it as adding a new transformation, instead of moving an old one. +/* (convert (X op C)) -> ((convert)X op (convert)C) if it is narrowing + conversion and both types wrap when overflow. */ +(for op (plus minus) + (simplify We used to indent by a single space in this file, but I see that other transforms have made it in with double spacing, so I guess it doesn't matter. + (convert (op @0 @1)) + (if (INTEGRAL_TYPE_P (type) + && TYPE_OVERFLOW_WRAPS (type) + && TREE_CODE (@1) == INTEGER_CST You can write (convert (op @0 INTEGER_CST@1)) and skip this line. + && INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) This seems quite restrictive, TYPE_OVERFLOW_UNDEFINED should also be fine for this type. I guess you are trying to avoid saturating / trapping types? + && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))) + (op (convert @0) (convert @1))))) + /* If we have a narrowing conversion to an integral type that is fed by a BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely masks off bits outside the final type (and nothing else). */ As I understand it, C says that s is promoted to int and added to 65530, but int is not TYPE_OVERFLOW_WRAPS so your transformation doesn't apply (the test already passes without your patch). It is better to write tests for the gimple version of transformations, i.e. don't write everything as a single expression, use intermediate variables. diff --git a/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c b/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c new file mode 100644 index 0000000..aff96a9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-narrow-unsgn-opcst.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-gimple" } */ + +unsigned char foo (unsigned short s) +{ + return (unsigned char)(s + 65530); +} +/* { dg-final { scan-tree-dump-not " 65530" "gimple" } } */