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