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 s) = delete;
40 /// Return the opcode for this Instruction or ConstantExpr.
41 unsigned getOpcode() const {
42 if (const Instruction *I = dyn_cast<Instruction>(this))
43 return I->getOpcode();
44 return cast<ConstantExpr>(this)->getOpcode();
47 /// If V is an Instruction or ConstantExpr, return its opcode.
48 /// Otherwise return UserOp1.
49 static unsigned getOpcode(const Value *V) {
50 if (const Instruction *I = dyn_cast<Instruction>(V))
51 return I->getOpcode();
52 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
53 return CE->getOpcode();
54 return Instruction::UserOp1;
57 static bool classof(const Instruction *) { return true; }
58 static bool classof(const ConstantExpr *) { return true; }
59 static bool classof(const Value *V) {
60 return isa<Instruction>(V) || isa<ConstantExpr>(V);
64 /// Utility class for integer arithmetic operators which may exhibit overflow -
65 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
66 /// the potential for overflow.
67 class OverflowingBinaryOperator : public Operator {
70 NoUnsignedWrap = (1 << 0),
71 NoSignedWrap = (1 << 1)
75 friend class Instruction;
76 friend class ConstantExpr;
78 void setHasNoUnsignedWrap(bool B) {
79 SubclassOptionalData =
80 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
82 void setHasNoSignedWrap(bool B) {
83 SubclassOptionalData =
84 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
88 /// Test whether this operation is known to never
89 /// undergo unsigned overflow, aka the nuw property.
90 bool hasNoUnsignedWrap() const {
91 return SubclassOptionalData & NoUnsignedWrap;
94 /// Test whether this operation is known to never
95 /// undergo signed overflow, aka the nsw property.
96 bool hasNoSignedWrap() const {
97 return (SubclassOptionalData & NoSignedWrap) != 0;
100 static bool classof(const Instruction *I) {
101 return I->getOpcode() == Instruction::Add ||
102 I->getOpcode() == Instruction::Sub ||
103 I->getOpcode() == Instruction::Mul ||
104 I->getOpcode() == Instruction::Shl;
106 static bool classof(const ConstantExpr *CE) {
107 return CE->getOpcode() == Instruction::Add ||
108 CE->getOpcode() == Instruction::Sub ||
109 CE->getOpcode() == Instruction::Mul ||
110 CE->getOpcode() == Instruction::Shl;
112 static bool classof(const Value *V) {
113 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
114 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
118 /// A udiv or sdiv instruction, which can be marked as "exact",
119 /// indicating that no bits are destroyed.
120 class PossiblyExactOperator : public Operator {
127 friend class Instruction;
128 friend class ConstantExpr;
130 void setIsExact(bool B) {
131 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
135 /// Test whether this division is known to be exact, with zero remainder.
136 bool isExact() const {
137 return SubclassOptionalData & IsExact;
140 static bool isPossiblyExactOpcode(unsigned OpC) {
141 return OpC == Instruction::SDiv ||
142 OpC == Instruction::UDiv ||
143 OpC == Instruction::AShr ||
144 OpC == Instruction::LShr;
147 static bool classof(const ConstantExpr *CE) {
148 return isPossiblyExactOpcode(CE->getOpcode());
150 static bool classof(const Instruction *I) {
151 return isPossiblyExactOpcode(I->getOpcode());
153 static bool classof(const Value *V) {
154 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
155 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
159 /// Convenience struct for specifying and reasoning about fast-math flags.
160 class FastMathFlags {
162 friend class FPMathOperator;
166 FastMathFlags(unsigned F) : Flags(F) { }
169 /// This is how the bits are used in Value::SubclassOptionalData so they
170 /// should fit there too.
172 UnsafeAlgebra = (1 << 0),
175 NoSignedZeros = (1 << 3),
176 AllowReciprocal = (1 << 4),
177 AllowContract = (1 << 5)
180 FastMathFlags() = default;
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 allowContract() const { return 0 != (Flags & AllowContract); }
194 bool unsafeAlgebra() const { 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 setAllowContract(bool B) {
202 Flags = (Flags & ~AllowContract) | B * AllowContract;
204 void setUnsafeAlgebra() {
205 Flags |= UnsafeAlgebra;
209 setAllowReciprocal();
210 setAllowContract(true);
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 void setHasAllowContract(bool B) {
263 SubclassOptionalData =
264 (SubclassOptionalData & ~FastMathFlags::AllowContract) |
265 (B * FastMathFlags::AllowContract);
268 /// Convenience function for setting multiple fast-math flags.
269 /// FMF is a mask of the bits to set.
270 void setFastMathFlags(FastMathFlags FMF) {
271 SubclassOptionalData |= FMF.Flags;
274 /// Convenience function for copying all fast-math flags.
275 /// All values in FMF are transferred to this operator.
276 void copyFastMathFlags(FastMathFlags FMF) {
277 SubclassOptionalData = FMF.Flags;
281 /// Test whether this operation is permitted to be
282 /// algebraically transformed, aka the 'A' fast-math property.
283 bool hasUnsafeAlgebra() const {
284 return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
287 /// Test whether this operation's arguments and results are to be
288 /// treated as non-NaN, aka the 'N' fast-math property.
289 bool hasNoNaNs() const {
290 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
293 /// Test whether this operation's arguments and results are to be
294 /// treated as NoN-Inf, aka the 'I' fast-math property.
295 bool hasNoInfs() const {
296 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
299 /// Test whether this operation can treat the sign of zero
300 /// as insignificant, aka the 'S' fast-math property.
301 bool hasNoSignedZeros() const {
302 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
305 /// Test whether this operation is permitted to use
306 /// reciprocal instead of division, aka the 'R' fast-math property.
307 bool hasAllowReciprocal() const {
308 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
311 /// Test whether this operation is permitted to
312 /// be floating-point contracted.
313 bool hasAllowContract() const {
314 return (SubclassOptionalData & FastMathFlags::AllowContract) != 0;
317 /// Convenience function for getting all the fast-math flags
318 FastMathFlags getFastMathFlags() const {
319 return FastMathFlags(SubclassOptionalData);
322 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
323 /// 0.0 means that the operation should be performed with the default
325 float getFPAccuracy() const;
327 static bool classof(const Instruction *I) {
328 return I->getType()->isFPOrFPVectorTy() ||
329 I->getOpcode() == Instruction::FCmp;
332 static bool classof(const ConstantExpr *CE) {
333 return CE->getType()->isFPOrFPVectorTy() ||
334 CE->getOpcode() == Instruction::FCmp;
337 static bool classof(const Value *V) {
338 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
339 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
343 /// A helper template for defining operators for individual opcodes.
344 template<typename SuperClass, unsigned Opc>
345 class ConcreteOperator : public SuperClass {
347 static bool classof(const Instruction *I) {
348 return I->getOpcode() == Opc;
350 static bool classof(const ConstantExpr *CE) {
351 return CE->getOpcode() == Opc;
353 static bool classof(const Value *V) {
354 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
355 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
360 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
363 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
366 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
369 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
373 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
376 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
379 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
382 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
385 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
388 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
389 friend class GetElementPtrInst;
390 friend class ConstantExpr;
393 IsInBounds = (1 << 0),
394 // InRangeIndex: bits 1-6
397 void setIsInBounds(bool B) {
398 SubclassOptionalData =
399 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
403 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
404 bool isInBounds() const {
405 return SubclassOptionalData & IsInBounds;
408 /// Returns the offset of the index with an inrange attachment, or None if
410 Optional<unsigned> getInRangeIndex() const {
411 if (SubclassOptionalData >> 1 == 0) return None;
412 return (SubclassOptionalData >> 1) - 1;
415 inline op_iterator idx_begin() { return op_begin()+1; }
416 inline const_op_iterator idx_begin() const { return op_begin()+1; }
417 inline op_iterator idx_end() { return op_end(); }
418 inline const_op_iterator idx_end() const { return op_end(); }
420 Value *getPointerOperand() {
421 return getOperand(0);
423 const Value *getPointerOperand() const {
424 return getOperand(0);
426 static unsigned getPointerOperandIndex() {
427 return 0U; // get index for modifying correct operand
430 /// Method to return the pointer operand as a PointerType.
431 Type *getPointerOperandType() const {
432 return getPointerOperand()->getType();
435 Type *getSourceElementType() const;
436 Type *getResultElementType() const;
438 /// Method to return the address space of the pointer operand.
439 unsigned getPointerAddressSpace() const {
440 return getPointerOperandType()->getPointerAddressSpace();
443 unsigned getNumIndices() const { // Note: always non-negative
444 return getNumOperands() - 1;
447 bool hasIndices() const {
448 return getNumOperands() > 1;
451 /// Return true if all of the indices of this GEP are zeros.
452 /// If so, the result pointer and the first operand have the same
453 /// value, just potentially different types.
454 bool hasAllZeroIndices() const {
455 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
456 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
464 /// Return true if all of the indices of this GEP are constant integers.
465 /// If so, the result pointer and the first operand have
466 /// a constant offset between them.
467 bool hasAllConstantIndices() const {
468 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
469 if (!isa<ConstantInt>(I))
475 /// \brief Accumulate the constant address offset of this GEP if possible.
477 /// This routine accepts an APInt into which it will accumulate the constant
478 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
479 /// all-constant, it returns false and the value of the offset APInt is
480 /// undefined (it is *not* preserved!). The APInt passed into this routine
481 /// must be at exactly as wide as the IntPtr type for the address space of the
482 /// base GEP pointer.
483 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
486 class PtrToIntOperator
487 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
488 friend class PtrToInt;
489 friend class ConstantExpr;
492 Value *getPointerOperand() {
493 return getOperand(0);
495 const Value *getPointerOperand() const {
496 return getOperand(0);
499 static unsigned getPointerOperandIndex() {
500 return 0U; // get index for modifying correct operand
503 /// Method to return the pointer operand as a PointerType.
504 Type *getPointerOperandType() const {
505 return getPointerOperand()->getType();
508 /// Method to return the address space of the pointer operand.
509 unsigned getPointerAddressSpace() const {
510 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
514 class BitCastOperator
515 : public ConcreteOperator<Operator, Instruction::BitCast> {
516 friend class BitCastInst;
517 friend class ConstantExpr;
520 Type *getSrcTy() const {
521 return getOperand(0)->getType();
524 Type *getDestTy() const {
529 } // end namespace llvm
531 #endif // LLVM_IR_OPERATOR_H