LangRef and basic memory-representation/reading/writing for 'cmpxchg' and
[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 //                           LandingPadInst Class
2033 //===----------------------------------------------------------------------===//
2034
2035 //===---------------------------------------------------------------------------
2036 /// LandingPadInst - The landingpad instruction holds all of the information
2037 /// necessary to generate correct exception handling. The landingpad instruction
2038 /// cannot be moved from the top of a landing pad block, which itself is
2039 /// accessible only from the 'unwind' edge of an invoke.
2040 ///
2041 class LandingPadInst : public Instruction {
2042   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2043   /// the number actually in use.
2044   unsigned ReservedSpace;
2045
2046   /// IsCleanup - True if the landingpad instruction is also a cleanup.
2047   bool IsCleanup;
2048   LandingPadInst(const LandingPadInst &LP);
2049 public:
2050   enum ClauseType { Catch, Filter };
2051 private:
2052   /// ClauseIdxs - This indexes into the OperandList, indicating what the
2053   /// values are at a given index.
2054   SmallVector<ClauseType, 8> ClauseIdxs;
2055
2056   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2057   // Allocate space for exactly zero operands.
2058   void *operator new(size_t s) {
2059     return User::operator new(s, 0);
2060   }
2061   void growOperands();
2062   void init(Function *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2063
2064   explicit LandingPadInst(Type *RetTy, Function *PersonalityFn,
2065                           unsigned NumReservedValues, const Twine &NameStr,
2066                           Instruction *InsertBefore)
2067     : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore),
2068       IsCleanup(false) {
2069     init(PersonalityFn, 1 + NumReservedValues, NameStr);
2070   }
2071   explicit LandingPadInst(Type *RetTy, Function *PersonalityFn,
2072                           unsigned NumReservedValues, const Twine &NameStr,
2073                           BasicBlock *InsertAtEnd)
2074     : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd),
2075       IsCleanup(false) {
2076     init(PersonalityFn, 1 + NumReservedValues, NameStr);
2077   }
2078 protected:
2079   virtual LandingPadInst *clone_impl() const;
2080 public:
2081   static LandingPadInst *Create(Type *RetTy, Function *PersonalityFn,
2082                                 unsigned NumReservedValues,
2083                                 const Twine &NameStr = "",
2084                                 Instruction *InsertBefore = 0) {
2085     return new LandingPadInst(RetTy, PersonalityFn, NumReservedValues, NameStr,
2086                               InsertBefore);
2087   }
2088   static LandingPadInst *Create(Type *RetTy, Function *PersonalityFn,
2089                                 unsigned NumReservedValues,
2090                                 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2091     return new LandingPadInst(RetTy, PersonalityFn, NumReservedValues, NameStr,
2092                               InsertAtEnd);
2093   }
2094   ~LandingPadInst();
2095
2096   /// Provide fast operand accessors
2097   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2098
2099   /// getPersonalityFn - Get the personality function associated with this
2100   /// landing pad.
2101   const Function *getPersonalityFn() const {
2102     return cast<Function>(getOperand(0));
2103   }
2104
2105   // Simple accessors.
2106   bool isCleanup() const { return IsCleanup; }
2107   void setCleanup(bool Val) { IsCleanup = Val; }
2108
2109   /// addClause - Add a clause to the landing pad.
2110   void addClause(ClauseType CT, Constant *ClauseVal);
2111
2112   /// getClauseType - Return the type of the clause at this index. The two
2113   /// supported clauses are Catch and Filter.
2114   ClauseType getClauseType(unsigned I) const {
2115     assert(I < ClauseIdxs.size() && "Index too large!");
2116     return ClauseIdxs[I];
2117   }
2118
2119   /// getClauseValue - Return the value of the clause at this index.
2120   Constant *getClauseValue(unsigned I) const {
2121     assert(I + 1 < getNumOperands() && "Index too large!");
2122     return cast<Constant>(OperandList[I + 1]);
2123   }
2124
2125   /// getNumClauses - Get the number of clauses for this landing pad.
2126   unsigned getNumClauses() const { return getNumOperands() - 1; }
2127
2128   /// reserveClauses - Grow the size of the operand list to accomodate the new
2129   /// number of clauses.
2130   void reserveClauses(unsigned Size);
2131
2132   // Methods for support type inquiry through isa, cast, and dyn_cast:
2133   static inline bool classof(const LandingPadInst *) { return true; }
2134   static inline bool classof(const Instruction *I) {
2135     return I->getOpcode() == Instruction::LandingPad;
2136   }
2137   static inline bool classof(const Value *V) {
2138     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2139   }
2140 };
2141
2142 template <>
2143 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2144 };
2145
2146 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2147
2148 //===----------------------------------------------------------------------===//
2149 //                               ReturnInst Class
2150 //===----------------------------------------------------------------------===//
2151
2152 //===---------------------------------------------------------------------------
2153 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2154 /// does not continue in this function any longer.
2155 ///
2156 class ReturnInst : public TerminatorInst {
2157   ReturnInst(const ReturnInst &RI);
2158
2159 private:
2160   // ReturnInst constructors:
2161   // ReturnInst()                  - 'ret void' instruction
2162   // ReturnInst(    null)          - 'ret void' instruction
2163   // ReturnInst(Value* X)          - 'ret X'    instruction
2164   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2165   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2166   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2167   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2168   //
2169   // NOTE: If the Value* passed is of type void then the constructor behaves as
2170   // if it was passed NULL.
2171   explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2172                       Instruction *InsertBefore = 0);
2173   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2174   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2175 protected:
2176   virtual ReturnInst *clone_impl() const;
2177 public:
2178   static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2179                             Instruction *InsertBefore = 0) {
2180     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2181   }
2182   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2183                             BasicBlock *InsertAtEnd) {
2184     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2185   }
2186   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2187     return new(0) ReturnInst(C, InsertAtEnd);
2188   }
2189   virtual ~ReturnInst();
2190
2191   /// Provide fast operand accessors
2192   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2193
2194   /// Convenience accessor. Returns null if there is no return value.
2195   Value *getReturnValue() const {
2196     return getNumOperands() != 0 ? getOperand(0) : 0;
2197   }
2198
2199   unsigned getNumSuccessors() const { return 0; }
2200
2201   // Methods for support type inquiry through isa, cast, and dyn_cast:
2202   static inline bool classof(const ReturnInst *) { return true; }
2203   static inline bool classof(const Instruction *I) {
2204     return (I->getOpcode() == Instruction::Ret);
2205   }
2206   static inline bool classof(const Value *V) {
2207     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2208   }
2209  private:
2210   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2211   virtual unsigned getNumSuccessorsV() const;
2212   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2213 };
2214
2215 template <>
2216 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2217 };
2218
2219 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2220
2221 //===----------------------------------------------------------------------===//
2222 //                               BranchInst Class
2223 //===----------------------------------------------------------------------===//
2224
2225 //===---------------------------------------------------------------------------
2226 /// BranchInst - Conditional or Unconditional Branch instruction.
2227 ///
2228 class BranchInst : public TerminatorInst {
2229   /// Ops list - Branches are strange.  The operands are ordered:
2230   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2231   /// they don't have to check for cond/uncond branchness. These are mostly
2232   /// accessed relative from op_end().
2233   BranchInst(const BranchInst &BI);
2234   void AssertOK();
2235   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2236   // BranchInst(BB *B)                           - 'br B'
2237   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2238   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2239   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2240   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2241   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2242   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2243   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2244              Instruction *InsertBefore = 0);
2245   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2246   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2247              BasicBlock *InsertAtEnd);
2248 protected:
2249   virtual BranchInst *clone_impl() const;
2250 public:
2251   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2252     return new(1) BranchInst(IfTrue, InsertBefore);
2253   }
2254   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2255                             Value *Cond, Instruction *InsertBefore = 0) {
2256     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2257   }
2258   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2259     return new(1) BranchInst(IfTrue, InsertAtEnd);
2260   }
2261   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2262                             Value *Cond, BasicBlock *InsertAtEnd) {
2263     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2264   }
2265
2266   /// Transparently provide more efficient getOperand methods.
2267   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2268
2269   bool isUnconditional() const { return getNumOperands() == 1; }
2270   bool isConditional()   const { return getNumOperands() == 3; }
2271
2272   Value *getCondition() const {
2273     assert(isConditional() && "Cannot get condition of an uncond branch!");
2274     return Op<-3>();
2275   }
2276
2277   void setCondition(Value *V) {
2278     assert(isConditional() && "Cannot set condition of unconditional branch!");
2279     Op<-3>() = V;
2280   }
2281
2282   unsigned getNumSuccessors() const { return 1+isConditional(); }
2283
2284   BasicBlock *getSuccessor(unsigned i) const {
2285     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2286     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2287   }
2288
2289   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2290     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2291     *(&Op<-1>() - idx) = (Value*)NewSucc;
2292   }
2293
2294   // Methods for support type inquiry through isa, cast, and dyn_cast:
2295   static inline bool classof(const BranchInst *) { return true; }
2296   static inline bool classof(const Instruction *I) {
2297     return (I->getOpcode() == Instruction::Br);
2298   }
2299   static inline bool classof(const Value *V) {
2300     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2301   }
2302 private:
2303   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2304   virtual unsigned getNumSuccessorsV() const;
2305   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2306 };
2307
2308 template <>
2309 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2310 };
2311
2312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2313
2314 //===----------------------------------------------------------------------===//
2315 //                               SwitchInst Class
2316 //===----------------------------------------------------------------------===//
2317
2318 //===---------------------------------------------------------------------------
2319 /// SwitchInst - Multiway switch
2320 ///
2321 class SwitchInst : public TerminatorInst {
2322   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2323   unsigned ReservedSpace;
2324   // Operand[0]    = Value to switch on
2325   // Operand[1]    = Default basic block destination
2326   // Operand[2n  ] = Value to match
2327   // Operand[2n+1] = BasicBlock to go to on match
2328   SwitchInst(const SwitchInst &SI);
2329   void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2330   void growOperands();
2331   // allocate space for exactly zero operands
2332   void *operator new(size_t s) {
2333     return User::operator new(s, 0);
2334   }
2335   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2336   /// switch on and a default destination.  The number of additional cases can
2337   /// be specified here to make memory allocation more efficient.  This
2338   /// constructor can also autoinsert before another instruction.
2339   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2340              Instruction *InsertBefore);
2341
2342   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2343   /// switch on and a default destination.  The number of additional cases can
2344   /// be specified here to make memory allocation more efficient.  This
2345   /// constructor also autoinserts at the end of the specified BasicBlock.
2346   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2347              BasicBlock *InsertAtEnd);
2348 protected:
2349   virtual SwitchInst *clone_impl() const;
2350 public:
2351   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2352                             unsigned NumCases, Instruction *InsertBefore = 0) {
2353     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2354   }
2355   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2356                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2357     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2358   }
2359   ~SwitchInst();
2360
2361   /// Provide fast operand accessors
2362   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2363
2364   // Accessor Methods for Switch stmt
2365   Value *getCondition() const { return getOperand(0); }
2366   void setCondition(Value *V) { setOperand(0, V); }
2367
2368   BasicBlock *getDefaultDest() const {
2369     return cast<BasicBlock>(getOperand(1));
2370   }
2371
2372   /// getNumCases - return the number of 'cases' in this switch instruction.
2373   /// Note that case #0 is always the default case.
2374   unsigned getNumCases() const {
2375     return getNumOperands()/2;
2376   }
2377
2378   /// getCaseValue - Return the specified case value.  Note that case #0, the
2379   /// default destination, does not have a case value.
2380   ConstantInt *getCaseValue(unsigned i) {
2381     assert(i && i < getNumCases() && "Illegal case value to get!");
2382     return getSuccessorValue(i);
2383   }
2384
2385   /// getCaseValue - Return the specified case value.  Note that case #0, the
2386   /// default destination, does not have a case value.
2387   const ConstantInt *getCaseValue(unsigned i) const {
2388     assert(i && i < getNumCases() && "Illegal case value to get!");
2389     return getSuccessorValue(i);
2390   }
2391
2392   /// findCaseValue - Search all of the case values for the specified constant.
2393   /// If it is explicitly handled, return the case number of it, otherwise
2394   /// return 0 to indicate that it is handled by the default handler.
2395   unsigned findCaseValue(const ConstantInt *C) const {
2396     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2397       if (getCaseValue(i) == C)
2398         return i;
2399     return 0;
2400   }
2401
2402   /// findCaseDest - Finds the unique case value for a given successor. Returns
2403   /// null if the successor is not found, not unique, or is the default case.
2404   ConstantInt *findCaseDest(BasicBlock *BB) {
2405     if (BB == getDefaultDest()) return NULL;
2406
2407     ConstantInt *CI = NULL;
2408     for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2409       if (getSuccessor(i) == BB) {
2410         if (CI) return NULL;   // Multiple cases lead to BB.
2411         else CI = getCaseValue(i);
2412       }
2413     }
2414     return CI;
2415   }
2416
2417   /// addCase - Add an entry to the switch instruction...
2418   ///
2419   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2420
2421   /// removeCase - This method removes the specified successor from the switch
2422   /// instruction.  Note that this cannot be used to remove the default
2423   /// destination (successor #0). Also note that this operation may reorder the
2424   /// remaining cases at index idx and above.
2425   ///
2426   void removeCase(unsigned idx);
2427
2428   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2429   BasicBlock *getSuccessor(unsigned idx) const {
2430     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2431     return cast<BasicBlock>(getOperand(idx*2+1));
2432   }
2433   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2434     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2435     setOperand(idx*2+1, (Value*)NewSucc);
2436   }
2437
2438   // getSuccessorValue - Return the value associated with the specified
2439   // successor.
2440   ConstantInt *getSuccessorValue(unsigned idx) const {
2441     assert(idx < getNumSuccessors() && "Successor # out of range!");
2442     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2443   }
2444
2445   // Methods for support type inquiry through isa, cast, and dyn_cast:
2446   static inline bool classof(const SwitchInst *) { return true; }
2447   static inline bool classof(const Instruction *I) {
2448     return I->getOpcode() == Instruction::Switch;
2449   }
2450   static inline bool classof(const Value *V) {
2451     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2452   }
2453 private:
2454   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2455   virtual unsigned getNumSuccessorsV() const;
2456   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2457 };
2458
2459 template <>
2460 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2461 };
2462
2463 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2464
2465
2466 //===----------------------------------------------------------------------===//
2467 //                             IndirectBrInst Class
2468 //===----------------------------------------------------------------------===//
2469
2470 //===---------------------------------------------------------------------------
2471 /// IndirectBrInst - Indirect Branch Instruction.
2472 ///
2473 class IndirectBrInst : public TerminatorInst {
2474   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2475   unsigned ReservedSpace;
2476   // Operand[0]    = Value to switch on
2477   // Operand[1]    = Default basic block destination
2478   // Operand[2n  ] = Value to match
2479   // Operand[2n+1] = BasicBlock to go to on match
2480   IndirectBrInst(const IndirectBrInst &IBI);
2481   void init(Value *Address, unsigned NumDests);
2482   void growOperands();
2483   // allocate space for exactly zero operands
2484   void *operator new(size_t s) {
2485     return User::operator new(s, 0);
2486   }
2487   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2488   /// Address to jump to.  The number of expected destinations can be specified
2489   /// here to make memory allocation more efficient.  This constructor can also
2490   /// autoinsert before another instruction.
2491   IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2492
2493   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2494   /// Address to jump to.  The number of expected destinations can be specified
2495   /// here to make memory allocation more efficient.  This constructor also
2496   /// autoinserts at the end of the specified BasicBlock.
2497   IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2498 protected:
2499   virtual IndirectBrInst *clone_impl() const;
2500 public:
2501   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2502                                 Instruction *InsertBefore = 0) {
2503     return new IndirectBrInst(Address, NumDests, InsertBefore);
2504   }
2505   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2506                                 BasicBlock *InsertAtEnd) {
2507     return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2508   }
2509   ~IndirectBrInst();
2510
2511   /// Provide fast operand accessors.
2512   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2513
2514   // Accessor Methods for IndirectBrInst instruction.
2515   Value *getAddress() { return getOperand(0); }
2516   const Value *getAddress() const { return getOperand(0); }
2517   void setAddress(Value *V) { setOperand(0, V); }
2518
2519
2520   /// getNumDestinations - return the number of possible destinations in this
2521   /// indirectbr instruction.
2522   unsigned getNumDestinations() const { return getNumOperands()-1; }
2523
2524   /// getDestination - Return the specified destination.
2525   BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2526   const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2527
2528   /// addDestination - Add a destination.
2529   ///
2530   void addDestination(BasicBlock *Dest);
2531
2532   /// removeDestination - This method removes the specified successor from the
2533   /// indirectbr instruction.
2534   void removeDestination(unsigned i);
2535
2536   unsigned getNumSuccessors() const { return getNumOperands()-1; }
2537   BasicBlock *getSuccessor(unsigned i) const {
2538     return cast<BasicBlock>(getOperand(i+1));
2539   }
2540   void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2541     setOperand(i+1, (Value*)NewSucc);
2542   }
2543
2544   // Methods for support type inquiry through isa, cast, and dyn_cast:
2545   static inline bool classof(const IndirectBrInst *) { return true; }
2546   static inline bool classof(const Instruction *I) {
2547     return I->getOpcode() == Instruction::IndirectBr;
2548   }
2549   static inline bool classof(const Value *V) {
2550     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2551   }
2552 private:
2553   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2554   virtual unsigned getNumSuccessorsV() const;
2555   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2556 };
2557
2558 template <>
2559 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2560 };
2561
2562 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2563
2564
2565 //===----------------------------------------------------------------------===//
2566 //                               InvokeInst Class
2567 //===----------------------------------------------------------------------===//
2568
2569 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2570 /// calling convention of the call.
2571 ///
2572 class InvokeInst : public TerminatorInst {
2573   AttrListPtr AttributeList;
2574   InvokeInst(const InvokeInst &BI);
2575   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2576             ArrayRef<Value *> Args, const Twine &NameStr);
2577
2578   /// Construct an InvokeInst given a range of arguments.
2579   ///
2580   /// @brief Construct an InvokeInst from a range of arguments
2581   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2582                     ArrayRef<Value *> Args, unsigned Values,
2583                     const Twine &NameStr, Instruction *InsertBefore);
2584
2585   /// Construct an InvokeInst given a range of arguments.
2586   ///
2587   /// @brief Construct an InvokeInst from a range of arguments
2588   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2589                     ArrayRef<Value *> Args, unsigned Values,
2590                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2591 protected:
2592   virtual InvokeInst *clone_impl() const;
2593 public:
2594   static InvokeInst *Create(Value *Func,
2595                             BasicBlock *IfNormal, BasicBlock *IfException,
2596                             ArrayRef<Value *> Args, const Twine &NameStr = "",
2597                             Instruction *InsertBefore = 0) {
2598     unsigned Values = unsigned(Args.size()) + 3;
2599     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2600                                   Values, NameStr, InsertBefore);
2601   }
2602   static InvokeInst *Create(Value *Func,
2603                             BasicBlock *IfNormal, BasicBlock *IfException,
2604                             ArrayRef<Value *> Args, const Twine &NameStr,
2605                             BasicBlock *InsertAtEnd) {
2606     unsigned Values = unsigned(Args.size()) + 3;
2607     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2608                                   Values, NameStr, InsertAtEnd);
2609   }
2610
2611   /// Provide fast operand accessors
2612   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2613
2614   /// getNumArgOperands - Return the number of invoke arguments.
2615   ///
2616   unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2617
2618   /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2619   ///
2620   Value *getArgOperand(unsigned i) const { return getOperand(i); }
2621   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2622
2623   /// getCallingConv/setCallingConv - Get or set the calling convention of this
2624   /// function call.
2625   CallingConv::ID getCallingConv() const {
2626     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2627   }
2628   void setCallingConv(CallingConv::ID CC) {
2629     setInstructionSubclassData(static_cast<unsigned>(CC));
2630   }
2631
2632   /// getAttributes - Return the parameter attributes for this invoke.
2633   ///
2634   const AttrListPtr &getAttributes() const { return AttributeList; }
2635
2636   /// setAttributes - Set the parameter attributes for this invoke.
2637   ///
2638   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2639
2640   /// addAttribute - adds the attribute to the list of attributes.
2641   void addAttribute(unsigned i, Attributes attr);
2642
2643   /// removeAttribute - removes the attribute from the list of attributes.
2644   void removeAttribute(unsigned i, Attributes attr);
2645
2646   /// @brief Determine whether the call or the callee has the given attribute.
2647   bool paramHasAttr(unsigned i, Attributes attr) const;
2648
2649   /// @brief Extract the alignment for a call or parameter (0=unknown).
2650   unsigned getParamAlignment(unsigned i) const {
2651     return AttributeList.getParamAlignment(i);
2652   }
2653
2654   /// @brief Return true if the call should not be inlined.
2655   bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2656   void setIsNoInline(bool Value = true) {
2657     if (Value) addAttribute(~0, Attribute::NoInline);
2658     else removeAttribute(~0, Attribute::NoInline);
2659   }
2660
2661   /// @brief Determine if the call does not access memory.
2662   bool doesNotAccessMemory() const {
2663     return paramHasAttr(~0, Attribute::ReadNone);
2664   }
2665   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2666     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2667     else removeAttribute(~0, Attribute::ReadNone);
2668   }
2669
2670   /// @brief Determine if the call does not access or only reads memory.
2671   bool onlyReadsMemory() const {
2672     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2673   }
2674   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2675     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2676     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2677   }
2678
2679   /// @brief Determine if the call cannot return.
2680   bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
2681   void setDoesNotReturn(bool DoesNotReturn = true) {
2682     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2683     else removeAttribute(~0, Attribute::NoReturn);
2684   }
2685
2686   /// @brief Determine if the call cannot unwind.
2687   bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
2688   void setDoesNotThrow(bool DoesNotThrow = true) {
2689     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2690     else removeAttribute(~0, Attribute::NoUnwind);
2691   }
2692
2693   /// @brief Determine if the call returns a structure through first
2694   /// pointer argument.
2695   bool hasStructRetAttr() const {
2696     // Be friendly and also check the callee.
2697     return paramHasAttr(1, Attribute::StructRet);
2698   }
2699
2700   /// @brief Determine if any call argument is an aggregate passed by value.
2701   bool hasByValArgument() const {
2702     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2703   }
2704
2705   /// getCalledFunction - Return the function called, or null if this is an
2706   /// indirect function invocation.
2707   ///
2708   Function *getCalledFunction() const {
2709     return dyn_cast<Function>(Op<-3>());
2710   }
2711
2712   /// getCalledValue - Get a pointer to the function that is invoked by this
2713   /// instruction
2714   const Value *getCalledValue() const { return Op<-3>(); }
2715         Value *getCalledValue()       { return Op<-3>(); }
2716
2717   /// setCalledFunction - Set the function called.
2718   void setCalledFunction(Value* Fn) {
2719     Op<-3>() = Fn;
2720   }
2721
2722   // get*Dest - Return the destination basic blocks...
2723   BasicBlock *getNormalDest() const {
2724     return cast<BasicBlock>(Op<-2>());
2725   }
2726   BasicBlock *getUnwindDest() const {
2727     return cast<BasicBlock>(Op<-1>());
2728   }
2729   void setNormalDest(BasicBlock *B) {
2730     Op<-2>() = reinterpret_cast<Value*>(B);
2731   }
2732   void setUnwindDest(BasicBlock *B) {
2733     Op<-1>() = reinterpret_cast<Value*>(B);
2734   }
2735
2736   // getLandingPad - Get the landingpad instruction from the landing pad block
2737   // (the unwind destination).
2738   LandingPadInst *getLandingPad() const;
2739
2740   BasicBlock *getSuccessor(unsigned i) const {
2741     assert(i < 2 && "Successor # out of range for invoke!");
2742     return i == 0 ? getNormalDest() : getUnwindDest();
2743   }
2744
2745   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2746     assert(idx < 2 && "Successor # out of range for invoke!");
2747     *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
2748   }
2749
2750   unsigned getNumSuccessors() const { return 2; }
2751
2752   // Methods for support type inquiry through isa, cast, and dyn_cast:
2753   static inline bool classof(const InvokeInst *) { return true; }
2754   static inline bool classof(const Instruction *I) {
2755     return (I->getOpcode() == Instruction::Invoke);
2756   }
2757   static inline bool classof(const Value *V) {
2758     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2759   }
2760
2761 private:
2762   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2763   virtual unsigned getNumSuccessorsV() const;
2764   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2765
2766   // Shadow Instruction::setInstructionSubclassData with a private forwarding
2767   // method so that subclasses cannot accidentally use it.
2768   void setInstructionSubclassData(unsigned short D) {
2769     Instruction::setInstructionSubclassData(D);
2770   }
2771 };
2772
2773 template <>
2774 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
2775 };
2776
2777 InvokeInst::InvokeInst(Value *Func,
2778                        BasicBlock *IfNormal, BasicBlock *IfException,
2779                        ArrayRef<Value *> Args, unsigned Values,
2780                        const Twine &NameStr, Instruction *InsertBefore)
2781   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2782                                       ->getElementType())->getReturnType(),
2783                    Instruction::Invoke,
2784                    OperandTraits<InvokeInst>::op_end(this) - Values,
2785                    Values, InsertBefore) {
2786   init(Func, IfNormal, IfException, Args, NameStr);
2787 }
2788 InvokeInst::InvokeInst(Value *Func,
2789                        BasicBlock *IfNormal, BasicBlock *IfException,
2790                        ArrayRef<Value *> Args, unsigned Values,
2791                        const Twine &NameStr, BasicBlock *InsertAtEnd)
2792   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2793                                       ->getElementType())->getReturnType(),
2794                    Instruction::Invoke,
2795                    OperandTraits<InvokeInst>::op_end(this) - Values,
2796                    Values, InsertAtEnd) {
2797   init(Func, IfNormal, IfException, Args, NameStr);
2798 }
2799
2800 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2801
2802 //===----------------------------------------------------------------------===//
2803 //                              UnwindInst Class
2804 //===----------------------------------------------------------------------===//
2805
2806 //===---------------------------------------------------------------------------
2807 /// UnwindInst - Immediately exit the current function, unwinding the stack
2808 /// until an invoke instruction is found.
2809 ///
2810 class UnwindInst : public TerminatorInst {
2811   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2812 protected:
2813   virtual UnwindInst *clone_impl() const;
2814 public:
2815   // allocate space for exactly zero operands
2816   void *operator new(size_t s) {
2817     return User::operator new(s, 0);
2818   }
2819   explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2820   explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2821
2822   unsigned getNumSuccessors() const { return 0; }
2823
2824   // Methods for support type inquiry through isa, cast, and dyn_cast:
2825   static inline bool classof(const UnwindInst *) { return true; }
2826   static inline bool classof(const Instruction *I) {
2827     return I->getOpcode() == Instruction::Unwind;
2828   }
2829   static inline bool classof(const Value *V) {
2830     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2831   }
2832 private:
2833   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2834   virtual unsigned getNumSuccessorsV() const;
2835   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2836 };
2837
2838 //===----------------------------------------------------------------------===//
2839 //                              ResumeInst Class
2840 //===----------------------------------------------------------------------===//
2841
2842 //===---------------------------------------------------------------------------
2843 /// ResumeInst - Resume the propagation of an exception.
2844 ///
2845 class ResumeInst : public TerminatorInst {
2846   ResumeInst(const ResumeInst &RI);
2847
2848   explicit ResumeInst(LLVMContext &C, Value *Exn, Instruction *InsertBefore=0);
2849   ResumeInst(LLVMContext &C, Value *Exn, BasicBlock *InsertAtEnd);
2850 protected:
2851   virtual ResumeInst *clone_impl() const;
2852 public:
2853   static ResumeInst *Create(LLVMContext &C, Value *Exn,
2854                             Instruction *InsertBefore = 0) {
2855     return new(1) ResumeInst(C, Exn, InsertBefore);
2856   }
2857   static ResumeInst *Create(LLVMContext &C, Value *Exn,
2858                             BasicBlock *InsertAtEnd) {
2859     return new(1) ResumeInst(C, Exn, InsertAtEnd);
2860   }
2861
2862   /// Provide fast operand accessors
2863   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2864
2865   /// Convenience accessor.
2866   Value *getResumeValue() const { return Op<0>(); }
2867
2868   unsigned getNumSuccessors() const { return 0; }
2869
2870   // Methods for support type inquiry through isa, cast, and dyn_cast:
2871   static inline bool classof(const ResumeInst *) { return true; }
2872   static inline bool classof(const Instruction *I) {
2873     return I->getOpcode() == Instruction::Resume;
2874   }
2875   static inline bool classof(const Value *V) {
2876     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2877   }
2878 private:
2879   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2880   virtual unsigned getNumSuccessorsV() const;
2881   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2882 };
2883
2884 template <>
2885 struct OperandTraits<ResumeInst> :
2886     public FixedNumOperandTraits<ResumeInst, 1> {
2887 };
2888
2889 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
2890
2891 //===----------------------------------------------------------------------===//
2892 //                           UnreachableInst Class
2893 //===----------------------------------------------------------------------===//
2894
2895 //===---------------------------------------------------------------------------
2896 /// UnreachableInst - This function has undefined behavior.  In particular, the
2897 /// presence of this instruction indicates some higher level knowledge that the
2898 /// end of the block cannot be reached.
2899 ///
2900 class UnreachableInst : public TerminatorInst {
2901   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2902 protected:
2903   virtual UnreachableInst *clone_impl() const;
2904
2905 public:
2906   // allocate space for exactly zero operands
2907   void *operator new(size_t s) {
2908     return User::operator new(s, 0);
2909   }
2910   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2911   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2912
2913   unsigned getNumSuccessors() const { return 0; }
2914
2915   // Methods for support type inquiry through isa, cast, and dyn_cast:
2916   static inline bool classof(const UnreachableInst *) { return true; }
2917   static inline bool classof(const Instruction *I) {
2918     return I->getOpcode() == Instruction::Unreachable;
2919   }
2920   static inline bool classof(const Value *V) {
2921     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2922   }
2923 private:
2924   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2925   virtual unsigned getNumSuccessorsV() const;
2926   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2927 };
2928
2929 //===----------------------------------------------------------------------===//
2930 //                                 TruncInst Class
2931 //===----------------------------------------------------------------------===//
2932
2933 /// @brief This class represents a truncation of integer types.
2934 class TruncInst : public CastInst {
2935 protected:
2936   /// @brief Clone an identical TruncInst
2937   virtual TruncInst *clone_impl() const;
2938
2939 public:
2940   /// @brief Constructor with insert-before-instruction semantics
2941   TruncInst(
2942     Value *S,                     ///< The value to be truncated
2943     Type *Ty,               ///< The (smaller) type to truncate to
2944     const Twine &NameStr = "",    ///< A name for the new instruction
2945     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2946   );
2947
2948   /// @brief Constructor with insert-at-end-of-block semantics
2949   TruncInst(
2950     Value *S,                     ///< The value to be truncated
2951     Type *Ty,               ///< The (smaller) type to truncate to
2952     const Twine &NameStr,         ///< A name for the new instruction
2953     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2954   );
2955
2956   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2957   static inline bool classof(const TruncInst *) { return true; }
2958   static inline bool classof(const Instruction *I) {
2959     return I->getOpcode() == Trunc;
2960   }
2961   static inline bool classof(const Value *V) {
2962     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2963   }
2964 };
2965
2966 //===----------------------------------------------------------------------===//
2967 //                                 ZExtInst Class
2968 //===----------------------------------------------------------------------===//
2969
2970 /// @brief This class represents zero extension of integer types.
2971 class ZExtInst : public CastInst {
2972 protected:
2973   /// @brief Clone an identical ZExtInst
2974   virtual ZExtInst *clone_impl() const;
2975
2976 public:
2977   /// @brief Constructor with insert-before-instruction semantics
2978   ZExtInst(
2979     Value *S,                     ///< The value to be zero extended
2980     Type *Ty,               ///< The type to zero extend to
2981     const Twine &NameStr = "",    ///< A name for the new instruction
2982     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2983   );
2984
2985   /// @brief Constructor with insert-at-end semantics.
2986   ZExtInst(
2987     Value *S,                     ///< The value to be zero extended
2988     Type *Ty,               ///< The type to zero extend to
2989     const Twine &NameStr,         ///< A name for the new instruction
2990     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2991   );
2992
2993   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2994   static inline bool classof(const ZExtInst *) { return true; }
2995   static inline bool classof(const Instruction *I) {
2996     return I->getOpcode() == ZExt;
2997   }
2998   static inline bool classof(const Value *V) {
2999     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3000   }
3001 };
3002
3003 //===----------------------------------------------------------------------===//
3004 //                                 SExtInst Class
3005 //===----------------------------------------------------------------------===//
3006
3007 /// @brief This class represents a sign extension of integer types.
3008 class SExtInst : public CastInst {
3009 protected:
3010   /// @brief Clone an identical SExtInst
3011   virtual SExtInst *clone_impl() const;
3012
3013 public:
3014   /// @brief Constructor with insert-before-instruction semantics
3015   SExtInst(
3016     Value *S,                     ///< The value to be sign extended
3017     Type *Ty,               ///< The type to sign extend to
3018     const Twine &NameStr = "",    ///< A name for the new instruction
3019     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3020   );
3021
3022   /// @brief Constructor with insert-at-end-of-block semantics
3023   SExtInst(
3024     Value *S,                     ///< The value to be sign extended
3025     Type *Ty,               ///< The type to sign extend to
3026     const Twine &NameStr,         ///< A name for the new instruction
3027     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3028   );
3029
3030   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3031   static inline bool classof(const SExtInst *) { return true; }
3032   static inline bool classof(const Instruction *I) {
3033     return I->getOpcode() == SExt;
3034   }
3035   static inline bool classof(const Value *V) {
3036     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3037   }
3038 };
3039
3040 //===----------------------------------------------------------------------===//
3041 //                                 FPTruncInst Class
3042 //===----------------------------------------------------------------------===//
3043
3044 /// @brief This class represents a truncation of floating point types.
3045 class FPTruncInst : public CastInst {
3046 protected:
3047   /// @brief Clone an identical FPTruncInst
3048   virtual FPTruncInst *clone_impl() const;
3049
3050 public:
3051   /// @brief Constructor with insert-before-instruction semantics
3052   FPTruncInst(
3053     Value *S,                     ///< The value to be truncated
3054     Type *Ty,               ///< The type to truncate to
3055     const Twine &NameStr = "",    ///< A name for the new instruction
3056     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3057   );
3058
3059   /// @brief Constructor with insert-before-instruction semantics
3060   FPTruncInst(
3061     Value *S,                     ///< The value to be truncated
3062     Type *Ty,               ///< The type to truncate to
3063     const Twine &NameStr,         ///< A name for the new instruction
3064     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3065   );
3066
3067   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3068   static inline bool classof(const FPTruncInst *) { return true; }
3069   static inline bool classof(const Instruction *I) {
3070     return I->getOpcode() == FPTrunc;
3071   }
3072   static inline bool classof(const Value *V) {
3073     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3074   }
3075 };
3076
3077 //===----------------------------------------------------------------------===//
3078 //                                 FPExtInst Class
3079 //===----------------------------------------------------------------------===//
3080
3081 /// @brief This class represents an extension of floating point types.
3082 class FPExtInst : public CastInst {
3083 protected:
3084   /// @brief Clone an identical FPExtInst
3085   virtual FPExtInst *clone_impl() const;
3086
3087 public:
3088   /// @brief Constructor with insert-before-instruction semantics
3089   FPExtInst(
3090     Value *S,                     ///< The value to be extended
3091     Type *Ty,               ///< The type to extend to
3092     const Twine &NameStr = "",    ///< A name for the new instruction
3093     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3094   );
3095
3096   /// @brief Constructor with insert-at-end-of-block semantics
3097   FPExtInst(
3098     Value *S,                     ///< The value to be extended
3099     Type *Ty,               ///< The type to extend to
3100     const Twine &NameStr,         ///< A name for the new instruction
3101     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3102   );
3103
3104   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3105   static inline bool classof(const FPExtInst *) { return true; }
3106   static inline bool classof(const Instruction *I) {
3107     return I->getOpcode() == FPExt;
3108   }
3109   static inline bool classof(const Value *V) {
3110     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3111   }
3112 };
3113
3114 //===----------------------------------------------------------------------===//
3115 //                                 UIToFPInst Class
3116 //===----------------------------------------------------------------------===//
3117
3118 /// @brief This class represents a cast unsigned integer to floating point.
3119 class UIToFPInst : public CastInst {
3120 protected:
3121   /// @brief Clone an identical UIToFPInst
3122   virtual UIToFPInst *clone_impl() const;
3123
3124 public:
3125   /// @brief Constructor with insert-before-instruction semantics
3126   UIToFPInst(
3127     Value *S,                     ///< The value to be converted
3128     Type *Ty,               ///< The type to convert to
3129     const Twine &NameStr = "",    ///< A name for the new instruction
3130     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3131   );
3132
3133   /// @brief Constructor with insert-at-end-of-block semantics
3134   UIToFPInst(
3135     Value *S,                     ///< The value to be converted
3136     Type *Ty,               ///< The type to convert to
3137     const Twine &NameStr,         ///< A name for the new instruction
3138     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3139   );
3140
3141   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3142   static inline bool classof(const UIToFPInst *) { return true; }
3143   static inline bool classof(const Instruction *I) {
3144     return I->getOpcode() == UIToFP;
3145   }
3146   static inline bool classof(const Value *V) {
3147     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3148   }
3149 };
3150
3151 //===----------------------------------------------------------------------===//
3152 //                                 SIToFPInst Class
3153 //===----------------------------------------------------------------------===//
3154
3155 /// @brief This class represents a cast from signed integer to floating point.
3156 class SIToFPInst : public CastInst {
3157 protected:
3158   /// @brief Clone an identical SIToFPInst
3159   virtual SIToFPInst *clone_impl() const;
3160
3161 public:
3162   /// @brief Constructor with insert-before-instruction semantics
3163   SIToFPInst(
3164     Value *S,                     ///< The value to be converted
3165     Type *Ty,               ///< The type to convert to
3166     const Twine &NameStr = "",    ///< A name for the new instruction
3167     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3168   );
3169
3170   /// @brief Constructor with insert-at-end-of-block semantics
3171   SIToFPInst(
3172     Value *S,                     ///< The value to be converted
3173     Type *Ty,               ///< The type to convert to
3174     const Twine &NameStr,         ///< A name for the new instruction
3175     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3176   );
3177
3178   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3179   static inline bool classof(const SIToFPInst *) { return true; }
3180   static inline bool classof(const Instruction *I) {
3181     return I->getOpcode() == SIToFP;
3182   }
3183   static inline bool classof(const Value *V) {
3184     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3185   }
3186 };
3187
3188 //===----------------------------------------------------------------------===//
3189 //                                 FPToUIInst Class
3190 //===----------------------------------------------------------------------===//
3191
3192 /// @brief This class represents a cast from floating point to unsigned integer
3193 class FPToUIInst  : public CastInst {
3194 protected:
3195   /// @brief Clone an identical FPToUIInst
3196   virtual FPToUIInst *clone_impl() const;
3197
3198 public:
3199   /// @brief Constructor with insert-before-instruction semantics
3200   FPToUIInst(
3201     Value *S,                     ///< The value to be converted
3202     Type *Ty,               ///< The type to convert to
3203     const Twine &NameStr = "",    ///< A name for the new instruction
3204     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3205   );
3206
3207   /// @brief Constructor with insert-at-end-of-block semantics
3208   FPToUIInst(
3209     Value *S,                     ///< The value to be converted
3210     Type *Ty,               ///< The type to convert to
3211     const Twine &NameStr,         ///< A name for the new instruction
3212     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
3213   );
3214
3215   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3216   static inline bool classof(const FPToUIInst *) { return true; }
3217   static inline bool classof(const Instruction *I) {
3218     return I->getOpcode() == FPToUI;
3219   }
3220   static inline bool classof(const Value *V) {
3221     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3222   }
3223 };
3224
3225 //===----------------------------------------------------------------------===//
3226 //                                 FPToSIInst Class
3227 //===----------------------------------------------------------------------===//
3228
3229 /// @brief This class represents a cast from floating point to signed integer.
3230 class FPToSIInst  : public CastInst {
3231 protected:
3232   /// @brief Clone an identical FPToSIInst
3233   virtual FPToSIInst *clone_impl() const;
3234
3235 public:
3236   /// @brief Constructor with insert-before-instruction semantics
3237   FPToSIInst(
3238     Value *S,                     ///< The value to be converted
3239     Type *Ty,               ///< The type to convert to
3240     const Twine &NameStr = "",    ///< A name for the new instruction
3241     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3242   );
3243
3244   /// @brief Constructor with insert-at-end-of-block semantics
3245   FPToSIInst(
3246     Value *S,                     ///< The value to be converted
3247     Type *Ty,               ///< The type to convert to
3248     const Twine &NameStr,         ///< A name for the new instruction
3249     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3250   );
3251
3252   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3253   static inline bool classof(const FPToSIInst *) { return true; }
3254   static inline bool classof(const Instruction *I) {
3255     return I->getOpcode() == FPToSI;
3256   }
3257   static inline bool classof(const Value *V) {
3258     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3259   }
3260 };
3261
3262 //===----------------------------------------------------------------------===//
3263 //                                 IntToPtrInst Class
3264 //===----------------------------------------------------------------------===//
3265
3266 /// @brief This class represents a cast from an integer to a pointer.
3267 class IntToPtrInst : public CastInst {
3268 public:
3269   /// @brief Constructor with insert-before-instruction semantics
3270   IntToPtrInst(
3271     Value *S,                     ///< The value to be converted
3272     Type *Ty,               ///< The type to convert to
3273     const Twine &NameStr = "",    ///< A name for the new instruction
3274     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3275   );
3276
3277   /// @brief Constructor with insert-at-end-of-block semantics
3278   IntToPtrInst(
3279     Value *S,                     ///< The value to be converted
3280     Type *Ty,               ///< The type to convert to
3281     const Twine &NameStr,         ///< A name for the new instruction
3282     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3283   );
3284
3285   /// @brief Clone an identical IntToPtrInst
3286   virtual IntToPtrInst *clone_impl() const;
3287
3288   // Methods for support type inquiry through isa, cast, and dyn_cast:
3289   static inline bool classof(const IntToPtrInst *) { return true; }
3290   static inline bool classof(const Instruction *I) {
3291     return I->getOpcode() == IntToPtr;
3292   }
3293   static inline bool classof(const Value *V) {
3294     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3295   }
3296 };
3297
3298 //===----------------------------------------------------------------------===//
3299 //                                 PtrToIntInst Class
3300 //===----------------------------------------------------------------------===//
3301
3302 /// @brief This class represents a cast from a pointer to an integer
3303 class PtrToIntInst : public CastInst {
3304 protected:
3305   /// @brief Clone an identical PtrToIntInst
3306   virtual PtrToIntInst *clone_impl() const;
3307
3308 public:
3309   /// @brief Constructor with insert-before-instruction semantics
3310   PtrToIntInst(
3311     Value *S,                     ///< The value to be converted
3312     Type *Ty,               ///< The type to convert to
3313     const Twine &NameStr = "",    ///< A name for the new instruction
3314     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3315   );
3316
3317   /// @brief Constructor with insert-at-end-of-block semantics
3318   PtrToIntInst(
3319     Value *S,                     ///< The value to be converted
3320     Type *Ty,               ///< The type to convert to
3321     const Twine &NameStr,         ///< A name for the new instruction
3322     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3323   );
3324
3325   // Methods for support type inquiry through isa, cast, and dyn_cast:
3326   static inline bool classof(const PtrToIntInst *) { return true; }
3327   static inline bool classof(const Instruction *I) {
3328     return I->getOpcode() == PtrToInt;
3329   }
3330   static inline bool classof(const Value *V) {
3331     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3332   }
3333 };
3334
3335 //===----------------------------------------------------------------------===//
3336 //                             BitCastInst Class
3337 //===----------------------------------------------------------------------===//
3338
3339 /// @brief This class represents a no-op cast from one type to another.
3340 class BitCastInst : public CastInst {
3341 protected:
3342   /// @brief Clone an identical BitCastInst
3343   virtual BitCastInst *clone_impl() const;
3344
3345 public:
3346   /// @brief Constructor with insert-before-instruction semantics
3347   BitCastInst(
3348     Value *S,                     ///< The value to be casted
3349     Type *Ty,               ///< The type to casted to
3350     const Twine &NameStr = "",    ///< A name for the new instruction
3351     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3352   );
3353
3354   /// @brief Constructor with insert-at-end-of-block semantics
3355   BitCastInst(
3356     Value *S,                     ///< The value to be casted
3357     Type *Ty,               ///< The type to casted to
3358     const Twine &NameStr,         ///< A name for the new instruction
3359     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3360   );
3361
3362   // Methods for support type inquiry through isa, cast, and dyn_cast:
3363   static inline bool classof(const BitCastInst *) { return true; }
3364   static inline bool classof(const Instruction *I) {
3365     return I->getOpcode() == BitCast;
3366   }
3367   static inline bool classof(const Value *V) {
3368     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3369   }
3370 };
3371
3372 } // End llvm namespace
3373
3374 #endif