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