Search code examples
llvmmachine-instruction

Extracting MachineBasicBlock from Branch Instruction


The branch instruction contains labels which are the names of the basicblocks that it might jump to. Given that, is there a way to extract a MachineBasicBlock object from a branching instruction? for example:

for(MachineBasicBlock &BB : MF){
    for(MachineInstr &MI : BB){
      if(MI.isConditionalBranch()){
        MachineBasicBlock &InstBB = something(MI.getOperand(0));
      }
    }
  }

Solution

  • First you cast MI's operand to BasicBlockSDNode and then use getBasicBlock(). Remember to perform the casting using LLVM cast<>() function.