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> {
361 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
365 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
367 IsInBounds = (1 << 0)
370 friend class GetElementPtrInst;
371 friend class ConstantExpr;
372 void setIsInBounds(bool B) {
373 SubclassOptionalData =
374 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
378 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
379 bool isInBounds() const {
380 return SubclassOptionalData & IsInBounds;
383 inline op_iterator idx_begin() { return op_begin()+1; }
384 inline const_op_iterator idx_begin() const { return op_begin()+1; }
385 inline op_iterator idx_end() { return op_end(); }
386 inline const_op_iterator idx_end() const { return op_end(); }
388 Value *getPointerOperand() {
389 return getOperand(0);
391 const Value *getPointerOperand() const {
392 return getOperand(0);
394 static unsigned getPointerOperandIndex() {
395 return 0U; // get index for modifying correct operand
398 /// Method to return the pointer operand as a PointerType.
399 Type *getPointerOperandType() const {
400 return getPointerOperand()->getType();
403 /// Method to return the address space of the pointer operand.
404 unsigned getPointerAddressSpace() const {
405 return getPointerOperandType()->getPointerAddressSpace();
408 unsigned getNumIndices() const { // Note: always non-negative
409 return getNumOperands() - 1;
412 bool hasIndices() const {
413 return getNumOperands() > 1;
416 /// Return true if all of the indices of this GEP are zeros.
417 /// If so, the result pointer and the first operand have the same
418 /// value, just potentially different types.
419 bool hasAllZeroIndices() const {
420 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
421 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
429 /// Return true if all of the indices of this GEP are constant integers.
430 /// If so, the result pointer and the first operand have
431 /// a constant offset between them.
432 bool hasAllConstantIndices() const {
433 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
434 if (!isa<ConstantInt>(I))
440 /// \brief Accumulate the constant address offset of this GEP if possible.
442 /// This routine accepts an APInt into which it will accumulate the constant
443 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
444 /// all-constant, it returns false and the value of the offset APInt is
445 /// undefined (it is *not* preserved!). The APInt passed into this routine
446 /// must be at exactly as wide as the IntPtr type for the address space of the
447 /// base GEP pointer.
448 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
449 assert(Offset.getBitWidth() ==
450 DL.getPointerSizeInBits(getPointerAddressSpace()) &&
451 "The offset must have exactly as many bits as our pointer.");
453 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
455 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
461 // Handle a struct index, which adds its field offset to the pointer.
462 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
463 unsigned ElementIdx = OpC->getZExtValue();
464 const StructLayout *SL = DL.getStructLayout(STy);
465 Offset += APInt(Offset.getBitWidth(),
466 SL->getElementOffset(ElementIdx));
470 // For array or vector indices, scale the index by the size of the type.
471 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
472 Offset += Index * APInt(Offset.getBitWidth(),
473 DL.getTypeAllocSize(GTI.getIndexedType()));
480 class PtrToIntOperator
481 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
482 friend class PtrToInt;
483 friend class ConstantExpr;
486 Value *getPointerOperand() {
487 return getOperand(0);
489 const Value *getPointerOperand() const {
490 return getOperand(0);
492 static unsigned getPointerOperandIndex() {
493 return 0U; // get index for modifying correct operand
496 /// Method to return the pointer operand as a PointerType.
497 Type *getPointerOperandType() const {
498 return getPointerOperand()->getType();
501 /// Method to return the address space of the pointer operand.
502 unsigned getPointerAddressSpace() const {
503 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
508 } // End llvm namespace