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