- static SwitchInst *Create(Value *Value, BasicBlock *Default,
- unsigned NumCases, Instruction *InsertBefore = 0) {
- return new SwitchInst(Value, Default, NumCases, InsertBefore);
- }
- static SwitchInst *Create(Value *Value, BasicBlock *Default,
- unsigned NumCases, BasicBlock *InsertAtEnd) {
- return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
- }
-
- ~SwitchInst();
-
- /// Provide fast operand accessors
- DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
-
- // Accessor Methods for Switch stmt
- Value *getCondition() const { return getOperand(0); }
- void setCondition(Value *V) { setOperand(0, V); }
-
- BasicBlock *getDefaultDest() const {
- return cast<BasicBlock>(getOperand(1));
- }
-
- 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 - 1;
- }
-
- /// Returns a read/write iterator that points to the first
- /// case in SwitchInst.
- CaseIt caseBegin() {
- return CaseIt(this, 0);
- }
- /// Returns a read-only iterator that points to the first
- /// case in the SwitchInst.
- ConstCaseIt caseBegin() const {
- return ConstCaseIt(this, 0);
- }
-
- /// Returns a read/write iterator that points one past the last
- /// in the SwitchInst.
- CaseIt caseEnd() {
- return CaseIt(this, getNumCases());
- }
- /// Returns a read-only iterator that points one past the last
- /// in the SwitchInst.
- ConstCaseIt caseEnd() const {
- return ConstCaseIt(this, getNumCases());
- }
- /// Returns an iterator that points to default case.
- /// Note: this iterator allows to resolve successor only. Attempt
- /// to resolve case value causes an assertion.
- CaseIt caseDefault() {
- return CaseIt(this, DefaultPseudoIndex);
- }
- ConstCaseIt caseDefault() const {
- return ConstCaseIt(this, DefaultPseudoIndex);
- }
-
- /// findCaseValue - Search all of the case values for the specified constant.
- /// If it is explicitly handled, return the case iterator of it, otherwise
- /// return default case iterator to indicate
- /// that it is handled by the default handler.
- CaseIt findCaseValue(const ConstantInt *C) {
- for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
- if (i.getCaseValue() == C)
- return i;
- return caseDefault();
- }
- ConstCaseIt findCaseValue(const ConstantInt *C) const {
- for (ConstCaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
- if (i.getCaseValue() == C)
- return i;
- return caseDefault();
- }
-
- /// findCaseDest - Finds the unique case value for a given successor. Returns
- /// null if the successor is not found, not unique, or is the default case.
- ConstantInt *findCaseDest(BasicBlock *BB) {
- if (BB == getDefaultDest()) return NULL;
-
- ConstantInt *CI = NULL;
- for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i) {
- if (i.getCaseSuccessor() == BB) {
- if (CI) return NULL; // Multiple cases lead to BB.
- else CI = i.getCaseValue();
- }
- }
- return CI;
- }
-
- /// addCase - Add an entry to the switch instruction...
- ///
- void addCase(ConstantInt *OnVal, BasicBlock *Dest);
-
- /// 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(CaseIt i);
-
- unsigned getNumSuccessors() const { return getNumOperands()/2; }
- BasicBlock *getSuccessor(unsigned idx) const {
- assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
- return cast<BasicBlock>(getOperand(idx*2+1));
- }
- void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
- assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
- setOperand(idx*2+1, (Value*)NewSucc);
- }
-