Bug 60196 - [4.8 Regression] Incorrect compilation with -fwrapv and -ftree-vectorize
Summary: [4.8 Regression] Incorrect compilation with -fwrapv and -ftree-vectorize
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.8.2
: P3 major
Target Milestone: 4.8.4
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-14 15:21 UTC by Роман Донченко
Modified: 2014-09-08 20:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-03-12 00:00:00


Attachments
Preprocessed source for the reproducer (3.37 KB, text/plain)
2014-02-14 15:21 UTC, Роман Донченко
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Роман Донченко 2014-02-14 15:21:57 UTC
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.
Comment 1 Роман Донченко 2014-03-12 14:33:55 UTC
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.
Comment 2 Jakub Jelinek 2014-03-12 16:08:28 UTC
Started with r185437 and fixed again with r202663 (both testcases).
Comment 3 Richard Biener 2014-05-22 09:01:34 UTC
GCC 4.8.3 is being released, adjusting target milestone.
Comment 4 Jakub Jelinek 2014-09-08 20:08:01 UTC
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
Comment 5 Jakub Jelinek 2014-09-08 20:16:04 UTC
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
Comment 6 Jakub Jelinek 2014-09-08 20:18:32 UTC
Fixed.
Comment 7 Jakub Jelinek 2014-09-08 20:18:34 UTC
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