From patchwork Mon Oct 26 10:00:10 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 535802 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 78C0E1401F6 for ; Mon, 26 Oct 2015 21:00:55 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=HGRerfsW; 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=omVXc9PSMw0gegNO qlrJnp1pn+IlhZwtodubWTFDDPZVNFSXXK97pV4Zn9rqbdTCsd6de6yToCT6SgD5 7toDg6+pYIYhEMB2/d4rI7dWwLlQ1leV51mR7/K72UbW+jlXbBN/H06cHM/F7v7o U2ByFvZGFkHBZqTxh7zoBQysjKE= 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=6cvpzGGDd5mPvXIZdXkfWh lB8QA=; b=HGRerfsW6AS/hqQNccXkVukfodzirnoIAQB2HPXQSLggs+18TZqQcb fUliBjY0zj8gKci6IhVUMwPr4I9LSa976EOO0WMxsN2SqedqpJsB18+rxFejiZQ1 cWIio+5V5PgcTI9cs6kMr/BNo99CPhUyQjAkvmb/L1KNingFRS2Yg= Received: (qmail 52931 invoked by alias); 26 Oct 2015 10:00:39 -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 49742 invoked by uid 89); 26 Oct 2015 10:00:31 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (207.82.80.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Oct 2015 10:00:20 +0000 Received: from cam-owa2.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-20-8b9o-3V1Sp-8eeeClmW_oA-1; Mon, 26 Oct 2015 10:00:10 +0000 Received: from localhost ([10.1.2.79]) by cam-owa2.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Mon, 26 Oct 2015 10:00:10 +0000 From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Move min(max...) and max(min...) folds to match.pd Date: Mon, 26 Oct 2015 10:00:10 +0000 Message-ID: <87twpeuez9.fsf@e105548-lin.cambridge.arm.com> User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 X-MC-Unique: 8b9o-3V1Sp-8eeeClmW_oA-1 This handles both integer and floating-point arguments. It's needed for the follow-on patch to move fmin and fmax to match.pd. Tested on x86_64-linux-gnu, aarch64-linux-gnu and arm-linux-gnueabi. OK to install? Thanks, Richard gcc/ * fold-const.c (fold_minmax): Delete. (fold_binary_loc): Don't call it. * match.pd: Add rules previously handled by fold_minmax. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index ffad732..e8ff1de 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8073,49 +8073,6 @@ fold_truth_andor (location_t loc, enum tree_code code, tree type, return NULL_TREE; } -/* Fold a binary expression of code CODE and type TYPE with operands - OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination. - Return the folded expression if folding is successful. Otherwise, - return NULL_TREE. */ - -static tree -fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1) -{ - enum tree_code compl_code; - - if (code == MIN_EXPR) - compl_code = MAX_EXPR; - else if (code == MAX_EXPR) - compl_code = MIN_EXPR; - else - gcc_unreachable (); - - /* MIN (MAX (a, b), b) == b. */ - if (TREE_CODE (op0) == compl_code - && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0)) - return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0)); - - /* MIN (MAX (b, a), b) == b. */ - if (TREE_CODE (op0) == compl_code - && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0) - && reorder_operands_p (TREE_OPERAND (op0, 1), op1)) - return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1)); - - /* MIN (a, MAX (a, b)) == a. */ - if (TREE_CODE (op1) == compl_code - && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0) - && reorder_operands_p (op0, TREE_OPERAND (op1, 1))) - return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1)); - - /* MIN (a, MAX (b, a)) == a. */ - if (TREE_CODE (op1) == compl_code - && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0) - && reorder_operands_p (op0, TREE_OPERAND (op1, 0))) - return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0)); - - return NULL_TREE; -} - /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1 by changing CODE to reduce the magnitude of constants involved in ARG0 of the comparison. @@ -10426,15 +10383,7 @@ fold_binary_loc (location_t loc, return NULL_TREE; case MIN_EXPR: - tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1); - if (tem) - return tem; - goto associate; - case MAX_EXPR: - tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1); - if (tem) - return tem; goto associate; case TRUTH_ANDIF_EXPR: diff --git a/gcc/match.pd b/gcc/match.pd index e02379f..338644c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1176,6 +1176,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (minmax @0 @0) @0)) +/* min(max(x,y),y) -> y. */ +(simplify + (min:c (max:c @0 @1) @1) + @1) +/* max(min(x,y),y) -> y. */ +(simplify + (max:c (min:c @0 @1) @1) + @1) (simplify (min @0 @1) (if (INTEGRAL_TYPE_P (type)