From patchwork Wed Dec 6 15:24:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 120869 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp7189169qgn; Wed, 6 Dec 2017 07:24:52 -0800 (PST) X-Google-Smtp-Source: AGs4zMbBUgEL3siLa4iMZek3i0ifGOSmG1Rz2aASkA0uSuDJPbiJuezKoJ9eyKZuqczPEGhN62e8 X-Received: by 10.84.168.35 with SMTP id e32mr9951156plb.297.1512573892469; Wed, 06 Dec 2017 07:24:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512573892; cv=none; d=google.com; s=arc-20160816; b=V9RZsInhlpFGGJmRre9AX/tfOYKTBwRLqR4CkdEHJBKexJgz0ThBBXSD7/BV5a9sky /zqq4vTfqF91y2w85r9p1bXktnFBtJTctOx/xvR7pg1ibG1O4mtowu5NPaAb+RKOHH6B JoIsV27eGQc1HlzuXybCA4xssckfxBRzE6jIMmsAyxzHAThzqvZNKB+4JpnZ/M2DnmZ2 2Q11iOubrO+KV+aTmMq1FaC6ypGVzbv6MGvPwGc1cGn2xK6DvgvDgOIYEkUkIRgmnU1h spl9HmOT84iddSVj1oqicoR5s9daF2VmPrI1PhttDM2IudNN+vpKMplUDHAfNiWyygTS +M5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:message-id:in-reply-to:date:references :subject:cc:mail-followup-to:to:from:delivered-to:sender:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=1EIY0HkUYh5kFVFFNOFDgm1IKla0LCxG63mte0e8Wq0=; b=m5B0Dvqie0I4DwiY7ZLIl+lNxEkXeN9Kcn8JGJVSwDcY2EbAlOiiF7Sb/NAPCRVkMs HIfw7eLV81Squw/0zz94HQZ3JiKts2RMxz0SLolGfvK+Bjvlz6MPvtOGn3A3xcWUBF8k 5mlj1/xOvP5t8+HN5v/iDD7ivWZzujIWV/BS0wSb0VJPDBTOMJGIkUJfJ8AsIpWMvSxo 6mkXQSziUi3yX9TG1z2wjXHsq8wcyi1ZzytQUVInP4cpTjJGTutjzpzudZVd2onKwmyQ y20k5/YtyFrQv0zkFA9yhm127VGBz3F7Z/OkeYi9dV0s/RmP14SklgTtZb/czaSyTBpu YBIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=U6NycHW7; spf=pass (google.com: domain of gcc-patches-return-468615-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-468615-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id m15si2100991pgt.327.2017.12.06.07.24.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 07:24:52 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-468615-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=U6NycHW7; spf=pass (google.com: domain of gcc-patches-return-468615-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-468615-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; q=dns; s=default; b=hXTvZUF7Q8eXGn/j phO/RwBRHBSxyz7IUMWnJgP2Ux5ntAvKXjvgApzC1ZCttkHXfmSVl9YAhYlKgeG9 qKI5bS3E104+PxnPtEW3X4lj5bl1oA3hf+sLEhI3LV/SChVU69gQZBt3mp9jx6nP imXusludhYnQs1pVcqQE2I3+Z/I= 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:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; s=default; bh=KVBc2DqiwCYfXH1mkAlSxT oySR0=; b=U6NycHW7Kb1L9yZE9YXyarGzBpbKnq6ce5pJOk5L4roB43QQO8yLsi 4EeyK00+V8aNrs7i01q0F1iKDKNmDZiGaaFWVNnMyNtRKdzSlr2WU0d9igp3IrcQ gsspnpu04Jo585hPdp7yiXKbXOCV5TTuzYwUD76SJU9rxnEDCANFE= Received: (qmail 128147 invoked by alias); 6 Dec 2017 15:24:40 -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 128129 invoked by uid 89); 6 Dec 2017 15:24:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wr0-f177.google.com Received: from mail-wr0-f177.google.com (HELO mail-wr0-f177.google.com) (209.85.128.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 06 Dec 2017 15:24:38 +0000 Received: by mail-wr0-f177.google.com with SMTP id z34so4288894wrz.10 for ; Wed, 06 Dec 2017 07:24:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:mail-followup-to:cc:subject:references :date:in-reply-to:message-id:user-agent:mime-version; bh=1EIY0HkUYh5kFVFFNOFDgm1IKla0LCxG63mte0e8Wq0=; b=cRi3a2lT9GyOrp71gT3JnxNonAMcqpaKeLrTFcd1mQzCAeYmVgWH/6tTpHVa2LqZEX o5o1jSCboAxw2c9U/Yt1XG8zgoGqAHevpFc/Y9l8QbuwNG0aq44CZF1MUyjM3w+6XrvB kU5PCrZrX9CVW1spdkHzUgkD62o7ZoAfC2BR4YWuVNTRLEDROHSP7DU8z2UCTmwqS1ok 1dD3CBeXQTeCLPaVA7nH4tAbQF+Gj2kLYlnNLJ02wGLl0DmGlstmzLyTo0UHQB+lw47+ k4omNEsz/bJebBlJDTGC8sMikmxkbfhtMrj1fSCgzIx+hw0YSdGZ92AfDxH8QZtAuupT f9tw== X-Gm-Message-State: AJaThX5lkHTlI4ffFZe8WXSKrCxHvtd049zmUKTbACWlASHLDyJSJRnV wEi44b/SMWggS2H2GYn6nxPCL/a0ivg= X-Received: by 10.223.171.202 with SMTP id s68mr19433518wrc.13.1512573876308; Wed, 06 Dec 2017 07:24:36 -0800 (PST) Received: from localhost (188.29.165.206.threembb.co.uk. [188.29.165.206]) by smtp.gmail.com with ESMTPSA id j77sm4006232wmf.36.2017.12.06.07.24.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 06 Dec 2017 07:24:35 -0800 (PST) From: Richard Sandiford To: Richard Biener Mail-Followup-To: Richard Biener , GCC Patches , richard.sandiford@linaro.org Cc: GCC Patches Subject: Use tree_vector_builder::new_binary_operation for folding References: <87shcxl2ka.fsf@linaro.org> <87efohkohu.fsf@linaro.org> <87efof9a6h.fsf@linaro.org> <87fu8nao27.fsf@linaro.org> Date: Wed, 06 Dec 2017 15:24:33 +0000 In-Reply-To: <87fu8nao27.fsf@linaro.org> (Richard Sandiford's message of "Wed, 06 Dec 2017 15:11:44 +0000") Message-ID: <87zi6v98we.fsf_-_@linaro.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 This patch makes fold-const.c operate directly on the VECTOR_CST encoding when folding an operation that has two VECTOR_CST inputs. Tested on aarch64-linux-gnu, x86_64-linux-gnu and powerpc64le-linux-gnu. Also spot-checked on sparc64-linux-gnu. OK to install? Thanks, Richard 2017-12-06 Richard Sandiford gcc/ * tree-vector-builder.h (tree_vector_builder::new_binary_operation): Declare. * tree-vector-builder.c (tree_vector_builder::new_binary_operation): New function. * fold-const.c (fold_relational_const): Use it. (const_binop): Likewise. Check that both input vectors have the same number of elements, thus excluding things like WIDEN_SUM. Check whether it is possible to operate directly on the encodings of stepped inputs. Index: gcc/tree-vector-builder.h =================================================================== --- gcc/tree-vector-builder.h 2017-12-06 14:46:14.131599903 +0000 +++ gcc/tree-vector-builder.h 2017-12-06 14:49:00.386854068 +0000 @@ -38,6 +38,7 @@ #define GCC_TREE_VECTOR_BUILDER_H void new_vector (tree, unsigned int, unsigned int); bool new_unary_operation (tree, tree, bool); + bool new_binary_operation (tree, tree, tree, bool); private: bool equal_p (const_tree, const_tree) const; Index: gcc/tree-vector-builder.c =================================================================== --- gcc/tree-vector-builder.c 2017-12-06 14:46:14.131599903 +0000 +++ gcc/tree-vector-builder.c 2017-12-06 14:49:00.386854068 +0000 @@ -49,6 +49,53 @@ tree_vector_builder::new_unary_operation return true; } +/* Try to start building a new vector of type TYPE that holds the result of + a binary operation on VECTOR_CSTs T1 and T2. ALLOW_STEPPED_P is true if + the operation can handle stepped encodings directly, without having to + expand the full sequence. + + Return true if the operation is possible. Leave the builder unchanged + otherwise. */ + +bool +tree_vector_builder::new_binary_operation (tree type, tree t1, tree t2, + bool allow_stepped_p) +{ + unsigned int full_nelts = TYPE_VECTOR_SUBPARTS (type); + gcc_assert (full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t1)) + && full_nelts == TYPE_VECTOR_SUBPARTS (TREE_TYPE (t2))); + /* Conceptually we split the patterns in T1 and T2 until we have + an equal number for both. Each split pattern requires the same + number of elements per pattern as the original. E.g. splitting: + + { 1, 2, 3, ... } + + into two gives: + + { 1, 3, 5, ... } + { 2, 4, 6, ... } + + while splitting: + + { 1, 0, ... } + + into two gives: + + { 1, 0, ... } + { 0, 0, ... }. */ + unsigned int npatterns = least_common_multiple (VECTOR_CST_NPATTERNS (t1), + VECTOR_CST_NPATTERNS (t2)); + unsigned int nelts_per_pattern = MAX (VECTOR_CST_NELTS_PER_PATTERN (t1), + VECTOR_CST_NELTS_PER_PATTERN (t2)); + if (!allow_stepped_p && nelts_per_pattern > 2) + { + npatterns = full_nelts; + nelts_per_pattern = 1; + } + new_vector (type, npatterns, nelts_per_pattern); + return true; +} + /* Return a VECTOR_CST for the current constant. */ tree Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c 2017-12-06 14:48:56.997993407 +0000 +++ gcc/fold-const.c 2017-12-06 14:49:00.386854068 +0000 @@ -1435,13 +1435,40 @@ const_binop (enum tree_code code, tree a } if (TREE_CODE (arg1) == VECTOR_CST - && TREE_CODE (arg2) == VECTOR_CST) + && TREE_CODE (arg2) == VECTOR_CST + && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) + == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)))) { tree type = TREE_TYPE (arg1); - int count = VECTOR_CST_NELTS (arg1), i; + bool step_ok_p; + if (VECTOR_CST_STEPPED_P (arg1) + && VECTOR_CST_STEPPED_P (arg2)) + /* We can operate directly on the encoding if: + + a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1 + implies + (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1) + + Addition and subtraction are the supported operators + for which this is true. */ + step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR); + else if (VECTOR_CST_STEPPED_P (arg1)) + /* We can operate directly on stepped encodings if: + + a3 - a2 == a2 - a1 + implies: + (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c) - auto_vec elts (count); - for (i = 0; i < count; i++) + which is true if (x -> x op c) distributes over addition. */ + step_ok_p = distributes_over_addition_p (code, 1); + else + /* Similarly in reverse. */ + step_ok_p = distributes_over_addition_p (code, 2); + tree_vector_builder elts; + if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p)) + return NULL_TREE; + unsigned int count = elts.encoded_nelts (); + for (unsigned int i = 0; i < count; ++i) { tree elem1 = VECTOR_CST_ELT (arg1, i); tree elem2 = VECTOR_CST_ELT (arg2, i); @@ -1455,7 +1482,7 @@ const_binop (enum tree_code code, tree a elts.quick_push (elt); } - return build_vector (type, elts); + return elts.build (); } /* Shifts allow a scalar offset for a vector. */ @@ -13770,11 +13797,10 @@ fold_relational_const (enum tree_code co } return constant_boolean_node (true, type); } - unsigned count = VECTOR_CST_NELTS (op0); - gcc_assert (VECTOR_CST_NELTS (op1) == count - && TYPE_VECTOR_SUBPARTS (type) == count); - - auto_vec elts (count); + tree_vector_builder elts; + if (!elts.new_binary_operation (type, op0, op1, false)) + return NULL_TREE; + unsigned int count = elts.encoded_nelts (); for (unsigned i = 0; i < count; i++) { tree elem_type = TREE_TYPE (type); @@ -13791,7 +13817,7 @@ fold_relational_const (enum tree_code co integer_zerop (tem) ? 0 : -1)); } - return build_vector (type, elts); + return elts.build (); } /* From here on we only handle LT, LE, GT, GE, EQ and NE.