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