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