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