aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Instructions.h
diff options
context:
space:
mode:
authorStepan Dyatkovskiy <stpworld@narod.ru>2012-02-01 07:49:51 +0000
committerStepan Dyatkovskiy <stpworld@narod.ru>2012-02-01 07:49:51 +0000
commit24473120a253a05f3601cd3373403b47e6d03d41 (patch)
treebc2bd5e85b17ffe6e39259aef743bad59f6c0508 /include/llvm/Instructions.h
parent11e43291540db9d885b736cbd652558faab80955 (diff)
downloadexternal_llvm-24473120a253a05f3601cd3373403b47e6d03d41.zip
external_llvm-24473120a253a05f3601cd3373403b47e6d03d41.tar.gz
external_llvm-24473120a253a05f3601cd3373403b47e6d03d41.tar.bz2
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149481 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Instructions.h')
-rw-r--r--include/llvm/Instructions.h85
1 files changed, 66 insertions, 19 deletions
diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h
index c27c6fa..bb6a6ff 100644
--- a/include/llvm/Instructions.h
+++ b/include/llvm/Instructions.h
@@ -24,6 +24,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ErrorHandling.h"
#include <iterator>
+#include <limits.h>
namespace llvm {
@@ -2467,6 +2468,9 @@ class SwitchInst : public TerminatorInst {
protected:
virtual SwitchInst *clone_impl() const;
public:
+
+ enum { ErrorIndex = UINT_MAX };
+
static SwitchInst *Create(Value *Value, BasicBlock *Default,
unsigned NumCases, Instruction *InsertBefore = 0) {
return new SwitchInst(Value, Default, NumCases, InsertBefore);
@@ -2488,34 +2492,62 @@ public:
return cast<BasicBlock>(getOperand(1));
}
- /// getNumCases - return the number of 'cases' in this switch instruction.
- /// Note that case #0 is always the default case.
+ void setDefaultDest(BasicBlock *DefaultCase) {
+ setOperand(1, reinterpret_cast<Value*>(DefaultCase));
+ }
+
+ /// getNumCases - return the number of 'cases' in this switch instruction,
+ /// except the default case
unsigned getNumCases() const {
- return getNumOperands()/2;
+ return getNumOperands()/2 - 1;
}
- /// getCaseValue - Return the specified case value. Note that case #0, the
- /// default destination, does not have a case value.
+ /// getCaseValue - Return the specified case value. Note that case #0, means
+ /// first case, not a default case.
ConstantInt *getCaseValue(unsigned i) {
- assert(i && i < getNumCases() && "Illegal case value to get!");
- return getSuccessorValue(i);
+ assert(i < getNumCases() && "Illegal case value to get!");
+ return reinterpret_cast<ConstantInt*>(getOperand(2 + i*2));
}
- /// getCaseValue - Return the specified case value. Note that case #0, the
- /// default destination, does not have a case value.
+ /// getCaseValue - Return the specified case value. Note that case #0, means
+ /// first case, not a default case.
const ConstantInt *getCaseValue(unsigned i) const {
- assert(i && i < getNumCases() && "Illegal case value to get!");
- return getSuccessorValue(i);
+ assert(i < getNumCases() && "Illegal case value to get!");
+ return reinterpret_cast<const ConstantInt*>(getOperand(2 + i*2));
+ }
+
+ // setSuccessorValue - Updates the value associated with the specified
+ // case.
+ void setCaseValue(unsigned i, ConstantInt *CaseValue) {
+ assert(i < getNumCases() && "Case index # out of range!");
+ setOperand(2 + i*2, reinterpret_cast<Value*>(CaseValue));
}
/// findCaseValue - Search all of the case values for the specified constant.
/// If it is explicitly handled, return the case number of it, otherwise
- /// return 0 to indicate that it is handled by the default handler.
+ /// return ErrorIndex to indicate that it is handled by the default handler.
unsigned findCaseValue(const ConstantInt *C) const {
- for (unsigned i = 1, e = getNumCases(); i != e; ++i)
+ for (unsigned i = 0, e = getNumCases(); i != e; ++i)
if (getCaseValue(i) == C)
return i;
- return 0;
+ return ErrorIndex;
+ }
+
+ /// resolveSuccessorIndex - Converts case index to index of its successor
+ /// index in TerminatorInst successors collection.
+ /// If CaseIndex == ErrorIndex, "default" successor will returned then.
+ unsigned resolveSuccessorIndex(unsigned CaseIndex) const {
+ assert((CaseIndex == ErrorIndex || CaseIndex < getNumCases()) &&
+ "Case index # out of range!");
+ return CaseIndex != ErrorIndex ? CaseIndex + 1 : 0;
+ }
+
+ /// resolveCaseIndex - Converts index of successor in TerminatorInst
+ /// collection to index of case that corresponds to this successor.
+ unsigned resolveCaseIndex(unsigned SuccessorIndex) const {
+ assert(SuccessorIndex < getNumSuccessors() &&
+ "Successor index # out of range!");
+ return SuccessorIndex != 0 ? SuccessorIndex - 1 : ErrorIndex;
}
/// findCaseDest - Finds the unique case value for a given successor. Returns
@@ -2524,8 +2556,8 @@ public:
if (BB == getDefaultDest()) return NULL;
ConstantInt *CI = NULL;
- for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
- if (getSuccessor(i) == BB) {
+ for (unsigned i = 0, e = getNumCases(); i != e; ++i) {
+ if (getSuccessor(i + 1) == BB) {
if (CI) return NULL; // Multiple cases lead to BB.
else CI = getCaseValue(i);
}
@@ -2537,9 +2569,8 @@ public:
///
void addCase(ConstantInt *OnVal, BasicBlock *Dest);
- /// removeCase - This method removes the specified successor from the switch
- /// instruction. Note that this cannot be used to remove the default
- /// destination (successor #0). Also note that this operation may reorder the
+ /// removeCase - This method removes the specified case and its successor
+ /// from the switch instruction. Note that this operation may reorder the
/// remaining cases at index idx and above.
///
void removeCase(unsigned idx);
@@ -2554,6 +2585,22 @@ public:
setOperand(idx*2+1, (Value*)NewSucc);
}
+ /// Resolves successor for idx-th case.
+ /// Use getCaseSuccessor instead of TerminatorInst::getSuccessor,
+ /// since internal SwitchInst organization of operands/successors is
+ /// hidden and may be changed in any moment.
+ BasicBlock *getCaseSuccessor(unsigned idx) const {
+ return getSuccessor(resolveSuccessorIndex(idx));
+ }
+
+ /// Set new successor for idx-th case.
+ /// Use setCaseSuccessor instead of TerminatorInst::setSuccessor,
+ /// since internal SwitchInst organization of operands/successors is
+ /// hidden and may be changed in any moment.
+ void setCaseSuccessor(unsigned idx, BasicBlock *NewSucc) {
+ setSuccessor(resolveSuccessorIndex(idx), NewSucc);
+ }
+
// getSuccessorValue - Return the value associated with the specified
// successor.
ConstantInt *getSuccessorValue(unsigned idx) const {