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 /// Operator - This is a utility class that provides an abstraction for the
32 /// common functionality between Instructions and ConstantExprs.
34 class Operator : public User {
36 // The Operator class is intended to be used as a utility, and is never itself
38 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
39 void *operator new(size_t s) LLVM_DELETED_FUNCTION;
40 Operator() LLVM_DELETED_FUNCTION;
43 // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
44 // an overridden method that's not deleted in the base class. Cannot leave
45 // this unimplemented because that leads to an ODR-violation.
49 /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
51 unsigned getOpcode() const {
52 if (const Instruction *I = dyn_cast<Instruction>(this))
53 return I->getOpcode();
54 return cast<ConstantExpr>(this)->getOpcode();
57 /// getOpcode - If V is an Instruction or ConstantExpr, return its
58 /// opcode. Otherwise return UserOp1.
60 static unsigned getOpcode(const Value *V) {
61 if (const Instruction *I = dyn_cast<Instruction>(V))
62 return I->getOpcode();
63 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
64 return CE->getOpcode();
65 return Instruction::UserOp1;
68 static inline bool classof(const Instruction *) { return true; }
69 static inline bool classof(const ConstantExpr *) { return true; }
70 static inline bool classof(const Value *V) {
71 return isa<Instruction>(V) || isa<ConstantExpr>(V);
75 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
76 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
77 /// despite that operator having the potential for overflow.
79 class OverflowingBinaryOperator : public Operator {
82 NoUnsignedWrap = (1 << 0),
83 NoSignedWrap = (1 << 1)
87 friend class BinaryOperator;
88 friend class ConstantExpr;
89 void setHasNoUnsignedWrap(bool B) {
90 SubclassOptionalData =
91 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
93 void setHasNoSignedWrap(bool B) {
94 SubclassOptionalData =
95 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
99 /// hasNoUnsignedWrap - Test whether this operation is known to never
100 /// undergo unsigned overflow, aka the nuw property.
101 bool hasNoUnsignedWrap() const {
102 return SubclassOptionalData & NoUnsignedWrap;
105 /// hasNoSignedWrap - Test whether this operation is known to never
106 /// undergo signed overflow, aka the nsw property.
107 bool hasNoSignedWrap() const {
108 return (SubclassOptionalData & NoSignedWrap) != 0;
111 static inline bool classof(const Instruction *I) {
112 return I->getOpcode() == Instruction::Add ||
113 I->getOpcode() == Instruction::Sub ||
114 I->getOpcode() == Instruction::Mul ||
115 I->getOpcode() == Instruction::Shl;
117 static inline bool classof(const ConstantExpr *CE) {
118 return CE->getOpcode() == Instruction::Add ||
119 CE->getOpcode() == Instruction::Sub ||
120 CE->getOpcode() == Instruction::Mul ||
121 CE->getOpcode() == Instruction::Shl;
123 static inline bool classof(const Value *V) {
124 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
125 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
129 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
130 /// "exact", indicating that no bits are destroyed.
131 class PossiblyExactOperator : public Operator {
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 /// Convenience struct for specifying and reasoning about fast-math flags.
170 class FastMathFlags {
172 friend class FPMathOperator;
174 FastMathFlags(unsigned F) : Flags(F) { }
178 UnsafeAlgebra = (1 << 0),
181 NoSignedZeros = (1 << 3),
182 AllowReciprocal = (1 << 4)
185 FastMathFlags() : Flags(0)
188 /// Whether any flag is set
189 bool any() { return Flags != 0; }
191 /// Set all the flags to false
192 void clear() { Flags = 0; }
195 bool noNaNs() { return 0 != (Flags & NoNaNs); }
196 bool noInfs() { return 0 != (Flags & NoInfs); }
197 bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); }
198 bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
199 bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); }
202 void setNoNaNs() { Flags |= NoNaNs; }
203 void setNoInfs() { Flags |= NoInfs; }
204 void setNoSignedZeros() { Flags |= NoSignedZeros; }
205 void setAllowReciprocal() { Flags |= AllowReciprocal; }
206 void setUnsafeAlgebra() {
207 Flags |= UnsafeAlgebra;
211 setAllowReciprocal();
214 void operator&=(const FastMathFlags &OtherFlags) {
215 Flags &= OtherFlags.Flags;
220 /// FPMathOperator - Utility class for floating point operations which can have
221 /// information about relaxed accuracy requirements attached to them.
222 class FPMathOperator : public Operator {
224 friend class Instruction;
226 void setHasUnsafeAlgebra(bool B) {
227 SubclassOptionalData =
228 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
229 (B * FastMathFlags::UnsafeAlgebra);
231 // Unsafe algebra implies all the others
235 setHasNoSignedZeros(true);
236 setHasAllowReciprocal(true);
239 void setHasNoNaNs(bool B) {
240 SubclassOptionalData =
241 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
242 (B * FastMathFlags::NoNaNs);
244 void setHasNoInfs(bool B) {
245 SubclassOptionalData =
246 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
247 (B * FastMathFlags::NoInfs);
249 void setHasNoSignedZeros(bool B) {
250 SubclassOptionalData =
251 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
252 (B * FastMathFlags::NoSignedZeros);
254 void setHasAllowReciprocal(bool B) {
255 SubclassOptionalData =
256 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
257 (B * FastMathFlags::AllowReciprocal);
260 /// Convenience function for setting multiple fast-math flags.
261 /// FMF is a mask of the bits to set.
262 void setFastMathFlags(FastMathFlags FMF) {
263 SubclassOptionalData |= FMF.Flags;
266 /// Convenience function for copying all fast-math flags.
267 /// All values in FMF are transferred to this operator.
268 void copyFastMathFlags(FastMathFlags FMF) {
269 SubclassOptionalData = FMF.Flags;
273 /// Test whether this operation is permitted to be
274 /// algebraically transformed, aka the 'A' fast-math property.
275 bool hasUnsafeAlgebra() const {
276 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
279 /// Test whether this operation's arguments and results are to be
280 /// treated as non-NaN, aka the 'N' fast-math property.
281 bool hasNoNaNs() const {
282 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
285 /// Test whether this operation's arguments and results are to be
286 /// treated as NoN-Inf, aka the 'I' fast-math property.
287 bool hasNoInfs() const {
288 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
291 /// Test whether this operation can treat the sign of zero
292 /// as insignificant, aka the 'S' fast-math property.
293 bool hasNoSignedZeros() const {
294 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
297 /// Test whether this operation is permitted to use
298 /// reciprocal instead of division, aka the 'R' fast-math property.
299 bool hasAllowReciprocal() const {
300 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
303 /// Convenience function for getting all the fast-math flags
304 FastMathFlags getFastMathFlags() const {
305 return FastMathFlags(SubclassOptionalData);
308 /// \brief Get the maximum error permitted by this operation in ULPs. An
309 /// accuracy of 0.0 means that the operation should be performed with the
310 /// default precision.
311 float getFPAccuracy() const;
313 static inline bool classof(const Instruction *I) {
314 return I->getType()->isFPOrFPVectorTy();
316 static inline bool classof(const Value *V) {
317 return isa<Instruction>(V) && classof(cast<Instruction>(V));
322 /// ConcreteOperator - A helper template for defining operators for individual
324 template<typename SuperClass, unsigned Opc>
325 class ConcreteOperator : public SuperClass {
327 static inline bool classof(const Instruction *I) {
328 return I->getOpcode() == Opc;
330 static inline bool classof(const ConstantExpr *CE) {
331 return CE->getOpcode() == Opc;
333 static inline bool classof(const Value *V) {
334 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
335 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
340 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
343 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
346 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
349 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
354 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
357 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
360 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
363 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
369 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
371 IsInBounds = (1 << 0)
374 friend class GetElementPtrInst;
375 friend class ConstantExpr;
376 void setIsInBounds(bool B) {
377 SubclassOptionalData =
378 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
382 /// isInBounds - Test whether this is an inbounds GEP, as defined
384 bool isInBounds() const {
385 return SubclassOptionalData & IsInBounds;
388 inline op_iterator idx_begin() { return op_begin()+1; }
389 inline const_op_iterator idx_begin() const { return op_begin()+1; }
390 inline op_iterator idx_end() { return op_end(); }
391 inline const_op_iterator idx_end() const { return op_end(); }
393 Value *getPointerOperand() {
394 return getOperand(0);
396 const Value *getPointerOperand() const {
397 return getOperand(0);
399 static unsigned getPointerOperandIndex() {
400 return 0U; // get index for modifying correct operand
403 /// getPointerOperandType - Method to return the pointer operand as a
405 Type *getPointerOperandType() const {
406 return getPointerOperand()->getType();
409 /// getPointerAddressSpace - Method to return the address space of the
411 unsigned getPointerAddressSpace() const {
412 return getPointerOperandType()->getPointerAddressSpace();
415 unsigned getNumIndices() const { // Note: always non-negative
416 return getNumOperands() - 1;
419 bool hasIndices() const {
420 return getNumOperands() > 1;
423 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
424 /// zeros. If so, the result pointer and the first operand have the same
425 /// value, just potentially different types.
426 bool hasAllZeroIndices() const {
427 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
428 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
436 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
437 /// constant integers. If so, the result pointer and the first operand have
438 /// a constant offset between them.
439 bool hasAllConstantIndices() const {
440 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
441 if (!isa<ConstantInt>(I))
447 /// \brief Accumulate the constant address offset of this GEP if possible.
449 /// This routine accepts an APInt into which it will accumulate the constant
450 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
451 /// all-constant, it returns false and the value of the offset APInt is
452 /// undefined (it is *not* preserved!). The APInt passed into this routine
453 /// must be at exactly as wide as the IntPtr type for the address space of the
454 /// base GEP pointer.
455 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
456 assert(Offset.getBitWidth() ==
457 DL.getPointerSizeInBits(getPointerAddressSpace()) &&
458 "The offset must have exactly as many bits as our pointer.");
460 for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
462 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
468 // Handle a struct index, which adds its field offset to the pointer.
469 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
470 unsigned ElementIdx = OpC->getZExtValue();
471 const StructLayout *SL = DL.getStructLayout(STy);
472 Offset += APInt(Offset.getBitWidth(),
473 SL->getElementOffset(ElementIdx));
477 // For array or vector indices, scale the index by the size of the type.
478 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
479 Offset += Index * APInt(Offset.getBitWidth(),
480 DL.getTypeAllocSize(GTI.getIndexedType()));
487 class PtrToIntOperator
488 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
489 friend class PtrToInt;
490 friend class ConstantExpr;
493 Value *getPointerOperand() {
494 return getOperand(0);
496 const Value *getPointerOperand() const {
497 return getOperand(0);
499 static unsigned getPointerOperandIndex() {
500 return 0U; // get index for modifying correct operand
503 /// getPointerOperandType - Method to return the pointer operand as a
505 Type *getPointerOperandType() const {
506 return getPointerOperand()->getType();
509 /// getPointerAddressSpace - Method to return the address space of the
511 unsigned getPointerAddressSpace() const {
512 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
517 } // End llvm namespace