remove some @deprecated markers: LLVM APIs aren't deprecated, they are removed when...
[oota-llvm.git] / include / llvm / IR / 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_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
18
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/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 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 sequential type.
766   SequentialType *getType() const {
767     return cast<SequentialType>(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()->getScalarType()->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, Attribute::AttrKind attr);
1276
1277   /// removeAttribute - removes the attribute from the list of attributes.
1278   void removeAttribute(unsigned i, Attribute attr);
1279
1280   /// \brief Determine whether this call has the given attribute.
1281   bool hasFnAttr(Attribute::AttrKind A) const;
1282
1283   /// \brief Determine whether the call or the callee has the given attributes.
1284   bool paramHasAttr(unsigned i, Attribute::AttrKind 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(Attribute::NoInline); }
1293   void setIsNoInline() {
1294     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1295   }
1296
1297   /// \brief Return true if the call can return twice
1298   bool canReturnTwice() const {
1299     return hasFnAttr(Attribute::ReturnsTwice);
1300   }
1301   void setCanReturnTwice() {
1302     addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1303   }
1304
1305   /// \brief Determine if the call does not access memory.
1306   bool doesNotAccessMemory() const {
1307     return hasFnAttr(Attribute::ReadNone);
1308   }
1309   void setDoesNotAccessMemory() {
1310     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1311   }
1312
1313   /// \brief Determine if the call does not access or only reads memory.
1314   bool onlyReadsMemory() const {
1315     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1316   }
1317   void setOnlyReadsMemory() {
1318     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1319   }
1320
1321   /// \brief Determine if the call cannot return.
1322   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1323   void setDoesNotReturn() {
1324     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1325   }
1326
1327   /// \brief Determine if the call cannot unwind.
1328   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1329   void setDoesNotThrow() {
1330     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1331   }
1332
1333   /// \brief Determine if the call cannot be duplicated.
1334   bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1335   void setCannotDuplicate() {
1336     addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
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, Attribute::StructRet);
1344   }
1345
1346   /// \brief Determine if any call argument is an aggregate passed by value.
1347   bool hasByValArgument() const {
1348     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1349   }
1350
1351   /// getCalledFunction - Return the function called, or null if this is an
1352   /// indirect function invocation.
1353   ///
1354   Function *getCalledFunction() const {
1355     return dyn_cast<Function>(Op<-1>());
1356   }
1357
1358   /// getCalledValue - Get a pointer to the function that is invoked by this
1359   /// instruction.
1360   const Value *getCalledValue() const { return Op<-1>(); }
1361         Value *getCalledValue()       { return Op<-1>(); }
1362
1363   /// setCalledFunction - Set the function called.
1364   void setCalledFunction(Value* Fn) {
1365     Op<-1>() = Fn;
1366   }
1367
1368   /// isInlineAsm - Check if this call is an inline asm statement.
1369   bool isInlineAsm() const {
1370     return isa<InlineAsm>(Op<-1>());
1371   }
1372
1373   // Methods for support type inquiry through isa, cast, and dyn_cast:
1374   static inline bool classof(const Instruction *I) {
1375     return I->getOpcode() == Instruction::Call;
1376   }
1377   static inline bool classof(const Value *V) {
1378     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1379   }
1380 private:
1381   // Shadow Instruction::setInstructionSubclassData with a private forwarding
1382   // method so that subclasses cannot accidentally use it.
1383   void setInstructionSubclassData(unsigned short D) {
1384     Instruction::setInstructionSubclassData(D);
1385   }
1386 };
1387
1388 template <>
1389 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1390 };
1391
1392 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1393                    const Twine &NameStr, BasicBlock *InsertAtEnd)
1394   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1395                                    ->getElementType())->getReturnType(),
1396                 Instruction::Call,
1397                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1398                 unsigned(Args.size() + 1), InsertAtEnd) {
1399   init(Func, Args, NameStr);
1400 }
1401
1402 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1403                    const Twine &NameStr, Instruction *InsertBefore)
1404   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1405                                    ->getElementType())->getReturnType(),
1406                 Instruction::Call,
1407                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1408                 unsigned(Args.size() + 1), InsertBefore) {
1409   init(Func, Args, NameStr);
1410 }
1411
1412
1413 // Note: if you get compile errors about private methods then
1414 //       please update your code to use the high-level operand
1415 //       interfaces. See line 943 above.
1416 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1417
1418 //===----------------------------------------------------------------------===//
1419 //                               SelectInst Class
1420 //===----------------------------------------------------------------------===//
1421
1422 /// SelectInst - This class represents the LLVM 'select' instruction.
1423 ///
1424 class SelectInst : public Instruction {
1425   void init(Value *C, Value *S1, Value *S2) {
1426     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1427     Op<0>() = C;
1428     Op<1>() = S1;
1429     Op<2>() = S2;
1430   }
1431
1432   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1433              Instruction *InsertBefore)
1434     : Instruction(S1->getType(), Instruction::Select,
1435                   &Op<0>(), 3, InsertBefore) {
1436     init(C, S1, S2);
1437     setName(NameStr);
1438   }
1439   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1440              BasicBlock *InsertAtEnd)
1441     : Instruction(S1->getType(), Instruction::Select,
1442                   &Op<0>(), 3, InsertAtEnd) {
1443     init(C, S1, S2);
1444     setName(NameStr);
1445   }
1446 protected:
1447   virtual SelectInst *clone_impl() const;
1448 public:
1449   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1450                             const Twine &NameStr = "",
1451                             Instruction *InsertBefore = 0) {
1452     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1453   }
1454   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1455                             const Twine &NameStr,
1456                             BasicBlock *InsertAtEnd) {
1457     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1458   }
1459
1460   const Value *getCondition() const { return Op<0>(); }
1461   const Value *getTrueValue() const { return Op<1>(); }
1462   const Value *getFalseValue() const { return Op<2>(); }
1463   Value *getCondition() { return Op<0>(); }
1464   Value *getTrueValue() { return Op<1>(); }
1465   Value *getFalseValue() { return Op<2>(); }
1466
1467   /// areInvalidOperands - Return a string if the specified operands are invalid
1468   /// for a select operation, otherwise return null.
1469   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1470
1471   /// Transparently provide more efficient getOperand methods.
1472   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1473
1474   OtherOps getOpcode() const {
1475     return static_cast<OtherOps>(Instruction::getOpcode());
1476   }
1477
1478   // Methods for support type inquiry through isa, cast, and dyn_cast:
1479   static inline bool classof(const Instruction *I) {
1480     return I->getOpcode() == Instruction::Select;
1481   }
1482   static inline bool classof(const Value *V) {
1483     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1484   }
1485 };
1486
1487 template <>
1488 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1489 };
1490
1491 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1492
1493 //===----------------------------------------------------------------------===//
1494 //                                VAArgInst Class
1495 //===----------------------------------------------------------------------===//
1496
1497 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1498 /// an argument of the specified type given a va_list and increments that list
1499 ///
1500 class VAArgInst : public UnaryInstruction {
1501 protected:
1502   virtual VAArgInst *clone_impl() const;
1503
1504 public:
1505   VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1506              Instruction *InsertBefore = 0)
1507     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1508     setName(NameStr);
1509   }
1510   VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1511             BasicBlock *InsertAtEnd)
1512     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1513     setName(NameStr);
1514   }
1515
1516   Value *getPointerOperand() { return getOperand(0); }
1517   const Value *getPointerOperand() const { return getOperand(0); }
1518   static unsigned getPointerOperandIndex() { return 0U; }
1519
1520   // Methods for support type inquiry through isa, cast, and dyn_cast:
1521   static inline bool classof(const Instruction *I) {
1522     return I->getOpcode() == VAArg;
1523   }
1524   static inline bool classof(const Value *V) {
1525     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1526   }
1527 };
1528
1529 //===----------------------------------------------------------------------===//
1530 //                                ExtractElementInst Class
1531 //===----------------------------------------------------------------------===//
1532
1533 /// ExtractElementInst - This instruction extracts a single (scalar)
1534 /// element from a VectorType value
1535 ///
1536 class ExtractElementInst : public Instruction {
1537   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1538                      Instruction *InsertBefore = 0);
1539   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1540                      BasicBlock *InsertAtEnd);
1541 protected:
1542   virtual ExtractElementInst *clone_impl() const;
1543
1544 public:
1545   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1546                                    const Twine &NameStr = "",
1547                                    Instruction *InsertBefore = 0) {
1548     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1549   }
1550   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1551                                    const Twine &NameStr,
1552                                    BasicBlock *InsertAtEnd) {
1553     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1554   }
1555
1556   /// isValidOperands - Return true if an extractelement instruction can be
1557   /// formed with the specified operands.
1558   static bool isValidOperands(const Value *Vec, const Value *Idx);
1559
1560   Value *getVectorOperand() { return Op<0>(); }
1561   Value *getIndexOperand() { return Op<1>(); }
1562   const Value *getVectorOperand() const { return Op<0>(); }
1563   const Value *getIndexOperand() const { return Op<1>(); }
1564
1565   VectorType *getVectorOperandType() const {
1566     return cast<VectorType>(getVectorOperand()->getType());
1567   }
1568
1569
1570   /// Transparently provide more efficient getOperand methods.
1571   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1572
1573   // Methods for support type inquiry through isa, cast, and dyn_cast:
1574   static inline bool classof(const Instruction *I) {
1575     return I->getOpcode() == Instruction::ExtractElement;
1576   }
1577   static inline bool classof(const Value *V) {
1578     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1579   }
1580 };
1581
1582 template <>
1583 struct OperandTraits<ExtractElementInst> :
1584   public FixedNumOperandTraits<ExtractElementInst, 2> {
1585 };
1586
1587 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1588
1589 //===----------------------------------------------------------------------===//
1590 //                                InsertElementInst Class
1591 //===----------------------------------------------------------------------===//
1592
1593 /// InsertElementInst - This instruction inserts a single (scalar)
1594 /// element into a VectorType value
1595 ///
1596 class InsertElementInst : public Instruction {
1597   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1598                     const Twine &NameStr = "",
1599                     Instruction *InsertBefore = 0);
1600   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1601                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1602 protected:
1603   virtual InsertElementInst *clone_impl() const;
1604
1605 public:
1606   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1607                                    const Twine &NameStr = "",
1608                                    Instruction *InsertBefore = 0) {
1609     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1610   }
1611   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1612                                    const Twine &NameStr,
1613                                    BasicBlock *InsertAtEnd) {
1614     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1615   }
1616
1617   /// isValidOperands - Return true if an insertelement instruction can be
1618   /// formed with the specified operands.
1619   static bool isValidOperands(const Value *Vec, const Value *NewElt,
1620                               const Value *Idx);
1621
1622   /// getType - Overload to return most specific vector type.
1623   ///
1624   VectorType *getType() const {
1625     return cast<VectorType>(Instruction::getType());
1626   }
1627
1628   /// Transparently provide more efficient getOperand methods.
1629   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1630
1631   // Methods for support type inquiry through isa, cast, and dyn_cast:
1632   static inline bool classof(const Instruction *I) {
1633     return I->getOpcode() == Instruction::InsertElement;
1634   }
1635   static inline bool classof(const Value *V) {
1636     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1637   }
1638 };
1639
1640 template <>
1641 struct OperandTraits<InsertElementInst> :
1642   public FixedNumOperandTraits<InsertElementInst, 3> {
1643 };
1644
1645 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1646
1647 //===----------------------------------------------------------------------===//
1648 //                           ShuffleVectorInst Class
1649 //===----------------------------------------------------------------------===//
1650
1651 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1652 /// input vectors.
1653 ///
1654 class ShuffleVectorInst : public Instruction {
1655 protected:
1656   virtual ShuffleVectorInst *clone_impl() const;
1657
1658 public:
1659   // allocate space for exactly three operands
1660   void *operator new(size_t s) {
1661     return User::operator new(s, 3);
1662   }
1663   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1664                     const Twine &NameStr = "",
1665                     Instruction *InsertBefor = 0);
1666   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1667                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1668
1669   /// isValidOperands - Return true if a shufflevector instruction can be
1670   /// formed with the specified operands.
1671   static bool isValidOperands(const Value *V1, const Value *V2,
1672                               const Value *Mask);
1673
1674   /// getType - Overload to return most specific vector type.
1675   ///
1676   VectorType *getType() const {
1677     return cast<VectorType>(Instruction::getType());
1678   }
1679
1680   /// Transparently provide more efficient getOperand methods.
1681   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1682
1683   Constant *getMask() const {
1684     return cast<Constant>(getOperand(2));
1685   }
1686
1687   /// getMaskValue - Return the index from the shuffle mask for the specified
1688   /// output result.  This is either -1 if the element is undef or a number less
1689   /// than 2*numelements.
1690   static int getMaskValue(Constant *Mask, unsigned i);
1691
1692   int getMaskValue(unsigned i) const {
1693     return getMaskValue(getMask(), i);
1694   }
1695
1696   /// getShuffleMask - Return the full mask for this instruction, where each
1697   /// element is the element number and undef's are returned as -1.
1698   static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1699
1700   void getShuffleMask(SmallVectorImpl<int> &Result) const {
1701     return getShuffleMask(getMask(), Result);
1702   }
1703
1704   SmallVector<int, 16> getShuffleMask() const {
1705     SmallVector<int, 16> Mask;
1706     getShuffleMask(Mask);
1707     return Mask;
1708   }
1709
1710
1711   // Methods for support type inquiry through isa, cast, and dyn_cast:
1712   static inline bool classof(const Instruction *I) {
1713     return I->getOpcode() == Instruction::ShuffleVector;
1714   }
1715   static inline bool classof(const Value *V) {
1716     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1717   }
1718 };
1719
1720 template <>
1721 struct OperandTraits<ShuffleVectorInst> :
1722   public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1723 };
1724
1725 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1726
1727 //===----------------------------------------------------------------------===//
1728 //                                ExtractValueInst Class
1729 //===----------------------------------------------------------------------===//
1730
1731 /// ExtractValueInst - This instruction extracts a struct member or array
1732 /// element value from an aggregate value.
1733 ///
1734 class ExtractValueInst : public UnaryInstruction {
1735   SmallVector<unsigned, 4> Indices;
1736
1737   ExtractValueInst(const ExtractValueInst &EVI);
1738   void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1739
1740   /// Constructors - Create a extractvalue instruction with a base aggregate
1741   /// value and a list of indices.  The first ctor can optionally insert before
1742   /// an existing instruction, the second appends the new instruction to the
1743   /// specified BasicBlock.
1744   inline ExtractValueInst(Value *Agg,
1745                           ArrayRef<unsigned> Idxs,
1746                           const Twine &NameStr,
1747                           Instruction *InsertBefore);
1748   inline ExtractValueInst(Value *Agg,
1749                           ArrayRef<unsigned> Idxs,
1750                           const Twine &NameStr, BasicBlock *InsertAtEnd);
1751
1752   // allocate space for exactly one operand
1753   void *operator new(size_t s) {
1754     return User::operator new(s, 1);
1755   }
1756 protected:
1757   virtual ExtractValueInst *clone_impl() const;
1758
1759 public:
1760   static ExtractValueInst *Create(Value *Agg,
1761                                   ArrayRef<unsigned> Idxs,
1762                                   const Twine &NameStr = "",
1763                                   Instruction *InsertBefore = 0) {
1764     return new
1765       ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1766   }
1767   static ExtractValueInst *Create(Value *Agg,
1768                                   ArrayRef<unsigned> Idxs,
1769                                   const Twine &NameStr,
1770                                   BasicBlock *InsertAtEnd) {
1771     return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1772   }
1773
1774   /// getIndexedType - Returns the type of the element that would be extracted
1775   /// with an extractvalue instruction with the specified parameters.
1776   ///
1777   /// Null is returned if the indices are invalid for the specified type.
1778   static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1779
1780   typedef const unsigned* idx_iterator;
1781   inline idx_iterator idx_begin() const { return Indices.begin(); }
1782   inline idx_iterator idx_end()   const { return Indices.end(); }
1783
1784   Value *getAggregateOperand() {
1785     return getOperand(0);
1786   }
1787   const Value *getAggregateOperand() const {
1788     return getOperand(0);
1789   }
1790   static unsigned getAggregateOperandIndex() {
1791     return 0U;                      // get index for modifying correct operand
1792   }
1793
1794   ArrayRef<unsigned> getIndices() const {
1795     return Indices;
1796   }
1797
1798   unsigned getNumIndices() const {
1799     return (unsigned)Indices.size();
1800   }
1801
1802   bool hasIndices() const {
1803     return true;
1804   }
1805
1806   // Methods for support type inquiry through isa, cast, and dyn_cast:
1807   static inline bool classof(const Instruction *I) {
1808     return I->getOpcode() == Instruction::ExtractValue;
1809   }
1810   static inline bool classof(const Value *V) {
1811     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1812   }
1813 };
1814
1815 ExtractValueInst::ExtractValueInst(Value *Agg,
1816                                    ArrayRef<unsigned> Idxs,
1817                                    const Twine &NameStr,
1818                                    Instruction *InsertBefore)
1819   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1820                      ExtractValue, Agg, InsertBefore) {
1821   init(Idxs, NameStr);
1822 }
1823 ExtractValueInst::ExtractValueInst(Value *Agg,
1824                                    ArrayRef<unsigned> Idxs,
1825                                    const Twine &NameStr,
1826                                    BasicBlock *InsertAtEnd)
1827   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1828                      ExtractValue, Agg, InsertAtEnd) {
1829   init(Idxs, NameStr);
1830 }
1831
1832
1833 //===----------------------------------------------------------------------===//
1834 //                                InsertValueInst Class
1835 //===----------------------------------------------------------------------===//
1836
1837 /// InsertValueInst - This instruction inserts a struct field of array element
1838 /// value into an aggregate value.
1839 ///
1840 class InsertValueInst : public Instruction {
1841   SmallVector<unsigned, 4> Indices;
1842
1843   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1844   InsertValueInst(const InsertValueInst &IVI);
1845   void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1846             const Twine &NameStr);
1847
1848   /// Constructors - Create a insertvalue instruction with a base aggregate
1849   /// value, a value to insert, and a list of indices.  The first ctor can
1850   /// optionally insert before an existing instruction, the second appends
1851   /// the new instruction to the specified BasicBlock.
1852   inline InsertValueInst(Value *Agg, Value *Val,
1853                          ArrayRef<unsigned> Idxs,
1854                          const Twine &NameStr,
1855                          Instruction *InsertBefore);
1856   inline InsertValueInst(Value *Agg, Value *Val,
1857                          ArrayRef<unsigned> Idxs,
1858                          const Twine &NameStr, BasicBlock *InsertAtEnd);
1859
1860   /// Constructors - These two constructors are convenience methods because one
1861   /// and two index insertvalue instructions are so common.
1862   InsertValueInst(Value *Agg, Value *Val,
1863                   unsigned Idx, const Twine &NameStr = "",
1864                   Instruction *InsertBefore = 0);
1865   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1866                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1867 protected:
1868   virtual InsertValueInst *clone_impl() const;
1869 public:
1870   // allocate space for exactly two operands
1871   void *operator new(size_t s) {
1872     return User::operator new(s, 2);
1873   }
1874
1875   static InsertValueInst *Create(Value *Agg, Value *Val,
1876                                  ArrayRef<unsigned> Idxs,
1877                                  const Twine &NameStr = "",
1878                                  Instruction *InsertBefore = 0) {
1879     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1880   }
1881   static InsertValueInst *Create(Value *Agg, Value *Val,
1882                                  ArrayRef<unsigned> Idxs,
1883                                  const Twine &NameStr,
1884                                  BasicBlock *InsertAtEnd) {
1885     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1886   }
1887
1888   /// Transparently provide more efficient getOperand methods.
1889   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1890
1891   typedef const unsigned* idx_iterator;
1892   inline idx_iterator idx_begin() const { return Indices.begin(); }
1893   inline idx_iterator idx_end()   const { return Indices.end(); }
1894
1895   Value *getAggregateOperand() {
1896     return getOperand(0);
1897   }
1898   const Value *getAggregateOperand() const {
1899     return getOperand(0);
1900   }
1901   static unsigned getAggregateOperandIndex() {
1902     return 0U;                      // get index for modifying correct operand
1903   }
1904
1905   Value *getInsertedValueOperand() {
1906     return getOperand(1);
1907   }
1908   const Value *getInsertedValueOperand() const {
1909     return getOperand(1);
1910   }
1911   static unsigned getInsertedValueOperandIndex() {
1912     return 1U;                      // get index for modifying correct operand
1913   }
1914
1915   ArrayRef<unsigned> getIndices() const {
1916     return Indices;
1917   }
1918
1919   unsigned getNumIndices() const {
1920     return (unsigned)Indices.size();
1921   }
1922
1923   bool hasIndices() const {
1924     return true;
1925   }
1926
1927   // Methods for support type inquiry through isa, cast, and dyn_cast:
1928   static inline bool classof(const Instruction *I) {
1929     return I->getOpcode() == Instruction::InsertValue;
1930   }
1931   static inline bool classof(const Value *V) {
1932     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1933   }
1934 };
1935
1936 template <>
1937 struct OperandTraits<InsertValueInst> :
1938   public FixedNumOperandTraits<InsertValueInst, 2> {
1939 };
1940
1941 InsertValueInst::InsertValueInst(Value *Agg,
1942                                  Value *Val,
1943                                  ArrayRef<unsigned> Idxs,
1944                                  const Twine &NameStr,
1945                                  Instruction *InsertBefore)
1946   : Instruction(Agg->getType(), InsertValue,
1947                 OperandTraits<InsertValueInst>::op_begin(this),
1948                 2, InsertBefore) {
1949   init(Agg, Val, Idxs, NameStr);
1950 }
1951 InsertValueInst::InsertValueInst(Value *Agg,
1952                                  Value *Val,
1953                                  ArrayRef<unsigned> Idxs,
1954                                  const Twine &NameStr,
1955                                  BasicBlock *InsertAtEnd)
1956   : Instruction(Agg->getType(), InsertValue,
1957                 OperandTraits<InsertValueInst>::op_begin(this),
1958                 2, InsertAtEnd) {
1959   init(Agg, Val, Idxs, NameStr);
1960 }
1961
1962 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1963
1964 //===----------------------------------------------------------------------===//
1965 //                               PHINode Class
1966 //===----------------------------------------------------------------------===//
1967
1968 // PHINode - The PHINode class is used to represent the magical mystical PHI
1969 // node, that can not exist in nature, but can be synthesized in a computer
1970 // scientist's overactive imagination.
1971 //
1972 class PHINode : public Instruction {
1973   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1974   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1975   /// the number actually in use.
1976   unsigned ReservedSpace;
1977   PHINode(const PHINode &PN);
1978   // allocate space for exactly zero operands
1979   void *operator new(size_t s) {
1980     return User::operator new(s, 0);
1981   }
1982   explicit PHINode(Type *Ty, unsigned NumReservedValues,
1983                    const Twine &NameStr = "", Instruction *InsertBefore = 0)
1984     : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1985       ReservedSpace(NumReservedValues) {
1986     setName(NameStr);
1987     OperandList = allocHungoffUses(ReservedSpace);
1988   }
1989
1990   PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1991           BasicBlock *InsertAtEnd)
1992     : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1993       ReservedSpace(NumReservedValues) {
1994     setName(NameStr);
1995     OperandList = allocHungoffUses(ReservedSpace);
1996   }
1997 protected:
1998   // allocHungoffUses - this is more complicated than the generic
1999   // User::allocHungoffUses, because we have to allocate Uses for the incoming
2000   // values and pointers to the incoming blocks, all in one allocation.
2001   Use *allocHungoffUses(unsigned) const;
2002
2003   virtual PHINode *clone_impl() const;
2004 public:
2005   /// Constructors - NumReservedValues is a hint for the number of incoming
2006   /// edges that this phi node will have (use 0 if you really have no idea).
2007   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2008                          const Twine &NameStr = "",
2009                          Instruction *InsertBefore = 0) {
2010     return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2011   }
2012   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2013                          const Twine &NameStr, BasicBlock *InsertAtEnd) {
2014     return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2015   }
2016   ~PHINode();
2017
2018   /// Provide fast operand accessors
2019   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2020
2021   // Block iterator interface. This provides access to the list of incoming
2022   // basic blocks, which parallels the list of incoming values.
2023
2024   typedef BasicBlock **block_iterator;
2025   typedef BasicBlock * const *const_block_iterator;
2026
2027   block_iterator block_begin() {
2028     Use::UserRef *ref =
2029       reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2030     return reinterpret_cast<block_iterator>(ref + 1);
2031   }
2032
2033   const_block_iterator block_begin() const {
2034     const Use::UserRef *ref =
2035       reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2036     return reinterpret_cast<const_block_iterator>(ref + 1);
2037   }
2038
2039   block_iterator block_end() {
2040     return block_begin() + getNumOperands();
2041   }
2042
2043   const_block_iterator block_end() const {
2044     return block_begin() + getNumOperands();
2045   }
2046
2047   /// getNumIncomingValues - Return the number of incoming edges
2048   ///
2049   unsigned getNumIncomingValues() const { return getNumOperands(); }
2050
2051   /// getIncomingValue - Return incoming value number x
2052   ///
2053   Value *getIncomingValue(unsigned i) const {
2054     return getOperand(i);
2055   }
2056   void setIncomingValue(unsigned i, Value *V) {
2057     setOperand(i, V);
2058   }
2059   static unsigned getOperandNumForIncomingValue(unsigned i) {
2060     return i;
2061   }
2062   static unsigned getIncomingValueNumForOperand(unsigned i) {
2063     return i;
2064   }
2065
2066   /// getIncomingBlock - Return incoming basic block number @p i.
2067   ///
2068   BasicBlock *getIncomingBlock(unsigned i) const {
2069     return block_begin()[i];
2070   }
2071
2072   /// getIncomingBlock - Return incoming basic block corresponding
2073   /// to an operand of the PHI.
2074   ///
2075   BasicBlock *getIncomingBlock(const Use &U) const {
2076     assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2077     return getIncomingBlock(unsigned(&U - op_begin()));
2078   }
2079
2080   /// getIncomingBlock - Return incoming basic block corresponding
2081   /// to value use iterator.
2082   ///
2083   template <typename U>
2084   BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
2085     return getIncomingBlock(I.getUse());
2086   }
2087
2088   void setIncomingBlock(unsigned i, BasicBlock *BB) {
2089     block_begin()[i] = BB;
2090   }
2091
2092   /// addIncoming - Add an incoming value to the end of the PHI list
2093   ///
2094   void addIncoming(Value *V, BasicBlock *BB) {
2095     assert(V && "PHI node got a null value!");
2096     assert(BB && "PHI node got a null basic block!");
2097     assert(getType() == V->getType() &&
2098            "All operands to PHI node must be the same type as the PHI node!");
2099     if (NumOperands == ReservedSpace)
2100       growOperands();  // Get more space!
2101     // Initialize some new operands.
2102     ++NumOperands;
2103     setIncomingValue(NumOperands - 1, V);
2104     setIncomingBlock(NumOperands - 1, BB);
2105   }
2106
2107   /// removeIncomingValue - Remove an incoming value.  This is useful if a
2108   /// predecessor basic block is deleted.  The value removed is returned.
2109   ///
2110   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2111   /// is true), the PHI node is destroyed and any uses of it are replaced with
2112   /// dummy values.  The only time there should be zero incoming values to a PHI
2113   /// node is when the block is dead, so this strategy is sound.
2114   ///
2115   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2116
2117   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2118     int Idx = getBasicBlockIndex(BB);
2119     assert(Idx >= 0 && "Invalid basic block argument to remove!");
2120     return removeIncomingValue(Idx, DeletePHIIfEmpty);
2121   }
2122
2123   /// getBasicBlockIndex - Return the first index of the specified basic
2124   /// block in the value list for this PHI.  Returns -1 if no instance.
2125   ///
2126   int getBasicBlockIndex(const BasicBlock *BB) const {
2127     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2128       if (block_begin()[i] == BB)
2129         return i;
2130     return -1;
2131   }
2132
2133   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2134     int Idx = getBasicBlockIndex(BB);
2135     assert(Idx >= 0 && "Invalid basic block argument!");
2136     return getIncomingValue(Idx);
2137   }
2138
2139   /// hasConstantValue - If the specified PHI node always merges together the
2140   /// same value, return the value, otherwise return null.
2141   Value *hasConstantValue() const;
2142
2143   /// Methods for support type inquiry through isa, cast, and dyn_cast:
2144   static inline bool classof(const Instruction *I) {
2145     return I->getOpcode() == Instruction::PHI;
2146   }
2147   static inline bool classof(const Value *V) {
2148     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2149   }
2150  private:
2151   void growOperands();
2152 };
2153
2154 template <>
2155 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2156 };
2157
2158 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2159
2160 //===----------------------------------------------------------------------===//
2161 //                           LandingPadInst Class
2162 //===----------------------------------------------------------------------===//
2163
2164 //===---------------------------------------------------------------------------
2165 /// LandingPadInst - The landingpad instruction holds all of the information
2166 /// necessary to generate correct exception handling. The landingpad instruction
2167 /// cannot be moved from the top of a landing pad block, which itself is
2168 /// accessible only from the 'unwind' edge of an invoke. This uses the
2169 /// SubclassData field in Value to store whether or not the landingpad is a
2170 /// cleanup.
2171 ///
2172 class LandingPadInst : public Instruction {
2173   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2174   /// the number actually in use.
2175   unsigned ReservedSpace;
2176   LandingPadInst(const LandingPadInst &LP);
2177 public:
2178   enum ClauseType { Catch, Filter };
2179 private:
2180   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2181   // Allocate space for exactly zero operands.
2182   void *operator new(size_t s) {
2183     return User::operator new(s, 0);
2184   }
2185   void growOperands(unsigned Size);
2186   void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2187
2188   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2189                           unsigned NumReservedValues, const Twine &NameStr,
2190                           Instruction *InsertBefore);
2191   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2192                           unsigned NumReservedValues, const Twine &NameStr,
2193                           BasicBlock *InsertAtEnd);
2194 protected:
2195   virtual LandingPadInst *clone_impl() const;
2196 public:
2197   /// Constructors - NumReservedClauses is a hint for the number of incoming
2198   /// clauses that this landingpad will have (use 0 if you really have no idea).
2199   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2200                                 unsigned NumReservedClauses,
2201                                 const Twine &NameStr = "",
2202                                 Instruction *InsertBefore = 0);
2203   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2204                                 unsigned NumReservedClauses,
2205                                 const Twine &NameStr, BasicBlock *InsertAtEnd);
2206   ~LandingPadInst();
2207
2208   /// Provide fast operand accessors
2209   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2210
2211   /// getPersonalityFn - Get the personality function associated with this
2212   /// landing pad.
2213   Value *getPersonalityFn() const { return getOperand(0); }
2214
2215   /// isCleanup - Return 'true' if this landingpad instruction is a
2216   /// cleanup. I.e., it should be run when unwinding even if its landing pad
2217   /// doesn't catch the exception.
2218   bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2219
2220   /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2221   void setCleanup(bool V) {
2222     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2223                                (V ? 1 : 0));
2224   }
2225
2226   /// addClause - Add a catch or filter clause to the landing pad.
2227   void addClause(Value *ClauseVal);
2228
2229   /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2230   /// to determine what type of clause this is.
2231   Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2232
2233   /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2234   bool isCatch(unsigned Idx) const {
2235     return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2236   }
2237
2238   /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2239   bool isFilter(unsigned Idx) const {
2240     return isa<ArrayType>(OperandList[Idx + 1]->getType());
2241   }
2242
2243   /// getNumClauses - Get the number of clauses for this landing pad.
2244   unsigned getNumClauses() const { return getNumOperands() - 1; }
2245
2246   /// reserveClauses - Grow the size of the operand list to accommodate the new
2247   /// number of clauses.
2248   void reserveClauses(unsigned Size) { growOperands(Size); }
2249
2250   // Methods for support type inquiry through isa, cast, and dyn_cast:
2251   static inline bool classof(const Instruction *I) {
2252     return I->getOpcode() == Instruction::LandingPad;
2253   }
2254   static inline bool classof(const Value *V) {
2255     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2256   }
2257 };
2258
2259 template <>
2260 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2261 };
2262
2263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2264
2265 //===----------------------------------------------------------------------===//
2266 //                               ReturnInst Class
2267 //===----------------------------------------------------------------------===//
2268
2269 //===---------------------------------------------------------------------------
2270 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2271 /// does not continue in this function any longer.
2272 ///
2273 class ReturnInst : public TerminatorInst {
2274   ReturnInst(const ReturnInst &RI);
2275
2276 private:
2277   // ReturnInst constructors:
2278   // ReturnInst()                  - 'ret void' instruction
2279   // ReturnInst(    null)          - 'ret void' instruction
2280   // ReturnInst(Value* X)          - 'ret X'    instruction
2281   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2282   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2283   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2284   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2285   //
2286   // NOTE: If the Value* passed is of type void then the constructor behaves as
2287   // if it was passed NULL.
2288   explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2289                       Instruction *InsertBefore = 0);
2290   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2291   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2292 protected:
2293   virtual ReturnInst *clone_impl() const;
2294 public:
2295   static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2296                             Instruction *InsertBefore = 0) {
2297     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2298   }
2299   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2300                             BasicBlock *InsertAtEnd) {
2301     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2302   }
2303   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2304     return new(0) ReturnInst(C, InsertAtEnd);
2305   }
2306   virtual ~ReturnInst();
2307
2308   /// Provide fast operand accessors
2309   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2310
2311   /// Convenience accessor. Returns null if there is no return value.
2312   Value *getReturnValue() const {
2313     return getNumOperands() != 0 ? getOperand(0) : 0;
2314   }
2315
2316   unsigned getNumSuccessors() const { return 0; }
2317
2318   // Methods for support type inquiry through isa, cast, and dyn_cast:
2319   static inline bool classof(const Instruction *I) {
2320     return (I->getOpcode() == Instruction::Ret);
2321   }
2322   static inline bool classof(const Value *V) {
2323     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2324   }
2325  private:
2326   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2327   virtual unsigned getNumSuccessorsV() const;
2328   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2329 };
2330
2331 template <>
2332 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2333 };
2334
2335 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2336
2337 //===----------------------------------------------------------------------===//
2338 //                               BranchInst Class
2339 //===----------------------------------------------------------------------===//
2340
2341 //===---------------------------------------------------------------------------
2342 /// BranchInst - Conditional or Unconditional Branch instruction.
2343 ///
2344 class BranchInst : public TerminatorInst {
2345   /// Ops list - Branches are strange.  The operands are ordered:
2346   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2347   /// they don't have to check for cond/uncond branchness. These are mostly
2348   /// accessed relative from op_end().
2349   BranchInst(const BranchInst &BI);
2350   void AssertOK();
2351   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2352   // BranchInst(BB *B)                           - 'br B'
2353   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2354   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2355   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2356   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2357   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2358   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2359   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2360              Instruction *InsertBefore = 0);
2361   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2362   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2363              BasicBlock *InsertAtEnd);
2364 protected:
2365   virtual BranchInst *clone_impl() const;
2366 public:
2367   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2368     return new(1) BranchInst(IfTrue, InsertBefore);
2369   }
2370   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2371                             Value *Cond, Instruction *InsertBefore = 0) {
2372     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2373   }
2374   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2375     return new(1) BranchInst(IfTrue, InsertAtEnd);
2376   }
2377   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2378                             Value *Cond, BasicBlock *InsertAtEnd) {
2379     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2380   }
2381
2382   /// Transparently provide more efficient getOperand methods.
2383   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2384
2385   bool isUnconditional() const { return getNumOperands() == 1; }
2386   bool isConditional()   const { return getNumOperands() == 3; }
2387
2388   Value *getCondition() const {
2389     assert(isConditional() && "Cannot get condition of an uncond branch!");
2390     return Op<-3>();
2391   }
2392
2393   void setCondition(Value *V) {
2394     assert(isConditional() && "Cannot set condition of unconditional branch!");
2395     Op<-3>() = V;
2396   }
2397
2398   unsigned getNumSuccessors() const { return 1+isConditional(); }
2399
2400   BasicBlock *getSuccessor(unsigned i) const {
2401     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2402     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2403   }
2404
2405   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2406     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2407     *(&Op<-1>() - idx) = (Value*)NewSucc;
2408   }
2409
2410   /// \brief Swap the successors of this branch instruction.
2411   ///
2412   /// Swaps the successors of the branch instruction. This also swaps any
2413   /// branch weight metadata associated with the instruction so that it
2414   /// continues to map correctly to each operand.
2415   void swapSuccessors();
2416
2417   // Methods for support type inquiry through isa, cast, and dyn_cast:
2418   static inline bool classof(const Instruction *I) {
2419     return (I->getOpcode() == Instruction::Br);
2420   }
2421   static inline bool classof(const Value *V) {
2422     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2423   }
2424 private:
2425   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2426   virtual unsigned getNumSuccessorsV() const;
2427   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2428 };
2429
2430 template <>
2431 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2432 };
2433
2434 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2435
2436 //===----------------------------------------------------------------------===//
2437 //                               SwitchInst Class
2438 //===----------------------------------------------------------------------===//
2439
2440 //===---------------------------------------------------------------------------
2441 /// SwitchInst - Multiway switch
2442 ///
2443 class SwitchInst : public TerminatorInst {
2444   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2445   unsigned ReservedSpace;
2446   // Operands format:
2447   // Operand[0]    = Value to switch on
2448   // Operand[1]    = Default basic block destination
2449   // Operand[2n  ] = Value to match
2450   // Operand[2n+1] = BasicBlock to go to on match
2451
2452   // Store case values separately from operands list. We needn't User-Use
2453   // concept here, since it is just a case value, it will always constant,
2454   // and case value couldn't reused with another instructions/values.
2455   // Additionally:
2456   // It allows us to use custom type for case values that is not inherited
2457   // from Value. Since case value is a complex type that implements
2458   // the subset of integers, we needn't extract sub-constants within
2459   // slow getAggregateElement method.
2460   // For case values we will use std::list to by two reasons:
2461   // 1. It allows to add/remove cases without whole collection reallocation.
2462   // 2. In most of cases we needn't random access.
2463   // Currently case values are also stored in Operands List, but it will moved
2464   // out in future commits.
2465   typedef std::list<IntegersSubset> Subsets;
2466   typedef Subsets::iterator SubsetsIt;
2467   typedef Subsets::const_iterator SubsetsConstIt;
2468
2469   Subsets TheSubsets;
2470
2471   SwitchInst(const SwitchInst &SI);
2472   void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2473   void growOperands();
2474   // allocate space for exactly zero operands
2475   void *operator new(size_t s) {
2476     return User::operator new(s, 0);
2477   }
2478   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2479   /// switch on and a default destination.  The number of additional cases can
2480   /// be specified here to make memory allocation more efficient.  This
2481   /// constructor can also autoinsert before another instruction.
2482   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2483              Instruction *InsertBefore);
2484
2485   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2486   /// switch on and a default destination.  The number of additional cases can
2487   /// be specified here to make memory allocation more efficient.  This
2488   /// constructor also autoinserts at the end of the specified BasicBlock.
2489   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2490              BasicBlock *InsertAtEnd);
2491 protected:
2492   virtual SwitchInst *clone_impl() const;
2493 public:
2494
2495   // FIXME: Currently there are a lot of unclean template parameters,
2496   // we need to make refactoring in future.
2497   // All these parameters are used to implement both iterator and const_iterator
2498   // without code duplication.
2499   // SwitchInstTy may be "const SwitchInst" or "SwitchInst"
2500   // ConstantIntTy may be "const ConstantInt" or "ConstantInt"
2501   // SubsetsItTy may be SubsetsConstIt or SubsetsIt
2502   // BasicBlockTy may be "const BasicBlock" or "BasicBlock"
2503   template <class SwitchInstTy, class ConstantIntTy,
2504             class SubsetsItTy, class BasicBlockTy>
2505     class CaseIteratorT;
2506
2507   typedef CaseIteratorT<const SwitchInst, const ConstantInt,
2508                         SubsetsConstIt, const BasicBlock> ConstCaseIt;
2509   class CaseIt;
2510
2511   // -2
2512   static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2513
2514   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2515                             unsigned NumCases, Instruction *InsertBefore = 0) {
2516     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2517   }
2518   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2519                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2520     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2521   }
2522
2523   ~SwitchInst();
2524
2525   /// Provide fast operand accessors
2526   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2527
2528   // Accessor Methods for Switch stmt
2529   Value *getCondition() const { return getOperand(0); }
2530   void setCondition(Value *V) { setOperand(0, V); }
2531
2532   BasicBlock *getDefaultDest() const {
2533     return cast<BasicBlock>(getOperand(1));
2534   }
2535
2536   void setDefaultDest(BasicBlock *DefaultCase) {
2537     setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2538   }
2539
2540   /// getNumCases - return the number of 'cases' in this switch instruction,
2541   /// except the default case
2542   unsigned getNumCases() const {
2543     return getNumOperands()/2 - 1;
2544   }
2545
2546   /// Returns a read/write iterator that points to the first
2547   /// case in SwitchInst.
2548   CaseIt case_begin() {
2549     return CaseIt(this, 0, TheSubsets.begin());
2550   }
2551   /// Returns a read-only iterator that points to the first
2552   /// case in the SwitchInst.
2553   ConstCaseIt case_begin() const {
2554     return ConstCaseIt(this, 0, TheSubsets.begin());
2555   }
2556
2557   /// Returns a read/write iterator that points one past the last
2558   /// in the SwitchInst.
2559   CaseIt case_end() {
2560     return CaseIt(this, getNumCases(), TheSubsets.end());
2561   }
2562   /// Returns a read-only iterator that points one past the last
2563   /// in the SwitchInst.
2564   ConstCaseIt case_end() const {
2565     return ConstCaseIt(this, getNumCases(), TheSubsets.end());
2566   }
2567   /// Returns an iterator that points to the default case.
2568   /// Note: this iterator allows to resolve successor only. Attempt
2569   /// to resolve case value causes an assertion.
2570   /// Also note, that increment and decrement also causes an assertion and
2571   /// makes iterator invalid.
2572   CaseIt case_default() {
2573     return CaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2574   }
2575   ConstCaseIt case_default() const {
2576     return ConstCaseIt(this, DefaultPseudoIndex, TheSubsets.end());
2577   }
2578
2579   /// findCaseValue - Search all of the case values for the specified constant.
2580   /// If it is explicitly handled, return the case iterator of it, otherwise
2581   /// return default case iterator to indicate
2582   /// that it is handled by the default handler.
2583   CaseIt findCaseValue(const ConstantInt *C) {
2584     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2585       if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2586         return i;
2587     return case_default();
2588   }
2589   ConstCaseIt findCaseValue(const ConstantInt *C) const {
2590     for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2591       if (i.getCaseValueEx().isSatisfies(IntItem::fromConstantInt(C)))
2592         return i;
2593     return case_default();
2594   }
2595
2596   /// findCaseDest - Finds the unique case value for a given successor. Returns
2597   /// null if the successor is not found, not unique, or is the default case.
2598   ConstantInt *findCaseDest(BasicBlock *BB) {
2599     if (BB == getDefaultDest()) return NULL;
2600
2601     ConstantInt *CI = NULL;
2602     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2603       if (i.getCaseSuccessor() == BB) {
2604         if (CI) return NULL;   // Multiple cases lead to BB.
2605         else CI = i.getCaseValue();
2606       }
2607     }
2608     return CI;
2609   }
2610
2611   /// addCase - Add an entry to the switch instruction...
2612   /// Note:
2613   /// This action invalidates case_end(). Old case_end() iterator will
2614   /// point to the added case.
2615   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2616
2617   /// addCase - Add an entry to the switch instruction.
2618   /// Note:
2619   /// This action invalidates case_end(). Old case_end() iterator will
2620   /// point to the added case.
2621   void addCase(IntegersSubset& OnVal, BasicBlock *Dest);
2622
2623   /// removeCase - This method removes the specified case and its successor
2624   /// from the switch instruction. Note that this operation may reorder the
2625   /// remaining cases at index idx and above.
2626   /// Note:
2627   /// This action invalidates iterators for all cases following the one removed,
2628   /// including the case_end() iterator.
2629   void removeCase(CaseIt& i);
2630
2631   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2632   BasicBlock *getSuccessor(unsigned idx) const {
2633     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2634     return cast<BasicBlock>(getOperand(idx*2+1));
2635   }
2636   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2637     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2638     setOperand(idx*2+1, (Value*)NewSucc);
2639   }
2640
2641   uint16_t hash() const {
2642     uint32_t NumberOfCases = (uint32_t)getNumCases();
2643     uint16_t Hash = (0xFFFF & NumberOfCases) ^ (NumberOfCases >> 16);
2644     for (ConstCaseIt i = case_begin(), e = case_end();
2645          i != e; ++i) {
2646       uint32_t NumItems = (uint32_t)i.getCaseValueEx().getNumItems();
2647       Hash = (Hash << 1) ^ (0xFFFF & NumItems) ^ (NumItems >> 16);
2648     }
2649     return Hash;
2650   }
2651
2652   // Case iterators definition.
2653
2654   template <class SwitchInstTy, class ConstantIntTy,
2655             class SubsetsItTy, class BasicBlockTy>
2656   class CaseIteratorT {
2657   protected:
2658
2659     SwitchInstTy *SI;
2660     unsigned Index;
2661     SubsetsItTy SubsetIt;
2662
2663     /// Initializes case iterator for given SwitchInst and for given
2664     /// case number.
2665     friend class SwitchInst;
2666     CaseIteratorT(SwitchInstTy *SI, unsigned SuccessorIndex,
2667                   SubsetsItTy CaseValueIt) {
2668       this->SI = SI;
2669       Index = SuccessorIndex;
2670       this->SubsetIt = CaseValueIt;
2671     }
2672
2673   public:
2674     typedef typename SubsetsItTy::reference IntegersSubsetRef;
2675     typedef CaseIteratorT<SwitchInstTy, ConstantIntTy,
2676                           SubsetsItTy, BasicBlockTy> Self;
2677
2678     CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2679           this->SI = SI;
2680           Index = CaseNum;
2681           SubsetIt = SI->TheSubsets.begin();
2682           std::advance(SubsetIt, CaseNum);
2683         }
2684
2685
2686     /// Initializes case iterator for given SwitchInst and for given
2687     /// TerminatorInst's successor index.
2688     static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2689       assert(SuccessorIndex < SI->getNumSuccessors() &&
2690              "Successor index # out of range!");
2691       return SuccessorIndex != 0 ?
2692              Self(SI, SuccessorIndex - 1) :
2693              Self(SI, DefaultPseudoIndex);
2694     }
2695
2696     /// Resolves case value for current case.
2697     ConstantIntTy *getCaseValue() {
2698       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2699       IntegersSubsetRef CaseRanges = *SubsetIt;
2700
2701       // FIXME: Currently we work with ConstantInt based cases.
2702       // So return CaseValue as ConstantInt.
2703       return CaseRanges.getSingleNumber(0).toConstantInt();
2704     }
2705
2706     /// Resolves case value for current case.
2707     IntegersSubsetRef getCaseValueEx() {
2708       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2709       return *SubsetIt;
2710     }
2711
2712     /// Resolves successor for current case.
2713     BasicBlockTy *getCaseSuccessor() {
2714       assert((Index < SI->getNumCases() ||
2715               Index == DefaultPseudoIndex) &&
2716              "Index out the number of cases.");
2717       return SI->getSuccessor(getSuccessorIndex());
2718     }
2719
2720     /// Returns number of current case.
2721     unsigned getCaseIndex() const { return Index; }
2722
2723     /// Returns TerminatorInst's successor index for current case successor.
2724     unsigned getSuccessorIndex() const {
2725       assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2726              "Index out the number of cases.");
2727       return Index != DefaultPseudoIndex ? Index + 1 : 0;
2728     }
2729
2730     Self operator++() {
2731       // Check index correctness after increment.
2732       // Note: Index == getNumCases() means end().
2733       assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2734       ++Index;
2735       if (Index == 0)
2736         SubsetIt = SI->TheSubsets.begin();
2737       else
2738         ++SubsetIt;
2739       return *this;
2740     }
2741     Self operator++(int) {
2742       Self tmp = *this;
2743       ++(*this);
2744       return tmp;
2745     }
2746     Self operator--() {
2747       // Check index correctness after decrement.
2748       // Note: Index == getNumCases() means end().
2749       // Also allow "-1" iterator here. That will became valid after ++.
2750       unsigned NumCases = SI->getNumCases();
2751       assert((Index == 0 || Index-1 <= NumCases) &&
2752              "Index out the number of cases.");
2753       --Index;
2754       if (Index == NumCases) {
2755         SubsetIt = SI->TheSubsets.end();
2756         return *this;
2757       }
2758
2759       if (Index != -1U)
2760         --SubsetIt;
2761
2762       return *this;
2763     }
2764     Self operator--(int) {
2765       Self tmp = *this;
2766       --(*this);
2767       return tmp;
2768     }
2769     bool operator==(const Self& RHS) const {
2770       assert(RHS.SI == SI && "Incompatible operators.");
2771       return RHS.Index == Index;
2772     }
2773     bool operator!=(const Self& RHS) const {
2774       assert(RHS.SI == SI && "Incompatible operators.");
2775       return RHS.Index != Index;
2776     }
2777   };
2778
2779   class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt,
2780                                       SubsetsIt, BasicBlock> {
2781     typedef CaseIteratorT<SwitchInst, ConstantInt, SubsetsIt, BasicBlock>
2782       ParentTy;
2783
2784   protected:
2785     friend class SwitchInst;
2786     CaseIt(SwitchInst *SI, unsigned CaseNum, SubsetsIt SubsetIt) :
2787       ParentTy(SI, CaseNum, SubsetIt) {}
2788
2789     void updateCaseValueOperand(IntegersSubset& V) {
2790       SI->setOperand(2 + Index*2, reinterpret_cast<Value*>((Constant*)V));
2791     }
2792
2793   public:
2794
2795     CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2796
2797     CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2798
2799     /// Sets the new value for current case.
2800     void setValue(ConstantInt *V) {
2801       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2802       IntegersSubsetToBB Mapping;
2803       // FIXME: Currently we work with ConstantInt based cases.
2804       // So inititalize IntItem container directly from ConstantInt.
2805       Mapping.add(IntItem::fromConstantInt(V));
2806       *SubsetIt = Mapping.getCase();
2807       updateCaseValueOperand(*SubsetIt);
2808     }
2809
2810     /// Sets the new value for current case.
2811     void setValueEx(IntegersSubset& V) {
2812       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2813       *SubsetIt = V;
2814       updateCaseValueOperand(*SubsetIt);
2815     }
2816
2817     /// Sets the new successor for current case.
2818     void setSuccessor(BasicBlock *S) {
2819       SI->setSuccessor(getSuccessorIndex(), S);
2820     }
2821   };
2822
2823   // Methods for support type inquiry through isa, cast, and dyn_cast:
2824
2825   static inline bool classof(const Instruction *I) {
2826     return I->getOpcode() == Instruction::Switch;
2827   }
2828   static inline bool classof(const Value *V) {
2829     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2830   }
2831 private:
2832   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2833   virtual unsigned getNumSuccessorsV() const;
2834   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2835 };
2836
2837 template <>
2838 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2839 };
2840
2841 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2842
2843
2844 //===----------------------------------------------------------------------===//
2845 //                             IndirectBrInst Class
2846 //===----------------------------------------------------------------------===//
2847
2848 //===---------------------------------------------------------------------------
2849 /// IndirectBrInst - Indirect Branch Instruction.
2850 ///
2851 class IndirectBrInst : public TerminatorInst {
2852   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2853   unsigned ReservedSpace;
2854   // Operand[0]    = Value to switch on
2855   // Operand[1]    = Default basic block destination
2856   // Operand[2n  ] = Value to match
2857   // Operand[2n+1] = BasicBlock to go to on match
2858   IndirectBrInst(const IndirectBrInst &IBI);
2859   void init(Value *Address, unsigned NumDests);
2860   void growOperands();
2861   // allocate space for exactly zero operands
2862   void *operator new(size_t s) {
2863     return User::operator new(s, 0);
2864   }
2865   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2866   /// Address to jump to.  The number of expected destinations can be specified
2867   /// here to make memory allocation more efficient.  This constructor can also
2868   /// autoinsert before another instruction.
2869   IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
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 also
2874   /// autoinserts at the end of the specified BasicBlock.
2875   IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2876 protected:
2877   virtual IndirectBrInst *clone_impl() const;
2878 public:
2879   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2880                                 Instruction *InsertBefore = 0) {
2881     return new IndirectBrInst(Address, NumDests, InsertBefore);
2882   }
2883   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2884                                 BasicBlock *InsertAtEnd) {
2885     return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2886   }
2887   ~IndirectBrInst();
2888
2889   /// Provide fast operand accessors.
2890   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2891
2892   // Accessor Methods for IndirectBrInst instruction.
2893   Value *getAddress() { return getOperand(0); }
2894   const Value *getAddress() const { return getOperand(0); }
2895   void setAddress(Value *V) { setOperand(0, V); }
2896
2897
2898   /// getNumDestinations - return the number of possible destinations in this
2899   /// indirectbr instruction.
2900   unsigned getNumDestinations() const { return getNumOperands()-1; }
2901
2902   /// getDestination - Return the specified destination.
2903   BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2904   const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2905
2906   /// addDestination - Add a destination.
2907   ///
2908   void addDestination(BasicBlock *Dest);
2909
2910   /// removeDestination - This method removes the specified successor from the
2911   /// indirectbr instruction.
2912   void removeDestination(unsigned i);
2913
2914   unsigned getNumSuccessors() const { return getNumOperands()-1; }
2915   BasicBlock *getSuccessor(unsigned i) const {
2916     return cast<BasicBlock>(getOperand(i+1));
2917   }
2918   void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2919     setOperand(i+1, (Value*)NewSucc);
2920   }
2921
2922   // Methods for support type inquiry through isa, cast, and dyn_cast:
2923   static inline bool classof(const Instruction *I) {
2924     return I->getOpcode() == Instruction::IndirectBr;
2925   }
2926   static inline bool classof(const Value *V) {
2927     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2928   }
2929 private:
2930   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2931   virtual unsigned getNumSuccessorsV() const;
2932   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2933 };
2934
2935 template <>
2936 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2937 };
2938
2939 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2940
2941
2942 //===----------------------------------------------------------------------===//
2943 //                               InvokeInst Class
2944 //===----------------------------------------------------------------------===//
2945
2946 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2947 /// calling convention of the call.
2948 ///
2949 class InvokeInst : public TerminatorInst {
2950   AttributeSet AttributeList;
2951   InvokeInst(const InvokeInst &BI);
2952   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2953             ArrayRef<Value *> Args, const Twine &NameStr);
2954
2955   /// Construct an InvokeInst given a range of arguments.
2956   ///
2957   /// \brief Construct an InvokeInst from a range of arguments
2958   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2959                     ArrayRef<Value *> Args, unsigned Values,
2960                     const Twine &NameStr, Instruction *InsertBefore);
2961
2962   /// Construct an InvokeInst given a range of arguments.
2963   ///
2964   /// \brief Construct an InvokeInst from a range of arguments
2965   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2966                     ArrayRef<Value *> Args, unsigned Values,
2967                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2968 protected:
2969   virtual InvokeInst *clone_impl() const;
2970 public:
2971   static InvokeInst *Create(Value *Func,
2972                             BasicBlock *IfNormal, BasicBlock *IfException,
2973                             ArrayRef<Value *> Args, const Twine &NameStr = "",
2974                             Instruction *InsertBefore = 0) {
2975     unsigned Values = unsigned(Args.size()) + 3;
2976     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2977                                   Values, NameStr, InsertBefore);
2978   }
2979   static InvokeInst *Create(Value *Func,
2980                             BasicBlock *IfNormal, BasicBlock *IfException,
2981                             ArrayRef<Value *> Args, const Twine &NameStr,
2982                             BasicBlock *InsertAtEnd) {
2983     unsigned Values = unsigned(Args.size()) + 3;
2984     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2985                                   Values, NameStr, InsertAtEnd);
2986   }
2987
2988   /// Provide fast operand accessors
2989   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2990
2991   /// getNumArgOperands - Return the number of invoke arguments.
2992   ///
2993   unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2994
2995   /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2996   ///
2997   Value *getArgOperand(unsigned i) const { return getOperand(i); }
2998   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2999
3000   /// getCallingConv/setCallingConv - Get or set the calling convention of this
3001   /// function call.
3002   CallingConv::ID getCallingConv() const {
3003     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3004   }
3005   void setCallingConv(CallingConv::ID CC) {
3006     setInstructionSubclassData(static_cast<unsigned>(CC));
3007   }
3008
3009   /// getAttributes - Return the parameter attributes for this invoke.
3010   ///
3011   const AttributeSet &getAttributes() const { return AttributeList; }
3012
3013   /// setAttributes - Set the parameter attributes for this invoke.
3014   ///
3015   void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3016
3017   /// addAttribute - adds the attribute to the list of attributes.
3018   void addAttribute(unsigned i, Attribute::AttrKind attr);
3019
3020   /// removeAttribute - removes the attribute from the list of attributes.
3021   void removeAttribute(unsigned i, Attribute attr);
3022
3023   /// \brief Determine whether this call has the NoAlias attribute.
3024   bool hasFnAttr(Attribute::AttrKind A) const;
3025
3026   /// \brief Determine whether the call or the callee has the given attributes.
3027   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3028
3029   /// \brief Extract the alignment for a call or parameter (0=unknown).
3030   unsigned getParamAlignment(unsigned i) const {
3031     return AttributeList.getParamAlignment(i);
3032   }
3033
3034   /// \brief Return true if the call should not be inlined.
3035   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3036   void setIsNoInline() {
3037     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3038   }
3039
3040   /// \brief Determine if the call does not access memory.
3041   bool doesNotAccessMemory() const {
3042     return hasFnAttr(Attribute::ReadNone);
3043   }
3044   void setDoesNotAccessMemory() {
3045     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3046   }
3047
3048   /// \brief Determine if the call does not access or only reads memory.
3049   bool onlyReadsMemory() const {
3050     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3051   }
3052   void setOnlyReadsMemory() {
3053     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3054   }
3055
3056   /// \brief Determine if the call cannot return.
3057   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3058   void setDoesNotReturn() {
3059     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3060   }
3061
3062   /// \brief Determine if the call cannot unwind.
3063   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3064   void setDoesNotThrow() {
3065     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3066   }
3067
3068   /// \brief Determine if the call returns a structure through first
3069   /// pointer argument.
3070   bool hasStructRetAttr() const {
3071     // Be friendly and also check the callee.
3072     return paramHasAttr(1, Attribute::StructRet);
3073   }
3074
3075   /// \brief Determine if any call argument is an aggregate passed by value.
3076   bool hasByValArgument() const {
3077     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3078   }
3079
3080   /// getCalledFunction - Return the function called, or null if this is an
3081   /// indirect function invocation.
3082   ///
3083   Function *getCalledFunction() const {
3084     return dyn_cast<Function>(Op<-3>());
3085   }
3086
3087   /// getCalledValue - Get a pointer to the function that is invoked by this
3088   /// instruction
3089   const Value *getCalledValue() const { return Op<-3>(); }
3090         Value *getCalledValue()       { return Op<-3>(); }
3091
3092   /// setCalledFunction - Set the function called.
3093   void setCalledFunction(Value* Fn) {
3094     Op<-3>() = Fn;
3095   }
3096
3097   // get*Dest - Return the destination basic blocks...
3098   BasicBlock *getNormalDest() const {
3099     return cast<BasicBlock>(Op<-2>());
3100   }
3101   BasicBlock *getUnwindDest() const {
3102     return cast<BasicBlock>(Op<-1>());
3103   }
3104   void setNormalDest(BasicBlock *B) {
3105     Op<-2>() = reinterpret_cast<Value*>(B);
3106   }
3107   void setUnwindDest(BasicBlock *B) {
3108     Op<-1>() = reinterpret_cast<Value*>(B);
3109   }
3110
3111   /// getLandingPadInst - Get the landingpad instruction from the landing pad
3112   /// block (the unwind destination).
3113   LandingPadInst *getLandingPadInst() const;
3114
3115   BasicBlock *getSuccessor(unsigned i) const {
3116     assert(i < 2 && "Successor # out of range for invoke!");
3117     return i == 0 ? getNormalDest() : getUnwindDest();
3118   }
3119
3120   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3121     assert(idx < 2 && "Successor # out of range for invoke!");
3122     *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3123   }
3124
3125   unsigned getNumSuccessors() const { return 2; }
3126
3127   // Methods for support type inquiry through isa, cast, and dyn_cast:
3128   static inline bool classof(const Instruction *I) {
3129     return (I->getOpcode() == Instruction::Invoke);
3130   }
3131   static inline bool classof(const Value *V) {
3132     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3133   }
3134
3135 private:
3136   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3137   virtual unsigned getNumSuccessorsV() const;
3138   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3139
3140   // Shadow Instruction::setInstructionSubclassData with a private forwarding
3141   // method so that subclasses cannot accidentally use it.
3142   void setInstructionSubclassData(unsigned short D) {
3143     Instruction::setInstructionSubclassData(D);
3144   }
3145 };
3146
3147 template <>
3148 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3149 };
3150
3151 InvokeInst::InvokeInst(Value *Func,
3152                        BasicBlock *IfNormal, BasicBlock *IfException,
3153                        ArrayRef<Value *> Args, unsigned Values,
3154                        const Twine &NameStr, Instruction *InsertBefore)
3155   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3156                                       ->getElementType())->getReturnType(),
3157                    Instruction::Invoke,
3158                    OperandTraits<InvokeInst>::op_end(this) - Values,
3159                    Values, InsertBefore) {
3160   init(Func, IfNormal, IfException, Args, NameStr);
3161 }
3162 InvokeInst::InvokeInst(Value *Func,
3163                        BasicBlock *IfNormal, BasicBlock *IfException,
3164                        ArrayRef<Value *> Args, unsigned Values,
3165                        const Twine &NameStr, BasicBlock *InsertAtEnd)
3166   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3167                                       ->getElementType())->getReturnType(),
3168                    Instruction::Invoke,
3169                    OperandTraits<InvokeInst>::op_end(this) - Values,
3170                    Values, InsertAtEnd) {
3171   init(Func, IfNormal, IfException, Args, NameStr);
3172 }
3173
3174 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3175
3176 //===----------------------------------------------------------------------===//
3177 //                              ResumeInst Class
3178 //===----------------------------------------------------------------------===//
3179
3180 //===---------------------------------------------------------------------------
3181 /// ResumeInst - Resume the propagation of an exception.
3182 ///
3183 class ResumeInst : public TerminatorInst {
3184   ResumeInst(const ResumeInst &RI);
3185
3186   explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3187   ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3188 protected:
3189   virtual ResumeInst *clone_impl() const;
3190 public:
3191   static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3192     return new(1) ResumeInst(Exn, InsertBefore);
3193   }
3194   static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3195     return new(1) ResumeInst(Exn, InsertAtEnd);
3196   }
3197
3198   /// Provide fast operand accessors
3199   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3200
3201   /// Convenience accessor.
3202   Value *getValue() const { return Op<0>(); }
3203
3204   unsigned getNumSuccessors() const { return 0; }
3205
3206   // Methods for support type inquiry through isa, cast, and dyn_cast:
3207   static inline bool classof(const Instruction *I) {
3208     return I->getOpcode() == Instruction::Resume;
3209   }
3210   static inline bool classof(const Value *V) {
3211     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3212   }
3213 private:
3214   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3215   virtual unsigned getNumSuccessorsV() const;
3216   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3217 };
3218
3219 template <>
3220 struct OperandTraits<ResumeInst> :
3221     public FixedNumOperandTraits<ResumeInst, 1> {
3222 };
3223
3224 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3225
3226 //===----------------------------------------------------------------------===//
3227 //                           UnreachableInst Class
3228 //===----------------------------------------------------------------------===//
3229
3230 //===---------------------------------------------------------------------------
3231 /// UnreachableInst - This function has undefined behavior.  In particular, the
3232 /// presence of this instruction indicates some higher level knowledge that the
3233 /// end of the block cannot be reached.
3234 ///
3235 class UnreachableInst : public TerminatorInst {
3236   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3237 protected:
3238   virtual UnreachableInst *clone_impl() const;
3239
3240 public:
3241   // allocate space for exactly zero operands
3242   void *operator new(size_t s) {
3243     return User::operator new(s, 0);
3244   }
3245   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3246   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3247
3248   unsigned getNumSuccessors() const { return 0; }
3249
3250   // Methods for support type inquiry through isa, cast, and dyn_cast:
3251   static inline bool classof(const Instruction *I) {
3252     return I->getOpcode() == Instruction::Unreachable;
3253   }
3254   static inline bool classof(const Value *V) {
3255     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3256   }
3257 private:
3258   virtual BasicBlock *getSuccessorV(unsigned idx) const;
3259   virtual unsigned getNumSuccessorsV() const;
3260   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
3261 };
3262
3263 //===----------------------------------------------------------------------===//
3264 //                                 TruncInst Class
3265 //===----------------------------------------------------------------------===//
3266
3267 /// \brief This class represents a truncation of integer types.
3268 class TruncInst : public CastInst {
3269 protected:
3270   /// \brief Clone an identical TruncInst
3271   virtual TruncInst *clone_impl() const;
3272
3273 public:
3274   /// \brief Constructor with insert-before-instruction semantics
3275   TruncInst(
3276     Value *S,                     ///< The value to be truncated
3277     Type *Ty,               ///< The (smaller) type to truncate to
3278     const Twine &NameStr = "",    ///< A name for the new instruction
3279     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3280   );
3281
3282   /// \brief Constructor with insert-at-end-of-block semantics
3283   TruncInst(
3284     Value *S,                     ///< The value to be truncated
3285     Type *Ty,               ///< The (smaller) type to truncate to
3286     const Twine &NameStr,         ///< A name for the new instruction
3287     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3288   );
3289
3290   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3291   static inline bool classof(const Instruction *I) {
3292     return I->getOpcode() == Trunc;
3293   }
3294   static inline bool classof(const Value *V) {
3295     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3296   }
3297 };
3298
3299 //===----------------------------------------------------------------------===//
3300 //                                 ZExtInst Class
3301 //===----------------------------------------------------------------------===//
3302
3303 /// \brief This class represents zero extension of integer types.
3304 class ZExtInst : public CastInst {
3305 protected:
3306   /// \brief Clone an identical ZExtInst
3307   virtual ZExtInst *clone_impl() const;
3308
3309 public:
3310   /// \brief Constructor with insert-before-instruction semantics
3311   ZExtInst(
3312     Value *S,                     ///< The value to be zero extended
3313     Type *Ty,               ///< The type to zero extend to
3314     const Twine &NameStr = "",    ///< A name for the new instruction
3315     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3316   );
3317
3318   /// \brief Constructor with insert-at-end semantics.
3319   ZExtInst(
3320     Value *S,                     ///< The value to be zero extended
3321     Type *Ty,               ///< The type to zero extend to
3322     const Twine &NameStr,         ///< A name for the new instruction
3323     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3324   );
3325
3326   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3327   static inline bool classof(const Instruction *I) {
3328     return I->getOpcode() == ZExt;
3329   }
3330   static inline bool classof(const Value *V) {
3331     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3332   }
3333 };
3334
3335 //===----------------------------------------------------------------------===//
3336 //                                 SExtInst Class
3337 //===----------------------------------------------------------------------===//
3338
3339 /// \brief This class represents a sign extension of integer types.
3340 class SExtInst : public CastInst {
3341 protected:
3342   /// \brief Clone an identical SExtInst
3343   virtual SExtInst *clone_impl() const;
3344
3345 public:
3346   /// \brief Constructor with insert-before-instruction semantics
3347   SExtInst(
3348     Value *S,                     ///< The value to be sign extended
3349     Type *Ty,               ///< The type to sign extend to
3350     const Twine &NameStr = "",    ///< A name for the new instruction
3351     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3352   );
3353
3354   /// \brief Constructor with insert-at-end-of-block semantics
3355   SExtInst(
3356     Value *S,                     ///< The value to be sign extended
3357     Type *Ty,               ///< The type to sign extend to
3358     const Twine &NameStr,         ///< A name for the new instruction
3359     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3360   );
3361
3362   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3363   static inline bool classof(const Instruction *I) {
3364     return I->getOpcode() == SExt;
3365   }
3366   static inline bool classof(const Value *V) {
3367     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3368   }
3369 };
3370
3371 //===----------------------------------------------------------------------===//
3372 //                                 FPTruncInst Class
3373 //===----------------------------------------------------------------------===//
3374
3375 /// \brief This class represents a truncation of floating point types.
3376 class FPTruncInst : public CastInst {
3377 protected:
3378   /// \brief Clone an identical FPTruncInst
3379   virtual FPTruncInst *clone_impl() const;
3380
3381 public:
3382   /// \brief Constructor with insert-before-instruction semantics
3383   FPTruncInst(
3384     Value *S,                     ///< The value to be truncated
3385     Type *Ty,               ///< The type to truncate to
3386     const Twine &NameStr = "",    ///< A name for the new instruction
3387     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3388   );
3389
3390   /// \brief Constructor with insert-before-instruction semantics
3391   FPTruncInst(
3392     Value *S,                     ///< The value to be truncated
3393     Type *Ty,               ///< The type to truncate to
3394     const Twine &NameStr,         ///< A name for the new instruction
3395     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3396   );
3397
3398   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3399   static inline bool classof(const Instruction *I) {
3400     return I->getOpcode() == FPTrunc;
3401   }
3402   static inline bool classof(const Value *V) {
3403     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3404   }
3405 };
3406
3407 //===----------------------------------------------------------------------===//
3408 //                                 FPExtInst Class
3409 //===----------------------------------------------------------------------===//
3410
3411 /// \brief This class represents an extension of floating point types.
3412 class FPExtInst : public CastInst {
3413 protected:
3414   /// \brief Clone an identical FPExtInst
3415   virtual FPExtInst *clone_impl() const;
3416
3417 public:
3418   /// \brief Constructor with insert-before-instruction semantics
3419   FPExtInst(
3420     Value *S,                     ///< The value to be extended
3421     Type *Ty,               ///< The type to extend to
3422     const Twine &NameStr = "",    ///< A name for the new instruction
3423     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3424   );
3425
3426   /// \brief Constructor with insert-at-end-of-block semantics
3427   FPExtInst(
3428     Value *S,                     ///< The value to be extended
3429     Type *Ty,               ///< The type to extend to
3430     const Twine &NameStr,         ///< A name for the new instruction
3431     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3432   );
3433
3434   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3435   static inline bool classof(const Instruction *I) {
3436     return I->getOpcode() == FPExt;
3437   }
3438   static inline bool classof(const Value *V) {
3439     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3440   }
3441 };
3442
3443 //===----------------------------------------------------------------------===//
3444 //                                 UIToFPInst Class
3445 //===----------------------------------------------------------------------===//
3446
3447 /// \brief This class represents a cast unsigned integer to floating point.
3448 class UIToFPInst : public CastInst {
3449 protected:
3450   /// \brief Clone an identical UIToFPInst
3451   virtual UIToFPInst *clone_impl() const;
3452
3453 public:
3454   /// \brief Constructor with insert-before-instruction semantics
3455   UIToFPInst(
3456     Value *S,                     ///< The value to be converted
3457     Type *Ty,               ///< The type to convert to
3458     const Twine &NameStr = "",    ///< A name for the new instruction
3459     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3460   );
3461
3462   /// \brief Constructor with insert-at-end-of-block semantics
3463   UIToFPInst(
3464     Value *S,                     ///< The value to be converted
3465     Type *Ty,               ///< The type to convert to
3466     const Twine &NameStr,         ///< A name for the new instruction
3467     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3468   );
3469
3470   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3471   static inline bool classof(const Instruction *I) {
3472     return I->getOpcode() == UIToFP;
3473   }
3474   static inline bool classof(const Value *V) {
3475     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3476   }
3477 };
3478
3479 //===----------------------------------------------------------------------===//
3480 //                                 SIToFPInst Class
3481 //===----------------------------------------------------------------------===//
3482
3483 /// \brief This class represents a cast from signed integer to floating point.
3484 class SIToFPInst : public CastInst {
3485 protected:
3486   /// \brief Clone an identical SIToFPInst
3487   virtual SIToFPInst *clone_impl() const;
3488
3489 public:
3490   /// \brief Constructor with insert-before-instruction semantics
3491   SIToFPInst(
3492     Value *S,                     ///< The value to be converted
3493     Type *Ty,               ///< The type to convert to
3494     const Twine &NameStr = "",    ///< A name for the new instruction
3495     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3496   );
3497
3498   /// \brief Constructor with insert-at-end-of-block semantics
3499   SIToFPInst(
3500     Value *S,                     ///< The value to be converted
3501     Type *Ty,               ///< The type to convert to
3502     const Twine &NameStr,         ///< A name for the new instruction
3503     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3504   );
3505
3506   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3507   static inline bool classof(const Instruction *I) {
3508     return I->getOpcode() == SIToFP;
3509   }
3510   static inline bool classof(const Value *V) {
3511     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3512   }
3513 };
3514
3515 //===----------------------------------------------------------------------===//
3516 //                                 FPToUIInst Class
3517 //===----------------------------------------------------------------------===//
3518
3519 /// \brief This class represents a cast from floating point to unsigned integer
3520 class FPToUIInst  : public CastInst {
3521 protected:
3522   /// \brief Clone an identical FPToUIInst
3523   virtual FPToUIInst *clone_impl() const;
3524
3525 public:
3526   /// \brief Constructor with insert-before-instruction semantics
3527   FPToUIInst(
3528     Value *S,                     ///< The value to be converted
3529     Type *Ty,               ///< The type to convert to
3530     const Twine &NameStr = "",    ///< A name for the new instruction
3531     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3532   );
3533
3534   /// \brief Constructor with insert-at-end-of-block semantics
3535   FPToUIInst(
3536     Value *S,                     ///< The value to be converted
3537     Type *Ty,               ///< The type to convert to
3538     const Twine &NameStr,         ///< A name for the new instruction
3539     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
3540   );
3541
3542   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3543   static inline bool classof(const Instruction *I) {
3544     return I->getOpcode() == FPToUI;
3545   }
3546   static inline bool classof(const Value *V) {
3547     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3548   }
3549 };
3550
3551 //===----------------------------------------------------------------------===//
3552 //                                 FPToSIInst Class
3553 //===----------------------------------------------------------------------===//
3554
3555 /// \brief This class represents a cast from floating point to signed integer.
3556 class FPToSIInst  : public CastInst {
3557 protected:
3558   /// \brief Clone an identical FPToSIInst
3559   virtual FPToSIInst *clone_impl() const;
3560
3561 public:
3562   /// \brief Constructor with insert-before-instruction semantics
3563   FPToSIInst(
3564     Value *S,                     ///< The value to be converted
3565     Type *Ty,               ///< The type to convert to
3566     const Twine &NameStr = "",    ///< A name for the new instruction
3567     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3568   );
3569
3570   /// \brief Constructor with insert-at-end-of-block semantics
3571   FPToSIInst(
3572     Value *S,                     ///< The value to be converted
3573     Type *Ty,               ///< The type to convert to
3574     const Twine &NameStr,         ///< A name for the new instruction
3575     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3576   );
3577
3578   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3579   static inline bool classof(const Instruction *I) {
3580     return I->getOpcode() == FPToSI;
3581   }
3582   static inline bool classof(const Value *V) {
3583     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3584   }
3585 };
3586
3587 //===----------------------------------------------------------------------===//
3588 //                                 IntToPtrInst Class
3589 //===----------------------------------------------------------------------===//
3590
3591 /// \brief This class represents a cast from an integer to a pointer.
3592 class IntToPtrInst : public CastInst {
3593 public:
3594   /// \brief Constructor with insert-before-instruction semantics
3595   IntToPtrInst(
3596     Value *S,                     ///< The value to be converted
3597     Type *Ty,               ///< The type to convert to
3598     const Twine &NameStr = "",    ///< A name for the new instruction
3599     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3600   );
3601
3602   /// \brief Constructor with insert-at-end-of-block semantics
3603   IntToPtrInst(
3604     Value *S,                     ///< The value to be converted
3605     Type *Ty,               ///< The type to convert to
3606     const Twine &NameStr,         ///< A name for the new instruction
3607     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3608   );
3609
3610   /// \brief Clone an identical IntToPtrInst
3611   virtual IntToPtrInst *clone_impl() const;
3612
3613   /// \brief Returns the address space of this instruction's pointer type.
3614   unsigned getAddressSpace() const {
3615     return getType()->getPointerAddressSpace();
3616   }
3617
3618   // Methods for support type inquiry through isa, cast, and dyn_cast:
3619   static inline bool classof(const Instruction *I) {
3620     return I->getOpcode() == IntToPtr;
3621   }
3622   static inline bool classof(const Value *V) {
3623     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3624   }
3625 };
3626
3627 //===----------------------------------------------------------------------===//
3628 //                                 PtrToIntInst Class
3629 //===----------------------------------------------------------------------===//
3630
3631 /// \brief This class represents a cast from a pointer to an integer
3632 class PtrToIntInst : public CastInst {
3633 protected:
3634   /// \brief Clone an identical PtrToIntInst
3635   virtual PtrToIntInst *clone_impl() const;
3636
3637 public:
3638   /// \brief Constructor with insert-before-instruction semantics
3639   PtrToIntInst(
3640     Value *S,                     ///< The value to be converted
3641     Type *Ty,               ///< The type to convert to
3642     const Twine &NameStr = "",    ///< A name for the new instruction
3643     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3644   );
3645
3646   /// \brief Constructor with insert-at-end-of-block semantics
3647   PtrToIntInst(
3648     Value *S,                     ///< The value to be converted
3649     Type *Ty,               ///< The type to convert to
3650     const Twine &NameStr,         ///< A name for the new instruction
3651     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3652   );
3653
3654   /// \brief Gets the pointer operand.
3655   Value *getPointerOperand() { return getOperand(0); }
3656   /// \brief Gets the pointer operand.
3657   const Value *getPointerOperand() const { return getOperand(0); }
3658   /// \brief Gets the operand index of the pointer operand.
3659   static unsigned getPointerOperandIndex() { return 0U; }
3660
3661   /// \brief Returns the address space of the pointer operand.
3662   unsigned getPointerAddressSpace() const {
3663     return getPointerOperand()->getType()->getPointerAddressSpace();
3664   }
3665
3666   // Methods for support type inquiry through isa, cast, and dyn_cast:
3667   static inline bool classof(const Instruction *I) {
3668     return I->getOpcode() == PtrToInt;
3669   }
3670   static inline bool classof(const Value *V) {
3671     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3672   }
3673 };
3674
3675 //===----------------------------------------------------------------------===//
3676 //                             BitCastInst Class
3677 //===----------------------------------------------------------------------===//
3678
3679 /// \brief This class represents a no-op cast from one type to another.
3680 class BitCastInst : public CastInst {
3681 protected:
3682   /// \brief Clone an identical BitCastInst
3683   virtual BitCastInst *clone_impl() const;
3684
3685 public:
3686   /// \brief Constructor with insert-before-instruction semantics
3687   BitCastInst(
3688     Value *S,                     ///< The value to be casted
3689     Type *Ty,               ///< The type to casted to
3690     const Twine &NameStr = "",    ///< A name for the new instruction
3691     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3692   );
3693
3694   /// \brief Constructor with insert-at-end-of-block semantics
3695   BitCastInst(
3696     Value *S,                     ///< The value to be casted
3697     Type *Ty,               ///< The type to casted to
3698     const Twine &NameStr,         ///< A name for the new instruction
3699     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3700   );
3701
3702   // Methods for support type inquiry through isa, cast, and dyn_cast:
3703   static inline bool classof(const Instruction *I) {
3704     return I->getOpcode() == BitCast;
3705   }
3706   static inline bool classof(const Value *V) {
3707     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3708   }
3709 };
3710
3711 } // End llvm namespace
3712
3713 #endif