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