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