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