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