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