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