Basic block liveouts

Is there an easy way to obtain all liveout variables of a basic block? Liveins
can be found for each MachineBasicBlock, but I can only find liveouts for the
whole function, at MachineRegisterInfo. Do I need to find them out manually?

They cannot be found like the live in values because different values may be live out.

For instance the psudocode may look like:

BB0:
vreg1 = rand()
if( vreg1 > .5 ) goto BB1;
else goto BB2;
BB1:
vreg 2 = rand();
EAX = copy vreg2;
Return
BB2:

vreg 3 = rand() * rand()’
EAX = copy vreg3;
Return

Sorry, I think I answered the wrong question.

Because I feel bad for giving a non-answer:

An easy way to find if a virtual register is alive after the basic block is toWhile iterating over the virtual registers

  • Check to see if the virtual register’s “next” value exists outside of the basic block.

for instance:

std::vector findLiveOut( MachineBasicBlock * mbb ) {
std::vector liveout;
for( MachineBasicBlock::iterator mbbi = mbb->begin(), mbbe = mbb->end(); mbbi != mbbe; ++mbbi ) {
for( opi = 0, ope = mbbi->getNumOperands(); opi < ope; ++opi ) {
MachineOperand & operand = mbbi->getOperand(opi);
if( operand.isReg() == false )
continue;
if( operand.getReg() == 0 )
continue;
if( ! TargetRegisterInfo::isVirtualRegister(operand.getReg()) )
continue;
if( mbb != operand.getNextOperandForReg()->getParent()->getParent() )
liveout.push_back( operand.getReg() );
}
}
return liveout;
}

I'm not sure this will work: The "next" use may be in a sibling block,
rather than a successor, or there could be intervening definitions. In
either case the value isn't really live out of the block.

I think the live-ins list only includes physical registers too.

You're probably best off running the LiveIntervals pass and seeing
which intervals are live across the last index in the basic block (see
LiveIntervals::getMBBEndIdx()).

- Lang.