[PATCH] Optimize strcmp(ptr,ptr) and friends

Roger Sayle roger@eyesopen.com
Tue Jul 15 00:25:00 GMT 2003


Michael Meisner wrote:
> My question is whether it is really worth adding this optimization,
> since I have a hard time imagining when it would be used in real code.
> The only case I can think of is if you have some convoluted macro usage
> that you do a string op with an argument against a string literal, and
> the argument happens to be a string literal also.

Its important to remember that GCC's optimizers are much like a game
of snakes-and-ladders, you place your source code in at one end and
but what comes out the other end is up to a roll of the dice.

Consider the following short code example:

typedef struct {
  char name[85];
} foo;

void bar(foo *ptr)
{
  foo *bar = ptr;
  *bar = *ptr;
}

The code itself is perfectly well formed and valid C.  The untrained
eye might not even see GCC's implicit call to memcpy with pointers to
overlapping memory invoking undefined behaviour in the system library...


p.s. A good politician should avoid phrases like "I have a hard time
imagining" in an argument or debate, as you open yourself up to the
obvious repost that perhaps one needs a little more imagination :>.

Roger
--



More information about the Gcc-patches mailing list