Add support for pushsection and popsection. Patch by Joerg Sonnenberger.
[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   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
198                                    const Twine &Name = "") {
199     BinaryOperator *BO = Create(Opc, V1, V2, Name);
200     BO->setHasNoSignedWrap(true);
201     return BO;
202   }
203   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
204                                    const Twine &Name, BasicBlock *BB) {
205     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
206     BO->setHasNoSignedWrap(true);
207     return BO;
208   }
209   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
210                                    const Twine &Name, Instruction *I) {
211     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
212     BO->setHasNoSignedWrap(true);
213     return BO;
214   }
215   
216   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
217                                    const Twine &Name = "") {
218     BinaryOperator *BO = Create(Opc, V1, V2, Name);
219     BO->setHasNoUnsignedWrap(true);
220     return BO;
221   }
222   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
223                                    const Twine &Name, BasicBlock *BB) {
224     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
225     BO->setHasNoUnsignedWrap(true);
226     return BO;
227   }
228   static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
229                                    const Twine &Name, Instruction *I) {
230     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
231     BO->setHasNoUnsignedWrap(true);
232     return BO;
233   }
234   
235   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
236                                      const Twine &Name = "") {
237     BinaryOperator *BO = Create(Opc, V1, V2, Name);
238     BO->setIsExact(true);
239     return BO;
240   }
241   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
242                                      const Twine &Name, BasicBlock *BB) {
243     BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
244     BO->setIsExact(true);
245     return BO;
246   }
247   static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
248                                      const Twine &Name, Instruction *I) {
249     BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
250     BO->setIsExact(true);
251     return BO;
252   }
253   
254 #define DEFINE_HELPERS(OPC, NUWNSWEXACT)                                     \
255   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
256            (Value *V1, Value *V2, const Twine &Name = "") {                  \
257     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name);            \
258   }                                                                          \
259   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
260            (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) {       \
261     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB);        \
262   }                                                                          \
263   static BinaryOperator *Create ## NUWNSWEXACT ## OPC                        \
264            (Value *V1, Value *V2, const Twine &Name, Instruction *I) {       \
265     return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I);         \
266   }
267   
268   DEFINE_HELPERS(Add, NSW)  // CreateNSWAdd
269   DEFINE_HELPERS(Add, NUW)  // CreateNUWAdd
270   DEFINE_HELPERS(Sub, NSW)  // CreateNSWSub
271   DEFINE_HELPERS(Sub, NUW)  // CreateNUWSub
272   DEFINE_HELPERS(Mul, NSW)  // CreateNSWMul
273   DEFINE_HELPERS(Mul, NUW)  // CreateNUWMul
274   DEFINE_HELPERS(Shl, NSW)  // CreateNSWShl
275   DEFINE_HELPERS(Shl, NUW)  // CreateNUWShl
276
277   DEFINE_HELPERS(SDiv, Exact)  // CreateExactSDiv
278   DEFINE_HELPERS(UDiv, Exact)  // CreateExactUDiv
279   DEFINE_HELPERS(AShr, Exact)  // CreateExactAShr
280   DEFINE_HELPERS(LShr, Exact)  // CreateExactLShr
281
282 #undef DEFINE_HELPERS
283   
284   /// Helper functions to construct and inspect unary operations (NEG and NOT)
285   /// via binary operators SUB and XOR:
286   ///
287   /// CreateNeg, CreateNot - Create the NEG and NOT
288   ///     instructions out of SUB and XOR instructions.
289   ///
290   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
291                                    Instruction *InsertBefore = 0);
292   static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
293                                    BasicBlock *InsertAtEnd);
294   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
295                                       Instruction *InsertBefore = 0);
296   static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
297                                       BasicBlock *InsertAtEnd);
298   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
299                                       Instruction *InsertBefore = 0);
300   static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
301                                       BasicBlock *InsertAtEnd);
302   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
303                                     Instruction *InsertBefore = 0);
304   static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
305                                     BasicBlock *InsertAtEnd);
306   static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
307                                    Instruction *InsertBefore = 0);
308   static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
309                                    BasicBlock *InsertAtEnd);
310
311   /// isNeg, isFNeg, isNot - Check if the given Value is a
312   /// NEG, FNeg, or NOT instruction.
313   ///
314   static bool isNeg(const Value *V);
315   static bool isFNeg(const Value *V);
316   static bool isNot(const Value *V);
317
318   /// getNegArgument, getNotArgument - Helper functions to extract the
319   ///     unary argument of a NEG, FNEG or NOT operation implemented via
320   ///     Sub, FSub, or Xor.
321   ///
322   static const Value *getNegArgument(const Value *BinOp);
323   static       Value *getNegArgument(      Value *BinOp);
324   static const Value *getFNegArgument(const Value *BinOp);
325   static       Value *getFNegArgument(      Value *BinOp);
326   static const Value *getNotArgument(const Value *BinOp);
327   static       Value *getNotArgument(      Value *BinOp);
328
329   BinaryOps getOpcode() const {
330     return static_cast<BinaryOps>(Instruction::getOpcode());
331   }
332
333   /// swapOperands - Exchange the two operands to this instruction.
334   /// This instruction is safe to use on any binary instruction and
335   /// does not modify the semantics of the instruction.  If the instruction
336   /// cannot be reversed (ie, it's a Div), then return true.
337   ///
338   bool swapOperands();
339
340   /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
341   /// which must be an operator which supports this flag. See LangRef.html
342   /// for the meaning of this flag.
343   void setHasNoUnsignedWrap(bool b = true);
344
345   /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
346   /// which must be an operator which supports this flag. See LangRef.html
347   /// for the meaning of this flag.
348   void setHasNoSignedWrap(bool b = true);
349
350   /// setIsExact - Set or clear the exact flag on this instruction,
351   /// which must be an operator which supports this flag. See LangRef.html
352   /// for the meaning of this flag.
353   void setIsExact(bool b = true);
354
355   /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
356   bool hasNoUnsignedWrap() const;
357
358   /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
359   bool hasNoSignedWrap() const;
360
361   /// isExact - Determine whether the exact flag is set.
362   bool isExact() const;
363
364   // Methods for support type inquiry through isa, cast, and dyn_cast:
365   static inline bool classof(const BinaryOperator *) { return true; }
366   static inline bool classof(const Instruction *I) {
367     return I->isBinaryOp();
368   }
369   static inline bool classof(const Value *V) {
370     return isa<Instruction>(V) && classof(cast<Instruction>(V));
371   }
372 };
373
374 template <>
375 struct OperandTraits<BinaryOperator> :
376   public FixedNumOperandTraits<BinaryOperator, 2> {
377 };
378
379 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
380
381 //===----------------------------------------------------------------------===//
382 //                               CastInst Class
383 //===----------------------------------------------------------------------===//
384
385 /// CastInst - This is the base class for all instructions that perform data
386 /// casts. It is simply provided so that instruction category testing
387 /// can be performed with code like:
388 ///
389 /// if (isa<CastInst>(Instr)) { ... }
390 /// @brief Base class of casting instructions.
391 class CastInst : public UnaryInstruction {
392 protected:
393   /// @brief Constructor with insert-before-instruction semantics for subclasses
394   CastInst(const 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(const 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     const 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     const 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     const 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     const 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     const 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     const 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     const 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     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
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     const 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     const 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     const 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     const 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     const 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     const 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     const Type *SrcTy, ///< The Type from which the value should be cast.
534     const 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     const 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     const Type *SrcTy,   ///< SrcTy of cast
573     const Type *DstTy,   ///< DstTy of cast
574     const 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     const 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     const Type *SrcTy, ///< SrcTy of 1st cast
592     const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
593     const Type *DstTy, ///< DstTy of 2nd cast
594     const 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   const Type* getSrcTy() const { return getOperand(0)->getType(); }
604   /// @brief Return the destination type, as a convenience
605   const 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, const 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(const Type *ty, Instruction::OtherOps op, unsigned short pred,
634           Value *LHS, Value *RHS, const Twine &Name = "",
635           Instruction *InsertBefore = 0);
636
637   CmpInst(const 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 const Type* makeCmpResultType(const Type* opnd_type) {
830     if (const VectorType* vt = dyn_cast<const 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