Message ID | 20151105161612.GB25378@msticlxl57.ims.intel.com |
---|---|
State | New |
Headers | show |
On Thu, Nov 5, 2015 at 5:16 PM, Ilya Enkovich <enkovich.gnu@gmail.com> wrote: > Hi, > > This patch fixes a way vectype is computed in vectorizable_operation. Currently op0 is always used to compute vectype. If it is a loop invariant then its type is used to get vectype which is impossible for booleans requiring a context to correctly compute vectype. This patch uses output vectype in such cases, this should always work fine for operations on booleans. Bootstrapped on x86_64-unknown-linux-gnu. Regression tesing is in progress. Ok if no regressions? Ok. Richard. > Thanks, > Ilya > -- > gcc/ > > 2015-11-05 Ilya Enkovich <enkovich.gnu@gmail.com> > > PR tree-optimization/68145 > * tree-vect-stmts.c (vectorizable_operation): Fix > determination for booleans. > > gcc/testsuite/ > > 2015-11-05 Ilya Enkovich <enkovich.gnu@gmail.com> > > PR tree-optimization/68145 > * g++.dg/vect/pr68145.cc: New test. > > > diff --git a/gcc/testsuite/g++.dg/vect/pr68145.cc b/gcc/testsuite/g++.dg/vect/pr68145.cc > new file mode 100644 > index 0000000..51e663a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/vect/pr68145.cc > @@ -0,0 +1,41 @@ > +/* { dg-do compile } */ > + > +struct A { > + bool operator()(int p1, int p2) { return p1 && p2; } > +}; > +class B { > +public: > + bool *cbegin(); > + bool *cend(); > +}; > +template <class T> void operator&&(B p1, T p2) { > + B a; > + arrayContTransform(p1, p2, a, A()); > +} > + > +template <typename _InputIterator1, typename T, typename _OutputIterator, > + typename _BinaryOperation> > +void myrtransform(_InputIterator1 p1, _OutputIterator p2, T p3, > + _BinaryOperation p4) { > + _InputIterator1 b; > + for (; b != p1; ++b, ++p2) > + *p2 = p4(*b, p3); > +} > + > +template <typename L, typename R, typename RES, typename BinaryOperator> > +void arrayContTransform(L p1, R p2, RES p3, BinaryOperator p4) { > + myrtransform(p1.cend(), p3.cbegin(), p2, p4); > +} > + > +class C { > +public: > + B getArrayBool(); > +}; > +class D { > + B getArrayBool(const int &); > + C lnode_p; > +}; > +bool c; > +B D::getArrayBool(const int &) { lnode_p.getArrayBool() && c; } > + > +// { dg-final { scan-tree-dump "vectorized 1 loops" "vect" { target { i?86-*-* x86_64-*-* } } } } > diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c > index ae14075..9aa2d4e 100644 > --- a/gcc/tree-vect-stmts.c > +++ b/gcc/tree-vect-stmts.c > @@ -4697,7 +4697,26 @@ vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi, > /* If op0 is an external or constant def use a vector type with > the same size as the output vector type. */ > if (!vectype) > - vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out); > + { > + /* For boolean type we cannot determine vectype by > + invariant value (don't know whether it is a vector > + of booleans or vector of integers). We use output > + vectype because operations on boolean don't change > + type. */ > + if (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE) > + { > + if (TREE_CODE (TREE_TYPE (scalar_dest)) != BOOLEAN_TYPE) > + { > + if (dump_enabled_p ()) > + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, > + "not supported operation on bool value.\n"); > + return false; > + } > + vectype = vectype_out; > + } > + else > + vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out); > + } > if (vec_stmt) > gcc_assert (vectype); > if (!vectype)
diff --git a/gcc/testsuite/g++.dg/vect/pr68145.cc b/gcc/testsuite/g++.dg/vect/pr68145.cc new file mode 100644 index 0000000..51e663a --- /dev/null +++ b/gcc/testsuite/g++.dg/vect/pr68145.cc @@ -0,0 +1,41 @@ +/* { dg-do compile } */ + +struct A { + bool operator()(int p1, int p2) { return p1 && p2; } +}; +class B { +public: + bool *cbegin(); + bool *cend(); +}; +template <class T> void operator&&(B p1, T p2) { + B a; + arrayContTransform(p1, p2, a, A()); +} + +template <typename _InputIterator1, typename T, typename _OutputIterator, + typename _BinaryOperation> +void myrtransform(_InputIterator1 p1, _OutputIterator p2, T p3, + _BinaryOperation p4) { + _InputIterator1 b; + for (; b != p1; ++b, ++p2) + *p2 = p4(*b, p3); +} + +template <typename L, typename R, typename RES, typename BinaryOperator> +void arrayContTransform(L p1, R p2, RES p3, BinaryOperator p4) { + myrtransform(p1.cend(), p3.cbegin(), p2, p4); +} + +class C { +public: + B getArrayBool(); +}; +class D { + B getArrayBool(const int &); + C lnode_p; +}; +bool c; +B D::getArrayBool(const int &) { lnode_p.getArrayBool() && c; } + +// { dg-final { scan-tree-dump "vectorized 1 loops" "vect" { target { i?86-*-* x86_64-*-* } } } } diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c index ae14075..9aa2d4e 100644 --- a/gcc/tree-vect-stmts.c +++ b/gcc/tree-vect-stmts.c @@ -4697,7 +4697,26 @@ vectorizable_operation (gimple *stmt, gimple_stmt_iterator *gsi, /* If op0 is an external or constant def use a vector type with the same size as the output vector type. */ if (!vectype) - vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out); + { + /* For boolean type we cannot determine vectype by + invariant value (don't know whether it is a vector + of booleans or vector of integers). We use output + vectype because operations on boolean don't change + type. */ + if (TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE) + { + if (TREE_CODE (TREE_TYPE (scalar_dest)) != BOOLEAN_TYPE) + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, + "not supported operation on bool value.\n"); + return false; + } + vectype = vectype_out; + } + else + vectype = get_same_sized_vectype (TREE_TYPE (op0), vectype_out); + } if (vec_stmt) gcc_assert (vectype); if (!vectype)