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