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;
41 // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
42 // an overridden method that's not deleted in the base class. Cannot leave
43 // this unimplemented because that leads to an ODR-violation.
47 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
49 unsigned getOpcode() const {
50 if (const Instruction *I = dyn_cast<Instruction>(this))
51 return I->getOpcode();
52 return cast<ConstantExpr>(this)->getOpcode();
55 /// getOpcode - If V is an Instruction or ConstantExpr, return its
56 /// opcode. Otherwise return UserOp1.
58 static unsigned getOpcode(const Value *V) {
59 if (const Instruction *I = dyn_cast<Instruction>(V))
60 return I->getOpcode();
61 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
62 return CE->getOpcode();
63 return Instruction::UserOp1;
66 static inline bool classof(const Instruction *) { return true; }
67 static inline bool classof(const ConstantExpr *) { return true; }
68 static inline bool classof(const Value *V) {
69 return isa<Instruction>(V) || isa<ConstantExpr>(V);
73 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
74 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
75 /// despite that operator having the potential for overflow.
77 class OverflowingBinaryOperator : public Operator {
80 NoUnsignedWrap = (1 << 0),
81 NoSignedWrap = (1 << 1)
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 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 friend class BinaryOperator;
137 friend class ConstantExpr;
138 void setIsExact(bool B) {
139 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
143 /// isExact - Test whether this division is known to be exact, with
145 bool isExact() const {
146 return SubclassOptionalData & IsExact;
149 static bool isPossiblyExactOpcode(unsigned OpC) {
150 return OpC == Instruction::SDiv ||
151 OpC == Instruction::UDiv ||
152 OpC == Instruction::AShr ||
153 OpC == Instruction::LShr;
155 static inline bool classof(const ConstantExpr *CE) {
156 return isPossiblyExactOpcode(CE->getOpcode());
158 static inline bool classof(const Instruction *I) {
159 return isPossiblyExactOpcode(I->getOpcode());
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)));
167 /// Convenience struct for specifying and reasoning about fast-math flags.
168 struct FastMathFlags {
169 bool UnsafeAlgebra : 1;
172 bool NoSignedZeros : 1;
173 bool AllowReciprocal : 1;
175 FastMathFlags() : UnsafeAlgebra(false), NoNaNs(false), NoInfs(false),
176 NoSignedZeros(false), AllowReciprocal(false)
180 return UnsafeAlgebra || NoNaNs || NoInfs || NoSignedZeros ||
186 /// FPMathOperator - Utility class for floating point operations which can have
187 /// information about relaxed accuracy requirements attached to them.
188 class FPMathOperator : public Operator {
191 UnsafeAlgebra = (1 << 0),
194 NoSignedZeros = (1 << 3),
195 AllowReciprocal = (1 << 4)
199 friend class Instruction;
201 void setHasUnsafeAlgebra(bool B) {
202 SubclassOptionalData =
203 (SubclassOptionalData & ~UnsafeAlgebra) | (B * UnsafeAlgebra);
205 // Unsafe algebra implies all the others
209 setHasNoSignedZeros(true);
210 setHasAllowReciprocal(true);
213 void setHasNoNaNs(bool B) {
214 SubclassOptionalData =
215 (SubclassOptionalData & ~NoNaNs) | (B * NoNaNs);
217 void setHasNoInfs(bool B) {
218 SubclassOptionalData =
219 (SubclassOptionalData & ~NoInfs) | (B * NoInfs);
221 void setHasNoSignedZeros(bool B) {
222 SubclassOptionalData =
223 (SubclassOptionalData & ~NoSignedZeros) | (B * NoSignedZeros);
225 void setHasAllowReciprocal(bool B) {
226 SubclassOptionalData =
227 (SubclassOptionalData & ~AllowReciprocal) | (B * AllowReciprocal);
230 /// Convenience function for setting all the fast-math flags
231 void setFastMathFlags(FastMathFlags FMF) {
232 if (FMF.UnsafeAlgebra) {
233 // Set all the bits to true
234 setHasUnsafeAlgebra(true);
238 setHasUnsafeAlgebra(FMF.UnsafeAlgebra);
239 setHasNoNaNs(FMF.NoNaNs);
240 setHasNoInfs(FMF.NoInfs);
241 setHasNoSignedZeros(FMF.NoSignedZeros);
242 setHasAllowReciprocal(FMF.AllowReciprocal);
246 /// Test whether this operation is permitted to be
247 /// algebraically transformed, aka the 'A' fast-math property.
248 bool hasUnsafeAlgebra() const {
249 return (SubclassOptionalData & UnsafeAlgebra) != 0;
252 /// Test whether this operation's arguments and results are to be
253 /// treated as non-NaN, aka the 'N' fast-math property.
254 bool hasNoNaNs() const {
255 return (SubclassOptionalData & NoNaNs) != 0;
258 /// Test whether this operation's arguments and results are to be
259 /// treated as NoN-Inf, aka the 'I' fast-math property.
260 bool hasNoInfs() const {
261 return (SubclassOptionalData & NoInfs) != 0;
264 /// Test whether this operation can treat the sign of zero
265 /// as insignificant, aka the 'S' fast-math property.
266 bool hasNoSignedZeros() const {
267 return (SubclassOptionalData & NoSignedZeros) != 0;
270 /// Test whether this operation is permitted to use
271 /// reciprocal instead of division, aka the 'R' fast-math property.
272 bool hasAllowReciprocal() const {
273 return (SubclassOptionalData & AllowReciprocal) != 0;
276 /// Convenience function for getting all the fast-math flags
277 FastMathFlags getFastMathFlags() const {
279 FMF.UnsafeAlgebra = hasUnsafeAlgebra();
280 FMF.NoNaNs = hasNoNaNs();
281 FMF.NoInfs = hasNoInfs();
282 FMF.NoSignedZeros = hasNoSignedZeros();
283 FMF.AllowReciprocal = hasAllowReciprocal();
288 /// \brief Get the maximum error permitted by this operation in ULPs. An
289 /// accuracy of 0.0 means that the operation should be performed with the
290 /// default precision.
291 float getFPAccuracy() const;
293 static inline bool classof(const Instruction *I) {
294 return I->getType()->isFPOrFPVectorTy();
296 static inline bool classof(const Value *V) {
297 return isa<Instruction>(V) && classof(cast<Instruction>(V));
302 /// ConcreteOperator - A helper template for defining operators for individual
304 template<typename SuperClass, unsigned Opc>
305 class ConcreteOperator : public SuperClass {
307 static inline bool classof(const Instruction *I) {
308 return I->getOpcode() == Opc;
310 static inline bool classof(const ConstantExpr *CE) {
311 return CE->getOpcode() == Opc;
313 static inline bool classof(const Value *V) {
314 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
315 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
320 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
323 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
326 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
329 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
334 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
337 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
340 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
343 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
349 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
351 IsInBounds = (1 << 0)
354 friend class GetElementPtrInst;
355 friend class ConstantExpr;
356 void setIsInBounds(bool B) {
357 SubclassOptionalData =
358 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
362 /// isInBounds - Test whether this is an inbounds GEP, as defined
364 bool isInBounds() const {
365 return SubclassOptionalData & IsInBounds;
368 inline op_iterator idx_begin() { return op_begin()+1; }
369 inline const_op_iterator idx_begin() const { return op_begin()+1; }
370 inline op_iterator idx_end() { return op_end(); }
371 inline const_op_iterator idx_end() const { return op_end(); }
373 Value *getPointerOperand() {
374 return getOperand(0);
376 const Value *getPointerOperand() const {
377 return getOperand(0);
379 static unsigned getPointerOperandIndex() {
380 return 0U; // get index for modifying correct operand
383 /// getPointerOperandType - Method to return the pointer operand as a
385 Type *getPointerOperandType() const {
386 return getPointerOperand()->getType();
389 /// getPointerAddressSpace - Method to return the address space of the
391 unsigned getPointerAddressSpace() const {
392 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
395 unsigned getNumIndices() const { // Note: always non-negative
396 return getNumOperands() - 1;
399 bool hasIndices() const {
400 return getNumOperands() > 1;
403 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
404 /// zeros. If so, the result pointer and the first operand have the same
405 /// value, just potentially different types.
406 bool hasAllZeroIndices() const {
407 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
408 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
416 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
417 /// constant integers. If so, the result pointer and the first operand have
418 /// a constant offset between them.
419 bool hasAllConstantIndices() const {
420 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
421 if (!isa<ConstantInt>(I))
428 } // End llvm namespace