[cfe-dev] A question on register allocation

Renato Golin via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 6 02:07:45 PDT 2015


On 5 October 2015 at 19:56, Joachim Durchholz via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
> Just for the record, I'm seeing another use case beyond operating systems:
> Virtual machines, runtimes for languages that do not put much value on an
> easy C interface.

IIRC, VMs use the "platform register", which is a specially reserved
register allowed by the ABIs of different targets, in certain cases.


> It could be pretty big for each use case. I guess operating system coders
> tend to have a specialist for each architecture anyway, and can do better
> than a compiler could; I'm not so sure for VM and runtime coders.

That excuse died a long time ago. When people tell you they can do
better than the compiler, it's normally for one very special corner
case, not for the whole program. Global named register affect the
whole program and can upset the compiler in unpredictable ways. My
reaction when people say "it should be simple" about register
allocation is the same as when people say "quantum mechanics is
easy"...

There are already three well supported ways to be better than the
compiler at a local level: assembly files, inline assembly, intrinsics
in C code. None of them require special registers to be globally
allocated. All of them well supported in LLVM and GNU toolchains.

As David said, the only *real* use for global named registers is for
registers that have a specific meaning throughout the program: the
stack pointer being the only one that has a useful meaning. You don't
want to be changing the PC that easily, but if you really do, doing so
in inline asm is perfectly valid.

Specific platforms have flags to reserve specific registers. For
instance, on ARM you can use -ffixed-r9, then you can use them in
local named registers and be sure that you're the only one updating
it. However, if the platform you're running requires it (I think
Darwin does), then your code isn't portable, as expected.

Assuming you can write more optimal code than the compiler for
specific functionality is ok. Assuming you can change how objects are
built and make it work regardless the platform or environment, is not.

cheers,
--renato



More information about the cfe-dev mailing list