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