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