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