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