https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78646
Bug ID: 78646 Summary: incorrect result type for pointer addition in slsr Product: gcc Version: 5.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: stefan at reservoir dot com Target Milestone: --- Created attachment 40225 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40225&action=edit output of -fdump-tree-slsr In the program below, struct A is 64-bit aligned and struct B is 128-bit aligned. An array of struct A in part of struct B. When the array in B is accessed with a variable index, slsr computes the pointer to the array element with the incorrect type 'struct B *' rather than 'struct A *'. On my out-of-tree target, this results in incorrect code being generated; on x86 I can only show the issue when looking at the debug output. In the attached dump, the type of _8 and _14 is 'struct B *' but I would expect them to have type 'struct A *'. Here is the preprocessed program: # 1 "bug.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "bug.cpp" typedef int v4si __attribute__((vector_size(16))); typedef int v2si __attribute__((vector_size(8))); int __builtin_foo(int, int, int); template <typename, typename> struct A; template <> struct alignas(v2si) A<int, int> { int _a; int _b; }; template <typename T> T foo(T a, A<int, int> &b, int) { return __builtin_foo(a, b._a, b._b); } template <typename T> T foo(A<int, int> &a, T b, int c) { return foo(b, a, c); } struct alignas(v4si) B { struct { A<int, int> ara[16]; }; int bar(int, int); }; int B::bar(int i, int a) { return foo(ara[i], a, 1); } Compiled using: g++ -v -save-temps -Wall -Wextra -std=gnu++14 -fdump-tree-slsr -fdump-tree-optimized -O -S bug.ii Using built-in specs. COLLECT_GCC=g++ Target: i686-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-i386/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-i386 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-i386 --with-arch-directory=i386 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-targets=all --enable-multiarch --disable-werror --with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-std=gnu++14' '-fdump-tree-slsr' '-fdump-tree-optimized' '-O' '-S' '-shared-libgcc' '-mtune=generic' '-march=i686' /usr/lib/gcc/i686-linux-gnu/5/cc1plus -fpreprocessed bug.ii -quiet -dumpbase bug.ii -mtune=generic -march=i686 -auxbase bug -O -Wall -Wextra -std=gnu++14 -version -fdump-tree-slsr -fdump-tree-optimized -o bug.s -fstack-protector-strong -Wformat-security GNU C++14 (Ubuntu 5.4.0-6ubuntu1~16.04.4) version 5.4.0 20160609 (i686-linux-gnu) compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C++14 (Ubuntu 5.4.0-6ubuntu1~16.04.4) version 5.4.0 20160609 (i686-linux-gnu) compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 427d6507e583781294a191f5962a1495 COMPILER_PATH=/usr/lib/gcc/i686-linux-gnu/5/:/usr/lib/gcc/i686-linux-gnu/5/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/5/:/usr/lib/gcc/i686-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/i686-linux-gnu/5/:/usr/lib/gcc/i686-linux-gnu/5/../../../i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/5/../../../../lib/:/lib/i386-linux-gnu/:/lib/../lib/:/usr/lib/i386-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/5/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Wextra' '-std=gnu++14' '-fdump-tree-slsr' '-fdump-tree-optimized' '-O' '-S' '-shared-libgcc' '-mtune=generic' '-march=i686' Here is a possible solution for this problem (the assert is presumably gratuitous): diff --git a/gcc/gimple-ssa-strength-reduction.c b/gcc/gimple-ssa-strength-reduction.c --- a/gcc/gimple-ssa-strength-reduction.c +++ b/gcc/gimple-ssa-strength-reduction.c @@ -1918,7 +1918,9 @@ replace_ref (tree *expr, slsr_cand_t c) if (align < TYPE_ALIGN (acc_type)) acc_type = build_aligned_type (acc_type, align); - add_expr = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (c->base_expr), + gcc_assert (POINTER_TYPE_P (c->cand_type)); + add_expr = fold_build2 (POINTER_PLUS_EXPR, + build_pointer_type (TREE_TYPE (c->cand_type)), c->base_expr, c->stride); mem_ref = fold_build2 (MEM_REF, acc_type, add_expr, wide_int_to_tree (c->cand_type, c->index));