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