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