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