Refactor common initialization code in private init() functions.
[oota-llvm.git] / lib / VMCore / iSwitch.cpp
1 //===-- iSwitch.cpp - Implement the Switch instruction --------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Switch instruction...
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/iTerminators.h"
15 #include "llvm/BasicBlock.h"
16 using namespace llvm;
17
18 void SwitchInst::init(Value *Value, BasicBlock *Default)
19 {
20   assert(Value && Default);
21   Operands.push_back(Use(Value, this));
22   Operands.push_back(Use(Default, this));
23 }
24
25 SwitchInst::SwitchInst(Value *V, BasicBlock *D,
26                        Instruction *InsertBefore) 
27   : TerminatorInst(Instruction::Switch, InsertBefore) {
28   init(V, D);
29 }
30
31 SwitchInst::SwitchInst(Value *V, BasicBlock *D, BasicBlock *InsertAtEnd) 
32   : TerminatorInst(Instruction::Switch, InsertAtEnd) {
33   init(V, D);
34 }
35
36 SwitchInst::SwitchInst(const SwitchInst &SI) 
37   : TerminatorInst(Instruction::Switch) {
38   Operands.reserve(SI.Operands.size());
39
40   for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
41     Operands.push_back(Use(SI.Operands[i], this));
42     Operands.push_back(Use(SI.Operands[i+1], this));
43   }
44 }
45
46 /// addCase - Add an entry to the switch instruction...
47 ///
48 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
49   Operands.push_back(Use((Value*)OnVal, this));
50   Operands.push_back(Use((Value*)Dest, this));
51 }
52
53 /// removeCase - This method removes the specified successor from the switch
54 /// instruction.  Note that this cannot be used to remove the default
55 /// destination (successor #0).
56 ///
57 void SwitchInst::removeCase(unsigned idx) {
58   assert(idx != 0 && "Cannot remove the default case!");
59   assert(idx*2 < Operands.size() && "Successor index out of range!!!");
60   Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);  
61 }