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/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
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 // NOTE: Cannot use = delete because it's not legal to delete
36 // an overridden method that's not deleted in the base class. Cannot leave
37 // this unimplemented because that leads to an ODR-violation.
41 // The Operator class is intended to be used as a utility, and is never itself
45 void *operator new(size_t, unsigned) = delete;
46 void *operator new(size_t s) = delete;
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 Instruction;
84 friend class ConstantExpr;
86 void setHasNoUnsignedWrap(bool B) {
87 SubclassOptionalData =
88 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
90 void setHasNoSignedWrap(bool B) {
91 SubclassOptionalData =
92 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
96 /// Test whether this operation is known to never
97 /// undergo unsigned overflow, aka the nuw property.
98 bool hasNoUnsignedWrap() const {
99 return SubclassOptionalData & NoUnsignedWrap;
102 /// Test whether this operation is known to never
103 /// undergo signed overflow, aka the nsw property.
104 bool hasNoSignedWrap() const {
105 return (SubclassOptionalData & NoSignedWrap) != 0;
108 static inline bool classof(const Instruction *I) {
109 return I->getOpcode() == Instruction::Add ||
110 I->getOpcode() == Instruction::Sub ||
111 I->getOpcode() == Instruction::Mul ||
112 I->getOpcode() == Instruction::Shl;
114 static inline bool classof(const ConstantExpr *CE) {
115 return CE->getOpcode() == Instruction::Add ||
116 CE->getOpcode() == Instruction::Sub ||
117 CE->getOpcode() == Instruction::Mul ||
118 CE->getOpcode() == Instruction::Shl;
120 static inline bool classof(const Value *V) {
121 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
122 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
126 /// A udiv or sdiv instruction, which can be marked as "exact",
127 /// indicating that no bits are destroyed.
128 class PossiblyExactOperator : public Operator {
135 friend class Instruction;
136 friend class ConstantExpr;
138 void setIsExact(bool B) {
139 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
143 /// Test whether this division is known to be exact, with zero remainder.
144 bool isExact() const {
145 return SubclassOptionalData & IsExact;
148 static bool isPossiblyExactOpcode(unsigned OpC) {
149 return OpC == Instruction::SDiv ||
150 OpC == Instruction::UDiv ||
151 OpC == Instruction::AShr ||
152 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 class FastMathFlags {
170 friend class FPMathOperator;
174 FastMathFlags(unsigned F) : Flags(F) { }
178 UnsafeAlgebra = (1 << 0),
181 NoSignedZeros = (1 << 3),
182 AllowReciprocal = (1 << 4)
185 FastMathFlags() = default;
187 /// Whether any flag is set
188 bool any() const { return Flags != 0; }
190 /// Set all the flags to false
191 void clear() { Flags = 0; }
194 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
195 bool noInfs() const { return 0 != (Flags & NoInfs); }
196 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
197 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
198 bool unsafeAlgebra() const { return 0 != (Flags & UnsafeAlgebra); }
201 void setNoNaNs() { Flags |= NoNaNs; }
202 void setNoInfs() { Flags |= NoInfs; }
203 void setNoSignedZeros() { Flags |= NoSignedZeros; }
204 void setAllowReciprocal() { Flags |= AllowReciprocal; }
205 void setUnsafeAlgebra() {
206 Flags |= UnsafeAlgebra;
210 setAllowReciprocal();
213 void operator&=(const FastMathFlags &OtherFlags) {
214 Flags &= OtherFlags.Flags;
218 /// Utility class for floating point operations which can have
219 /// information about relaxed accuracy requirements attached to them.
220 class FPMathOperator : public Operator {
222 friend class Instruction;
224 void setHasUnsafeAlgebra(bool B) {
225 SubclassOptionalData =
226 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
227 (B * FastMathFlags::UnsafeAlgebra);
229 // Unsafe algebra implies all the others
233 setHasNoSignedZeros(true);
234 setHasAllowReciprocal(true);
238 void setHasNoNaNs(bool B) {
239 SubclassOptionalData =
240 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
241 (B * FastMathFlags::NoNaNs);
244 void setHasNoInfs(bool B) {
245 SubclassOptionalData =
246 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
247 (B * FastMathFlags::NoInfs);
250 void setHasNoSignedZeros(bool B) {
251 SubclassOptionalData =
252 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
253 (B * FastMathFlags::NoSignedZeros);
256 void setHasAllowReciprocal(bool B) {
257 SubclassOptionalData =
258 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
259 (B * FastMathFlags::AllowReciprocal);
262 /// Convenience function for setting multiple fast-math flags.
263 /// FMF is a mask of the bits to set.
264 void setFastMathFlags(FastMathFlags FMF) {
265 SubclassOptionalData |= FMF.Flags;
268 /// Convenience function for copying all fast-math flags.
269 /// All values in FMF are transferred to this operator.
270 void copyFastMathFlags(FastMathFlags FMF) {
271 SubclassOptionalData = FMF.Flags;
275 /// Test whether this operation is permitted to be
276 /// algebraically transformed, aka the 'A' fast-math property.
277 bool hasUnsafeAlgebra() const {
278 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
281 /// Test whether this operation's arguments and results are to be
282 /// treated as non-NaN, aka the 'N' fast-math property.
283 bool hasNoNaNs() const {
284 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
287 /// Test whether this operation's arguments and results are to be
288 /// treated as NoN-Inf, aka the 'I' fast-math property.
289 bool hasNoInfs() const {
290 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
293 /// Test whether this operation can treat the sign of zero
294 /// as insignificant, aka the 'S' fast-math property.
295 bool hasNoSignedZeros() const {
296 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
299 /// Test whether this operation is permitted to use
300 /// reciprocal instead of division, aka the 'R' fast-math property.
301 bool hasAllowReciprocal() const {
302 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
305 /// Convenience function for getting all the fast-math flags
306 FastMathFlags getFastMathFlags() const {
307 return FastMathFlags(SubclassOptionalData);
310 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
311 /// 0.0 means that the operation should be performed with the default
313 float getFPAccuracy() const;
315 static inline bool classof(const Instruction *I) {
316 return I->getType()->isFPOrFPVectorTy() ||
317 I->getOpcode() == Instruction::FCmp;
319 static inline bool classof(const Value *V) {
320 return isa<Instruction>(V) && classof(cast<Instruction>(V));
324 /// A helper template for defining operators for individual opcodes.
325 template<typename SuperClass, unsigned Opc>
326 class ConcreteOperator : public SuperClass {
328 static inline bool classof(const Instruction *I) {
329 return I->getOpcode() == Opc;
331 static inline bool classof(const ConstantExpr *CE) {
332 return CE->getOpcode() == Opc;
334 static inline bool classof(const Value *V) {
335 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
336 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
341 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
344 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
347 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
350 : 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> {
366 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
369 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
370 friend class GetElementPtrInst;
371 friend class ConstantExpr;
374 IsInBounds = (1 << 0),
375 // InRangeIndex: bits 1-6
378 void setIsInBounds(bool B) {
379 SubclassOptionalData =
380 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
384 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
385 bool isInBounds() const {
386 return SubclassOptionalData & IsInBounds;
389 /// Returns the offset of the index with an inrange attachment, or None if
391 Optional<unsigned> getInRangeIndex() const {
392 if (SubclassOptionalData >> 1 == 0) return None;
393 return (SubclassOptionalData >> 1) - 1;
396 inline op_iterator idx_begin() { return op_begin()+1; }
397 inline const_op_iterator idx_begin() const { return op_begin()+1; }
398 inline op_iterator idx_end() { return op_end(); }
399 inline const_op_iterator idx_end() const { return op_end(); }
401 Value *getPointerOperand() {
402 return getOperand(0);
404 const Value *getPointerOperand() const {
405 return getOperand(0);
407 static unsigned getPointerOperandIndex() {
408 return 0U; // get index for modifying correct operand
411 /// Method to return the pointer operand as a PointerType.
412 Type *getPointerOperandType() const {
413 return getPointerOperand()->getType();
416 Type *getSourceElementType() const;
417 Type *getResultElementType() const;
419 /// Method to return the address space of the pointer operand.
420 unsigned getPointerAddressSpace() const {
421 return getPointerOperandType()->getPointerAddressSpace();
424 unsigned getNumIndices() const { // Note: always non-negative
425 return getNumOperands() - 1;
428 bool hasIndices() const {
429 return getNumOperands() > 1;
432 /// Return true if all of the indices of this GEP are zeros.
433 /// If so, the result pointer and the first operand have the same
434 /// value, just potentially different types.
435 bool hasAllZeroIndices() const {
436 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
437 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
445 /// Return true if all of the indices of this GEP are constant integers.
446 /// If so, the result pointer and the first operand have
447 /// a constant offset between them.
448 bool hasAllConstantIndices() const {
449 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
450 if (!isa<ConstantInt>(I))
456 /// \brief Accumulate the constant address offset of this GEP if possible.
458 /// This routine accepts an APInt into which it will accumulate the constant
459 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
460 /// all-constant, it returns false and the value of the offset APInt is
461 /// undefined (it is *not* preserved!). The APInt passed into this routine
462 /// must be at exactly as wide as the IntPtr type for the address space of the
463 /// base GEP pointer.
464 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
467 class PtrToIntOperator
468 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
469 friend class PtrToInt;
470 friend class ConstantExpr;
473 Value *getPointerOperand() {
474 return getOperand(0);
476 const Value *getPointerOperand() const {
477 return getOperand(0);
480 static unsigned getPointerOperandIndex() {
481 return 0U; // get index for modifying correct operand
484 /// Method to return the pointer operand as a PointerType.
485 Type *getPointerOperandType() const {
486 return getPointerOperand()->getType();
489 /// Method to return the address space of the pointer operand.
490 unsigned getPointerAddressSpace() const {
491 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
495 class BitCastOperator
496 : public ConcreteOperator<Operator, Instruction::BitCast> {
497 friend class BitCastInst;
498 friend class ConstantExpr;
501 Type *getSrcTy() const {
502 return getOperand(0)->getType();
505 Type *getDestTy() const {
510 } // end namespace llvm
512 #endif // LLVM_IR_OPERATOR_H