Two minor improvements:

1. Get rid of the silly abort block.  When doing bb extraction, we get one
     abort block for every block extracted, which is kinda annoying.
  2. If the switch ends up having a single destination, turn it into an
     unconditional branch.

I would like to add support for conditional branches, but to do this we will
want to have the function return a bool instead of a ushort.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13478 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2004-05-12 03:22:33 +00:00
parent b83c0f3f63
commit 5b01e298ed

View File

@@ -390,24 +390,20 @@ CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
} }
} }
// Now that we've done the deed, make the default destination of the switch // Now that we've done the deed, simplify the switch instruction.
// instruction be a block with a call to abort() -- since this path should not unsigned NumSuccs = TheSwitch->getNumSuccessors();
// be taken, this will abort sooner rather than later. if (NumSuccs > 1) {
if (TheSwitch->getNumSuccessors() > 1) { if (NumSuccs-1 == 1) {
Function *container = codeReplacer->getParent(); // Only a single destination, change the switch into an unconditional
BasicBlock *abortBB = new BasicBlock("abortBlock", container); // branch.
std::vector<const Type*> paramTypes; new BranchInst(TheSwitch->getSuccessor(1), TheSwitch);
FunctionType *abortTy = FunctionType::get(Type::VoidTy, paramTypes, false); TheSwitch->getParent()->getInstList().erase(TheSwitch);
Function *abortFunc = } else {
container->getParent()->getOrInsertFunction("abort", abortTy); // Otherwise, make the default destination of the switch instruction be
abortBB->getInstList().push_back(new CallInst(abortFunc)); // one of the other successors.
Function *ParentFunc = TheSwitch->getParent()->getParent(); TheSwitch->setSuccessor(0, TheSwitch->getSuccessor(NumSuccs-1));
if (ParentFunc->getReturnType() == Type::VoidTy) TheSwitch->removeCase(NumSuccs-1); // Remove redundant case
new ReturnInst(0, abortBB); }
else
new ReturnInst(Constant::getNullValue(ParentFunc->getReturnType()),
abortBB);
TheSwitch->setSuccessor(0, abortBB);
} else { } else {
// There is only 1 successor (the block containing the switch itself), which // There is only 1 successor (the block containing the switch itself), which
// means that previously this was the last part of the function, and hence // means that previously this was the last part of the function, and hence