From patchwork Wed Nov 3 11:45:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Artem Shinkarov X-Patchwork-Id: 69978 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]) by ozlabs.org (Postfix) with SMTP id 4A186B6F0D for ; Wed, 3 Nov 2010 22:45:59 +1100 (EST) Received: (qmail 21048 invoked by alias); 3 Nov 2010 11:45:56 -0000 Received: (qmail 21031 invoked by uid 22791); 3 Nov 2010 11:45:53 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, TW_TM X-Spam-Check-By: sourceware.org Received: from mail-vw0-f47.google.com (HELO mail-vw0-f47.google.com) (209.85.212.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 03 Nov 2010 11:45:48 +0000 Received: by vws17 with SMTP id 17so986693vws.20 for ; Wed, 03 Nov 2010 04:45:46 -0700 (PDT) Received: by 10.224.76.81 with SMTP id b17mr3652889qak.146.1288784746180; Wed, 03 Nov 2010 04:45:46 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.4.25 with HTTP; Wed, 3 Nov 2010 04:45:26 -0700 (PDT) In-Reply-To: <4CD05F30.2060500@redhat.com> References: <4CD05F30.2060500@redhat.com> From: Artem Shinkarov Date: Wed, 3 Nov 2010 11:45:26 +0000 Message-ID: Subject: Re: Fold vector to scalar optimization To: Richard Henderson Cc: Richard Guenther , gcc-patches@gcc.gnu.org, "Joseph S. Myers" X-IsSubscribed: yes 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 All right, here we go. On Tue, Nov 2, 2010 at 6:57 PM, Richard Henderson wrote: >> +        if (!(eq = operand_equal_p (first, TREE_VALUE(t), 0))) > ... >> +          if (!(eq = (first == t))) > > Do not bury assignments inside conditionals. > > > r~ > Index: gcc/testsuite/gcc.dg/vec-scal-opt2.c =================================================================== --- gcc/testsuite/gcc.dg/vec-scal-opt2.c (revision 0) +++ gcc/testsuite/gcc.dg/vec-scal-opt2.c (revision 0) @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-veclower" } */ + +#define vidx(type, vec, idx) (*((type *) &(vec) + idx)) +#define vector(elcount, type) \ +__attribute__((vector_size((elcount)*sizeof(type)))) type + +int main (int argc, char *argv[]) { + vector(8, short) v0 = {argc,1,2,3,4,5,6,7}; + vector(8, short) v1 = {2,2,2,2,2,2,2,2}; + vector(8, short) r1; + + r1 = v0 >> v1; + + return vidx(short, r1, 0); +} + +/* { dg-final { scan-tree-dump-times ">> 2" 1 "veclower" } } */ +/* { dg-final { cleanup-tree-dump "veclower" } } */ Index: gcc/testsuite/gcc.dg/vec-scal-opt.c =================================================================== --- gcc/testsuite/gcc.dg/vec-scal-opt.c (revision 0) +++ gcc/testsuite/gcc.dg/vec-scal-opt.c (revision 0) @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-veclower" } */ + +#define vidx(type, vec, idx) (*((type *) &(vec) + idx)) +#define vector(elcount, type) \ +__attribute__((vector_size((elcount)*sizeof(type)))) type + +short k; + +int main (int argc, char *argv[]) { + k = argc; + vector(8, short) v0 = {argc,1,2,3,4,5,6,7}; + vector(8, short) v2 = {k, k,k,k,k,k,k,k}; + vector(8, short) r1; + + r1 = v0 >> v2; + + return vidx(short, r1, 0); +} + +/* { dg-final { scan-tree-dump-times ">> k.\[0-9_\]*" 1 "veclower" } } */ +/* { dg-final { cleanup-tree-dump "veclower" } } */ Index: gcc/testsuite/gcc.dg/vec-scal-opt1.c =================================================================== --- gcc/testsuite/gcc.dg/vec-scal-opt1.c (revision 0) +++ gcc/testsuite/gcc.dg/vec-scal-opt1.c (revision 0) @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-veclower" } */ + +#define vidx(type, vec, idx) (*((type *) &(vec) + idx)) +#define vector(elcount, type) \ +__attribute__((vector_size((elcount)*sizeof(type)))) type + +short k; + +int main (int argc, char *argv[]) { + vector(8, short) v0 = {argc,1,2,3,4,5,6,7}; + vector(8, short) r1; + + r1 = v0 >> (vector(8, short)){2,2,2,2,2,2,2,2}; + + return vidx(short, r1, 0); +} + +/* { dg-final { scan-tree-dump-times ">> 2" 1 "veclower" } } */ +/* { dg-final { cleanup-tree-dump "veclower" } } */ Index: gcc/tree-vect-generic.c =================================================================== --- gcc/tree-vect-generic.c (revision 166126) +++ gcc/tree-vect-generic.c (working copy) @@ -284,6 +284,60 @@ expand_vector_addition (gimple_stmt_iter a, b, code); } +/* Check if vector VEC consists of all the equal elements and + that the number of elements corresponds to the type of VEC. + Function sets ELEMENT with the first element of the vector + or NULL_TREE if comparison failed. */ +static bool uniform_vector_p (tree vec, tree *element) +{ + tree first, t, els; + bool eq = true; + unsigned HOST_WIDE_INT i; + + *element = NULL_TREE; + + if (vec == NULL_TREE) + return false; + + if (TREE_CODE (vec) == VECTOR_CST) + { + els = TREE_VECTOR_CST_ELTS (vec); + first = TREE_VALUE (els); + els = TREE_CHAIN (els); + + for (t = els, i = 0; t; t = TREE_CHAIN (t), i++) + eq = operand_equal_p (first, TREE_VALUE(t), 0); + if (!eq) + break; + + *element = first; + return eq && (i == TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec))-1); + } + + else if (TREE_CODE (vec) == CONSTRUCTOR) + { + first = error_mark_node; + + FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t) + { + if (i == 0) + { + first = t; + continue; + } + eq = (first == t); + if (!eq) + break; + } + + *element = first; + return eq && (i == TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec))); + + } + + return false; +} + static tree expand_vector_operation (gimple_stmt_iterator *gsi, tree type, tree compute_type, gimple assign, enum tree_code code) @@ -392,7 +446,7 @@ expand_vector_operations_1 (gimple_stmt_ tree lhs, rhs1, rhs2 = NULL, type, compute_type; enum tree_code code; enum machine_mode compute_mode; - optab op; + optab op = NULL; enum gimple_rhs_class rhs_class; tree new_rhs; @@ -434,18 +488,48 @@ expand_vector_operations_1 (gimple_stmt_ || code == LROTATE_EXPR || code == RROTATE_EXPR) { - /* If the 2nd argument is vector, we need a vector/vector shift */ + bool vector_scalar_shift; + tree x = build_int_cst (integer_type_node, 2); + + warning (0, x == build_int_cst (integer_type_node, 2) ? "Yes" : "No"); + op = optab_for_tree_code (code, type, optab_scalar); + + /* Vector/Scalar shift is supported. */ + vector_scalar_shift = !(!op + || optab_handler (op, TYPE_MODE (type)) + == CODE_FOR_nothing); + + /* If the 2nd argument is vector, we need a vector/vector shift. + Except all the elements in the second vector are the same. */ if (VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (rhs2)))) - op = optab_for_tree_code (code, type, optab_vector); - else - { - /* Try for a vector/scalar shift, and if we don't have one, see if we - have a vector/vector shift */ - op = optab_for_tree_code (code, type, optab_scalar); - if (!op - || optab_handler (op, TYPE_MODE (type)) == CODE_FOR_nothing) - op = optab_for_tree_code (code, type, optab_vector); - } + { + tree first, var = NULL_TREE; + gimple def_stmt; + + /* Check whether we have vector {x,x,x,x} where x + could be a scalar variable or a constant. Transform + vector {x,x,x,x} ==> vector scalar. */ + if (vector_scalar_shift + && ((TREE_CODE (rhs2) == VECTOR_CST && (var = rhs2)) + || + (TREE_CODE (rhs2) == SSA_NAME + && (def_stmt = SSA_NAME_DEF_STMT (rhs2)) + && gimple_assign_single_p (def_stmt) + && (var = gimple_assign_rhs1 (def_stmt)))) + && uniform_vector_p (var, &first)) + { + gimple_assign_set_rhs2 (stmt, first); + update_stmt (stmt); + rhs2 = first; + } + else + op = optab_for_tree_code (code, type, optab_vector); + } + + /* Try for a vector/scalar shift, and if we don't have one, see if we + have a vector/vector shift */ + else if (!vector_scalar_shift) + op = optab_for_tree_code (code, type, optab_vector); } else op = optab_for_tree_code (code, type, optab_default); Index: gcc/passes.c =================================================================== --- gcc/passes.c (revision 166126) +++ gcc/passes.c (working copy) @@ -736,7 +736,6 @@ init_optimization_passes (void) NEXT_PASS (pass_refactor_eh); NEXT_PASS (pass_lower_eh); NEXT_PASS (pass_build_cfg); - NEXT_PASS (pass_lower_vector); NEXT_PASS (pass_warn_function_return); NEXT_PASS (pass_build_cgraph_edges); NEXT_PASS (pass_inline_parameters); @@ -755,6 +754,7 @@ init_optimization_passes (void) NEXT_PASS (pass_referenced_vars); NEXT_PASS (pass_build_ssa); + NEXT_PASS (pass_lower_vector); NEXT_PASS (pass_early_warn_uninitialized); /* Note that it is not strictly necessary to schedule an early inline pass here. However, some test cases (e.g.,