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