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/Instruction.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Support/Casting.h"
29 /// This is a utility class that provides an abstraction for the common
30 /// functionality between Instructions and ConstantExprs.
31 class Operator : public User {
33 // The Operator class is intended to be used as a utility, and is never itself
38 void *operator new(size_t, unsigned) = delete;
39 void *operator new(size_t s) = delete;
41 /// Return the opcode for this Instruction or ConstantExpr.
42 unsigned getOpcode() const {
43 if (const Instruction *I = dyn_cast<Instruction>(this))
44 return I->getOpcode();
45 return cast<ConstantExpr>(this)->getOpcode();
48 /// If V is an Instruction or ConstantExpr, return its opcode.
49 /// Otherwise return UserOp1.
50 static unsigned getOpcode(const Value *V) {
51 if (const Instruction *I = dyn_cast<Instruction>(V))
52 return I->getOpcode();
53 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
54 return CE->getOpcode();
55 return Instruction::UserOp1;
58 static inline bool classof(const Instruction *) { return true; }
59 static inline bool classof(const ConstantExpr *) { return true; }
60 static inline bool classof(const Value *V) {
61 return isa<Instruction>(V) || isa<ConstantExpr>(V);
65 /// Utility class for integer arithmetic operators which may exhibit overflow -
66 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
67 /// the potential for overflow.
68 class OverflowingBinaryOperator : public Operator {
71 NoUnsignedWrap = (1 << 0),
72 NoSignedWrap = (1 << 1)
76 friend class Instruction;
77 friend class ConstantExpr;
79 void setHasNoUnsignedWrap(bool B) {
80 SubclassOptionalData =
81 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
83 void setHasNoSignedWrap(bool B) {
84 SubclassOptionalData =
85 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
89 /// Test whether this operation is known to never
90 /// undergo unsigned overflow, aka the nuw property.
91 bool hasNoUnsignedWrap() const {
92 return SubclassOptionalData & NoUnsignedWrap;
95 /// Test whether this operation is known to never
96 /// undergo signed overflow, aka the nsw property.
97 bool hasNoSignedWrap() const {
98 return (SubclassOptionalData & NoSignedWrap) != 0;
101 static inline bool classof(const Instruction *I) {
102 return I->getOpcode() == Instruction::Add ||
103 I->getOpcode() == Instruction::Sub ||
104 I->getOpcode() == Instruction::Mul ||
105 I->getOpcode() == Instruction::Shl;
107 static inline bool classof(const ConstantExpr *CE) {
108 return CE->getOpcode() == Instruction::Add ||
109 CE->getOpcode() == Instruction::Sub ||
110 CE->getOpcode() == Instruction::Mul ||
111 CE->getOpcode() == Instruction::Shl;
113 static inline bool classof(const Value *V) {
114 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
115 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
119 /// A udiv or sdiv instruction, which can be marked as "exact",
120 /// indicating that no bits are destroyed.
121 class PossiblyExactOperator : public Operator {
128 friend class Instruction;
129 friend class ConstantExpr;
131 void setIsExact(bool B) {
132 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
136 /// Test whether this division is known to be exact, with zero remainder.
137 bool isExact() const {
138 return SubclassOptionalData & IsExact;
141 static bool isPossiblyExactOpcode(unsigned OpC) {
142 return OpC == Instruction::SDiv ||
143 OpC == Instruction::UDiv ||
144 OpC == Instruction::AShr ||
145 OpC == Instruction::LShr;
148 static inline bool classof(const ConstantExpr *CE) {
149 return isPossiblyExactOpcode(CE->getOpcode());
151 static inline bool classof(const Instruction *I) {
152 return isPossiblyExactOpcode(I->getOpcode());
154 static inline bool classof(const Value *V) {
155 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
156 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
160 /// Convenience struct for specifying and reasoning about fast-math flags.
161 class FastMathFlags {
163 friend class FPMathOperator;
167 FastMathFlags(unsigned F) : Flags(F) { }
170 /// This is how the bits are used in Value::SubclassOptionalData so they
171 /// should fit there too.
173 UnsafeAlgebra = (1 << 0),
176 NoSignedZeros = (1 << 3),
177 AllowReciprocal = (1 << 4),
178 AllowContract = (1 << 5)
181 FastMathFlags() = default;
183 /// Whether any flag is set
184 bool any() const { return Flags != 0; }
186 /// Set all the flags to false
187 void clear() { Flags = 0; }
190 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
191 bool noInfs() const { return 0 != (Flags & NoInfs); }
192 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
193 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
194 bool allowContract() const { return 0 != (Flags & AllowContract); }
195 bool unsafeAlgebra() const { return 0 != (Flags & UnsafeAlgebra); }
198 void setNoNaNs() { Flags |= NoNaNs; }
199 void setNoInfs() { Flags |= NoInfs; }
200 void setNoSignedZeros() { Flags |= NoSignedZeros; }
201 void setAllowReciprocal() { Flags |= AllowReciprocal; }
202 void setAllowContract(bool B) {
203 Flags = (Flags & ~AllowContract) | B * AllowContract;
205 void setUnsafeAlgebra() {
206 Flags |= UnsafeAlgebra;
210 setAllowReciprocal();
211 setAllowContract(true);
214 void operator&=(const FastMathFlags &OtherFlags) {
215 Flags &= OtherFlags.Flags;
219 /// Utility class for floating point operations which can have
220 /// information about relaxed accuracy requirements attached to them.
221 class FPMathOperator : public Operator {
223 friend class Instruction;
225 void setHasUnsafeAlgebra(bool B) {
226 SubclassOptionalData =
227 (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
228 (B * FastMathFlags::UnsafeAlgebra);
230 // Unsafe algebra implies all the others
234 setHasNoSignedZeros(true);
235 setHasAllowReciprocal(true);
239 void setHasNoNaNs(bool B) {
240 SubclassOptionalData =
241 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
242 (B * FastMathFlags::NoNaNs);
245 void setHasNoInfs(bool B) {
246 SubclassOptionalData =
247 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
248 (B * FastMathFlags::NoInfs);
251 void setHasNoSignedZeros(bool B) {
252 SubclassOptionalData =
253 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
254 (B * FastMathFlags::NoSignedZeros);
257 void setHasAllowReciprocal(bool B) {
258 SubclassOptionalData =
259 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
260 (B * FastMathFlags::AllowReciprocal);
263 void setHasAllowContract(bool B) {
264 SubclassOptionalData =
265 (SubclassOptionalData & ~FastMathFlags::AllowContract) |
266 (B * FastMathFlags::AllowContract);
269 /// Convenience function for setting multiple fast-math flags.
270 /// FMF is a mask of the bits to set.
271 void setFastMathFlags(FastMathFlags FMF) {
272 SubclassOptionalData |= FMF.Flags;
275 /// Convenience function for copying all fast-math flags.
276 /// All values in FMF are transferred to this operator.
277 void copyFastMathFlags(FastMathFlags FMF) {
278 SubclassOptionalData = FMF.Flags;
282 /// Test whether this operation is permitted to be
283 /// algebraically transformed, aka the 'A' fast-math property.
284 bool hasUnsafeAlgebra() const {
285 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
288 /// Test whether this operation's arguments and results are to be
289 /// treated as non-NaN, aka the 'N' fast-math property.
290 bool hasNoNaNs() const {
291 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
294 /// Test whether this operation's arguments and results are to be
295 /// treated as NoN-Inf, aka the 'I' fast-math property.
296 bool hasNoInfs() const {
297 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
300 /// Test whether this operation can treat the sign of zero
301 /// as insignificant, aka the 'S' fast-math property.
302 bool hasNoSignedZeros() const {
303 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
306 /// Test whether this operation is permitted to use
307 /// reciprocal instead of division, aka the 'R' fast-math property.
308 bool hasAllowReciprocal() const {
309 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
312 /// Test whether this operation is permitted to
313 /// be floating-point contracted.
314 bool hasAllowContract() const {
315 return (SubclassOptionalData & FastMathFlags::AllowContract) != 0;
318 /// Convenience function for getting all the fast-math flags
319 FastMathFlags getFastMathFlags() const {
320 return FastMathFlags(SubclassOptionalData);
323 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
324 /// 0.0 means that the operation should be performed with the default
326 float getFPAccuracy() const;
328 static inline bool classof(const Instruction *I) {
329 return I->getType()->isFPOrFPVectorTy() ||
330 I->getOpcode() == Instruction::FCmp;
332 static inline bool classof(const Value *V) {
333 return isa<Instruction>(V) && classof(cast<Instruction>(V));
337 /// A helper template for defining operators for individual opcodes.
338 template<typename SuperClass, unsigned Opc>
339 class ConcreteOperator : public SuperClass {
341 static inline bool classof(const Instruction *I) {
342 return I->getOpcode() == Opc;
344 static inline bool classof(const ConstantExpr *CE) {
345 return CE->getOpcode() == Opc;
347 static inline bool classof(const Value *V) {
348 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
349 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
354 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
357 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
360 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
363 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
367 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
370 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
373 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
376 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
379 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
382 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
383 friend class GetElementPtrInst;
384 friend class ConstantExpr;
387 IsInBounds = (1 << 0),
388 // InRangeIndex: bits 1-6
391 void setIsInBounds(bool B) {
392 SubclassOptionalData =
393 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
397 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
398 bool isInBounds() const {
399 return SubclassOptionalData & IsInBounds;
402 /// Returns the offset of the index with an inrange attachment, or None if
404 Optional<unsigned> getInRangeIndex() const {
405 if (SubclassOptionalData >> 1 == 0) return None;
406 return (SubclassOptionalData >> 1) - 1;
409 inline op_iterator idx_begin() { return op_begin()+1; }
410 inline const_op_iterator idx_begin() const { return op_begin()+1; }
411 inline op_iterator idx_end() { return op_end(); }
412 inline const_op_iterator idx_end() const { return op_end(); }
414 Value *getPointerOperand() {
415 return getOperand(0);
417 const Value *getPointerOperand() const {
418 return getOperand(0);
420 static unsigned getPointerOperandIndex() {
421 return 0U; // get index for modifying correct operand
424 /// Method to return the pointer operand as a PointerType.
425 Type *getPointerOperandType() const {
426 return getPointerOperand()->getType();
429 Type *getSourceElementType() const;
430 Type *getResultElementType() const;
432 /// Method to return the address space of the pointer operand.
433 unsigned getPointerAddressSpace() const {
434 return getPointerOperandType()->getPointerAddressSpace();
437 unsigned getNumIndices() const { // Note: always non-negative
438 return getNumOperands() - 1;
441 bool hasIndices() const {
442 return getNumOperands() > 1;
445 /// Return true if all of the indices of this GEP are zeros.
446 /// If so, the result pointer and the first operand have the same
447 /// value, just potentially different types.
448 bool hasAllZeroIndices() const {
449 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
450 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
458 /// Return true if all of the indices of this GEP are constant integers.
459 /// If so, the result pointer and the first operand have
460 /// a constant offset between them.
461 bool hasAllConstantIndices() const {
462 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
463 if (!isa<ConstantInt>(I))
469 /// \brief Accumulate the constant address offset of this GEP if possible.
471 /// This routine accepts an APInt into which it will accumulate the constant
472 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
473 /// all-constant, it returns false and the value of the offset APInt is
474 /// undefined (it is *not* preserved!). The APInt passed into this routine
475 /// must be at exactly as wide as the IntPtr type for the address space of the
476 /// base GEP pointer.
477 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
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);
493 static unsigned getPointerOperandIndex() {
494 return 0U; // get index for modifying correct operand
497 /// Method to return the pointer operand as a PointerType.
498 Type *getPointerOperandType() const {
499 return getPointerOperand()->getType();
502 /// Method to return the address space of the pointer operand.
503 unsigned getPointerAddressSpace() const {
504 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
508 class BitCastOperator
509 : public ConcreteOperator<Operator, Instruction::BitCast> {
510 friend class BitCastInst;
511 friend class ConstantExpr;
514 Type *getSrcTy() const {
515 return getOperand(0)->getType();
518 Type *getDestTy() const {
523 } // end namespace llvm
525 #endif // LLVM_IR_OPERATOR_H