Fix a logic bug when copying fast-math flags.
[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   /// Convenience method to copy supported wrapping, exact, and fast-math flags
362   /// from V to this instruction.
363   void copyFlags(const Value *V);
364
365   // Methods for support type inquiry through isa, cast, and dyn_cast:
366   static inline bool classof(const Instruction *I) {
367     return I->isBinaryOp();
368   }
369   static inline bool classof(const Value *V) {
370     return isa<Instruction>(V) && classof(cast<Instruction>(V));
371   }
372 };
373
374 template <>
375 struct OperandTraits<BinaryOperator> :
376   public FixedNumOperandTraits<BinaryOperator, 2> {
377 };
378
379 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
380
381 //===----------------------------------------------------------------------===//
382 //                               CastInst Class
383 //===----------------------------------------------------------------------===//
384
385 /// CastInst - This is the base class for all instructions that perform data
386 /// casts. It is simply provided so that instruction category testing
387 /// can be performed with code like:
388 ///
389 /// if (isa<CastInst>(Instr)) { ... }
390 /// @brief Base class of casting instructions.
391 class CastInst : public UnaryInstruction {
392   void anchor() override;
393 protected:
394   /// @brief Constructor with insert-before-instruction semantics for subclasses
395   CastInst(Type *Ty, unsigned iType, Value *S,
396            const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
397     : UnaryInstruction(Ty, iType, S, InsertBefore) {
398     setName(NameStr);
399   }
400   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
401   CastInst(Type *Ty, unsigned iType, Value *S,
402            const Twine &NameStr, BasicBlock *InsertAtEnd)
403     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
404     setName(NameStr);
405   }
406 public:
407   /// Provides a way to construct any of the CastInst subclasses using an
408   /// opcode instead of the subclass's constructor. The opcode must be in the
409   /// CastOps category (Instruction::isCast(opcode) returns true). This
410   /// constructor has insert-before-instruction semantics to automatically
411   /// insert the new CastInst before InsertBefore (if it is non-null).
412   /// @brief Construct any of the CastInst subclasses
413   static CastInst *Create(
414     Instruction::CastOps,    ///< The opcode of the cast instruction
415     Value *S,                ///< The value to be casted (operand 0)
416     Type *Ty,          ///< The type to which cast should be made
417     const Twine &Name = "", ///< Name for the instruction
418     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
419   );
420   /// Provides a way to construct any of the CastInst subclasses using an
421   /// opcode instead of the subclass's constructor. The opcode must be in the
422   /// CastOps category. This constructor has insert-at-end-of-block semantics
423   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
424   /// its non-null).
425   /// @brief Construct any of the CastInst subclasses
426   static CastInst *Create(
427     Instruction::CastOps,    ///< The opcode for the cast instruction
428     Value *S,                ///< The value to be casted (operand 0)
429     Type *Ty,          ///< The type to which operand is casted
430     const Twine &Name, ///< The name for the instruction
431     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
432   );
433
434   /// @brief Create a ZExt or BitCast cast instruction
435   static CastInst *CreateZExtOrBitCast(
436     Value *S,                ///< The value to be casted (operand 0)
437     Type *Ty,          ///< The type to which cast should be made
438     const Twine &Name = "", ///< Name for the instruction
439     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
440   );
441
442   /// @brief Create a ZExt or BitCast cast instruction
443   static CastInst *CreateZExtOrBitCast(
444     Value *S,                ///< The value to be casted (operand 0)
445     Type *Ty,          ///< The type to which operand is casted
446     const Twine &Name, ///< The name for the instruction
447     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
448   );
449
450   /// @brief Create a SExt or BitCast cast instruction
451   static CastInst *CreateSExtOrBitCast(
452     Value *S,                ///< The value to be casted (operand 0)
453     Type *Ty,          ///< The type to which cast should be made
454     const Twine &Name = "", ///< Name for the instruction
455     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
456   );
457
458   /// @brief Create a SExt or BitCast cast instruction
459   static CastInst *CreateSExtOrBitCast(
460     Value *S,                ///< The value to be casted (operand 0)
461     Type *Ty,          ///< The type to which operand is casted
462     const Twine &Name, ///< The name for the instruction
463     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
464   );
465
466   /// @brief Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
467   static CastInst *CreatePointerCast(
468     Value *S,                ///< The pointer value to be casted (operand 0)
469     Type *Ty,          ///< The type to which operand is casted
470     const Twine &Name, ///< The name for the instruction
471     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
472   );
473
474   /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
475   static CastInst *CreatePointerCast(
476     Value *S,                ///< The pointer value to be casted (operand 0)
477     Type *Ty,          ///< The type to which cast should be made
478     const Twine &Name = "", ///< Name for the instruction
479     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
480   );
481
482   /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
483   static CastInst *CreatePointerBitCastOrAddrSpaceCast(
484     Value *S,                ///< The pointer value to be casted (operand 0)
485     Type *Ty,          ///< The type to which operand is casted
486     const Twine &Name, ///< The name for the instruction
487     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
488   );
489
490   /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
491   static CastInst *CreatePointerBitCastOrAddrSpaceCast(
492     Value *S,                ///< The pointer value to be casted (operand 0)
493     Type *Ty,          ///< The type to which cast should be made
494     const Twine &Name = "", ///< Name for the instruction
495     Instruction *InsertBefore = 0 ///< Place to insert the instruction
496   );
497
498   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
499   static CastInst *CreateIntegerCast(
500     Value *S,                ///< The pointer value to be casted (operand 0)
501     Type *Ty,          ///< The type to which cast should be made
502     bool isSigned,           ///< Whether to regard S as signed or not
503     const Twine &Name = "", ///< Name for the instruction
504     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
505   );
506
507   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
508   static CastInst *CreateIntegerCast(
509     Value *S,                ///< The integer value to be casted (operand 0)
510     Type *Ty,          ///< The integer type to which operand is casted
511     bool isSigned,           ///< Whether to regard S as signed or not
512     const Twine &Name, ///< The name for the instruction
513     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
514   );
515
516   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
517   static CastInst *CreateFPCast(
518     Value *S,                ///< The floating point value to be casted
519     Type *Ty,          ///< The floating point type to cast to
520     const Twine &Name = "", ///< Name for the instruction
521     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
522   );
523
524   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
525   static CastInst *CreateFPCast(
526     Value *S,                ///< The floating point value to be casted
527     Type *Ty,          ///< The floating point type to cast to
528     const Twine &Name, ///< The name for the instruction
529     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
530   );
531
532   /// @brief Create a Trunc or BitCast cast instruction
533   static CastInst *CreateTruncOrBitCast(
534     Value *S,                ///< The value to be casted (operand 0)
535     Type *Ty,          ///< The type to which cast should be made
536     const Twine &Name = "", ///< Name for the instruction
537     Instruction *InsertBefore = nullptr ///< Place to insert the instruction
538   );
539
540   /// @brief Create a Trunc or BitCast cast instruction
541   static CastInst *CreateTruncOrBitCast(
542     Value *S,                ///< The value to be casted (operand 0)
543     Type *Ty,          ///< The type to which operand is casted
544     const Twine &Name, ///< The name for the instruction
545     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
546   );
547
548   /// @brief Check whether it is valid to call getCastOpcode for these types.
549   static bool isCastable(
550     Type *SrcTy, ///< The Type from which the value should be cast.
551     Type *DestTy ///< The Type to which the value should be cast.
552   );
553
554   /// @brief Check whether a bitcast between these types is valid
555   static bool isBitCastable(
556     Type *SrcTy, ///< The Type from which the value should be cast.
557     Type *DestTy ///< The Type to which the value should be cast.
558   );
559
560   /// Returns the opcode necessary to cast Val into Ty using usual casting
561   /// rules.
562   /// @brief Infer the opcode for cast operand and type
563   static Instruction::CastOps getCastOpcode(
564     const Value *Val, ///< The value to cast
565     bool SrcIsSigned, ///< Whether to treat the source as signed
566     Type *Ty,   ///< The Type to which the value should be casted
567     bool DstIsSigned  ///< Whether to treate the dest. as signed
568   );
569
570   /// There are several places where we need to know if a cast instruction
571   /// only deals with integer source and destination types. To simplify that
572   /// logic, this method is provided.
573   /// @returns true iff the cast has only integral typed operand and dest type.
574   /// @brief Determine if this is an integer-only cast.
575   bool isIntegerCast() const;
576
577   /// A lossless cast is one that does not alter the basic value. It implies
578   /// a no-op cast but is more stringent, preventing things like int->float,
579   /// long->double, or int->ptr.
580   /// @returns true iff the cast is lossless.
581   /// @brief Determine if this is a lossless cast.
582   bool isLosslessCast() const;
583
584   /// A no-op cast is one that can be effected without changing any bits.
585   /// It implies that the source and destination types are the same size. The
586   /// IntPtrTy argument is used to make accurate determinations for casts
587   /// involving Integer and Pointer types. They are no-op casts if the integer
588   /// is the same size as the pointer. However, pointer size varies with
589   /// platform. Generally, the result of DataLayout::getIntPtrType() should be
590   /// passed in. If that's not available, use Type::Int64Ty, which will make
591   /// the isNoopCast call conservative.
592   /// @brief Determine if the described cast is a no-op cast.
593   static bool isNoopCast(
594     Instruction::CastOps Opcode,  ///< Opcode of cast
595     Type *SrcTy,   ///< SrcTy of cast
596     Type *DstTy,   ///< DstTy of cast
597     Type *IntPtrTy ///< Integer type corresponding to Ptr types
598   );
599
600   /// @brief Determine if this cast is a no-op cast.
601   bool isNoopCast(
602     Type *IntPtrTy ///< Integer type corresponding to pointer
603   ) const;
604
605   /// @brief Determine if this cast is a no-op cast.
606   bool isNoopCast(
607     const DataLayout *DL ///< DataLayout to get the Int Ptr type from.
608   ) const;
609
610   /// Determine how a pair of casts can be eliminated, if they can be at all.
611   /// This is a helper function for both CastInst and ConstantExpr.
612   /// @returns 0 if the CastInst pair can't be eliminated, otherwise
613   /// returns Instruction::CastOps value for a cast that can replace
614   /// the pair, casting SrcTy to DstTy.
615   /// @brief Determine if a cast pair is eliminable
616   static unsigned isEliminableCastPair(
617     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
618     Instruction::CastOps secondOpcode, ///< Opcode of second cast
619     Type *SrcTy, ///< SrcTy of 1st cast
620     Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
621     Type *DstTy, ///< DstTy of 2nd cast
622     Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
623     Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
624     Type *DstIntPtrTy  ///< Integer type corresponding to Ptr DstTy, or null
625   );
626
627   /// @brief Return the opcode of this CastInst
628   Instruction::CastOps getOpcode() const {
629     return Instruction::CastOps(Instruction::getOpcode());
630   }
631
632   /// @brief Return the source type, as a convenience
633   Type* getSrcTy() const { return getOperand(0)->getType(); }
634   /// @brief Return the destination type, as a convenience
635   Type* getDestTy() const { return getType(); }
636
637   /// This method can be used to determine if a cast from S to DstTy using
638   /// Opcode op is valid or not.
639   /// @returns true iff the proposed cast is valid.
640   /// @brief Determine if a cast is valid without creating one.
641   static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
642
643   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
644   static inline bool classof(const Instruction *I) {
645     return I->isCast();
646   }
647   static inline bool classof(const Value *V) {
648     return isa<Instruction>(V) && classof(cast<Instruction>(V));
649   }
650 };
651
652 //===----------------------------------------------------------------------===//
653 //                               CmpInst Class
654 //===----------------------------------------------------------------------===//
655
656 /// This class is the base class for the comparison instructions.
657 /// @brief Abstract base class of comparison instructions.
658 class CmpInst : public Instruction {
659   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
660   CmpInst() LLVM_DELETED_FUNCTION;
661 protected:
662   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
663           Value *LHS, Value *RHS, const Twine &Name = "",
664           Instruction *InsertBefore = nullptr);
665
666   CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
667           Value *LHS, Value *RHS, const Twine &Name,
668           BasicBlock *InsertAtEnd);
669
670   void anchor() override; // Out of line virtual method.
671 public:
672   /// This enumeration lists the possible predicates for CmpInst subclasses.
673   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
674   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
675   /// predicate values are not overlapping between the classes.
676   enum Predicate {
677     // Opcode              U L G E    Intuitive operation
678     FCMP_FALSE =  0,  ///< 0 0 0 0    Always false (always folded)
679     FCMP_OEQ   =  1,  ///< 0 0 0 1    True if ordered and equal
680     FCMP_OGT   =  2,  ///< 0 0 1 0    True if ordered and greater than
681     FCMP_OGE   =  3,  ///< 0 0 1 1    True if ordered and greater than or equal
682     FCMP_OLT   =  4,  ///< 0 1 0 0    True if ordered and less than
683     FCMP_OLE   =  5,  ///< 0 1 0 1    True if ordered and less than or equal
684     FCMP_ONE   =  6,  ///< 0 1 1 0    True if ordered and operands are unequal
685     FCMP_ORD   =  7,  ///< 0 1 1 1    True if ordered (no nans)
686     FCMP_UNO   =  8,  ///< 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
687     FCMP_UEQ   =  9,  ///< 1 0 0 1    True if unordered or equal
688     FCMP_UGT   = 10,  ///< 1 0 1 0    True if unordered or greater than
689     FCMP_UGE   = 11,  ///< 1 0 1 1    True if unordered, greater than, or equal
690     FCMP_ULT   = 12,  ///< 1 1 0 0    True if unordered or less than
691     FCMP_ULE   = 13,  ///< 1 1 0 1    True if unordered, less than, or equal
692     FCMP_UNE   = 14,  ///< 1 1 1 0    True if unordered or not equal
693     FCMP_TRUE  = 15,  ///< 1 1 1 1    Always true (always folded)
694     FIRST_FCMP_PREDICATE = FCMP_FALSE,
695     LAST_FCMP_PREDICATE = FCMP_TRUE,
696     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
697     ICMP_EQ    = 32,  ///< equal
698     ICMP_NE    = 33,  ///< not equal
699     ICMP_UGT   = 34,  ///< unsigned greater than
700     ICMP_UGE   = 35,  ///< unsigned greater or equal
701     ICMP_ULT   = 36,  ///< unsigned less than
702     ICMP_ULE   = 37,  ///< unsigned less or equal
703     ICMP_SGT   = 38,  ///< signed greater than
704     ICMP_SGE   = 39,  ///< signed greater or equal
705     ICMP_SLT   = 40,  ///< signed less than
706     ICMP_SLE   = 41,  ///< signed less or equal
707     FIRST_ICMP_PREDICATE = ICMP_EQ,
708     LAST_ICMP_PREDICATE = ICMP_SLE,
709     BAD_ICMP_PREDICATE = ICMP_SLE + 1
710   };
711
712   // allocate space for exactly two operands
713   void *operator new(size_t s) {
714     return User::operator new(s, 2);
715   }
716   /// Construct a compare instruction, given the opcode, the predicate and
717   /// the two operands.  Optionally (if InstBefore is specified) insert the
718   /// instruction into a BasicBlock right before the specified instruction.
719   /// The specified Instruction is allowed to be a dereferenced end iterator.
720   /// @brief Create a CmpInst
721   static CmpInst *Create(OtherOps Op,
722                          unsigned short predicate, Value *S1,
723                          Value *S2, const Twine &Name = "",
724                          Instruction *InsertBefore = nullptr);
725
726   /// Construct a compare instruction, given the opcode, the predicate and the
727   /// two operands.  Also automatically insert this instruction to the end of
728   /// the BasicBlock specified.
729   /// @brief Create a CmpInst
730   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
731                          Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
732
733   /// @brief Get the opcode casted to the right type
734   OtherOps getOpcode() const {
735     return static_cast<OtherOps>(Instruction::getOpcode());
736   }
737
738   /// @brief Return the predicate for this instruction.
739   Predicate getPredicate() const {
740     return Predicate(getSubclassDataFromInstruction());
741   }
742
743   /// @brief Set the predicate for this instruction to the specified value.
744   void setPredicate(Predicate P) { setInstructionSubclassData(P); }
745
746   static bool isFPPredicate(Predicate P) {
747     return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
748   }
749
750   static bool isIntPredicate(Predicate P) {
751     return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
752   }
753
754   bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
755   bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
756
757
758   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
759   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
760   /// @returns the inverse predicate for the instruction's current predicate.
761   /// @brief Return the inverse of the instruction's predicate.
762   Predicate getInversePredicate() const {
763     return getInversePredicate(getPredicate());
764   }
765
766   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
767   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
768   /// @returns the inverse predicate for predicate provided in \p pred.
769   /// @brief Return the inverse of a given predicate
770   static Predicate getInversePredicate(Predicate pred);
771
772   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
773   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
774   /// @returns the predicate that would be the result of exchanging the two
775   /// operands of the CmpInst instruction without changing the result
776   /// produced.
777   /// @brief Return the predicate as if the operands were swapped
778   Predicate getSwappedPredicate() const {
779     return getSwappedPredicate(getPredicate());
780   }
781
782   /// This is a static version that you can use without an instruction
783   /// available.
784   /// @brief Return the predicate as if the operands were swapped.
785   static Predicate getSwappedPredicate(Predicate pred);
786
787   /// @brief Provide more efficient getOperand methods.
788   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
789
790   /// This is just a convenience that dispatches to the subclasses.
791   /// @brief Swap the operands and adjust predicate accordingly to retain
792   /// the same comparison.
793   void swapOperands();
794
795   /// This is just a convenience that dispatches to the subclasses.
796   /// @brief Determine if this CmpInst is commutative.
797   bool isCommutative() const;
798
799   /// This is just a convenience that dispatches to the subclasses.
800   /// @brief Determine if this is an equals/not equals predicate.
801   bool isEquality() const;
802
803   /// @returns true if the comparison is signed, false otherwise.
804   /// @brief Determine if this instruction is using a signed comparison.
805   bool isSigned() const {
806     return isSigned(getPredicate());
807   }
808
809   /// @returns true if the comparison is unsigned, false otherwise.
810   /// @brief Determine if this instruction is using an unsigned comparison.
811   bool isUnsigned() const {
812     return isUnsigned(getPredicate());
813   }
814
815   /// This is just a convenience.
816   /// @brief Determine if this is true when both operands are the same.
817   bool isTrueWhenEqual() const {
818     return isTrueWhenEqual(getPredicate());
819   }
820
821   /// This is just a convenience.
822   /// @brief Determine if this is false when both operands are the same.
823   bool isFalseWhenEqual() const {
824     return isFalseWhenEqual(getPredicate());
825   }
826
827   /// @returns true if the predicate is unsigned, false otherwise.
828   /// @brief Determine if the predicate is an unsigned operation.
829   static bool isUnsigned(unsigned short predicate);
830
831   /// @returns true if the predicate is signed, false otherwise.
832   /// @brief Determine if the predicate is an signed operation.
833   static bool isSigned(unsigned short predicate);
834
835   /// @brief Determine if the predicate is an ordered operation.
836   static bool isOrdered(unsigned short predicate);
837
838   /// @brief Determine if the predicate is an unordered operation.
839   static bool isUnordered(unsigned short predicate);
840
841   /// Determine if the predicate is true when comparing a value with itself.
842   static bool isTrueWhenEqual(unsigned short predicate);
843
844   /// Determine if the predicate is false when comparing a value with itself.
845   static bool isFalseWhenEqual(unsigned short predicate);
846
847   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
848   static inline bool classof(const Instruction *I) {
849     return I->getOpcode() == Instruction::ICmp ||
850            I->getOpcode() == Instruction::FCmp;
851   }
852   static inline bool classof(const Value *V) {
853     return isa<Instruction>(V) && classof(cast<Instruction>(V));
854   }
855
856   /// @brief Create a result type for fcmp/icmp
857   static Type* makeCmpResultType(Type* opnd_type) {
858     if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
859       return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
860                              vt->getNumElements());
861     }
862     return Type::getInt1Ty(opnd_type->getContext());
863   }
864 private:
865   // Shadow Value::setValueSubclassData with a private forwarding method so that
866   // subclasses cannot accidentally use it.
867   void setValueSubclassData(unsigned short D) {
868     Value::setValueSubclassData(D);
869   }
870 };
871
872
873 // FIXME: these are redundant if CmpInst < BinaryOperator
874 template <>
875 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
876 };
877
878 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
879
880 } // End llvm namespace
881
882 #endif