1 //===-- iSwitch.cpp - Implement the Switch instruction -----------*- C++ -*--=//
3 // This file implements the Switch instruction...
5 //===----------------------------------------------------------------------===//
7 #include "llvm/iTerminators.h"
8 #include "llvm/BasicBlock.h"
10 #include "llvm/Type.h"
13 SwitchInst::SwitchInst(Value *V, BasicBlock *DefV)
14 : TerminatorInst(Instruction::Switch),
15 DefaultDest(DefV, this), Val(V, this) {
19 SwitchInst::SwitchInst(const SwitchInst &SI)
20 : TerminatorInst(Instruction::Switch), DefaultDest(SI.DefaultDest),
23 for (dest_const_iterator I = SI.Destinations.begin(),
24 end = SI.Destinations.end(); I != end; ++I)
25 Destinations.push_back(dest_value(ConstPoolUse(I->first, this),
26 BasicBlockUse(I->second, this)));
30 void SwitchInst::dest_push_back(ConstPoolVal *OnVal, BasicBlock *Dest) {
31 Destinations.push_back(dest_value(ConstPoolUse(OnVal, this),
32 BasicBlockUse(Dest, this)));
35 void SwitchInst::dropAllReferences() {
41 const BasicBlock *SwitchInst::getSuccessor(unsigned idx) const {
42 if (idx == 0) return DefaultDest;
43 if (idx > Destinations.size()) return 0;
44 return Destinations[idx-1].second;
47 unsigned SwitchInst::getNumOperands() const {
48 return 2+Destinations.size();
51 const Value *SwitchInst::getOperand(unsigned i) const {
52 if (i == 0) return Val;
53 else if (i == 1) return DefaultDest;
55 unsigned slot = (i-2) >> 1;
56 if (slot >= Destinations.size()) return 0;
58 if (i & 1) return Destinations[slot].second;
59 return Destinations[slot].first;
62 bool SwitchInst::setOperand(unsigned i, Value *V) {
63 if (i == 0) { Val = V; return true; }
65 assert(V->getType() == Type::LabelTy);
66 DefaultDest = (BasicBlock*)V;
70 unsigned slot = (i-2) >> 1;
71 if (slot >= Destinations.size()) return 0;
74 assert(V->getType() == Type::LabelTy);
75 Destinations[slot].second = (BasicBlock*)V;
77 // TODO: assert constant
78 Destinations[slot].first = (ConstPoolVal*)V;