Is this a BUG? (yagarto)

OP #1609005
Rate this post
• ▲ useful
▼ not useful
In my first post (http://embdev.net/topic/166639)
I had problems with the -Wunreachable-code.

I have now removed that option, DLed latest yagarto,
and changed my prog like this:
1
typedef struct {
2
  int x, y;
3
} POINT, *pPOINT;
4

5
pPOINT TestProc (
6
        pPOINT start,
7
        pPOINT finish)
8
{
9

10
    pPOINT src, dst;
11
    POINT pt;
12

13
    src = start;
14
    dst = start;
15

16
    while (1) {
17
        // read point
18
        pt.x = src->x;
19
        pt.y = src->y;
20

21
        // store point & INC dst
22
        dst->x = pt.x;
23
        dst->y = pt.y;
24
        dst++;
25

26
        // skip same points (not just one, as before)
27
        do {
28
            src++;
29
            if (src == finish) return dst;
30
        } while ( (src->x == pt.x) && (src->y == pt.y) );
31
    }
32
}

and I get this:
warning: cannot optimize loop, the loop counter may overflow.

(the warning is on RETURN DST line)

Minimum command-line options to reproduce the warning are:

-Wunsafe-loop-optimizations
-Ox (-Os, -O1, -O2, -O3)

Is it really unsafe, or is it a bug?

-----------
This is a complete list of command-line options that I use,
should I add/remove some options?

-mcpu=arm926ej-s
-mfpu=fpa
-O3
-c
-MD
-MP
-gdwarf-2
-std=gnu99
-Wall
-Wextra
-pedantic
-Waggregate-return
-Wattributes
-Wcast-align
-Wcast-qual
-Wconversion
-Wdisabled-optimization
-Wdiv-by-zero
-Wfloat-equal
-Winit-self
-Winline
-Wint-to-pointer-cast
-Wlogical-op
-Wmissing-include-dirs
-Wnested-externs
-Wold-style-definition
-Wpadded
-Wpointer-arith
-Wpointer-to-int-cast
-Wpragmas
-Wredundant-decls
-Wstrict-prototypes
-Wundef
-Wunsafe-loop-optimizations
-Wunused
#1609614
Rate this post
• ▲ useful
▼ not useful
What happens if you change

while(1)

to

while( finish > start )

and then have a 'default' return before the last brace.  The optimizer 
will then be able to make safe assumptions about the relationship 
between start and finish in the loop.

Alternatively you could try adding an assert( finish > start ) before 
the while loop.  I am not sure whether the optimiser uses asserts as 
hints to make assumptions, but it may be worth a try (if such 
micro-optimisation is ever worth the effort in the first instance that 
is, which often it is not)

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.