From patchwork Mon Jun 10 11:20:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 1113041 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-502683-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=inria.fr Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="eQDpKks6"; 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 45MrK92ZF8z9s6w for ; Mon, 10 Jun 2019 21:20:27 +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:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=e69X+P6mafhxwfrLn7/1xovRkVXVCkrSDyXWXhIJy1rm/jtbtIdaD lR4fYz4rhsWFs7Fpjc6SwYrm92B0B/L8vcMLvq5o+2pEpUZWk8qFi6iM8gON4aAx Pz64h3fba24U7WfaoA0Khh92WvsDaDJ+oE26gL5Gh7q7qNdsbQx7tU= 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:subject:message-id:mime-version:content-type; s= default; bh=yjPgCDMnhY+stJ72aAqyHweVbVk=; b=eQDpKks6JkJeHnGPe4n7 scdCVqjt++k/r6e3cRxygO1stNHRLKfYcC+TI9+7+Ft7sURzJDJSZsR7gkVIi0bq 1h78JsPnybFsiQdIpDk3pknaaBJimem8q4iSitmh0LXpGAcDNw+6+RarbWe2QUA8 M+Fr9u/rE5UpxYc3JWGmJ4I= Received: (qmail 20584 invoked by alias); 10 Jun 2019 11:20:20 -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 20576 invoked by uid 89); 10 Jun 2019 11:20:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_NUMSUBJECT, SPF_PASS autolearn=ham version=3.3.1 spammy=H*c:HHHHH, SIGNED, H*c:MULTIPART, H*c:MIXED 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; Mon, 10 Jun 2019 11:20:18 +0000 Received: from grove.saclay.inria.fr ([193.55.177.244]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 10 Jun 2019 13:20:15 +0200 Date: Mon, 10 Jun 2019 13:20:09 +0200 (CEST) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: Allow conversions in X/[ex]4 < Y/[ex]4 Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, if we compare for instance size() and capacity() for a std::vector, we get this kind of pattern. I started with just nop conversions and got a bit carried away handling all those cases. Bootstrap+regtest on x86_64-pc-linux-gnu. 2019-06-11 Marc Glisse gcc/ * match.pd (X/[ex]4 1 && (wi::to_wide (@2) == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1)) (with { tree stype = signed_type_for (TREE_TYPE (@0)); } (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ (for cmp (simple_comparison) (simplify - (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2)) - (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) - (cmp @0 @1) + (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2))) + (if (element_precision (TREE_TYPE (@3)) >= element_precision (TREE_TYPE (@0)) + && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) - (cmp @1 @0))))) + (if (!TYPE_UNSIGNED (TREE_TYPE (@3))) + (cmp @1 @0) + (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1)) + (with + { + tree utype = unsigned_type_for (TREE_TYPE (@0)); + } + (cmp (convert:utype @1) (convert:utype @0))))) + (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2)))) + (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3))) + (cmp @0 @1) + (with + { + tree utype = unsigned_type_for (TREE_TYPE (@0)); + } + (cmp (convert:utype @0) (convert:utype @1))))))))) /* X / C1 op C2 into a simple range test. */ (for cmp (simple_comparison) (simplify (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && integer_nonzerop (@1) && !TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)) (with { tree lo, hi; bool neg_overflow; Index: gcc/testsuite/gcc.dg/tree-ssa/cmpexactdiv-5.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/cmpexactdiv-5.c (nonexistent) +++ gcc/testsuite/gcc.dg/tree-ssa/cmpexactdiv-5.c (working copy) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +int f(int *a, int *b, int *c){ + if(sizeof(__SIZE_TYPE__) != sizeof(__PTRDIFF_TYPE__)) return 2; + __SIZE_TYPE__ s = b - a; + __SIZE_TYPE__ t = c - a; + return s < t; +} + +/* { dg-final { scan-tree-dump-not "exact_div_expr" "optimized" } } */