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