Autoupgrade malloc insts to malloc calls.
[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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class.  This is meant to be an easy way to get access to all
12 // instruction subclasses.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
18
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include <iterator>
27
28 namespace llvm {
29
30 class ConstantInt;
31 class ConstantRange;
32 class APInt;
33 class LLVMContext;
34 class DominatorTree;
35
36 //===----------------------------------------------------------------------===//
37 //                             AllocationInst Class
38 //===----------------------------------------------------------------------===//
39
40 /// AllocationInst - This class is the common base class of MallocInst and
41 /// AllocaInst.
42 ///
43 class AllocationInst : public UnaryInstruction {
44 protected:
45   AllocationInst(const Type *Ty, Value *ArraySize, 
46                  unsigned iTy, unsigned Align, const Twine &Name = "", 
47                  Instruction *InsertBefore = 0);
48   AllocationInst(const Type *Ty, Value *ArraySize,
49                  unsigned iTy, unsigned Align, const Twine &Name,
50                  BasicBlock *InsertAtEnd);
51 public:
52   // Out of line virtual method, so the vtable, etc. has a home.
53   virtual ~AllocationInst();
54
55   /// isArrayAllocation - Return true if there is an allocation size parameter
56   /// to the allocation instruction that is not 1.
57   ///
58   bool isArrayAllocation() const;
59
60   /// getArraySize - Get the number of elements allocated. For a simple
61   /// allocation of a single element, this will return a constant 1 value.
62   ///
63   const Value *getArraySize() const { return getOperand(0); }
64   Value *getArraySize() { return getOperand(0); }
65
66   /// getType - Overload to return most specific pointer type
67   ///
68   const PointerType *getType() const {
69     return reinterpret_cast<const PointerType*>(Instruction::getType());
70   }
71
72   /// getAllocatedType - Return the type that is being allocated by the
73   /// instruction.
74   ///
75   const Type *getAllocatedType() const;
76
77   /// getAlignment - Return the alignment of the memory that is being allocated
78   /// by the instruction.
79   ///
80   unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
81   void setAlignment(unsigned Align);
82
83   virtual AllocationInst *clone() const = 0;
84
85   // Methods for support type inquiry through isa, cast, and dyn_cast:
86   static inline bool classof(const AllocationInst *) { return true; }
87   static inline bool classof(const Instruction *I) {
88     return I->getOpcode() == Instruction::Alloca ||
89            I->getOpcode() == Instruction::Malloc;
90   }
91   static inline bool classof(const Value *V) {
92     return isa<Instruction>(V) && classof(cast<Instruction>(V));
93   }
94 };
95
96
97 //===----------------------------------------------------------------------===//
98 //                                MallocInst Class
99 //===----------------------------------------------------------------------===//
100
101 /// MallocInst - an instruction to allocated memory on the heap
102 ///
103 class MallocInst : public AllocationInst {
104 public:
105   explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
106                       const Twine &NameStr = "",
107                       Instruction *InsertBefore = 0)
108     : AllocationInst(Ty, ArraySize, Malloc,
109                      0, NameStr, InsertBefore) {}
110   MallocInst(const Type *Ty, Value *ArraySize,
111              const Twine &NameStr, BasicBlock *InsertAtEnd)
112     : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113
114   MallocInst(const Type *Ty, const Twine &NameStr,
115              Instruction *InsertBefore = 0)
116     : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
117   MallocInst(const Type *Ty, const Twine &NameStr,
118              BasicBlock *InsertAtEnd)
119     : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120
121   MallocInst(const Type *Ty, Value *ArraySize,
122              unsigned Align, const Twine &NameStr,
123              BasicBlock *InsertAtEnd)
124     : AllocationInst(Ty, ArraySize, Malloc,
125                      Align, NameStr, InsertAtEnd) {}
126   MallocInst(const Type *Ty, Value *ArraySize,
127              unsigned Align, const Twine &NameStr = "", 
128              Instruction *InsertBefore = 0)
129     : AllocationInst(Ty, ArraySize,
130                      Malloc, Align, NameStr, InsertBefore) {}
131
132   virtual MallocInst *clone() const;
133
134   // Methods for support type inquiry through isa, cast, and dyn_cast:
135   static inline bool classof(const MallocInst *) { return true; }
136   static inline bool classof(const Instruction *I) {
137     return (I->getOpcode() == Instruction::Malloc);
138   }
139   static inline bool classof(const Value *V) {
140     return isa<Instruction>(V) && classof(cast<Instruction>(V));
141   }
142 };
143
144
145 //===----------------------------------------------------------------------===//
146 //                                AllocaInst Class
147 //===----------------------------------------------------------------------===//
148
149 /// AllocaInst - an instruction to allocate memory on the stack
150 ///
151 class AllocaInst : public AllocationInst {
152 public:
153   explicit AllocaInst(const Type *Ty,
154                       Value *ArraySize = 0,
155                       const Twine &NameStr = "",
156                       Instruction *InsertBefore = 0)
157     : AllocationInst(Ty, ArraySize, Alloca,
158                      0, NameStr, InsertBefore) {}
159   AllocaInst(const Type *Ty,
160              Value *ArraySize, const Twine &NameStr,
161              BasicBlock *InsertAtEnd)
162     : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
163
164   AllocaInst(const Type *Ty, const Twine &NameStr,
165              Instruction *InsertBefore = 0)
166     : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167   AllocaInst(const Type *Ty, const Twine &NameStr,
168              BasicBlock *InsertAtEnd)
169     : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
170
171   AllocaInst(const Type *Ty, Value *ArraySize,
172              unsigned Align, const Twine &NameStr = "",
173              Instruction *InsertBefore = 0)
174     : AllocationInst(Ty, ArraySize, Alloca,
175                      Align, NameStr, InsertBefore) {}
176   AllocaInst(const Type *Ty, Value *ArraySize,
177              unsigned Align, const Twine &NameStr,
178              BasicBlock *InsertAtEnd)
179     : AllocationInst(Ty, ArraySize, Alloca,
180                      Align, NameStr, InsertAtEnd) {}
181
182   virtual AllocaInst *clone() const;
183
184   /// isStaticAlloca - Return true if this alloca is in the entry block of the
185   /// function and is a constant size.  If so, the code generator will fold it
186   /// into the prolog/epilog code, so it is basically free.
187   bool isStaticAlloca() const;
188
189   // Methods for support type inquiry through isa, cast, and dyn_cast:
190   static inline bool classof(const AllocaInst *) { return true; }
191   static inline bool classof(const Instruction *I) {
192     return (I->getOpcode() == Instruction::Alloca);
193   }
194   static inline bool classof(const Value *V) {
195     return isa<Instruction>(V) && classof(cast<Instruction>(V));
196   }
197 };
198
199
200 //===----------------------------------------------------------------------===//
201 //                                 FreeInst Class
202 //===----------------------------------------------------------------------===//
203
204 /// FreeInst - an instruction to deallocate memory
205 ///
206 class FreeInst : public UnaryInstruction {
207   void AssertOK();
208 public:
209   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
211
212   virtual FreeInst *clone() const;
213
214   // Accessor methods for consistency with other memory operations
215   Value *getPointerOperand() { return getOperand(0); }
216   const Value *getPointerOperand() const { return getOperand(0); }
217
218   // Methods for support type inquiry through isa, cast, and dyn_cast:
219   static inline bool classof(const FreeInst *) { return true; }
220   static inline bool classof(const Instruction *I) {
221     return (I->getOpcode() == Instruction::Free);
222   }
223   static inline bool classof(const Value *V) {
224     return isa<Instruction>(V) && classof(cast<Instruction>(V));
225   }
226 };
227
228
229 //===----------------------------------------------------------------------===//
230 //                                LoadInst Class
231 //===----------------------------------------------------------------------===//
232
233 /// LoadInst - an instruction for reading from memory.  This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
235 ///
236 class LoadInst : public UnaryInstruction {
237   void AssertOK();
238 public:
239   LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
240   LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
241   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
242            Instruction *InsertBefore = 0);
243   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
244            unsigned Align, Instruction *InsertBefore = 0);
245   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
246            BasicBlock *InsertAtEnd);
247   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
248            unsigned Align, BasicBlock *InsertAtEnd);
249
250   LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
251   LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
252   explicit LoadInst(Value *Ptr, const char *NameStr = 0,
253                     bool isVolatile = false,  Instruction *InsertBefore = 0);
254   LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
255            BasicBlock *InsertAtEnd);
256
257   /// isVolatile - Return true if this is a load from a volatile memory
258   /// location.
259   ///
260   bool isVolatile() const { return SubclassData & 1; }
261
262   /// setVolatile - Specify whether this is a volatile load or not.
263   ///
264   void setVolatile(bool V) {
265     SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
266   }
267
268   virtual LoadInst *clone() const;
269
270   /// getAlignment - Return the alignment of the access that is being performed
271   ///
272   unsigned getAlignment() const {
273     return (1 << (SubclassData>>1)) >> 1;
274   }
275
276   void setAlignment(unsigned Align);
277
278   Value *getPointerOperand() { return getOperand(0); }
279   const Value *getPointerOperand() const { return getOperand(0); }
280   static unsigned getPointerOperandIndex() { return 0U; }
281
282   unsigned getPointerAddressSpace() const {
283     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
284   }
285   
286   
287   // Methods for support type inquiry through isa, cast, and dyn_cast:
288   static inline bool classof(const LoadInst *) { return true; }
289   static inline bool classof(const Instruction *I) {
290     return I->getOpcode() == Instruction::Load;
291   }
292   static inline bool classof(const Value *V) {
293     return isa<Instruction>(V) && classof(cast<Instruction>(V));
294   }
295 };
296
297
298 //===----------------------------------------------------------------------===//
299 //                                StoreInst Class
300 //===----------------------------------------------------------------------===//
301
302 /// StoreInst - an instruction for storing to memory
303 ///
304 class StoreInst : public Instruction {
305   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
306   void AssertOK();
307 public:
308   // allocate space for exactly two operands
309   void *operator new(size_t s) {
310     return User::operator new(s, 2);
311   }
312   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
313   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
314   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
315             Instruction *InsertBefore = 0);
316   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317             unsigned Align, Instruction *InsertBefore = 0);
318   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
319   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320             unsigned Align, BasicBlock *InsertAtEnd);
321
322
323   /// isVolatile - Return true if this is a load from a volatile memory
324   /// location.
325   ///
326   bool isVolatile() const { return SubclassData & 1; }
327
328   /// setVolatile - Specify whether this is a volatile load or not.
329   ///
330   void setVolatile(bool V) {
331     SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
332   }
333
334   /// Transparently provide more efficient getOperand methods.
335   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
336
337   /// getAlignment - Return the alignment of the access that is being performed
338   ///
339   unsigned getAlignment() const {
340     return (1 << (SubclassData>>1)) >> 1;
341   }
342
343   void setAlignment(unsigned Align);
344
345   virtual StoreInst *clone() const;
346
347   Value *getPointerOperand() { return getOperand(1); }
348   const Value *getPointerOperand() const { return getOperand(1); }
349   static unsigned getPointerOperandIndex() { return 1U; }
350
351   unsigned getPointerAddressSpace() const {
352     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
353   }
354   
355   // Methods for support type inquiry through isa, cast, and dyn_cast:
356   static inline bool classof(const StoreInst *) { return true; }
357   static inline bool classof(const Instruction *I) {
358     return I->getOpcode() == Instruction::Store;
359   }
360   static inline bool classof(const Value *V) {
361     return isa<Instruction>(V) && classof(cast<Instruction>(V));
362   }
363 };
364
365 template <>
366 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
367 };
368
369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
370
371 //===----------------------------------------------------------------------===//
372 //                             GetElementPtrInst Class
373 //===----------------------------------------------------------------------===//
374
375 // checkType - Simple wrapper function to give a better assertion failure
376 // message on bad indexes for a gep instruction.
377 //
378 static inline const Type *checkType(const Type *Ty) {
379   assert(Ty && "Invalid GetElementPtrInst indices for type!");
380   return Ty;
381 }
382
383 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
384 /// access elements of arrays and structs
385 ///
386 class GetElementPtrInst : public Instruction {
387   GetElementPtrInst(const GetElementPtrInst &GEPI);
388   void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
389             const Twine &NameStr);
390   void init(Value *Ptr, Value *Idx, const Twine &NameStr);
391
392   template<typename InputIterator>
393   void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
394             const Twine &NameStr,
395             // This argument ensures that we have an iterator we can
396             // do arithmetic on in constant time
397             std::random_access_iterator_tag) {
398     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
399
400     if (NumIdx > 0) {
401       // This requires that the iterator points to contiguous memory.
402       init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
403                                      // we have to build an array here
404     }
405     else {
406       init(Ptr, 0, NumIdx, NameStr);
407     }
408   }
409
410   /// getIndexedType - Returns the type of the element that would be loaded with
411   /// a load instruction with the specified parameters.
412   ///
413   /// Null is returned if the indices are invalid for the specified
414   /// pointer type.
415   ///
416   template<typename InputIterator>
417   static const Type *getIndexedType(const Type *Ptr,
418                                     InputIterator IdxBegin,
419                                     InputIterator IdxEnd,
420                                     // This argument ensures that we
421                                     // have an iterator we can do
422                                     // arithmetic on in constant time
423                                     std::random_access_iterator_tag) {
424     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
425
426     if (NumIdx > 0)
427       // This requires that the iterator points to contiguous memory.
428       return getIndexedType(Ptr, &*IdxBegin, NumIdx);
429     else
430       return getIndexedType(Ptr, (Value *const*)0, NumIdx);
431   }
432
433   /// Constructors - Create a getelementptr instruction with a base pointer an
434   /// list of indices.  The first ctor can optionally insert before an existing
435   /// instruction, the second appends the new instruction to the specified
436   /// BasicBlock.
437   template<typename InputIterator>
438   inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
439                            InputIterator IdxEnd,
440                            unsigned Values,
441                            const Twine &NameStr,
442                            Instruction *InsertBefore);
443   template<typename InputIterator>
444   inline GetElementPtrInst(Value *Ptr,
445                            InputIterator IdxBegin, InputIterator IdxEnd,
446                            unsigned Values,
447                            const Twine &NameStr, BasicBlock *InsertAtEnd);
448
449   /// Constructors - These two constructors are convenience methods because one
450   /// and two index getelementptr instructions are so common.
451   GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
452                     Instruction *InsertBefore = 0);
453   GetElementPtrInst(Value *Ptr, Value *Idx,
454                     const Twine &NameStr, BasicBlock *InsertAtEnd);
455 public:
456   template<typename InputIterator>
457   static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
458                                    InputIterator IdxEnd,
459                                    const Twine &NameStr = "",
460                                    Instruction *InsertBefore = 0) {
461     typename std::iterator_traits<InputIterator>::difference_type Values =
462       1 + std::distance(IdxBegin, IdxEnd);
463     return new(Values)
464       GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
465   }
466   template<typename InputIterator>
467   static GetElementPtrInst *Create(Value *Ptr,
468                                    InputIterator IdxBegin, InputIterator IdxEnd,
469                                    const Twine &NameStr,
470                                    BasicBlock *InsertAtEnd) {
471     typename std::iterator_traits<InputIterator>::difference_type Values =
472       1 + std::distance(IdxBegin, IdxEnd);
473     return new(Values)
474       GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
475   }
476
477   /// Constructors - These two creators are convenience methods because one
478   /// index getelementptr instructions are so common.
479   static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
480                                    const Twine &NameStr = "",
481                                    Instruction *InsertBefore = 0) {
482     return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
483   }
484   static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
485                                    const Twine &NameStr,
486                                    BasicBlock *InsertAtEnd) {
487     return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
488   }
489
490   /// Create an "inbounds" getelementptr. See the documentation for the
491   /// "inbounds" flag in LangRef.html for details.
492   template<typename InputIterator>
493   static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
494                                            InputIterator IdxEnd,
495                                            const Twine &NameStr = "",
496                                            Instruction *InsertBefore = 0) {
497     GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
498                                     NameStr, InsertBefore);
499     GEP->setIsInBounds(true);
500     return GEP;
501   }
502   template<typename InputIterator>
503   static GetElementPtrInst *CreateInBounds(Value *Ptr,
504                                            InputIterator IdxBegin,
505                                            InputIterator IdxEnd,
506                                            const Twine &NameStr,
507                                            BasicBlock *InsertAtEnd) {
508     GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
509                                     NameStr, InsertAtEnd);
510     GEP->setIsInBounds(true);
511     return GEP;
512   }
513   static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
514                                            const Twine &NameStr = "",
515                                            Instruction *InsertBefore = 0) {
516     GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
517     GEP->setIsInBounds(true);
518     return GEP;
519   }
520   static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
521                                            const Twine &NameStr,
522                                            BasicBlock *InsertAtEnd) {
523     GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
524     GEP->setIsInBounds(true);
525     return GEP;
526   }
527
528   virtual GetElementPtrInst *clone() const;
529
530   /// Transparently provide more efficient getOperand methods.
531   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
532
533   // getType - Overload to return most specific pointer type...
534   const PointerType *getType() const {
535     return reinterpret_cast<const PointerType*>(Instruction::getType());
536   }
537
538   /// getIndexedType - Returns the type of the element that would be loaded with
539   /// a load instruction with the specified parameters.
540   ///
541   /// Null is returned if the indices are invalid for the specified
542   /// pointer type.
543   ///
544   template<typename InputIterator>
545   static const Type *getIndexedType(const Type *Ptr,
546                                     InputIterator IdxBegin,
547                                     InputIterator IdxEnd) {
548     return getIndexedType(Ptr, IdxBegin, IdxEnd,
549                           typename std::iterator_traits<InputIterator>::
550                           iterator_category());
551   }
552
553   static const Type *getIndexedType(const Type *Ptr,
554                                     Value* const *Idx, unsigned NumIdx);
555
556   static const Type *getIndexedType(const Type *Ptr,
557                                     uint64_t const *Idx, unsigned NumIdx);
558
559   static const Type *getIndexedType(const Type *Ptr, Value *Idx);
560
561   inline op_iterator       idx_begin()       { return op_begin()+1; }
562   inline const_op_iterator idx_begin() const { return op_begin()+1; }
563   inline op_iterator       idx_end()         { return op_end(); }
564   inline const_op_iterator idx_end()   const { return op_end(); }
565
566   Value *getPointerOperand() {
567     return getOperand(0);
568   }
569   const Value *getPointerOperand() const {
570     return getOperand(0);
571   }
572   static unsigned getPointerOperandIndex() {
573     return 0U;                      // get index for modifying correct operand
574   }
575   
576   unsigned getPointerAddressSpace() const {
577     return cast<PointerType>(getType())->getAddressSpace();
578   }
579
580   /// getPointerOperandType - Method to return the pointer operand as a
581   /// PointerType.
582   const PointerType *getPointerOperandType() const {
583     return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
584   }
585
586
587   unsigned getNumIndices() const {  // Note: always non-negative
588     return getNumOperands() - 1;
589   }
590
591   bool hasIndices() const {
592     return getNumOperands() > 1;
593   }
594
595   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
596   /// zeros.  If so, the result pointer and the first operand have the same
597   /// value, just potentially different types.
598   bool hasAllZeroIndices() const;
599
600   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
601   /// constant integers.  If so, the result pointer and the first operand have
602   /// a constant offset between them.
603   bool hasAllConstantIndices() const;
604
605   /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
606   /// See LangRef.html for the meaning of inbounds on a getelementptr.
607   void setIsInBounds(bool b = true);
608
609   /// isInBounds - Determine whether the GEP has the inbounds flag.
610   bool isInBounds() const;
611
612   // Methods for support type inquiry through isa, cast, and dyn_cast:
613   static inline bool classof(const GetElementPtrInst *) { return true; }
614   static inline bool classof(const Instruction *I) {
615     return (I->getOpcode() == Instruction::GetElementPtr);
616   }
617   static inline bool classof(const Value *V) {
618     return isa<Instruction>(V) && classof(cast<Instruction>(V));
619   }
620 };
621
622 template <>
623 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
624 };
625
626 template<typename InputIterator>
627 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
628                                      InputIterator IdxBegin,
629                                      InputIterator IdxEnd,
630                                      unsigned Values,
631                                      const Twine &NameStr,
632                                      Instruction *InsertBefore)
633   : Instruction(PointerType::get(checkType(
634                                    getIndexedType(Ptr->getType(),
635                                                   IdxBegin, IdxEnd)),
636                                  cast<PointerType>(Ptr->getType())
637                                    ->getAddressSpace()),
638                 GetElementPtr,
639                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
640                 Values, InsertBefore) {
641   init(Ptr, IdxBegin, IdxEnd, NameStr,
642        typename std::iterator_traits<InputIterator>::iterator_category());
643 }
644 template<typename InputIterator>
645 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
646                                      InputIterator IdxBegin,
647                                      InputIterator IdxEnd,
648                                      unsigned Values,
649                                      const Twine &NameStr,
650                                      BasicBlock *InsertAtEnd)
651   : Instruction(PointerType::get(checkType(
652                                    getIndexedType(Ptr->getType(),
653                                                   IdxBegin, IdxEnd)),
654                                  cast<PointerType>(Ptr->getType())
655                                    ->getAddressSpace()),
656                 GetElementPtr,
657                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
658                 Values, InsertAtEnd) {
659   init(Ptr, IdxBegin, IdxEnd, NameStr,
660        typename std::iterator_traits<InputIterator>::iterator_category());
661 }
662
663
664 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
665
666
667 //===----------------------------------------------------------------------===//
668 //                               ICmpInst Class
669 //===----------------------------------------------------------------------===//
670
671 /// This instruction compares its operands according to the predicate given
672 /// to the constructor. It only operates on integers or pointers. The operands
673 /// must be identical types.
674 /// @brief Represent an integer comparison operator.
675 class ICmpInst: public CmpInst {
676 public:
677   /// @brief Constructor with insert-before-instruction semantics.
678   ICmpInst(
679     Instruction *InsertBefore,  ///< Where to insert
680     Predicate pred,  ///< The predicate to use for the comparison
681     Value *LHS,      ///< The left-hand-side of the expression
682     Value *RHS,      ///< The right-hand-side of the expression
683     const Twine &NameStr = ""  ///< Name of the instruction
684   ) : CmpInst(makeCmpResultType(LHS->getType()),
685               Instruction::ICmp, pred, LHS, RHS, NameStr,
686               InsertBefore) {
687     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
688            pred <= CmpInst::LAST_ICMP_PREDICATE &&
689            "Invalid ICmp predicate value");
690     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
691           "Both operands to ICmp instruction are not of the same type!");
692     // Check that the operands are the right type
693     assert((getOperand(0)->getType()->isIntOrIntVector() ||
694             isa<PointerType>(getOperand(0)->getType())) &&
695            "Invalid operand types for ICmp instruction");
696   }
697
698   /// @brief Constructor with insert-at-end semantics.
699   ICmpInst(
700     BasicBlock &InsertAtEnd, ///< Block to insert into.
701     Predicate pred,  ///< The predicate to use for the comparison
702     Value *LHS,      ///< The left-hand-side of the expression
703     Value *RHS,      ///< The right-hand-side of the expression
704     const Twine &NameStr = ""  ///< Name of the instruction
705   ) : CmpInst(makeCmpResultType(LHS->getType()),
706               Instruction::ICmp, pred, LHS, RHS, NameStr,
707               &InsertAtEnd) {
708     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
709           pred <= CmpInst::LAST_ICMP_PREDICATE &&
710           "Invalid ICmp predicate value");
711     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
712           "Both operands to ICmp instruction are not of the same type!");
713     // Check that the operands are the right type
714     assert((getOperand(0)->getType()->isIntOrIntVector() ||
715             isa<PointerType>(getOperand(0)->getType())) &&
716            "Invalid operand types for ICmp instruction");
717   }
718
719   /// @brief Constructor with no-insertion semantics
720   ICmpInst(
721     Predicate pred, ///< The predicate to use for the comparison
722     Value *LHS,     ///< The left-hand-side of the expression
723     Value *RHS,     ///< The right-hand-side of the expression
724     const Twine &NameStr = "" ///< Name of the instruction
725   ) : CmpInst(makeCmpResultType(LHS->getType()),
726               Instruction::ICmp, pred, LHS, RHS, NameStr) {
727     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
728            pred <= CmpInst::LAST_ICMP_PREDICATE &&
729            "Invalid ICmp predicate value");
730     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
731           "Both operands to ICmp instruction are not of the same type!");
732     // Check that the operands are the right type
733     assert((getOperand(0)->getType()->isIntOrIntVector() ||
734             isa<PointerType>(getOperand(0)->getType())) &&
735            "Invalid operand types for ICmp instruction");
736   }
737
738   /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
739   /// @returns the predicate that would be the result if the operand were
740   /// regarded as signed.
741   /// @brief Return the signed version of the predicate
742   Predicate getSignedPredicate() const {
743     return getSignedPredicate(getPredicate());
744   }
745
746   /// This is a static version that you can use without an instruction.
747   /// @brief Return the signed version of the predicate.
748   static Predicate getSignedPredicate(Predicate pred);
749
750   /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
751   /// @returns the predicate that would be the result if the operand were
752   /// regarded as unsigned.
753   /// @brief Return the unsigned version of the predicate
754   Predicate getUnsignedPredicate() const {
755     return getUnsignedPredicate(getPredicate());
756   }
757
758   /// This is a static version that you can use without an instruction.
759   /// @brief Return the unsigned version of the predicate.
760   static Predicate getUnsignedPredicate(Predicate pred);
761
762   /// isEquality - Return true if this predicate is either EQ or NE.  This also
763   /// tests for commutativity.
764   static bool isEquality(Predicate P) {
765     return P == ICMP_EQ || P == ICMP_NE;
766   }
767
768   /// isEquality - Return true if this predicate is either EQ or NE.  This also
769   /// tests for commutativity.
770   bool isEquality() const {
771     return isEquality(getPredicate());
772   }
773
774   /// @returns true if the predicate of this ICmpInst is commutative
775   /// @brief Determine if this relation is commutative.
776   bool isCommutative() const { return isEquality(); }
777
778   /// isRelational - Return true if the predicate is relational (not EQ or NE).
779   ///
780   bool isRelational() const {
781     return !isEquality();
782   }
783
784   /// isRelational - Return true if the predicate is relational (not EQ or NE).
785   ///
786   static bool isRelational(Predicate P) {
787     return !isEquality(P);
788   }
789
790   /// @returns true if the predicate of this ICmpInst is signed, false otherwise
791   /// @brief Determine if this instruction's predicate is signed.
792   bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
793
794   /// @returns true if the predicate provided is signed, false otherwise
795   /// @brief Determine if the predicate is signed.
796   static bool isSignedPredicate(Predicate pred);
797
798   /// @returns true if the specified compare predicate is
799   /// true when both operands are equal...
800   /// @brief Determine if the icmp is true when both operands are equal
801   static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
802     return pred == ICmpInst::ICMP_EQ  || pred == ICmpInst::ICMP_UGE ||
803            pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
804            pred == ICmpInst::ICMP_SLE;
805   }
806
807   /// @returns true if the specified compare instruction is
808   /// true when both operands are equal...
809   /// @brief Determine if the ICmpInst returns true when both operands are equal
810   bool isTrueWhenEqual() {
811     return isTrueWhenEqual(getPredicate());
812   }
813
814   /// Initialize a set of values that all satisfy the predicate with C.
815   /// @brief Make a ConstantRange for a relation with a constant value.
816   static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
817
818   /// Exchange the two operands to this instruction in such a way that it does
819   /// not modify the semantics of the instruction. The predicate value may be
820   /// changed to retain the same result if the predicate is order dependent
821   /// (e.g. ult).
822   /// @brief Swap operands and adjust predicate.
823   void swapOperands() {
824     SubclassData = getSwappedPredicate();
825     Op<0>().swap(Op<1>());
826   }
827
828   virtual ICmpInst *clone() const;
829
830   // Methods for support type inquiry through isa, cast, and dyn_cast:
831   static inline bool classof(const ICmpInst *) { return true; }
832   static inline bool classof(const Instruction *I) {
833     return I->getOpcode() == Instruction::ICmp;
834   }
835   static inline bool classof(const Value *V) {
836     return isa<Instruction>(V) && classof(cast<Instruction>(V));
837   }
838
839 };
840
841 //===----------------------------------------------------------------------===//
842 //                               FCmpInst Class
843 //===----------------------------------------------------------------------===//
844
845 /// This instruction compares its operands according to the predicate given
846 /// to the constructor. It only operates on floating point values or packed
847 /// vectors of floating point values. The operands must be identical types.
848 /// @brief Represents a floating point comparison operator.
849 class FCmpInst: public CmpInst {
850 public:
851   /// @brief Constructor with insert-before-instruction semantics.
852   FCmpInst(
853     Instruction *InsertBefore, ///< Where to insert
854     Predicate pred,  ///< The predicate to use for the comparison
855     Value *LHS,      ///< The left-hand-side of the expression
856     Value *RHS,      ///< The right-hand-side of the expression
857     const Twine &NameStr = ""  ///< Name of the instruction
858   ) : CmpInst(makeCmpResultType(LHS->getType()),
859               Instruction::FCmp, pred, LHS, RHS, NameStr,
860               InsertBefore) {
861     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
862            "Invalid FCmp predicate value");
863     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
864            "Both operands to FCmp instruction are not of the same type!");
865     // Check that the operands are the right type
866     assert(getOperand(0)->getType()->isFPOrFPVector() &&
867            "Invalid operand types for FCmp instruction");
868   }
869   
870   /// @brief Constructor with insert-at-end semantics.
871   FCmpInst(
872     BasicBlock &InsertAtEnd, ///< Block to insert into.
873     Predicate pred,  ///< The predicate to use for the comparison
874     Value *LHS,      ///< The left-hand-side of the expression
875     Value *RHS,      ///< The right-hand-side of the expression
876     const Twine &NameStr = ""  ///< Name of the instruction
877   ) : CmpInst(makeCmpResultType(LHS->getType()),
878               Instruction::FCmp, pred, LHS, RHS, NameStr,
879               &InsertAtEnd) {
880     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
881            "Invalid FCmp predicate value");
882     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
883            "Both operands to FCmp instruction are not of the same type!");
884     // Check that the operands are the right type
885     assert(getOperand(0)->getType()->isFPOrFPVector() &&
886            "Invalid operand types for FCmp instruction");
887   }
888
889   /// @brief Constructor with no-insertion semantics
890   FCmpInst(
891     Predicate pred, ///< The predicate to use for the comparison
892     Value *LHS,     ///< The left-hand-side of the expression
893     Value *RHS,     ///< The right-hand-side of the expression
894     const Twine &NameStr = "" ///< Name of the instruction
895   ) : CmpInst(makeCmpResultType(LHS->getType()),
896               Instruction::FCmp, pred, LHS, RHS, NameStr) {
897     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
898            "Invalid FCmp predicate value");
899     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
900            "Both operands to FCmp instruction are not of the same type!");
901     // Check that the operands are the right type
902     assert(getOperand(0)->getType()->isFPOrFPVector() &&
903            "Invalid operand types for FCmp instruction");
904   }
905
906   /// @returns true if the predicate of this instruction is EQ or NE.
907   /// @brief Determine if this is an equality predicate.
908   bool isEquality() const {
909     return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
910            SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
911   }
912
913   /// @returns true if the predicate of this instruction is commutative.
914   /// @brief Determine if this is a commutative predicate.
915   bool isCommutative() const {
916     return isEquality() ||
917            SubclassData == FCMP_FALSE ||
918            SubclassData == FCMP_TRUE ||
919            SubclassData == FCMP_ORD ||
920            SubclassData == FCMP_UNO;
921   }
922
923   /// @returns true if the predicate is relational (not EQ or NE).
924   /// @brief Determine if this a relational predicate.
925   bool isRelational() const { return !isEquality(); }
926
927   /// Exchange the two operands to this instruction in such a way that it does
928   /// not modify the semantics of the instruction. The predicate value may be
929   /// changed to retain the same result if the predicate is order dependent
930   /// (e.g. ult).
931   /// @brief Swap operands and adjust predicate.
932   void swapOperands() {
933     SubclassData = getSwappedPredicate();
934     Op<0>().swap(Op<1>());
935   }
936
937   virtual FCmpInst *clone() const;
938
939   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
940   static inline bool classof(const FCmpInst *) { return true; }
941   static inline bool classof(const Instruction *I) {
942     return I->getOpcode() == Instruction::FCmp;
943   }
944   static inline bool classof(const Value *V) {
945     return isa<Instruction>(V) && classof(cast<Instruction>(V));
946   }
947 };
948
949 //===----------------------------------------------------------------------===//
950 //                                 CallInst Class
951 //===----------------------------------------------------------------------===//
952 /// CallInst - This class represents a function call, abstracting a target
953 /// machine's calling convention.  This class uses low bit of the SubClassData
954 /// field to indicate whether or not this is a tail call.  The rest of the bits
955 /// hold the calling convention of the call.
956 ///
957
958 class CallInst : public Instruction {
959   AttrListPtr AttributeList; ///< parameter attributes for call
960   CallInst(const CallInst &CI);
961   void init(Value *Func, Value* const *Params, unsigned NumParams);
962   void init(Value *Func, Value *Actual1, Value *Actual2);
963   void init(Value *Func, Value *Actual);
964   void init(Value *Func);
965
966   template<typename InputIterator>
967   void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
968             const Twine &NameStr,
969             // This argument ensures that we have an iterator we can
970             // do arithmetic on in constant time
971             std::random_access_iterator_tag) {
972     unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
973
974     // This requires that the iterator points to contiguous memory.
975     init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
976     setName(NameStr);
977   }
978
979   /// Construct a CallInst given a range of arguments.  InputIterator
980   /// must be a random-access iterator pointing to contiguous storage
981   /// (e.g. a std::vector<>::iterator).  Checks are made for
982   /// random-accessness but not for contiguous storage as that would
983   /// incur runtime overhead.
984   /// @brief Construct a CallInst from a range of arguments
985   template<typename InputIterator>
986   CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
987            const Twine &NameStr, Instruction *InsertBefore);
988
989   /// Construct a CallInst given a range of arguments.  InputIterator
990   /// must be a random-access iterator pointing to contiguous storage
991   /// (e.g. a std::vector<>::iterator).  Checks are made for
992   /// random-accessness but not for contiguous storage as that would
993   /// incur runtime overhead.
994   /// @brief Construct a CallInst from a range of arguments
995   template<typename InputIterator>
996   inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
997                   const Twine &NameStr, BasicBlock *InsertAtEnd);
998
999   CallInst(Value *F, Value *Actual, const Twine &NameStr,
1000            Instruction *InsertBefore);
1001   CallInst(Value *F, Value *Actual, const Twine &NameStr,
1002            BasicBlock *InsertAtEnd);
1003   explicit CallInst(Value *F, const Twine &NameStr,
1004                     Instruction *InsertBefore);
1005   CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1006 public:
1007   template<typename InputIterator>
1008   static CallInst *Create(Value *Func,
1009                           InputIterator ArgBegin, InputIterator ArgEnd,
1010                           const Twine &NameStr = "",
1011                           Instruction *InsertBefore = 0) {
1012     return new((unsigned)(ArgEnd - ArgBegin + 1))
1013       CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1014   }
1015   template<typename InputIterator>
1016   static CallInst *Create(Value *Func,
1017                           InputIterator ArgBegin, InputIterator ArgEnd,
1018                           const Twine &NameStr, BasicBlock *InsertAtEnd) {
1019     return new((unsigned)(ArgEnd - ArgBegin + 1))
1020       CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1021   }
1022   static CallInst *Create(Value *F, Value *Actual,
1023                           const Twine &NameStr = "",
1024                           Instruction *InsertBefore = 0) {
1025     return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1026   }
1027   static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1028                           BasicBlock *InsertAtEnd) {
1029     return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1030   }
1031   static CallInst *Create(Value *F, const Twine &NameStr = "",
1032                           Instruction *InsertBefore = 0) {
1033     return new(1) CallInst(F, NameStr, InsertBefore);
1034   }
1035   static CallInst *Create(Value *F, const Twine &NameStr,
1036                           BasicBlock *InsertAtEnd) {
1037     return new(1) CallInst(F, NameStr, InsertAtEnd);
1038   }
1039   /// CreateMalloc - Generate the IR for a call to malloc:
1040   /// 1. Compute the malloc call's argument as the specified type's size,
1041   ///    possibly multiplied by the array size if the array size is not
1042   ///    constant 1.
1043   /// 2. Call malloc with that argument.
1044   /// 3. Bitcast the result of the malloc call to the specified type.
1045   static Value *CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
1046                              const Type *AllocTy, Value *ArraySize = 0,
1047                              const Twine &Name = "");
1048   static Value *CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
1049                              const Type *AllocTy, Value *ArraySize = 0,
1050                              Function* MallocF = 0, const Twine &Name = "");
1051
1052   ~CallInst();
1053
1054   bool isTailCall() const           { return SubclassData & 1; }
1055   void setTailCall(bool isTC = true) {
1056     SubclassData = (SubclassData & ~1) | unsigned(isTC);
1057   }
1058
1059   virtual CallInst *clone() const;
1060
1061   /// Provide fast operand accessors
1062   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1063
1064   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1065   /// function call.
1066   CallingConv::ID getCallingConv() const {
1067     return static_cast<CallingConv::ID>(SubclassData >> 1);
1068   }
1069   void setCallingConv(CallingConv::ID CC) {
1070     SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1071   }
1072
1073   /// getAttributes - Return the parameter attributes for this call.
1074   ///
1075   const AttrListPtr &getAttributes() const { return AttributeList; }
1076
1077   /// setAttributes - Set the parameter attributes for this call.
1078   ///
1079   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1080
1081   /// addAttribute - adds the attribute to the list of attributes.
1082   void addAttribute(unsigned i, Attributes attr);
1083
1084   /// removeAttribute - removes the attribute from the list of attributes.
1085   void removeAttribute(unsigned i, Attributes attr);
1086
1087   /// @brief Determine whether the call or the callee has the given attribute.
1088   bool paramHasAttr(unsigned i, Attributes attr) const;
1089
1090   /// @brief Extract the alignment for a call or parameter (0=unknown).
1091   unsigned getParamAlignment(unsigned i) const {
1092     return AttributeList.getParamAlignment(i);
1093   }
1094
1095   /// @brief Determine if the call does not access memory.
1096   bool doesNotAccessMemory() const {
1097     return paramHasAttr(~0, Attribute::ReadNone);
1098   }
1099   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1100     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1101     else removeAttribute(~0, Attribute::ReadNone);
1102   }
1103
1104   /// @brief Determine if the call does not access or only reads memory.
1105   bool onlyReadsMemory() const {
1106     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1107   }
1108   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1109     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1110     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1111   }
1112
1113   /// @brief Determine if the call cannot return.
1114   bool doesNotReturn() const {
1115     return paramHasAttr(~0, Attribute::NoReturn);
1116   }
1117   void setDoesNotReturn(bool DoesNotReturn = true) {
1118     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1119     else removeAttribute(~0, Attribute::NoReturn);
1120   }
1121
1122   /// @brief Determine if the call cannot unwind.
1123   bool doesNotThrow() const {
1124     return paramHasAttr(~0, Attribute::NoUnwind);
1125   }
1126   void setDoesNotThrow(bool DoesNotThrow = true) {
1127     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1128     else removeAttribute(~0, Attribute::NoUnwind);
1129   }
1130
1131   /// @brief Determine if the call returns a structure through first
1132   /// pointer argument.
1133   bool hasStructRetAttr() const {
1134     // Be friendly and also check the callee.
1135     return paramHasAttr(1, Attribute::StructRet);
1136   }
1137
1138   /// @brief Determine if any call argument is an aggregate passed by value.
1139   bool hasByValArgument() const {
1140     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1141   }
1142
1143   /// getCalledFunction - Return the function called, or null if this is an
1144   /// indirect function invocation.
1145   ///
1146   Function *getCalledFunction() const {
1147     return dyn_cast<Function>(Op<0>());
1148   }
1149
1150   /// getCalledValue - Get a pointer to the function that is invoked by this
1151   /// instruction
1152   const Value *getCalledValue() const { return Op<0>(); }
1153         Value *getCalledValue()       { return Op<0>(); }
1154
1155   /// setCalledFunction - Set the function called
1156   void setCalledFunction(Value* Fn) {
1157     Op<0>() = Fn;
1158   }
1159
1160   // Methods for support type inquiry through isa, cast, and dyn_cast:
1161   static inline bool classof(const CallInst *) { return true; }
1162   static inline bool classof(const Instruction *I) {
1163     return I->getOpcode() == Instruction::Call;
1164   }
1165   static inline bool classof(const Value *V) {
1166     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1167   }
1168 };
1169
1170 template <>
1171 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1172 };
1173
1174 template<typename InputIterator>
1175 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1176                    const Twine &NameStr, BasicBlock *InsertAtEnd)
1177   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1178                                    ->getElementType())->getReturnType(),
1179                 Instruction::Call,
1180                 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1181                 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1182   init(Func, ArgBegin, ArgEnd, NameStr,
1183        typename std::iterator_traits<InputIterator>::iterator_category());
1184 }
1185
1186 template<typename InputIterator>
1187 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1188                    const Twine &NameStr, Instruction *InsertBefore)
1189   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1190                                    ->getElementType())->getReturnType(),
1191                 Instruction::Call,
1192                 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1193                 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1194   init(Func, ArgBegin, ArgEnd, NameStr,
1195        typename std::iterator_traits<InputIterator>::iterator_category());
1196 }
1197
1198 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1199
1200 //===----------------------------------------------------------------------===//
1201 //                               SelectInst Class
1202 //===----------------------------------------------------------------------===//
1203
1204 /// SelectInst - This class represents the LLVM 'select' instruction.
1205 ///
1206 class SelectInst : public Instruction {
1207   void init(Value *C, Value *S1, Value *S2) {
1208     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1209     Op<0>() = C;
1210     Op<1>() = S1;
1211     Op<2>() = S2;
1212   }
1213
1214   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1215              Instruction *InsertBefore)
1216     : Instruction(S1->getType(), Instruction::Select,
1217                   &Op<0>(), 3, InsertBefore) {
1218     init(C, S1, S2);
1219     setName(NameStr);
1220   }
1221   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1222              BasicBlock *InsertAtEnd)
1223     : Instruction(S1->getType(), Instruction::Select,
1224                   &Op<0>(), 3, InsertAtEnd) {
1225     init(C, S1, S2);
1226     setName(NameStr);
1227   }
1228 public:
1229   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1230                             const Twine &NameStr = "",
1231                             Instruction *InsertBefore = 0) {
1232     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1233   }
1234   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1235                             const Twine &NameStr,
1236                             BasicBlock *InsertAtEnd) {
1237     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1238   }
1239
1240   const Value *getCondition() const { return Op<0>(); }
1241   const Value *getTrueValue() const { return Op<1>(); }
1242   const Value *getFalseValue() const { return Op<2>(); }
1243   Value *getCondition() { return Op<0>(); }
1244   Value *getTrueValue() { return Op<1>(); }
1245   Value *getFalseValue() { return Op<2>(); }
1246   
1247   /// areInvalidOperands - Return a string if the specified operands are invalid
1248   /// for a select operation, otherwise return null.
1249   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1250
1251   /// Transparently provide more efficient getOperand methods.
1252   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1253
1254   OtherOps getOpcode() const {
1255     return static_cast<OtherOps>(Instruction::getOpcode());
1256   }
1257
1258   virtual SelectInst *clone() const;
1259
1260   // Methods for support type inquiry through isa, cast, and dyn_cast:
1261   static inline bool classof(const SelectInst *) { return true; }
1262   static inline bool classof(const Instruction *I) {
1263     return I->getOpcode() == Instruction::Select;
1264   }
1265   static inline bool classof(const Value *V) {
1266     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1267   }
1268 };
1269
1270 template <>
1271 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1272 };
1273
1274 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1275
1276 //===----------------------------------------------------------------------===//
1277 //                                VAArgInst Class
1278 //===----------------------------------------------------------------------===//
1279
1280 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1281 /// an argument of the specified type given a va_list and increments that list
1282 ///
1283 class VAArgInst : public UnaryInstruction {
1284 public:
1285   VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1286              Instruction *InsertBefore = 0)
1287     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1288     setName(NameStr);
1289   }
1290   VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1291             BasicBlock *InsertAtEnd)
1292     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1293     setName(NameStr);
1294   }
1295
1296   virtual VAArgInst *clone() const;
1297
1298   // Methods for support type inquiry through isa, cast, and dyn_cast:
1299   static inline bool classof(const VAArgInst *) { return true; }
1300   static inline bool classof(const Instruction *I) {
1301     return I->getOpcode() == VAArg;
1302   }
1303   static inline bool classof(const Value *V) {
1304     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1305   }
1306 };
1307
1308 //===----------------------------------------------------------------------===//
1309 //                                ExtractElementInst Class
1310 //===----------------------------------------------------------------------===//
1311
1312 /// ExtractElementInst - This instruction extracts a single (scalar)
1313 /// element from a VectorType value
1314 ///
1315 class ExtractElementInst : public Instruction {
1316   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1317                      Instruction *InsertBefore = 0);
1318   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1319                      BasicBlock *InsertAtEnd);
1320 public:
1321   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1322                                    const Twine &NameStr = "",
1323                                    Instruction *InsertBefore = 0) {
1324     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1325   }
1326   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1327                                    const Twine &NameStr,
1328                                    BasicBlock *InsertAtEnd) {
1329     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1330   }
1331
1332   /// isValidOperands - Return true if an extractelement instruction can be
1333   /// formed with the specified operands.
1334   static bool isValidOperands(const Value *Vec, const Value *Idx);
1335
1336   virtual ExtractElementInst *clone() const;
1337
1338   Value *getVectorOperand() { return Op<0>(); }
1339   Value *getIndexOperand() { return Op<1>(); }
1340   const Value *getVectorOperand() const { return Op<0>(); }
1341   const Value *getIndexOperand() const { return Op<1>(); }
1342   
1343   const VectorType *getVectorOperandType() const {
1344     return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1345   }
1346   
1347   
1348   /// Transparently provide more efficient getOperand methods.
1349   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1350
1351   // Methods for support type inquiry through isa, cast, and dyn_cast:
1352   static inline bool classof(const ExtractElementInst *) { return true; }
1353   static inline bool classof(const Instruction *I) {
1354     return I->getOpcode() == Instruction::ExtractElement;
1355   }
1356   static inline bool classof(const Value *V) {
1357     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1358   }
1359 };
1360
1361 template <>
1362 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1363 };
1364
1365 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1366
1367 //===----------------------------------------------------------------------===//
1368 //                                InsertElementInst Class
1369 //===----------------------------------------------------------------------===//
1370
1371 /// InsertElementInst - This instruction inserts a single (scalar)
1372 /// element into a VectorType value
1373 ///
1374 class InsertElementInst : public Instruction {
1375   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1376                     const Twine &NameStr = "",
1377                     Instruction *InsertBefore = 0);
1378   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1379                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1380 public:
1381   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1382                                    const Twine &NameStr = "",
1383                                    Instruction *InsertBefore = 0) {
1384     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1385   }
1386   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1387                                    const Twine &NameStr,
1388                                    BasicBlock *InsertAtEnd) {
1389     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1390   }
1391
1392   /// isValidOperands - Return true if an insertelement instruction can be
1393   /// formed with the specified operands.
1394   static bool isValidOperands(const Value *Vec, const Value *NewElt,
1395                               const Value *Idx);
1396
1397   virtual InsertElementInst *clone() const;
1398
1399   /// getType - Overload to return most specific vector type.
1400   ///
1401   const VectorType *getType() const {
1402     return reinterpret_cast<const VectorType*>(Instruction::getType());
1403   }
1404
1405   /// Transparently provide more efficient getOperand methods.
1406   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1407
1408   // Methods for support type inquiry through isa, cast, and dyn_cast:
1409   static inline bool classof(const InsertElementInst *) { return true; }
1410   static inline bool classof(const Instruction *I) {
1411     return I->getOpcode() == Instruction::InsertElement;
1412   }
1413   static inline bool classof(const Value *V) {
1414     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1415   }
1416 };
1417
1418 template <>
1419 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1420 };
1421
1422 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1423
1424 //===----------------------------------------------------------------------===//
1425 //                           ShuffleVectorInst Class
1426 //===----------------------------------------------------------------------===//
1427
1428 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1429 /// input vectors.
1430 ///
1431 class ShuffleVectorInst : public Instruction {
1432 public:
1433   // allocate space for exactly three operands
1434   void *operator new(size_t s) {
1435     return User::operator new(s, 3);
1436   }
1437   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1438                     const Twine &NameStr = "",
1439                     Instruction *InsertBefor = 0);
1440   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1441                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1442
1443   /// isValidOperands - Return true if a shufflevector instruction can be
1444   /// formed with the specified operands.
1445   static bool isValidOperands(const Value *V1, const Value *V2,
1446                               const Value *Mask);
1447
1448   virtual ShuffleVectorInst *clone() const;
1449
1450   /// getType - Overload to return most specific vector type.
1451   ///
1452   const VectorType *getType() const {
1453     return reinterpret_cast<const VectorType*>(Instruction::getType());
1454   }
1455
1456   /// Transparently provide more efficient getOperand methods.
1457   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1458
1459   /// getMaskValue - Return the index from the shuffle mask for the specified
1460   /// output result.  This is either -1 if the element is undef or a number less
1461   /// than 2*numelements.
1462   int getMaskValue(unsigned i) const;
1463
1464   // Methods for support type inquiry through isa, cast, and dyn_cast:
1465   static inline bool classof(const ShuffleVectorInst *) { return true; }
1466   static inline bool classof(const Instruction *I) {
1467     return I->getOpcode() == Instruction::ShuffleVector;
1468   }
1469   static inline bool classof(const Value *V) {
1470     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1471   }
1472 };
1473
1474 template <>
1475 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1476 };
1477
1478 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1479
1480 //===----------------------------------------------------------------------===//
1481 //                                ExtractValueInst Class
1482 //===----------------------------------------------------------------------===//
1483
1484 /// ExtractValueInst - This instruction extracts a struct member or array
1485 /// element value from an aggregate value.
1486 ///
1487 class ExtractValueInst : public UnaryInstruction {
1488   SmallVector<unsigned, 4> Indices;
1489
1490   ExtractValueInst(const ExtractValueInst &EVI);
1491   void init(const unsigned *Idx, unsigned NumIdx,
1492             const Twine &NameStr);
1493   void init(unsigned Idx, const Twine &NameStr);
1494
1495   template<typename InputIterator>
1496   void init(InputIterator IdxBegin, InputIterator IdxEnd,
1497             const Twine &NameStr,
1498             // This argument ensures that we have an iterator we can
1499             // do arithmetic on in constant time
1500             std::random_access_iterator_tag) {
1501     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1502
1503     // There's no fundamental reason why we require at least one index
1504     // (other than weirdness with &*IdxBegin being invalid; see
1505     // getelementptr's init routine for example). But there's no
1506     // present need to support it.
1507     assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1508
1509     // This requires that the iterator points to contiguous memory.
1510     init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1511                                          // we have to build an array here
1512   }
1513
1514   /// getIndexedType - Returns the type of the element that would be extracted
1515   /// with an extractvalue instruction with the specified parameters.
1516   ///
1517   /// Null is returned if the indices are invalid for the specified
1518   /// pointer type.
1519   ///
1520   static const Type *getIndexedType(const Type *Agg,
1521                                     const unsigned *Idx, unsigned NumIdx);
1522
1523   template<typename InputIterator>
1524   static const Type *getIndexedType(const Type *Ptr,
1525                                     InputIterator IdxBegin,
1526                                     InputIterator IdxEnd,
1527                                     // This argument ensures that we
1528                                     // have an iterator we can do
1529                                     // arithmetic on in constant time
1530                                     std::random_access_iterator_tag) {
1531     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1532
1533     if (NumIdx > 0)
1534       // This requires that the iterator points to contiguous memory.
1535       return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1536     else
1537       return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1538   }
1539
1540   /// Constructors - Create a extractvalue instruction with a base aggregate
1541   /// value and a list of indices.  The first ctor can optionally insert before
1542   /// an existing instruction, the second appends the new instruction to the
1543   /// specified BasicBlock.
1544   template<typename InputIterator>
1545   inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1546                           InputIterator IdxEnd,
1547                           const Twine &NameStr,
1548                           Instruction *InsertBefore);
1549   template<typename InputIterator>
1550   inline ExtractValueInst(Value *Agg,
1551                           InputIterator IdxBegin, InputIterator IdxEnd,
1552                           const Twine &NameStr, BasicBlock *InsertAtEnd);
1553
1554   // allocate space for exactly one operand
1555   void *operator new(size_t s) {
1556     return User::operator new(s, 1);
1557   }
1558
1559 public:
1560   template<typename InputIterator>
1561   static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1562                                   InputIterator IdxEnd,
1563                                   const Twine &NameStr = "",
1564                                   Instruction *InsertBefore = 0) {
1565     return new
1566       ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1567   }
1568   template<typename InputIterator>
1569   static ExtractValueInst *Create(Value *Agg,
1570                                   InputIterator IdxBegin, InputIterator IdxEnd,
1571                                   const Twine &NameStr,
1572                                   BasicBlock *InsertAtEnd) {
1573     return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1574   }
1575
1576   /// Constructors - These two creators are convenience methods because one
1577   /// index extractvalue instructions are much more common than those with
1578   /// more than one.
1579   static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1580                                   const Twine &NameStr = "",
1581                                   Instruction *InsertBefore = 0) {
1582     unsigned Idxs[1] = { Idx };
1583     return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1584   }
1585   static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1586                                   const Twine &NameStr,
1587                                   BasicBlock *InsertAtEnd) {
1588     unsigned Idxs[1] = { Idx };
1589     return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1590   }
1591
1592   virtual ExtractValueInst *clone() const;
1593
1594   /// getIndexedType - Returns the type of the element that would be extracted
1595   /// with an extractvalue instruction with the specified parameters.
1596   ///
1597   /// Null is returned if the indices are invalid for the specified
1598   /// pointer type.
1599   ///
1600   template<typename InputIterator>
1601   static const Type *getIndexedType(const Type *Ptr,
1602                                     InputIterator IdxBegin,
1603                                     InputIterator IdxEnd) {
1604     return getIndexedType(Ptr, IdxBegin, IdxEnd,
1605                           typename std::iterator_traits<InputIterator>::
1606                           iterator_category());
1607   }
1608   static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1609
1610   typedef const unsigned* idx_iterator;
1611   inline idx_iterator idx_begin() const { return Indices.begin(); }
1612   inline idx_iterator idx_end()   const { return Indices.end(); }
1613
1614   Value *getAggregateOperand() {
1615     return getOperand(0);
1616   }
1617   const Value *getAggregateOperand() const {
1618     return getOperand(0);
1619   }
1620   static unsigned getAggregateOperandIndex() {
1621     return 0U;                      // get index for modifying correct operand
1622   }
1623
1624   unsigned getNumIndices() const {  // Note: always non-negative
1625     return (unsigned)Indices.size();
1626   }
1627
1628   bool hasIndices() const {
1629     return true;
1630   }
1631
1632   // Methods for support type inquiry through isa, cast, and dyn_cast:
1633   static inline bool classof(const ExtractValueInst *) { return true; }
1634   static inline bool classof(const Instruction *I) {
1635     return I->getOpcode() == Instruction::ExtractValue;
1636   }
1637   static inline bool classof(const Value *V) {
1638     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1639   }
1640 };
1641
1642 template<typename InputIterator>
1643 ExtractValueInst::ExtractValueInst(Value *Agg,
1644                                    InputIterator IdxBegin,
1645                                    InputIterator IdxEnd,
1646                                    const Twine &NameStr,
1647                                    Instruction *InsertBefore)
1648   : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1649                                               IdxBegin, IdxEnd)),
1650                      ExtractValue, Agg, InsertBefore) {
1651   init(IdxBegin, IdxEnd, NameStr,
1652        typename std::iterator_traits<InputIterator>::iterator_category());
1653 }
1654 template<typename InputIterator>
1655 ExtractValueInst::ExtractValueInst(Value *Agg,
1656                                    InputIterator IdxBegin,
1657                                    InputIterator IdxEnd,
1658                                    const Twine &NameStr,
1659                                    BasicBlock *InsertAtEnd)
1660   : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1661                                               IdxBegin, IdxEnd)),
1662                      ExtractValue, Agg, InsertAtEnd) {
1663   init(IdxBegin, IdxEnd, NameStr,
1664        typename std::iterator_traits<InputIterator>::iterator_category());
1665 }
1666
1667
1668 //===----------------------------------------------------------------------===//
1669 //                                InsertValueInst Class
1670 //===----------------------------------------------------------------------===//
1671
1672 /// InsertValueInst - This instruction inserts a struct field of array element
1673 /// value into an aggregate value.
1674 ///
1675 class InsertValueInst : public Instruction {
1676   SmallVector<unsigned, 4> Indices;
1677
1678   void *operator new(size_t, unsigned); // Do not implement
1679   InsertValueInst(const InsertValueInst &IVI);
1680   void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1681             const Twine &NameStr);
1682   void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1683
1684   template<typename InputIterator>
1685   void init(Value *Agg, Value *Val,
1686             InputIterator IdxBegin, InputIterator IdxEnd,
1687             const Twine &NameStr,
1688             // This argument ensures that we have an iterator we can
1689             // do arithmetic on in constant time
1690             std::random_access_iterator_tag) {
1691     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1692
1693     // There's no fundamental reason why we require at least one index
1694     // (other than weirdness with &*IdxBegin being invalid; see
1695     // getelementptr's init routine for example). But there's no
1696     // present need to support it.
1697     assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1698
1699     // This requires that the iterator points to contiguous memory.
1700     init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1701                                               // we have to build an array here
1702   }
1703
1704   /// Constructors - Create a insertvalue instruction with a base aggregate
1705   /// value, a value to insert, and a list of indices.  The first ctor can
1706   /// optionally insert before an existing instruction, the second appends
1707   /// the new instruction to the specified BasicBlock.
1708   template<typename InputIterator>
1709   inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1710                          InputIterator IdxEnd,
1711                          const Twine &NameStr,
1712                          Instruction *InsertBefore);
1713   template<typename InputIterator>
1714   inline InsertValueInst(Value *Agg, Value *Val,
1715                          InputIterator IdxBegin, InputIterator IdxEnd,
1716                          const Twine &NameStr, BasicBlock *InsertAtEnd);
1717
1718   /// Constructors - These two constructors are convenience methods because one
1719   /// and two index insertvalue instructions are so common.
1720   InsertValueInst(Value *Agg, Value *Val,
1721                   unsigned Idx, const Twine &NameStr = "",
1722                   Instruction *InsertBefore = 0);
1723   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1724                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1725 public:
1726   // allocate space for exactly two operands
1727   void *operator new(size_t s) {
1728     return User::operator new(s, 2);
1729   }
1730
1731   template<typename InputIterator>
1732   static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1733                                  InputIterator IdxEnd,
1734                                  const Twine &NameStr = "",
1735                                  Instruction *InsertBefore = 0) {
1736     return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1737                                NameStr, InsertBefore);
1738   }
1739   template<typename InputIterator>
1740   static InsertValueInst *Create(Value *Agg, Value *Val,
1741                                  InputIterator IdxBegin, InputIterator IdxEnd,
1742                                  const Twine &NameStr,
1743                                  BasicBlock *InsertAtEnd) {
1744     return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1745                                NameStr, InsertAtEnd);
1746   }
1747
1748   /// Constructors - These two creators are convenience methods because one
1749   /// index insertvalue instructions are much more common than those with
1750   /// more than one.
1751   static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1752                                  const Twine &NameStr = "",
1753                                  Instruction *InsertBefore = 0) {
1754     return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1755   }
1756   static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1757                                  const Twine &NameStr,
1758                                  BasicBlock *InsertAtEnd) {
1759     return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1760   }
1761
1762   virtual InsertValueInst *clone() const;
1763
1764   /// Transparently provide more efficient getOperand methods.
1765   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1766
1767   typedef const unsigned* idx_iterator;
1768   inline idx_iterator idx_begin() const { return Indices.begin(); }
1769   inline idx_iterator idx_end()   const { return Indices.end(); }
1770
1771   Value *getAggregateOperand() {
1772     return getOperand(0);
1773   }
1774   const Value *getAggregateOperand() const {
1775     return getOperand(0);
1776   }
1777   static unsigned getAggregateOperandIndex() {
1778     return 0U;                      // get index for modifying correct operand
1779   }
1780
1781   Value *getInsertedValueOperand() {
1782     return getOperand(1);
1783   }
1784   const Value *getInsertedValueOperand() const {
1785     return getOperand(1);
1786   }
1787   static unsigned getInsertedValueOperandIndex() {
1788     return 1U;                      // get index for modifying correct operand
1789   }
1790
1791   unsigned getNumIndices() const {  // Note: always non-negative
1792     return (unsigned)Indices.size();
1793   }
1794
1795   bool hasIndices() const {
1796     return true;
1797   }
1798
1799   // Methods for support type inquiry through isa, cast, and dyn_cast:
1800   static inline bool classof(const InsertValueInst *) { return true; }
1801   static inline bool classof(const Instruction *I) {
1802     return I->getOpcode() == Instruction::InsertValue;
1803   }
1804   static inline bool classof(const Value *V) {
1805     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1806   }
1807 };
1808
1809 template <>
1810 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1811 };
1812
1813 template<typename InputIterator>
1814 InsertValueInst::InsertValueInst(Value *Agg,
1815                                  Value *Val,
1816                                  InputIterator IdxBegin,
1817                                  InputIterator IdxEnd,
1818                                  const Twine &NameStr,
1819                                  Instruction *InsertBefore)
1820   : Instruction(Agg->getType(), InsertValue,
1821                 OperandTraits<InsertValueInst>::op_begin(this),
1822                 2, InsertBefore) {
1823   init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1824        typename std::iterator_traits<InputIterator>::iterator_category());
1825 }
1826 template<typename InputIterator>
1827 InsertValueInst::InsertValueInst(Value *Agg,
1828                                  Value *Val,
1829                                  InputIterator IdxBegin,
1830                                  InputIterator IdxEnd,
1831                                  const Twine &NameStr,
1832                                  BasicBlock *InsertAtEnd)
1833   : Instruction(Agg->getType(), InsertValue,
1834                 OperandTraits<InsertValueInst>::op_begin(this),
1835                 2, InsertAtEnd) {
1836   init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1837        typename std::iterator_traits<InputIterator>::iterator_category());
1838 }
1839
1840 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1841
1842 //===----------------------------------------------------------------------===//
1843 //                               PHINode Class
1844 //===----------------------------------------------------------------------===//
1845
1846 // PHINode - The PHINode class is used to represent the magical mystical PHI
1847 // node, that can not exist in nature, but can be synthesized in a computer
1848 // scientist's overactive imagination.
1849 //
1850 class PHINode : public Instruction {
1851   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
1852   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1853   /// the number actually in use.
1854   unsigned ReservedSpace;
1855   PHINode(const PHINode &PN);
1856   // allocate space for exactly zero operands
1857   void *operator new(size_t s) {
1858     return User::operator new(s, 0);
1859   }
1860   explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1861                    Instruction *InsertBefore = 0)
1862     : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1863       ReservedSpace(0) {
1864     setName(NameStr);
1865   }
1866
1867   PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1868     : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1869       ReservedSpace(0) {
1870     setName(NameStr);
1871   }
1872 public:
1873   static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1874                          Instruction *InsertBefore = 0) {
1875     return new PHINode(Ty, NameStr, InsertBefore);
1876   }
1877   static PHINode *Create(const Type *Ty, const Twine &NameStr,
1878                          BasicBlock *InsertAtEnd) {
1879     return new PHINode(Ty, NameStr, InsertAtEnd);
1880   }
1881   ~PHINode();
1882
1883   /// reserveOperandSpace - This method can be used to avoid repeated
1884   /// reallocation of PHI operand lists by reserving space for the correct
1885   /// number of operands before adding them.  Unlike normal vector reserves,
1886   /// this method can also be used to trim the operand space.
1887   void reserveOperandSpace(unsigned NumValues) {
1888     resizeOperands(NumValues*2);
1889   }
1890
1891   virtual PHINode *clone() const;
1892
1893   /// Provide fast operand accessors
1894   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1895
1896   /// getNumIncomingValues - Return the number of incoming edges
1897   ///
1898   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1899
1900   /// getIncomingValue - Return incoming value number x
1901   ///
1902   Value *getIncomingValue(unsigned i) const {
1903     assert(i*2 < getNumOperands() && "Invalid value number!");
1904     return getOperand(i*2);
1905   }
1906   void setIncomingValue(unsigned i, Value *V) {
1907     assert(i*2 < getNumOperands() && "Invalid value number!");
1908     setOperand(i*2, V);
1909   }
1910   static unsigned getOperandNumForIncomingValue(unsigned i) {
1911     return i*2;
1912   }
1913   static unsigned getIncomingValueNumForOperand(unsigned i) {
1914     assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1915     return i/2;
1916   }
1917
1918   /// getIncomingBlock - Return incoming basic block #i.
1919   ///
1920   BasicBlock *getIncomingBlock(unsigned i) const {
1921     return cast<BasicBlock>(getOperand(i*2+1));
1922   }
1923   
1924   /// getIncomingBlock - Return incoming basic block corresponding
1925   /// to an operand of the PHI.
1926   ///
1927   BasicBlock *getIncomingBlock(const Use &U) const {
1928     assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1929     return cast<BasicBlock>((&U + 1)->get());
1930   }
1931   
1932   /// getIncomingBlock - Return incoming basic block corresponding
1933   /// to value use iterator.
1934   ///
1935   template <typename U>
1936   BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1937     return getIncomingBlock(I.getUse());
1938   }
1939   
1940   
1941   void setIncomingBlock(unsigned i, BasicBlock *BB) {
1942     setOperand(i*2+1, BB);
1943   }
1944   static unsigned getOperandNumForIncomingBlock(unsigned i) {
1945     return i*2+1;
1946   }
1947   static unsigned getIncomingBlockNumForOperand(unsigned i) {
1948     assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1949     return i/2;
1950   }
1951
1952   /// addIncoming - Add an incoming value to the end of the PHI list
1953   ///
1954   void addIncoming(Value *V, BasicBlock *BB) {
1955     assert(V && "PHI node got a null value!");
1956     assert(BB && "PHI node got a null basic block!");
1957     assert(getType() == V->getType() &&
1958            "All operands to PHI node must be the same type as the PHI node!");
1959     unsigned OpNo = NumOperands;
1960     if (OpNo+2 > ReservedSpace)
1961       resizeOperands(0);  // Get more space!
1962     // Initialize some new operands.
1963     NumOperands = OpNo+2;
1964     OperandList[OpNo] = V;
1965     OperandList[OpNo+1] = BB;
1966   }
1967
1968   /// removeIncomingValue - Remove an incoming value.  This is useful if a
1969   /// predecessor basic block is deleted.  The value removed is returned.
1970   ///
1971   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1972   /// is true), the PHI node is destroyed and any uses of it are replaced with
1973   /// dummy values.  The only time there should be zero incoming values to a PHI
1974   /// node is when the block is dead, so this strategy is sound.
1975   ///
1976   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1977
1978   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1979     int Idx = getBasicBlockIndex(BB);
1980     assert(Idx >= 0 && "Invalid basic block argument to remove!");
1981     return removeIncomingValue(Idx, DeletePHIIfEmpty);
1982   }
1983
1984   /// getBasicBlockIndex - Return the first index of the specified basic
1985   /// block in the value list for this PHI.  Returns -1 if no instance.
1986   ///
1987   int getBasicBlockIndex(const BasicBlock *BB) const {
1988     Use *OL = OperandList;
1989     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1990       if (OL[i+1].get() == BB) return i/2;
1991     return -1;
1992   }
1993
1994   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1995     return getIncomingValue(getBasicBlockIndex(BB));
1996   }
1997
1998   /// hasConstantValue - If the specified PHI node always merges together the
1999   /// same value, return the value, otherwise return null.
2000   ///
2001   /// If the PHI has undef operands, but all the rest of the operands are
2002   /// some unique value, return that value if it can be proved that the
2003   /// value dominates the PHI. If DT is null, use a conservative check,
2004   /// otherwise use DT to test for dominance.
2005   ///
2006   Value *hasConstantValue(DominatorTree *DT = 0) const;
2007
2008   /// Methods for support type inquiry through isa, cast, and dyn_cast:
2009   static inline bool classof(const PHINode *) { return true; }
2010   static inline bool classof(const Instruction *I) {
2011     return I->getOpcode() == Instruction::PHI;
2012   }
2013   static inline bool classof(const Value *V) {
2014     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2015   }
2016  private:
2017   void resizeOperands(unsigned NumOperands);
2018 };
2019
2020 template <>
2021 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2022 };
2023
2024 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2025
2026
2027 //===----------------------------------------------------------------------===//
2028 //                               ReturnInst Class
2029 //===----------------------------------------------------------------------===//
2030
2031 //===---------------------------------------------------------------------------
2032 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2033 /// does not continue in this function any longer.
2034 ///
2035 class ReturnInst : public TerminatorInst {
2036   ReturnInst(const ReturnInst &RI);
2037
2038 private:
2039   // ReturnInst constructors:
2040   // ReturnInst()                  - 'ret void' instruction
2041   // ReturnInst(    null)          - 'ret void' instruction
2042   // ReturnInst(Value* X)          - 'ret X'    instruction
2043   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2044   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2045   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2046   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2047   //
2048   // NOTE: If the Value* passed is of type void then the constructor behaves as
2049   // if it was passed NULL.
2050   explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2051                       Instruction *InsertBefore = 0);
2052   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2053   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2054 public:
2055   static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2056                             Instruction *InsertBefore = 0) {
2057     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2058   }
2059   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2060                             BasicBlock *InsertAtEnd) {
2061     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2062   }
2063   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2064     return new(0) ReturnInst(C, InsertAtEnd);
2065   }
2066   virtual ~ReturnInst();
2067
2068   virtual ReturnInst *clone() const;
2069
2070   /// Provide fast operand accessors
2071   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2072
2073   /// Convenience accessor
2074   Value *getReturnValue(unsigned n = 0) const {
2075     return n < getNumOperands()
2076       ? getOperand(n)
2077       : 0;
2078   }
2079
2080   unsigned getNumSuccessors() const { return 0; }
2081
2082   // Methods for support type inquiry through isa, cast, and dyn_cast:
2083   static inline bool classof(const ReturnInst *) { return true; }
2084   static inline bool classof(const Instruction *I) {
2085     return (I->getOpcode() == Instruction::Ret);
2086   }
2087   static inline bool classof(const Value *V) {
2088     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2089   }
2090  private:
2091   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2092   virtual unsigned getNumSuccessorsV() const;
2093   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2094 };
2095
2096 template <>
2097 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
2098 };
2099
2100 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2101
2102 //===----------------------------------------------------------------------===//
2103 //                               BranchInst Class
2104 //===----------------------------------------------------------------------===//
2105
2106 //===---------------------------------------------------------------------------
2107 /// BranchInst - Conditional or Unconditional Branch instruction.
2108 ///
2109 class BranchInst : public TerminatorInst {
2110   /// Ops list - Branches are strange.  The operands are ordered:
2111   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2112   /// they don't have to check for cond/uncond branchness. These are mostly
2113   /// accessed relative from op_end().
2114   BranchInst(const BranchInst &BI);
2115   void AssertOK();
2116   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2117   // BranchInst(BB *B)                           - 'br B'
2118   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2119   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2120   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2121   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2122   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2123   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2124   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2125              Instruction *InsertBefore = 0);
2126   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2127   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2128              BasicBlock *InsertAtEnd);
2129 public:
2130   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2131     return new(1, true) BranchInst(IfTrue, InsertBefore);
2132   }
2133   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2134                             Value *Cond, Instruction *InsertBefore = 0) {
2135     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2136   }
2137   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2138     return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2139   }
2140   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2141                             Value *Cond, BasicBlock *InsertAtEnd) {
2142     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2143   }
2144
2145   ~BranchInst();
2146
2147   /// Transparently provide more efficient getOperand methods.
2148   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2149
2150   virtual BranchInst *clone() const;
2151
2152   bool isUnconditional() const { return getNumOperands() == 1; }
2153   bool isConditional()   const { return getNumOperands() == 3; }
2154
2155   Value *getCondition() const {
2156     assert(isConditional() && "Cannot get condition of an uncond branch!");
2157     return Op<-3>();
2158   }
2159
2160   void setCondition(Value *V) {
2161     assert(isConditional() && "Cannot set condition of unconditional branch!");
2162     Op<-3>() = V;
2163   }
2164
2165   // setUnconditionalDest - Change the current branch to an unconditional branch
2166   // targeting the specified block.
2167   // FIXME: Eliminate this ugly method.
2168   void setUnconditionalDest(BasicBlock *Dest) {
2169     Op<-1>() = Dest;
2170     if (isConditional()) {  // Convert this to an uncond branch.
2171       Op<-2>() = 0;
2172       Op<-3>() = 0;
2173       NumOperands = 1;
2174       OperandList = op_begin();
2175     }
2176   }
2177
2178   unsigned getNumSuccessors() const { return 1+isConditional(); }
2179
2180   BasicBlock *getSuccessor(unsigned i) const {
2181     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2182     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2183   }
2184
2185   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2186     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2187     *(&Op<-1>() - idx) = NewSucc;
2188   }
2189
2190   // Methods for support type inquiry through isa, cast, and dyn_cast:
2191   static inline bool classof(const BranchInst *) { return true; }
2192   static inline bool classof(const Instruction *I) {
2193     return (I->getOpcode() == Instruction::Br);
2194   }
2195   static inline bool classof(const Value *V) {
2196     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2197   }
2198 private:
2199   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2200   virtual unsigned getNumSuccessorsV() const;
2201   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2202 };
2203
2204 template <>
2205 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2206
2207 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2208
2209 //===----------------------------------------------------------------------===//
2210 //                               SwitchInst Class
2211 //===----------------------------------------------------------------------===//
2212
2213 //===---------------------------------------------------------------------------
2214 /// SwitchInst - Multiway switch
2215 ///
2216 class SwitchInst : public TerminatorInst {
2217   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2218   unsigned ReservedSpace;
2219   // Operand[0]    = Value to switch on
2220   // Operand[1]    = Default basic block destination
2221   // Operand[2n  ] = Value to match
2222   // Operand[2n+1] = BasicBlock to go to on match
2223   SwitchInst(const SwitchInst &RI);
2224   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2225   void resizeOperands(unsigned No);
2226   // allocate space for exactly zero operands
2227   void *operator new(size_t s) {
2228     return User::operator new(s, 0);
2229   }
2230   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2231   /// switch on and a default destination.  The number of additional cases can
2232   /// be specified here to make memory allocation more efficient.  This
2233   /// constructor can also autoinsert before another instruction.
2234   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2235              Instruction *InsertBefore = 0);
2236
2237   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2238   /// switch on and a default destination.  The number of additional cases can
2239   /// be specified here to make memory allocation more efficient.  This
2240   /// constructor also autoinserts at the end of the specified BasicBlock.
2241   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2242              BasicBlock *InsertAtEnd);
2243 public:
2244   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2245                             unsigned NumCases, Instruction *InsertBefore = 0) {
2246     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2247   }
2248   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2249                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2250     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2251   }
2252   ~SwitchInst();
2253
2254   /// Provide fast operand accessors
2255   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2256
2257   // Accessor Methods for Switch stmt
2258   Value *getCondition() const { return getOperand(0); }
2259   void setCondition(Value *V) { setOperand(0, V); }
2260
2261   BasicBlock *getDefaultDest() const {
2262     return cast<BasicBlock>(getOperand(1));
2263   }
2264
2265   /// getNumCases - return the number of 'cases' in this switch instruction.
2266   /// Note that case #0 is always the default case.
2267   unsigned getNumCases() const {
2268     return getNumOperands()/2;
2269   }
2270
2271   /// getCaseValue - Return the specified case value.  Note that case #0, the
2272   /// default destination, does not have a case value.
2273   ConstantInt *getCaseValue(unsigned i) {
2274     assert(i && i < getNumCases() && "Illegal case value to get!");
2275     return getSuccessorValue(i);
2276   }
2277
2278   /// getCaseValue - Return the specified case value.  Note that case #0, the
2279   /// default destination, does not have a case value.
2280   const ConstantInt *getCaseValue(unsigned i) const {
2281     assert(i && i < getNumCases() && "Illegal case value to get!");
2282     return getSuccessorValue(i);
2283   }
2284
2285   /// findCaseValue - Search all of the case values for the specified constant.
2286   /// If it is explicitly handled, return the case number of it, otherwise
2287   /// return 0 to indicate that it is handled by the default handler.
2288   unsigned findCaseValue(const ConstantInt *C) const {
2289     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2290       if (getCaseValue(i) == C)
2291         return i;
2292     return 0;
2293   }
2294
2295   /// findCaseDest - Finds the unique case value for a given successor. Returns
2296   /// null if the successor is not found, not unique, or is the default case.
2297   ConstantInt *findCaseDest(BasicBlock *BB) {
2298     if (BB == getDefaultDest()) return NULL;
2299
2300     ConstantInt *CI = NULL;
2301     for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2302       if (getSuccessor(i) == BB) {
2303         if (CI) return NULL;   // Multiple cases lead to BB.
2304         else CI = getCaseValue(i);
2305       }
2306     }
2307     return CI;
2308   }
2309
2310   /// addCase - Add an entry to the switch instruction...
2311   ///
2312   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2313
2314   /// removeCase - This method removes the specified successor from the switch
2315   /// instruction.  Note that this cannot be used to remove the default
2316   /// destination (successor #0).
2317   ///
2318   void removeCase(unsigned idx);
2319
2320   virtual SwitchInst *clone() const;
2321
2322   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2323   BasicBlock *getSuccessor(unsigned idx) const {
2324     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2325     return cast<BasicBlock>(getOperand(idx*2+1));
2326   }
2327   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2328     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2329     setOperand(idx*2+1, NewSucc);
2330   }
2331
2332   // getSuccessorValue - Return the value associated with the specified
2333   // successor.
2334   ConstantInt *getSuccessorValue(unsigned idx) const {
2335     assert(idx < getNumSuccessors() && "Successor # out of range!");
2336     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2337   }
2338
2339   // Methods for support type inquiry through isa, cast, and dyn_cast:
2340   static inline bool classof(const SwitchInst *) { return true; }
2341   static inline bool classof(const Instruction *I) {
2342     return I->getOpcode() == Instruction::Switch;
2343   }
2344   static inline bool classof(const Value *V) {
2345     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2346   }
2347 private:
2348   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2349   virtual unsigned getNumSuccessorsV() const;
2350   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2351 };
2352
2353 template <>
2354 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2355 };
2356
2357 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2358
2359
2360 //===----------------------------------------------------------------------===//
2361 //                               InvokeInst Class
2362 //===----------------------------------------------------------------------===//
2363
2364 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2365 /// calling convention of the call.
2366 ///
2367 class InvokeInst : public TerminatorInst {
2368   AttrListPtr AttributeList;
2369   InvokeInst(const InvokeInst &BI);
2370   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2371             Value* const *Args, unsigned NumArgs);
2372
2373   template<typename InputIterator>
2374   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2375             InputIterator ArgBegin, InputIterator ArgEnd,
2376             const Twine &NameStr,
2377             // This argument ensures that we have an iterator we can
2378             // do arithmetic on in constant time
2379             std::random_access_iterator_tag) {
2380     unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2381
2382     // This requires that the iterator points to contiguous memory.
2383     init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2384     setName(NameStr);
2385   }
2386
2387   /// Construct an InvokeInst given a range of arguments.
2388   /// InputIterator must be a random-access iterator pointing to
2389   /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
2390   /// made for random-accessness but not for contiguous storage as
2391   /// that would incur runtime overhead.
2392   ///
2393   /// @brief Construct an InvokeInst from a range of arguments
2394   template<typename InputIterator>
2395   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2396                     InputIterator ArgBegin, InputIterator ArgEnd,
2397                     unsigned Values,
2398                     const Twine &NameStr, Instruction *InsertBefore);
2399
2400   /// Construct an InvokeInst given a range of arguments.
2401   /// InputIterator must be a random-access iterator pointing to
2402   /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
2403   /// made for random-accessness but not for contiguous storage as
2404   /// that would incur runtime overhead.
2405   ///
2406   /// @brief Construct an InvokeInst from a range of arguments
2407   template<typename InputIterator>
2408   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2409                     InputIterator ArgBegin, InputIterator ArgEnd,
2410                     unsigned Values,
2411                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2412 public:
2413   template<typename InputIterator>
2414   static InvokeInst *Create(Value *Func,
2415                             BasicBlock *IfNormal, BasicBlock *IfException,
2416                             InputIterator ArgBegin, InputIterator ArgEnd,
2417                             const Twine &NameStr = "",
2418                             Instruction *InsertBefore = 0) {
2419     unsigned Values(ArgEnd - ArgBegin + 3);
2420     return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2421                                   Values, NameStr, InsertBefore);
2422   }
2423   template<typename InputIterator>
2424   static InvokeInst *Create(Value *Func,
2425                             BasicBlock *IfNormal, BasicBlock *IfException,
2426                             InputIterator ArgBegin, InputIterator ArgEnd,
2427                             const Twine &NameStr,
2428                             BasicBlock *InsertAtEnd) {
2429     unsigned Values(ArgEnd - ArgBegin + 3);
2430     return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2431                                   Values, NameStr, InsertAtEnd);
2432   }
2433
2434   virtual InvokeInst *clone() const;
2435
2436   /// Provide fast operand accessors
2437   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2438
2439   /// getCallingConv/setCallingConv - Get or set the calling convention of this
2440   /// function call.
2441   CallingConv::ID getCallingConv() const {
2442     return static_cast<CallingConv::ID>(SubclassData);
2443   }
2444   void setCallingConv(CallingConv::ID CC) {
2445     SubclassData = static_cast<unsigned>(CC);
2446   }
2447
2448   /// getAttributes - Return the parameter attributes for this invoke.
2449   ///
2450   const AttrListPtr &getAttributes() const { return AttributeList; }
2451
2452   /// setAttributes - Set the parameter attributes for this invoke.
2453   ///
2454   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2455
2456   /// addAttribute - adds the attribute to the list of attributes.
2457   void addAttribute(unsigned i, Attributes attr);
2458
2459   /// removeAttribute - removes the attribute from the list of attributes.
2460   void removeAttribute(unsigned i, Attributes attr);
2461
2462   /// @brief Determine whether the call or the callee has the given attribute.
2463   bool paramHasAttr(unsigned i, Attributes attr) const;
2464
2465   /// @brief Extract the alignment for a call or parameter (0=unknown).
2466   unsigned getParamAlignment(unsigned i) const {
2467     return AttributeList.getParamAlignment(i);
2468   }
2469
2470   /// @brief Determine if the call does not access memory.
2471   bool doesNotAccessMemory() const {
2472     return paramHasAttr(~0, Attribute::ReadNone);
2473   }
2474   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2475     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2476     else removeAttribute(~0, Attribute::ReadNone);
2477   }
2478
2479   /// @brief Determine if the call does not access or only reads memory.
2480   bool onlyReadsMemory() const {
2481     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2482   }
2483   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2484     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2485     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2486   }
2487
2488   /// @brief Determine if the call cannot return.
2489   bool doesNotReturn() const {
2490     return paramHasAttr(~0, Attribute::NoReturn);
2491   }
2492   void setDoesNotReturn(bool DoesNotReturn = true) {
2493     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2494     else removeAttribute(~0, Attribute::NoReturn);
2495   }
2496
2497   /// @brief Determine if the call cannot unwind.
2498   bool doesNotThrow() const {
2499     return paramHasAttr(~0, Attribute::NoUnwind);
2500   }
2501   void setDoesNotThrow(bool DoesNotThrow = true) {
2502     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2503     else removeAttribute(~0, Attribute::NoUnwind);
2504   }
2505
2506   /// @brief Determine if the call returns a structure through first
2507   /// pointer argument.
2508   bool hasStructRetAttr() const {
2509     // Be friendly and also check the callee.
2510     return paramHasAttr(1, Attribute::StructRet);
2511   }
2512
2513   /// @brief Determine if any call argument is an aggregate passed by value.
2514   bool hasByValArgument() const {
2515     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2516   }
2517
2518   /// getCalledFunction - Return the function called, or null if this is an
2519   /// indirect function invocation.
2520   ///
2521   Function *getCalledFunction() const {
2522     return dyn_cast<Function>(getOperand(0));
2523   }
2524
2525   /// getCalledValue - Get a pointer to the function that is invoked by this
2526   /// instruction
2527   const Value *getCalledValue() const { return getOperand(0); }
2528         Value *getCalledValue()       { return getOperand(0); }
2529
2530   // get*Dest - Return the destination basic blocks...
2531   BasicBlock *getNormalDest() const {
2532     return cast<BasicBlock>(getOperand(1));
2533   }
2534   BasicBlock *getUnwindDest() const {
2535     return cast<BasicBlock>(getOperand(2));
2536   }
2537   void setNormalDest(BasicBlock *B) {
2538     setOperand(1, B);
2539   }
2540
2541   void setUnwindDest(BasicBlock *B) {
2542     setOperand(2, B);
2543   }
2544
2545   BasicBlock *getSuccessor(unsigned i) const {
2546     assert(i < 2 && "Successor # out of range for invoke!");
2547     return i == 0 ? getNormalDest() : getUnwindDest();
2548   }
2549
2550   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2551     assert(idx < 2 && "Successor # out of range for invoke!");
2552     setOperand(idx+1, NewSucc);
2553   }
2554
2555   unsigned getNumSuccessors() const { return 2; }
2556
2557   // Methods for support type inquiry through isa, cast, and dyn_cast:
2558   static inline bool classof(const InvokeInst *) { return true; }
2559   static inline bool classof(const Instruction *I) {
2560     return (I->getOpcode() == Instruction::Invoke);
2561   }
2562   static inline bool classof(const Value *V) {
2563     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2564   }
2565 private:
2566   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2567   virtual unsigned getNumSuccessorsV() const;
2568   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2569 };
2570
2571 template <>
2572 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2573 };
2574
2575 template<typename InputIterator>
2576 InvokeInst::InvokeInst(Value *Func,
2577                        BasicBlock *IfNormal, BasicBlock *IfException,
2578                        InputIterator ArgBegin, InputIterator ArgEnd,
2579                        unsigned Values,
2580                        const Twine &NameStr, Instruction *InsertBefore)
2581   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2582                                       ->getElementType())->getReturnType(),
2583                    Instruction::Invoke,
2584                    OperandTraits<InvokeInst>::op_end(this) - Values,
2585                    Values, InsertBefore) {
2586   init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2587        typename std::iterator_traits<InputIterator>::iterator_category());
2588 }
2589 template<typename InputIterator>
2590 InvokeInst::InvokeInst(Value *Func,
2591                        BasicBlock *IfNormal, BasicBlock *IfException,
2592                        InputIterator ArgBegin, InputIterator ArgEnd,
2593                        unsigned Values,
2594                        const Twine &NameStr, BasicBlock *InsertAtEnd)
2595   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2596                                       ->getElementType())->getReturnType(),
2597                    Instruction::Invoke,
2598                    OperandTraits<InvokeInst>::op_end(this) - Values,
2599                    Values, InsertAtEnd) {
2600   init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2601        typename std::iterator_traits<InputIterator>::iterator_category());
2602 }
2603
2604 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2605
2606 //===----------------------------------------------------------------------===//
2607 //                              UnwindInst Class
2608 //===----------------------------------------------------------------------===//
2609
2610 //===---------------------------------------------------------------------------
2611 /// UnwindInst - Immediately exit the current function, unwinding the stack
2612 /// until an invoke instruction is found.
2613 ///
2614 class UnwindInst : public TerminatorInst {
2615   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2616 public:
2617   // allocate space for exactly zero operands
2618   void *operator new(size_t s) {
2619     return User::operator new(s, 0);
2620   }
2621   explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2622   explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2623
2624   virtual UnwindInst *clone() const;
2625
2626   unsigned getNumSuccessors() const { return 0; }
2627
2628   // Methods for support type inquiry through isa, cast, and dyn_cast:
2629   static inline bool classof(const UnwindInst *) { return true; }
2630   static inline bool classof(const Instruction *I) {
2631     return I->getOpcode() == Instruction::Unwind;
2632   }
2633   static inline bool classof(const Value *V) {
2634     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2635   }
2636 private:
2637   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2638   virtual unsigned getNumSuccessorsV() const;
2639   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2640 };
2641
2642 //===----------------------------------------------------------------------===//
2643 //                           UnreachableInst Class
2644 //===----------------------------------------------------------------------===//
2645
2646 //===---------------------------------------------------------------------------
2647 /// UnreachableInst - This function has undefined behavior.  In particular, the
2648 /// presence of this instruction indicates some higher level knowledge that the
2649 /// end of the block cannot be reached.
2650 ///
2651 class UnreachableInst : public TerminatorInst {
2652   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2653 public:
2654   // allocate space for exactly zero operands
2655   void *operator new(size_t s) {
2656     return User::operator new(s, 0);
2657   }
2658   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2659   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2660
2661   virtual UnreachableInst *clone() const;
2662
2663   unsigned getNumSuccessors() const { return 0; }
2664
2665   // Methods for support type inquiry through isa, cast, and dyn_cast:
2666   static inline bool classof(const UnreachableInst *) { return true; }
2667   static inline bool classof(const Instruction *I) {
2668     return I->getOpcode() == Instruction::Unreachable;
2669   }
2670   static inline bool classof(const Value *V) {
2671     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2672   }
2673 private:
2674   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2675   virtual unsigned getNumSuccessorsV() const;
2676   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2677 };
2678
2679 //===----------------------------------------------------------------------===//
2680 //                                 TruncInst Class
2681 //===----------------------------------------------------------------------===//
2682
2683 /// @brief This class represents a truncation of integer types.
2684 class TruncInst : public CastInst {
2685 public:
2686   /// @brief Constructor with insert-before-instruction semantics
2687   TruncInst(
2688     Value *S,                     ///< The value to be truncated
2689     const Type *Ty,               ///< The (smaller) type to truncate to
2690     const Twine &NameStr = "", ///< A name for the new instruction
2691     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2692   );
2693
2694   /// @brief Constructor with insert-at-end-of-block semantics
2695   TruncInst(
2696     Value *S,                     ///< The value to be truncated
2697     const Type *Ty,               ///< The (smaller) type to truncate to
2698     const Twine &NameStr,   ///< A name for the new instruction
2699     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2700   );
2701
2702   /// @brief Clone an identical TruncInst
2703   virtual TruncInst *clone() const;
2704
2705   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2706   static inline bool classof(const TruncInst *) { return true; }
2707   static inline bool classof(const Instruction *I) {
2708     return I->getOpcode() == Trunc;
2709   }
2710   static inline bool classof(const Value *V) {
2711     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2712   }
2713 };
2714
2715 //===----------------------------------------------------------------------===//
2716 //                                 ZExtInst Class
2717 //===----------------------------------------------------------------------===//
2718
2719 /// @brief This class represents zero extension of integer types.
2720 class ZExtInst : public CastInst {
2721 public:
2722   /// @brief Constructor with insert-before-instruction semantics
2723   ZExtInst(
2724     Value *S,                     ///< The value to be zero extended
2725     const Type *Ty,               ///< The type to zero extend to
2726     const Twine &NameStr = "", ///< A name for the new instruction
2727     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2728   );
2729
2730   /// @brief Constructor with insert-at-end semantics.
2731   ZExtInst(
2732     Value *S,                     ///< The value to be zero extended
2733     const Type *Ty,               ///< The type to zero extend to
2734     const Twine &NameStr,   ///< A name for the new instruction
2735     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2736   );
2737
2738   /// @brief Clone an identical ZExtInst
2739   virtual ZExtInst *clone() const;
2740
2741   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2742   static inline bool classof(const ZExtInst *) { return true; }
2743   static inline bool classof(const Instruction *I) {
2744     return I->getOpcode() == ZExt;
2745   }
2746   static inline bool classof(const Value *V) {
2747     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2748   }
2749 };
2750
2751 //===----------------------------------------------------------------------===//
2752 //                                 SExtInst Class
2753 //===----------------------------------------------------------------------===//
2754
2755 /// @brief This class represents a sign extension of integer types.
2756 class SExtInst : public CastInst {
2757 public:
2758   /// @brief Constructor with insert-before-instruction semantics
2759   SExtInst(
2760     Value *S,                     ///< The value to be sign extended
2761     const Type *Ty,               ///< The type to sign extend to
2762     const Twine &NameStr = "", ///< A name for the new instruction
2763     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2764   );
2765
2766   /// @brief Constructor with insert-at-end-of-block semantics
2767   SExtInst(
2768     Value *S,                     ///< The value to be sign extended
2769     const Type *Ty,               ///< The type to sign extend to
2770     const Twine &NameStr,   ///< A name for the new instruction
2771     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2772   );
2773
2774   /// @brief Clone an identical SExtInst
2775   virtual SExtInst *clone() const;
2776
2777   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2778   static inline bool classof(const SExtInst *) { return true; }
2779   static inline bool classof(const Instruction *I) {
2780     return I->getOpcode() == SExt;
2781   }
2782   static inline bool classof(const Value *V) {
2783     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2784   }
2785 };
2786
2787 //===----------------------------------------------------------------------===//
2788 //                                 FPTruncInst Class
2789 //===----------------------------------------------------------------------===//
2790
2791 /// @brief This class represents a truncation of floating point types.
2792 class FPTruncInst : public CastInst {
2793 public:
2794   /// @brief Constructor with insert-before-instruction semantics
2795   FPTruncInst(
2796     Value *S,                     ///< The value to be truncated
2797     const Type *Ty,               ///< The type to truncate to
2798     const Twine &NameStr = "", ///< A name for the new instruction
2799     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2800   );
2801
2802   /// @brief Constructor with insert-before-instruction semantics
2803   FPTruncInst(
2804     Value *S,                     ///< The value to be truncated
2805     const Type *Ty,               ///< The type to truncate to
2806     const Twine &NameStr,   ///< A name for the new instruction
2807     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2808   );
2809
2810   /// @brief Clone an identical FPTruncInst
2811   virtual FPTruncInst *clone() const;
2812
2813   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2814   static inline bool classof(const FPTruncInst *) { return true; }
2815   static inline bool classof(const Instruction *I) {
2816     return I->getOpcode() == FPTrunc;
2817   }
2818   static inline bool classof(const Value *V) {
2819     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2820   }
2821 };
2822
2823 //===----------------------------------------------------------------------===//
2824 //                                 FPExtInst Class
2825 //===----------------------------------------------------------------------===//
2826
2827 /// @brief This class represents an extension of floating point types.
2828 class FPExtInst : public CastInst {
2829 public:
2830   /// @brief Constructor with insert-before-instruction semantics
2831   FPExtInst(
2832     Value *S,                     ///< The value to be extended
2833     const Type *Ty,               ///< The type to extend to
2834     const Twine &NameStr = "", ///< A name for the new instruction
2835     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2836   );
2837
2838   /// @brief Constructor with insert-at-end-of-block semantics
2839   FPExtInst(
2840     Value *S,                     ///< The value to be extended
2841     const Type *Ty,               ///< The type to extend to
2842     const Twine &NameStr,   ///< A name for the new instruction
2843     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2844   );
2845
2846   /// @brief Clone an identical FPExtInst
2847   virtual FPExtInst *clone() const;
2848
2849   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2850   static inline bool classof(const FPExtInst *) { return true; }
2851   static inline bool classof(const Instruction *I) {
2852     return I->getOpcode() == FPExt;
2853   }
2854   static inline bool classof(const Value *V) {
2855     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2856   }
2857 };
2858
2859 //===----------------------------------------------------------------------===//
2860 //                                 UIToFPInst Class
2861 //===----------------------------------------------------------------------===//
2862
2863 /// @brief This class represents a cast unsigned integer to floating point.
2864 class UIToFPInst : public CastInst {
2865 public:
2866   /// @brief Constructor with insert-before-instruction semantics
2867   UIToFPInst(
2868     Value *S,                     ///< The value to be converted
2869     const Type *Ty,               ///< The type to convert to
2870     const Twine &NameStr = "", ///< A name for the new instruction
2871     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2872   );
2873
2874   /// @brief Constructor with insert-at-end-of-block semantics
2875   UIToFPInst(
2876     Value *S,                     ///< The value to be converted
2877     const Type *Ty,               ///< The type to convert to
2878     const Twine &NameStr,   ///< A name for the new instruction
2879     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2880   );
2881
2882   /// @brief Clone an identical UIToFPInst
2883   virtual UIToFPInst *clone() const;
2884
2885   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2886   static inline bool classof(const UIToFPInst *) { return true; }
2887   static inline bool classof(const Instruction *I) {
2888     return I->getOpcode() == UIToFP;
2889   }
2890   static inline bool classof(const Value *V) {
2891     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2892   }
2893 };
2894
2895 //===----------------------------------------------------------------------===//
2896 //                                 SIToFPInst Class
2897 //===----------------------------------------------------------------------===//
2898
2899 /// @brief This class represents a cast from signed integer to floating point.
2900 class SIToFPInst : public CastInst {
2901 public:
2902   /// @brief Constructor with insert-before-instruction semantics
2903   SIToFPInst(
2904     Value *S,                     ///< The value to be converted
2905     const Type *Ty,               ///< The type to convert to
2906     const Twine &NameStr = "", ///< A name for the new instruction
2907     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2908   );
2909
2910   /// @brief Constructor with insert-at-end-of-block semantics
2911   SIToFPInst(
2912     Value *S,                     ///< The value to be converted
2913     const Type *Ty,               ///< The type to convert to
2914     const Twine &NameStr,   ///< A name for the new instruction
2915     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2916   );
2917
2918   /// @brief Clone an identical SIToFPInst
2919   virtual SIToFPInst *clone() const;
2920
2921   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2922   static inline bool classof(const SIToFPInst *) { return true; }
2923   static inline bool classof(const Instruction *I) {
2924     return I->getOpcode() == SIToFP;
2925   }
2926   static inline bool classof(const Value *V) {
2927     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2928   }
2929 };
2930
2931 //===----------------------------------------------------------------------===//
2932 //                                 FPToUIInst Class
2933 //===----------------------------------------------------------------------===//
2934
2935 /// @brief This class represents a cast from floating point to unsigned integer
2936 class FPToUIInst  : public CastInst {
2937 public:
2938   /// @brief Constructor with insert-before-instruction semantics
2939   FPToUIInst(
2940     Value *S,                     ///< The value to be converted
2941     const Type *Ty,               ///< The type to convert to
2942     const Twine &NameStr = "", ///< A name for the new instruction
2943     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2944   );
2945
2946   /// @brief Constructor with insert-at-end-of-block semantics
2947   FPToUIInst(
2948     Value *S,                     ///< The value to be converted
2949     const Type *Ty,               ///< The type to convert to
2950     const Twine &NameStr,   ///< A name for the new instruction
2951     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
2952   );
2953
2954   /// @brief Clone an identical FPToUIInst
2955   virtual FPToUIInst *clone() const;
2956
2957   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2958   static inline bool classof(const FPToUIInst *) { return true; }
2959   static inline bool classof(const Instruction *I) {
2960     return I->getOpcode() == FPToUI;
2961   }
2962   static inline bool classof(const Value *V) {
2963     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2964   }
2965 };
2966
2967 //===----------------------------------------------------------------------===//
2968 //                                 FPToSIInst Class
2969 //===----------------------------------------------------------------------===//
2970
2971 /// @brief This class represents a cast from floating point to signed integer.
2972 class FPToSIInst  : public CastInst {
2973 public:
2974   /// @brief Constructor with insert-before-instruction semantics
2975   FPToSIInst(
2976     Value *S,                     ///< The value to be converted
2977     const Type *Ty,               ///< The type to convert to
2978     const Twine &NameStr = "", ///< A name for the new instruction
2979     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2980   );
2981
2982   /// @brief Constructor with insert-at-end-of-block semantics
2983   FPToSIInst(
2984     Value *S,                     ///< The value to be converted
2985     const Type *Ty,               ///< The type to convert to
2986     const Twine &NameStr,   ///< A name for the new instruction
2987     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2988   );
2989
2990   /// @brief Clone an identical FPToSIInst
2991   virtual FPToSIInst *clone() const;
2992
2993   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2994   static inline bool classof(const FPToSIInst *) { return true; }
2995   static inline bool classof(const Instruction *I) {
2996     return I->getOpcode() == FPToSI;
2997   }
2998   static inline bool classof(const Value *V) {
2999     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3000   }
3001 };
3002
3003 //===----------------------------------------------------------------------===//
3004 //                                 IntToPtrInst Class
3005 //===----------------------------------------------------------------------===//
3006
3007 /// @brief This class represents a cast from an integer to a pointer.
3008 class IntToPtrInst : public CastInst {
3009 public:
3010   /// @brief Constructor with insert-before-instruction semantics
3011   IntToPtrInst(
3012     Value *S,                     ///< The value to be converted
3013     const Type *Ty,               ///< The type to convert to
3014     const Twine &NameStr = "", ///< A name for the new instruction
3015     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3016   );
3017
3018   /// @brief Constructor with insert-at-end-of-block semantics
3019   IntToPtrInst(
3020     Value *S,                     ///< The value to be converted
3021     const Type *Ty,               ///< The type to convert to
3022     const Twine &NameStr,   ///< A name for the new instruction
3023     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3024   );
3025
3026   /// @brief Clone an identical IntToPtrInst
3027   virtual IntToPtrInst *clone() const;
3028
3029   // Methods for support type inquiry through isa, cast, and dyn_cast:
3030   static inline bool classof(const IntToPtrInst *) { return true; }
3031   static inline bool classof(const Instruction *I) {
3032     return I->getOpcode() == IntToPtr;
3033   }
3034   static inline bool classof(const Value *V) {
3035     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3036   }
3037 };
3038
3039 //===----------------------------------------------------------------------===//
3040 //                                 PtrToIntInst Class
3041 //===----------------------------------------------------------------------===//
3042
3043 /// @brief This class represents a cast from a pointer to an integer
3044 class PtrToIntInst : public CastInst {
3045 public:
3046   /// @brief Constructor with insert-before-instruction semantics
3047   PtrToIntInst(
3048     Value *S,                     ///< The value to be converted
3049     const Type *Ty,               ///< The type to convert to
3050     const Twine &NameStr = "", ///< A name for the new instruction
3051     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3052   );
3053
3054   /// @brief Constructor with insert-at-end-of-block semantics
3055   PtrToIntInst(
3056     Value *S,                     ///< The value to be converted
3057     const Type *Ty,               ///< The type to convert to
3058     const Twine &NameStr,   ///< A name for the new instruction
3059     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3060   );
3061
3062   /// @brief Clone an identical PtrToIntInst
3063   virtual PtrToIntInst *clone() const;
3064
3065   // Methods for support type inquiry through isa, cast, and dyn_cast:
3066   static inline bool classof(const PtrToIntInst *) { return true; }
3067   static inline bool classof(const Instruction *I) {
3068     return I->getOpcode() == PtrToInt;
3069   }
3070   static inline bool classof(const Value *V) {
3071     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3072   }
3073 };
3074
3075 //===----------------------------------------------------------------------===//
3076 //                             BitCastInst Class
3077 //===----------------------------------------------------------------------===//
3078
3079 /// @brief This class represents a no-op cast from one type to another.
3080 class BitCastInst : public CastInst {
3081 public:
3082   /// @brief Constructor with insert-before-instruction semantics
3083   BitCastInst(
3084     Value *S,                     ///< The value to be casted
3085     const Type *Ty,               ///< The type to casted to
3086     const Twine &NameStr = "", ///< A name for the new instruction
3087     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3088   );
3089
3090   /// @brief Constructor with insert-at-end-of-block semantics
3091   BitCastInst(
3092     Value *S,                     ///< The value to be casted
3093     const Type *Ty,               ///< The type to casted to
3094     const Twine &NameStr,      ///< A name for the new instruction
3095     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3096   );
3097
3098   /// @brief Clone an identical BitCastInst
3099   virtual BitCastInst *clone() const;
3100
3101   // Methods for support type inquiry through isa, cast, and dyn_cast:
3102   static inline bool classof(const BitCastInst *) { return true; }
3103   static inline bool classof(const Instruction *I) {
3104     return I->getOpcode() == BitCast;
3105   }
3106   static inline bool classof(const Value *V) {
3107     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3108   }
3109 };
3110
3111 } // End llvm namespace
3112
3113 #endif