1 //===-- iSwitch.cpp - Implement the Switch instruction --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Switch instruction...
12 //===----------------------------------------------------------------------===//
14 #include "llvm/iTerminators.h"
15 #include "llvm/BasicBlock.h"
18 SwitchInst::SwitchInst(Value *V, BasicBlock *DefaultDest,
19 Instruction *InsertBefore)
20 : TerminatorInst(Instruction::Switch, InsertBefore) {
21 assert(V && DefaultDest);
22 Operands.push_back(Use(V, this));
23 Operands.push_back(Use(DefaultDest, this));
26 SwitchInst::SwitchInst(Value *V, BasicBlock *DefaultDest,
27 BasicBlock *InsertAtEnd)
28 : TerminatorInst(Instruction::Switch, InsertAtEnd) {
29 assert(V && DefaultDest);
30 Operands.push_back(Use(V, this));
31 Operands.push_back(Use(DefaultDest, this));
34 SwitchInst::SwitchInst(const SwitchInst &SI)
35 : TerminatorInst(Instruction::Switch) {
36 Operands.reserve(SI.Operands.size());
38 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
39 Operands.push_back(Use(SI.Operands[i], this));
40 Operands.push_back(Use(SI.Operands[i+1], this));
44 /// addCase - Add an entry to the switch instruction...
46 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
47 Operands.push_back(Use((Value*)OnVal, this));
48 Operands.push_back(Use((Value*)Dest, this));
51 /// removeCase - This method removes the specified successor from the switch
52 /// instruction. Note that this cannot be used to remove the default
53 /// destination (successor #0).
55 void SwitchInst::removeCase(unsigned idx) {
56 assert(idx != 0 && "Cannot remove the default case!");
57 assert(idx*2 < Operands.size() && "Successor index out of range!!!");
58 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);