Problem of getting two unused registers in eliminateFrameIndex()

Hello all,

For my custom processor backend I am trying add some instruction using
BuildMI() inside eliminateFrameIndex().

I tried RegScavenger like this:

unsigned RegUnused0 = RS->FindUnusedReg(&LASER::GNPRegsRegClass);
if (!RegUnused0)
  RegUnused0 = RS->scavengeRegister(&LASER::GNPRegsRegClass, II, SPAdj);
assert(RegUnused0 && "Register scavenger failed");
RS->setRegUsed(RegUnused0);

It works but there are two issues:

1) I need to registers and RegScavenger only returns one.
2) I cannot unset the used register and I get spill slot error message
when I ran out of the available registers.

So I gave up and tried createVirtualRegister():

MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
const TargetRegisterClass *RC = &LASER::GNPRegsRegClass;
unsigned Reg = RegInfo.createVirtualRegister(RC);

But then I get the following error:

MachineCopyPropagation.cpp:267: void
anonymous}::MachineCopyPropagation::CopyPropagateBlock(llvm::MachineBasicBlock&):
Assertion `!TargetRegisterInfo::isVirtualRegister(Reg) &&
"MachineCopyPropagation should be run after register allocation!"'
failed.

How can I solve my problem of getting two free registers in
eliminateFrameIndex() ?

Thank you,
Ehsan Ali.

There isn't really much else that you can do here. If you anticipate that two extra registers will be needed at some particular point in the function, reserve two spill slots.

Creating virtual registers in replaceFrameIndex should work, but make sure that your target implements "requiresRegisterScavenging", "requiresFrameIndexScavenging", "requiresFrameIndexReplacementScavenging" (all from TargetRegisterInfo) as appropriate.

-Krzysztof

Your hint was very helpful.

I enabled "requiresFrameIndexScavenging" and the
createVirtualRegister() worked as expected:

bool LaserRegisterInfo::
requiresFrameIndexScavenging(const MachineFunction &MF) const {
  return true;
}

I don't understand the logic behind it but somehow they are related.

Thank you Krzysztof.

Ehsan Ali.