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 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);
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.
74 class OverflowingBinaryOperator : public Operator {
77 NoUnsignedWrap = (1 << 0),
78 NoSignedWrap = (1 << 1)
82 ~OverflowingBinaryOperator(); // DO NOT IMPLEMENT
84 friend class BinaryOperator;
85 friend class ConstantExpr;
86 void setHasNoUnsignedWrap(bool B) {
87 SubclassOptionalData =
88 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
90 void setHasNoSignedWrap(bool B) {
91 SubclassOptionalData =
92 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
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;
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;
108 static inline bool classof(const Instruction *I) {
109 return I->getOpcode() == Instruction::Add ||
110 I->getOpcode() == Instruction::Sub ||
111 I->getOpcode() == Instruction::Mul ||
112 I->getOpcode() == Instruction::Shl;
114 static inline bool classof(const ConstantExpr *CE) {
115 return CE->getOpcode() == Instruction::Add ||
116 CE->getOpcode() == Instruction::Sub ||
117 CE->getOpcode() == Instruction::Mul ||
118 CE->getOpcode() == Instruction::Shl;
120 static inline bool classof(const Value *V) {
121 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
122 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
126 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
127 /// "exact", indicating that no bits are destroyed.
128 class PossiblyExactOperator : public Operator {
135 ~PossiblyExactOperator(); // DO NOT IMPLEMENT
137 friend class BinaryOperator;
138 friend class ConstantExpr;
139 void setIsExact(bool B) {
140 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
144 /// isExact - Test whether this division is known to be exact, with
146 bool isExact() const {
147 return SubclassOptionalData & IsExact;
150 static bool isPossiblyExactOpcode(unsigned OpC) {
151 return OpC == Instruction::SDiv ||
152 OpC == Instruction::UDiv ||
153 OpC == Instruction::AShr ||
154 OpC == Instruction::LShr;
156 static inline bool classof(const ConstantExpr *CE) {
157 return isPossiblyExactOpcode(CE->getOpcode());
159 static inline bool classof(const Instruction *I) {
160 return isPossiblyExactOpcode(I->getOpcode());
162 static inline bool classof(const Value *V) {
163 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
164 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
168 /// FPMathOperator - Utility class for floating point operations which can have
169 /// information about relaxed accuracy requirements attached to them.
170 class FPMathOperator : public Operator {
172 ~FPMathOperator(); // DO NOT IMPLEMENT
176 /// \brief Get the maximum error permitted by this operation in ULPs. An
177 /// accuracy of 0.0 means that the operation should be performed with the
178 /// default precision.
179 float getFPAccuracy() const;
181 static inline bool classof(const Instruction *I) {
182 return I->getType()->isFPOrFPVectorTy();
184 static inline bool classof(const Value *V) {
185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
190 /// ConcreteOperator - A helper template for defining operators for individual
192 template<typename SuperClass, unsigned Opc>
193 class ConcreteOperator : public SuperClass {
194 ~ConcreteOperator() LLVM_DELETED_FUNCTION;
196 static inline bool classof(const Instruction *I) {
197 return I->getOpcode() == Opc;
199 static inline bool classof(const ConstantExpr *CE) {
200 return CE->getOpcode() == Opc;
202 static inline bool classof(const Value *V) {
203 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
204 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
209 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
210 ~AddOperator() LLVM_DELETED_FUNCTION;
213 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
214 ~SubOperator() LLVM_DELETED_FUNCTION;
217 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
218 ~MulOperator() LLVM_DELETED_FUNCTION;
221 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
222 ~ShlOperator() LLVM_DELETED_FUNCTION;
227 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
228 ~SDivOperator() LLVM_DELETED_FUNCTION;
231 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
232 ~UDivOperator() LLVM_DELETED_FUNCTION;
235 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
236 ~AShrOperator() LLVM_DELETED_FUNCTION;
239 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
240 ~LShrOperator() LLVM_DELETED_FUNCTION;
246 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
247 ~GEPOperator() LLVM_DELETED_FUNCTION;
250 IsInBounds = (1 << 0)
253 friend class GetElementPtrInst;
254 friend class ConstantExpr;
255 void setIsInBounds(bool B) {
256 SubclassOptionalData =
257 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
261 /// isInBounds - Test whether this is an inbounds GEP, as defined
263 bool isInBounds() const {
264 return SubclassOptionalData & IsInBounds;
267 inline op_iterator idx_begin() { return op_begin()+1; }
268 inline const_op_iterator idx_begin() const { return op_begin()+1; }
269 inline op_iterator idx_end() { return op_end(); }
270 inline const_op_iterator idx_end() const { return op_end(); }
272 Value *getPointerOperand() {
273 return getOperand(0);
275 const Value *getPointerOperand() const {
276 return getOperand(0);
278 static unsigned getPointerOperandIndex() {
279 return 0U; // get index for modifying correct operand
282 /// getPointerOperandType - Method to return the pointer operand as a
284 Type *getPointerOperandType() const {
285 return getPointerOperand()->getType();
288 /// getPointerAddressSpace - Method to return the address space of the
290 unsigned getPointerAddressSpace() const {
291 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
294 unsigned getNumIndices() const { // Note: always non-negative
295 return getNumOperands() - 1;
298 bool hasIndices() const {
299 return getNumOperands() > 1;
302 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
303 /// zeros. If so, the result pointer and the first operand have the same
304 /// value, just potentially different types.
305 bool hasAllZeroIndices() const {
306 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
307 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
315 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
316 /// constant integers. If so, the result pointer and the first operand have
317 /// a constant offset between them.
318 bool hasAllConstantIndices() const {
319 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
320 if (!isa<ConstantInt>(I))
327 } // End llvm namespace