Operator.h revision 92a97a9166e359e195d949e63d7e24a4a33284cf
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
21namespace llvm {
22
23class GetElementPtrInst;
24
25/// Operator - This is a utility class that provides an abstraction for the
26/// common functionality between Instructions and ConstantExprs.
27///
28class Operator : public User {
29private:
30  // Do not implement any of these. The Operator class is intended to be used
31  // as a utility, and is never itself instantiated.
32  void *operator new(size_t, unsigned);
33  void *operator new(size_t s);
34  Operator();
35  ~Operator();
36
37public:
38  /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
39  ///
40  unsigned getOpcode() const {
41    if (const Instruction *I = dyn_cast<Instruction>(this))
42      return I->getOpcode();
43    return cast<ConstantExpr>(this)->getOpcode();
44  }
45
46  /// getOpcode - If V is an Instruction or ConstantExpr, return its
47  /// opcode. Otherwise return UserOp1.
48  ///
49  static unsigned getOpcode(const Value *V) {
50    if (const Instruction *I = dyn_cast<Instruction>(V))
51      return I->getOpcode();
52    if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
53      return CE->getOpcode();
54    return Instruction::UserOp1;
55  }
56
57  static inline bool classof(const Operator *) { return true; }
58  static inline bool classof(const Instruction *I) { return true; }
59  static inline bool classof(const ConstantExpr *I) { return true; }
60  static inline bool classof(const Value *V) {
61    return isa<Instruction>(V) || isa<ConstantExpr>(V);
62  }
63};
64
65/// OverflowingBinaryOperator - Utility class for integer arithmetic operators
66/// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
67/// despite that operator having the potential for overflow.
68///
69class OverflowingBinaryOperator : public Operator {
70  ~OverflowingBinaryOperator(); // do not implement
71public:
72  /// hasNoUnsignedWrap - Test whether this operation is known to never
73  /// undergo unsigned overflow, aka the nuw property.
74  bool hasNoUnsignedWrap() const {
75    return SubclassOptionalData & (1 << 0);
76  }
77  void setHasNoUnsignedWrap(bool B) {
78    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
79  }
80
81  /// hasNoSignedWrap - Test whether this operation is known to never
82  /// undergo signed overflow, aka the nsw property.
83  bool hasNoSignedWrap() const {
84    return SubclassOptionalData & (1 << 1);
85  }
86  void setHasNoSignedWrap(bool B) {
87    SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
88  }
89
90  static inline bool classof(const OverflowingBinaryOperator *) { return true; }
91  static inline bool classof(const Instruction *I) {
92    return I->getOpcode() == Instruction::Add ||
93           I->getOpcode() == Instruction::Sub ||
94           I->getOpcode() == Instruction::Mul;
95  }
96  static inline bool classof(const ConstantExpr *CE) {
97    return CE->getOpcode() == Instruction::Add ||
98           CE->getOpcode() == Instruction::Sub ||
99           CE->getOpcode() == Instruction::Mul;
100  }
101  static inline bool classof(const Value *V) {
102    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
103           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
104  }
105};
106
107/// AddOperator - Utility class for integer addition operators.
108///
109class AddOperator : public OverflowingBinaryOperator {
110  ~AddOperator(); // do not implement
111public:
112  static inline bool classof(const AddOperator *) { return true; }
113  static inline bool classof(const Instruction *I) {
114    return I->getOpcode() == Instruction::Add;
115  }
116  static inline bool classof(const ConstantExpr *CE) {
117    return CE->getOpcode() == Instruction::Add;
118  }
119  static inline bool classof(const Value *V) {
120    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
121           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
122  }
123};
124
125/// SubOperator - Utility class for integer subtraction operators.
126///
127class SubOperator : public OverflowingBinaryOperator {
128  ~SubOperator(); // do not implement
129public:
130  static inline bool classof(const SubOperator *) { return true; }
131  static inline bool classof(const Instruction *I) {
132    return I->getOpcode() == Instruction::Sub;
133  }
134  static inline bool classof(const ConstantExpr *CE) {
135    return CE->getOpcode() == Instruction::Sub;
136  }
137  static inline bool classof(const Value *V) {
138    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
139           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
140  }
141};
142
143/// MulOperator - Utility class for integer multiplication operators.
144///
145class MulOperator : public OverflowingBinaryOperator {
146  ~MulOperator(); // do not implement
147public:
148  static inline bool classof(const MulOperator *) { return true; }
149  static inline bool classof(const Instruction *I) {
150    return I->getOpcode() == Instruction::Mul;
151  }
152  static inline bool classof(const ConstantExpr *CE) {
153    return CE->getOpcode() == Instruction::Mul;
154  }
155  static inline bool classof(const Value *V) {
156    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
157           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
158  }
159};
160
161/// SDivOperator - An Operator with opcode Instruction::SDiv.
162///
163class SDivOperator : public Operator {
164  ~SDivOperator(); // do not implement
165public:
166  /// isExact - Test whether this division is known to be exact, with
167  /// zero remainder.
168  bool isExact() const {
169    return SubclassOptionalData & (1 << 0);
170  }
171  void setIsExact(bool B) {
172    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
173  }
174
175  // Methods for support type inquiry through isa, cast, and dyn_cast:
176  static inline bool classof(const SDivOperator *) { return true; }
177  static inline bool classof(const ConstantExpr *CE) {
178    return CE->getOpcode() == Instruction::SDiv;
179  }
180  static inline bool classof(const Instruction *I) {
181    return I->getOpcode() == Instruction::SDiv;
182  }
183  static inline bool classof(const Value *V) {
184    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
185           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
186  }
187};
188
189class GEPOperator : public Operator {
190  ~GEPOperator(); // do not implement
191public:
192  /// isInBounds - Test whether this is an inbounds GEP, as defined
193  /// by LangRef.html.
194  bool isInBounds() const {
195    return SubclassOptionalData & (1 << 0);
196  }
197  void setIsInBounds(bool B) {
198    SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
199  }
200
201  inline op_iterator       idx_begin()       { return op_begin()+1; }
202  inline const_op_iterator idx_begin() const { return op_begin()+1; }
203  inline op_iterator       idx_end()         { return op_end(); }
204  inline const_op_iterator idx_end()   const { return op_end(); }
205
206  Value *getPointerOperand() {
207    return getOperand(0);
208  }
209  const Value *getPointerOperand() const {
210    return getOperand(0);
211  }
212  static unsigned getPointerOperandIndex() {
213    return 0U;                      // get index for modifying correct operand
214  }
215
216  /// getPointerOperandType - Method to return the pointer operand as a
217  /// PointerType.
218  const PointerType *getPointerOperandType() const {
219    return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
220  }
221
222  unsigned getNumIndices() const {  // Note: always non-negative
223    return getNumOperands() - 1;
224  }
225
226  bool hasIndices() const {
227    return getNumOperands() > 1;
228  }
229
230  /// hasAllZeroIndices - Return true if all of the indices of this GEP are
231  /// zeros.  If so, the result pointer and the first operand have the same
232  /// value, just potentially different types.
233  bool hasAllZeroIndices() const {
234    for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
235      if (Constant *C = dyn_cast<Constant>(I))
236        if (C->isNullValue())
237          continue;
238      return false;
239    }
240    return true;
241  }
242
243  // Methods for support type inquiry through isa, cast, and dyn_cast:
244  static inline bool classof(const GEPOperator *) { return true; }
245  static inline bool classof(const GetElementPtrInst *) { return true; }
246  static inline bool classof(const ConstantExpr *CE) {
247    return CE->getOpcode() == Instruction::GetElementPtr;
248  }
249  static inline bool classof(const Instruction *I) {
250    return I->getOpcode() == Instruction::GetElementPtr;
251  }
252  static inline bool classof(const Value *V) {
253    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
254           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
255  }
256};
257
258} // End llvm namespace
259
260#endif
261