From bb1bc604a852c38afd0df9d2f6a66a0e1a80db34 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 21 Apr 2017 12:09:20 +0000 Subject: [PATCH] re PR tree-optimization/78847 (pointer arithmetic from c++ ranged-based for loop not optimized) 2017-04-21 Richard Biener PR tree-optimization/78847 * fold-const.c (split_tree): Handle POINTER_PLUS_EXPR. * g++.dg/tree-ssa/pr78847.C: New testcase. From-SVN: r247061 --- gcc/ChangeLog | 5 +++++ gcc/fold-const.c | 7 +++++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/tree-ssa/pr78847.C | 26 +++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr78847.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 370d0b4d1b1..359273ef092 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2017-04-21 Richard Biener + + PR tree-optimization/78847 + * fold-const.c (split_tree): Handle POINTER_PLUS_EXPR. + 2017-04-21 Richard Biener * tree.h (build_qualified_type): Annotate with CXX_MEM_STAT_INFO. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 770162251f2..4ae21fb0d2f 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -798,8 +798,11 @@ split_tree (location_t loc, tree in, tree type, enum tree_code code, though the C standard doesn't say so) for integers because the value is not affected. For reals, the value might be affected, so we can't. */ - && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) - || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR)))) + && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR) + || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) + || (code == MINUS_EXPR + && (TREE_CODE (in) == PLUS_EXPR + || TREE_CODE (in) == POINTER_PLUS_EXPR))))) { tree op0 = TREE_OPERAND (in, 0); tree op1 = TREE_OPERAND (in, 1); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a40f9ffda33..414f27972bf 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-04-21 Richard Biener + + PR tree-optimization/78847 + * g++.dg/tree-ssa/pr78847.C: New testcase. + 2017-04-21 Jakub Jelinek PR c/80468 diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr78847.C b/gcc/testsuite/g++.dg/tree-ssa/pr78847.C new file mode 100644 index 00000000000..d5a83943804 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr78847.C @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target c++14 } */ +/* { dg-options "-O3 -fdump-tree-ldist" } */ + +#include +#include +#include + +using string_view = std::experimental::string_view; + +class Foo { + constexpr static size_t Length = 9; + char ascii_[Length]; +public: + Foo(); + string_view view() const { + return string_view(ascii_, Length); + } +}; + +void testWithLoopValue(const Foo foo, size_t ptr, char *buf_) { + for (auto c : foo.view()) + buf_[ptr++] = c; +} + +/* { dg-final { scan-tree-dump "memcpy\[^\n\r\]*, 9\\);" "ldist" } } */ -- 2.30.2