Remove unncessary include
[oota-llvm.git] / include / llvm / Instructions.h
1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
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 exposes the class definitions of all of the subclasses of the
11 // Instruction class.  This is meant to be an easy way to get access to all
12 // instruction subclasses.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
18
19 #include "llvm/Instruction.h"
20 #include "llvm/InstrTypes.h"
21
22 namespace llvm {
23
24 class BasicBlock;
25 class ConstantInt;
26 class PointerType;
27 class PackedType;
28
29 //===----------------------------------------------------------------------===//
30 //                             AllocationInst Class
31 //===----------------------------------------------------------------------===//
32
33 /// AllocationInst - This class is the common base class of MallocInst and
34 /// AllocaInst.
35 ///
36 class AllocationInst : public UnaryInstruction {
37   unsigned Alignment;
38 protected:
39   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
40                  const std::string &Name = "", Instruction *InsertBefore = 0);
41   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
42                  const std::string &Name, BasicBlock *InsertAtEnd);
43
44 public:
45
46   /// isArrayAllocation - Return true if there is an allocation size parameter
47   /// to the allocation instruction that is not 1.
48   ///
49   bool isArrayAllocation() const;
50
51   /// getArraySize - Get the number of element allocated, for a simple
52   /// allocation of a single element, this will return a constant 1 value.
53   ///
54   inline const Value *getArraySize() const { return getOperand(0); }
55   inline Value *getArraySize() { return getOperand(0); }
56
57   /// getType - Overload to return most specific pointer type
58   ///
59   inline const PointerType *getType() const {
60     return reinterpret_cast<const PointerType*>(Instruction::getType());
61   }
62
63   /// getAllocatedType - Return the type that is being allocated by the
64   /// instruction.
65   ///
66   const Type *getAllocatedType() const;
67
68   /// getAlignment - Return the alignment of the memory that is being allocated
69   /// by the instruction.
70   ///
71   unsigned getAlignment() const { return Alignment; }
72   void setAlignment(unsigned Align) {
73     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
74     Alignment = Align;
75   }
76   
77   virtual Instruction *clone() const = 0;
78
79   // Methods for support type inquiry through isa, cast, and dyn_cast:
80   static inline bool classof(const AllocationInst *) { return true; }
81   static inline bool classof(const Instruction *I) {
82     return I->getOpcode() == Instruction::Alloca ||
83            I->getOpcode() == Instruction::Malloc;
84   }
85   static inline bool classof(const Value *V) {
86     return isa<Instruction>(V) && classof(cast<Instruction>(V));
87   }
88 };
89
90
91 //===----------------------------------------------------------------------===//
92 //                                MallocInst Class
93 //===----------------------------------------------------------------------===//
94
95 /// MallocInst - an instruction to allocated memory on the heap
96 ///
97 class MallocInst : public AllocationInst {
98   MallocInst(const MallocInst &MI);
99 public:
100   explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101                       const std::string &Name = "",
102                       Instruction *InsertBefore = 0)
103     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
104   MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
105              BasicBlock *InsertAtEnd)
106     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
107   MallocInst(const Type *Ty, Value *ArraySize, unsigned Align, 
108              const std::string &Name, BasicBlock *InsertAtEnd)
109   : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
110   explicit MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
111                       const std::string &Name = "",
112                       Instruction *InsertBefore = 0)
113   : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
114   
115   virtual MallocInst *clone() const;
116
117   // Methods for support type inquiry through isa, cast, and dyn_cast:
118   static inline bool classof(const MallocInst *) { return true; }
119   static inline bool classof(const Instruction *I) {
120     return (I->getOpcode() == Instruction::Malloc);
121   }
122   static inline bool classof(const Value *V) {
123     return isa<Instruction>(V) && classof(cast<Instruction>(V));
124   }
125 };
126
127
128 //===----------------------------------------------------------------------===//
129 //                                AllocaInst Class
130 //===----------------------------------------------------------------------===//
131
132 /// AllocaInst - an instruction to allocate memory on the stack
133 ///
134 class AllocaInst : public AllocationInst {
135   AllocaInst(const AllocaInst &);
136 public:
137   explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
138                       const std::string &Name = "",
139                       Instruction *InsertBefore = 0)
140     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
141   AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
142              BasicBlock *InsertAtEnd)
143     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
144   AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
145              const std::string &Name, BasicBlock *InsertAtEnd)
146   : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
147   explicit AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
148                       const std::string &Name = "",
149                       Instruction *InsertBefore = 0)
150   : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
151   
152   virtual AllocaInst *clone() const;
153
154   // Methods for support type inquiry through isa, cast, and dyn_cast:
155   static inline bool classof(const AllocaInst *) { return true; }
156   static inline bool classof(const Instruction *I) {
157     return (I->getOpcode() == Instruction::Alloca);
158   }
159   static inline bool classof(const Value *V) {
160     return isa<Instruction>(V) && classof(cast<Instruction>(V));
161   }
162 };
163
164
165 //===----------------------------------------------------------------------===//
166 //                                 FreeInst Class
167 //===----------------------------------------------------------------------===//
168
169 /// FreeInst - an instruction to deallocate memory
170 ///
171 class FreeInst : public UnaryInstruction {
172   void AssertOK();
173 public:
174   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
175   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
176
177   virtual FreeInst *clone() const;
178
179   virtual bool mayWriteToMemory() const { return true; }
180
181   // Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const FreeInst *) { return true; }
183   static inline bool classof(const Instruction *I) {
184     return (I->getOpcode() == Instruction::Free);
185   }
186   static inline bool classof(const Value *V) {
187     return isa<Instruction>(V) && classof(cast<Instruction>(V));
188   }
189 };
190
191
192 //===----------------------------------------------------------------------===//
193 //                                LoadInst Class
194 //===----------------------------------------------------------------------===//
195
196 /// LoadInst - an instruction for reading from memory.  This uses the
197 /// SubclassData field in Value to store whether or not the load is volatile.
198 ///
199 class LoadInst : public UnaryInstruction {
200   LoadInst(const LoadInst &LI)
201     : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
202     setVolatile(LI.isVolatile());
203
204 #ifndef NDEBUG
205     AssertOK();
206 #endif
207   }
208   void AssertOK();
209 public:
210   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
211   LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
212   LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
213            Instruction *InsertBefore = 0);
214   LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
215            BasicBlock *InsertAtEnd);
216
217   /// isVolatile - Return true if this is a load from a volatile memory
218   /// location.
219   ///
220   bool isVolatile() const { return SubclassData; }
221
222   /// setVolatile - Specify whether this is a volatile load or not.
223   ///
224   void setVolatile(bool V) { SubclassData = V; }
225
226   virtual LoadInst *clone() const;
227
228   virtual bool mayWriteToMemory() const { return isVolatile(); }
229
230   Value *getPointerOperand() { return getOperand(0); }
231   const Value *getPointerOperand() const { return getOperand(0); }
232   static unsigned getPointerOperandIndex() { return 0U; }
233
234   // Methods for support type inquiry through isa, cast, and dyn_cast:
235   static inline bool classof(const LoadInst *) { return true; }
236   static inline bool classof(const Instruction *I) {
237     return I->getOpcode() == Instruction::Load;
238   }
239   static inline bool classof(const Value *V) {
240     return isa<Instruction>(V) && classof(cast<Instruction>(V));
241   }
242 };
243
244
245 //===----------------------------------------------------------------------===//
246 //                                StoreInst Class
247 //===----------------------------------------------------------------------===//
248
249 /// StoreInst - an instruction for storing to memory
250 ///
251 class StoreInst : public Instruction {
252   Use Ops[2];
253   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
254     Ops[0].init(SI.Ops[0], this);
255     Ops[1].init(SI.Ops[1], this);
256     setVolatile(SI.isVolatile());
257 #ifndef NDEBUG
258     AssertOK();
259 #endif
260   }
261   void AssertOK();
262 public:
263   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
264   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
265   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
266             Instruction *InsertBefore = 0);
267   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
268
269
270   /// isVolatile - Return true if this is a load from a volatile memory
271   /// location.
272   ///
273   bool isVolatile() const { return SubclassData; }
274
275   /// setVolatile - Specify whether this is a volatile load or not.
276   ///
277   void setVolatile(bool V) { SubclassData = V; }
278
279   /// Transparently provide more efficient getOperand methods.
280   Value *getOperand(unsigned i) const {
281     assert(i < 2 && "getOperand() out of range!");
282     return Ops[i];
283   }
284   void setOperand(unsigned i, Value *Val) {
285     assert(i < 2 && "setOperand() out of range!");
286     Ops[i] = Val;
287   }
288   unsigned getNumOperands() const { return 2; }
289
290
291   virtual StoreInst *clone() const;
292
293   virtual bool mayWriteToMemory() const { return true; }
294
295   Value *getPointerOperand() { return getOperand(1); }
296   const Value *getPointerOperand() const { return getOperand(1); }
297   static unsigned getPointerOperandIndex() { return 1U; }
298
299   // Methods for support type inquiry through isa, cast, and dyn_cast:
300   static inline bool classof(const StoreInst *) { return true; }
301   static inline bool classof(const Instruction *I) {
302     return I->getOpcode() == Instruction::Store;
303   }
304   static inline bool classof(const Value *V) {
305     return isa<Instruction>(V) && classof(cast<Instruction>(V));
306   }
307 };
308
309
310 //===----------------------------------------------------------------------===//
311 //                             GetElementPtrInst Class
312 //===----------------------------------------------------------------------===//
313
314 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
315 /// access elements of arrays and structs
316 ///
317 class GetElementPtrInst : public Instruction {
318   GetElementPtrInst(const GetElementPtrInst &GEPI)
319     : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
320                   0, GEPI.getNumOperands()) {
321     Use *OL = OperandList = new Use[NumOperands];
322     Use *GEPIOL = GEPI.OperandList;
323     for (unsigned i = 0, E = NumOperands; i != E; ++i)
324       OL[i].init(GEPIOL[i], this);
325   }
326   void init(Value *Ptr, const std::vector<Value*> &Idx);
327   void init(Value *Ptr, Value *Idx0, Value *Idx1);
328   void init(Value *Ptr, Value *Idx);
329 public:
330   /// Constructors - Create a getelementptr instruction with a base pointer an
331   /// list of indices.  The first ctor can optionally insert before an existing
332   /// instruction, the second appends the new instruction to the specified
333   /// BasicBlock.
334   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
335                     const std::string &Name = "", Instruction *InsertBefore =0);
336   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
337                     const std::string &Name, BasicBlock *InsertAtEnd);
338
339   /// Constructors - These two constructors are convenience methods because one
340   /// and two index getelementptr instructions are so common.
341   GetElementPtrInst(Value *Ptr, Value *Idx,
342                     const std::string &Name = "", Instruction *InsertBefore =0);
343   GetElementPtrInst(Value *Ptr, Value *Idx,
344                     const std::string &Name, BasicBlock *InsertAtEnd);
345   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
346                     const std::string &Name = "", Instruction *InsertBefore =0);
347   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
348                     const std::string &Name, BasicBlock *InsertAtEnd);
349   ~GetElementPtrInst();
350
351   virtual GetElementPtrInst *clone() const;
352
353   // getType - Overload to return most specific pointer type...
354   inline const PointerType *getType() const {
355     return reinterpret_cast<const PointerType*>(Instruction::getType());
356   }
357
358   /// getIndexedType - Returns the type of the element that would be loaded with
359   /// a load instruction with the specified parameters.
360   ///
361   /// A null type is returned if the indices are invalid for the specified
362   /// pointer type.
363   ///
364   static const Type *getIndexedType(const Type *Ptr,
365                                     const std::vector<Value*> &Indices,
366                                     bool AllowStructLeaf = false);
367   static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
368                                     bool AllowStructLeaf = false);
369   static const Type *getIndexedType(const Type *Ptr, Value *Idx);
370
371   inline op_iterator       idx_begin()       { return op_begin()+1; }
372   inline const_op_iterator idx_begin() const { return op_begin()+1; }
373   inline op_iterator       idx_end()         { return op_end(); }
374   inline const_op_iterator idx_end()   const { return op_end(); }
375
376   Value *getPointerOperand() {
377     return getOperand(0);
378   }
379   const Value *getPointerOperand() const {
380     return getOperand(0);
381   }
382   static unsigned getPointerOperandIndex() {
383     return 0U;                      // get index for modifying correct operand
384   }
385
386   inline unsigned getNumIndices() const {  // Note: always non-negative
387     return getNumOperands() - 1;
388   }
389
390   inline bool hasIndices() const {
391     return getNumOperands() > 1;
392   }
393
394   // Methods for support type inquiry through isa, cast, and dyn_cast:
395   static inline bool classof(const GetElementPtrInst *) { return true; }
396   static inline bool classof(const Instruction *I) {
397     return (I->getOpcode() == Instruction::GetElementPtr);
398   }
399   static inline bool classof(const Value *V) {
400     return isa<Instruction>(V) && classof(cast<Instruction>(V));
401   }
402 };
403
404 //===----------------------------------------------------------------------===//
405 //                            SetCondInst Class
406 //===----------------------------------------------------------------------===//
407
408 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
409 /// le, or ge.
410 ///
411 class SetCondInst : public BinaryOperator {
412 public:
413   SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
414               const std::string &Name = "", Instruction *InsertBefore = 0);
415   SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
416               const std::string &Name, BasicBlock *InsertAtEnd);
417
418   /// getInverseCondition - Return the inverse of the current condition opcode.
419   /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
420   ///
421   BinaryOps getInverseCondition() const {
422     return getInverseCondition(getOpcode());
423   }
424
425   /// getInverseCondition - Static version that you can use without an
426   /// instruction available.
427   ///
428   static BinaryOps getInverseCondition(BinaryOps Opcode);
429
430   /// getSwappedCondition - Return the condition opcode that would be the result
431   /// of exchanging the two operands of the setcc instruction without changing
432   /// the result produced.  Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
433   ///
434   BinaryOps getSwappedCondition() const {
435     return getSwappedCondition(getOpcode());
436   }
437
438   /// getSwappedCondition - Static version that you can use without an
439   /// instruction available.
440   ///
441   static BinaryOps getSwappedCondition(BinaryOps Opcode);
442
443
444   // Methods for support type inquiry through isa, cast, and dyn_cast:
445   static inline bool classof(const SetCondInst *) { return true; }
446   static inline bool classof(const Instruction *I) {
447     return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
448            I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
449            I->getOpcode() == SetLT || I->getOpcode() == SetGT;
450   }
451   static inline bool classof(const Value *V) {
452     return isa<Instruction>(V) && classof(cast<Instruction>(V));
453   }
454 };
455
456 //===----------------------------------------------------------------------===//
457 //                                 CastInst Class
458 //===----------------------------------------------------------------------===//
459
460 /// CastInst - This class represents a cast from Operand[0] to the type of
461 /// the instruction (i->getType()).
462 ///
463 class CastInst : public UnaryInstruction {
464   CastInst(const CastInst &CI)
465     : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
466   }
467 public:
468   CastInst(Value *S, const Type *Ty, const std::string &Name = "",
469            Instruction *InsertBefore = 0)
470     : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
471   }
472   CastInst(Value *S, const Type *Ty, const std::string &Name,
473            BasicBlock *InsertAtEnd)
474     : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
475   }
476
477   virtual CastInst *clone() const;
478
479   // Methods for support type inquiry through isa, cast, and dyn_cast:
480   static inline bool classof(const CastInst *) { return true; }
481   static inline bool classof(const Instruction *I) {
482     return I->getOpcode() == Cast;
483   }
484   static inline bool classof(const Value *V) {
485     return isa<Instruction>(V) && classof(cast<Instruction>(V));
486   }
487 };
488
489
490 //===----------------------------------------------------------------------===//
491 //                                 CallInst Class
492 //===----------------------------------------------------------------------===//
493
494 /// CallInst - This class represents a function call, abstracting a target
495 /// machine's calling convention.  This class uses low bit of the SubClassData
496 /// field to indicate whether or not this is a tail call.  The rest of the bits
497 /// hold the calling convention of the call.
498 ///
499 class CallInst : public Instruction {
500   CallInst(const CallInst &CI);
501   void init(Value *Func, const std::vector<Value*> &Params);
502   void init(Value *Func, Value *Actual1, Value *Actual2);
503   void init(Value *Func, Value *Actual);
504   void init(Value *Func);
505
506 public:
507   CallInst(Value *F, const std::vector<Value*> &Par,
508            const std::string &Name = "", Instruction *InsertBefore = 0);
509   CallInst(Value *F, const std::vector<Value*> &Par,
510            const std::string &Name, BasicBlock *InsertAtEnd);
511
512   // Alternate CallInst ctors w/ two actuals, w/ one actual and no
513   // actuals, respectively.
514   CallInst(Value *F, Value *Actual1, Value *Actual2,
515            const std::string& Name = "", Instruction *InsertBefore = 0);
516   CallInst(Value *F, Value *Actual1, Value *Actual2,
517            const std::string& Name, BasicBlock *InsertAtEnd);
518   CallInst(Value *F, Value *Actual, const std::string& Name = "",
519            Instruction *InsertBefore = 0);
520   CallInst(Value *F, Value *Actual, const std::string& Name,
521            BasicBlock *InsertAtEnd);
522   explicit CallInst(Value *F, const std::string &Name = "",
523                     Instruction *InsertBefore = 0);
524   explicit CallInst(Value *F, const std::string &Name,
525                     BasicBlock *InsertAtEnd);
526   ~CallInst();
527
528   virtual CallInst *clone() const;
529   bool mayWriteToMemory() const { return true; }
530
531   bool isTailCall() const           { return SubclassData & 1; }
532   void setTailCall(bool isTailCall = true) {
533     SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
534   }
535
536   /// getCallingConv/setCallingConv - Get or set the calling convention of this
537   /// function call.
538   unsigned getCallingConv() const { return SubclassData >> 1; }
539   void setCallingConv(unsigned CC) {
540     SubclassData = (SubclassData & 1) | (CC << 1);
541   }
542
543   /// getCalledFunction - Return the function being called by this instruction
544   /// if it is a direct call.  If it is a call through a function pointer,
545   /// return null.
546   Function *getCalledFunction() const {
547     return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
548   }
549
550   // getCalledValue - Get a pointer to a method that is invoked by this inst.
551   inline const Value *getCalledValue() const { return getOperand(0); }
552   inline       Value *getCalledValue()       { return getOperand(0); }
553
554   // Methods for support type inquiry through isa, cast, and dyn_cast:
555   static inline bool classof(const CallInst *) { return true; }
556   static inline bool classof(const Instruction *I) {
557     return I->getOpcode() == Instruction::Call;
558   }
559   static inline bool classof(const Value *V) {
560     return isa<Instruction>(V) && classof(cast<Instruction>(V));
561   }
562 };
563
564
565 //===----------------------------------------------------------------------===//
566 //                                 ShiftInst Class
567 //===----------------------------------------------------------------------===//
568
569 /// ShiftInst - This class represents left and right shift instructions.
570 ///
571 class ShiftInst : public Instruction {
572   Use Ops[2];
573   ShiftInst(const ShiftInst &SI)
574     : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
575     Ops[0].init(SI.Ops[0], this);
576     Ops[1].init(SI.Ops[1], this);
577   }
578   void init(OtherOps Opcode, Value *S, Value *SA) {
579     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
580     Ops[0].init(S, this);
581     Ops[1].init(SA, this);
582   }
583
584 public:
585   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
586             Instruction *InsertBefore = 0)
587     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
588     init(Opcode, S, SA);
589   }
590   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
591             BasicBlock *InsertAtEnd)
592     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
593     init(Opcode, S, SA);
594   }
595
596   OtherOps getOpcode() const {
597     return static_cast<OtherOps>(Instruction::getOpcode());
598   }
599
600   /// Transparently provide more efficient getOperand methods.
601   Value *getOperand(unsigned i) const {
602     assert(i < 2 && "getOperand() out of range!");
603     return Ops[i];
604   }
605   void setOperand(unsigned i, Value *Val) {
606     assert(i < 2 && "setOperand() out of range!");
607     Ops[i] = Val;
608   }
609   unsigned getNumOperands() const { return 2; }
610
611   virtual ShiftInst *clone() const;
612
613   // Methods for support type inquiry through isa, cast, and dyn_cast:
614   static inline bool classof(const ShiftInst *) { return true; }
615   static inline bool classof(const Instruction *I) {
616     return (I->getOpcode() == Instruction::Shr) |
617            (I->getOpcode() == Instruction::Shl);
618   }
619   static inline bool classof(const Value *V) {
620     return isa<Instruction>(V) && classof(cast<Instruction>(V));
621   }
622 };
623
624 //===----------------------------------------------------------------------===//
625 //                               SelectInst Class
626 //===----------------------------------------------------------------------===//
627
628 /// SelectInst - This class represents the LLVM 'select' instruction.
629 ///
630 class SelectInst : public Instruction {
631   Use Ops[3];
632
633   void init(Value *C, Value *S1, Value *S2) {
634     Ops[0].init(C, this);
635     Ops[1].init(S1, this);
636     Ops[2].init(S2, this);
637   }
638
639   SelectInst(const SelectInst &SI)
640     : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
641     init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
642   }
643 public:
644   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
645              Instruction *InsertBefore = 0)
646     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
647                   Name, InsertBefore) {
648     init(C, S1, S2);
649   }
650   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
651              BasicBlock *InsertAtEnd)
652     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
653                   Name, InsertAtEnd) {
654     init(C, S1, S2);
655   }
656
657   Value *getCondition() const { return Ops[0]; }
658   Value *getTrueValue() const { return Ops[1]; }
659   Value *getFalseValue() const { return Ops[2]; }
660
661   /// Transparently provide more efficient getOperand methods.
662   Value *getOperand(unsigned i) const {
663     assert(i < 3 && "getOperand() out of range!");
664     return Ops[i];
665   }
666   void setOperand(unsigned i, Value *Val) {
667     assert(i < 3 && "setOperand() out of range!");
668     Ops[i] = Val;
669   }
670   unsigned getNumOperands() const { return 3; }
671
672   OtherOps getOpcode() const {
673     return static_cast<OtherOps>(Instruction::getOpcode());
674   }
675
676   virtual SelectInst *clone() const;
677
678   // Methods for support type inquiry through isa, cast, and dyn_cast:
679   static inline bool classof(const SelectInst *) { return true; }
680   static inline bool classof(const Instruction *I) {
681     return I->getOpcode() == Instruction::Select;
682   }
683   static inline bool classof(const Value *V) {
684     return isa<Instruction>(V) && classof(cast<Instruction>(V));
685   }
686 };
687
688 //===----------------------------------------------------------------------===//
689 //                                VAArgInst Class
690 //===----------------------------------------------------------------------===//
691
692 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
693 /// an argument of the specified type given a va_list and increments that list
694 ///
695 class VAArgInst : public UnaryInstruction {
696   VAArgInst(const VAArgInst &VAA)
697     : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
698 public:
699   VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
700              Instruction *InsertBefore = 0)
701     : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
702   }
703   VAArgInst(Value *List, const Type *Ty, const std::string &Name,
704             BasicBlock *InsertAtEnd)
705     : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
706   }
707
708   virtual VAArgInst *clone() const;
709   bool mayWriteToMemory() const { return true; }
710
711   // Methods for support type inquiry through isa, cast, and dyn_cast:
712   static inline bool classof(const VAArgInst *) { return true; }
713   static inline bool classof(const Instruction *I) {
714     return I->getOpcode() == VAArg;
715   }
716   static inline bool classof(const Value *V) {
717     return isa<Instruction>(V) && classof(cast<Instruction>(V));
718   }
719 };
720
721 //===----------------------------------------------------------------------===//
722 //                                ExtractElementInst Class
723 //===----------------------------------------------------------------------===//
724
725 /// ExtractElementInst - This instruction extracts a single (scalar)
726 /// element from a PackedType value
727 ///
728 class ExtractElementInst : public Instruction {
729   Use Ops[2];
730   ExtractElementInst(const ExtractElementInst &EE) : 
731     Instruction(EE.getType(), ExtractElement, Ops, 2) {
732     Ops[0].init(EE.Ops[0], this);
733     Ops[1].init(EE.Ops[1], this);
734   }
735
736 public:
737   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
738                      Instruction *InsertBefore = 0);
739   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
740                      BasicBlock *InsertAtEnd);
741
742   /// isValidOperands - Return true if an extractelement instruction can be
743   /// formed with the specified operands.
744   static bool isValidOperands(const Value *Vec, const Value *Idx);
745   
746   virtual ExtractElementInst *clone() const;
747
748   virtual bool mayWriteToMemory() const { return false; }
749
750   /// Transparently provide more efficient getOperand methods.
751   Value *getOperand(unsigned i) const {
752     assert(i < 2 && "getOperand() out of range!");
753     return Ops[i];
754   }
755   void setOperand(unsigned i, Value *Val) {
756     assert(i < 2 && "setOperand() out of range!");
757     Ops[i] = Val;
758   }
759   unsigned getNumOperands() const { return 2; }
760
761   // Methods for support type inquiry through isa, cast, and dyn_cast:
762   static inline bool classof(const ExtractElementInst *) { return true; }
763   static inline bool classof(const Instruction *I) {
764     return I->getOpcode() == Instruction::ExtractElement;
765   }
766   static inline bool classof(const Value *V) {
767     return isa<Instruction>(V) && classof(cast<Instruction>(V));
768   }
769 };
770
771 //===----------------------------------------------------------------------===//
772 //                                InsertElementInst Class
773 //===----------------------------------------------------------------------===//
774
775 /// InsertElementInst - This instruction inserts a single (scalar)
776 /// element into a PackedType value
777 ///
778 class InsertElementInst : public Instruction {
779   Use Ops[3];
780   InsertElementInst(const InsertElementInst &IE);
781 public:
782   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
783                     const std::string &Name = "",Instruction *InsertBefore = 0);
784   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
785                     const std::string &Name, BasicBlock *InsertAtEnd);
786
787   /// isValidOperands - Return true if an insertelement instruction can be
788   /// formed with the specified operands.
789   static bool isValidOperands(const Value *Vec, const Value *NewElt,
790                               const Value *Idx);
791   
792   virtual InsertElementInst *clone() const;
793
794   virtual bool mayWriteToMemory() const { return false; }
795
796   /// getType - Overload to return most specific packed type.
797   ///
798   inline const PackedType *getType() const {
799     return reinterpret_cast<const PackedType*>(Instruction::getType());
800   }
801   
802   /// Transparently provide more efficient getOperand methods.
803   Value *getOperand(unsigned i) const {
804     assert(i < 3 && "getOperand() out of range!");
805     return Ops[i];
806   }
807   void setOperand(unsigned i, Value *Val) {
808     assert(i < 3 && "setOperand() out of range!");
809     Ops[i] = Val;
810   }
811   unsigned getNumOperands() const { return 3; }
812
813   // Methods for support type inquiry through isa, cast, and dyn_cast:
814   static inline bool classof(const InsertElementInst *) { return true; }
815   static inline bool classof(const Instruction *I) {
816     return I->getOpcode() == Instruction::InsertElement;
817   }
818   static inline bool classof(const Value *V) {
819     return isa<Instruction>(V) && classof(cast<Instruction>(V));
820   }
821 };
822
823 //===----------------------------------------------------------------------===//
824 //                           ShuffleVectorInst Class
825 //===----------------------------------------------------------------------===//
826
827 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
828 /// input vectors.
829 ///
830 class ShuffleVectorInst : public Instruction {
831   Use Ops[3];
832   ShuffleVectorInst(const ShuffleVectorInst &IE);  
833 public:
834   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
835                     const std::string &Name = "", Instruction *InsertBefor = 0);
836   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
837                     const std::string &Name, BasicBlock *InsertAtEnd);
838   
839   /// isValidOperands - Return true if a shufflevector instruction can be
840   /// formed with the specified operands.
841   static bool isValidOperands(const Value *V1, const Value *V2,
842                               const Value *Mask);
843   
844   virtual ShuffleVectorInst *clone() const;
845   
846   virtual bool mayWriteToMemory() const { return false; }
847   
848   /// getType - Overload to return most specific packed type.
849   ///
850   inline const PackedType *getType() const {
851     return reinterpret_cast<const PackedType*>(Instruction::getType());
852   }
853   
854   /// Transparently provide more efficient getOperand methods.
855   Value *getOperand(unsigned i) const {
856     assert(i < 3 && "getOperand() out of range!");
857     return Ops[i];
858   }
859   void setOperand(unsigned i, Value *Val) {
860     assert(i < 3 && "setOperand() out of range!");
861     Ops[i] = Val;
862   }
863   unsigned getNumOperands() const { return 3; }
864   
865   // Methods for support type inquiry through isa, cast, and dyn_cast:
866   static inline bool classof(const ShuffleVectorInst *) { return true; }
867   static inline bool classof(const Instruction *I) {
868     return I->getOpcode() == Instruction::ShuffleVector;
869   }
870   static inline bool classof(const Value *V) {
871     return isa<Instruction>(V) && classof(cast<Instruction>(V));
872   }
873 };
874
875
876 //===----------------------------------------------------------------------===//
877 //                               PHINode Class
878 //===----------------------------------------------------------------------===//
879
880 // PHINode - The PHINode class is used to represent the magical mystical PHI
881 // node, that can not exist in nature, but can be synthesized in a computer
882 // scientist's overactive imagination.
883 //
884 class PHINode : public Instruction {
885   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
886   /// the number actually in use.
887   unsigned ReservedSpace;
888   PHINode(const PHINode &PN);
889 public:
890   PHINode(const Type *Ty, const std::string &Name = "",
891           Instruction *InsertBefore = 0)
892     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
893       ReservedSpace(0) {
894   }
895
896   PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
897     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
898       ReservedSpace(0) {
899   }
900
901   ~PHINode();
902
903   /// reserveOperandSpace - This method can be used to avoid repeated
904   /// reallocation of PHI operand lists by reserving space for the correct
905   /// number of operands before adding them.  Unlike normal vector reserves,
906   /// this method can also be used to trim the operand space.
907   void reserveOperandSpace(unsigned NumValues) {
908     resizeOperands(NumValues*2);
909   }
910
911   virtual PHINode *clone() const;
912
913   /// getNumIncomingValues - Return the number of incoming edges
914   ///
915   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
916
917   /// getIncomingValue - Return incoming value #x
918   ///
919   Value *getIncomingValue(unsigned i) const {
920     assert(i*2 < getNumOperands() && "Invalid value number!");
921     return getOperand(i*2);
922   }
923   void setIncomingValue(unsigned i, Value *V) {
924     assert(i*2 < getNumOperands() && "Invalid value number!");
925     setOperand(i*2, V);
926   }
927   unsigned getOperandNumForIncomingValue(unsigned i) {
928     return i*2;
929   }
930
931   /// getIncomingBlock - Return incoming basic block #x
932   ///
933   BasicBlock *getIncomingBlock(unsigned i) const {
934     return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
935   }
936   void setIncomingBlock(unsigned i, BasicBlock *BB) {
937     setOperand(i*2+1, reinterpret_cast<Value*>(BB));
938   }
939   unsigned getOperandNumForIncomingBlock(unsigned i) {
940     return i*2+1;
941   }
942
943   /// addIncoming - Add an incoming value to the end of the PHI list
944   ///
945   void addIncoming(Value *V, BasicBlock *BB) {
946     assert(getType() == V->getType() &&
947            "All operands to PHI node must be the same type as the PHI node!");
948     unsigned OpNo = NumOperands;
949     if (OpNo+2 > ReservedSpace)
950       resizeOperands(0);  // Get more space!
951     // Initialize some new operands.
952     NumOperands = OpNo+2;
953     OperandList[OpNo].init(V, this);
954     OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
955   }
956
957   /// removeIncomingValue - Remove an incoming value.  This is useful if a
958   /// predecessor basic block is deleted.  The value removed is returned.
959   ///
960   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
961   /// is true), the PHI node is destroyed and any uses of it are replaced with
962   /// dummy values.  The only time there should be zero incoming values to a PHI
963   /// node is when the block is dead, so this strategy is sound.
964   ///
965   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
966
967   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
968     int Idx = getBasicBlockIndex(BB);
969     assert(Idx >= 0 && "Invalid basic block argument to remove!");
970     return removeIncomingValue(Idx, DeletePHIIfEmpty);
971   }
972
973   /// getBasicBlockIndex - Return the first index of the specified basic
974   /// block in the value list for this PHI.  Returns -1 if no instance.
975   ///
976   int getBasicBlockIndex(const BasicBlock *BB) const {
977     Use *OL = OperandList;
978     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
979       if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
980     return -1;
981   }
982
983   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
984     return getIncomingValue(getBasicBlockIndex(BB));
985   }
986
987   /// hasConstantValue - If the specified PHI node always merges together the 
988   /// same value, return the value, otherwise return null.
989   ///
990   Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
991   
992   /// Methods for support type inquiry through isa, cast, and dyn_cast:
993   static inline bool classof(const PHINode *) { return true; }
994   static inline bool classof(const Instruction *I) {
995     return I->getOpcode() == Instruction::PHI;
996   }
997   static inline bool classof(const Value *V) {
998     return isa<Instruction>(V) && classof(cast<Instruction>(V));
999   }
1000  private:
1001   void resizeOperands(unsigned NumOperands);
1002 };
1003
1004 //===----------------------------------------------------------------------===//
1005 //                               ReturnInst Class
1006 //===----------------------------------------------------------------------===//
1007
1008 //===---------------------------------------------------------------------------
1009 /// ReturnInst - Return a value (possibly void), from a function.  Execution
1010 /// does not continue in this function any longer.
1011 ///
1012 class ReturnInst : public TerminatorInst {
1013   Use RetVal;  // Possibly null retval.
1014   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1015                                                     RI.getNumOperands()) {
1016     if (RI.getNumOperands())
1017       RetVal.init(RI.RetVal, this);
1018   }
1019
1020   void init(Value *RetVal);
1021
1022 public:
1023   // ReturnInst constructors:
1024   // ReturnInst()                  - 'ret void' instruction
1025   // ReturnInst(    null)          - 'ret void' instruction
1026   // ReturnInst(Value* X)          - 'ret X'    instruction
1027   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
1028   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
1029   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
1030   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
1031   //
1032   // NOTE: If the Value* passed is of type void then the constructor behaves as
1033   // if it was passed NULL.
1034   ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1035     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1036     init(retVal);
1037   }
1038   ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1039     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1040     init(retVal);
1041   }
1042   ReturnInst(BasicBlock *InsertAtEnd)
1043     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1044   }
1045
1046   virtual ReturnInst *clone() const;
1047
1048   // Transparently provide more efficient getOperand methods.
1049   Value *getOperand(unsigned i) const {
1050     assert(i < getNumOperands() && "getOperand() out of range!");
1051     return RetVal;
1052   }
1053   void setOperand(unsigned i, Value *Val) {
1054     assert(i < getNumOperands() && "setOperand() out of range!");
1055     RetVal = Val;
1056   }
1057
1058   Value *getReturnValue() const { return RetVal; }
1059
1060   unsigned getNumSuccessors() const { return 0; }
1061
1062   // Methods for support type inquiry through isa, cast, and dyn_cast:
1063   static inline bool classof(const ReturnInst *) { return true; }
1064   static inline bool classof(const Instruction *I) {
1065     return (I->getOpcode() == Instruction::Ret);
1066   }
1067   static inline bool classof(const Value *V) {
1068     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1069   }
1070  private:
1071   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1072   virtual unsigned getNumSuccessorsV() const;
1073   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1074 };
1075
1076 //===----------------------------------------------------------------------===//
1077 //                               BranchInst Class
1078 //===----------------------------------------------------------------------===//
1079
1080 //===---------------------------------------------------------------------------
1081 /// BranchInst - Conditional or Unconditional Branch instruction.
1082 ///
1083 class BranchInst : public TerminatorInst {
1084   /// Ops list - Branches are strange.  The operands are ordered:
1085   ///  TrueDest, FalseDest, Cond.  This makes some accessors faster because
1086   /// they don't have to check for cond/uncond branchness.
1087   Use Ops[3];
1088   BranchInst(const BranchInst &BI);
1089   void AssertOK();
1090 public:
1091   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1092   // BranchInst(BB *B)                           - 'br B'
1093   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
1094   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
1095   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1096   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
1097   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
1098   BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
1099     : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1100     assert(IfTrue != 0 && "Branch destination may not be null!");
1101     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1102   }
1103   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1104              Instruction *InsertBefore = 0)
1105     : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1106     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1107     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1108     Ops[2].init(Cond, this);
1109 #ifndef NDEBUG
1110     AssertOK();
1111 #endif
1112   }
1113
1114   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
1115     : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1116     assert(IfTrue != 0 && "Branch destination may not be null!");
1117     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1118   }
1119
1120   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1121              BasicBlock *InsertAtEnd)
1122     : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1123     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1124     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1125     Ops[2].init(Cond, this);
1126 #ifndef NDEBUG
1127     AssertOK();
1128 #endif
1129   }
1130
1131
1132   /// Transparently provide more efficient getOperand methods.
1133   Value *getOperand(unsigned i) const {
1134     assert(i < getNumOperands() && "getOperand() out of range!");
1135     return Ops[i];
1136   }
1137   void setOperand(unsigned i, Value *Val) {
1138     assert(i < getNumOperands() && "setOperand() out of range!");
1139     Ops[i] = Val;
1140   }
1141
1142   virtual BranchInst *clone() const;
1143
1144   inline bool isUnconditional() const { return getNumOperands() == 1; }
1145   inline bool isConditional()   const { return getNumOperands() == 3; }
1146
1147   inline Value *getCondition() const {
1148     assert(isConditional() && "Cannot get condition of an uncond branch!");
1149     return getOperand(2);
1150   }
1151
1152   void setCondition(Value *V) {
1153     assert(isConditional() && "Cannot set condition of unconditional branch!");
1154     setOperand(2, V);
1155   }
1156
1157   // setUnconditionalDest - Change the current branch to an unconditional branch
1158   // targeting the specified block.
1159   // FIXME: Eliminate this ugly method.
1160   void setUnconditionalDest(BasicBlock *Dest) {
1161     if (isConditional()) {  // Convert this to an uncond branch.
1162       NumOperands = 1;
1163       Ops[1].set(0);
1164       Ops[2].set(0);
1165     }
1166     setOperand(0, reinterpret_cast<Value*>(Dest));
1167   }
1168
1169   unsigned getNumSuccessors() const { return 1+isConditional(); }
1170
1171   BasicBlock *getSuccessor(unsigned i) const {
1172     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1173     return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1174                       cast<BasicBlock>(getOperand(1));
1175   }
1176
1177   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1178     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1179     setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1180   }
1181
1182   // Methods for support type inquiry through isa, cast, and dyn_cast:
1183   static inline bool classof(const BranchInst *) { return true; }
1184   static inline bool classof(const Instruction *I) {
1185     return (I->getOpcode() == Instruction::Br);
1186   }
1187   static inline bool classof(const Value *V) {
1188     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1189   }
1190 private:
1191   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1192   virtual unsigned getNumSuccessorsV() const;
1193   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1194 };
1195
1196 //===----------------------------------------------------------------------===//
1197 //                               SwitchInst Class
1198 //===----------------------------------------------------------------------===//
1199
1200 //===---------------------------------------------------------------------------
1201 /// SwitchInst - Multiway switch
1202 ///
1203 class SwitchInst : public TerminatorInst {
1204   unsigned ReservedSpace;
1205   // Operand[0]    = Value to switch on
1206   // Operand[1]    = Default basic block destination
1207   // Operand[2n  ] = Value to match
1208   // Operand[2n+1] = BasicBlock to go to on match
1209   SwitchInst(const SwitchInst &RI);
1210   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1211   void resizeOperands(unsigned No);
1212 public:
1213   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1214   /// switch on and a default destination.  The number of additional cases can
1215   /// be specified here to make memory allocation more efficient.  This
1216   /// constructor can also autoinsert before another instruction.
1217   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1218              Instruction *InsertBefore = 0)
1219     : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1220     init(Value, Default, NumCases);
1221   }
1222
1223   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1224   /// switch on and a default destination.  The number of additional cases can
1225   /// be specified here to make memory allocation more efficient.  This
1226   /// constructor also autoinserts at the end of the specified BasicBlock.
1227   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1228              BasicBlock *InsertAtEnd)
1229     : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1230     init(Value, Default, NumCases);
1231   }
1232   ~SwitchInst();
1233
1234
1235   // Accessor Methods for Switch stmt
1236   inline Value *getCondition() const { return getOperand(0); }
1237   void setCondition(Value *V) { setOperand(0, V); }
1238
1239   inline BasicBlock *getDefaultDest() const {
1240     return cast<BasicBlock>(getOperand(1));
1241   }
1242
1243   /// getNumCases - return the number of 'cases' in this switch instruction.
1244   /// Note that case #0 is always the default case.
1245   unsigned getNumCases() const {
1246     return getNumOperands()/2;
1247   }
1248
1249   /// getCaseValue - Return the specified case value.  Note that case #0, the
1250   /// default destination, does not have a case value.
1251   ConstantInt *getCaseValue(unsigned i) {
1252     assert(i && i < getNumCases() && "Illegal case value to get!");
1253     return getSuccessorValue(i);
1254   }
1255
1256   /// getCaseValue - Return the specified case value.  Note that case #0, the
1257   /// default destination, does not have a case value.
1258   const ConstantInt *getCaseValue(unsigned i) const {
1259     assert(i && i < getNumCases() && "Illegal case value to get!");
1260     return getSuccessorValue(i);
1261   }
1262
1263   /// findCaseValue - Search all of the case values for the specified constant.
1264   /// If it is explicitly handled, return the case number of it, otherwise
1265   /// return 0 to indicate that it is handled by the default handler.
1266   unsigned findCaseValue(const ConstantInt *C) const {
1267     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1268       if (getCaseValue(i) == C)
1269         return i;
1270     return 0;
1271   }
1272
1273   /// addCase - Add an entry to the switch instruction...
1274   ///
1275   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1276
1277   /// removeCase - This method removes the specified successor from the switch
1278   /// instruction.  Note that this cannot be used to remove the default
1279   /// destination (successor #0).
1280   ///
1281   void removeCase(unsigned idx);
1282
1283   virtual SwitchInst *clone() const;
1284
1285   unsigned getNumSuccessors() const { return getNumOperands()/2; }
1286   BasicBlock *getSuccessor(unsigned idx) const {
1287     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1288     return cast<BasicBlock>(getOperand(idx*2+1));
1289   }
1290   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1291     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1292     setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1293   }
1294
1295   // getSuccessorValue - Return the value associated with the specified
1296   // successor.
1297   inline ConstantInt *getSuccessorValue(unsigned idx) const {
1298     assert(idx < getNumSuccessors() && "Successor # out of range!");
1299     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1300   }
1301
1302   // Methods for support type inquiry through isa, cast, and dyn_cast:
1303   static inline bool classof(const SwitchInst *) { return true; }
1304   static inline bool classof(const Instruction *I) {
1305     return I->getOpcode() == Instruction::Switch;
1306   }
1307   static inline bool classof(const Value *V) {
1308     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1309   }
1310 private:
1311   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1312   virtual unsigned getNumSuccessorsV() const;
1313   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1314 };
1315
1316 //===----------------------------------------------------------------------===//
1317 //                               InvokeInst Class
1318 //===----------------------------------------------------------------------===//
1319
1320 //===---------------------------------------------------------------------------
1321
1322 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
1323 /// calling convention of the call.
1324 ///
1325 class InvokeInst : public TerminatorInst {
1326   InvokeInst(const InvokeInst &BI);
1327   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1328             const std::vector<Value*> &Params);
1329 public:
1330   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1331              const std::vector<Value*> &Params, const std::string &Name = "",
1332              Instruction *InsertBefore = 0);
1333   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1334              const std::vector<Value*> &Params, const std::string &Name,
1335              BasicBlock *InsertAtEnd);
1336   ~InvokeInst();
1337
1338   virtual InvokeInst *clone() const;
1339
1340   bool mayWriteToMemory() const { return true; }
1341
1342   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1343   /// function call.
1344   unsigned getCallingConv() const { return SubclassData; }
1345   void setCallingConv(unsigned CC) {
1346     SubclassData = CC;
1347   }
1348
1349   /// getCalledFunction - Return the function called, or null if this is an
1350   /// indirect function invocation.
1351   ///
1352   Function *getCalledFunction() const {
1353     return dyn_cast<Function>(getOperand(0));
1354   }
1355
1356   // getCalledValue - Get a pointer to a function that is invoked by this inst.
1357   inline Value *getCalledValue() const { return getOperand(0); }
1358
1359   // get*Dest - Return the destination basic blocks...
1360   BasicBlock *getNormalDest() const {
1361     return cast<BasicBlock>(getOperand(1));
1362   }
1363   BasicBlock *getUnwindDest() const {
1364     return cast<BasicBlock>(getOperand(2));
1365   }
1366   void setNormalDest(BasicBlock *B) {
1367     setOperand(1, reinterpret_cast<Value*>(B));
1368   }
1369
1370   void setUnwindDest(BasicBlock *B) {
1371     setOperand(2, reinterpret_cast<Value*>(B));
1372   }
1373
1374   inline BasicBlock *getSuccessor(unsigned i) const {
1375     assert(i < 2 && "Successor # out of range for invoke!");
1376     return i == 0 ? getNormalDest() : getUnwindDest();
1377   }
1378
1379   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1380     assert(idx < 2 && "Successor # out of range for invoke!");
1381     setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1382   }
1383
1384   unsigned getNumSuccessors() const { return 2; }
1385
1386   // Methods for support type inquiry through isa, cast, and dyn_cast:
1387   static inline bool classof(const InvokeInst *) { return true; }
1388   static inline bool classof(const Instruction *I) {
1389     return (I->getOpcode() == Instruction::Invoke);
1390   }
1391   static inline bool classof(const Value *V) {
1392     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1393   }
1394 private:
1395   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1396   virtual unsigned getNumSuccessorsV() const;
1397   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1398 };
1399
1400
1401 //===----------------------------------------------------------------------===//
1402 //                              UnwindInst Class
1403 //===----------------------------------------------------------------------===//
1404
1405 //===---------------------------------------------------------------------------
1406 /// UnwindInst - Immediately exit the current function, unwinding the stack
1407 /// until an invoke instruction is found.
1408 ///
1409 class UnwindInst : public TerminatorInst {
1410 public:
1411   UnwindInst(Instruction *InsertBefore = 0)
1412     : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1413   }
1414   UnwindInst(BasicBlock *InsertAtEnd)
1415     : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1416   }
1417
1418   virtual UnwindInst *clone() const;
1419
1420   unsigned getNumSuccessors() const { return 0; }
1421
1422   // Methods for support type inquiry through isa, cast, and dyn_cast:
1423   static inline bool classof(const UnwindInst *) { return true; }
1424   static inline bool classof(const Instruction *I) {
1425     return I->getOpcode() == Instruction::Unwind;
1426   }
1427   static inline bool classof(const Value *V) {
1428     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1429   }
1430 private:
1431   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1432   virtual unsigned getNumSuccessorsV() const;
1433   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1434 };
1435
1436 //===----------------------------------------------------------------------===//
1437 //                           UnreachableInst Class
1438 //===----------------------------------------------------------------------===//
1439
1440 //===---------------------------------------------------------------------------
1441 /// UnreachableInst - This function has undefined behavior.  In particular, the
1442 /// presence of this instruction indicates some higher level knowledge that the
1443 /// end of the block cannot be reached.
1444 ///
1445 class UnreachableInst : public TerminatorInst {
1446 public:
1447   UnreachableInst(Instruction *InsertBefore = 0)
1448     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1449   }
1450   UnreachableInst(BasicBlock *InsertAtEnd)
1451     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1452   }
1453
1454   virtual UnreachableInst *clone() const;
1455
1456   unsigned getNumSuccessors() const { return 0; }
1457
1458   // Methods for support type inquiry through isa, cast, and dyn_cast:
1459   static inline bool classof(const UnreachableInst *) { return true; }
1460   static inline bool classof(const Instruction *I) {
1461     return I->getOpcode() == Instruction::Unreachable;
1462   }
1463   static inline bool classof(const Value *V) {
1464     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1465   }
1466 private:
1467   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1468   virtual unsigned getNumSuccessorsV() const;
1469   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1470 };
1471
1472 } // End llvm namespace
1473
1474 #endif