1 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various classes for working with Instructions and
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OPERATOR_H
16 #define LLVM_OPERATOR_H
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/Type.h"
25 class GetElementPtrInst;
29 /// Operator - This is a utility class that provides an abstraction for the
30 /// common functionality between Instructions and ConstantExprs.
32 class Operator : public User {
34 // Do not implement any of these. The Operator class is intended to be used
35 // as a utility, and is never itself instantiated.
36 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
37 void *operator new(size_t s) LLVM_DELETED_FUNCTION;
38 Operator() LLVM_DELETED_FUNCTION;
39 // NOTE: cannot use LLVM_DELETED_FUNCTION because gcc errors when deleting
40 // an override of a non-deleted function.
44 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
46 unsigned getOpcode() const {
47 if (const Instruction *I = dyn_cast<Instruction>(this))
48 return I->getOpcode();
49 return cast<ConstantExpr>(this)->getOpcode();
52 /// getOpcode - If V is an Instruction or ConstantExpr, return its
53 /// opcode. Otherwise return UserOp1.
55 static unsigned getOpcode(const Value *V) {
56 if (const Instruction *I = dyn_cast<Instruction>(V))
57 return I->getOpcode();
58 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
59 return CE->getOpcode();
60 return Instruction::UserOp1;
63 static inline bool classof(const Operator *) { return true; }
64 static inline bool classof(const Instruction *) { return true; }
65 static inline bool classof(const ConstantExpr *) { return true; }
66 static inline bool classof(const Value *V) {
67 return isa<Instruction>(V) || isa<ConstantExpr>(V);
71 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
72 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
73 /// despite that operator having the potential for overflow.
75 class OverflowingBinaryOperator : public Operator {
78 NoUnsignedWrap = (1 << 0),
79 NoSignedWrap = (1 << 1)
83 ~OverflowingBinaryOperator(); // DO NOT IMPLEMENT
85 friend class BinaryOperator;
86 friend class ConstantExpr;
87 void setHasNoUnsignedWrap(bool B) {
88 SubclassOptionalData =
89 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
91 void setHasNoSignedWrap(bool B) {
92 SubclassOptionalData =
93 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
97 /// hasNoUnsignedWrap - Test whether this operation is known to never
98 /// undergo unsigned overflow, aka the nuw property.
99 bool hasNoUnsignedWrap() const {
100 return SubclassOptionalData & NoUnsignedWrap;
103 /// hasNoSignedWrap - Test whether this operation is known to never
104 /// undergo signed overflow, aka the nsw property.
105 bool hasNoSignedWrap() const {
106 return (SubclassOptionalData & NoSignedWrap) != 0;
109 static inline bool classof(const OverflowingBinaryOperator *) { return true; }
110 static inline bool classof(const Instruction *I) {
111 return I->getOpcode() == Instruction::Add ||
112 I->getOpcode() == Instruction::Sub ||
113 I->getOpcode() == Instruction::Mul ||
114 I->getOpcode() == Instruction::Shl;
116 static inline bool classof(const ConstantExpr *CE) {
117 return CE->getOpcode() == Instruction::Add ||
118 CE->getOpcode() == Instruction::Sub ||
119 CE->getOpcode() == Instruction::Mul ||
120 CE->getOpcode() == Instruction::Shl;
122 static inline bool classof(const Value *V) {
123 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
124 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
128 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
129 /// "exact", indicating that no bits are destroyed.
130 class PossiblyExactOperator : public Operator {
137 ~PossiblyExactOperator(); // DO NOT IMPLEMENT
139 friend class BinaryOperator;
140 friend class ConstantExpr;
141 void setIsExact(bool B) {
142 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
146 /// isExact - Test whether this division is known to be exact, with
148 bool isExact() const {
149 return SubclassOptionalData & IsExact;
152 static bool isPossiblyExactOpcode(unsigned OpC) {
153 return OpC == Instruction::SDiv ||
154 OpC == Instruction::UDiv ||
155 OpC == Instruction::AShr ||
156 OpC == Instruction::LShr;
158 static inline bool classof(const ConstantExpr *CE) {
159 return isPossiblyExactOpcode(CE->getOpcode());
161 static inline bool classof(const Instruction *I) {
162 return isPossiblyExactOpcode(I->getOpcode());
164 static inline bool classof(const Value *V) {
165 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
166 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
170 /// FPMathOperator - Utility class for floating point operations which can have
171 /// information about relaxed accuracy requirements attached to them.
172 class FPMathOperator : public Operator {
174 ~FPMathOperator(); // DO NOT IMPLEMENT
178 /// \brief Get the maximum error permitted by this operation in ULPs. An
179 /// accuracy of 0.0 means that the operation should be performed with the
180 /// default precision.
181 float getFPAccuracy() const;
183 static inline bool classof(const FPMathOperator *) { return true; }
184 static inline bool classof(const Instruction *I) {
185 return I->getType()->isFPOrFPVectorTy();
187 static inline bool classof(const Value *V) {
188 return isa<Instruction>(V) && classof(cast<Instruction>(V));
193 /// ConcreteOperator - A helper template for defining operators for individual
195 template<typename SuperClass, unsigned Opc>
196 class ConcreteOperator : public SuperClass {
197 ~ConcreteOperator() LLVM_DELETED_FUNCTION;
199 static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
202 static inline bool classof(const Instruction *I) {
203 return I->getOpcode() == Opc;
205 static inline bool classof(const ConstantExpr *CE) {
206 return CE->getOpcode() == Opc;
208 static inline bool classof(const Value *V) {
209 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
210 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
215 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
216 ~AddOperator() LLVM_DELETED_FUNCTION;
219 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
220 ~SubOperator() LLVM_DELETED_FUNCTION;
223 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
224 ~MulOperator() LLVM_DELETED_FUNCTION;
227 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
228 ~ShlOperator() LLVM_DELETED_FUNCTION;
233 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
234 ~SDivOperator() LLVM_DELETED_FUNCTION;
237 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
238 ~UDivOperator() LLVM_DELETED_FUNCTION;
241 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
242 ~AShrOperator() LLVM_DELETED_FUNCTION;
245 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
246 ~LShrOperator() LLVM_DELETED_FUNCTION;
252 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
253 ~GEPOperator() LLVM_DELETED_FUNCTION;
256 IsInBounds = (1 << 0)
259 friend class GetElementPtrInst;
260 friend class ConstantExpr;
261 void setIsInBounds(bool B) {
262 SubclassOptionalData =
263 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
267 /// isInBounds - Test whether this is an inbounds GEP, as defined
269 bool isInBounds() const {
270 return SubclassOptionalData & IsInBounds;
273 inline op_iterator idx_begin() { return op_begin()+1; }
274 inline const_op_iterator idx_begin() const { return op_begin()+1; }
275 inline op_iterator idx_end() { return op_end(); }
276 inline const_op_iterator idx_end() const { return op_end(); }
278 Value *getPointerOperand() {
279 return getOperand(0);
281 const Value *getPointerOperand() const {
282 return getOperand(0);
284 static unsigned getPointerOperandIndex() {
285 return 0U; // get index for modifying correct operand
288 /// getPointerOperandType - Method to return the pointer operand as a
290 Type *getPointerOperandType() const {
291 return getPointerOperand()->getType();
294 /// getPointerAddressSpace - Method to return the address space of the
296 unsigned getPointerAddressSpace() const {
297 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
300 unsigned getNumIndices() const { // Note: always non-negative
301 return getNumOperands() - 1;
304 bool hasIndices() const {
305 return getNumOperands() > 1;
308 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
309 /// zeros. If so, the result pointer and the first operand have the same
310 /// value, just potentially different types.
311 bool hasAllZeroIndices() const {
312 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
313 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
321 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
322 /// constant integers. If so, the result pointer and the first operand have
323 /// a constant offset between them.
324 bool hasAllConstantIndices() const {
325 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
326 if (!isa<ConstantInt>(I))
333 } // End llvm namespace