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