Implement == and != correctly. Before they would incorrectly return !=
[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
17 namespace llvm {
18
19 SwitchInst::SwitchInst(Value *V, BasicBlock *DefaultDest,
20                        Instruction *InsertBefore) 
21   : TerminatorInst(Instruction::Switch, InsertBefore) {
22   assert(V && DefaultDest);
23   Operands.push_back(Use(V, this));
24   Operands.push_back(Use(DefaultDest, this));
25 }
26
27 SwitchInst::SwitchInst(const SwitchInst &SI) 
28   : TerminatorInst(Instruction::Switch) {
29   Operands.reserve(SI.Operands.size());
30
31   for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
32     Operands.push_back(Use(SI.Operands[i], this));
33     Operands.push_back(Use(SI.Operands[i+1], this));
34   }
35 }
36
37 /// addCase - Add an entry to the switch instruction...
38 ///
39 void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
40   Operands.push_back(Use((Value*)OnVal, this));
41   Operands.push_back(Use((Value*)Dest, this));
42 }
43
44 /// removeCase - This method removes the specified successor from the switch
45 /// instruction.  Note that this cannot be used to remove the default
46 /// destination (successor #0).
47 ///
48 void SwitchInst::removeCase(unsigned idx) {
49   assert(idx != 0 && "Cannot remove the default case!");
50   assert(idx*2 < Operands.size() && "Successor index out of range!!!");
51   Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);  
52 }
53
54 } // End llvm namespace