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