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