Created attachment 32135 [details] Preprocessed source for the reproducer This program should print 36 (preprocessed source attached): #include <stdio.h> static int foo(const short * a, int len) { int x; int x1 = 0; for( x = 0; x < len; x++ ) x1 += x * a[x]; return x1; } int main() { short stuff[9] = {1,1,1,1,1,1,1,1,1}; printf("%d\n", foo(stuff, 9)); return 0; } However, if compiled with -O1 -fwrapv -ftree-vectorize, it prints 16 instead. Omitting any of those flags gives the correct result. I can reproduce this on 4.8.2 and 4.8 branch head. It doesn't reproduce on trunk, though. (All three built from vanilla source.) Target is x86_64-unknown-linux-gnu. I also tested it on a bunch of Ubuntu's prebuilt GCC versions. It reproduces on 4.6.4, 4.7.3, 4.8.1, but not on 4.4.7.
Here's another reproducer for what looks like the same problem: #include <stdio.h> static const short a[8] = {1,1,1,1,1,1,1,1}; static const unsigned char b[8] = {0,0,0,0,0,0,0,0}; static int foo() { int sum = 0, i; for (i = 0; i < 8; ++i) sum += a[i] * b[i]; return sum; } int main() { printf("%d\n", foo()); return 0; } The correct result is 0, but compiling with -O1 -ftree-vectorize yields 8. I looked at the disassembly, and it just sums the elements of a, b is not used at all. For this one, adding -fwrapv actually fixes it, but I don't think there are any overflows here.
Started with r185437 and fixed again with r202663 (both testcases).
GCC 4.8.3 is being released, adjusting target milestone.
Author: jakub Date: Mon Sep 8 20:07:29 2014 New Revision: 215024 URL: https://gcc.gnu.org/viewcvs?rev=215024&root=gcc&view=rev Log: PR tree-optimization/60196 PR tree-optimization/63189 Backported from mainline 2013-09-17 Cong Hou <congh@google.com> * tree-vect-patterns.c (vect_recog_dot_prod_pattern): Fix a bug when checking the dot production pattern. The type of rhs operand of multiply is now checked correctly. * gcc.dg/vect/pr63189.c: New test. * gcc.dg/vect/pr60196-1.c: New test. * gcc.dg/vect/pr60196-2.c: New test. Backported from mainline 2013-09-17 Cong Hou <congh@google.com> * gcc.dg/vect/vect-reduc-dot-s16c.c: Add a test case with dot product on two arrays with short and int types. This should not be recognized as a dot product pattern. Added: branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr60196-1.c branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr60196-2.c branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/pr63189.c branches/gcc-4_8-branch/gcc/testsuite/gcc.dg/vect/vect-reduc-dot-s16c.c Modified: branches/gcc-4_8-branch/gcc/ChangeLog branches/gcc-4_8-branch/gcc/testsuite/ChangeLog branches/gcc-4_8-branch/gcc/tree-vect-patterns.c
Author: jakub Date: Mon Sep 8 20:15:31 2014 New Revision: 215025 URL: https://gcc.gnu.org/viewcvs?rev=215025&root=gcc&view=rev Log: PR tree-optimization/60196 PR tree-optimization/63189 * gcc.dg/vect/pr63189.c: New test. * gcc.dg/vect/pr60196-1.c: New test. * gcc.dg/vect/pr60196-2.c: New test. Added: trunk/gcc/testsuite/gcc.dg/vect/pr60196-1.c trunk/gcc/testsuite/gcc.dg/vect/pr60196-2.c trunk/gcc/testsuite/gcc.dg/vect/pr63189.c Modified: trunk/gcc/testsuite/ChangeLog
Fixed.
Author: jakub Date: Mon Sep 8 20:18:02 2014 New Revision: 215026 URL: https://gcc.gnu.org/viewcvs?rev=215026&root=gcc&view=rev Log: PR tree-optimization/60196 PR tree-optimization/63189 * gcc.dg/vect/pr63189.c: New test. * gcc.dg/vect/pr60196-1.c: New test. * gcc.dg/vect/pr60196-2.c: New test. Added: branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr60196-1.c branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr60196-2.c branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/vect/pr63189.c Modified: branches/gcc-4_9-branch/gcc/testsuite/ChangeLog