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/Instruction.h"
20 #include "llvm/Type.h"
24 class GetElementPtrInst;
28 /// Operator - This is a utility class that provides an abstraction for the
29 /// common functionality between Instructions and ConstantExprs.
31 class Operator : public User {
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.
43 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
45 unsigned getOpcode() const {
46 if (const Instruction *I = dyn_cast<Instruction>(this))
47 return I->getOpcode();
48 return cast<ConstantExpr>(this)->getOpcode();
51 /// getOpcode - If V is an Instruction or ConstantExpr, return its
52 /// opcode. Otherwise return UserOp1.
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;
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);
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() LLVM_DELETED_FUNCTION;
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 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;
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;
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)));
127 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
128 /// "exact", indicating that no bits are destroyed.
129 class PossiblyExactOperator : public Operator {
136 ~PossiblyExactOperator() LLVM_DELETED_FUNCTION;
138 friend class BinaryOperator;
139 friend class ConstantExpr;
140 void setIsExact(bool B) {
141 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
145 /// isExact - Test whether this division is known to be exact, with
147 bool isExact() const {
148 return SubclassOptionalData & IsExact;
151 static bool isPossiblyExactOpcode(unsigned OpC) {
152 return OpC == Instruction::SDiv ||
153 OpC == Instruction::UDiv ||
154 OpC == Instruction::AShr ||
155 OpC == Instruction::LShr;
157 static inline bool classof(const ConstantExpr *CE) {
158 return isPossiblyExactOpcode(CE->getOpcode());
160 static inline bool classof(const Instruction *I) {
161 return isPossiblyExactOpcode(I->getOpcode());
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)));
169 /// FPMathOperator - Utility class for floating point operations which can have
170 /// information about relaxed accuracy requirements attached to them.
171 class FPMathOperator : public Operator {
173 ~FPMathOperator() LLVM_DELETED_FUNCTION;
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;
182 static inline bool classof(const FPMathOperator *) { return true; }
183 static inline bool classof(const Instruction *I) {
184 return I->getType()->isFPOrFPVectorTy();
186 static inline bool classof(const Value *V) {
187 return isa<Instruction>(V) && classof(cast<Instruction>(V));
192 /// ConcreteOperator - A helper template for defining operators for individual
194 template<typename SuperClass, unsigned Opc>
195 class ConcreteOperator : public SuperClass {
196 ~ConcreteOperator() LLVM_DELETED_FUNCTION;
198 static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
201 static inline bool classof(const Instruction *I) {
202 return I->getOpcode() == Opc;
204 static inline bool classof(const ConstantExpr *CE) {
205 return CE->getOpcode() == Opc;
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)));
214 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
215 ~AddOperator() LLVM_DELETED_FUNCTION;
218 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
219 ~SubOperator() LLVM_DELETED_FUNCTION;
222 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
223 ~MulOperator() LLVM_DELETED_FUNCTION;
226 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
227 ~ShlOperator() LLVM_DELETED_FUNCTION;
232 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
233 ~SDivOperator() LLVM_DELETED_FUNCTION;
236 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
237 ~UDivOperator() LLVM_DELETED_FUNCTION;
240 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
241 ~AShrOperator() LLVM_DELETED_FUNCTION;
244 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
245 ~LShrOperator() LLVM_DELETED_FUNCTION;
251 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
252 ~GEPOperator() LLVM_DELETED_FUNCTION;
255 IsInBounds = (1 << 0)
258 friend class GetElementPtrInst;
259 friend class ConstantExpr;
260 void setIsInBounds(bool B) {
261 SubclassOptionalData =
262 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
266 /// isInBounds - Test whether this is an inbounds GEP, as defined
268 bool isInBounds() const {
269 return SubclassOptionalData & IsInBounds;
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(); }
277 Value *getPointerOperand() {
278 return getOperand(0);
280 const Value *getPointerOperand() const {
281 return getOperand(0);
283 static unsigned getPointerOperandIndex() {
284 return 0U; // get index for modifying correct operand
287 /// getPointerOperandType - Method to return the pointer operand as a
289 Type *getPointerOperandType() const {
290 return getPointerOperand()->getType();
293 unsigned getNumIndices() const { // Note: always non-negative
294 return getNumOperands() - 1;
297 bool hasIndices() const {
298 return getNumOperands() > 1;
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))
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))
326 } // End llvm namespace