Add a new keyword 'inbounds' for use with getelementptr. See the
[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. It does not include SDiv,
65 /// despite that operator having the potential for overflow.
66 ///
67 class OverflowingBinaryOperator : public Operator {
68 public:
69   /// hasNoUnsignedOverflow - Test whether this operation is known to never
70   /// undergo unsigned overflow.
71   bool hasNoUnsignedOverflow() const {
72     return SubclassOptionalData & (1 << 0);
73   }
74   void setHasNoUnsignedOverflow(bool B) {
75     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
76   }
77
78   /// hasNoSignedOverflow - Test whether this operation is known to never
79   /// undergo signed overflow.
80   bool hasNoSignedOverflow() const {
81     return SubclassOptionalData & (1 << 1);
82   }
83   void setHasNoSignedOverflow(bool B) {
84     SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
85   }
86
87   static inline bool classof(const OverflowingBinaryOperator *) { return true; }
88   static inline bool classof(const Instruction *I) {
89     return I->getOpcode() == Instruction::Add ||
90            I->getOpcode() == Instruction::Sub ||
91            I->getOpcode() == Instruction::Mul;
92   }
93   static inline bool classof(const ConstantExpr *CE) {
94     return CE->getOpcode() == Instruction::Add ||
95            CE->getOpcode() == Instruction::Sub ||
96            CE->getOpcode() == Instruction::Mul;
97   }
98   static inline bool classof(const Value *V) {
99     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
100            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
101   }
102 };
103
104 /// AddOperator - Utility class for integer addition operators.
105 ///
106 class AddOperator : public OverflowingBinaryOperator {
107 public:
108   static inline bool classof(const AddOperator *) { return true; }
109   static inline bool classof(const Instruction *I) {
110     return I->getOpcode() == Instruction::Add;
111   }
112   static inline bool classof(const ConstantExpr *CE) {
113     return CE->getOpcode() == Instruction::Add;
114   }
115   static inline bool classof(const Value *V) {
116     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
117            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
118   }
119 };
120
121 /// SubOperator - Utility class for integer subtraction operators.
122 ///
123 class SubOperator : public OverflowingBinaryOperator {
124 public:
125   static inline bool classof(const SubOperator *) { return true; }
126   static inline bool classof(const Instruction *I) {
127     return I->getOpcode() == Instruction::Sub;
128   }
129   static inline bool classof(const ConstantExpr *CE) {
130     return CE->getOpcode() == Instruction::Sub;
131   }
132   static inline bool classof(const Value *V) {
133     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
134            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
135   }
136 };
137
138 /// MulOperator - Utility class for integer multiplication operators.
139 ///
140 class MulOperator : public OverflowingBinaryOperator {
141 public:
142   static inline bool classof(const MulOperator *) { return true; }
143   static inline bool classof(const Instruction *I) {
144     return I->getOpcode() == Instruction::Mul;
145   }
146   static inline bool classof(const ConstantExpr *CE) {
147     return CE->getOpcode() == Instruction::Mul;
148   }
149   static inline bool classof(const Value *V) {
150     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
151            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
152   }
153 };
154
155 /// SDivOperator - An Operator with opcode Instruction::SDiv.
156 ///
157 class SDivOperator : public Operator {
158 public:
159   /// isExact - Test whether this division is known to be exact, with
160   /// zero remainder.
161   bool isExact() const {
162     return SubclassOptionalData & (1 << 0);
163   }
164   void setIsExact(bool B) {
165     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
166   }
167
168   // Methods for support type inquiry through isa, cast, and dyn_cast:
169   static inline bool classof(const SDivOperator *) { return true; }
170   static inline bool classof(const ConstantExpr *CE) {
171     return CE->getOpcode() == Instruction::SDiv;
172   }
173   static inline bool classof(const Instruction *I) {
174     return I->getOpcode() == Instruction::SDiv;
175   }
176   static inline bool classof(const Value *V) {
177     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
178            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
179   }
180 };
181
182 class GEPOperator : public Operator {
183 public:
184   /// isInBounds - Test whether this is an inbounds GEP, as defined
185   /// by LangRef.html.
186   bool isInBounds() const {
187     return SubclassOptionalData & (1 << 0);
188   }
189   void setIsInBounds(bool B) {
190     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
191   }
192
193   inline op_iterator       idx_begin()       { return op_begin()+1; }
194   inline const_op_iterator idx_begin() const { return op_begin()+1; }
195   inline op_iterator       idx_end()         { return op_end(); }
196   inline const_op_iterator idx_end()   const { return op_end(); }
197
198   Value *getPointerOperand() {
199     return getOperand(0);
200   }
201   const Value *getPointerOperand() const {
202     return getOperand(0);
203   }
204   static unsigned getPointerOperandIndex() {
205     return 0U;                      // get index for modifying correct operand
206   }
207
208   /// getPointerOperandType - Method to return the pointer operand as a
209   /// PointerType.
210   const PointerType *getPointerOperandType() const {
211     return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
212   }
213
214   unsigned getNumIndices() const {  // Note: always non-negative
215     return getNumOperands() - 1;
216   }
217
218   bool hasIndices() const {
219     return getNumOperands() > 1;
220   }
221
222   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
223   /// zeros.  If so, the result pointer and the first operand have the same
224   /// value, just potentially different types.
225   bool hasAllZeroIndices() const {
226     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
227       if (Constant *C = dyn_cast<Constant>(I))
228         if (C->isNullValue())
229           continue;
230       return false;
231     }
232     return true;
233   }
234
235   // Methods for support type inquiry through isa, cast, and dyn_cast:
236   static inline bool classof(const GEPOperator *) { return true; }
237   static inline bool classof(const GetElementPtrInst *) { return true; }
238   static inline bool classof(const ConstantExpr *CE) {
239     return CE->getOpcode() == Instruction::GetElementPtr;
240   }
241   static inline bool classof(const Instruction *I) {
242     return I->getOpcode() == Instruction::GetElementPtr;
243   }
244   static inline bool classof(const Value *V) {
245     return isa<GetElementPtrInst>(V) ||
246            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
247   }
248 };
249
250 } // End llvm namespace
251
252 #endif