unstick r149486

Revision 149486

2012/02/01 08:00:22
Revision Log:
Compatability fix for SwitchInst refactoring.

The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.

What was done:

1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.

Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.



  • llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp

    776 776 // Change the default destination to go to one of the other destinations, if
    777 777 // there is any other dest.
    778 778 SwitchInst *SI = cast<SwitchInst>(IndirectGotoBlock->getTerminator());
    779 if (SI->getNumSuccessors() > 1)
    780 SI->setSuccessor(0, SI->getSuccessor(1));
    779 if (SI->getNumCases() > 0)
    780 SI->setDefaultDest(SI->getCaseSuccessor(0));
    781 781 }
    782 782
    783 783 // Remove any cached LLVM values that are local to this function. Such values
    1981 1981 TREE_VEC_LENGTH(Cases));
    1982 1982 EmitBlock(new BasicBlock(""));
    1983 1983 // Default location starts out as fall-through
    1984 SI->setSuccessor(0, Builder.GetInsertBlock());
    1984 SI->setDefaultDest(Builder.GetInsertBlock());
    1985 1985
    1986 1986 assert(!SWITCH_BODY(exp) && "not a gimple switch?");
    1987 1987
    2035 2035 }
    2036 2036
    2037 2037 if (DefaultDest)
    2038 if (SI->getSuccessor(0) == Builder.GetInsertBlock())
    2039 SI->setSuccessor(0, DefaultDest);
    2038 if (SI->getDefaultDest() == Builder.GetInsertBlock())
    2039 SI->setDefaultDest(DefaultDest);
    2040 2040 else {
    2041 2041 Builder.CreateBr(DefaultDest);
    2042 2042 // Emit a "fallthrough" block, which is almost certainly dead.