Taken into account Duncan's comments for r149481 dated by 2nd Feb 2012:

http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120130/136146.html

Implemented CaseIterator and it solves almost all described issues: we don't need to mix operand/case/successor indexing anymore. Base iterator class is implemented as a template since it may be initialized either from "const SwitchInst*" or from "SwitchInst*".

ConstCaseIt is just a read-only iterator.
CaseIt is read-write iterator; it allows to change case successor and case value.

Usage of iterator allows totally remove resolveXXXX methods. All indexing convertions done automatically inside the iterator's getters.

Main way of iterator usage looks like this:
SwitchInst *SI = ... // intialize it somehow

for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i) {
  BasicBlock *BB = i.getCaseSuccessor();
  ConstantInt *V = i.getCaseValue();
  // Do something.
}

If you want to convert case number to TerminatorInst successor index, just use getSuccessorIndex iterator's method.
If you want initialize iterator from TerminatorInst successor index, use CaseIt::fromSuccessorIndex(...) method.

There are also related changes in llvm-clients: klee and clang.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152297 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Stepan Dyatkovskiy
2012-03-08 07:06:20 +00:00
parent 88d2fa438a
commit c10fa6c801
25 changed files with 302 additions and 211 deletions

View File

@ -106,31 +106,32 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions) {
// If we are switching on a constant, we can convert the switch into a
// single branch instruction!
ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
BasicBlock *TheOnlyDest = SI->getDefaultDest(); // The default dest
BasicBlock *TheOnlyDest = SI->getDefaultDest();
BasicBlock *DefaultDest = TheOnlyDest;
// Figure out which case it goes to.
for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
i != e; ++i) {
// Found case matching a constant operand?
if (SI->getCaseValue(i) == CI) {
TheOnlyDest = SI->getCaseSuccessor(i);
if (i.getCaseValue() == CI) {
TheOnlyDest = i.getCaseSuccessor();
break;
}
// Check to see if this branch is going to the same place as the default
// dest. If so, eliminate it as an explicit compare.
if (SI->getCaseSuccessor(i) == DefaultDest) {
if (i.getCaseSuccessor() == DefaultDest) {
// Remove this entry.
DefaultDest->removePredecessor(SI->getParent());
SI->removeCase(i);
--i; --e; // Don't skip an entry...
--i; --e;
continue;
}
// Otherwise, check to see if the switch only branches to one destination.
// We do this by reseting "TheOnlyDest" to null when we find two non-equal
// destinations.
if (SI->getCaseSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = 0;
}
if (CI && !TheOnlyDest) {
@ -167,11 +168,13 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions) {
if (SI->getNumCases() == 1) {
// Otherwise, we can fold this switch into a conditional branch
// instruction if it has only one non-default destination.
SwitchInst::CaseIt FirstCase = SI->caseBegin();
Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
SI->getCaseValue(0), "cond");
FirstCase.getCaseValue(), "cond");
// Insert the new branch.
Builder.CreateCondBr(Cond, SI->getCaseSuccessor(0), SI->getDefaultDest());
Builder.CreateCondBr(Cond, FirstCase.getCaseSuccessor(),
SI->getDefaultDest());
// Delete the old switch.
SI->eraseFromParent();