1413ce3d20ca72579f558e696e44bd83c1e0a8c6
[oota-llvm.git] / include / llvm / Operator.h
1 //===-- llvm/Operator.h - Operator utility subclass -------------*- 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 classes for working with Instructions and
11 // ConstantExprs.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OPERATOR_H
16 #define LLVM_OPERATOR_H
17
18 #include "llvm/Instruction.h"
19 #include "llvm/Constants.h"
20
21 namespace llvm {
22
23 /// Operator - This is a utility class that provides an abstraction for the
24 /// common functionality between Instructions and ConstantExprs.
25 ///
26 class Operator : public User {
27 private:
28   // Do not implement any of these. The Operator class is intended to be used
29   // as a utility, and is never itself instantiated.
30   void *operator new(size_t, unsigned);
31   void *operator new(size_t s);
32   Operator();
33   ~Operator();
34
35 public:
36   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
37   ///
38   unsigned getOpcode() const {
39     if (const Instruction *I = dyn_cast<Instruction>(this))
40       return I->getOpcode();
41     return cast<ConstantExpr>(this)->getOpcode();
42   }
43
44   /// getOpcode - If V is an Instruction or ConstantExpr, return its
45   /// opcode. Otherwise return UserOp1.
46   ///
47   static unsigned getOpcode(const Value *V) {
48     if (const Instruction *I = dyn_cast<Instruction>(V))
49       return I->getOpcode();
50     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
51       return CE->getOpcode();
52     return Instruction::UserOp1;
53   }
54
55   static inline bool classof(const Operator *) { return true; }
56   static inline bool classof(const Instruction *I) { return true; }
57   static inline bool classof(const ConstantExpr *I) { return true; }
58   static inline bool classof(const Value *V) {
59     return isa<Instruction>(V) || isa<ConstantExpr>(V);
60   }
61 };
62
63 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
64 /// which may exhibit overflow - Add, Sub, and Mul.
65 ///
66 class OverflowingBinaryOperator : public Operator {
67 public:
68   /// hasNoSignedOverflow - Test whether this operation is known to never
69   /// undergo signed overflow.
70   bool hasNoSignedOverflow() const {
71     return SubclassOptionalData & (1 << 0);
72   }
73   void setHasNoSignedOverflow(bool B) {
74     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
75   }
76
77   /// hasNoUnsignedOverflow - Test whether this operation is known to never
78   /// undergo unsigned overflow.
79   bool hasNoUnsignedOverflow() const {
80     return SubclassOptionalData & (1 << 1);
81   }
82   void setHasNoUnsignedOverflow(bool B) {
83     SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
84   }
85
86   static inline bool classof(const OverflowingBinaryOperator *) { return true; }
87   static inline bool classof(const Instruction *I) {
88     return I->getOpcode() == Instruction::Add ||
89            I->getOpcode() == Instruction::Sub ||
90            I->getOpcode() == Instruction::Mul;
91   }
92   static inline bool classof(const ConstantExpr *CE) {
93     return CE->getOpcode() == Instruction::Add ||
94            CE->getOpcode() == Instruction::Sub ||
95            CE->getOpcode() == Instruction::Mul;
96   }
97   static inline bool classof(const Value *V) {
98     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
99            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
100   }
101 };
102
103 /// UDivOperator - An Operator with opcode Instruction::UDiv.
104 ///
105 class UDivOperator : public Operator {
106 public:
107   /// isExact - Test whether this division is known to be exact, with
108   /// zero remainder.
109   bool isExact() const {
110     return SubclassOptionalData & (1 << 0);
111   }
112   void setIsExact(bool B) {
113     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
114   }
115
116   // Methods for support type inquiry through isa, cast, and dyn_cast:
117   static inline bool classof(const UDivOperator *) { return true; }
118   static inline bool classof(const ConstantExpr *CE) {
119     return CE->getOpcode() == Instruction::UDiv;
120   }
121   static inline bool classof(const Instruction *I) {
122     return I->getOpcode() == Instruction::UDiv;
123   }
124   static inline bool classof(const Value *V) {
125     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
126            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
127   }
128 };
129
130 class GEPOperator : public Operator {
131 public:
132   /// hasNoPointerOverflow - Return true if this GetElementPtr is known to
133   /// never have overflow in the pointer addition portions of its effective
134   /// computation. GetElementPtr computation involves several phases;
135   /// overflow can be considered to occur in index typecasting, array index
136   /// scaling, and the addition of the base pointer with offsets. This flag
137   /// only applies to the last of these. The operands are added to the base
138   /// pointer one at a time from left to right. This function returns false
139   /// if any of these additions results in an address value which is not
140   /// known to be within the allocated address space that the base pointer
141   /// points into, or within one element (of the original allocation) past
142   /// the end.
143   bool hasNoPointerOverflow() const {
144     return SubclassOptionalData & (1 << 0);
145   }
146   void setHasNoPointerOverflow(bool B) {
147     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
148   }
149
150   // Methods for support type inquiry through isa, cast, and dyn_cast:
151   static inline bool classof(const GEPOperator *) { return true; }
152   static inline bool classof(const GetElementPtrInst *) { return true; }
153   static inline bool classof(const ConstantExpr *CE) {
154     return CE->getOpcode() == Instruction::GetElementPtr;
155   }
156   static inline bool classof(const Instruction *I) {
157     return I->getOpcode() == Instruction::GetElementPtr;
158   }
159   static inline bool classof(const Value *V) {
160     return isa<GetElementPtrInst>(V) ||
161            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
162   }
163 };
164
165 } // End llvm namespace
166
167 #endif