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/Instruction.h"
22 #include "llvm/IR/Type.h"
26 class GetElementPtrInst;
30 /// This is a utility class that provides an abstraction for the common
31 /// functionality between Instructions and ConstantExprs.
32 class Operator : public User {
34 // The Operator class is intended to be used as a utility, and is never itself
36 void *operator new(size_t, unsigned) = delete;
37 void *operator new(size_t s) = delete;
41 // NOTE: Cannot use = delete 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 /// 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 /// If V is an Instruction or ConstantExpr, return its opcode.
55 /// Otherwise return UserOp1.
56 static unsigned getOpcode(const Value *V) {
57 if (const Instruction *I = dyn_cast<Instruction>(V))
58 return I->getOpcode();
59 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
60 return CE->getOpcode();
61 return Instruction::UserOp1;
64 static inline bool classof(const Instruction *) { return true; }
65 static inline bool classof(const ConstantExpr *) { return true; }
66 static inline bool classof(const Value *V) {
67 return isa<Instruction>(V) || isa<ConstantExpr>(V);
71 /// Utility class for integer arithmetic operators which may exhibit overflow -
72 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
73 /// the potential for overflow.
74 class OverflowingBinaryOperator : public Operator {
77 NoUnsignedWrap = (1 << 0),
78 NoSignedWrap = (1 << 1)
82 friend class BinaryOperator;
83 friend class ConstantExpr;
84 void setHasNoUnsignedWrap(bool B) {
85 SubclassOptionalData =
86 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
88 void setHasNoSignedWrap(bool B) {
89 SubclassOptionalData =
90 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
94 /// Test whether this operation is known to never
95 /// undergo unsigned overflow, aka the nuw property.
96 bool hasNoUnsignedWrap() const {
97 return SubclassOptionalData & NoUnsignedWrap;
100 /// Test whether this operation is known to never
101 /// undergo signed overflow, aka the nsw property.
102 bool hasNoSignedWrap() const {
103 return (SubclassOptionalData & NoSignedWrap) != 0;
106 static inline bool classof(const Instruction *I) {
107 return I->getOpcode() == Instruction::Add ||
108 I->getOpcode() == Instruction::Sub ||
109 I->getOpcode() == Instruction::Mul ||
110 I->getOpcode() == Instruction::Shl;
112 static inline bool classof(const ConstantExpr *CE) {
113 return CE->getOpcode() == Instruction::Add ||
114 CE->getOpcode() == Instruction::Sub ||
115 CE->getOpcode() == Instruction::Mul ||
116 CE->getOpcode() == Instruction::Shl;
118 static inline bool classof(const Value *V) {
119 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
120 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
124 /// A udiv or sdiv instruction, which can be marked as "exact",
125 /// indicating that no bits are destroyed.
126 class PossiblyExactOperator : public Operator {
133 friend class BinaryOperator;
134 friend class ConstantExpr;
135 void setIsExact(bool B) {
136 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
140 /// Test whether this division is known to be exact, with zero remainder.
141 bool isExact() const {
142 return SubclassOptionalData & IsExact;
145 static bool isPossiblyExactOpcode(unsigned OpC) {
146 return OpC == Instruction::SDiv ||
147 OpC == Instruction::UDiv ||
148 OpC == Instruction::AShr ||
149 OpC == Instruction::LShr;
151 static inline bool classof(const ConstantExpr *CE) {
152 return isPossiblyExactOpcode(CE->getOpcode());
154 static inline bool classof(const Instruction *I) {
155 return isPossiblyExactOpcode(I->getOpcode());
157 static inline bool classof(const Value *V) {
158 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
159 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
163 /// Convenience struct for specifying and reasoning about fast-math flags.
164 class FastMathFlags {
166 friend class FPMathOperator;
168 FastMathFlags(unsigned F) : Flags(F) { }
172 UnsafeAlgebra = (1 << 0),
175 NoSignedZeros = (1 << 3),
176 AllowReciprocal = (1 << 4)
179 FastMathFlags() : Flags(0)
182 /// Whether any flag is set
183 bool any() const { return Flags != 0; }
185 /// Set all the flags to false
186 void clear() { Flags = 0; }
189 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
190 bool noInfs() const { return 0 != (Flags & NoInfs); }
191 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
192 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
193 bool unsafeAlgebra() const { return 0 != (Flags & UnsafeAlgebra); }
196 void setNoNaNs() { Flags |= NoNaNs; }
197 void setNoInfs() { Flags |= NoInfs; }
198 void setNoSignedZeros() { Flags |= NoSignedZeros; }
199 void setAllowReciprocal() { Flags |= AllowReciprocal; }
200 void setUnsafeAlgebra() {
201 Flags |= UnsafeAlgebra;
205 setAllowReciprocal();
208 void operator&=(const FastMathFlags &OtherFlags) {
209 Flags &= OtherFlags.Flags;
214 /// Utility class for floating point operations which can have
215 /// information about relaxed accuracy requirements attached to them.
216 class FPMathOperator : public Operator {
218 friend class Instruction;
220 void setHasUnsafeAlgebra(bool B) {
221 SubclassOptionalData =
222 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
223 (B * FastMathFlags::UnsafeAlgebra);
225 // Unsafe algebra implies all the others
229 setHasNoSignedZeros(true);
230 setHasAllowReciprocal(true);
233 void setHasNoNaNs(bool B) {
234 SubclassOptionalData =
235 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
236 (B * FastMathFlags::NoNaNs);
238 void setHasNoInfs(bool B) {
239 SubclassOptionalData =
240 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
241 (B * FastMathFlags::NoInfs);
243 void setHasNoSignedZeros(bool B) {
244 SubclassOptionalData =
245 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
246 (B * FastMathFlags::NoSignedZeros);
248 void setHasAllowReciprocal(bool B) {
249 SubclassOptionalData =
250 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
251 (B * FastMathFlags::AllowReciprocal);
254 /// Convenience function for setting multiple fast-math flags.
255 /// FMF is a mask of the bits to set.
256 void setFastMathFlags(FastMathFlags FMF) {
257 SubclassOptionalData |= FMF.Flags;
260 /// Convenience function for copying all fast-math flags.
261 /// All values in FMF are transferred to this operator.
262 void copyFastMathFlags(FastMathFlags FMF) {
263 SubclassOptionalData = FMF.Flags;
267 /// Test whether this operation is permitted to be
268 /// algebraically transformed, aka the 'A' fast-math property.
269 bool hasUnsafeAlgebra() const {
270 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
273 /// Test whether this operation's arguments and results are to be
274 /// treated as non-NaN, aka the 'N' fast-math property.
275 bool hasNoNaNs() const {
276 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
279 /// Test whether this operation's arguments and results are to be
280 /// treated as NoN-Inf, aka the 'I' fast-math property.
281 bool hasNoInfs() const {
282 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
285 /// Test whether this operation can treat the sign of zero
286 /// as insignificant, aka the 'S' fast-math property.
287 bool hasNoSignedZeros() const {
288 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
291 /// Test whether this operation is permitted to use
292 /// reciprocal instead of division, aka the 'R' fast-math property.
293 bool hasAllowReciprocal() const {
294 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
297 /// Convenience function for getting all the fast-math flags
298 FastMathFlags getFastMathFlags() const {
299 return FastMathFlags(SubclassOptionalData);
302 /// \brief Get the maximum error permitted by this operation in ULPs. An
303 /// accuracy of 0.0 means that the operation should be performed with the
304 /// default precision.
305 float getFPAccuracy() const;
307 static inline bool classof(const Instruction *I) {
308 return I->getType()->isFPOrFPVectorTy();
310 static inline bool classof(const Value *V) {
311 return isa<Instruction>(V) && classof(cast<Instruction>(V));
316 /// A helper template for defining operators for individual opcodes.
317 template<typename SuperClass, unsigned Opc>
318 class ConcreteOperator : public SuperClass {
320 static inline bool classof(const Instruction *I) {
321 return I->getOpcode() == Opc;
323 static inline bool classof(const ConstantExpr *CE) {
324 return CE->getOpcode() == Opc;
326 static inline bool classof(const Value *V) {
327 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
328 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
333 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
336 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
339 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
342 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
347 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
350 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
353 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
356 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
360 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
364 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
366 IsInBounds = (1 << 0)
369 friend class GetElementPtrInst;
370 friend class ConstantExpr;
371 void setIsInBounds(bool B) {
372 SubclassOptionalData =
373 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
377 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
378 bool isInBounds() const {
379 return SubclassOptionalData & IsInBounds;
382 inline op_iterator idx_begin() { return op_begin()+1; }
383 inline const_op_iterator idx_begin() const { return op_begin()+1; }
384 inline op_iterator idx_end() { return op_end(); }
385 inline const_op_iterator idx_end() const { return op_end(); }
387 Value *getPointerOperand() {
388 return getOperand(0);
390 const Value *getPointerOperand() const {
391 return getOperand(0);
393 static unsigned getPointerOperandIndex() {
394 return 0U; // get index for modifying correct operand
397 /// Method to return the pointer operand as a PointerType.
398 Type *getPointerOperandType() const {
399 return getPointerOperand()->getType();
402 Type *getSourceElementType() const;
404 /// Method to return the address space of the pointer operand.
405 unsigned getPointerAddressSpace() const {
406 return getPointerOperandType()->getPointerAddressSpace();
409 unsigned getNumIndices() const { // Note: always non-negative
410 return getNumOperands() - 1;
413 bool hasIndices() const {
414 return getNumOperands() > 1;
417 /// Return true if all of the indices of this GEP are zeros.
418 /// If so, the result pointer and the first operand have the same
419 /// value, just potentially different types.
420 bool hasAllZeroIndices() const {
421 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
422 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
430 /// Return true if all of the indices of this GEP are constant integers.
431 /// If so, the result pointer and the first operand have
432 /// a constant offset between them.
433 bool hasAllConstantIndices() const {
434 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
435 if (!isa<ConstantInt>(I))
441 /// \brief Accumulate the constant address offset of this GEP if possible.
443 /// This routine accepts an APInt into which it will accumulate the constant
444 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
445 /// all-constant, it returns false and the value of the offset APInt is
446 /// undefined (it is *not* preserved!). The APInt passed into this routine
447 /// must be at exactly as wide as the IntPtr type for the address space of the
448 /// base GEP pointer.
449 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
452 class PtrToIntOperator
453 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
454 friend class PtrToInt;
455 friend class ConstantExpr;
458 Value *getPointerOperand() {
459 return getOperand(0);
461 const Value *getPointerOperand() const {
462 return getOperand(0);
464 static unsigned getPointerOperandIndex() {
465 return 0U; // get index for modifying correct operand
468 /// Method to return the pointer operand as a PointerType.
469 Type *getPointerOperandType() const {
470 return getPointerOperand()->getType();
473 /// Method to return the address space of the pointer operand.
474 unsigned getPointerAddressSpace() const {
475 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
479 class BitCastOperator
480 : public ConcreteOperator<Operator, Instruction::BitCast> {
481 friend class BitCastInst;
482 friend class ConstantExpr;
485 Type *getSrcTy() const {
486 return getOperand(0)->getType();
489 Type *getDestTy() const {
494 } // End llvm namespace