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