How many years ago? Which compiler? I'm sure you can no longer answer this, but I also wonder what optimization flags did they try and whether they tried PGO or
compiler hints (i.e. expect, unreachable, assume_aligned, prefetch, etc.).
There are some options like
-fomit-frame-pointer
and
-fPIC
that can also affect, as well as hardening options which do things like array bounds-checks.
However, based on your description I'm guessing the real culprit is pointer aliasing. Here's where it pays to know your language and your tools, because you can simply assert that two pointers or variabes don't alias and the compiler will then treat them as if they don't. The main reason fortran is traditionally faster is that it has no possibility of aliasing.
It's a shame that people often just ditch the compiler and drop straight to assembly language, in cases like this, because if they just put the time into figuring out
why the compiler is generating this "unnecessary" code, they might actually learn something about their tools and how to use them better. Instead, hubris tends to rear its ugly head and they just assume it's because the compiler is dumb and they're
ever so smart.
It's a tool, not magic. Don't think of them in idealized terms, any more than you would regard another complex machine, like a car engine or a printing press. One thing I always try to drill into junior programmers is:
learn your tools! This is common sense, in any other trade, yet somehow I've met more than one working programmer, near the end of their career who almost seem proud of how little they know about them.
You can usually specify both which machine you want the generated code to be compatible with and which it should be tuned for, via separate
-mtune
and
-march
options. There are blended targets, as well. I believe GCC has long supported function multi-versioning, though it's not an area I've delved into. This page describes the basics, but it's quite old and I'd imagine support for it has probably evolved since:
Update:
Oh. Well, some compilers were indeed a lot more primitive, back then. It's risky to apply lessons from such ancient history to modern tools and software ...at least, not without more specifics.