how to get MachineBasicBlock of a BasicBlock

Hello, we can get BasicBlock from MachineBasicBlock through MachineBasicBlock::getBasicBlock() function, but how can I get MachineBasicBlock of a BasicBlock?
Thank you!

I don't think you can.

The BasicBlock is a member of MachineBasicBlock. It is not inherited,
so it cannot be cast. The number of the MachineBasicBlock is not the
same as any BasicBlock values. So
MachineFunction::getMachineBasicBlock( BasicBlock::{get the number} )
cannot work. I do not see much in the basic block which can identify

So you can search for it.

typedef struct findBlock {
  BasicBlock * block;
  findBlock( BasicBlock * block ) : block(block) {}
  bool operator( MachineBasicBlock * mbb ) { return
mbb->getBasicBlock() == block; }
} findBlock;
MachineBasicBlock = *std::find( machineFunction.begin(), machineFunction.end(),
  findBlock( basicBlock ) );

-Jeff Kunkel

Also note: there may be multiple MachineBasicBlock’s for a single BasicBlock.

  • David M

So a simple revision

typedef struct findBlock {
const BasicBlock * block;
std::vector<MachineBasicBlock*> * blocks
findBlock( BasicBlock * block, std::vector<MachineBasicBlock*> * blocks )
  : block(block), blocks(blocks) {}
void operator( MachineBasicBlock * mbb ) {
    if( mbb->getBasicBlock() == block)
} findBlock;

std::vector<MachineBasicBlock*> blocks
std::for_each( machineFunction.begin(), machineFunction.end(),
findBlock( basicBlock, blocks ) );

Jeff Kunkel