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