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_IR_OPERATOR_H
16 #define LLVM_IR_OPERATOR_H
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/GetElementPtrTypeIterator.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/IR/Type.h"
27 class GetElementPtrInst;
31 /// This is a utility class that provides an abstraction for the common
32 /// functionality between Instructions and ConstantExprs.
33 class Operator : public User {
35 // The Operator class is intended to be used as a utility, and is never itself
37 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
38 void *operator new(size_t s) LLVM_DELETED_FUNCTION;
39 Operator() LLVM_DELETED_FUNCTION;
42 // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
43 // an overridden method that's not deleted in the base class. Cannot leave
44 // this unimplemented because that leads to an ODR-violation.
48 /// 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 /// If V is an Instruction or ConstantExpr, return its opcode.
56 /// 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 /// Utility class for integer arithmetic operators which may exhibit overflow -
73 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
74 /// the potential for overflow.
75 class OverflowingBinaryOperator : public Operator {
78 NoUnsignedWrap = (1 << 0),
79 NoSignedWrap = (1 << 1)
83 friend class BinaryOperator;
84 friend class ConstantExpr;
85 void setHasNoUnsignedWrap(bool B) {
86 SubclassOptionalData =
87 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
89 void setHasNoSignedWrap(bool B) {
90 SubclassOptionalData =
91 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
95 /// Test whether this operation is known to never
96 /// undergo unsigned overflow, aka the nuw property.
97 bool hasNoUnsignedWrap() const {
98 return SubclassOptionalData & NoUnsignedWrap;
101 /// Test whether this operation is known to never
102 /// undergo signed overflow, aka the nsw property.
103 bool hasNoSignedWrap() const {
104 return (SubclassOptionalData & NoSignedWrap) != 0;
107 static inline bool classof(const Instruction *I) {
108 return I->getOpcode() == Instruction::Add ||
109 I->getOpcode() == Instruction::Sub ||
110 I->getOpcode() == Instruction::Mul ||
111 I->getOpcode() == Instruction::Shl;
113 static inline bool classof(const ConstantExpr *CE) {
114 return CE->getOpcode() == Instruction::Add ||
115 CE->getOpcode() == Instruction::Sub ||
116 CE->getOpcode() == Instruction::Mul ||
117 CE->getOpcode() == Instruction::Shl;
119 static inline bool classof(const Value *V) {
120 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
121 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
125 /// A udiv or sdiv instruction, which can be marked as "exact",
126 /// indicating that no bits are destroyed.
127 class PossiblyExactOperator : public Operator {
134 friend class BinaryOperator;
135 friend class ConstantExpr;
136 void setIsExact(bool B) {
137 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
141 /// Test whether this division is known to be exact, with zero remainder.
142 bool isExact() const {
143 return SubclassOptionalData & IsExact;
146 static bool isPossiblyExactOpcode(unsigned OpC) {
147 return OpC == Instruction::SDiv ||
148 OpC == Instruction::UDiv ||
149 OpC == Instruction::AShr ||
150 OpC == Instruction::LShr;
152 static inline bool classof(const ConstantExpr *CE) {
153 return isPossiblyExactOpcode(CE->getOpcode());
155 static inline bool classof(const Instruction *I) {
156 return isPossiblyExactOpcode(I->getOpcode());
158 static inline bool classof(const Value *V) {
159 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
160 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
164 /// Convenience struct for specifying and reasoning about fast-math flags.
165 class FastMathFlags {
167 friend class FPMathOperator;
169 FastMathFlags(unsigned F) : Flags(F) { }
173 UnsafeAlgebra = (1 << 0),
176 NoSignedZeros = (1 << 3),
177 AllowReciprocal = (1 << 4)
180 FastMathFlags() : Flags(0)
183 /// Whether any flag is set
184 bool any() { return Flags != 0; }
186 /// Set all the flags to false
187 void clear() { Flags = 0; }
190 bool noNaNs() { return 0 != (Flags & NoNaNs); }
191 bool noInfs() { return 0 != (Flags & NoInfs); }
192 bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); }
193 bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
194 bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); }
197 void setNoNaNs() { Flags |= NoNaNs; }
198 void setNoInfs() { Flags |= NoInfs; }
199 void setNoSignedZeros() { Flags |= NoSignedZeros; }
200 void setAllowReciprocal() { Flags |= AllowReciprocal; }
201 void setUnsafeAlgebra() {
202 Flags |= UnsafeAlgebra;
206 setAllowReciprocal();
209 void operator&=(const FastMathFlags &OtherFlags) {
210 Flags &= OtherFlags.Flags;
215 /// Utility class for floating point operations which can have
216 /// information about relaxed accuracy requirements attached to them.
217 class FPMathOperator : public Operator {
219 friend class Instruction;
221 void setHasUnsafeAlgebra(bool B) {
222 SubclassOptionalData =
223 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
224 (B * FastMathFlags::UnsafeAlgebra);
226 // Unsafe algebra implies all the others
230 setHasNoSignedZeros(true);
231 setHasAllowReciprocal(true);
234 void setHasNoNaNs(bool B) {
235 SubclassOptionalData =
236 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
237 (B * FastMathFlags::NoNaNs);
239 void setHasNoInfs(bool B) {
240 SubclassOptionalData =
241 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
242 (B * FastMathFlags::NoInfs);
244 void setHasNoSignedZeros(bool B) {
245 SubclassOptionalData =
246 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
247 (B * FastMathFlags::NoSignedZeros);
249 void setHasAllowReciprocal(bool B) {
250 SubclassOptionalData =
251 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
252 (B * FastMathFlags::AllowReciprocal);
255 /// Convenience function for setting multiple fast-math flags.
256 /// FMF is a mask of the bits to set.
257 void setFastMathFlags(FastMathFlags FMF) {
258 SubclassOptionalData |= FMF.Flags;
261 /// Convenience function for copying all fast-math flags.
262 /// All values in FMF are transferred to this operator.
263 void copyFastMathFlags(FastMathFlags FMF) {
264 SubclassOptionalData = FMF.Flags;
268 /// Test whether this operation is permitted to be
269 /// algebraically transformed, aka the 'A' fast-math property.
270 bool hasUnsafeAlgebra() const {
271 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
274 /// Test whether this operation's arguments and results are to be
275 /// treated as non-NaN, aka the 'N' fast-math property.
276 bool hasNoNaNs() const {
277 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
280 /// Test whether this operation's arguments and results are to be
281 /// treated as NoN-Inf, aka the 'I' fast-math property.
282 bool hasNoInfs() const {
283 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
286 /// Test whether this operation can treat the sign of zero
287 /// as insignificant, aka the 'S' fast-math property.
288 bool hasNoSignedZeros() const {
289 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
292 /// Test whether this operation is permitted to use
293 /// reciprocal instead of division, aka the 'R' fast-math property.
294 bool hasAllowReciprocal() const {
295 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
298 /// Convenience function for getting all the fast-math flags
299 FastMathFlags getFastMathFlags() const {
300 return FastMathFlags(SubclassOptionalData);
303 /// \brief Get the maximum error permitted by this operation in ULPs. An
304 /// accuracy of 0.0 means that the operation should be performed with the
305 /// default precision.
306 float getFPAccuracy() const;
308 static inline bool classof(const Instruction *I) {
309 return I->getType()->isFPOrFPVectorTy();
311 static inline bool classof(const Value *V) {
312 return isa<Instruction>(V) && classof(cast<Instruction>(V));
317 /// A helper template for defining operators for individual opcodes.
318 template<typename SuperClass, unsigned Opc>
319 class ConcreteOperator : public SuperClass {
321 static inline bool classof(const Instruction *I) {
322 return I->getOpcode() == Opc;
324 static inline bool classof(const ConstantExpr *CE) {
325 return CE->getOpcode() == Opc;
327 static inline bool classof(const Value *V) {
328 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
329 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
334 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
337 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
340 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
343 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
348 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
351 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
354 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
357 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
363 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
365 IsInBounds = (1 << 0)
368 friend class GetElementPtrInst;
369 friend class ConstantExpr;
370 void setIsInBounds(bool B) {
371 SubclassOptionalData =
372 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
376 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
377 bool isInBounds() const {
378 return SubclassOptionalData & IsInBounds;
381 inline op_iterator idx_begin() { return op_begin()+1; }
382 inline const_op_iterator idx_begin() const { return op_begin()+1; }
383 inline op_iterator idx_end() { return op_end(); }
384 inline const_op_iterator idx_end() const { return op_end(); }
386 Value *getPointerOperand() {
387 return getOperand(0);
389 const Value *getPointerOperand() const {
390 return getOperand(0);
392 static unsigned getPointerOperandIndex() {
393 return 0U; // get index for modifying correct operand
396 /// Method to return the pointer operand as a PointerType.
397 Type *getPointerOperandType() const {
398 return getPointerOperand()->getType();
401 /// Method to return the address space of the pointer operand.
402 unsigned getPointerAddressSpace() const {
403 return getPointerOperandType()->getPointerAddressSpace();
406 unsigned getNumIndices() const { // Note: always non-negative
407 return getNumOperands() - 1;
410 bool hasIndices() const {
411 return getNumOperands() > 1;
414 /// Return true if all of the indices of this GEP are zeros.
415 /// If so, the result pointer and the first operand have the same
416 /// value, just potentially different types.
417 bool hasAllZeroIndices() const {
418 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
419 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
427 /// Return true if all of the indices of this GEP are constant integers.
428 /// If so, the result pointer and the first operand have
429 /// a constant offset between them.
430 bool hasAllConstantIndices() const {
431 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
432 if (!isa<ConstantInt>(I))
438 /// \brief Accumulate the constant address offset of this GEP if possible.
440 /// This routine accepts an APInt into which it will accumulate the constant
441 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
442 /// all-constant, it returns false and the value of the offset APInt is
443 /// undefined (it is *not* preserved!). The APInt passed into this routine
444 /// must be at exactly as wide as the IntPtr type for the address space of the
445 /// base GEP pointer.
446 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
447 assert(Offset.getBitWidth() ==
448 DL.getPointerSizeInBits(getPointerAddressSpace()) &&
449 "The offset must have exactly as many bits as our pointer.");
451 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
453 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
459 // Handle a struct index, which adds its field offset to the pointer.
460 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
461 unsigned ElementIdx = OpC->getZExtValue();
462 const StructLayout *SL = DL.getStructLayout(STy);
463 Offset += APInt(Offset.getBitWidth(),
464 SL->getElementOffset(ElementIdx));
468 // For array or vector indices, scale the index by the size of the type.
469 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
470 Offset += Index * APInt(Offset.getBitWidth(),
471 DL.getTypeAllocSize(GTI.getIndexedType()));
478 class PtrToIntOperator
479 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
480 friend class PtrToInt;
481 friend class ConstantExpr;
484 Value *getPointerOperand() {
485 return getOperand(0);
487 const Value *getPointerOperand() const {
488 return getOperand(0);
490 static unsigned getPointerOperandIndex() {
491 return 0U; // get index for modifying correct operand
494 /// Method to return the pointer operand as a PointerType.
495 Type *getPointerOperandType() const {
496 return getPointerOperand()->getType();
499 /// Method to return the address space of the pointer operand.
500 unsigned getPointerAddressSpace() const {
501 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
506 } // End llvm namespace