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 operators which may exhibit overflow - Add, Sub,
65 /// Mul, and Shl. It does not include SDiv, despite that operator having the
66 /// 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) {
167 // If all 7 bits are set, turn this into -1. If the number of bits grows,
168 // this must be updated. This is intended to provide some forward binary
169 // compatibility insurance for the meaning of 'fast' in case bits are added.
170 if (F == 0x7F) Flags = ~0U;
175 // This is how the bits are used in Value::SubclassOptionalData so they
176 // should fit there too.
177 // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
178 // functionality will require a change in how this information is stored.
180 AllowReassoc = (1 << 0),
183 NoSignedZeros = (1 << 3),
184 AllowReciprocal = (1 << 4),
185 AllowContract = (1 << 5),
186 ApproxFunc = (1 << 6)
189 FastMathFlags() = default;
191 bool any() const { return Flags != 0; }
192 bool none() const { return Flags == 0; }
193 bool all() const { return Flags == ~0U; }
195 void clear() { Flags = 0; }
196 void set() { Flags = ~0U; }
199 bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
200 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
201 bool noInfs() const { return 0 != (Flags & NoInfs); }
202 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
203 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
204 bool allowContract() const { return 0 != (Flags & AllowContract); }
205 bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
206 /// 'Fast' means all bits are set.
207 bool isFast() const { return all(); }
210 void setAllowReassoc(bool B = true) {
211 Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
213 void setNoNaNs(bool B = true) {
214 Flags = (Flags & ~NoNaNs) | B * NoNaNs;
216 void setNoInfs(bool B = true) {
217 Flags = (Flags & ~NoInfs) | B * NoInfs;
219 void setNoSignedZeros(bool B = true) {
220 Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
222 void setAllowReciprocal(bool B = true) {
223 Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
225 void setAllowContract(bool B = true) {
226 Flags = (Flags & ~AllowContract) | B * AllowContract;
228 void setApproxFunc(bool B = true) {
229 Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
231 void setFast(bool B = true) { B ? set() : clear(); }
233 void operator&=(const FastMathFlags &OtherFlags) {
234 Flags &= OtherFlags.Flags;
238 /// Utility class for floating point operations which can have
239 /// information about relaxed accuracy requirements attached to them.
240 class FPMathOperator : public Operator {
242 friend class Instruction;
244 /// 'Fast' means all bits are set.
245 void setFast(bool B) {
246 setHasAllowReassoc(B);
249 setHasNoSignedZeros(B);
250 setHasAllowReciprocal(B);
251 setHasAllowContract(B);
255 void setHasAllowReassoc(bool B) {
256 SubclassOptionalData =
257 (SubclassOptionalData & ~FastMathFlags::AllowReassoc) |
258 (B * FastMathFlags::AllowReassoc);
261 void setHasNoNaNs(bool B) {
262 SubclassOptionalData =
263 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
264 (B * FastMathFlags::NoNaNs);
267 void setHasNoInfs(bool B) {
268 SubclassOptionalData =
269 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
270 (B * FastMathFlags::NoInfs);
273 void setHasNoSignedZeros(bool B) {
274 SubclassOptionalData =
275 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
276 (B * FastMathFlags::NoSignedZeros);
279 void setHasAllowReciprocal(bool B) {
280 SubclassOptionalData =
281 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
282 (B * FastMathFlags::AllowReciprocal);
285 void setHasAllowContract(bool B) {
286 SubclassOptionalData =
287 (SubclassOptionalData & ~FastMathFlags::AllowContract) |
288 (B * FastMathFlags::AllowContract);
291 void setHasApproxFunc(bool B) {
292 SubclassOptionalData =
293 (SubclassOptionalData & ~FastMathFlags::ApproxFunc) |
294 (B * FastMathFlags::ApproxFunc);
297 /// Convenience function for setting multiple fast-math flags.
298 /// FMF is a mask of the bits to set.
299 void setFastMathFlags(FastMathFlags FMF) {
300 SubclassOptionalData |= FMF.Flags;
303 /// Convenience function for copying all fast-math flags.
304 /// All values in FMF are transferred to this operator.
305 void copyFastMathFlags(FastMathFlags FMF) {
306 SubclassOptionalData = FMF.Flags;
310 /// Test if this operation allows all non-strict floating-point transforms.
311 bool isFast() const {
312 return ((SubclassOptionalData & FastMathFlags::AllowReassoc) != 0 &&
313 (SubclassOptionalData & FastMathFlags::NoNaNs) != 0 &&
314 (SubclassOptionalData & FastMathFlags::NoInfs) != 0 &&
315 (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0 &&
316 (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0 &&
317 (SubclassOptionalData & FastMathFlags::AllowContract) != 0 &&
318 (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0);
321 /// Test if this operation may be simplified with reassociative transforms.
322 bool hasAllowReassoc() const {
323 return (SubclassOptionalData & FastMathFlags::AllowReassoc) != 0;
326 /// Test if this operation's arguments and results are assumed not-NaN.
327 bool hasNoNaNs() const {
328 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
331 /// Test if this operation's arguments and results are assumed not-infinite.
332 bool hasNoInfs() const {
333 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
336 /// Test if this operation can ignore the sign of zero.
337 bool hasNoSignedZeros() const {
338 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
341 /// Test if this operation can use reciprocal multiply instead of division.
342 bool hasAllowReciprocal() const {
343 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
346 /// Test if this operation can be floating-point contracted (FMA).
347 bool hasAllowContract() const {
348 return (SubclassOptionalData & FastMathFlags::AllowContract) != 0;
351 /// Test if this operation allows approximations of math library functions or
353 bool hasApproxFunc() const {
354 return (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0;
357 /// Convenience function for getting all the fast-math flags
358 FastMathFlags getFastMathFlags() const {
359 return FastMathFlags(SubclassOptionalData);
362 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
363 /// 0.0 means that the operation should be performed with the default
365 float getFPAccuracy() const;
367 static bool classof(const Instruction *I) {
368 return I->getType()->isFPOrFPVectorTy() ||
369 I->getOpcode() == Instruction::FCmp;
372 static bool classof(const ConstantExpr *CE) {
373 return CE->getType()->isFPOrFPVectorTy() ||
374 CE->getOpcode() == Instruction::FCmp;
377 static bool classof(const Value *V) {
378 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
379 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
383 /// A helper template for defining operators for individual opcodes.
384 template<typename SuperClass, unsigned Opc>
385 class ConcreteOperator : public SuperClass {
387 static bool classof(const Instruction *I) {
388 return I->getOpcode() == Opc;
390 static bool classof(const ConstantExpr *CE) {
391 return CE->getOpcode() == Opc;
393 static bool classof(const Value *V) {
394 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
395 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
400 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
403 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
406 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
409 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
413 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
416 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
419 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
422 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
425 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
428 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
429 friend class GetElementPtrInst;
430 friend class ConstantExpr;
433 IsInBounds = (1 << 0),
434 // InRangeIndex: bits 1-6
437 void setIsInBounds(bool B) {
438 SubclassOptionalData =
439 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
443 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
444 bool isInBounds() const {
445 return SubclassOptionalData & IsInBounds;
448 /// Returns the offset of the index with an inrange attachment, or None if
450 Optional<unsigned> getInRangeIndex() const {
451 if (SubclassOptionalData >> 1 == 0) return None;
452 return (SubclassOptionalData >> 1) - 1;
455 inline op_iterator idx_begin() { return op_begin()+1; }
456 inline const_op_iterator idx_begin() const { return op_begin()+1; }
457 inline op_iterator idx_end() { return op_end(); }
458 inline const_op_iterator idx_end() const { return op_end(); }
460 Value *getPointerOperand() {
461 return getOperand(0);
463 const Value *getPointerOperand() const {
464 return getOperand(0);
466 static unsigned getPointerOperandIndex() {
467 return 0U; // get index for modifying correct operand
470 /// Method to return the pointer operand as a PointerType.
471 Type *getPointerOperandType() const {
472 return getPointerOperand()->getType();
475 Type *getSourceElementType() const;
476 Type *getResultElementType() const;
478 /// Method to return the address space of the pointer operand.
479 unsigned getPointerAddressSpace() const {
480 return getPointerOperandType()->getPointerAddressSpace();
483 unsigned getNumIndices() const { // Note: always non-negative
484 return getNumOperands() - 1;
487 bool hasIndices() const {
488 return getNumOperands() > 1;
491 /// Return true if all of the indices of this GEP are zeros.
492 /// If so, the result pointer and the first operand have the same
493 /// value, just potentially different types.
494 bool hasAllZeroIndices() const {
495 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
496 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
504 /// Return true if all of the indices of this GEP are constant integers.
505 /// If so, the result pointer and the first operand have
506 /// a constant offset between them.
507 bool hasAllConstantIndices() const {
508 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
509 if (!isa<ConstantInt>(I))
515 unsigned countNonConstantIndices() const {
516 return count_if(make_range(idx_begin(), idx_end()), [](const Use& use) {
517 return !isa<ConstantInt>(*use);
521 /// Accumulate the constant address offset of this GEP if possible.
523 /// This routine accepts an APInt into which it will accumulate the constant
524 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
525 /// all-constant, it returns false and the value of the offset APInt is
526 /// undefined (it is *not* preserved!). The APInt passed into this routine
527 /// must be at exactly as wide as the IntPtr type for the address space of the
528 /// base GEP pointer.
529 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
532 class PtrToIntOperator
533 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
534 friend class PtrToInt;
535 friend class ConstantExpr;
538 Value *getPointerOperand() {
539 return getOperand(0);
541 const Value *getPointerOperand() const {
542 return getOperand(0);
545 static unsigned getPointerOperandIndex() {
546 return 0U; // get index for modifying correct operand
549 /// Method to return the pointer operand as a PointerType.
550 Type *getPointerOperandType() const {
551 return getPointerOperand()->getType();
554 /// Method to return the address space of the pointer operand.
555 unsigned getPointerAddressSpace() const {
556 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
560 class BitCastOperator
561 : public ConcreteOperator<Operator, Instruction::BitCast> {
562 friend class BitCastInst;
563 friend class ConstantExpr;
566 Type *getSrcTy() const {
567 return getOperand(0)->getType();
570 Type *getDestTy() const {
575 } // end namespace llvm
577 #endif // LLVM_IR_OPERATOR_H