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