[backport gcc-4.9/trunk r197843, needed by PR57051 fix ] List-Archive: Date: Thu, 11 Apr 2013 18:24:02 +0200 (CEST) From: Marc Glisse Subject: Fold {2, 3, 4, 5} >> 31 Hello, here is a patch to handle constant folding of mixed vector-integer operations. I could have shared the loop with the vector-vector case, but that would have meant re-testing if arg2 was a vector at every iteration (I can go back to that version if you prefer). bootstrap+testsuite on x86_64-linux-gnu gcc/ 2013-04-12 Marc Glisse * fold-const.c (const_binop): Handle vector shifts by a scalar. (fold_binary_loc): Call const_binop also for mixed vector-scalar operations. gcc/testsuite/ 2013-04-12 Marc Glisse * gcc.dg/fold-cstvecshift.c: New testcase. --- gcc-4.8.0/gcc/fold-const.c.~1~ 2013-02-26 11:00:31.000000000 +0100 +++ gcc-4.8.0/gcc/fold-const.c 2013-04-30 12:35:55.920486334 +0200 @@ -1342,21 +1342,44 @@ const_binop (enum tree_code code, tree a if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST) { - tree type = TREE_TYPE(arg1); + tree type = TREE_TYPE (arg1); int count = TYPE_VECTOR_SUBPARTS (type), i; - tree *elts = XALLOCAVEC (tree, count); + tree *elts = XALLOCAVEC (tree, count); for (i = 0; i < count; i++) { - tree elem1 = VECTOR_CST_ELT (arg1, i); + tree elem1 = VECTOR_CST_ELT (arg1, i); tree elem2 = VECTOR_CST_ELT (arg2, i); - elts[i] = const_binop (code, elem1, elem2); + elts[i] = const_binop (code, elem1, elem2); - /* It is possible that const_binop cannot handle the given - code and return NULL_TREE */ - if(elts[i] == NULL_TREE) - return NULL_TREE; + /* It is possible that const_binop cannot handle the given + code and return NULL_TREE */ + if (elts[i] == NULL_TREE) + return NULL_TREE; + } + + return build_vector (type, elts); + } + + /* Shifts allow a scalar offset for a vector. */ + if (TREE_CODE (arg1) == VECTOR_CST + && TREE_CODE (arg2) == INTEGER_CST) + { + tree type = TREE_TYPE (arg1); + int count = TYPE_VECTOR_SUBPARTS (type), i; + tree *elts = XALLOCAVEC (tree, count); + + for (i = 0; i < count; i++) + { + tree elem1 = VECTOR_CST_ELT (arg1, i); + + elts[i] = const_binop (code, elem1, arg2); + + /* It is possible that const_binop cannot handle the given + code and return NULL_TREE */ + if (elts[i] == NULL_TREE) + return NULL_TREE; } return build_vector (type, elts); @@ -9855,7 +9878,8 @@ fold_binary_loc (location_t loc, || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST) || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST) || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST) - || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)) + || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST) + || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == INTEGER_CST)) { if (kind == tcc_binary) { --- gcc-4.8.0/gcc/testsuite/gcc.dg/fold-cstvecshift.c.~1~ 1970-01-01 01:00:00.000000000 +0100 +++ gcc-4.8.0/gcc/testsuite/gcc.dg/fold-cstvecshift.c 2013-04-30 12:35:55.920486334 +0200 @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-ccp1" } */ + +typedef int vec __attribute__ ((vector_size (4 * sizeof (int)))); + +void f (vec *r) +{ + vec a = { 2, 3, 4, 5 }; + *r = (a << 2) >> 1; +} + +/* { dg-final { scan-tree-dump "{ 4, 6, 8, 10 }" "ccp1"} } */ +/* { dg-final { cleanup-tree-dump "ccp1" } } */