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