Add another required #include for freestanding .h files.
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- 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 defines various meta classes of instructions that exist in the VM
11 // representation.  Specific concrete subclasses of these may be found in the
12 // i*.h files...
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
18
19 #include "llvm/Instruction.h"
20 #include "llvm/OperandTraits.h"
21 #include "llvm/Operator.h"
22 #include "llvm/DerivedTypes.h"
23
24 namespace llvm {
25
26 class LLVMContext;
27
28 //===----------------------------------------------------------------------===//
29 //                            TerminatorInst Class
30 //===----------------------------------------------------------------------===//
31
32 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
33 /// block.  Thus, these are all the flow control type of operations.
34 ///
35 class TerminatorInst : public Instruction {
36 protected:
37   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
38                  Use *Ops, unsigned NumOps,
39                  Instruction *InsertBefore = 0)
40     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
41
42   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
43                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
44     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
45
46   // Out of line virtual method, so the vtable, etc has a home.
47   ~TerminatorInst();
48
49   /// Virtual methods - Terminators should overload these and provide inline
50   /// overrides of non-V methods.
51   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
52   virtual unsigned getNumSuccessorsV() const = 0;
53   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
54 public:
55
56   virtual TerminatorInst *clone() const = 0;
57
58   /// getNumSuccessors - Return the number of successors that this terminator
59   /// has.
60   unsigned getNumSuccessors() const {
61     return getNumSuccessorsV();
62   }
63
64   /// getSuccessor - Return the specified successor.
65   ///
66   BasicBlock *getSuccessor(unsigned idx) const {
67     return getSuccessorV(idx);
68   }
69
70   /// setSuccessor - Update the specified successor to point at the provided
71   /// block.
72   void setSuccessor(unsigned idx, BasicBlock *B) {
73     setSuccessorV(idx, B);
74   }
75
76   // Methods for support type inquiry through isa, cast, and dyn_cast:
77   static inline bool classof(const TerminatorInst *) { return true; }
78   static inline bool classof(const Instruction *I) {
79     return I->isTerminator();
80   }
81   static inline bool classof(const Value *V) {
82     return isa<Instruction>(V) && classof(cast<Instruction>(V));
83   }
84 };
85
86
87 //===----------------------------------------------------------------------===//
88 //                          UnaryInstruction Class
89 //===----------------------------------------------------------------------===//
90
91 class UnaryInstruction : public Instruction {
92   void *operator new(size_t, unsigned);      // Do not implement
93
94 protected:
95   UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
96                    Instruction *IB = 0)
97     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
98     Op<0>() = V;
99   }
100   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
101     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
102     Op<0>() = V;
103   }
104 public:
105   // allocate space for exactly one operand
106   void *operator new(size_t s) {
107     return User::operator new(s, 1);
108   }
109
110   // Out of line virtual method, so the vtable, etc has a home.
111   ~UnaryInstruction();
112
113   /// Transparently provide more efficient getOperand methods.
114   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
115
116   // Methods for support type inquiry through isa, cast, and dyn_cast:
117   static inline bool classof(const UnaryInstruction *) { return true; }
118   static inline bool classof(const Instruction *I) {
119     return I->getOpcode() == Instruction::Alloca ||
120            I->getOpcode() == Instruction::Free ||
121            I->getOpcode() == Instruction::Load ||
122            I->getOpcode() == Instruction::VAArg ||
123            I->getOpcode() == Instruction::ExtractValue ||
124            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
125   }
126   static inline bool classof(const Value *V) {
127     return isa<Instruction>(V) && classof(cast<Instruction>(V));
128   }
129 };
130
131 template <>
132 struct OperandTraits<UnaryInstruction> : public FixedNumOperandTraits<1> {
133 };
134
135 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
136
137 //===----------------------------------------------------------------------===//
138 //                           BinaryOperator Class
139 //===----------------------------------------------------------------------===//
140
141 class BinaryOperator : public Instruction {
142   void *operator new(size_t, unsigned); // Do not implement
143 protected:
144   void init(BinaryOps iType);
145   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146                  const Twine &Name, Instruction *InsertBefore);
147   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
148                  const Twine &Name, BasicBlock *InsertAtEnd);
149 public:
150   // allocate space for exactly two operands
151   void *operator new(size_t s) {
152     return User::operator new(s, 2);
153   }
154
155   /// Transparently provide more efficient getOperand methods.
156   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157
158   /// Create() - Construct a binary instruction, given the opcode and the two
159   /// operands.  Optionally (if InstBefore is specified) insert the instruction
160   /// into a BasicBlock right before the specified instruction.  The specified
161   /// Instruction is allowed to be a dereferenced end iterator.
162   ///
163   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
164                                 const Twine &Name = "",
165                                 Instruction *InsertBefore = 0);
166
167   /// Create() - Construct a binary instruction, given the opcode and the two
168   /// operands.  Also automatically insert this instruction to the end of the
169   /// BasicBlock specified.
170   ///
171   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
172                                 const Twine &Name, BasicBlock *InsertAtEnd);
173
174   /// Create* - These methods just forward to Create, and are useful when you
175   /// statically know what type of instruction you're going to create.  These
176   /// helpers just save some typing.
177 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
178   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
179                                      const Twine &Name = "") {\
180     return Create(Instruction::OPC, V1, V2, Name);\
181   }
182 #include "llvm/Instruction.def"
183 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
184   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
185                                      const Twine &Name, BasicBlock *BB) {\
186     return Create(Instruction::OPC, V1, V2, Name, BB);\
187   }
188 #include "llvm/Instruction.def"
189 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
190   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
191                                      const Twine &Name, Instruction *I) {\
192     return Create(Instruction::OPC, V1, V2, Name, I);\
193   }
194 #include "llvm/Instruction.def"
195
196
197   /// CreateNSWAdd - Create an Add operator with the NSW flag set.
198   ///
199   static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
200                                       const Twine &Name = "") {
201     BinaryOperator *BO = CreateAdd(V1, V2, Name);
202     BO->setHasNoSignedWrap(true);
203     return BO;
204   }
205   static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
206                                       const Twine &Name, BasicBlock *BB) {
207     BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
208     BO->setHasNoSignedWrap(true);
209     return BO;
210   }
211   static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
212                                       const Twine &Name, Instruction *I) {
213     BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
214     BO->setHasNoSignedWrap(true);
215     return BO;
216   }
217
218   /// CreateNSWSub - Create an Sub operator with the NSW flag set.
219   ///
220   static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
221                                       const Twine &Name = "") {
222     BinaryOperator *BO = CreateSub(V1, V2, Name);
223     BO->setHasNoSignedWrap(true);
224     return BO;
225   }
226   static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
227                                       const Twine &Name, BasicBlock *BB) {
228     BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
229     BO->setHasNoSignedWrap(true);
230     return BO;
231   }
232   static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
233                                       const Twine &Name, Instruction *I) {
234     BinaryOperator *BO = CreateSub(V1, V2, Name, I);
235     BO->setHasNoSignedWrap(true);
236     return BO;
237   }
238
239   /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
240   ///
241   static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
242                                          const Twine &Name = "") {
243     BinaryOperator *BO = CreateSDiv(V1, V2, Name);
244     BO->setIsExact(true);
245     return BO;
246   }
247   static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
248                                          const Twine &Name, BasicBlock *BB) {
249     BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
250     BO->setIsExact(true);
251     return BO;
252   }
253   static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
254                                          const Twine &Name, Instruction *I) {
255     BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
256     BO->setIsExact(true);
257     return BO;
258   }
259
260   /// Helper functions to construct and inspect unary operations (NEG and NOT)
261   /// via binary operators SUB and XOR:
262   ///
263   /// CreateNeg, CreateNot - Create the NEG and NOT
264   ///     instructions out of SUB and XOR instructions.
265   ///
266   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
267                                    Instruction *InsertBefore = 0);
268   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
269                                    BasicBlock *InsertAtEnd);
270   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
271                                     Instruction *InsertBefore = 0);
272   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
273                                     BasicBlock *InsertAtEnd);
274   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
275                                    Instruction *InsertBefore = 0);
276   static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
277                                    BasicBlock *InsertAtEnd);
278
279   /// isNeg, isFNeg, isNot - Check if the given Value is a
280   /// NEG, FNeg, or NOT instruction.
281   ///
282   static bool isNeg(const Value *V);
283   static bool isFNeg(const Value *V);
284   static bool isNot(const Value *V);
285
286   /// getNegArgument, getNotArgument - Helper functions to extract the
287   ///     unary argument of a NEG, FNEG or NOT operation implemented via
288   ///     Sub, FSub, or Xor.
289   ///
290   static const Value *getNegArgument(const Value *BinOp);
291   static       Value *getNegArgument(      Value *BinOp);
292   static const Value *getFNegArgument(const Value *BinOp);
293   static       Value *getFNegArgument(      Value *BinOp);
294   static const Value *getNotArgument(const Value *BinOp);
295   static       Value *getNotArgument(      Value *BinOp);
296
297   BinaryOps getOpcode() const {
298     return static_cast<BinaryOps>(Instruction::getOpcode());
299   }
300
301   virtual BinaryOperator *clone() const;
302
303   /// swapOperands - Exchange the two operands to this instruction.
304   /// This instruction is safe to use on any binary instruction and
305   /// does not modify the semantics of the instruction.  If the instruction
306   /// cannot be reversed (ie, it's a Div), then return true.
307   ///
308   bool swapOperands();
309
310   /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
311   /// which must be an operator which supports this flag. See LangRef.html
312   /// for the meaning of this flag.
313   void setHasNoUnsignedWrap(bool b = true);
314
315   /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
316   /// which must be an operator which supports this flag. See LangRef.html
317   /// for the meaning of this flag.
318   void setHasNoSignedWrap(bool b = true);
319
320   /// setIsExact - Set or clear the exact flag on this instruction,
321   /// which must be an operator which supports this flag. See LangRef.html
322   /// for the meaning of this flag.
323   void setIsExact(bool b = true);
324
325   /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
326   bool hasNoUnsignedWrap() const;
327
328   /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
329   bool hasNoSignedWrap() const;
330
331   /// isExact - Determine whether the exact flag is set.
332   bool isExact() const;
333
334   // Methods for support type inquiry through isa, cast, and dyn_cast:
335   static inline bool classof(const BinaryOperator *) { return true; }
336   static inline bool classof(const Instruction *I) {
337     return I->isBinaryOp();
338   }
339   static inline bool classof(const Value *V) {
340     return isa<Instruction>(V) && classof(cast<Instruction>(V));
341   }
342 };
343
344 template <>
345 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
346 };
347
348 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
349
350 //===----------------------------------------------------------------------===//
351 //                               CastInst Class
352 //===----------------------------------------------------------------------===//
353
354 /// CastInst - This is the base class for all instructions that perform data
355 /// casts. It is simply provided so that instruction category testing
356 /// can be performed with code like:
357 ///
358 /// if (isa<CastInst>(Instr)) { ... }
359 /// @brief Base class of casting instructions.
360 class CastInst : public UnaryInstruction {
361 protected:
362   /// @brief Constructor with insert-before-instruction semantics for subclasses
363   CastInst(const Type *Ty, unsigned iType, Value *S,
364            const Twine &NameStr = "", Instruction *InsertBefore = 0)
365     : UnaryInstruction(Ty, iType, S, InsertBefore) {
366     setName(NameStr);
367   }
368   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
369   CastInst(const Type *Ty, unsigned iType, Value *S,
370            const Twine &NameStr, BasicBlock *InsertAtEnd)
371     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
372     setName(NameStr);
373   }
374 public:
375   /// Provides a way to construct any of the CastInst subclasses using an
376   /// opcode instead of the subclass's constructor. The opcode must be in the
377   /// CastOps category (Instruction::isCast(opcode) returns true). This
378   /// constructor has insert-before-instruction semantics to automatically
379   /// insert the new CastInst before InsertBefore (if it is non-null).
380   /// @brief Construct any of the CastInst subclasses
381   static CastInst *Create(
382     Instruction::CastOps,    ///< The opcode of the cast instruction
383     Value *S,                ///< The value to be casted (operand 0)
384     const Type *Ty,          ///< The type to which cast should be made
385     const Twine &Name = "", ///< Name for the instruction
386     Instruction *InsertBefore = 0 ///< Place to insert the instruction
387   );
388   /// Provides a way to construct any of the CastInst subclasses using an
389   /// opcode instead of the subclass's constructor. The opcode must be in the
390   /// CastOps category. This constructor has insert-at-end-of-block semantics
391   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
392   /// its non-null).
393   /// @brief Construct any of the CastInst subclasses
394   static CastInst *Create(
395     Instruction::CastOps,    ///< The opcode for the cast instruction
396     Value *S,                ///< The value to be casted (operand 0)
397     const Type *Ty,          ///< The type to which operand is casted
398     const Twine &Name, ///< The name for the instruction
399     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
400   );
401
402   /// @brief Create a ZExt or BitCast cast instruction
403   static CastInst *CreateZExtOrBitCast(
404     Value *S,                ///< The value to be casted (operand 0)
405     const Type *Ty,          ///< The type to which cast should be made
406     const Twine &Name = "", ///< Name for the instruction
407     Instruction *InsertBefore = 0 ///< Place to insert the instruction
408   );
409
410   /// @brief Create a ZExt or BitCast cast instruction
411   static CastInst *CreateZExtOrBitCast(
412     Value *S,                ///< The value to be casted (operand 0)
413     const Type *Ty,          ///< The type to which operand is casted
414     const Twine &Name, ///< The name for the instruction
415     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
416   );
417
418   /// @brief Create a SExt or BitCast cast instruction
419   static CastInst *CreateSExtOrBitCast(
420     Value *S,                ///< The value to be casted (operand 0)
421     const Type *Ty,          ///< The type to which cast should be made
422     const Twine &Name = "", ///< Name for the instruction
423     Instruction *InsertBefore = 0 ///< Place to insert the instruction
424   );
425
426   /// @brief Create a SExt or BitCast cast instruction
427   static CastInst *CreateSExtOrBitCast(
428     Value *S,                ///< The value to be casted (operand 0)
429     const Type *Ty,          ///< The type to which operand is casted
430     const Twine &Name, ///< The name for the instruction
431     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
432   );
433
434   /// @brief Create a BitCast or a PtrToInt cast instruction
435   static CastInst *CreatePointerCast(
436     Value *S,                ///< The pointer value to be casted (operand 0)
437     const Type *Ty,          ///< The type to which operand is casted
438     const Twine &Name, ///< The name for the instruction
439     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
440   );
441
442   /// @brief Create a BitCast or a PtrToInt cast instruction
443   static CastInst *CreatePointerCast(
444     Value *S,                ///< The pointer value to be casted (operand 0)
445     const Type *Ty,          ///< The type to which cast should be made
446     const Twine &Name = "", ///< Name for the instruction
447     Instruction *InsertBefore = 0 ///< Place to insert the instruction
448   );
449
450   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
451   static CastInst *CreateIntegerCast(
452     Value *S,                ///< The pointer value to be casted (operand 0)
453     const Type *Ty,          ///< The type to which cast should be made
454     bool isSigned,           ///< Whether to regard S as signed or not
455     const Twine &Name = "", ///< Name for the instruction
456     Instruction *InsertBefore = 0 ///< Place to insert the instruction
457   );
458
459   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
460   static CastInst *CreateIntegerCast(
461     Value *S,                ///< The integer value to be casted (operand 0)
462     const Type *Ty,          ///< The integer type to which operand is casted
463     bool isSigned,           ///< Whether to regard S as signed or not
464     const Twine &Name, ///< The name for the instruction
465     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
466   );
467
468   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
469   static CastInst *CreateFPCast(
470     Value *S,                ///< The floating point value to be casted
471     const Type *Ty,          ///< The floating point type to cast to
472     const Twine &Name = "", ///< Name for the instruction
473     Instruction *InsertBefore = 0 ///< Place to insert the instruction
474   );
475
476   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
477   static CastInst *CreateFPCast(
478     Value *S,                ///< The floating point value to be casted
479     const Type *Ty,          ///< The floating point type to cast to
480     const Twine &Name, ///< The name for the instruction
481     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
482   );
483
484   /// @brief Create a Trunc or BitCast cast instruction
485   static CastInst *CreateTruncOrBitCast(
486     Value *S,                ///< The value to be casted (operand 0)
487     const Type *Ty,          ///< The type to which cast should be made
488     const Twine &Name = "", ///< Name for the instruction
489     Instruction *InsertBefore = 0 ///< Place to insert the instruction
490   );
491
492   /// @brief Create a Trunc or BitCast cast instruction
493   static CastInst *CreateTruncOrBitCast(
494     Value *S,                ///< The value to be casted (operand 0)
495     const Type *Ty,          ///< The type to which operand is casted
496     const Twine &Name, ///< The name for the instruction
497     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
498   );
499
500   /// @brief Check whether it is valid to call getCastOpcode for these types.
501   static bool isCastable(
502     const Type *SrcTy, ///< The Type from which the value should be cast.
503     const Type *DestTy ///< The Type to which the value should be cast.
504   );
505
506   /// Returns the opcode necessary to cast Val into Ty using usual casting
507   /// rules.
508   /// @brief Infer the opcode for cast operand and type
509   static Instruction::CastOps getCastOpcode(
510     const Value *Val, ///< The value to cast
511     bool SrcIsSigned, ///< Whether to treat the source as signed
512     const Type *Ty,   ///< The Type to which the value should be casted
513     bool DstIsSigned  ///< Whether to treate the dest. as signed
514   );
515
516   /// There are several places where we need to know if a cast instruction
517   /// only deals with integer source and destination types. To simplify that
518   /// logic, this method is provided.
519   /// @returns true iff the cast has only integral typed operand and dest type.
520   /// @brief Determine if this is an integer-only cast.
521   bool isIntegerCast() const;
522
523   /// A lossless cast is one that does not alter the basic value. It implies
524   /// a no-op cast but is more stringent, preventing things like int->float,
525   /// long->double, int->ptr, or vector->anything.
526   /// @returns true iff the cast is lossless.
527   /// @brief Determine if this is a lossless cast.
528   bool isLosslessCast() const;
529
530   /// A no-op cast is one that can be effected without changing any bits.
531   /// It implies that the source and destination types are the same size. The
532   /// IntPtrTy argument is used to make accurate determinations for casts
533   /// involving Integer and Pointer types. They are no-op casts if the integer
534   /// is the same size as the pointer. However, pointer size varies with
535   /// platform. Generally, the result of TargetData::getIntPtrType() should be
536   /// passed in. If that's not available, use Type::Int64Ty, which will make
537   /// the isNoopCast call conservative.
538   /// @brief Determine if this cast is a no-op cast.
539   bool isNoopCast(
540     const Type *IntPtrTy ///< Integer type corresponding to pointer
541   ) const;
542
543   /// Determine how a pair of casts can be eliminated, if they can be at all.
544   /// This is a helper function for both CastInst and ConstantExpr.
545   /// @returns 0 if the CastInst pair can't be eliminated
546   /// @returns Instruction::CastOps value for a cast that can replace
547   /// the pair, casting SrcTy to DstTy.
548   /// @brief Determine if a cast pair is eliminable
549   static unsigned isEliminableCastPair(
550     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
551     Instruction::CastOps secondOpcode, ///< Opcode of second cast
552     const Type *SrcTy, ///< SrcTy of 1st cast
553     const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
554     const Type *DstTy, ///< DstTy of 2nd cast
555     const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
556   );
557
558   /// @brief Return the opcode of this CastInst
559   Instruction::CastOps getOpcode() const {
560     return Instruction::CastOps(Instruction::getOpcode());
561   }
562
563   /// @brief Return the source type, as a convenience
564   const Type* getSrcTy() const { return getOperand(0)->getType(); }
565   /// @brief Return the destination type, as a convenience
566   const Type* getDestTy() const { return getType(); }
567
568   /// This method can be used to determine if a cast from S to DstTy using
569   /// Opcode op is valid or not.
570   /// @returns true iff the proposed cast is valid.
571   /// @brief Determine if a cast is valid without creating one.
572   static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
573
574   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
575   static inline bool classof(const CastInst *) { return true; }
576   static inline bool classof(const Instruction *I) {
577     return I->isCast();
578   }
579   static inline bool classof(const Value *V) {
580     return isa<Instruction>(V) && classof(cast<Instruction>(V));
581   }
582 };
583
584 //===----------------------------------------------------------------------===//
585 //                               CmpInst Class
586 //===----------------------------------------------------------------------===//
587
588 /// This class is the base class for the comparison instructions.
589 /// @brief Abstract base class of comparison instructions.
590 // FIXME: why not derive from BinaryOperator?
591 class CmpInst: public Instruction {
592   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
593   CmpInst(); // do not implement
594 protected:
595   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
596           Value *LHS, Value *RHS, const Twine &Name = "",
597           Instruction *InsertBefore = 0);
598
599   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
600           Value *LHS, Value *RHS, const Twine &Name,
601           BasicBlock *InsertAtEnd);
602
603 public:
604   /// This enumeration lists the possible predicates for CmpInst subclasses.
605   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
606   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
607   /// predicate values are not overlapping between the classes.
608   enum Predicate {
609     // Opcode             U L G E    Intuitive operation
610     FCMP_FALSE =  0,  /// 0 0 0 0    Always false (always folded)
611     FCMP_OEQ   =  1,  /// 0 0 0 1    True if ordered and equal
612     FCMP_OGT   =  2,  /// 0 0 1 0    True if ordered and greater than
613     FCMP_OGE   =  3,  /// 0 0 1 1    True if ordered and greater than or equal
614     FCMP_OLT   =  4,  /// 0 1 0 0    True if ordered and less than
615     FCMP_OLE   =  5,  /// 0 1 0 1    True if ordered and less than or equal
616     FCMP_ONE   =  6,  /// 0 1 1 0    True if ordered and operands are unequal
617     FCMP_ORD   =  7,  /// 0 1 1 1    True if ordered (no nans)
618     FCMP_UNO   =  8,  /// 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
619     FCMP_UEQ   =  9,  /// 1 0 0 1    True if unordered or equal
620     FCMP_UGT   = 10,  /// 1 0 1 0    True if unordered or greater than
621     FCMP_UGE   = 11,  /// 1 0 1 1    True if unordered, greater than, or equal
622     FCMP_ULT   = 12,  /// 1 1 0 0    True if unordered or less than
623     FCMP_ULE   = 13,  /// 1 1 0 1    True if unordered, less than, or equal
624     FCMP_UNE   = 14,  /// 1 1 1 0    True if unordered or not equal
625     FCMP_TRUE  = 15,  /// 1 1 1 1    Always true (always folded)
626     FIRST_FCMP_PREDICATE = FCMP_FALSE,
627     LAST_FCMP_PREDICATE = FCMP_TRUE,
628     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
629     ICMP_EQ    = 32,  /// equal
630     ICMP_NE    = 33,  /// not equal
631     ICMP_UGT   = 34,  /// unsigned greater than
632     ICMP_UGE   = 35,  /// unsigned greater or equal
633     ICMP_ULT   = 36,  /// unsigned less than
634     ICMP_ULE   = 37,  /// unsigned less or equal
635     ICMP_SGT   = 38,  /// signed greater than
636     ICMP_SGE   = 39,  /// signed greater or equal
637     ICMP_SLT   = 40,  /// signed less than
638     ICMP_SLE   = 41,  /// signed less or equal
639     FIRST_ICMP_PREDICATE = ICMP_EQ,
640     LAST_ICMP_PREDICATE = ICMP_SLE,
641     BAD_ICMP_PREDICATE = ICMP_SLE + 1
642   };
643
644   // allocate space for exactly two operands
645   void *operator new(size_t s) {
646     return User::operator new(s, 2);
647   }
648   /// Construct a compare instruction, given the opcode, the predicate and
649   /// the two operands.  Optionally (if InstBefore is specified) insert the
650   /// instruction into a BasicBlock right before the specified instruction.
651   /// The specified Instruction is allowed to be a dereferenced end iterator.
652   /// @brief Create a CmpInst
653   static CmpInst *Create(OtherOps Op,
654                          unsigned short predicate, Value *S1,
655                          Value *S2, const Twine &Name = "",
656                          Instruction *InsertBefore = 0);
657
658   /// Construct a compare instruction, given the opcode, the predicate and the
659   /// two operands.  Also automatically insert this instruction to the end of
660   /// the BasicBlock specified.
661   /// @brief Create a CmpInst
662   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
663                          Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
664
665   /// @brief Get the opcode casted to the right type
666   OtherOps getOpcode() const {
667     return static_cast<OtherOps>(Instruction::getOpcode());
668   }
669
670   /// @brief Return the predicate for this instruction.
671   Predicate getPredicate() const { return Predicate(SubclassData); }
672
673   /// @brief Set the predicate for this instruction to the specified value.
674   void setPredicate(Predicate P) { SubclassData = P; }
675
676   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
677   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
678   /// @returns the inverse predicate for the instruction's current predicate.
679   /// @brief Return the inverse of the instruction's predicate.
680   Predicate getInversePredicate() const {
681     return getInversePredicate(getPredicate());
682   }
683
684   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
685   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
686   /// @returns the inverse predicate for predicate provided in \p pred.
687   /// @brief Return the inverse of a given predicate
688   static Predicate getInversePredicate(Predicate pred);
689
690   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
691   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
692   /// @returns the predicate that would be the result of exchanging the two
693   /// operands of the CmpInst instruction without changing the result
694   /// produced.
695   /// @brief Return the predicate as if the operands were swapped
696   Predicate getSwappedPredicate() const {
697     return getSwappedPredicate(getPredicate());
698   }
699
700   /// This is a static version that you can use without an instruction
701   /// available.
702   /// @brief Return the predicate as if the operands were swapped.
703   static Predicate getSwappedPredicate(Predicate pred);
704
705   /// @brief Provide more efficient getOperand methods.
706   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
707
708   /// This is just a convenience that dispatches to the subclasses.
709   /// @brief Swap the operands and adjust predicate accordingly to retain
710   /// the same comparison.
711   void swapOperands();
712
713   /// This is just a convenience that dispatches to the subclasses.
714   /// @brief Determine if this CmpInst is commutative.
715   bool isCommutative();
716
717   /// This is just a convenience that dispatches to the subclasses.
718   /// @brief Determine if this is an equals/not equals predicate.
719   bool isEquality();
720
721   /// @returns true if the predicate is unsigned, false otherwise.
722   /// @brief Determine if the predicate is an unsigned operation.
723   static bool isUnsigned(unsigned short predicate);
724
725   /// @returns true if the predicate is signed, false otherwise.
726   /// @brief Determine if the predicate is an signed operation.
727   static bool isSigned(unsigned short predicate);
728
729   /// @brief Determine if the predicate is an ordered operation.
730   static bool isOrdered(unsigned short predicate);
731
732   /// @brief Determine if the predicate is an unordered operation.
733   static bool isUnordered(unsigned short predicate);
734
735   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
736   static inline bool classof(const CmpInst *) { return true; }
737   static inline bool classof(const Instruction *I) {
738     return I->getOpcode() == Instruction::ICmp ||
739            I->getOpcode() == Instruction::FCmp;
740   }
741   static inline bool classof(const Value *V) {
742     return isa<Instruction>(V) && classof(cast<Instruction>(V));
743   }
744   
745   /// @brief Create a result type for fcmp/icmp
746   static const Type* makeCmpResultType(const Type* opnd_type) {
747     if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
748       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
749                              vt->getNumElements());
750     }
751     return Type::getInt1Ty(opnd_type->getContext());
752   }
753 };
754
755
756 // FIXME: these are redundant if CmpInst < BinaryOperator
757 template <>
758 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
759 };
760
761 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
762
763 } // End llvm namespace
764
765 #endif