Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- iSwitch.cpp - Implement the Switch instruction --------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the Switch instruction... |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/iTerminators.h" |
| 15 | #include "llvm/BasicBlock.h" |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame^] | 16 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 18 | SwitchInst::SwitchInst(Value *V, BasicBlock *DefaultDest, |
| 19 | Instruction *InsertBefore) |
| 20 | : TerminatorInst(Instruction::Switch, InsertBefore) { |
| 21 | assert(V && DefaultDest); |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 22 | Operands.push_back(Use(V, this)); |
Chris Lattner | 2aa8311 | 2002-09-10 15:45:53 +0000 | [diff] [blame] | 23 | Operands.push_back(Use(DefaultDest, this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Chris Lattner | 4b74c83 | 2003-11-20 17:45:12 +0000 | [diff] [blame^] | 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)); |
| 32 | } |
| 33 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 35 | : TerminatorInst(Instruction::Switch) { |
| 36 | Operands.reserve(SI.Operands.size()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | |
Chris Lattner | c8b25d4 | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 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)); |
| 41 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 131d19f | 2003-08-23 23:14:37 +0000 | [diff] [blame] | 44 | /// addCase - Add an entry to the switch instruction... |
| 45 | /// |
| 46 | void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) { |
Chris Lattner | 991bc4e | 2002-04-09 18:37:08 +0000 | [diff] [blame] | 47 | Operands.push_back(Use((Value*)OnVal, this)); |
| 48 | Operands.push_back(Use((Value*)Dest, this)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | } |
Chris Lattner | 131d19f | 2003-08-23 23:14:37 +0000 | [diff] [blame] | 50 | |
| 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). |
| 54 | /// |
| 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); |
| 59 | } |