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;
40 // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
41 // an overridden method that's not deleted in the base class. Cannot leave
42 // this unimplemented because that leads to an ODR-violation.
46 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
48 unsigned getOpcode() const {
49 if (const Instruction *I = dyn_cast<Instruction>(this))
50 return I->getOpcode();
51 return cast<ConstantExpr>(this)->getOpcode();
54 /// getOpcode - If V is an Instruction or ConstantExpr, return its
55 /// opcode. Otherwise return UserOp1.
57 static unsigned getOpcode(const Value *V) {
58 if (const Instruction *I = dyn_cast<Instruction>(V))
59 return I->getOpcode();
60 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
61 return CE->getOpcode();
62 return Instruction::UserOp1;
65 static inline bool classof(const Instruction *) { return true; }
66 static inline bool classof(const ConstantExpr *) { return true; }
67 static inline bool classof(const Value *V) {
68 return isa<Instruction>(V) || isa<ConstantExpr>(V);
72 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
73 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
74 /// despite that operator having the potential for overflow.
76 class OverflowingBinaryOperator : public Operator {
79 NoUnsignedWrap = (1 << 0),
80 NoSignedWrap = (1 << 1)
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 friend class BinaryOperator;
136 friend class ConstantExpr;
137 void setIsExact(bool B) {
138 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
142 /// isExact - Test whether this division is known to be exact, with
144 bool isExact() const {
145 return SubclassOptionalData & IsExact;
148 static bool isPossiblyExactOpcode(unsigned OpC) {
149 return OpC == Instruction::SDiv ||
150 OpC == Instruction::UDiv ||
151 OpC == Instruction::AShr ||
152 OpC == Instruction::LShr;
154 static inline bool classof(const ConstantExpr *CE) {
155 return isPossiblyExactOpcode(CE->getOpcode());
157 static inline bool classof(const Instruction *I) {
158 return isPossiblyExactOpcode(I->getOpcode());
160 static inline bool classof(const Value *V) {
161 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
162 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
166 /// FPMathOperator - Utility class for floating point operations which can have
167 /// information about relaxed accuracy requirements attached to them.
168 class FPMathOperator : public Operator {
171 /// \brief Get the maximum error permitted by this operation in ULPs. An
172 /// accuracy of 0.0 means that the operation should be performed with the
173 /// default precision.
174 float getFPAccuracy() const;
176 static inline bool classof(const Instruction *I) {
177 return I->getType()->isFPOrFPVectorTy();
179 static inline bool classof(const Value *V) {
180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
185 /// ConcreteOperator - A helper template for defining operators for individual
187 template<typename SuperClass, unsigned Opc>
188 class ConcreteOperator : public SuperClass {
190 static inline bool classof(const Instruction *I) {
191 return I->getOpcode() == Opc;
193 static inline bool classof(const ConstantExpr *CE) {
194 return CE->getOpcode() == Opc;
196 static inline bool classof(const Value *V) {
197 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
198 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
203 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
206 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
209 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
212 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
217 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
220 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
223 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
226 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
232 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
234 IsInBounds = (1 << 0)
237 friend class GetElementPtrInst;
238 friend class ConstantExpr;
239 void setIsInBounds(bool B) {
240 SubclassOptionalData =
241 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
245 /// isInBounds - Test whether this is an inbounds GEP, as defined
247 bool isInBounds() const {
248 return SubclassOptionalData & IsInBounds;
251 inline op_iterator idx_begin() { return op_begin()+1; }
252 inline const_op_iterator idx_begin() const { return op_begin()+1; }
253 inline op_iterator idx_end() { return op_end(); }
254 inline const_op_iterator idx_end() const { return op_end(); }
256 Value *getPointerOperand() {
257 return getOperand(0);
259 const Value *getPointerOperand() const {
260 return getOperand(0);
262 static unsigned getPointerOperandIndex() {
263 return 0U; // get index for modifying correct operand
266 /// getPointerOperandType - Method to return the pointer operand as a
268 Type *getPointerOperandType() const {
269 return getPointerOperand()->getType();
272 /// getPointerAddressSpace - Method to return the address space of the
274 unsigned getPointerAddressSpace() const {
275 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
278 unsigned getNumIndices() const { // Note: always non-negative
279 return getNumOperands() - 1;
282 bool hasIndices() const {
283 return getNumOperands() > 1;
286 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
287 /// zeros. If so, the result pointer and the first operand have the same
288 /// value, just potentially different types.
289 bool hasAllZeroIndices() const {
290 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
291 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
299 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
300 /// constant integers. If so, the result pointer and the first operand have
301 /// a constant offset between them.
302 bool hasAllConstantIndices() const {
303 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
304 if (!isa<ConstantInt>(I))
311 } // End llvm namespace