clean up use of 'explicit'. This is PR934.
[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/InstrTypes.h"
20
21 namespace llvm {
22
23 class BasicBlock;
24 class ConstantInt;
25 class PointerType;
26 class PackedType;
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   unsigned Alignment;
37 protected:
38   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
39                  const std::string &Name = "", Instruction *InsertBefore = 0);
40   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
41                  const std::string &Name, BasicBlock *InsertAtEnd);
42 public:
43   // Out of line virtual method, so the vtable, etc has a home.
44   virtual ~AllocationInst();
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   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   explicit LoadInst(Value *Ptr, const std::string &Name = "",
226                     bool isVolatile = false, 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   /// isEquality - Return true if this comparison is an ==/!= comparison.
457   ///
458   bool isEquality() const {
459     return getOpcode() == SetEQ || getOpcode() == SetNE;
460   }
461
462   /// isRelational - Return true if this comparison is a </>/<=/>= comparison.
463   ///
464   bool isRelational() const {
465     return !isEquality();
466   }
467
468   // Methods for support type inquiry through isa, cast, and dyn_cast:
469   static inline bool classof(const SetCondInst *) { return true; }
470   static inline bool classof(const Instruction *I) {
471     return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
472            I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
473            I->getOpcode() == SetLT || I->getOpcode() == SetGT;
474   }
475   static inline bool classof(const Value *V) {
476     return isa<Instruction>(V) && classof(cast<Instruction>(V));
477   }
478 };
479
480 //===----------------------------------------------------------------------===//
481 //                                 CastInst Class
482 //===----------------------------------------------------------------------===//
483
484 /// CastInst - This class represents a cast from Operand[0] to the type of
485 /// the instruction (i->getType()).
486 ///
487 class CastInst : public UnaryInstruction {
488   CastInst(const CastInst &CI)
489     : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
490   }
491 public:
492   CastInst(Value *S, const Type *Ty, const std::string &Name = "",
493            Instruction *InsertBefore = 0)
494     : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
495   }
496   CastInst(Value *S, const Type *Ty, const std::string &Name,
497            BasicBlock *InsertAtEnd)
498     : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
499   }
500
501   /// isTruncIntCast - Return true if this is a truncating integer cast
502   /// instruction, e.g. a cast from long to uint.
503   bool isTruncIntCast() const;
504
505
506   virtual CastInst *clone() const;
507
508   // Methods for support type inquiry through isa, cast, and dyn_cast:
509   static inline bool classof(const CastInst *) { return true; }
510   static inline bool classof(const Instruction *I) {
511     return I->getOpcode() == Cast;
512   }
513   static inline bool classof(const Value *V) {
514     return isa<Instruction>(V) && classof(cast<Instruction>(V));
515   }
516 };
517
518
519 //===----------------------------------------------------------------------===//
520 //                                 CallInst Class
521 //===----------------------------------------------------------------------===//
522
523 /// CallInst - This class represents a function call, abstracting a target
524 /// machine's calling convention.  This class uses low bit of the SubClassData
525 /// field to indicate whether or not this is a tail call.  The rest of the bits
526 /// hold the calling convention of the call.
527 ///
528 class CallInst : public Instruction {
529   CallInst(const CallInst &CI);
530   void init(Value *Func, const std::vector<Value*> &Params);
531   void init(Value *Func, Value *Actual1, Value *Actual2);
532   void init(Value *Func, Value *Actual);
533   void init(Value *Func);
534
535 public:
536   CallInst(Value *F, const std::vector<Value*> &Par,
537            const std::string &Name = "", Instruction *InsertBefore = 0);
538   CallInst(Value *F, const std::vector<Value*> &Par,
539            const std::string &Name, BasicBlock *InsertAtEnd);
540
541   // Alternate CallInst ctors w/ two actuals, w/ one actual and no
542   // actuals, respectively.
543   CallInst(Value *F, Value *Actual1, Value *Actual2,
544            const std::string& Name = "", Instruction *InsertBefore = 0);
545   CallInst(Value *F, Value *Actual1, Value *Actual2,
546            const std::string& Name, BasicBlock *InsertAtEnd);
547   CallInst(Value *F, Value *Actual, const std::string& Name = "",
548            Instruction *InsertBefore = 0);
549   CallInst(Value *F, Value *Actual, const std::string& Name,
550            BasicBlock *InsertAtEnd);
551   explicit CallInst(Value *F, const std::string &Name = "",
552                     Instruction *InsertBefore = 0);
553   CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
554   ~CallInst();
555
556   virtual CallInst *clone() const;
557   bool mayWriteToMemory() const { return true; }
558
559   bool isTailCall() const           { return SubclassData & 1; }
560   void setTailCall(bool isTailCall = true) {
561     SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
562   }
563
564   /// getCallingConv/setCallingConv - Get or set the calling convention of this
565   /// function call.
566   unsigned getCallingConv() const { return SubclassData >> 1; }
567   void setCallingConv(unsigned CC) {
568     SubclassData = (SubclassData & 1) | (CC << 1);
569   }
570
571   /// getCalledFunction - Return the function being called by this instruction
572   /// if it is a direct call.  If it is a call through a function pointer,
573   /// return null.
574   Function *getCalledFunction() const {
575     return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
576   }
577
578   // getCalledValue - Get a pointer to a method that is invoked by this inst.
579   inline const Value *getCalledValue() const { return getOperand(0); }
580   inline       Value *getCalledValue()       { return getOperand(0); }
581
582   // Methods for support type inquiry through isa, cast, and dyn_cast:
583   static inline bool classof(const CallInst *) { return true; }
584   static inline bool classof(const Instruction *I) {
585     return I->getOpcode() == Instruction::Call;
586   }
587   static inline bool classof(const Value *V) {
588     return isa<Instruction>(V) && classof(cast<Instruction>(V));
589   }
590 };
591
592
593 //===----------------------------------------------------------------------===//
594 //                                 ShiftInst Class
595 //===----------------------------------------------------------------------===//
596
597 /// ShiftInst - This class represents left and right shift instructions.
598 ///
599 class ShiftInst : public Instruction {
600   Use Ops[2];
601   ShiftInst(const ShiftInst &SI)
602     : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
603     Ops[0].init(SI.Ops[0], this);
604     Ops[1].init(SI.Ops[1], this);
605   }
606   void init(OtherOps Opcode, Value *S, Value *SA) {
607     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
608     Ops[0].init(S, this);
609     Ops[1].init(SA, this);
610   }
611
612 public:
613   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
614             Instruction *InsertBefore = 0)
615     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
616     init(Opcode, S, SA);
617   }
618   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
619             BasicBlock *InsertAtEnd)
620     : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
621     init(Opcode, S, SA);
622   }
623
624   OtherOps getOpcode() const {
625     return static_cast<OtherOps>(Instruction::getOpcode());
626   }
627
628   /// Transparently provide more efficient getOperand methods.
629   Value *getOperand(unsigned i) const {
630     assert(i < 2 && "getOperand() out of range!");
631     return Ops[i];
632   }
633   void setOperand(unsigned i, Value *Val) {
634     assert(i < 2 && "setOperand() out of range!");
635     Ops[i] = Val;
636   }
637   unsigned getNumOperands() const { return 2; }
638
639   /// isLogicalShift - Return true if this is a logical shift left or a logical
640   /// shift right.
641   bool isLogicalShift() const;
642
643   /// isArithmeticShift - Return true if this is a sign-extending shift right
644   /// operation.
645   bool isArithmeticShift() const {
646     return !isLogicalShift();
647   }
648
649
650   virtual ShiftInst *clone() const;
651
652   // Methods for support type inquiry through isa, cast, and dyn_cast:
653   static inline bool classof(const ShiftInst *) { return true; }
654   static inline bool classof(const Instruction *I) {
655     return (I->getOpcode() == Instruction::Shr) |
656            (I->getOpcode() == Instruction::Shl);
657   }
658   static inline bool classof(const Value *V) {
659     return isa<Instruction>(V) && classof(cast<Instruction>(V));
660   }
661 };
662
663 //===----------------------------------------------------------------------===//
664 //                               SelectInst Class
665 //===----------------------------------------------------------------------===//
666
667 /// SelectInst - This class represents the LLVM 'select' instruction.
668 ///
669 class SelectInst : public Instruction {
670   Use Ops[3];
671
672   void init(Value *C, Value *S1, Value *S2) {
673     Ops[0].init(C, this);
674     Ops[1].init(S1, this);
675     Ops[2].init(S2, this);
676   }
677
678   SelectInst(const SelectInst &SI)
679     : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
680     init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
681   }
682 public:
683   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
684              Instruction *InsertBefore = 0)
685     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
686                   Name, InsertBefore) {
687     init(C, S1, S2);
688   }
689   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
690              BasicBlock *InsertAtEnd)
691     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
692                   Name, InsertAtEnd) {
693     init(C, S1, S2);
694   }
695
696   Value *getCondition() const { return Ops[0]; }
697   Value *getTrueValue() const { return Ops[1]; }
698   Value *getFalseValue() const { return Ops[2]; }
699
700   /// Transparently provide more efficient getOperand methods.
701   Value *getOperand(unsigned i) const {
702     assert(i < 3 && "getOperand() out of range!");
703     return Ops[i];
704   }
705   void setOperand(unsigned i, Value *Val) {
706     assert(i < 3 && "setOperand() out of range!");
707     Ops[i] = Val;
708   }
709   unsigned getNumOperands() const { return 3; }
710
711   OtherOps getOpcode() const {
712     return static_cast<OtherOps>(Instruction::getOpcode());
713   }
714
715   virtual SelectInst *clone() const;
716
717   // Methods for support type inquiry through isa, cast, and dyn_cast:
718   static inline bool classof(const SelectInst *) { return true; }
719   static inline bool classof(const Instruction *I) {
720     return I->getOpcode() == Instruction::Select;
721   }
722   static inline bool classof(const Value *V) {
723     return isa<Instruction>(V) && classof(cast<Instruction>(V));
724   }
725 };
726
727 //===----------------------------------------------------------------------===//
728 //                                VAArgInst Class
729 //===----------------------------------------------------------------------===//
730
731 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
732 /// an argument of the specified type given a va_list and increments that list
733 ///
734 class VAArgInst : public UnaryInstruction {
735   VAArgInst(const VAArgInst &VAA)
736     : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
737 public:
738   VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
739              Instruction *InsertBefore = 0)
740     : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
741   }
742   VAArgInst(Value *List, const Type *Ty, const std::string &Name,
743             BasicBlock *InsertAtEnd)
744     : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
745   }
746
747   virtual VAArgInst *clone() const;
748   bool mayWriteToMemory() const { return true; }
749
750   // Methods for support type inquiry through isa, cast, and dyn_cast:
751   static inline bool classof(const VAArgInst *) { return true; }
752   static inline bool classof(const Instruction *I) {
753     return I->getOpcode() == VAArg;
754   }
755   static inline bool classof(const Value *V) {
756     return isa<Instruction>(V) && classof(cast<Instruction>(V));
757   }
758 };
759
760 //===----------------------------------------------------------------------===//
761 //                                ExtractElementInst Class
762 //===----------------------------------------------------------------------===//
763
764 /// ExtractElementInst - This instruction extracts a single (scalar)
765 /// element from a PackedType value
766 ///
767 class ExtractElementInst : public Instruction {
768   Use Ops[2];
769   ExtractElementInst(const ExtractElementInst &EE) :
770     Instruction(EE.getType(), ExtractElement, Ops, 2) {
771     Ops[0].init(EE.Ops[0], this);
772     Ops[1].init(EE.Ops[1], this);
773   }
774
775 public:
776   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
777                      Instruction *InsertBefore = 0);
778   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
779                      BasicBlock *InsertAtEnd);
780
781   /// isValidOperands - Return true if an extractelement instruction can be
782   /// formed with the specified operands.
783   static bool isValidOperands(const Value *Vec, const Value *Idx);
784
785   virtual ExtractElementInst *clone() const;
786
787   virtual bool mayWriteToMemory() const { return false; }
788
789   /// Transparently provide more efficient getOperand methods.
790   Value *getOperand(unsigned i) const {
791     assert(i < 2 && "getOperand() out of range!");
792     return Ops[i];
793   }
794   void setOperand(unsigned i, Value *Val) {
795     assert(i < 2 && "setOperand() out of range!");
796     Ops[i] = Val;
797   }
798   unsigned getNumOperands() const { return 2; }
799
800   // Methods for support type inquiry through isa, cast, and dyn_cast:
801   static inline bool classof(const ExtractElementInst *) { return true; }
802   static inline bool classof(const Instruction *I) {
803     return I->getOpcode() == Instruction::ExtractElement;
804   }
805   static inline bool classof(const Value *V) {
806     return isa<Instruction>(V) && classof(cast<Instruction>(V));
807   }
808 };
809
810 //===----------------------------------------------------------------------===//
811 //                                InsertElementInst Class
812 //===----------------------------------------------------------------------===//
813
814 /// InsertElementInst - This instruction inserts a single (scalar)
815 /// element into a PackedType value
816 ///
817 class InsertElementInst : public Instruction {
818   Use Ops[3];
819   InsertElementInst(const InsertElementInst &IE);
820 public:
821   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
822                     const std::string &Name = "",Instruction *InsertBefore = 0);
823   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
824                     const std::string &Name, BasicBlock *InsertAtEnd);
825
826   /// isValidOperands - Return true if an insertelement instruction can be
827   /// formed with the specified operands.
828   static bool isValidOperands(const Value *Vec, const Value *NewElt,
829                               const Value *Idx);
830
831   virtual InsertElementInst *clone() const;
832
833   virtual bool mayWriteToMemory() const { return false; }
834
835   /// getType - Overload to return most specific packed type.
836   ///
837   inline const PackedType *getType() const {
838     return reinterpret_cast<const PackedType*>(Instruction::getType());
839   }
840
841   /// Transparently provide more efficient getOperand methods.
842   Value *getOperand(unsigned i) const {
843     assert(i < 3 && "getOperand() out of range!");
844     return Ops[i];
845   }
846   void setOperand(unsigned i, Value *Val) {
847     assert(i < 3 && "setOperand() out of range!");
848     Ops[i] = Val;
849   }
850   unsigned getNumOperands() const { return 3; }
851
852   // Methods for support type inquiry through isa, cast, and dyn_cast:
853   static inline bool classof(const InsertElementInst *) { return true; }
854   static inline bool classof(const Instruction *I) {
855     return I->getOpcode() == Instruction::InsertElement;
856   }
857   static inline bool classof(const Value *V) {
858     return isa<Instruction>(V) && classof(cast<Instruction>(V));
859   }
860 };
861
862 //===----------------------------------------------------------------------===//
863 //                           ShuffleVectorInst Class
864 //===----------------------------------------------------------------------===//
865
866 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
867 /// input vectors.
868 ///
869 class ShuffleVectorInst : public Instruction {
870   Use Ops[3];
871   ShuffleVectorInst(const ShuffleVectorInst &IE);
872 public:
873   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
874                     const std::string &Name = "", Instruction *InsertBefor = 0);
875   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
876                     const std::string &Name, BasicBlock *InsertAtEnd);
877
878   /// isValidOperands - Return true if a shufflevector instruction can be
879   /// formed with the specified operands.
880   static bool isValidOperands(const Value *V1, const Value *V2,
881                               const Value *Mask);
882
883   virtual ShuffleVectorInst *clone() const;
884
885   virtual bool mayWriteToMemory() const { return false; }
886
887   /// getType - Overload to return most specific packed type.
888   ///
889   inline const PackedType *getType() const {
890     return reinterpret_cast<const PackedType*>(Instruction::getType());
891   }
892
893   /// Transparently provide more efficient getOperand methods.
894   Value *getOperand(unsigned i) const {
895     assert(i < 3 && "getOperand() out of range!");
896     return Ops[i];
897   }
898   void setOperand(unsigned i, Value *Val) {
899     assert(i < 3 && "setOperand() out of range!");
900     Ops[i] = Val;
901   }
902   unsigned getNumOperands() const { return 3; }
903
904   // Methods for support type inquiry through isa, cast, and dyn_cast:
905   static inline bool classof(const ShuffleVectorInst *) { return true; }
906   static inline bool classof(const Instruction *I) {
907     return I->getOpcode() == Instruction::ShuffleVector;
908   }
909   static inline bool classof(const Value *V) {
910     return isa<Instruction>(V) && classof(cast<Instruction>(V));
911   }
912 };
913
914
915 //===----------------------------------------------------------------------===//
916 //                               PHINode Class
917 //===----------------------------------------------------------------------===//
918
919 // PHINode - The PHINode class is used to represent the magical mystical PHI
920 // node, that can not exist in nature, but can be synthesized in a computer
921 // scientist's overactive imagination.
922 //
923 class PHINode : public Instruction {
924   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
925   /// the number actually in use.
926   unsigned ReservedSpace;
927   PHINode(const PHINode &PN);
928 public:
929   explicit PHINode(const Type *Ty, const std::string &Name = "",
930                    Instruction *InsertBefore = 0)
931     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
932       ReservedSpace(0) {
933   }
934
935   PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
936     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
937       ReservedSpace(0) {
938   }
939
940   ~PHINode();
941
942   /// reserveOperandSpace - This method can be used to avoid repeated
943   /// reallocation of PHI operand lists by reserving space for the correct
944   /// number of operands before adding them.  Unlike normal vector reserves,
945   /// this method can also be used to trim the operand space.
946   void reserveOperandSpace(unsigned NumValues) {
947     resizeOperands(NumValues*2);
948   }
949
950   virtual PHINode *clone() const;
951
952   /// getNumIncomingValues - Return the number of incoming edges
953   ///
954   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
955
956   /// getIncomingValue - Return incoming value number x
957   ///
958   Value *getIncomingValue(unsigned i) const {
959     assert(i*2 < getNumOperands() && "Invalid value number!");
960     return getOperand(i*2);
961   }
962   void setIncomingValue(unsigned i, Value *V) {
963     assert(i*2 < getNumOperands() && "Invalid value number!");
964     setOperand(i*2, V);
965   }
966   unsigned getOperandNumForIncomingValue(unsigned i) {
967     return i*2;
968   }
969
970   /// getIncomingBlock - Return incoming basic block number x
971   ///
972   BasicBlock *getIncomingBlock(unsigned i) const {
973     return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
974   }
975   void setIncomingBlock(unsigned i, BasicBlock *BB) {
976     setOperand(i*2+1, reinterpret_cast<Value*>(BB));
977   }
978   unsigned getOperandNumForIncomingBlock(unsigned i) {
979     return i*2+1;
980   }
981
982   /// addIncoming - Add an incoming value to the end of the PHI list
983   ///
984   void addIncoming(Value *V, BasicBlock *BB) {
985     assert(getType() == V->getType() &&
986            "All operands to PHI node must be the same type as the PHI node!");
987     unsigned OpNo = NumOperands;
988     if (OpNo+2 > ReservedSpace)
989       resizeOperands(0);  // Get more space!
990     // Initialize some new operands.
991     NumOperands = OpNo+2;
992     OperandList[OpNo].init(V, this);
993     OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
994   }
995
996   /// removeIncomingValue - Remove an incoming value.  This is useful if a
997   /// predecessor basic block is deleted.  The value removed is returned.
998   ///
999   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1000   /// is true), the PHI node is destroyed and any uses of it are replaced with
1001   /// dummy values.  The only time there should be zero incoming values to a PHI
1002   /// node is when the block is dead, so this strategy is sound.
1003   ///
1004   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1005
1006   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1007     int Idx = getBasicBlockIndex(BB);
1008     assert(Idx >= 0 && "Invalid basic block argument to remove!");
1009     return removeIncomingValue(Idx, DeletePHIIfEmpty);
1010   }
1011
1012   /// getBasicBlockIndex - Return the first index of the specified basic
1013   /// block in the value list for this PHI.  Returns -1 if no instance.
1014   ///
1015   int getBasicBlockIndex(const BasicBlock *BB) const {
1016     Use *OL = OperandList;
1017     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1018       if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1019     return -1;
1020   }
1021
1022   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1023     return getIncomingValue(getBasicBlockIndex(BB));
1024   }
1025
1026   /// hasConstantValue - If the specified PHI node always merges together the
1027   /// same value, return the value, otherwise return null.
1028   ///
1029   Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1030
1031   /// Methods for support type inquiry through isa, cast, and dyn_cast:
1032   static inline bool classof(const PHINode *) { return true; }
1033   static inline bool classof(const Instruction *I) {
1034     return I->getOpcode() == Instruction::PHI;
1035   }
1036   static inline bool classof(const Value *V) {
1037     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1038   }
1039  private:
1040   void resizeOperands(unsigned NumOperands);
1041 };
1042
1043 //===----------------------------------------------------------------------===//
1044 //                               ReturnInst Class
1045 //===----------------------------------------------------------------------===//
1046
1047 //===---------------------------------------------------------------------------
1048 /// ReturnInst - Return a value (possibly void), from a function.  Execution
1049 /// does not continue in this function any longer.
1050 ///
1051 class ReturnInst : public TerminatorInst {
1052   Use RetVal;  // Possibly null retval.
1053   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1054                                                     RI.getNumOperands()) {
1055     if (RI.getNumOperands())
1056       RetVal.init(RI.RetVal, this);
1057   }
1058
1059   void init(Value *RetVal);
1060
1061 public:
1062   // ReturnInst constructors:
1063   // ReturnInst()                  - 'ret void' instruction
1064   // ReturnInst(    null)          - 'ret void' instruction
1065   // ReturnInst(Value* X)          - 'ret X'    instruction
1066   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
1067   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
1068   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
1069   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
1070   //
1071   // NOTE: If the Value* passed is of type void then the constructor behaves as
1072   // if it was passed NULL.
1073   explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1074     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1075     init(retVal);
1076   }
1077   ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1078     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1079     init(retVal);
1080   }
1081   explicit ReturnInst(BasicBlock *InsertAtEnd)
1082     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1083   }
1084
1085   virtual ReturnInst *clone() const;
1086
1087   // Transparently provide more efficient getOperand methods.
1088   Value *getOperand(unsigned i) const {
1089     assert(i < getNumOperands() && "getOperand() out of range!");
1090     return RetVal;
1091   }
1092   void setOperand(unsigned i, Value *Val) {
1093     assert(i < getNumOperands() && "setOperand() out of range!");
1094     RetVal = Val;
1095   }
1096
1097   Value *getReturnValue() const { return RetVal; }
1098
1099   unsigned getNumSuccessors() const { return 0; }
1100
1101   // Methods for support type inquiry through isa, cast, and dyn_cast:
1102   static inline bool classof(const ReturnInst *) { return true; }
1103   static inline bool classof(const Instruction *I) {
1104     return (I->getOpcode() == Instruction::Ret);
1105   }
1106   static inline bool classof(const Value *V) {
1107     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1108   }
1109  private:
1110   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1111   virtual unsigned getNumSuccessorsV() const;
1112   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1113 };
1114
1115 //===----------------------------------------------------------------------===//
1116 //                               BranchInst Class
1117 //===----------------------------------------------------------------------===//
1118
1119 //===---------------------------------------------------------------------------
1120 /// BranchInst - Conditional or Unconditional Branch instruction.
1121 ///
1122 class BranchInst : public TerminatorInst {
1123   /// Ops list - Branches are strange.  The operands are ordered:
1124   ///  TrueDest, FalseDest, Cond.  This makes some accessors faster because
1125   /// they don't have to check for cond/uncond branchness.
1126   Use Ops[3];
1127   BranchInst(const BranchInst &BI);
1128   void AssertOK();
1129 public:
1130   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1131   // BranchInst(BB *B)                           - 'br B'
1132   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
1133   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
1134   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1135   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
1136   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
1137   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
1138     : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1139     assert(IfTrue != 0 && "Branch destination may not be null!");
1140     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1141   }
1142   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1143              Instruction *InsertBefore = 0)
1144     : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1145     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1146     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1147     Ops[2].init(Cond, this);
1148 #ifndef NDEBUG
1149     AssertOK();
1150 #endif
1151   }
1152
1153   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
1154     : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1155     assert(IfTrue != 0 && "Branch destination may not be null!");
1156     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1157   }
1158
1159   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1160              BasicBlock *InsertAtEnd)
1161     : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1162     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1163     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1164     Ops[2].init(Cond, this);
1165 #ifndef NDEBUG
1166     AssertOK();
1167 #endif
1168   }
1169
1170
1171   /// Transparently provide more efficient getOperand methods.
1172   Value *getOperand(unsigned i) const {
1173     assert(i < getNumOperands() && "getOperand() out of range!");
1174     return Ops[i];
1175   }
1176   void setOperand(unsigned i, Value *Val) {
1177     assert(i < getNumOperands() && "setOperand() out of range!");
1178     Ops[i] = Val;
1179   }
1180
1181   virtual BranchInst *clone() const;
1182
1183   inline bool isUnconditional() const { return getNumOperands() == 1; }
1184   inline bool isConditional()   const { return getNumOperands() == 3; }
1185
1186   inline Value *getCondition() const {
1187     assert(isConditional() && "Cannot get condition of an uncond branch!");
1188     return getOperand(2);
1189   }
1190
1191   void setCondition(Value *V) {
1192     assert(isConditional() && "Cannot set condition of unconditional branch!");
1193     setOperand(2, V);
1194   }
1195
1196   // setUnconditionalDest - Change the current branch to an unconditional branch
1197   // targeting the specified block.
1198   // FIXME: Eliminate this ugly method.
1199   void setUnconditionalDest(BasicBlock *Dest) {
1200     if (isConditional()) {  // Convert this to an uncond branch.
1201       NumOperands = 1;
1202       Ops[1].set(0);
1203       Ops[2].set(0);
1204     }
1205     setOperand(0, reinterpret_cast<Value*>(Dest));
1206   }
1207
1208   unsigned getNumSuccessors() const { return 1+isConditional(); }
1209
1210   BasicBlock *getSuccessor(unsigned i) const {
1211     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1212     return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1213                       cast<BasicBlock>(getOperand(1));
1214   }
1215
1216   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1217     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1218     setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1219   }
1220
1221   // Methods for support type inquiry through isa, cast, and dyn_cast:
1222   static inline bool classof(const BranchInst *) { return true; }
1223   static inline bool classof(const Instruction *I) {
1224     return (I->getOpcode() == Instruction::Br);
1225   }
1226   static inline bool classof(const Value *V) {
1227     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1228   }
1229 private:
1230   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1231   virtual unsigned getNumSuccessorsV() const;
1232   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1233 };
1234
1235 //===----------------------------------------------------------------------===//
1236 //                               SwitchInst Class
1237 //===----------------------------------------------------------------------===//
1238
1239 //===---------------------------------------------------------------------------
1240 /// SwitchInst - Multiway switch
1241 ///
1242 class SwitchInst : public TerminatorInst {
1243   unsigned ReservedSpace;
1244   // Operand[0]    = Value to switch on
1245   // Operand[1]    = Default basic block destination
1246   // Operand[2n  ] = Value to match
1247   // Operand[2n+1] = BasicBlock to go to on match
1248   SwitchInst(const SwitchInst &RI);
1249   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1250   void resizeOperands(unsigned No);
1251 public:
1252   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1253   /// switch on and a default destination.  The number of additional cases can
1254   /// be specified here to make memory allocation more efficient.  This
1255   /// constructor can also autoinsert before another instruction.
1256   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1257              Instruction *InsertBefore = 0)
1258     : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1259     init(Value, Default, NumCases);
1260   }
1261
1262   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1263   /// switch on and a default destination.  The number of additional cases can
1264   /// be specified here to make memory allocation more efficient.  This
1265   /// constructor also autoinserts at the end of the specified BasicBlock.
1266   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1267              BasicBlock *InsertAtEnd)
1268     : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1269     init(Value, Default, NumCases);
1270   }
1271   ~SwitchInst();
1272
1273
1274   // Accessor Methods for Switch stmt
1275   inline Value *getCondition() const { return getOperand(0); }
1276   void setCondition(Value *V) { setOperand(0, V); }
1277
1278   inline BasicBlock *getDefaultDest() const {
1279     return cast<BasicBlock>(getOperand(1));
1280   }
1281
1282   /// getNumCases - return the number of 'cases' in this switch instruction.
1283   /// Note that case #0 is always the default case.
1284   unsigned getNumCases() const {
1285     return getNumOperands()/2;
1286   }
1287
1288   /// getCaseValue - Return the specified case value.  Note that case #0, the
1289   /// default destination, does not have a case value.
1290   ConstantInt *getCaseValue(unsigned i) {
1291     assert(i && i < getNumCases() && "Illegal case value to get!");
1292     return getSuccessorValue(i);
1293   }
1294
1295   /// getCaseValue - Return the specified case value.  Note that case #0, the
1296   /// default destination, does not have a case value.
1297   const ConstantInt *getCaseValue(unsigned i) const {
1298     assert(i && i < getNumCases() && "Illegal case value to get!");
1299     return getSuccessorValue(i);
1300   }
1301
1302   /// findCaseValue - Search all of the case values for the specified constant.
1303   /// If it is explicitly handled, return the case number of it, otherwise
1304   /// return 0 to indicate that it is handled by the default handler.
1305   unsigned findCaseValue(const ConstantInt *C) const {
1306     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1307       if (getCaseValue(i) == C)
1308         return i;
1309     return 0;
1310   }
1311
1312   /// findCaseDest - Finds the unique case value for a given successor. Returns
1313   /// null if the successor is not found, not unique, or is the default case.
1314   ConstantInt *findCaseDest(BasicBlock *BB) {
1315     if (BB == getDefaultDest()) return NULL;
1316
1317     ConstantInt *CI = NULL;
1318     for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1319       if (getSuccessor(i) == BB) {
1320         if (CI) return NULL;   // Multiple cases lead to BB.
1321         else CI = getCaseValue(i);
1322       }
1323     }
1324     return CI;
1325   }
1326
1327   /// addCase - Add an entry to the switch instruction...
1328   ///
1329   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1330
1331   /// removeCase - This method removes the specified successor from the switch
1332   /// instruction.  Note that this cannot be used to remove the default
1333   /// destination (successor #0).
1334   ///
1335   void removeCase(unsigned idx);
1336
1337   virtual SwitchInst *clone() const;
1338
1339   unsigned getNumSuccessors() const { return getNumOperands()/2; }
1340   BasicBlock *getSuccessor(unsigned idx) const {
1341     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1342     return cast<BasicBlock>(getOperand(idx*2+1));
1343   }
1344   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1345     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1346     setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1347   }
1348
1349   // getSuccessorValue - Return the value associated with the specified
1350   // successor.
1351   inline ConstantInt *getSuccessorValue(unsigned idx) const {
1352     assert(idx < getNumSuccessors() && "Successor # out of range!");
1353     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1354   }
1355
1356   // Methods for support type inquiry through isa, cast, and dyn_cast:
1357   static inline bool classof(const SwitchInst *) { return true; }
1358   static inline bool classof(const Instruction *I) {
1359     return I->getOpcode() == Instruction::Switch;
1360   }
1361   static inline bool classof(const Value *V) {
1362     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1363   }
1364 private:
1365   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1366   virtual unsigned getNumSuccessorsV() const;
1367   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1368 };
1369
1370 //===----------------------------------------------------------------------===//
1371 //                               InvokeInst Class
1372 //===----------------------------------------------------------------------===//
1373
1374 //===---------------------------------------------------------------------------
1375
1376 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
1377 /// calling convention of the call.
1378 ///
1379 class InvokeInst : public TerminatorInst {
1380   InvokeInst(const InvokeInst &BI);
1381   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1382             const std::vector<Value*> &Params);
1383 public:
1384   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1385              const std::vector<Value*> &Params, const std::string &Name = "",
1386              Instruction *InsertBefore = 0);
1387   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1388              const std::vector<Value*> &Params, const std::string &Name,
1389              BasicBlock *InsertAtEnd);
1390   ~InvokeInst();
1391
1392   virtual InvokeInst *clone() const;
1393
1394   bool mayWriteToMemory() const { return true; }
1395
1396   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1397   /// function call.
1398   unsigned getCallingConv() const { return SubclassData; }
1399   void setCallingConv(unsigned CC) {
1400     SubclassData = CC;
1401   }
1402
1403   /// getCalledFunction - Return the function called, or null if this is an
1404   /// indirect function invocation.
1405   ///
1406   Function *getCalledFunction() const {
1407     return dyn_cast<Function>(getOperand(0));
1408   }
1409
1410   // getCalledValue - Get a pointer to a function that is invoked by this inst.
1411   inline Value *getCalledValue() const { return getOperand(0); }
1412
1413   // get*Dest - Return the destination basic blocks...
1414   BasicBlock *getNormalDest() const {
1415     return cast<BasicBlock>(getOperand(1));
1416   }
1417   BasicBlock *getUnwindDest() const {
1418     return cast<BasicBlock>(getOperand(2));
1419   }
1420   void setNormalDest(BasicBlock *B) {
1421     setOperand(1, reinterpret_cast<Value*>(B));
1422   }
1423
1424   void setUnwindDest(BasicBlock *B) {
1425     setOperand(2, reinterpret_cast<Value*>(B));
1426   }
1427
1428   inline BasicBlock *getSuccessor(unsigned i) const {
1429     assert(i < 2 && "Successor # out of range for invoke!");
1430     return i == 0 ? getNormalDest() : getUnwindDest();
1431   }
1432
1433   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1434     assert(idx < 2 && "Successor # out of range for invoke!");
1435     setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1436   }
1437
1438   unsigned getNumSuccessors() const { return 2; }
1439
1440   // Methods for support type inquiry through isa, cast, and dyn_cast:
1441   static inline bool classof(const InvokeInst *) { return true; }
1442   static inline bool classof(const Instruction *I) {
1443     return (I->getOpcode() == Instruction::Invoke);
1444   }
1445   static inline bool classof(const Value *V) {
1446     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1447   }
1448 private:
1449   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1450   virtual unsigned getNumSuccessorsV() const;
1451   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1452 };
1453
1454
1455 //===----------------------------------------------------------------------===//
1456 //                              UnwindInst Class
1457 //===----------------------------------------------------------------------===//
1458
1459 //===---------------------------------------------------------------------------
1460 /// UnwindInst - Immediately exit the current function, unwinding the stack
1461 /// until an invoke instruction is found.
1462 ///
1463 class UnwindInst : public TerminatorInst {
1464 public:
1465   explicit UnwindInst(Instruction *InsertBefore = 0)
1466     : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1467   }
1468   explicit UnwindInst(BasicBlock *InsertAtEnd)
1469     : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1470   }
1471
1472   virtual UnwindInst *clone() const;
1473
1474   unsigned getNumSuccessors() const { return 0; }
1475
1476   // Methods for support type inquiry through isa, cast, and dyn_cast:
1477   static inline bool classof(const UnwindInst *) { return true; }
1478   static inline bool classof(const Instruction *I) {
1479     return I->getOpcode() == Instruction::Unwind;
1480   }
1481   static inline bool classof(const Value *V) {
1482     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1483   }
1484 private:
1485   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1486   virtual unsigned getNumSuccessorsV() const;
1487   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1488 };
1489
1490 //===----------------------------------------------------------------------===//
1491 //                           UnreachableInst Class
1492 //===----------------------------------------------------------------------===//
1493
1494 //===---------------------------------------------------------------------------
1495 /// UnreachableInst - This function has undefined behavior.  In particular, the
1496 /// presence of this instruction indicates some higher level knowledge that the
1497 /// end of the block cannot be reached.
1498 ///
1499 class UnreachableInst : public TerminatorInst {
1500 public:
1501   explicit UnreachableInst(Instruction *InsertBefore = 0)
1502     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1503   }
1504   explicit UnreachableInst(BasicBlock *InsertAtEnd)
1505     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1506   }
1507
1508   virtual UnreachableInst *clone() const;
1509
1510   unsigned getNumSuccessors() const { return 0; }
1511
1512   // Methods for support type inquiry through isa, cast, and dyn_cast:
1513   static inline bool classof(const UnreachableInst *) { return true; }
1514   static inline bool classof(const Instruction *I) {
1515     return I->getOpcode() == Instruction::Unreachable;
1516   }
1517   static inline bool classof(const Value *V) {
1518     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1519   }
1520 private:
1521   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1522   virtual unsigned getNumSuccessorsV() const;
1523   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1524 };
1525
1526 } // End llvm namespace
1527
1528 #endif