From patchwork Wed Dec 6 15:23:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 120868 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp7188079qgn; Wed, 6 Dec 2017 07:23:47 -0800 (PST) X-Google-Smtp-Source: AGs4zMaUfeEB//8ZWHArDnU4P9gyOvdaN/D4mImQHswx/ZA9AAX5YPpSjy+HAKSr/KDSJ1ts+9SH X-Received: by 10.101.101.211 with SMTP id y19mr21466781pgv.191.1512573827699; Wed, 06 Dec 2017 07:23:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512573827; cv=none; d=google.com; s=arc-20160816; b=PBsozRmiWyWXAwzI+OfuahGdlC/x1dpb4Aew1yVfVVs8kwcSS7N4HSS5Q6ToPziuVD +G+w/LHDOCtdzPrRZuePUVOeK3/Fbmk0iTAkXka6XkBlk8D00F3cpQs9pu2fLTnm5/47 L1wbuR266GVVBWDPiHx6EaJjexpvJIThLE0sfWL9ueb1WE2EJkCX6CHRdLsXnxM+l3pY JghYvYUlW4DUw/rklg/qNDhEWPxPCsFJOao9sHWo0sNl8XRUKGGxqMP//HESyoF1Jtur LNYPD64NVj3M9PUa/ViQgUgVNyc+lYeZJ5Qvdpfb1JO4uLjXl7KX2akeIqNRru08SZqD ujmg== 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=0UMIVhpEQ4FhlhTsDQAmfJ4tT7uZpf++3zkfqDldYhk=; b=J7LHy9JIllIYKKQvLiIov2H/dMtjA/Xuny7THX5NNna70Qu6B6VfYdJWgv4wLH/6TZ /6X+bVyhg/rvfX1bEvAXeytnBvfiKmJUOO5uyZAbBdOGm9HhrSf3Osmqjfy54jxETGfn 4+HkpmTSq8aqbCdIVRO9WsLnLJRFtVY0NJ/pXZFPBq+zoq/dIPcvhciQdtupJ9tq3MQ5 uN5OhF8TV4F+rfO24F8tqQUxv4QI3vy6NttmxUqEUsLbV0wqDedIRiwbX1KO+ycupLnS v5+4qFah+i40CgnJU7/2JQqFtnXAnD7ZFwXypI5T+IpoXM89DFB+p7G5xv9Kis1ocOaV 6/og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZR4+NAzu; spf=pass (google.com: domain of gcc-patches-return-468614-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-468614-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 c32si2113126plj.281.2017.12.06.07.23.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 07:23:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-468614-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=ZR4+NAzu; spf=pass (google.com: domain of gcc-patches-return-468614-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-468614-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=MOnWUkjTi21tsUzZ 9rvhmPQJy3EFKdI+30XXYUkg0m1MxQCt3L0QCqLtnWOSVTqYpN3KIt8nwyRhBMHX nSCFrM+PIKNBPHc8gpqquwwEyBoC4oTcm74PAyKgGW9ajl9TQldP3fdzfZ81JN81 6scYwUlYIrbaCMwhS2dxKBzyDro= 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=KO3mWMvtECeqo8ZguVJsE7 mpkK4=; b=ZR4+NAzuTGeytXgFy/LmF1G/uS+8cZgJWrJzBTKHsbOhi3aBC8Emv2 q6lU58oBTWBfK1mRmr2Pefbel19LCc6jKV06Xtqti3YZ9b9pGgJg8g7jC+uQ6qVF 13feVlCgK1KT7/OWs412anOKd27Qh1OYBdJXv0io+g/9zJkWyHFns= Received: (qmail 124046 invoked by alias); 6 Dec 2017 15:23:35 -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 124033 invoked by uid 89); 6 Dec 2017 15:23:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wm0-f45.google.com Received: from mail-wm0-f45.google.com (HELO mail-wm0-f45.google.com) (74.125.82.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 06 Dec 2017 15:23:32 +0000 Received: by mail-wm0-f45.google.com with SMTP id r78so7915789wme.5 for ; Wed, 06 Dec 2017 07:23:31 -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=0UMIVhpEQ4FhlhTsDQAmfJ4tT7uZpf++3zkfqDldYhk=; b=EEWMM22sJy+iw0I4eMg3t96DQeRNoFY7g3kSH0jQm84bDkbAeYE9di2WWdTCUl4xTQ 3VRcDzWO5JjKRGsNFeNwvWeSiTD80Bvag1yodw4Uf6YUj9ZrK6whH2uk2TfoyS+r/bD3 2SYuncLS2x3yD8juppatqaobcxeGfusg/pfZgyXOBgK0AAyLGYMAKePE1/g1IzJ7tdTo mA8RJx3milADhy5kjLMjyDh/fxlBPYVakvza7fjT5qusJLkC+7TYoZAfXk8WBQqQuJSy oATvRytiDv5Xa4pVvJR5DKle7gvirMiTXX8F3Ehju1DNmHf3dcveZS5LFrJ3ltwUOvhU 3tnQ== X-Gm-Message-State: AKGB3mKxi8iQlPaKk82I8UX9sJMA8vFR/+vnquWk0ty/oO9az6XpMmSL MDG++gklLv2kTOZRE9UhHe+2cn2dzuA= X-Received: by 10.28.111.218 with SMTP id c87mr8998197wmi.91.1512573810020; Wed, 06 Dec 2017 07:23:30 -0800 (PST) Received: from localhost (188.29.165.206.threembb.co.uk. [188.29.165.206]) by smtp.gmail.com with ESMTPSA id m50sm3448952wrm.12.2017.12.06.07.23.28 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 06 Dec 2017 07:23:29 -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_unary_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:23:27 +0000 In-Reply-To: <87fu8nao27.fsf@linaro.org> (Richard Sandiford's message of "Wed, 06 Dec 2017 15:11:44 +0000") Message-ID: <874lp3anio.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 a single VECTOR_CST input. 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/ * fold-const.c (fold_negate_expr_1): Use tree_vector_builder and new_unary_operation, operating only on the encoded elements. (const_unop): Likewise. (exact_inverse): Likewise. (distributes_over_addition_p): New function. (const_binop): Use tree_vector_builder and new_unary_operation for combinations of VECTOR_CST and INTEGER_CST. Operate only on the encoded elements unless the encoding is strided and the operation does not distribute over addition. (fold_convert_const): Use tree_vector_builder and new_unary_operation. Operate only on the encoded elements for truncating integer conversions, or for non-stepped encodings. Index: gcc/fold-const.c =================================================================== --- gcc/fold-const.c 2017-12-06 14:48:52.887162217 +0000 +++ gcc/fold-const.c 2017-12-06 14:48:56.997993407 +0000 @@ -566,10 +566,10 @@ fold_negate_expr_1 (location_t loc, tree case VECTOR_CST: { - int count = VECTOR_CST_NELTS (t), i; - - auto_vec elts (count); - for (i = 0; i < count; i++) + tree_vector_builder elts; + elts.new_unary_operation (type, t, true); + unsigned int count = elts.encoded_nelts (); + for (unsigned int i = 0; i < count; ++i) { tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i)); if (elt == NULL_TREE) @@ -577,7 +577,7 @@ fold_negate_expr_1 (location_t loc, tree elts.quick_push (elt); } - return build_vector (type, elts); + return elts.build (); } case COMPLEX_EXPR: @@ -1121,6 +1121,27 @@ int_const_binop (enum tree_code code, co return int_const_binop_1 (code, arg1, arg2, 1); } +/* Return true if binary operation OP distributes over addition in operand + OPNO, with the other operand being held constant. OPNO counts from 1. */ + +static bool +distributes_over_addition_p (tree_code op, int opno) +{ + switch (op) + { + case PLUS_EXPR: + case MINUS_EXPR: + case MULT_EXPR: + return true; + + case LSHIFT_EXPR: + return opno == 1; + + default: + return false; + } +} + /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new constant. We assume ARG1 and ARG2 have the same data type, or at least are the same kind of constant and the same machine mode. Return zero if @@ -1442,10 +1463,12 @@ const_binop (enum tree_code code, tree a && TREE_CODE (arg2) == INTEGER_CST) { tree type = TREE_TYPE (arg1); - int count = VECTOR_CST_NELTS (arg1), i; - - auto_vec elts (count); - for (i = 0; i < count; i++) + bool step_ok_p = distributes_over_addition_p (code, 1); + tree_vector_builder elts; + if (!elts.new_unary_operation (type, arg1, 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); @@ -1458,7 +1481,7 @@ const_binop (enum tree_code code, tree a elts.quick_push (elt); } - return build_vector (type, elts); + return elts.build (); } return NULL_TREE; } @@ -1649,10 +1672,12 @@ const_unop (enum tree_code code, tree ty else if (TREE_CODE (arg0) == VECTOR_CST) { tree elem; - unsigned count = VECTOR_CST_NELTS (arg0), i; - auto_vec elements (count); - for (i = 0; i < count; i++) + /* This can cope with stepped encodings because ~x == -1 - x. */ + tree_vector_builder elements; + elements.new_unary_operation (type, arg0, true); + unsigned int i, count = elements.encoded_nelts (); + for (i = 0; i < count; ++i) { elem = VECTOR_CST_ELT (arg0, i); elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem); @@ -1661,7 +1686,7 @@ const_unop (enum tree_code code, tree ty elements.quick_push (elem); } if (i == count) - return build_vector (type, elements); + return elements.build (); } break; @@ -2135,10 +2160,19 @@ fold_convert_const (enum tree_code code, if (TREE_CODE (arg1) == VECTOR_CST && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1)) { - int len = VECTOR_CST_NELTS (arg1); tree elttype = TREE_TYPE (type); - auto_vec v (len); - for (int i = 0; i < len; ++i) + tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1)); + /* We can't handle steps directly when extending, since the + values need to wrap at the original precision first. */ + bool step_ok_p + = (INTEGRAL_TYPE_P (elttype) + && INTEGRAL_TYPE_P (arg1_elttype) + && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype)); + tree_vector_builder v; + if (!v.new_unary_operation (type, arg1, step_ok_p)) + return NULL_TREE; + unsigned int len = v.encoded_nelts (); + for (unsigned int i = 0; i < len; ++i) { tree elt = VECTOR_CST_ELT (arg1, i); tree cvt = fold_convert_const (code, elttype, elt); @@ -2146,7 +2180,7 @@ fold_convert_const (enum tree_code code, return NULL_TREE; v.quick_push (cvt); } - return build_vector (type, v); + return v.build (); } } return NULL_TREE; @@ -8832,7 +8866,6 @@ exact_inverse (tree type, tree cst) REAL_VALUE_TYPE r; tree unit_type; machine_mode mode; - unsigned vec_nelts, i; switch (TREE_CODE (cst)) { @@ -8846,12 +8879,14 @@ exact_inverse (tree type, tree cst) case VECTOR_CST: { - vec_nelts = VECTOR_CST_NELTS (cst); unit_type = TREE_TYPE (type); mode = TYPE_MODE (unit_type); - auto_vec elts (vec_nelts); - for (i = 0; i < vec_nelts; i++) + tree_vector_builder elts; + if (!elts.new_unary_operation (type, cst, false)) + return NULL_TREE; + unsigned int count = elts.encoded_nelts (); + for (unsigned int i = 0; i < count; ++i) { r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i)); if (!exact_real_inverse (mode, &r)) @@ -8859,7 +8894,7 @@ exact_inverse (tree type, tree cst) elts.quick_push (build_real (unit_type, r)); } - return build_vector (type, elts); + return elts.build (); } default: