新しいものを挿入するには、すべての基本ブロックから後継者を削除する必要があります
このコードを試しましたが、うまくいきません
void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
assert(SuccNum < TI->getNumSuccessors() &&
"Trying to remove a nonexistant successor!");
// If our old successor block contains any PHI nodes, remove the entry in the
// PHI nodes that comes from this branch...
//
BasicBlock *BB = TI->getParent();
TI->getSuccessor(SuccNum)->removePredecessor(BB);
TerminatorInst *NewTI = 0;
switch (TI->getOpcode()) {
case Instruction::Br:
// If this is a conditional branch... convert to unconditional branch.
if (TI->getNumSuccessors() == 2) {
cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
} else { // Otherwise convert to a return instruction...
Value *RetVal = 0;
// Create a value to return... if the function doesn't return null...
if (!(BB->getParent()->getReturnType())->isVoidTy())
RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
// Create the return...
NewTI = 0;
}
break;
case Instruction::Invoke: // Should convert to call
case Instruction::Switch: // Should remove entry
default:
case Instruction::Ret: // Cannot happen, has no successors!
assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
abort();
}
if (NewTI) // If it's a different instruction, replace.
ReplaceInstWithInst(TI, NewTI);
}
結果の例:
if.then: ; preds = %for.inc, %entry, %for.body
preds
新しいサクセサーを挿入する前に%for.body
、のサクセサーを削除するに従って含めないでください%for.body