New to LLVM. Need help getting available register

Hi,
I’m new to LLVM. I’d like to know if there is a method I can call whenever I need any available CPU register.
Thanks.

m m via llvm-dev <llvm-dev@lists.llvm.org> writes:

I'm new to LLVM. I'd like to know if there is a method I can call
whenever I need any available CPU register.

Take a look at RegisterScavenging. Of course, this only works after
register allocation. Before register allocation you'd just create a new
virtual register.

                              -David

Is it possible to get a virtual register and then use that to create a real register? I’ve seen it done in unittests/CodeGen/MachineInstrTest.cpp like this:

unsigned VirtualDef1 = -42;

VD1VU->addOperand(*MF,

MachineOperand::CreateReg(VirtualDef1, /isDef/ true));

But when I do that in my code I get an assertion so I wasn’t sure if it’s legal or not.
Thanks.

It would help if you can describe in more detail what you are trying to do.
In which phase of the compiler is this being done? What is your ultimate
goal?

I would not look to unit tests as good examples of how to do things. They
are specifically designed to pinpoint small bits of functionality and often
rare corner cases. It's possible the bit of code you're looking at is actually
testing that the assertion triggers.

                                     -David

David,

It is done inside eliminateFrameIndex(). I’m trying to move the stack offset to a register by using my custom MOVRI_P pseudo instruction this way:

unsigned AR = MF.getRegInfo().createVirtualRegister(XXX::RegClass);
BuildMI(MB,II,dl, TII.get(XXX::MOVRI_P), AR).addImm(Offset);

(When I print AR, I get 2147483648 = 0x80000000)

Then, in expandPostRAPseudo(), I replace it with the real MOVRI instruction

MachineOperand op_0 = MI.getOperand(0);

MachineOperand op_1 = MI.getOperand(1);

BuildMI(MB,MBI,dl, get(XXX::MOVRI)).add(op_0).add(op_1);

At this point op_1_0.getReg() is 2147483648 and op_1_1.getImm() is correct

When I execute the program it says: Assertion `RegNo && RegNo < 37 && “Invalid register number!”’ failed.

Thanks

The assertion happens because you've got a virtual register where a
physical (machine) register is expected.

RegScavenger is your best bet to get an available physical register. It
takes a little setup to use but it's not too bad.

                                    -David