Create new MachineBasicBlock

I’m trying to add a new label after an instruction, in a post reg alloc pass.

The current solution I have is:

// Create a new label
MachineBasicBlock *NewLabel = MF.CreateMachineBasicBlock();

// Add it to the machine function - after the instruction
MF.insert(std::next(MBBI), NewLabel);

// Copy the successors of the current label to the new label
for (auto SI = MBBI->succ_begin(), SE = MBBI->succ_end(); SI != SE; ++SI)
        NewLabel->copySuccessor(&*MBBI, SI);

// Add the new label as a successor to the new label (still need to check if this is needed)

// Transfer the rest of the  instructions to NewLabel
NewLabel->splice(NewLabel->begin(), &*MBBI, std::next(MI), MBBI->end());

And it seems to work?

I don’t think that copying the probability of the successors is the right way, but I could not figure any better than that.

Is there a better way to do it?

Have you looked into MachineInstr::setPostInstrSymbol as an alternative? It may not do what you want for whatever reason, but if you are mostly interested in the label rather than messing with control flow it’s probably simpler than creating a new block.

Thanks! 'll look into it.

My goal is to add return label as operand to call instructions.
Im creating a psudeo call instruction at first, than I add a label after that instruction, and use it as an operand to the instruction.

It worked!

I used it in a post-isel hook as @efriedma-quic suggested, and using the setPostInstrSymbol function that you recommended it seems to work perfectly.

Thanks a lot! :smiley: