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