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/Constants.h"
19#include "llvm/Instruction.h"
20#include "llvm/Type.h"
21
22namespace llvm {
23
24class GetElementPtrInst;
25class BinaryOperator;
26class ConstantExpr;
27
28/// Operator - This is a utility class that provides an abstraction for the
29/// common functionality between Instructions and ConstantExprs.
30///
31class Operator : public User {
32private:
33  // Do not implement any of these. The Operator class is intended to be used
34  // as a utility, and is never itself instantiated.
35  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
36  void *operator new(size_t s) LLVM_DELETED_FUNCTION;
37  Operator() LLVM_DELETED_FUNCTION;
38  // NOTE: cannot use LLVM_DELETED_FUNCTION because gcc errors when deleting
39  // an override of a non-deleted function.
40  ~Operator();
41
42public:
43  /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
44  ///
45  unsigned getOpcode() const {
46    if (const Instruction *I = dyn_cast<Instruction>(this))
47      return I->getOpcode();
48    return cast<ConstantExpr>(this)->getOpcode();
49  }
50
51  /// getOpcode - If V is an Instruction or ConstantExpr, return its
52  /// opcode. Otherwise return UserOp1.
53  ///
54  static unsigned getOpcode(const Value *V) {
55    if (const Instruction *I = dyn_cast<Instruction>(V))
56      return I->getOpcode();
57    if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
58      return CE->getOpcode();
59    return Instruction::UserOp1;
60  }
61
62  static inline bool classof(const Operator *) { return true; }
63  static inline bool classof(const Instruction *) { return true; }
64  static inline bool classof(const ConstantExpr *) { return true; }
65  static inline bool classof(const Value *V) {
66    return isa<Instruction>(V) || isa<ConstantExpr>(V);
67  }
68};
69
70/// OverflowingBinaryOperator - Utility class for integer arithmetic operators
71/// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
72/// despite that operator having the potential for overflow.
73///
74class OverflowingBinaryOperator : public Operator {
75public:
76  enum {
77    NoUnsignedWrap = (1 << 0),
78    NoSignedWrap   = (1 << 1)
79  };
80
81private:
82  ~OverflowingBinaryOperator(); // DO NOT IMPLEMENT
83
84  friend class BinaryOperator;
85  friend class ConstantExpr;
86  void setHasNoUnsignedWrap(bool B) {
87    SubclassOptionalData =
88      (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
89  }
90  void setHasNoSignedWrap(bool B) {
91    SubclassOptionalData =
92      (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
93  }
94
95public:
96  /// hasNoUnsignedWrap - Test whether this operation is known to never
97  /// undergo unsigned overflow, aka the nuw property.
98  bool hasNoUnsignedWrap() const {
99    return SubclassOptionalData & NoUnsignedWrap;
100  }
101
102  /// hasNoSignedWrap - Test whether this operation is known to never
103  /// undergo signed overflow, aka the nsw property.
104  bool hasNoSignedWrap() const {
105    return (SubclassOptionalData & NoSignedWrap) != 0;
106  }
107
108  static inline bool classof(const OverflowingBinaryOperator *) { return true; }
109  static inline bool classof(const Instruction *I) {
110    return I->getOpcode() == Instruction::Add ||
111           I->getOpcode() == Instruction::Sub ||
112           I->getOpcode() == Instruction::Mul ||
113           I->getOpcode() == Instruction::Shl;
114  }
115  static inline bool classof(const ConstantExpr *CE) {
116    return CE->getOpcode() == Instruction::Add ||
117           CE->getOpcode() == Instruction::Sub ||
118           CE->getOpcode() == Instruction::Mul ||
119           CE->getOpcode() == Instruction::Shl;
120  }
121  static inline bool classof(const Value *V) {
122    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
123           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
124  }
125};
126
127/// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
128/// "exact", indicating that no bits are destroyed.
129class PossiblyExactOperator : public Operator {
130public:
131  enum {
132    IsExact = (1 << 0)
133  };
134
135private:
136  ~PossiblyExactOperator(); // DO NOT IMPLEMENT
137
138  friend class BinaryOperator;
139  friend class ConstantExpr;
140  void setIsExact(bool B) {
141    SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
142  }
143
144public:
145  /// isExact - Test whether this division is known to be exact, with
146  /// zero remainder.
147  bool isExact() const {
148    return SubclassOptionalData & IsExact;
149  }
150
151  static bool isPossiblyExactOpcode(unsigned OpC) {
152    return OpC == Instruction::SDiv ||
153           OpC == Instruction::UDiv ||
154           OpC == Instruction::AShr ||
155           OpC == Instruction::LShr;
156  }
157  static inline bool classof(const ConstantExpr *CE) {
158    return isPossiblyExactOpcode(CE->getOpcode());
159  }
160  static inline bool classof(const Instruction *I) {
161    return isPossiblyExactOpcode(I->getOpcode());
162  }
163  static inline bool classof(const Value *V) {
164    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
165           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
166  }
167};
168
169/// FPMathOperator - Utility class for floating point operations which can have
170/// information about relaxed accuracy requirements attached to them.
171class FPMathOperator : public Operator {
172private:
173  ~FPMathOperator(); // DO NOT IMPLEMENT
174
175public:
176
177  /// \brief Get the maximum error permitted by this operation in ULPs.  An
178  /// accuracy of 0.0 means that the operation should be performed with the
179  /// default precision.
180  float getFPAccuracy() const;
181
182  static inline bool classof(const FPMathOperator *) { return true; }
183  static inline bool classof(const Instruction *I) {
184    return I->getType()->isFPOrFPVectorTy();
185  }
186  static inline bool classof(const Value *V) {
187    return isa<Instruction>(V) && classof(cast<Instruction>(V));
188  }
189};
190
191
192/// ConcreteOperator - A helper template for defining operators for individual
193/// opcodes.
194template<typename SuperClass, unsigned Opc>
195class ConcreteOperator : public SuperClass {
196  ~ConcreteOperator() LLVM_DELETED_FUNCTION;
197public:
198  static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
199    return true;
200  }
201  static inline bool classof(const Instruction *I) {
202    return I->getOpcode() == Opc;
203  }
204  static inline bool classof(const ConstantExpr *CE) {
205    return CE->getOpcode() == Opc;
206  }
207  static inline bool classof(const Value *V) {
208    return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
209           (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
210  }
211};
212
213class AddOperator
214  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
215  ~AddOperator() LLVM_DELETED_FUNCTION;
216};
217class SubOperator
218  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
219  ~SubOperator() LLVM_DELETED_FUNCTION;
220};
221class MulOperator
222  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
223  ~MulOperator() LLVM_DELETED_FUNCTION;
224};
225class ShlOperator
226  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
227  ~ShlOperator() LLVM_DELETED_FUNCTION;
228};
229
230
231class SDivOperator
232  : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
233  ~SDivOperator() LLVM_DELETED_FUNCTION;
234};
235class UDivOperator
236  : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
237  ~UDivOperator() LLVM_DELETED_FUNCTION;
238};
239class AShrOperator
240  : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
241  ~AShrOperator() LLVM_DELETED_FUNCTION;
242};
243class LShrOperator
244  : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
245  ~LShrOperator() LLVM_DELETED_FUNCTION;
246};
247
248
249
250class GEPOperator
251  : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
252  ~GEPOperator() LLVM_DELETED_FUNCTION;
253
254  enum {
255    IsInBounds = (1 << 0)
256  };
257
258  friend class GetElementPtrInst;
259  friend class ConstantExpr;
260  void setIsInBounds(bool B) {
261    SubclassOptionalData =
262      (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
263  }
264
265public:
266  /// isInBounds - Test whether this is an inbounds GEP, as defined
267  /// by LangRef.html.
268  bool isInBounds() const {
269    return SubclassOptionalData & IsInBounds;
270  }
271
272  inline op_iterator       idx_begin()       { return op_begin()+1; }
273  inline const_op_iterator idx_begin() const { return op_begin()+1; }
274  inline op_iterator       idx_end()         { return op_end(); }
275  inline const_op_iterator idx_end()   const { return op_end(); }
276
277  Value *getPointerOperand() {
278    return getOperand(0);
279  }
280  const Value *getPointerOperand() const {
281    return getOperand(0);
282  }
283  static unsigned getPointerOperandIndex() {
284    return 0U;                      // get index for modifying correct operand
285  }
286
287  /// getPointerOperandType - Method to return the pointer operand as a
288  /// PointerType.
289  Type *getPointerOperandType() const {
290    return getPointerOperand()->getType();
291  }
292
293  unsigned getNumIndices() const {  // Note: always non-negative
294    return getNumOperands() - 1;
295  }
296
297  bool hasIndices() const {
298    return getNumOperands() > 1;
299  }
300
301  /// hasAllZeroIndices - Return true if all of the indices of this GEP are
302  /// zeros.  If so, the result pointer and the first operand have the same
303  /// value, just potentially different types.
304  bool hasAllZeroIndices() const {
305    for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
306      if (ConstantInt *C = dyn_cast<ConstantInt>(I))
307        if (C->isZero())
308          continue;
309      return false;
310    }
311    return true;
312  }
313
314  /// hasAllConstantIndices - Return true if all of the indices of this GEP are
315  /// constant integers.  If so, the result pointer and the first operand have
316  /// a constant offset between them.
317  bool hasAllConstantIndices() const {
318    for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
319      if (!isa<ConstantInt>(I))
320        return false;
321    }
322    return true;
323  }
324};
325
326} // End llvm namespace
327
328#endif
329