Add in some interfaces that will allow easier access to the pointer address space.
[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/Support/IntegersSubset.h"
24 #include "llvm/Support/IntegersSubsetMapping.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <iterator>
29
30 namespace llvm {
31
32 class ConstantInt;
33 class ConstantRange;
34 class APInt;
35 class LLVMContext;
36
37 enum AtomicOrdering {
38   NotAtomic = 0,
39   Unordered = 1,
40   Monotonic = 2,
41   // Consume = 3,  // Not specified yet.
42   Acquire = 4,
43   Release = 5,
44   AcquireRelease = 6,
45   SequentiallyConsistent = 7
46 };
47
48 enum SynchronizationScope {
49   SingleThread = 0,
50   CrossThread = 1
51 };
52
53 //===----------------------------------------------------------------------===//
54 //                                AllocaInst Class
55 //===----------------------------------------------------------------------===//
56
57 /// AllocaInst - an instruction to allocate memory on the stack
58 ///
59 class AllocaInst : public UnaryInstruction {
60 protected:
61   virtual AllocaInst *clone_impl() const;
62 public:
63   explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
64                       const Twine &Name = "", Instruction *InsertBefore = 0);
65   AllocaInst(Type *Ty, Value *ArraySize,
66              const Twine &Name, BasicBlock *InsertAtEnd);
67
68   AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
69   AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
70
71   AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
72              const Twine &Name = "", Instruction *InsertBefore = 0);
73   AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
74              const Twine &Name, BasicBlock *InsertAtEnd);
75
76   // Out of line virtual method, so the vtable, etc. has a home.
77   virtual ~AllocaInst();
78
79   /// isArrayAllocation - Return true if there is an allocation size parameter
80   /// to the allocation instruction that is not 1.
81   ///
82   bool isArrayAllocation() const;
83
84   /// getArraySize - Get the number of elements allocated. For a simple
85   /// allocation of a single element, this will return a constant 1 value.
86   ///
87   const Value *getArraySize() const { return getOperand(0); }
88   Value *getArraySize() { return getOperand(0); }
89
90   /// getType - Overload to return most specific pointer type
91   ///
92   PointerType *getType() const {
93     return reinterpret_cast<PointerType*>(Instruction::getType());
94   }
95
96   /// getAllocatedType - Return the type that is being allocated by the
97   /// instruction.
98   ///
99   Type *getAllocatedType() const;
100
101   /// getAlignment - Return the alignment of the memory that is being allocated
102   /// by the instruction.
103   ///
104   unsigned getAlignment() const {
105     return (1u << getSubclassDataFromInstruction()) >> 1;
106   }
107   void setAlignment(unsigned Align);
108
109   /// isStaticAlloca - Return true if this alloca is in the entry block of the
110   /// function and is a constant size.  If so, the code generator will fold it
111   /// into the prolog/epilog code, so it is basically free.
112   bool isStaticAlloca() const;
113
114   // Methods for support type inquiry through isa, cast, and dyn_cast:
115   static inline bool classof(const AllocaInst *) { return true; }
116   static inline bool classof(const Instruction *I) {
117     return (I->getOpcode() == Instruction::Alloca);
118   }
119   static inline bool classof(const Value *V) {
120     return isa<Instruction>(V) && classof(cast<Instruction>(V));
121   }
122 private:
123   // Shadow Instruction::setInstructionSubclassData with a private forwarding
124   // method so that subclasses cannot accidentally use it.
125   void setInstructionSubclassData(unsigned short D) {
126     Instruction::setInstructionSubclassData(D);
127   }
128 };
129
130
131 //===----------------------------------------------------------------------===//
132 //                                LoadInst Class
133 //===----------------------------------------------------------------------===//
134
135 /// LoadInst - an instruction for reading from memory.  This uses the
136 /// SubclassData field in Value to store whether or not the load is volatile.
137 ///
138 class LoadInst : public UnaryInstruction {
139   void AssertOK();
140 protected:
141   virtual LoadInst *clone_impl() const;
142 public:
143   LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
144   LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
145   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
146            Instruction *InsertBefore = 0);
147   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
148            BasicBlock *InsertAtEnd);
149   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
150            unsigned Align, Instruction *InsertBefore = 0);
151   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
152            unsigned Align, BasicBlock *InsertAtEnd);
153   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
154            unsigned Align, AtomicOrdering Order,
155            SynchronizationScope SynchScope = CrossThread,
156            Instruction *InsertBefore = 0);
157   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
158            unsigned Align, AtomicOrdering Order,
159            SynchronizationScope SynchScope,
160            BasicBlock *InsertAtEnd);
161
162   LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
163   LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
164   explicit LoadInst(Value *Ptr, const char *NameStr = 0,
165                     bool isVolatile = false,  Instruction *InsertBefore = 0);
166   LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
167            BasicBlock *InsertAtEnd);
168
169   /// isVolatile - Return true if this is a load from a volatile memory
170   /// location.
171   ///
172   bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
173
174   /// setVolatile - Specify whether this is a volatile load or not.
175   ///
176   void setVolatile(bool V) {
177     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
178                                (V ? 1 : 0));
179   }
180
181   /// getAlignment - Return the alignment of the access that is being performed
182   ///
183   unsigned getAlignment() const {
184     return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
185   }
186
187   void setAlignment(unsigned Align);
188
189   /// Returns the ordering effect of this fence.
190   AtomicOrdering getOrdering() const {
191     return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
192   }
193
194   /// Set the ordering constraint on this load. May not be Release or
195   /// AcquireRelease.
196   void setOrdering(AtomicOrdering Ordering) {
197     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
198                                (Ordering << 7));
199   }
200
201   SynchronizationScope getSynchScope() const {
202     return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
203   }
204
205   /// Specify whether this load is ordered with respect to all
206   /// concurrently executing threads, or only with respect to signal handlers
207   /// executing in the same thread.
208   void setSynchScope(SynchronizationScope xthread) {
209     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
210                                (xthread << 6));
211   }
212
213   bool isAtomic() const { return getOrdering() != NotAtomic; }
214   void setAtomic(AtomicOrdering Ordering,
215                  SynchronizationScope SynchScope = CrossThread) {
216     setOrdering(Ordering);
217     setSynchScope(SynchScope);
218   }
219
220   bool isSimple() const { return !isAtomic() && !isVolatile(); }
221   bool isUnordered() const {
222     return getOrdering() <= Unordered && !isVolatile();
223   }
224
225   Value *getPointerOperand() { return getOperand(0); }
226   const Value *getPointerOperand() const { return getOperand(0); }
227   static unsigned getPointerOperandIndex() { return 0U; }
228
229   unsigned getPointerAddressSpace() const {
230     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
231   }
232
233
234   // Methods for support type inquiry through isa, cast, and dyn_cast:
235   static inline bool classof(const LoadInst *) { return true; }
236   static inline bool classof(const Instruction *I) {
237     return I->getOpcode() == Instruction::Load;
238   }
239   static inline bool classof(const Value *V) {
240     return isa<Instruction>(V) && classof(cast<Instruction>(V));
241   }
242 private:
243   // Shadow Instruction::setInstructionSubclassData with a private forwarding
244   // method so that subclasses cannot accidentally use it.
245   void setInstructionSubclassData(unsigned short D) {
246     Instruction::setInstructionSubclassData(D);
247   }
248 };
249
250
251 //===----------------------------------------------------------------------===//
252 //                                StoreInst Class
253 //===----------------------------------------------------------------------===//
254
255 /// StoreInst - an instruction for storing to memory
256 ///
257 class StoreInst : public Instruction {
258   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
259   void AssertOK();
260 protected:
261   virtual StoreInst *clone_impl() const;
262 public:
263   // allocate space for exactly two operands
264   void *operator new(size_t s) {
265     return User::operator new(s, 2);
266   }
267   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
268   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
269   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
270             Instruction *InsertBefore = 0);
271   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
272   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
273             unsigned Align, Instruction *InsertBefore = 0);
274   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
275             unsigned Align, BasicBlock *InsertAtEnd);
276   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
277             unsigned Align, AtomicOrdering Order,
278             SynchronizationScope SynchScope = CrossThread,
279             Instruction *InsertBefore = 0);
280   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
281             unsigned Align, AtomicOrdering Order,
282             SynchronizationScope SynchScope,
283             BasicBlock *InsertAtEnd);
284           
285
286   /// isVolatile - Return true if this is a store to a volatile memory
287   /// location.
288   ///
289   bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
290
291   /// setVolatile - Specify whether this is a volatile store or not.
292   ///
293   void setVolatile(bool V) {
294     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
295                                (V ? 1 : 0));
296   }
297
298   /// Transparently provide more efficient getOperand methods.
299   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
300
301   /// getAlignment - Return the alignment of the access that is being performed
302   ///
303   unsigned getAlignment() const {
304     return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
305   }
306
307   void setAlignment(unsigned Align);
308
309   /// Returns the ordering effect of this store.
310   AtomicOrdering getOrdering() const {
311     return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
312   }
313
314   /// Set the ordering constraint on this store.  May not be Acquire or
315   /// AcquireRelease.
316   void setOrdering(AtomicOrdering Ordering) {
317     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
318                                (Ordering << 7));
319   }
320
321   SynchronizationScope getSynchScope() const {
322     return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
323   }
324
325   /// Specify whether this store instruction is ordered with respect to all
326   /// concurrently executing threads, or only with respect to signal handlers
327   /// executing in the same thread.
328   void setSynchScope(SynchronizationScope xthread) {
329     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
330                                (xthread << 6));
331   }
332
333   bool isAtomic() const { return getOrdering() != NotAtomic; }
334   void setAtomic(AtomicOrdering Ordering,
335                  SynchronizationScope SynchScope = CrossThread) {
336     setOrdering(Ordering);
337     setSynchScope(SynchScope);
338   }
339
340   bool isSimple() const { return !isAtomic() && !isVolatile(); }
341   bool isUnordered() const {
342     return getOrdering() <= Unordered && !isVolatile();
343   }
344
345   Value *getValueOperand() { return getOperand(0); }
346   const Value *getValueOperand() const { return getOperand(0); }
347
348   Value *getPointerOperand() { return getOperand(1); }
349   const Value *getPointerOperand() const { return getOperand(1); }
350   static unsigned getPointerOperandIndex() { return 1U; }
351
352   unsigned getPointerAddressSpace() const {
353     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
354   }
355
356   // Methods for support type inquiry through isa, cast, and dyn_cast:
357   static inline bool classof(const StoreInst *) { return true; }
358   static inline bool classof(const Instruction *I) {
359     return I->getOpcode() == Instruction::Store;
360   }
361   static inline bool classof(const Value *V) {
362     return isa<Instruction>(V) && classof(cast<Instruction>(V));
363   }
364 private:
365   // Shadow Instruction::setInstructionSubclassData with a private forwarding
366   // method so that subclasses cannot accidentally use it.
367   void setInstructionSubclassData(unsigned short D) {
368     Instruction::setInstructionSubclassData(D);
369   }
370 };
371
372 template <>
373 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
374 };
375
376 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
377
378 //===----------------------------------------------------------------------===//
379 //                                FenceInst Class
380 //===----------------------------------------------------------------------===//
381
382 /// FenceInst - an instruction for ordering other memory operations
383 ///
384 class FenceInst : public Instruction {
385   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
386   void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
387 protected:
388   virtual FenceInst *clone_impl() const;
389 public:
390   // allocate space for exactly zero operands
391   void *operator new(size_t s) {
392     return User::operator new(s, 0);
393   }
394
395   // Ordering may only be Acquire, Release, AcquireRelease, or
396   // SequentiallyConsistent.
397   FenceInst(LLVMContext &C, AtomicOrdering Ordering,
398             SynchronizationScope SynchScope = CrossThread,
399             Instruction *InsertBefore = 0);
400   FenceInst(LLVMContext &C, AtomicOrdering Ordering,
401             SynchronizationScope SynchScope,
402             BasicBlock *InsertAtEnd);
403
404   /// Returns the ordering effect of this fence.
405   AtomicOrdering getOrdering() const {
406     return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
407   }
408
409   /// Set the ordering constraint on this fence.  May only be Acquire, Release,
410   /// AcquireRelease, or SequentiallyConsistent.
411   void setOrdering(AtomicOrdering Ordering) {
412     setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
413                                (Ordering << 1));
414   }
415
416   SynchronizationScope getSynchScope() const {
417     return SynchronizationScope(getSubclassDataFromInstruction() & 1);
418   }
419
420   /// Specify whether this fence orders other operations with respect to all
421   /// concurrently executing threads, or only with respect to signal handlers
422   /// executing in the same thread.
423   void setSynchScope(SynchronizationScope xthread) {
424     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
425                                xthread);
426   }
427
428   // Methods for support type inquiry through isa, cast, and dyn_cast:
429   static inline bool classof(const FenceInst *) { return true; }
430   static inline bool classof(const Instruction *I) {
431     return I->getOpcode() == Instruction::Fence;
432   }
433   static inline bool classof(const Value *V) {
434     return isa<Instruction>(V) && classof(cast<Instruction>(V));
435   }
436 private:
437   // Shadow Instruction::setInstructionSubclassData with a private forwarding
438   // method so that subclasses cannot accidentally use it.
439   void setInstructionSubclassData(unsigned short D) {
440     Instruction::setInstructionSubclassData(D);
441   }
442 };
443
444 //===----------------------------------------------------------------------===//
445 //                                AtomicCmpXchgInst Class
446 //===----------------------------------------------------------------------===//
447
448 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
449 /// specified value is in a memory location, and, if it is, stores a new value
450 /// there.  Returns the value that was loaded.
451 ///
452 class AtomicCmpXchgInst : public Instruction {
453   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
454   void Init(Value *Ptr, Value *Cmp, Value *NewVal,
455             AtomicOrdering Ordering, SynchronizationScope SynchScope);
456 protected:
457   virtual AtomicCmpXchgInst *clone_impl() const;
458 public:
459   // allocate space for exactly three operands
460   void *operator new(size_t s) {
461     return User::operator new(s, 3);
462   }
463   AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
464                     AtomicOrdering Ordering, SynchronizationScope SynchScope,
465                     Instruction *InsertBefore = 0);
466   AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
467                     AtomicOrdering Ordering, SynchronizationScope SynchScope,
468                     BasicBlock *InsertAtEnd);
469
470   /// isVolatile - Return true if this is a cmpxchg from a volatile memory
471   /// location.
472   ///
473   bool isVolatile() const {
474     return getSubclassDataFromInstruction() & 1;
475   }
476
477   /// setVolatile - Specify whether this is a volatile cmpxchg.
478   ///
479   void setVolatile(bool V) {
480      setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
481                                 (unsigned)V);
482   }
483
484   /// Transparently provide more efficient getOperand methods.
485   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
486
487   /// Set the ordering constraint on this cmpxchg.
488   void setOrdering(AtomicOrdering Ordering) {
489     assert(Ordering != NotAtomic &&
490            "CmpXchg instructions can only be atomic.");
491     setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
492                                (Ordering << 2));
493   }
494
495   /// Specify whether this cmpxchg is atomic and orders other operations with
496   /// respect to all concurrently executing threads, or only with respect to
497   /// signal handlers executing in the same thread.
498   void setSynchScope(SynchronizationScope SynchScope) {
499     setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
500                                (SynchScope << 1));
501   }
502
503   /// Returns the ordering constraint on this cmpxchg.
504   AtomicOrdering getOrdering() const {
505     return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
506   }
507
508   /// Returns whether this cmpxchg is atomic between threads or only within a
509   /// single thread.
510   SynchronizationScope getSynchScope() const {
511     return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
512   }
513
514   Value *getPointerOperand() { return getOperand(0); }
515   const Value *getPointerOperand() const { return getOperand(0); }
516   static unsigned getPointerOperandIndex() { return 0U; }
517
518   Value *getCompareOperand() { return getOperand(1); }
519   const Value *getCompareOperand() const { return getOperand(1); }
520   
521   Value *getNewValOperand() { return getOperand(2); }
522   const Value *getNewValOperand() const { return getOperand(2); }
523   
524   unsigned getPointerAddressSpace() const {
525     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
526   }
527   
528   // Methods for support type inquiry through isa, cast, and dyn_cast:
529   static inline bool classof(const AtomicCmpXchgInst *) { return true; }
530   static inline bool classof(const Instruction *I) {
531     return I->getOpcode() == Instruction::AtomicCmpXchg;
532   }
533   static inline bool classof(const Value *V) {
534     return isa<Instruction>(V) && classof(cast<Instruction>(V));
535   }
536 private:
537   // Shadow Instruction::setInstructionSubclassData with a private forwarding
538   // method so that subclasses cannot accidentally use it.
539   void setInstructionSubclassData(unsigned short D) {
540     Instruction::setInstructionSubclassData(D);
541   }
542 };
543
544 template <>
545 struct OperandTraits<AtomicCmpXchgInst> :
546     public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
547 };
548
549 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
550
551 //===----------------------------------------------------------------------===//
552 //                                AtomicRMWInst Class
553 //===----------------------------------------------------------------------===//
554
555 /// AtomicRMWInst - an instruction that atomically reads a memory location,
556 /// combines it with another value, and then stores the result back.  Returns
557 /// the old value.
558 ///
559 class AtomicRMWInst : public Instruction {
560   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
561 protected:
562   virtual AtomicRMWInst *clone_impl() const;
563 public:
564   /// This enumeration lists the possible modifications atomicrmw can make.  In
565   /// the descriptions, 'p' is the pointer to the instruction's memory location,
566   /// 'old' is the initial value of *p, and 'v' is the other value passed to the
567   /// instruction.  These instructions always return 'old'.
568   enum BinOp {
569     /// *p = v
570     Xchg,
571     /// *p = old + v
572     Add,
573     /// *p = old - v
574     Sub,
575     /// *p = old & v
576     And,
577     /// *p = ~old & v
578     Nand,
579     /// *p = old | v
580     Or,
581     /// *p = old ^ v
582     Xor,
583     /// *p = old >signed v ? old : v
584     Max,
585     /// *p = old <signed v ? old : v
586     Min,
587     /// *p = old >unsigned v ? old : v
588     UMax,
589     /// *p = old <unsigned v ? old : v
590     UMin,
591
592     FIRST_BINOP = Xchg,
593     LAST_BINOP = UMin,
594     BAD_BINOP
595   };
596
597   // allocate space for exactly two operands
598   void *operator new(size_t s) {
599     return User::operator new(s, 2);
600   }
601   AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
602                 AtomicOrdering Ordering, SynchronizationScope SynchScope,
603                 Instruction *InsertBefore = 0);
604   AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
605                 AtomicOrdering Ordering, SynchronizationScope SynchScope,
606                 BasicBlock *InsertAtEnd);
607
608   BinOp getOperation() const {
609     return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
610   }
611
612   void setOperation(BinOp Operation) {
613     unsigned short SubclassData = getSubclassDataFromInstruction();
614     setInstructionSubclassData((SubclassData & 31) |
615                                (Operation << 5));
616   }
617
618   /// isVolatile - Return true if this is a RMW on a volatile memory location.
619   ///
620   bool isVolatile() const {
621     return getSubclassDataFromInstruction() & 1;
622   }
623
624   /// setVolatile - Specify whether this is a volatile RMW or not.
625   ///
626   void setVolatile(bool V) {
627      setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
628                                 (unsigned)V);
629   }
630
631   /// Transparently provide more efficient getOperand methods.
632   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
633
634   /// Set the ordering constraint on this RMW.
635   void setOrdering(AtomicOrdering Ordering) {
636     assert(Ordering != NotAtomic &&
637            "atomicrmw instructions can only be atomic.");
638     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
639                                (Ordering << 2));
640   }
641
642   /// Specify whether this RMW orders other operations with respect to all
643   /// concurrently executing threads, or only with respect to signal handlers
644   /// executing in the same thread.
645   void setSynchScope(SynchronizationScope SynchScope) {
646     setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
647                                (SynchScope << 1));
648   }
649
650   /// Returns the ordering constraint on this RMW.
651   AtomicOrdering getOrdering() const {
652     return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
653   }
654
655   /// Returns whether this RMW is atomic between threads or only within a
656   /// single thread.
657   SynchronizationScope getSynchScope() const {
658     return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
659   }
660
661   Value *getPointerOperand() { return getOperand(0); }
662   const Value *getPointerOperand() const { return getOperand(0); }
663   static unsigned getPointerOperandIndex() { return 0U; }
664
665   Value *getValOperand() { return getOperand(1); }
666   const Value *getValOperand() const { return getOperand(1); }
667
668   unsigned getPointerAddressSpace() const {
669     return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
670   }
671
672   // Methods for support type inquiry through isa, cast, and dyn_cast:
673   static inline bool classof(const AtomicRMWInst *) { return true; }
674   static inline bool classof(const Instruction *I) {
675     return I->getOpcode() == Instruction::AtomicRMW;
676   }
677   static inline bool classof(const Value *V) {
678     return isa<Instruction>(V) && classof(cast<Instruction>(V));
679   }
680 private:
681   void Init(BinOp Operation, Value *Ptr, Value *Val,
682             AtomicOrdering Ordering, SynchronizationScope SynchScope);
683   // Shadow Instruction::setInstructionSubclassData with a private forwarding
684   // method so that subclasses cannot accidentally use it.
685   void setInstructionSubclassData(unsigned short D) {
686     Instruction::setInstructionSubclassData(D);
687   }
688 };
689
690 template <>
691 struct OperandTraits<AtomicRMWInst>
692     : public FixedNumOperandTraits<AtomicRMWInst,2> {
693 };
694
695 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
696
697 //===----------------------------------------------------------------------===//
698 //                             GetElementPtrInst Class
699 //===----------------------------------------------------------------------===//
700
701 // checkGEPType - Simple wrapper function to give a better assertion failure
702 // message on bad indexes for a gep instruction.
703 //
704 inline Type *checkGEPType(Type *Ty) {
705   assert(Ty && "Invalid GetElementPtrInst indices for type!");
706   return Ty;
707 }
708
709 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
710 /// access elements of arrays and structs
711 ///
712 class GetElementPtrInst : public Instruction {
713   GetElementPtrInst(const GetElementPtrInst &GEPI);
714   void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
715
716   /// Constructors - Create a getelementptr instruction with a base pointer an
717   /// list of indices. The first ctor can optionally insert before an existing
718   /// instruction, the second appends the new instruction to the specified
719   /// BasicBlock.
720   inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
721                            unsigned Values, const Twine &NameStr,
722                            Instruction *InsertBefore);
723   inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
724                            unsigned Values, const Twine &NameStr,
725                            BasicBlock *InsertAtEnd);
726 protected:
727   virtual GetElementPtrInst *clone_impl() const;
728 public:
729   static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
730                                    const Twine &NameStr = "",
731                                    Instruction *InsertBefore = 0) {
732     unsigned Values = 1 + unsigned(IdxList.size());
733     return new(Values)
734       GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
735   }
736   static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
737                                    const Twine &NameStr,
738                                    BasicBlock *InsertAtEnd) {
739     unsigned Values = 1 + unsigned(IdxList.size());
740     return new(Values)
741       GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
742   }
743
744   /// Create an "inbounds" getelementptr. See the documentation for the
745   /// "inbounds" flag in LangRef.html for details.
746   static GetElementPtrInst *CreateInBounds(Value *Ptr,
747                                            ArrayRef<Value *> IdxList,
748                                            const Twine &NameStr = "",
749                                            Instruction *InsertBefore = 0) {
750     GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
751     GEP->setIsInBounds(true);
752     return GEP;
753   }
754   static GetElementPtrInst *CreateInBounds(Value *Ptr,
755                                            ArrayRef<Value *> IdxList,
756                                            const Twine &NameStr,
757                                            BasicBlock *InsertAtEnd) {
758     GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
759     GEP->setIsInBounds(true);
760     return GEP;
761   }
762
763   /// Transparently provide more efficient getOperand methods.
764   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
765
766   // getType - Overload to return most specific pointer type...
767   PointerType *getType() const {
768     return reinterpret_cast<PointerType*>(Instruction::getType());
769   }
770
771   /// getIndexedType - Returns the type of the element that would be loaded with
772   /// a load instruction with the specified parameters.
773   ///
774   /// Null is returned if the indices are invalid for the specified
775   /// pointer type.
776   ///
777   static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
778   static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
779   static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
780
781   /// getAddressSpace - Returns the address space used by the GEP pointer.
782   ///
783   static unsigned getAddressSpace(Value *Ptr);
784
785   inline op_iterator       idx_begin()       { return op_begin()+1; }
786   inline const_op_iterator idx_begin() const { return op_begin()+1; }
787   inline op_iterator       idx_end()         { return op_end(); }
788   inline const_op_iterator idx_end()   const { return op_end(); }
789
790   Value *getPointerOperand() {
791     return getOperand(0);
792   }
793   const Value *getPointerOperand() const {
794     return getOperand(0);
795   }
796   static unsigned getPointerOperandIndex() {
797     return 0U;    // get index for modifying correct operand.
798   }
799
800   unsigned getPointerAddressSpace() const {
801     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
802   }
803
804   /// getPointerOperandType - Method to return the pointer operand as a
805   /// PointerType.
806   Type *getPointerOperandType() const {
807     return getPointerOperand()->getType();
808   }
809
810   /// GetGEPReturnType - Returns the pointer type returned by the GEP
811   /// instruction, which may be a vector of pointers.
812   static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
813     Type *PtrTy = PointerType::get(checkGEPType(
814                                    getIndexedType(Ptr->getType(), IdxList)),
815                                    getAddressSpace(Ptr));
816     // Vector GEP
817     if (Ptr->getType()->isVectorTy()) {
818       unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
819       return VectorType::get(PtrTy, NumElem);
820     }
821
822     // Scalar GEP
823     return PtrTy;
824   }
825
826   unsigned getNumIndices() const {  // Note: always non-negative
827     return getNumOperands() - 1;
828   }
829
830   bool hasIndices() const {
831     return getNumOperands() > 1;
832   }
833
834   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
835   /// zeros.  If so, the result pointer and the first operand have the same
836   /// value, just potentially different types.
837   bool hasAllZeroIndices() const;
838
839   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
840   /// constant integers.  If so, the result pointer and the first operand have
841   /// a constant offset between them.
842   bool hasAllConstantIndices() const;
843
844   /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
845   /// See LangRef.html for the meaning of inbounds on a getelementptr.
846   void setIsInBounds(bool b = true);
847
848   /// isInBounds - Determine whether the GEP has the inbounds flag.
849   bool isInBounds() const;
850
851   // Methods for support type inquiry through isa, cast, and dyn_cast:
852   static inline bool classof(const GetElementPtrInst *) { return true; }
853   static inline bool classof(const Instruction *I) {
854     return (I->getOpcode() == Instruction::GetElementPtr);
855   }
856   static inline bool classof(const Value *V) {
857     return isa<Instruction>(V) && classof(cast<Instruction>(V));
858   }
859 };
860
861 template <>
862 struct OperandTraits<GetElementPtrInst> :
863   public VariadicOperandTraits<GetElementPtrInst, 1> {
864 };
865
866 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
867                                      ArrayRef<Value *> IdxList,
868                                      unsigned Values,
869                                      const Twine &NameStr,
870                                      Instruction *InsertBefore)
871   : Instruction(getGEPReturnType(Ptr, IdxList),
872                 GetElementPtr,
873                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
874                 Values, InsertBefore) {
875   init(Ptr, IdxList, NameStr);
876 }
877 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
878                                      ArrayRef<Value *> IdxList,
879                                      unsigned Values,
880                                      const Twine &NameStr,
881                                      BasicBlock *InsertAtEnd)
882   : Instruction(getGEPReturnType(Ptr, IdxList),
883                 GetElementPtr,
884                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
885                 Values, InsertAtEnd) {
886   init(Ptr, IdxList, NameStr);
887 }
888
889
890 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
891
892
893 //===----------------------------------------------------------------------===//
894 //                               ICmpInst Class
895 //===----------------------------------------------------------------------===//
896
897 /// This instruction compares its operands according to the predicate given
898 /// to the constructor. It only operates on integers or pointers. The operands
899 /// must be identical types.
900 /// @brief Represent an integer comparison operator.
901 class ICmpInst: public CmpInst {
902 protected:
903   /// @brief Clone an identical ICmpInst
904   virtual ICmpInst *clone_impl() const;
905 public:
906   /// @brief Constructor with insert-before-instruction semantics.
907   ICmpInst(
908     Instruction *InsertBefore,  ///< Where to insert
909     Predicate pred,  ///< The predicate to use for the comparison
910     Value *LHS,      ///< The left-hand-side of the expression
911     Value *RHS,      ///< The right-hand-side of the expression
912     const Twine &NameStr = ""  ///< Name of the instruction
913   ) : CmpInst(makeCmpResultType(LHS->getType()),
914               Instruction::ICmp, pred, LHS, RHS, NameStr,
915               InsertBefore) {
916     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
917            pred <= CmpInst::LAST_ICMP_PREDICATE &&
918            "Invalid ICmp predicate value");
919     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
920           "Both operands to ICmp instruction are not of the same type!");
921     // Check that the operands are the right type
922     assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
923             getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
924            "Invalid operand types for ICmp instruction");
925   }
926
927   /// @brief Constructor with insert-at-end semantics.
928   ICmpInst(
929     BasicBlock &InsertAtEnd, ///< Block to insert into.
930     Predicate pred,  ///< The predicate to use for the comparison
931     Value *LHS,      ///< The left-hand-side of the expression
932     Value *RHS,      ///< The right-hand-side of the expression
933     const Twine &NameStr = ""  ///< Name of the instruction
934   ) : CmpInst(makeCmpResultType(LHS->getType()),
935               Instruction::ICmp, pred, LHS, RHS, NameStr,
936               &InsertAtEnd) {
937     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
938           pred <= CmpInst::LAST_ICMP_PREDICATE &&
939           "Invalid ICmp predicate value");
940     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
941           "Both operands to ICmp instruction are not of the same type!");
942     // Check that the operands are the right type
943     assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
944             getOperand(0)->getType()->isPointerTy()) &&
945            "Invalid operand types for ICmp instruction");
946   }
947
948   /// @brief Constructor with no-insertion semantics
949   ICmpInst(
950     Predicate pred, ///< The predicate to use for the comparison
951     Value *LHS,     ///< The left-hand-side of the expression
952     Value *RHS,     ///< The right-hand-side of the expression
953     const Twine &NameStr = "" ///< Name of the instruction
954   ) : CmpInst(makeCmpResultType(LHS->getType()),
955               Instruction::ICmp, pred, LHS, RHS, NameStr) {
956     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
957            pred <= CmpInst::LAST_ICMP_PREDICATE &&
958            "Invalid ICmp predicate value");
959     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
960           "Both operands to ICmp instruction are not of the same type!");
961     // Check that the operands are the right type
962     assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
963             getOperand(0)->getType()->getScalarType()->isPointerTy()) &&
964            "Invalid operand types for ICmp instruction");
965   }
966
967   /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
968   /// @returns the predicate that would be the result if the operand were
969   /// regarded as signed.
970   /// @brief Return the signed version of the predicate
971   Predicate getSignedPredicate() const {
972     return getSignedPredicate(getPredicate());
973   }
974
975   /// This is a static version that you can use without an instruction.
976   /// @brief Return the signed version of the predicate.
977   static Predicate getSignedPredicate(Predicate pred);
978
979   /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
980   /// @returns the predicate that would be the result if the operand were
981   /// regarded as unsigned.
982   /// @brief Return the unsigned version of the predicate
983   Predicate getUnsignedPredicate() const {
984     return getUnsignedPredicate(getPredicate());
985   }
986
987   /// This is a static version that you can use without an instruction.
988   /// @brief Return the unsigned version of the predicate.
989   static Predicate getUnsignedPredicate(Predicate pred);
990
991   /// isEquality - Return true if this predicate is either EQ or NE.  This also
992   /// tests for commutativity.
993   static bool isEquality(Predicate P) {
994     return P == ICMP_EQ || P == ICMP_NE;
995   }
996
997   /// isEquality - Return true if this predicate is either EQ or NE.  This also
998   /// tests for commutativity.
999   bool isEquality() const {
1000     return isEquality(getPredicate());
1001   }
1002
1003   /// @returns true if the predicate of this ICmpInst is commutative
1004   /// @brief Determine if this relation is commutative.
1005   bool isCommutative() const { return isEquality(); }
1006
1007   /// isRelational - Return true if the predicate is relational (not EQ or NE).
1008   ///
1009   bool isRelational() const {
1010     return !isEquality();
1011   }
1012
1013   /// isRelational - Return true if the predicate is relational (not EQ or NE).
1014   ///
1015   static bool isRelational(Predicate P) {
1016     return !isEquality(P);
1017   }
1018
1019   /// Initialize a set of values that all satisfy the predicate with C.
1020   /// @brief Make a ConstantRange for a relation with a constant value.
1021   static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1022
1023   /// Exchange the two operands to this instruction in such a way that it does
1024   /// not modify the semantics of the instruction. The predicate value may be
1025   /// changed to retain the same result if the predicate is order dependent
1026   /// (e.g. ult).
1027   /// @brief Swap operands and adjust predicate.
1028   void swapOperands() {
1029     setPredicate(getSwappedPredicate());
1030     Op<0>().swap(Op<1>());
1031   }
1032
1033   // Methods for support type inquiry through isa, cast, and dyn_cast:
1034   static inline bool classof(const ICmpInst *) { return true; }
1035   static inline bool classof(const Instruction *I) {
1036     return I->getOpcode() == Instruction::ICmp;
1037   }
1038   static inline bool classof(const Value *V) {
1039     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1040   }
1041
1042 };
1043
1044 //===----------------------------------------------------------------------===//
1045 //                               FCmpInst Class
1046 //===----------------------------------------------------------------------===//
1047
1048 /// This instruction compares its operands according to the predicate given
1049 /// to the constructor. It only operates on floating point values or packed
1050 /// vectors of floating point values. The operands must be identical types.
1051 /// @brief Represents a floating point comparison operator.
1052 class FCmpInst: public CmpInst {
1053 protected:
1054   /// @brief Clone an identical FCmpInst
1055   virtual FCmpInst *clone_impl() const;
1056 public:
1057   /// @brief Constructor with insert-before-instruction semantics.
1058   FCmpInst(
1059     Instruction *InsertBefore, ///< Where to insert
1060     Predicate pred,  ///< The predicate to use for the comparison
1061     Value *LHS,      ///< The left-hand-side of the expression
1062     Value *RHS,      ///< The right-hand-side of the expression
1063     const Twine &NameStr = ""  ///< Name of the instruction
1064   ) : CmpInst(makeCmpResultType(LHS->getType()),
1065               Instruction::FCmp, pred, LHS, RHS, NameStr,
1066               InsertBefore) {
1067     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1068            "Invalid FCmp predicate value");
1069     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1070            "Both operands to FCmp instruction are not of the same type!");
1071     // Check that the operands are the right type
1072     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1073            "Invalid operand types for FCmp instruction");
1074   }
1075
1076   /// @brief Constructor with insert-at-end semantics.
1077   FCmpInst(
1078     BasicBlock &InsertAtEnd, ///< Block to insert into.
1079     Predicate pred,  ///< The predicate to use for the comparison
1080     Value *LHS,      ///< The left-hand-side of the expression
1081     Value *RHS,      ///< The right-hand-side of the expression
1082     const Twine &NameStr = ""  ///< Name of the instruction
1083   ) : CmpInst(makeCmpResultType(LHS->getType()),
1084               Instruction::FCmp, pred, LHS, RHS, NameStr,
1085               &InsertAtEnd) {
1086     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1087            "Invalid FCmp predicate value");
1088     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1089            "Both operands to FCmp instruction are not of the same type!");
1090     // Check that the operands are the right type
1091     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1092            "Invalid operand types for FCmp instruction");
1093   }
1094
1095   /// @brief Constructor with no-insertion semantics
1096   FCmpInst(
1097     Predicate pred, ///< The predicate to use for the comparison
1098     Value *LHS,     ///< The left-hand-side of the expression
1099     Value *RHS,     ///< The right-hand-side of the expression
1100     const Twine &NameStr = "" ///< Name of the instruction
1101   ) : CmpInst(makeCmpResultType(LHS->getType()),
1102               Instruction::FCmp, pred, LHS, RHS, NameStr) {
1103     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1104            "Invalid FCmp predicate value");
1105     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1106            "Both operands to FCmp instruction are not of the same type!");
1107     // Check that the operands are the right type
1108     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1109            "Invalid operand types for FCmp instruction");
1110   }
1111
1112   /// @returns true if the predicate of this instruction is EQ or NE.
1113   /// @brief Determine if this is an equality predicate.
1114   bool isEquality() const {
1115     return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1116            getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1117   }
1118
1119   /// @returns true if the predicate of this instruction is commutative.
1120   /// @brief Determine if this is a commutative predicate.
1121   bool isCommutative() const {
1122     return isEquality() ||
1123            getPredicate() == FCMP_FALSE ||
1124            getPredicate() == FCMP_TRUE ||
1125            getPredicate() == FCMP_ORD ||
1126            getPredicate() == FCMP_UNO;
1127   }
1128
1129   /// @returns true if the predicate is relational (not EQ or NE).
1130   /// @brief Determine if this a relational predicate.
1131   bool isRelational() const { return !isEquality(); }
1132
1133   /// Exchange the two operands to this instruction in such a way that it does
1134   /// not modify the semantics of the instruction. The predicate value may be
1135   /// changed to retain the same result if the predicate is order dependent
1136   /// (e.g. ult).
1137   /// @brief Swap operands and adjust predicate.
1138   void swapOperands() {
1139     setPredicate(getSwappedPredicate());
1140     Op<0>().swap(Op<1>());
1141   }
1142
1143   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1144   static inline bool classof(const FCmpInst *) { return true; }
1145   static inline bool classof(const Instruction *I) {
1146     return I->getOpcode() == Instruction::FCmp;
1147   }
1148   static inline bool classof(const Value *V) {
1149     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1150   }
1151 };
1152
1153 //===----------------------------------------------------------------------===//
1154 /// CallInst - This class represents a function call, abstracting a target
1155 /// machine's calling convention.  This class uses low bit of the SubClassData
1156 /// field to indicate whether or not this is a tail call.  The rest of the bits
1157 /// hold the calling convention of the call.
1158 ///
1159 class CallInst : public Instruction {
1160   AttrListPtr AttributeList; ///< parameter attributes for call
1161   CallInst(const CallInst &CI);
1162   void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1163   void init(Value *Func, const Twine &NameStr);
1164
1165   /// Construct a CallInst given a range of arguments.
1166   /// @brief Construct a CallInst from a range of arguments
1167   inline CallInst(Value *Func, ArrayRef<Value *> Args,
1168                   const Twine &NameStr, Instruction *InsertBefore);
1169
1170   /// Construct a CallInst given a range of arguments.
1171   /// @brief Construct a CallInst from a range of arguments
1172   inline CallInst(Value *Func, ArrayRef<Value *> Args,
1173                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1174
1175   CallInst(Value *F, Value *Actual, const Twine &NameStr,
1176            Instruction *InsertBefore);
1177   CallInst(Value *F, Value *Actual, const Twine &NameStr,
1178            BasicBlock *InsertAtEnd);
1179   explicit CallInst(Value *F, const Twine &NameStr,
1180                     Instruction *InsertBefore);
1181   CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1182 protected:
1183   virtual CallInst *clone_impl() const;
1184 public:
1185   static CallInst *Create(Value *Func,
1186                           ArrayRef<Value *> Args,
1187                           const Twine &NameStr = "",
1188                           Instruction *InsertBefore = 0) {
1189     return new(unsigned(Args.size() + 1))
1190       CallInst(Func, Args, NameStr, InsertBefore);
1191   }
1192   static CallInst *Create(Value *Func,
1193                           ArrayRef<Value *> Args,
1194                           const Twine &NameStr, BasicBlock *InsertAtEnd) {
1195     return new(unsigned(Args.size() + 1))
1196       CallInst(Func, Args, NameStr, InsertAtEnd);
1197   }
1198   static CallInst *Create(Value *F, const Twine &NameStr = "",
1199                           Instruction *InsertBefore = 0) {
1200     return new(1) CallInst(F, NameStr, InsertBefore);
1201   }
1202   static CallInst *Create(Value *F, const Twine &NameStr,
1203                           BasicBlock *InsertAtEnd) {
1204     return new(1) CallInst(F, NameStr, InsertAtEnd);
1205   }
1206   /// CreateMalloc - Generate the IR for a call to malloc:
1207   /// 1. Compute the malloc call's argument as the specified type's size,
1208   ///    possibly multiplied by the array size if the array size is not
1209   ///    constant 1.
1210   /// 2. Call malloc with that argument.
1211   /// 3. Bitcast the result of the malloc call to the specified type.
1212   static Instruction *CreateMalloc(Instruction *InsertBefore,
1213                                    Type *IntPtrTy, Type *AllocTy,
1214                                    Value *AllocSize, Value *ArraySize = 0,
1215                                    Function* MallocF = 0,
1216                                    const Twine &Name = "");
1217   static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1218                                    Type *IntPtrTy, Type *AllocTy,
1219                                    Value *AllocSize, Value *ArraySize = 0,
1220                                    Function* MallocF = 0,
1221                                    const Twine &Name = "");
1222   /// CreateFree - Generate the IR for a call to the builtin free function.
1223   static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1224   static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1225
1226   ~CallInst();
1227
1228   bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1229   void setTailCall(bool isTC = true) {
1230     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1231                                unsigned(isTC));
1232   }
1233
1234   /// Provide fast operand accessors
1235   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1236
1237   /// getNumArgOperands - Return the number of call arguments.
1238   ///
1239   unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1240
1241   /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1242   ///
1243   Value *getArgOperand(unsigned i) const { return getOperand(i); }
1244   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1245
1246   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1247   /// function call.
1248   CallingConv::ID getCallingConv() const {
1249     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1250   }
1251   void setCallingConv(CallingConv::ID CC) {
1252     setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1253                                (static_cast<unsigned>(CC) << 1));
1254   }
1255
1256   /// getAttributes - Return the parameter attributes for this call.
1257   ///
1258   const AttrListPtr &getAttributes() const { return AttributeList; }
1259
1260   /// setAttributes - Set the parameter attributes for this call.
1261   ///
1262   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1263
1264   /// addAttribute - adds the attribute to the list of attributes.
1265   void addAttribute(unsigned i, Attributes attr);
1266
1267   /// removeAttribute - removes the attribute from the list of attributes.
1268   void removeAttribute(unsigned i, Attributes attr);
1269
1270   /// @brief Determine whether this call has the given attribute.
1271   bool hasFnAttr(Attributes::AttrVal A) const;
1272
1273   /// @brief Determine whether the call or the callee has the given attributes.
1274   bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
1275
1276   /// @brief Extract the alignment for a call or parameter (0=unknown).
1277   unsigned getParamAlignment(unsigned i) const {
1278     return AttributeList.getParamAlignment(i);
1279   }
1280
1281   /// @brief Return true if the call should not be inlined.
1282   bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
1283   void setIsNoInline(bool Value = true) {
1284     if (Value) addAttribute(~0, Attribute::NoInline);
1285     else removeAttribute(~0, Attribute::NoInline);
1286   }
1287
1288   /// @brief Return true if the call can return twice
1289   bool canReturnTwice() const {
1290     return hasFnAttr(Attributes::ReturnsTwice);
1291   }
1292   void setCanReturnTwice(bool Value = true) {
1293     if (Value) addAttribute(~0, Attribute::ReturnsTwice);
1294     else removeAttribute(~0, Attribute::ReturnsTwice);
1295   }
1296
1297   /// @brief Determine if the call does not access memory.
1298   bool doesNotAccessMemory() const {
1299     return hasFnAttr(Attributes::ReadNone);
1300   }
1301   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1302     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1303     else removeAttribute(~0, Attribute::ReadNone);
1304   }
1305
1306   /// @brief Determine if the call does not access or only reads memory.
1307   bool onlyReadsMemory() const {
1308     return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
1309   }
1310   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1311     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1312     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1313   }
1314
1315   /// @brief Determine if the call cannot return.
1316   bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
1317   void setDoesNotReturn(bool DoesNotReturn = true) {
1318     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1319     else removeAttribute(~0, Attribute::NoReturn);
1320   }
1321
1322   /// @brief Determine if the call cannot unwind.
1323   bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
1324   void setDoesNotThrow(bool DoesNotThrow = true) {
1325     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1326     else removeAttribute(~0, Attribute::NoUnwind);
1327   }
1328
1329   /// @brief Determine if the call returns a structure through first
1330   /// pointer argument.
1331   bool hasStructRetAttr() const {
1332     // Be friendly and also check the callee.
1333     return paramHasAttr(1, Attributes::StructRet);
1334   }
1335
1336   /// @brief Determine if any call argument is an aggregate passed by value.
1337   bool hasByValArgument() const {
1338     for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
1339       if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
1340         return true;
1341     return false;
1342   }
1343
1344   /// getCalledFunction - Return the function called, or null if this is an
1345   /// indirect function invocation.
1346   ///
1347   Function *getCalledFunction() const {
1348     return dyn_cast<Function>(Op<-1>());
1349   }
1350
1351   /// getCalledValue - Get a pointer to the function that is invoked by this
1352   /// instruction.
1353   const Value *getCalledValue() const { return Op<-1>(); }
1354         Value *getCalledValue()       { return Op<-1>(); }
1355
1356   /// setCalledFunction - Set the function called.
1357   void setCalledFunction(Value* Fn) {
1358     Op<-1>() = Fn;
1359   }
1360
1361   /// isInlineAsm - Check if this call is an inline asm statement.
1362   bool isInlineAsm() const {
1363     return isa<InlineAsm>(Op<-1>());
1364   }
1365
1366   // Methods for support type inquiry through isa, cast, and dyn_cast:
1367   static inline bool classof(const CallInst *) { return true; }
1368   static inline bool classof(const Instruction *I) {
1369     return I->getOpcode() == Instruction::Call;
1370   }
1371   static inline bool classof(const Value *V) {
1372     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1373   }
1374 private:
1375   // Shadow Instruction::setInstructionSubclassData with a private forwarding
1376   // method so that subclasses cannot accidentally use it.
1377   void setInstructionSubclassData(unsigned short D) {
1378     Instruction::setInstructionSubclassData(D);
1379   }
1380 };
1381
1382 template <>
1383 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1384 };
1385
1386 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1387                    const Twine &NameStr, BasicBlock *InsertAtEnd)
1388   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1389                                    ->getElementType())->getReturnType(),
1390                 Instruction::Call,
1391                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1392                 unsigned(Args.size() + 1), InsertAtEnd) {
1393   init(Func, Args, NameStr);
1394 }
1395
1396 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1397                    const Twine &NameStr, Instruction *InsertBefore)
1398   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1399                                    ->getElementType())->getReturnType(),
1400                 Instruction::Call,
1401                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1402                 unsigned(Args.size() + 1), InsertBefore) {
1403   init(Func, Args, NameStr);
1404 }
1405
1406
1407 // Note: if you get compile errors about private methods then
1408 //       please update your code to use the high-level operand
1409 //       interfaces. See line 943 above.
1410 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1411
1412 //===----------------------------------------------------------------------===//
1413 //                               SelectInst Class
1414 //===----------------------------------------------------------------------===//
1415
1416 /// SelectInst - This class represents the LLVM 'select' instruction.
1417 ///
1418 class SelectInst : public Instruction {
1419   void init(Value *C, Value *S1, Value *S2) {
1420     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1421     Op<0>() = C;
1422     Op<1>() = S1;
1423     Op<2>() = S2;
1424   }
1425
1426   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1427              Instruction *InsertBefore)
1428     : Instruction(S1->getType(), Instruction::Select,
1429                   &Op<0>(), 3, InsertBefore) {
1430     init(C, S1, S2);
1431     setName(NameStr);
1432   }
1433   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1434              BasicBlock *InsertAtEnd)
1435     : Instruction(S1->getType(), Instruction::Select,
1436                   &Op<0>(), 3, InsertAtEnd) {
1437     init(C, S1, S2);
1438     setName(NameStr);
1439   }
1440 protected:
1441   virtual SelectInst *clone_impl() const;
1442 public:
1443   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1444                             const Twine &NameStr = "",
1445                             Instruction *InsertBefore = 0) {
1446     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1447   }
1448   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1449                             const Twine &NameStr,
1450                             BasicBlock *InsertAtEnd) {
1451     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1452   }
1453
1454   const Value *getCondition() const { return Op<0>(); }
1455   const Value *getTrueValue() const { return Op<1>(); }
1456   const Value *getFalseValue() const { return Op<2>(); }
1457   Value *getCondition() { return Op<0>(); }
1458   Value *getTrueValue() { return Op<1>(); }
1459   Value *getFalseValue() { return Op<2>(); }
1460
1461   /// areInvalidOperands - Return a string if the specified operands are invalid
1462   /// for a select operation, otherwise return null.
1463   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1464
1465   /// Transparently provide more efficient getOperand methods.
1466   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1467
1468   OtherOps getOpcode() const {
1469     return static_cast<OtherOps>(Instruction::getOpcode());
1470   }
1471
1472   // Methods for support type inquiry through isa, cast, and dyn_cast:
1473   static inline bool classof(const SelectInst *) { return true; }
1474   static inline bool classof(const Instruction *I) {
1475     return I->getOpcode() == Instruction::Select;
1476   }
1477   static inline bool classof(const Value *V) {
1478     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1479   }
1480 };
1481
1482 template <>
1483 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1484 };
1485
1486 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1487
1488 //===----------------------------------------------------------------------===//
1489 //                                VAArgInst Class
1490 //===----------------------------------------------------------------------===//
1491
1492 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1493 /// an argument of the specified type given a va_list and increments that list
1494 ///
1495 class VAArgInst : public UnaryInstruction {
1496 protected:
1497   virtual VAArgInst *clone_impl() const;
1498
1499 public:
1500   VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1501              Instruction *InsertBefore = 0)
1502     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1503     setName(NameStr);
1504   }
1505   VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1506             BasicBlock *InsertAtEnd)
1507     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1508     setName(NameStr);
1509   }
1510
1511   Value *getPointerOperand() { return getOperand(0); }
1512   const Value *getPointerOperand() const { return getOperand(0); }
1513   static unsigned getPointerOperandIndex() { return 0U; }
1514
1515   // Methods for support type inquiry through isa, cast, and dyn_cast:
1516   static inline bool classof(const VAArgInst *) { return true; }
1517   static inline bool classof(const Instruction *I) {
1518     return I->getOpcode() == VAArg;
1519   }
1520   static inline bool classof(const Value *V) {
1521     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1522   }
1523 };
1524
1525 //===----------------------------------------------------------------------===//
1526 //                                ExtractElementInst Class
1527 //===----------------------------------------------------------------------===//
1528
1529 /// ExtractElementInst - This instruction extracts a single (scalar)
1530 /// element from a VectorType value
1531 ///
1532 class ExtractElementInst : public Instruction {
1533   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1534                      Instruction *InsertBefore = 0);
1535   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1536                      BasicBlock *InsertAtEnd);
1537 protected:
1538   virtual ExtractElementInst *clone_impl() const;
1539
1540 public:
1541   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1542                                    const Twine &NameStr = "",
1543                                    Instruction *InsertBefore = 0) {
1544     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1545   }
1546   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1547                                    const Twine &NameStr,
1548                                    BasicBlock *InsertAtEnd) {
1549     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1550   }
1551
1552   /// isValidOperands - Return true if an extractelement instruction can be
1553   /// formed with the specified operands.
1554   static bool isValidOperands(const Value *Vec, const Value *Idx);
1555
1556   Value *getVectorOperand() { return Op<0>(); }
1557   Value *getIndexOperand() { return Op<1>(); }
1558   const Value *getVectorOperand() const { return Op<0>(); }
1559   const Value *getIndexOperand() const { return Op<1>(); }
1560
1561   VectorType *getVectorOperandType() const {
1562     return reinterpret_cast<VectorType*>(getVectorOperand()->getType());
1563   }
1564
1565
1566   /// Transparently provide more efficient getOperand methods.
1567   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1568
1569   // Methods for support type inquiry through isa, cast, and dyn_cast:
1570   static inline bool classof(const ExtractElementInst *) { return true; }
1571   static inline bool classof(const Instruction *I) {
1572     return I->getOpcode() == Instruction::ExtractElement;
1573   }
1574   static inline bool classof(const Value *V) {
1575     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1576   }
1577 };
1578
1579 template <>
1580 struct OperandTraits<ExtractElementInst> :
1581   public FixedNumOperandTraits<ExtractElementInst, 2> {
1582 };
1583
1584 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1585
1586 //===----------------------------------------------------------------------===//
1587 //                                InsertElementInst Class
1588 //===----------------------------------------------------------------------===//
1589
1590 /// InsertElementInst - This instruction inserts a single (scalar)
1591 /// element into a VectorType value
1592 ///
1593 class InsertElementInst : public Instruction {
1594   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1595                     const Twine &NameStr = "",
1596                     Instruction *InsertBefore = 0);
1597   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1598                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1599 protected:
1600   virtual InsertElementInst *clone_impl() const;
1601
1602 public:
1603   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1604                                    const Twine &NameStr = "",
1605                                    Instruction *InsertBefore = 0) {
1606     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1607   }
1608   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1609                                    const Twine &NameStr,
1610                                    BasicBlock *InsertAtEnd) {
1611     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1612   }
1613
1614   /// isValidOperands - Return true if an insertelement instruction can be
1615   /// formed with the specified operands.
1616   static bool isValidOperands(const Value *Vec, const Value *NewElt,
1617                               const Value *Idx);
1618
1619   /// getType - Overload to return most specific vector type.
1620   ///
1621   VectorType *getType() const {
1622     return reinterpret_cast<VectorType*>(Instruction::getType());
1623   }
1624
1625   /// Transparently provide more efficient getOperand methods.
1626   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1627
1628   // Methods for support type inquiry through isa, cast, and dyn_cast:
1629   static inline bool classof(const InsertElementInst *) { return true; }
1630   static inline bool classof(const Instruction *I) {
1631     return I->getOpcode() == Instruction::InsertElement;
1632   }
1633   static inline bool classof(const Value *V) {
1634     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1635   }
1636 };
1637
1638 template <>
1639 struct OperandTraits<InsertElementInst> :
1640   public FixedNumOperandTraits<InsertElementInst, 3> {
1641 };
1642
1643 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1644
1645 //===----------------------------------------------------------------------===//
1646 //                           ShuffleVectorInst Class
1647 //===----------------------------------------------------------------------===//
1648
1649 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1650 /// input vectors.
1651 ///
1652 class ShuffleVectorInst : public Instruction {
1653 protected:
1654   virtual ShuffleVectorInst *clone_impl() const;
1655
1656 public:
1657   // allocate space for exactly three operands
1658   void *operator new(size_t s) {
1659     return User::operator new(s, 3);
1660   }
1661   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1662                     const Twine &NameStr = "",
1663                     Instruction *InsertBefor = 0);
1664   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1665                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1666
1667   /// isValidOperands - Return true if a shufflevector instruction can be
1668   /// formed with the specified operands.
1669   static bool isValidOperands(const Value *V1, const Value *V2,
1670                               const Value *Mask);
1671
1672   /// getType - Overload to return most specific vector type.
1673   ///
1674   VectorType *getType() const {
1675     return reinterpret_cast<VectorType*>(Instruction::getType());
1676   }
1677
1678   /// Transparently provide more efficient getOperand methods.
1679   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1680
1681   Constant *getMask() const {
1682     return reinterpret_cast<Constant*>(getOperand(2));
1683   }
1684   
1685   /// getMaskValue - Return the index from the shuffle mask for the specified
1686   /// output result.  This is either -1 if the element is undef or a number less
1687   /// than 2*numelements.
1688   static int getMaskValue(Constant *Mask, unsigned i);
1689
1690   int getMaskValue(unsigned i) const {
1691     return getMaskValue(getMask(), i);
1692   }
1693   
1694   /// getShuffleMask - Return the full mask for this instruction, where each
1695   /// element is the element number and undef's are returned as -1.
1696   static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1697
1698   void getShuffleMask(SmallVectorImpl<int> &Result) const {
1699     return getShuffleMask(getMask(), Result);
1700   }
1701
1702   SmallVector<int, 16> getShuffleMask() const {
1703     SmallVector<int, 16> Mask;
1704     getShuffleMask(Mask);
1705     return Mask;
1706   }
1707
1708
1709   // Methods for support type inquiry through isa, cast, and dyn_cast:
1710   static inline bool classof(const ShuffleVectorInst *) { return true; }
1711   static inline bool classof(const Instruction *I) {
1712     return I->getOpcode() == Instruction::ShuffleVector;
1713   }
1714   static inline bool classof(const Value *V) {
1715     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1716   }
1717 };
1718
1719 template <>
1720 struct OperandTraits<ShuffleVectorInst> :
1721   public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1722 };
1723
1724 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1725
1726 //===----------------------------------------------------------------------===//
1727 //                                ExtractValueInst Class
1728 //===----------------------------------------------------------------------===//
1729
1730 /// ExtractValueInst - This instruction extracts a struct member or array
1731 /// element value from an aggregate value.
1732 ///
1733 class ExtractValueInst : public UnaryInstruction {
1734   SmallVector<unsigned, 4> Indices;
1735
1736   ExtractValueInst(const ExtractValueInst &EVI);
1737   void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1738
1739   /// Constructors - Create a extractvalue instruction with a base aggregate
1740   /// value and a list of indices.  The first ctor can optionally insert before
1741   /// an existing instruction, the second appends the new instruction to the
1742   /// specified BasicBlock.
1743   inline ExtractValueInst(Value *Agg,
1744                           ArrayRef<unsigned> Idxs,
1745                           const Twine &NameStr,
1746                           Instruction *InsertBefore);
1747   inline ExtractValueInst(Value *Agg,
1748                           ArrayRef<unsigned> Idxs,
1749                           const Twine &NameStr, BasicBlock *InsertAtEnd);
1750
1751   // allocate space for exactly one operand
1752   void *operator new(size_t s) {
1753     return User::operator new(s, 1);
1754   }
1755 protected:
1756   virtual ExtractValueInst *clone_impl() const;
1757
1758 public:
1759   static ExtractValueInst *Create(Value *Agg,
1760                                   ArrayRef<unsigned> Idxs,
1761                                   const Twine &NameStr = "",
1762                                   Instruction *InsertBefore = 0) {
1763     return new
1764       ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1765   }
1766   static ExtractValueInst *Create(Value *Agg,
1767                                   ArrayRef<unsigned> Idxs,
1768                                   const Twine &NameStr,
1769                                   BasicBlock *InsertAtEnd) {
1770     return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1771   }
1772
1773   /// getIndexedType - Returns the type of the element that would be extracted
1774   /// with an extractvalue instruction with the specified parameters.
1775   ///
1776   /// Null is returned if the indices are invalid for the specified type.
1777   static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1778
1779   typedef const unsigned* idx_iterator;
1780   inline idx_iterator idx_begin() const { return Indices.begin(); }
1781   inline idx_iterator idx_end()   const { return Indices.end(); }
1782
1783   Value *getAggregateOperand() {
1784     return getOperand(0);
1785   }
1786   const Value *getAggregateOperand() const {
1787     return getOperand(0);
1788   }
1789   static unsigned getAggregateOperandIndex() {
1790     return 0U;                      // get index for modifying correct operand
1791   }
1792
1793   ArrayRef<unsigned> getIndices() const {
1794     return Indices;
1795   }
1796
1797   unsigned getNumIndices() const {
1798     return (unsigned)Indices.size();
1799   }
1800
1801   bool hasIndices() const {
1802     return true;
1803   }
1804
1805   // Methods for support type inquiry through isa, cast, and dyn_cast:
1806   static inline bool classof(const ExtractValueInst *) { return true; }
1807   static inline bool classof(const Instruction *I) {
1808     return I->getOpcode() == Instruction::ExtractValue;
1809   }
1810   static inline bool classof(const Value *V) {
1811     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1812   }
1813 };
1814
1815 ExtractValueInst::ExtractValueInst(Value *Agg,
1816                                    ArrayRef<unsigned> Idxs,
1817                                    const Twine &NameStr,
1818                                    Instruction *InsertBefore)
1819   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1820                      ExtractValue, Agg, InsertBefore) {
1821   init(Idxs, NameStr);
1822 }
1823 ExtractValueInst::ExtractValueInst(Value *Agg,
1824                                    ArrayRef<unsigned> Idxs,
1825                                    const Twine &NameStr,
1826                                    BasicBlock *InsertAtEnd)
1827   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1828                      ExtractValue, Agg, InsertAtEnd) {
1829   init(Idxs, NameStr);
1830 }
1831
1832
1833 //===----------------------------------------------------------------------===//
1834 //                                InsertValueInst Class
1835 //===----------------------------------------------------------------------===//
1836
1837 /// InsertValueInst - This instruction inserts a struct field of array element
1838 /// value into an aggregate value.
1839 ///
1840 class InsertValueInst : public Instruction {
1841   SmallVector<unsigned, 4> Indices;
1842
1843   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1844   InsertValueInst(const InsertValueInst &IVI);
1845   void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1846             const Twine &NameStr);
1847
1848   /// Constructors - Create a insertvalue instruction with a base aggregate
1849   /// value, a value to insert, and a list of indices.  The first ctor can
1850   /// optionally insert before an existing instruction, the second appends
1851   /// the new instruction to the specified BasicBlock.
1852   inline InsertValueInst(Value *Agg, Value *Val,
1853                          ArrayRef<unsigned> Idxs,
1854                          const Twine &NameStr,
1855                          Instruction *InsertBefore);
1856   inline InsertValueInst(Value *Agg, Value *Val,
1857                          ArrayRef<unsigned> Idxs,
1858                          const Twine &NameStr, BasicBlock *InsertAtEnd);
1859
1860   /// Constructors - These two constructors are convenience methods because one
1861   /// and two index insertvalue instructions are so common.
1862   InsertValueInst(Value *Agg, Value *Val,
1863                   unsigned Idx, const Twine &NameStr = "",
1864                   Instruction *InsertBefore = 0);
1865   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1866                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1867 protected:
1868   virtual InsertValueInst *clone_impl() const;
1869 public:
1870   // allocate space for exactly two operands
1871   void *operator new(size_t s) {
1872     return User::operator new(s, 2);
1873   }
1874
1875   static InsertValueInst *Create(Value *Agg, Value *Val,
1876                                  ArrayRef<unsigned> Idxs,
1877                                  const Twine &NameStr = "",
1878                                  Instruction *InsertBefore = 0) {
1879     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1880   }
1881   static InsertValueInst *Create(Value *Agg, Value *Val,
1882                                  ArrayRef<unsigned> Idxs,
1883                                  const Twine &NameStr,
1884                                  BasicBlock *InsertAtEnd) {
1885     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1886   }
1887
1888   /// Transparently provide more efficient getOperand methods.
1889   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1890
1891   typedef const unsigned* idx_iterator;
1892   inline idx_iterator idx_begin() const { return Indices.begin(); }
1893   inline idx_iterator idx_end()   const { return Indices.end(); }
1894
1895   Value *getAggregateOperand() {
1896     return getOperand(0);
1897   }
1898   const Value *getAggregateOperand() const {
1899     return getOperand(0);
1900   }
1901   static unsigned getAggregateOperandIndex() {
1902     return 0U;                      // get index for modifying correct operand
1903   }
1904
1905   Value *getInsertedValueOperand() {
1906     return getOperand(1);
1907   }
1908   const Value *getInsertedValueOperand() const {
1909     return getOperand(1);
1910   }
1911   static unsigned getInsertedValueOperandIndex() {
1912     return 1U;                      // get index for modifying correct operand
1913   }
1914
1915   ArrayRef<unsigned> getIndices() const {
1916     return Indices;
1917   }
1918
1919   unsigned getNumIndices() const {
1920     return (unsigned)Indices.size();
1921   }
1922
1923   bool hasIndices() const {
1924     return true;
1925   }
1926
1927   // Methods for support type inquiry through isa, cast, and dyn_cast:
1928   static inline bool classof(const InsertValueInst *) { return true; }
1929   static inline bool classof(const Instruction *I) {
1930     return I->getOpcode() == Instruction::InsertValue;
1931   }
1932   static inline bool classof(const Value *V) {
1933     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1934   }
1935 };
1936
1937 template <>
1938 struct OperandTraits<InsertValueInst> :
1939   public FixedNumOperandTraits<InsertValueInst, 2> {
1940 };
1941
1942 InsertValueInst::InsertValueInst(Value *Agg,
1943                                  Value *Val,
1944                                  ArrayRef<unsigned> Idxs,
1945                                  const Twine &NameStr,
1946                                  Instruction *InsertBefore)
1947   : Instruction(Agg->getType(), InsertValue,
1948                 OperandTraits<InsertValueInst>::op_begin(this),
1949                 2, InsertBefore) {
1950   init(Agg, Val, Idxs, NameStr);
1951 }
1952 InsertValueInst::InsertValueInst(Value *Agg,
1953                                  Value *Val,
1954                                  ArrayRef<unsigned> Idxs,
1955                                  const Twine &NameStr,
1956                                  BasicBlock *InsertAtEnd)
1957   : Instruction(Agg->getType(), InsertValue,
1958                 OperandTraits<InsertValueInst>::op_begin(this),
1959                 2, InsertAtEnd) {
1960   init(Agg, Val, Idxs, NameStr);
1961 }
1962
1963 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1964
1965 //===----------------------------------------------------------------------===//
1966 //                               PHINode Class
1967 //===----------------------------------------------------------------------===//
1968
1969 // PHINode - The PHINode class is used to represent the magical mystical PHI
1970 // node, that can not exist in nature, but can be synthesized in a computer
1971 // scientist's overactive imagination.
1972 //
1973 class PHINode : public Instruction {
1974   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1975   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1976   /// the number actually in use.
1977   unsigned ReservedSpace;
1978   PHINode(const PHINode &PN);
1979   // allocate space for exactly zero operands
1980   void *operator new(size_t s) {
1981     return User::operator new(s, 0);
1982   }
1983   explicit PHINode(Type *Ty, unsigned NumReservedValues,
1984                    const Twine &NameStr = "", Instruction *InsertBefore = 0)
1985     : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1986       ReservedSpace(NumReservedValues) {
1987     setName(NameStr);
1988     OperandList = allocHungoffUses(ReservedSpace);
1989   }
1990
1991   PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1992           BasicBlock *InsertAtEnd)
1993     : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1994       ReservedSpace(NumReservedValues) {
1995     setName(NameStr);
1996     OperandList = allocHungoffUses(ReservedSpace);
1997   }
1998 protected:
1999   // allocHungoffUses - this is more complicated than the generic
2000   // User::allocHungoffUses, because we have to allocate Uses for the incoming
2001   // values and pointers to the incoming blocks, all in one allocation.
2002   Use *allocHungoffUses(unsigned) const;
2003
2004   virtual PHINode *clone_impl() const;
2005 public:
2006   /// Constructors - NumReservedValues is a hint for the number of incoming
2007   /// edges that this phi node will have (use 0 if you really have no idea).
2008   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2009                          const Twine &NameStr = "",
2010                          Instruction *InsertBefore = 0) {
2011     return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2012   }
2013   static PHINode *Create(Type *Ty, unsigned NumReservedValues, 
2014                          const Twine &NameStr, BasicBlock *InsertAtEnd) {
2015     return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2016   }
2017   ~PHINode();
2018
2019   /// Provide fast operand accessors
2020   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2021
2022   // Block iterator interface. This provides access to the list of incoming
2023   // basic blocks, which parallels the list of incoming values.
2024
2025   typedef BasicBlock **block_iterator;
2026   typedef BasicBlock * const *const_block_iterator;
2027
2028   block_iterator block_begin() {
2029     Use::UserRef *ref =
2030       reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2031     return reinterpret_cast<block_iterator>(ref + 1);
2032   }
2033
2034   const_block_iterator block_begin() const {
2035     const Use::UserRef *ref =
2036       reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2037     return reinterpret_cast<const_block_iterator>(ref + 1);
2038   }
2039
2040   block_iterator block_end() {
2041     return block_begin() + getNumOperands();
2042   }
2043
2044   const_block_iterator block_end() const {
2045     return block_begin() + getNumOperands();
2046   }
2047
2048   /// getNumIncomingValues - Return the number of incoming edges
2049   ///
2050   unsigned getNumIncomingValues() const { return getNumOperands(); }
2051
2052   /// getIncomingValue - Return incoming value number x
2053   ///
2054   Value *getIncomingValue(unsigned i) const {
2055     return getOperand(i);
2056   }
2057   void setIncomingValue(unsigned i, Value *V) {
2058     setOperand(i, V);
2059   }
2060   static unsigned getOperandNumForIncomingValue(unsigned i) {
2061     return i;
2062   }
2063   static unsigned getIncomingValueNumForOperand(unsigned i) {
2064     return i;
2065   }
2066
2067   /// getIncomingBlock - Return incoming basic block number @p i.
2068   ///
2069   BasicBlock *getIncomingBlock(unsigned i) const {
2070     return block_begin()[i];
2071   }
2072
2073   /// getIncomingBlock - Return incoming basic block corresponding
2074   /// to an operand of the PHI.
2075   ///
2076   BasicBlock *getIncomingBlock(const Use &U) const {
2077     assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2078     return getIncomingBlock(unsigned(&U - op_begin()));
2079   }
2080
2081   /// getIncomingBlock - Return incoming basic block corresponding
2082   /// to value use iterator.
2083   ///
2084   template <typename U>
2085   BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2086     return getIncomingBlock(I.getUse());
2087   }
2088
2089   void setIncomingBlock(unsigned i, BasicBlock *BB) {
2090     block_begin()[i] = BB;
2091   }
2092
2093   /// addIncoming - Add an incoming value to the end of the PHI list
2094   ///
2095   void addIncoming(Value *V, BasicBlock *BB) {
2096     assert(V && "PHI node got a null value!");
2097     assert(BB && "PHI node got a null basic block!");
2098     assert(getType() == V->getType() &&
2099            "All operands to PHI node must be the same type as the PHI node!");
2100     if (NumOperands == ReservedSpace)
2101       growOperands();  // Get more space!
2102     // Initialize some new operands.
2103     ++NumOperands;
2104     setIncomingValue(NumOperands - 1, V);
2105     setIncomingBlock(NumOperands - 1, BB);
2106   }
2107
2108   /// removeIncomingValue - Remove an incoming value.  This is useful if a
2109   /// predecessor basic block is deleted.  The value removed is returned.
2110   ///
2111   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2112   /// is true), the PHI node is destroyed and any uses of it are replaced with
2113   /// dummy values.  The only time there should be zero incoming values to a PHI
2114   /// node is when the block is dead, so this strategy is sound.
2115   ///
2116   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2117
2118   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2119     int Idx = getBasicBlockIndex(BB);
2120     assert(Idx >= 0 && "Invalid basic block argument to remove!");
2121     return removeIncomingValue(Idx, DeletePHIIfEmpty);
2122   }
2123
2124   /// getBasicBlockIndex - Return the first index of the specified basic
2125   /// block in the value list for this PHI.  Returns -1 if no instance.
2126   ///
2127   int getBasicBlockIndex(const BasicBlock *BB) const {
2128     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2129       if (block_begin()[i] == BB)
2130         return i;
2131     return -1;
2132   }
2133
2134   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2135     int Idx = getBasicBlockIndex(BB);
2136     assert(Idx >= 0 && "Invalid basic block argument!");
2137     return getIncomingValue(Idx);
2138   }
2139
2140   /// hasConstantValue - If the specified PHI node always merges together the
2141   /// same value, return the value, otherwise return null.
2142   Value *hasConstantValue() const;
2143
2144   /// Methods for support type inquiry through isa, cast, and dyn_cast:
2145   static inline bool classof(const PHINode *) { return true; }
2146   static inline bool classof(const Instruction *I) {
2147     return I->getOpcode() == Instruction::PHI;
2148   }
2149   static inline bool classof(const Value *V) {
2150     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2151   }
2152  private:
2153   void growOperands();
2154 };
2155
2156 template <>
2157 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2158 };
2159
2160 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2161
2162 //===----------------------------------------------------------------------===//
2163 //                           LandingPadInst Class
2164 //===----------------------------------------------------------------------===//
2165
2166 //===---------------------------------------------------------------------------
2167 /// LandingPadInst - The landingpad instruction holds all of the information
2168 /// necessary to generate correct exception handling. The landingpad instruction
2169 /// cannot be moved from the top of a landing pad block, which itself is
2170 /// accessible only from the 'unwind' edge of an invoke. This uses the
2171 /// SubclassData field in Value to store whether or not the landingpad is a
2172 /// cleanup.
2173 ///
2174 class LandingPadInst : public Instruction {
2175   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2176   /// the number actually in use.
2177   unsigned ReservedSpace;
2178   LandingPadInst(const LandingPadInst &LP);
2179 public:
2180   enum ClauseType { Catch, Filter };
2181 private:
2182   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2183   // Allocate space for exactly zero operands.
2184   void *operator new(size_t s) {
2185     return User::operator new(s, 0);
2186   }
2187   void growOperands(unsigned Size);
2188   void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2189
2190   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2191                           unsigned NumReservedValues, const Twine &NameStr,
2192                           Instruction *InsertBefore);
2193   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2194                           unsigned NumReservedValues, const Twine &NameStr,
2195                           BasicBlock *InsertAtEnd);
2196 protected:
2197   virtual LandingPadInst *clone_impl() const;
2198 public:
2199   /// Constructors - NumReservedClauses is a hint for the number of incoming
2200   /// clauses that this landingpad will have (use 0 if you really have no idea).
2201   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2202                                 unsigned NumReservedClauses,
2203                                 const Twine &NameStr = "",
2204                                 Instruction *InsertBefore = 0);
2205   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2206                                 unsigned NumReservedClauses,
2207                                 const Twine &NameStr, BasicBlock *InsertAtEnd);
2208   ~LandingPadInst();
2209
2210   /// Provide fast operand accessors
2211   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2212
2213   /// getPersonalityFn - Get the personality function associated with this
2214   /// landing pad.
2215   Value *getPersonalityFn() const { return getOperand(0); }
2216
2217   /// isCleanup - Return 'true' if this landingpad instruction is a
2218   /// cleanup. I.e., it should be run when unwinding even if its landing pad
2219   /// doesn't catch the exception.
2220   bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2221
2222   /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2223   void setCleanup(bool V) {
2224     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2225                                (V ? 1 : 0));
2226   }
2227
2228   /// addClause - Add a catch or filter clause to the landing pad.
2229   void addClause(Value *ClauseVal);
2230
2231   /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2232   /// to determine what type of clause this is.
2233   Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2234
2235   /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2236   bool isCatch(unsigned Idx) const {
2237     return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2238   }
2239
2240   /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2241   bool isFilter(unsigned Idx) const {
2242     return isa<ArrayType>(OperandList[Idx + 1]->getType());
2243   }
2244
2245   /// getNumClauses - Get the number of clauses for this landing pad.
2246   unsigned getNumClauses() const { return getNumOperands() - 1; }
2247
2248   /// reserveClauses - Grow the size of the operand list to accommodate the new
2249   /// number of clauses.
2250   void reserveClauses(unsigned Size) { growOperands(Size); }
2251
2252   // Methods for support type inquiry through isa, cast, and dyn_cast:
2253   static inline bool classof(const LandingPadInst *) { return true; }
2254   static inline bool classof(const Instruction *I) {
2255     return I->getOpcode() == Instruction::LandingPad;
2256   }
2257   static inline bool classof(const Value *V) {
2258     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2259   }
2260 };
2261
2262 template <>
2263 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2264 };
2265
2266 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2267
2268 //===----------------------------------------------------------------------===//
2269 //                               ReturnInst Class
2270 //===----------------------------------------------------------------------===//
2271
2272 //===---------------------------------------------------------------------------
2273 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2274 /// does not continue in this function any longer.
2275 ///
2276 class ReturnInst : public TerminatorInst {
2277   ReturnInst(const ReturnInst &RI);
2278
2279 private:
2280   // ReturnInst constructors:
2281   // ReturnInst()                  - 'ret void' instruction
2282   // ReturnInst(    null)          - 'ret void' instruction
2283   // ReturnInst(Value* X)          - 'ret X'    instruction
2284   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2285   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2286   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2287   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2288   //
2289   // NOTE: If the Value* passed is of type void then the constructor behaves as
2290   // if it was passed NULL.
2291   explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2292                       Instruction *InsertBefore = 0);
2293   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2294   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2295 protected:
2296   virtual ReturnInst *clone_impl() const;
2297 public:
2298   static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2299                             Instruction *InsertBefore = 0) {
2300     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2301   }
2302   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2303                             BasicBlock *InsertAtEnd) {
2304     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2305   }
2306   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2307     return new(0) ReturnInst(C, InsertAtEnd);
2308   }
2309   virtual ~ReturnInst();
2310
2311   /// Provide fast operand accessors
2312   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2313
2314   /// Convenience accessor. Returns null if there is no return value.
2315   Value *getReturnValue() const {
2316     return getNumOperands() != 0 ? getOperand(0) : 0;
2317   }
2318
2319   unsigned getNumSuccessors() const { return 0; }
2320
2321   // Methods for support type inquiry through isa, cast, and dyn_cast:
2322   static inline bool classof(const ReturnInst *) { return true; }
2323   static inline bool classof(const Instruction *I) {
2324     return (I->getOpcode() == Instruction::Ret);
2325   }
2326   static inline bool classof(const Value *V) {
2327     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2328   }
2329  private:
2330   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2331   virtual unsigned getNumSuccessorsV() const;
2332   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2333 };
2334
2335 template <>
2336 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2337 };
2338
2339 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2340
2341 //===----------------------------------------------------------------------===//
2342 //                               BranchInst Class
2343 //===----------------------------------------------------------------------===//
2344
2345 //===---------------------------------------------------------------------------
2346 /// BranchInst - Conditional or Unconditional Branch instruction.
2347 ///
2348 class BranchInst : public TerminatorInst {
2349   /// Ops list - Branches are strange.  The operands are ordered:
2350   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2351   /// they don't have to check for cond/uncond branchness. These are mostly
2352   /// accessed relative from op_end().
2353   BranchInst(const BranchInst &BI);
2354   void AssertOK();
2355   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2356   // BranchInst(BB *B)                           - 'br B'
2357   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2358   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2359   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2360   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2361   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2362   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2363   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2364              Instruction *InsertBefore = 0);
2365   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2366   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2367              BasicBlock *InsertAtEnd);
2368 protected:
2369   virtual BranchInst *clone_impl() const;
2370 public:
2371   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2372     return new(1) BranchInst(IfTrue, InsertBefore);
2373   }
2374   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2375                             Value *Cond, Instruction *InsertBefore = 0) {
2376     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2377   }
2378   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2379     return new(1) BranchInst(IfTrue, InsertAtEnd);
2380   }
2381   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2382                             Value *Cond, BasicBlock *InsertAtEnd) {
2383     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2384   }
2385
2386   /// Transparently provide more efficient getOperand methods.
2387   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2388
2389   bool isUnconditional() const { return getNumOperands() == 1; }
2390   bool isConditional()   const { return getNumOperands() == 3; }
2391
2392   Value *getCondition() const {
2393     assert(isConditional() && "Cannot get condition of an uncond branch!");
2394     return Op<-3>();
2395   }
2396
2397   void setCondition(Value *V) {
2398     assert(isConditional() && "Cannot set condition of unconditional branch!");
2399     Op<-3>() = V;
2400   }
2401
2402   unsigned getNumSuccessors() const { return 1+isConditional(); }
2403
2404   BasicBlock *getSuccessor(unsigned i) const {
2405     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2406     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2407   }
2408
2409   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2410     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2411     *(&Op<-1>() - idx) = (Value*)NewSucc;
2412   }
2413
2414   /// \brief Swap the successors of this branch instruction.
2415   ///
2416   /// Swaps the successors of the branch instruction. This also swaps any
2417   /// branch weight metadata associated with the instruction so that it
2418   /// continues to map correctly to each operand.
2419   void swapSuccessors();
2420
2421   // Methods for support type inquiry through isa, cast, and dyn_cast:
2422   static inline bool classof(const BranchInst *) { return true; }
2423   static inline bool classof(const Instruction *I) {
2424     return (I->getOpcode() == Instruction::Br);
2425   }
2426   static inline bool classof(const Value *V) {
2427     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2428   }
2429 private:
2430   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2431   virtual unsigned getNumSuccessorsV() const;
2432   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2433 };
2434
2435 template <>
2436 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2437 };
2438
2439 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2440
2441 //===----------------------------------------------------------------------===//
2442 //                               SwitchInst Class
2443 //===----------------------------------------------------------------------===//
2444
2445 //===---------------------------------------------------------------------------
2446 /// SwitchInst - Multiway switch
2447 ///
2448 class SwitchInst : public TerminatorInst {
2449   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2450   unsigned ReservedSpace;
2451   // Operands format:
2452   // Operand[0]    = Value to switch on
2453   // Operand[1]    = Default basic block destination
2454   // Operand[2n  ] = Value to match
2455   // Operand[2n+1] = BasicBlock to go to on match
2456   
2457   // Store case values separately from operands list. We needn't User-Use
2458   // concept here, since it is just a case value, it will always constant,
2459   // and case value couldn't reused with another instructions/values.
2460   // Additionally:
2461   // It allows us to use custom type for case values that is not inherited
2462   // from Value. Since case value is a complex type that implements
2463   // the subset of integers, we needn't extract sub-constants within
2464   // slow getAggregateElement method.
2465   // For case values we will use std::list to by two reasons:
2466   // 1. It allows to add/remove cases without whole collection reallocation.
2467   // 2. In most of cases we needn't random access.
2468   // Currently case values are also stored in Operands List, but it will moved
2469   // out in future commits.
2470   typedef std::list<IntegersSubset> Subsets;
2471   typedef Subsets::iterator SubsetsIt;
2472   typedef Subsets::const_iterator SubsetsConstIt;
2473   
2474   Subsets TheSubsets;
2475   
2476   SwitchInst(const SwitchInst &SI);
2477   void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2478   void growOperands();
2479   // allocate space for exactly zero operands
2480   void *operator new(size_t s) {
2481     return User::operator new(s, 0);
2482   }
2483   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2484   /// switch on and a default destination.  The number of additional cases can
2485   /// be specified here to make memory allocation more efficient.  This
2486   /// constructor can also autoinsert before another instruction.
2487   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2488              Instruction *InsertBefore);
2489
2490   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2491   /// switch on and a default destination.  The number of additional cases can
2492   /// be specified here to make memory allocation more efficient.  This
2493   /// constructor also autoinserts at the end of the specified BasicBlock.
2494   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2495              BasicBlock *InsertAtEnd);
2496 protected:
2497   virtual SwitchInst *clone_impl() const;
2498 public:
2499   
2500   // FIXME: Currently there are a lot of unclean template parameters,
2501   // we need to make refactoring in future.
2502   // All these parameters are used to implement both iterator and const_iterator
2503   // without code duplication.
2504   // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2505   // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2506   // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2507   // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2508   template <class SwitchInstTy, class ConstantIntTy,
2509             class SubsetsItTy, class BasicBlockTy> 
2510     class CaseIteratorT;
2511
2512   typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2513                         SubsetsConstIt, const BasicBlock> ConstCaseIt;
2514   class CaseIt;
2515   
2516   // -2
2517   static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2518   
2519   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2520                             unsigned NumCases, Instruction *InsertBefore = 0) {
2521     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2522   }
2523   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2524                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2525     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2526   }
2527   
2528   ~SwitchInst();
2529
2530   /// Provide fast operand accessors
2531   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2532
2533   // Accessor Methods for Switch stmt
2534   Value *getCondition() const { return getOperand(0); }
2535   void setCondition(Value *V) { setOperand(0, V); }
2536
2537   BasicBlock *getDefaultDest() const {
2538     return cast<BasicBlock>(getOperand(1));
2539   }
2540
2541   void setDefaultDest(BasicBlock *DefaultCase) {
2542     setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2543   }
2544
2545   /// getNumCases - return the number of 'cases' in this switch instruction,
2546   /// except the default case
2547   unsigned getNumCases() const {
2548     return getNumOperands()/2 - 1;
2549   }
2550
2551   /// Returns a read/write iterator that points to the first
2552   /// case in SwitchInst.
2553   CaseIt case_begin() {
2554     return CaseIt(this, 0, TheSubsets.begin());
2555   }
2556   /// Returns a read-only iterator that points to the first
2557   /// case in the SwitchInst.
2558   ConstCaseIt case_begin() const {
2559     return ConstCaseIt(this, 0, TheSubsets.begin());
2560   }
2561   
2562   /// Returns a read/write iterator that points one past the last
2563   /// in the SwitchInst.
2564   CaseIt case_end() {
2565     return CaseIt(this, getNumCases(), TheSubsets.end());
2566   }
2567   /// Returns a read-only iterator that points one past the last
2568   /// in the SwitchInst.
2569   ConstCaseIt case_end() const {
2570     return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2571   }
2572   /// Returns an iterator that points to the default case.
2573   /// Note: this iterator allows to resolve successor only. Attempt
2574   /// to resolve case value causes an assertion.
2575   /// Also note, that increment and decrement also causes an assertion and
2576   /// makes iterator invalid. 
2577   CaseIt case_default() {
2578     return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2579   }
2580   ConstCaseIt case_default() const {
2581     return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2582   }
2583   
2584   /// findCaseValue - Search all of the case values for the specified constant.
2585   /// If it is explicitly handled, return the case iterator of it, otherwise
2586   /// return default case iterator to indicate
2587   /// that it is handled by the default handler.
2588   CaseIt findCaseValue(const ConstantInt *C) {
2589     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2590       if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2591         return i;
2592     return case_default();
2593   }
2594   ConstCaseIt findCaseValue(const ConstantInt *C) const {
2595     for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2596       if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2597         return i;
2598     return case_default();
2599   }    
2600   
2601   /// findCaseDest - Finds the unique case value for a given successor. Returns
2602   /// null if the successor is not found, not unique, or is the default case.
2603   ConstantInt *findCaseDest(BasicBlock *BB) {
2604     if (BB == getDefaultDest()) return NULL;
2605
2606     ConstantInt *CI = NULL;
2607     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2608       if (i.getCaseSuccessor() == BB) {
2609         if (CI) return NULL;   // Multiple cases lead to BB.
2610         else CI = i.getCaseValue();
2611       }
2612     }
2613     return CI;
2614   }
2615
2616   /// addCase - Add an entry to the switch instruction...
2617   /// @deprecated
2618   /// Note:
2619   /// This action invalidates case_end(). Old case_end() iterator will
2620   /// point to the added case.
2621   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2622   
2623   /// addCase - Add an entry to the switch instruction.
2624   /// Note:
2625   /// This action invalidates case_end(). Old case_end() iterator will
2626   /// point to the added case.
2627   void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2628
2629   /// removeCase - This method removes the specified case and its successor
2630   /// from the switch instruction. Note that this operation may reorder the
2631   /// remaining cases at index idx and above.
2632   /// Note:
2633   /// This action invalidates iterators for all cases following the one removed,
2634   /// including the case_end() iterator.
2635   void removeCase(CaseIt& i);
2636
2637   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2638   BasicBlock *getSuccessor(unsigned idx) const {
2639     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2640     return cast<BasicBlock>(getOperand(idx*2+1));
2641   }
2642   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2643     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2644     setOperand(idx*2+1, (Value*)NewSucc);
2645   }
2646   
2647   uint16_t hash() const {
2648     uint32_t NumberOfCases = (uint32_t)getNumCases();
2649     uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2650     for (ConstCaseIt i = case_begin(), e = case_end();
2651          i != e; ++i) {
2652       uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems(); 
2653       Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2654     }
2655     return Hash;
2656   }  
2657   
2658   // Case iterators definition.
2659
2660   template <class SwitchInstTy, class ConstantIntTy,
2661             class SubsetsItTy, class BasicBlockTy> 
2662   class CaseIteratorT {
2663   protected:
2664     
2665     SwitchInstTy *SI;
2666     unsigned long Index;
2667     SubsetsItTy SubsetIt;
2668     
2669     /// Initializes case iterator for given SwitchInst and for given
2670     /// case number.    
2671     friend class SwitchInst;
2672     CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2673                   SubsetsItTy CaseValueIt) {
2674       this->SI = SI;
2675       Index = SuccessorIndex;
2676       this->SubsetIt = CaseValueIt;
2677     }
2678     
2679   public:
2680     typedef typename SubsetsItTy::reference IntegersSubsetRef;
2681     typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2682                           SubsetsItTy, BasicBlockTy> Self;
2683     
2684     CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2685           this->SI = SI;
2686           Index = CaseNum;
2687           SubsetIt = SI->TheSubsets.begin();
2688           std::advance(SubsetIt, CaseNum);
2689         }
2690         
2691     
2692     /// Initializes case iterator for given SwitchInst and for given
2693     /// TerminatorInst's successor index.
2694     static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2695       assert(SuccessorIndex < SI->getNumSuccessors() &&
2696              "Successor index # out of range!");    
2697       return SuccessorIndex != 0 ? 
2698              Self(SI, SuccessorIndex - 1) :
2699              Self(SI, DefaultPseudoIndex);       
2700     }
2701     
2702     /// Resolves case value for current case.
2703     /// @deprecated
2704     ConstantIntTy *getCaseValue() {
2705       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2706       IntegersSubsetRef CaseRanges = *SubsetIt;
2707       
2708       // FIXME: Currently we work with ConstantInt based cases.
2709       // So return CaseValue as ConstantInt.
2710       return CaseRanges.getSingleNumber(0).toConstantInt();
2711     }
2712
2713     /// Resolves case value for current case.
2714     IntegersSubsetRef getCaseValueEx() {
2715       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2716       return *SubsetIt;
2717     }
2718     
2719     /// Resolves successor for current case.
2720     BasicBlockTy *getCaseSuccessor() {
2721       assert((Index < SI->getNumCases() ||
2722               Index == DefaultPseudoIndex) &&
2723              "Index out the number of cases.");
2724       return SI->getSuccessor(getSuccessorIndex());      
2725     }
2726     
2727     /// Returns number of current case.
2728     unsigned getCaseIndex() const { return Index; }
2729     
2730     /// Returns TerminatorInst's successor index for current case successor.
2731     unsigned getSuccessorIndex() const {
2732       assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2733              "Index out the number of cases.");
2734       return Index != DefaultPseudoIndex ? Index + 1 : 0;
2735     }
2736     
2737     Self operator++() {
2738       // Check index correctness after increment.
2739       // Note: Index == getNumCases() means end().
2740       assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2741       ++Index;
2742       if (Index == 0)
2743         SubsetIt = SI->TheSubsets.begin();
2744       else
2745         ++SubsetIt;
2746       return *this;
2747     }
2748     Self operator++(int) {
2749       Self tmp = *this;
2750       ++(*this);
2751       return tmp;
2752     }
2753     Self operator--() { 
2754       // Check index correctness after decrement.
2755       // Note: Index == getNumCases() means end().
2756       // Also allow "-1" iterator here. That will became valid after ++.
2757       unsigned NumCases = SI->getNumCases();
2758       assert((Index == 0 || Index-1 <= NumCases) &&
2759              "Index out the number of cases.");
2760       --Index;
2761       if (Index == NumCases) {
2762         SubsetIt = SI->TheSubsets.end();
2763         return *this;
2764       }
2765         
2766       if (Index != -1UL)
2767         --SubsetIt;
2768       
2769       return *this;
2770     }
2771     Self operator--(int) {
2772       Self tmp = *this;
2773       --(*this);
2774       return tmp;
2775     }
2776     bool operator==(const Self& RHS) const {
2777       assert(RHS.SI == SI && "Incompatible operators.");
2778       return RHS.Index == Index;
2779     }
2780     bool operator!=(const Self& RHS) const {
2781       assert(RHS.SI == SI && "Incompatible operators.");
2782       return RHS.Index != Index;
2783     }
2784   };
2785
2786   class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2787                                       SubsetsIt, BasicBlock> {
2788     typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2789       ParentTy;
2790     
2791   protected:
2792     friend class SwitchInst;
2793     CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2794       ParentTy(SI, CaseNum, SubsetIt) {}
2795     
2796     void updateCaseValueOperand(IntegersSubset& V) {
2797       SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));      
2798     }
2799   
2800   public:
2801
2802     CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}    
2803     
2804     CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2805
2806     /// Sets the new value for current case.    
2807     /// @deprecated.
2808     void setValue(ConstantInt *V) {
2809       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2810       IntegersSubsetToBB Mapping;
2811       // FIXME: Currently we work with ConstantInt based cases.
2812       // So inititalize IntItem container directly from ConstantInt.
2813       Mapping.add(IntItem::fromConstantInt(V));
2814       *SubsetIt = Mapping.getCase();
2815       updateCaseValueOperand(*SubsetIt);
2816     }
2817     
2818     /// Sets the new value for current case.
2819     void setValueEx(IntegersSubset& V) {
2820       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2821       *SubsetIt = V;
2822       updateCaseValueOperand(*SubsetIt);   
2823     }
2824     
2825     /// Sets the new successor for current case.
2826     void setSuccessor(BasicBlock *S) {
2827       SI->setSuccessor(getSuccessorIndex(), S);      
2828     }
2829   };
2830
2831   // Methods for support type inquiry through isa, cast, and dyn_cast:
2832
2833   static inline bool classof(const SwitchInst *) { return true; }
2834   static inline bool classof(const Instruction *I) {
2835     return I->getOpcode() == Instruction::Switch;
2836   }
2837   static inline bool classof(const Value *V) {
2838     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2839   }
2840 private:
2841   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2842   virtual unsigned getNumSuccessorsV() const;
2843   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2844 };
2845
2846 template <>
2847 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2848 };
2849
2850 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2851
2852
2853 //===----------------------------------------------------------------------===//
2854 //                             IndirectBrInst Class
2855 //===----------------------------------------------------------------------===//
2856
2857 //===---------------------------------------------------------------------------
2858 /// IndirectBrInst - Indirect Branch Instruction.
2859 ///
2860 class IndirectBrInst : public TerminatorInst {
2861   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2862   unsigned ReservedSpace;
2863   // Operand[0]    = Value to switch on
2864   // Operand[1]    = Default basic block destination
2865   // Operand[2n  ] = Value to match
2866   // Operand[2n+1] = BasicBlock to go to on match
2867   IndirectBrInst(const IndirectBrInst &IBI);
2868   void init(Value *Address, unsigned NumDests);
2869   void growOperands();
2870   // allocate space for exactly zero operands
2871   void *operator new(size_t s) {
2872     return User::operator new(s, 0);
2873   }
2874   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2875   /// Address to jump to.  The number of expected destinations can be specified
2876   /// here to make memory allocation more efficient.  This constructor can also
2877   /// autoinsert before another instruction.
2878   IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2879
2880   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2881   /// Address to jump to.  The number of expected destinations can be specified
2882   /// here to make memory allocation more efficient.  This constructor also
2883   /// autoinserts at the end of the specified BasicBlock.
2884   IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2885 protected:
2886   virtual IndirectBrInst *clone_impl() const;
2887 public:
2888   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2889                                 Instruction *InsertBefore = 0) {
2890     return new IndirectBrInst(Address, NumDests, InsertBefore);
2891   }
2892   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2893                                 BasicBlock *InsertAtEnd) {
2894     return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2895   }
2896   ~IndirectBrInst();
2897
2898   /// Provide fast operand accessors.
2899   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2900
2901   // Accessor Methods for IndirectBrInst instruction.
2902   Value *getAddress() { return getOperand(0); }
2903   const Value *getAddress() const { return getOperand(0); }
2904   void setAddress(Value *V) { setOperand(0, V); }
2905
2906
2907   /// getNumDestinations - return the number of possible destinations in this
2908   /// indirectbr instruction.
2909   unsigned getNumDestinations() const { return getNumOperands()-1; }
2910
2911   /// getDestination - Return the specified destination.
2912   BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2913   const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2914
2915   /// addDestination - Add a destination.
2916   ///
2917   void addDestination(BasicBlock *Dest);
2918
2919   /// removeDestination - This method removes the specified successor from the
2920   /// indirectbr instruction.
2921   void removeDestination(unsigned i);
2922
2923   unsigned getNumSuccessors() const { return getNumOperands()-1; }
2924   BasicBlock *getSuccessor(unsigned i) const {
2925     return cast<BasicBlock>(getOperand(i+1));
2926   }
2927   void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2928     setOperand(i+1, (Value*)NewSucc);
2929   }
2930
2931   // Methods for support type inquiry through isa, cast, and dyn_cast:
2932   static inline bool classof(const IndirectBrInst *) { return true; }
2933   static inline bool classof(const Instruction *I) {
2934     return I->getOpcode() == Instruction::IndirectBr;
2935   }
2936   static inline bool classof(const Value *V) {
2937     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2938   }
2939 private:
2940   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2941   virtual unsigned getNumSuccessorsV() const;
2942   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2943 };
2944
2945 template <>
2946 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2947 };
2948
2949 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2950
2951
2952 //===----------------------------------------------------------------------===//
2953 //                               InvokeInst Class
2954 //===----------------------------------------------------------------------===//
2955
2956 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2957 /// calling convention of the call.
2958 ///
2959 class InvokeInst : public TerminatorInst {
2960   AttrListPtr AttributeList;
2961   InvokeInst(const InvokeInst &BI);
2962   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2963             ArrayRef<Value *> Args, const Twine &NameStr);
2964
2965   /// Construct an InvokeInst given a range of arguments.
2966   ///
2967   /// @brief Construct an InvokeInst from a range of arguments
2968   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2969                     ArrayRef<Value *> Args, unsigned Values,
2970                     const Twine &NameStr, Instruction *InsertBefore);
2971
2972   /// Construct an InvokeInst given a range of arguments.
2973   ///
2974   /// @brief Construct an InvokeInst from a range of arguments
2975   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2976                     ArrayRef<Value *> Args, unsigned Values,
2977                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2978 protected:
2979   virtual InvokeInst *clone_impl() const;
2980 public:
2981   static InvokeInst *Create(Value *Func,
2982                             BasicBlock *IfNormal, BasicBlock *IfException,
2983                             ArrayRef<Value *> Args, const Twine &NameStr = "",
2984                             Instruction *InsertBefore = 0) {
2985     unsigned Values = unsigned(Args.size()) + 3;
2986     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2987                                   Values, NameStr, InsertBefore);
2988   }
2989   static InvokeInst *Create(Value *Func,
2990                             BasicBlock *IfNormal, BasicBlock *IfException,
2991                             ArrayRef<Value *> Args, const Twine &NameStr,
2992                             BasicBlock *InsertAtEnd) {
2993     unsigned Values = unsigned(Args.size()) + 3;
2994     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2995                                   Values, NameStr, InsertAtEnd);
2996   }
2997
2998   /// Provide fast operand accessors
2999   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3000
3001   /// getNumArgOperands - Return the number of invoke arguments.
3002   ///
3003   unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3004
3005   /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3006   ///
3007   Value *getArgOperand(unsigned i) const { return getOperand(i); }
3008   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3009
3010   /// getCallingConv/setCallingConv - Get or set the calling convention of this
3011   /// function call.
3012   CallingConv::ID getCallingConv() const {
3013     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3014   }
3015   void setCallingConv(CallingConv::ID CC) {
3016     setInstructionSubclassData(static_cast<unsigned>(CC));
3017   }
3018
3019   /// getAttributes - Return the parameter attributes for this invoke.
3020   ///
3021   const AttrListPtr &getAttributes() const { return AttributeList; }
3022
3023   /// setAttributes - Set the parameter attributes for this invoke.
3024   ///
3025   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
3026
3027   /// addAttribute - adds the attribute to the list of attributes.
3028   void addAttribute(unsigned i, Attributes attr);
3029
3030   /// removeAttribute - removes the attribute from the list of attributes.
3031   void removeAttribute(unsigned i, Attributes attr);
3032
3033   /// @brief Determine whether this call has the NoAlias attribute.
3034   bool hasFnAttr(Attributes::AttrVal A) const;
3035
3036   /// @brief Determine whether the call or the callee has the given attributes.
3037   bool paramHasAttr(unsigned i, Attributes::AttrVal A) const;
3038
3039   /// @brief Extract the alignment for a call or parameter (0=unknown).
3040   unsigned getParamAlignment(unsigned i) const {
3041     return AttributeList.getParamAlignment(i);
3042   }
3043
3044   /// @brief Return true if the call should not be inlined.
3045   bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
3046   void setIsNoInline(bool Value = true) {
3047     if (Value) addAttribute(~0, Attribute::NoInline);
3048     else removeAttribute(~0, Attribute::NoInline);
3049   }
3050
3051   /// @brief Determine if the call does not access memory.
3052   bool doesNotAccessMemory() const {
3053     return hasFnAttr(Attributes::ReadNone);
3054   }
3055   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
3056     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
3057     else removeAttribute(~0, Attribute::ReadNone);
3058   }
3059
3060   /// @brief Determine if the call does not access or only reads memory.
3061   bool onlyReadsMemory() const {
3062     return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
3063   }
3064   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
3065     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
3066     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
3067   }
3068
3069   /// @brief Determine if the call cannot return.
3070   bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
3071   void setDoesNotReturn(bool DoesNotReturn = true) {
3072     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
3073     else removeAttribute(~0, Attribute::NoReturn);
3074   }
3075
3076   /// @brief Determine if the call cannot unwind.
3077   bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
3078   void setDoesNotThrow(bool DoesNotThrow = true) {
3079     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
3080     else removeAttribute(~0, Attribute::NoUnwind);
3081   }
3082
3083   /// @brief Determine if the call returns a structure through first
3084   /// pointer argument.
3085   bool hasStructRetAttr() const {
3086     // Be friendly and also check the callee.
3087     return paramHasAttr(1, Attributes::StructRet);
3088   }
3089
3090   /// @brief Determine if any call argument is an aggregate passed by value.
3091   bool hasByValArgument() const {
3092     for (unsigned I = 0, E = AttributeList.getNumAttrs(); I != E; ++I)
3093       if (AttributeList.getAttributesAtIndex(I).hasAttribute(Attributes::ByVal))
3094         return true;
3095     return false;
3096   }
3097
3098   /// getCalledFunction - Return the function called, or null if this is an
3099   /// indirect function invocation.
3100   ///
3101   Function *getCalledFunction() const {
3102     return dyn_cast<Function>(Op<-3>());
3103   }
3104
3105   /// getCalledValue - Get a pointer to the function that is invoked by this
3106   /// instruction
3107   const Value *getCalledValue() const { return Op<-3>(); }
3108         Value *getCalledValue()       { return Op<-3>(); }
3109
3110   /// setCalledFunction - Set the function called.
3111   void setCalledFunction(Value* Fn) {
3112     Op<-3>() = Fn;
3113   }
3114
3115   // get*Dest - Return the destination basic blocks...
3116   BasicBlock *getNormalDest() const {
3117     return cast<BasicBlock>(Op<-2>());
3118   }
3119   BasicBlock *getUnwindDest() const {
3120     return cast<BasicBlock>(Op<-1>());
3121   }
3122   void setNormalDest(BasicBlock *B) {
3123     Op<-2>() = reinterpret_cast<Value*>(B);
3124   }
3125   void setUnwindDest(BasicBlock *B) {
3126     Op<-1>() = reinterpret_cast<Value*>(B);
3127   }
3128
3129   /// getLandingPadInst - Get the landingpad instruction from the landing pad
3130   /// block (the unwind destination).
3131   LandingPadInst *getLandingPadInst() const;
3132
3133   BasicBlock *getSuccessor(unsigned i) const {
3134     assert(i < 2 && "Successor # out of range for invoke!");
3135     return i == 0 ? getNormalDest() : getUnwindDest();
3136   }
3137
3138   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3139     assert(idx < 2 && "Successor # out of range for invoke!");
3140     *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3141   }
3142
3143   unsigned getNumSuccessors() const { return 2; }
3144
3145   // Methods for support type inquiry through isa, cast, and dyn_cast:
3146   static inline bool classof(const InvokeInst *) { return true; }
3147   static inline bool classof(const Instruction *I) {
3148     return (I->getOpcode() == Instruction::Invoke);
3149   }
3150   static inline bool classof(const Value *V) {
3151     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3152   }
3153
3154 private:
3155   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3156   virtual unsigned getNumSuccessorsV() const;
3157   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3158
3159   // Shadow Instruction::setInstructionSubclassData with a private forwarding
3160   // method so that subclasses cannot accidentally use it.
3161   void setInstructionSubclassData(unsigned short D) {
3162     Instruction::setInstructionSubclassData(D);
3163   }
3164 };
3165
3166 template <>
3167 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3168 };
3169
3170 InvokeInst::InvokeInst(Value *Func,
3171                        BasicBlock *IfNormal, BasicBlock *IfException,
3172                        ArrayRef<Value *> Args, unsigned Values,
3173                        const Twine &NameStr, Instruction *InsertBefore)
3174   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3175                                       ->getElementType())->getReturnType(),
3176                    Instruction::Invoke,
3177                    OperandTraits<InvokeInst>::op_end(this) - Values,
3178                    Values, InsertBefore) {
3179   init(Func, IfNormal, IfException, Args, NameStr);
3180 }
3181 InvokeInst::InvokeInst(Value *Func,
3182                        BasicBlock *IfNormal, BasicBlock *IfException,
3183                        ArrayRef<Value *> Args, unsigned Values,
3184                        const Twine &NameStr, BasicBlock *InsertAtEnd)
3185   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3186                                       ->getElementType())->getReturnType(),
3187                    Instruction::Invoke,
3188                    OperandTraits<InvokeInst>::op_end(this) - Values,
3189                    Values, InsertAtEnd) {
3190   init(Func, IfNormal, IfException, Args, NameStr);
3191 }
3192
3193 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3194
3195 //===----------------------------------------------------------------------===//
3196 //                              ResumeInst Class
3197 //===----------------------------------------------------------------------===//
3198
3199 //===---------------------------------------------------------------------------
3200 /// ResumeInst - Resume the propagation of an exception.
3201 ///
3202 class ResumeInst : public TerminatorInst {
3203   ResumeInst(const ResumeInst &RI);
3204
3205   explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3206   ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3207 protected:
3208   virtual ResumeInst *clone_impl() const;
3209 public:
3210   static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3211     return new(1) ResumeInst(Exn, InsertBefore);
3212   }
3213   static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3214     return new(1) ResumeInst(Exn, InsertAtEnd);
3215   }
3216
3217   /// Provide fast operand accessors
3218   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3219
3220   /// Convenience accessor.
3221   Value *getValue() const { return Op<0>(); }
3222
3223   unsigned getNumSuccessors() const { return 0; }
3224
3225   // Methods for support type inquiry through isa, cast, and dyn_cast:
3226   static inline bool classof(const ResumeInst *) { return true; }
3227   static inline bool classof(const Instruction *I) {
3228     return I->getOpcode() == Instruction::Resume;
3229   }
3230   static inline bool classof(const Value *V) {
3231     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3232   }
3233 private:
3234   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3235   virtual unsigned getNumSuccessorsV() const;
3236   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3237 };
3238
3239 template <>
3240 struct OperandTraits<ResumeInst> :
3241     public FixedNumOperandTraits<ResumeInst, 1> {
3242 };
3243
3244 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3245
3246 //===----------------------------------------------------------------------===//
3247 //                           UnreachableInst Class
3248 //===----------------------------------------------------------------------===//
3249
3250 //===---------------------------------------------------------------------------
3251 /// UnreachableInst - This function has undefined behavior.  In particular, the
3252 /// presence of this instruction indicates some higher level knowledge that the
3253 /// end of the block cannot be reached.
3254 ///
3255 class UnreachableInst : public TerminatorInst {
3256   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3257 protected:
3258   virtual UnreachableInst *clone_impl() const;
3259
3260 public:
3261   // allocate space for exactly zero operands
3262   void *operator new(size_t s) {
3263     return User::operator new(s, 0);
3264   }
3265   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3266   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3267
3268   unsigned getNumSuccessors() const { return 0; }
3269
3270   // Methods for support type inquiry through isa, cast, and dyn_cast:
3271   static inline bool classof(const UnreachableInst *) { return true; }
3272   static inline bool classof(const Instruction *I) {
3273     return I->getOpcode() == Instruction::Unreachable;
3274   }
3275   static inline bool classof(const Value *V) {
3276     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3277   }
3278 private:
3279   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3280   virtual unsigned getNumSuccessorsV() const;
3281   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3282 };
3283
3284 //===----------------------------------------------------------------------===//
3285 //                                 TruncInst Class
3286 //===----------------------------------------------------------------------===//
3287
3288 /// @brief This class represents a truncation of integer types.
3289 class TruncInst : public CastInst {
3290 protected:
3291   /// @brief Clone an identical TruncInst
3292   virtual TruncInst *clone_impl() const;
3293
3294 public:
3295   /// @brief Constructor with insert-before-instruction semantics
3296   TruncInst(
3297     Value *S,                     ///< The value to be truncated
3298     Type *Ty,               ///< The (smaller) type to truncate to
3299     const Twine &NameStr = "",    ///< A name for the new instruction
3300     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3301   );
3302
3303   /// @brief Constructor with insert-at-end-of-block semantics
3304   TruncInst(
3305     Value *S,                     ///< The value to be truncated
3306     Type *Ty,               ///< The (smaller) type to truncate to
3307     const Twine &NameStr,         ///< A name for the new instruction
3308     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3309   );
3310
3311   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3312   static inline bool classof(const TruncInst *) { return true; }
3313   static inline bool classof(const Instruction *I) {
3314     return I->getOpcode() == Trunc;
3315   }
3316   static inline bool classof(const Value *V) {
3317     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3318   }
3319 };
3320
3321 //===----------------------------------------------------------------------===//
3322 //                                 ZExtInst Class
3323 //===----------------------------------------------------------------------===//
3324
3325 /// @brief This class represents zero extension of integer types.
3326 class ZExtInst : public CastInst {
3327 protected:
3328   /// @brief Clone an identical ZExtInst
3329   virtual ZExtInst *clone_impl() const;
3330
3331 public:
3332   /// @brief Constructor with insert-before-instruction semantics
3333   ZExtInst(
3334     Value *S,                     ///< The value to be zero extended
3335     Type *Ty,               ///< The type to zero extend to
3336     const Twine &NameStr = "",    ///< A name for the new instruction
3337     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3338   );
3339
3340   /// @brief Constructor with insert-at-end semantics.
3341   ZExtInst(
3342     Value *S,                     ///< The value to be zero extended
3343     Type *Ty,               ///< The type to zero extend to
3344     const Twine &NameStr,         ///< A name for the new instruction
3345     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3346   );
3347
3348   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3349   static inline bool classof(const ZExtInst *) { return true; }
3350   static inline bool classof(const Instruction *I) {
3351     return I->getOpcode() == ZExt;
3352   }
3353   static inline bool classof(const Value *V) {
3354     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3355   }
3356 };
3357
3358 //===----------------------------------------------------------------------===//
3359 //                                 SExtInst Class
3360 //===----------------------------------------------------------------------===//
3361
3362 /// @brief This class represents a sign extension of integer types.
3363 class SExtInst : public CastInst {
3364 protected:
3365   /// @brief Clone an identical SExtInst
3366   virtual SExtInst *clone_impl() const;
3367
3368 public:
3369   /// @brief Constructor with insert-before-instruction semantics
3370   SExtInst(
3371     Value *S,                     ///< The value to be sign extended
3372     Type *Ty,               ///< The type to sign extend to
3373     const Twine &NameStr = "",    ///< A name for the new instruction
3374     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3375   );
3376
3377   /// @brief Constructor with insert-at-end-of-block semantics
3378   SExtInst(
3379     Value *S,                     ///< The value to be sign extended
3380     Type *Ty,               ///< The type to sign extend to
3381     const Twine &NameStr,         ///< A name for the new instruction
3382     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3383   );
3384
3385   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3386   static inline bool classof(const SExtInst *) { return true; }
3387   static inline bool classof(const Instruction *I) {
3388     return I->getOpcode() == SExt;
3389   }
3390   static inline bool classof(const Value *V) {
3391     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3392   }
3393 };
3394
3395 //===----------------------------------------------------------------------===//
3396 //                                 FPTruncInst Class
3397 //===----------------------------------------------------------------------===//
3398
3399 /// @brief This class represents a truncation of floating point types.
3400 class FPTruncInst : public CastInst {
3401 protected:
3402   /// @brief Clone an identical FPTruncInst
3403   virtual FPTruncInst *clone_impl() const;
3404
3405 public:
3406   /// @brief Constructor with insert-before-instruction semantics
3407   FPTruncInst(
3408     Value *S,                     ///< The value to be truncated
3409     Type *Ty,               ///< The type to truncate to
3410     const Twine &NameStr = "",    ///< A name for the new instruction
3411     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3412   );
3413
3414   /// @brief Constructor with insert-before-instruction semantics
3415   FPTruncInst(
3416     Value *S,                     ///< The value to be truncated
3417     Type *Ty,               ///< The type to truncate to
3418     const Twine &NameStr,         ///< A name for the new instruction
3419     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3420   );
3421
3422   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3423   static inline bool classof(const FPTruncInst *) { return true; }
3424   static inline bool classof(const Instruction *I) {
3425     return I->getOpcode() == FPTrunc;
3426   }
3427   static inline bool classof(const Value *V) {
3428     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3429   }
3430 };
3431
3432 //===----------------------------------------------------------------------===//
3433 //                                 FPExtInst Class
3434 //===----------------------------------------------------------------------===//
3435
3436 /// @brief This class represents an extension of floating point types.
3437 class FPExtInst : public CastInst {
3438 protected:
3439   /// @brief Clone an identical FPExtInst
3440   virtual FPExtInst *clone_impl() const;
3441
3442 public:
3443   /// @brief Constructor with insert-before-instruction semantics
3444   FPExtInst(
3445     Value *S,                     ///< The value to be extended
3446     Type *Ty,               ///< The type to extend to
3447     const Twine &NameStr = "",    ///< A name for the new instruction
3448     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3449   );
3450
3451   /// @brief Constructor with insert-at-end-of-block semantics
3452   FPExtInst(
3453     Value *S,                     ///< The value to be extended
3454     Type *Ty,               ///< The type to extend to
3455     const Twine &NameStr,         ///< A name for the new instruction
3456     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3457   );
3458
3459   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3460   static inline bool classof(const FPExtInst *) { return true; }
3461   static inline bool classof(const Instruction *I) {
3462     return I->getOpcode() == FPExt;
3463   }
3464   static inline bool classof(const Value *V) {
3465     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3466   }
3467 };
3468
3469 //===----------------------------------------------------------------------===//
3470 //                                 UIToFPInst Class
3471 //===----------------------------------------------------------------------===//
3472
3473 /// @brief This class represents a cast unsigned integer to floating point.
3474 class UIToFPInst : public CastInst {
3475 protected:
3476   /// @brief Clone an identical UIToFPInst
3477   virtual UIToFPInst *clone_impl() const;
3478
3479 public:
3480   /// @brief Constructor with insert-before-instruction semantics
3481   UIToFPInst(
3482     Value *S,                     ///< The value to be converted
3483     Type *Ty,               ///< The type to convert to
3484     const Twine &NameStr = "",    ///< A name for the new instruction
3485     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3486   );
3487
3488   /// @brief Constructor with insert-at-end-of-block semantics
3489   UIToFPInst(
3490     Value *S,                     ///< The value to be converted
3491     Type *Ty,               ///< The type to convert to
3492     const Twine &NameStr,         ///< A name for the new instruction
3493     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3494   );
3495
3496   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3497   static inline bool classof(const UIToFPInst *) { return true; }
3498   static inline bool classof(const Instruction *I) {
3499     return I->getOpcode() == UIToFP;
3500   }
3501   static inline bool classof(const Value *V) {
3502     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3503   }
3504 };
3505
3506 //===----------------------------------------------------------------------===//
3507 //                                 SIToFPInst Class
3508 //===----------------------------------------------------------------------===//
3509
3510 /// @brief This class represents a cast from signed integer to floating point.
3511 class SIToFPInst : public CastInst {
3512 protected:
3513   /// @brief Clone an identical SIToFPInst
3514   virtual SIToFPInst *clone_impl() const;
3515
3516 public:
3517   /// @brief Constructor with insert-before-instruction semantics
3518   SIToFPInst(
3519     Value *S,                     ///< The value to be converted
3520     Type *Ty,               ///< The type to convert to
3521     const Twine &NameStr = "",    ///< A name for the new instruction
3522     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3523   );
3524
3525   /// @brief Constructor with insert-at-end-of-block semantics
3526   SIToFPInst(
3527     Value *S,                     ///< The value to be converted
3528     Type *Ty,               ///< The type to convert to
3529     const Twine &NameStr,         ///< A name for the new instruction
3530     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3531   );
3532
3533   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3534   static inline bool classof(const SIToFPInst *) { return true; }
3535   static inline bool classof(const Instruction *I) {
3536     return I->getOpcode() == SIToFP;
3537   }
3538   static inline bool classof(const Value *V) {
3539     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3540   }
3541 };
3542
3543 //===----------------------------------------------------------------------===//
3544 //                                 FPToUIInst Class
3545 //===----------------------------------------------------------------------===//
3546
3547 /// @brief This class represents a cast from floating point to unsigned integer
3548 class FPToUIInst  : public CastInst {
3549 protected:
3550   /// @brief Clone an identical FPToUIInst
3551   virtual FPToUIInst *clone_impl() const;
3552
3553 public:
3554   /// @brief Constructor with insert-before-instruction semantics
3555   FPToUIInst(
3556     Value *S,                     ///< The value to be converted
3557     Type *Ty,               ///< The type to convert to
3558     const Twine &NameStr = "",    ///< A name for the new instruction
3559     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3560   );
3561
3562   /// @brief Constructor with insert-at-end-of-block semantics
3563   FPToUIInst(
3564     Value *S,                     ///< The value to be converted
3565     Type *Ty,               ///< The type to convert to
3566     const Twine &NameStr,         ///< A name for the new instruction
3567     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
3568   );
3569
3570   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3571   static inline bool classof(const FPToUIInst *) { return true; }
3572   static inline bool classof(const Instruction *I) {
3573     return I->getOpcode() == FPToUI;
3574   }
3575   static inline bool classof(const Value *V) {
3576     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3577   }
3578 };
3579
3580 //===----------------------------------------------------------------------===//
3581 //                                 FPToSIInst Class
3582 //===----------------------------------------------------------------------===//
3583
3584 /// @brief This class represents a cast from floating point to signed integer.
3585 class FPToSIInst  : public CastInst {
3586 protected:
3587   /// @brief Clone an identical FPToSIInst
3588   virtual FPToSIInst *clone_impl() const;
3589
3590 public:
3591   /// @brief Constructor with insert-before-instruction semantics
3592   FPToSIInst(
3593     Value *S,                     ///< The value to be converted
3594     Type *Ty,               ///< The type to convert to
3595     const Twine &NameStr = "",    ///< A name for the new instruction
3596     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3597   );
3598
3599   /// @brief Constructor with insert-at-end-of-block semantics
3600   FPToSIInst(
3601     Value *S,                     ///< The value to be converted
3602     Type *Ty,               ///< The type to convert to
3603     const Twine &NameStr,         ///< A name for the new instruction
3604     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3605   );
3606
3607   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3608   static inline bool classof(const FPToSIInst *) { return true; }
3609   static inline bool classof(const Instruction *I) {
3610     return I->getOpcode() == FPToSI;
3611   }
3612   static inline bool classof(const Value *V) {
3613     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3614   }
3615 };
3616
3617 //===----------------------------------------------------------------------===//
3618 //                                 IntToPtrInst Class
3619 //===----------------------------------------------------------------------===//
3620
3621 /// @brief This class represents a cast from an integer to a pointer.
3622 class IntToPtrInst : public CastInst {
3623 public:
3624   /// @brief Constructor with insert-before-instruction semantics
3625   IntToPtrInst(
3626     Value *S,                     ///< The value to be converted
3627     Type *Ty,               ///< The type to convert to
3628     const Twine &NameStr = "",    ///< A name for the new instruction
3629     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3630   );
3631
3632   /// @brief Constructor with insert-at-end-of-block semantics
3633   IntToPtrInst(
3634     Value *S,                     ///< The value to be converted
3635     Type *Ty,               ///< The type to convert to
3636     const Twine &NameStr,         ///< A name for the new instruction
3637     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3638   );
3639
3640   /// @brief Clone an identical IntToPtrInst
3641   virtual IntToPtrInst *clone_impl() const;
3642
3643   /// @brief return the address space of the pointer.
3644   unsigned getAddressSpace() const {
3645     return cast<PointerType>(getType())->getAddressSpace();
3646   }
3647
3648   // Methods for support type inquiry through isa, cast, and dyn_cast:
3649   static inline bool classof(const IntToPtrInst *) { return true; }
3650   static inline bool classof(const Instruction *I) {
3651     return I->getOpcode() == IntToPtr;
3652   }
3653   static inline bool classof(const Value *V) {
3654     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3655   }
3656 };
3657
3658 //===----------------------------------------------------------------------===//
3659 //                                 PtrToIntInst Class
3660 //===----------------------------------------------------------------------===//
3661
3662 /// @brief This class represents a cast from a pointer to an integer
3663 class PtrToIntInst : public CastInst {
3664 protected:
3665   /// @brief Clone an identical PtrToIntInst
3666   virtual PtrToIntInst *clone_impl() const;
3667
3668 public:
3669   /// @brief Constructor with insert-before-instruction semantics
3670   PtrToIntInst(
3671     Value *S,                     ///< The value to be converted
3672     Type *Ty,               ///< The type to convert to
3673     const Twine &NameStr = "",    ///< A name for the new instruction
3674     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3675   );
3676
3677   /// @brief Constructor with insert-at-end-of-block semantics
3678   PtrToIntInst(
3679     Value *S,                     ///< The value to be converted
3680     Type *Ty,               ///< The type to convert to
3681     const Twine &NameStr,         ///< A name for the new instruction
3682     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3683   );
3684
3685   /// @brief return the address space of the pointer.
3686   unsigned getPointerAddressSpace() const {
3687     return cast<PointerType>(getOperand(0)->getType())->getAddressSpace();
3688   }
3689
3690   // Methods for support type inquiry through isa, cast, and dyn_cast:
3691   static inline bool classof(const PtrToIntInst *) { return true; }
3692   static inline bool classof(const Instruction *I) {
3693     return I->getOpcode() == PtrToInt;
3694   }
3695   static inline bool classof(const Value *V) {
3696     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3697   }
3698 };
3699
3700 //===----------------------------------------------------------------------===//
3701 //                             BitCastInst Class
3702 //===----------------------------------------------------------------------===//
3703
3704 /// @brief This class represents a no-op cast from one type to another.
3705 class BitCastInst : public CastInst {
3706 protected:
3707   /// @brief Clone an identical BitCastInst
3708   virtual BitCastInst *clone_impl() const;
3709
3710 public:
3711   /// @brief Constructor with insert-before-instruction semantics
3712   BitCastInst(
3713     Value *S,                     ///< The value to be casted
3714     Type *Ty,               ///< The type to casted to
3715     const Twine &NameStr = "",    ///< A name for the new instruction
3716     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3717   );
3718
3719   /// @brief Constructor with insert-at-end-of-block semantics
3720   BitCastInst(
3721     Value *S,                     ///< The value to be casted
3722     Type *Ty,               ///< The type to casted to
3723     const Twine &NameStr,         ///< A name for the new instruction
3724     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3725   );
3726
3727   // Methods for support type inquiry through isa, cast, and dyn_cast:
3728   static inline bool classof(const BitCastInst *) { return true; }
3729   static inline bool classof(const Instruction *I) {
3730     return I->getOpcode() == BitCast;
3731   }
3732   static inline bool classof(const Value *V) {
3733     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3734   }
3735 };
3736
3737 } // End llvm namespace
3738
3739 #endif