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