0ffe45ff922806356e61d941422aeffc0bfe2e7d
[oota-llvm.git] / lib / VMCore / iSwitch.cpp
1 //===-- iSwitch.cpp - Implement the Switch instruction --------------------===//
2 //
3 // This file implements the Switch instruction...
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iTerminators.h"
8 #include "llvm/BasicBlock.h"
9
10 SwitchInst::SwitchInst(Value *V, BasicBlock *DefaultDest,
11                        Instruction *InsertBefore) 
12   : TerminatorInst(Instruction::Switch, InsertBefore) {
13   assert(V && DefaultDest);
14   Operands.push_back(Use(V, this));
15   Operands.push_back(Use(DefaultDest, this));
16 }
17
18 SwitchInst::SwitchInst(const SwitchInst &SI) 
19   : TerminatorInst(Instruction::Switch) {
20   Operands.reserve(SI.Operands.size());
21
22   for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
23     Operands.push_back(Use(SI.Operands[i], this));
24     Operands.push_back(Use(SI.Operands[i+1], this));
25   }
26 }
27
28 /// addCase - Add an entry to the switch instruction...
29 ///
30 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
31   Operands.push_back(Use((Value*)OnVal, this));
32   Operands.push_back(Use((Value*)Dest, this));
33 }
34
35 /// removeCase - This method removes the specified successor from the switch
36 /// instruction.  Note that this cannot be used to remove the default
37 /// destination (successor #0).
38 ///
39 void SwitchInst::removeCase(unsigned idx) {
40   assert(idx != 0 && "Cannot remove the default case!");
41   assert(idx*2 < Operands.size() && "Successor index out of range!!!");
42   Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);  
43 }