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