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