[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / IR / Operator.h
blob608f33c3ed54925cde00e3989eb4b3b54903cfb5
1 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines various classes for working with Instructions and
10 // ConstantExprs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_OPERATOR_H
15 #define LLVM_IR_OPERATOR_H
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/IR/Value.h"
23 #include "llvm/Support/Casting.h"
24 #include <cstddef>
26 namespace llvm {
28 /// This is a utility class that provides an abstraction for the common
29 /// functionality between Instructions and ConstantExprs.
30 class Operator : public User {
31 public:
32 // The Operator class is intended to be used as a utility, and is never itself
33 // instantiated.
34 Operator() = delete;
35 ~Operator() = delete;
37 void *operator new(size_t s) = delete;
39 /// Return the opcode for this Instruction or ConstantExpr.
40 unsigned getOpcode() const {
41 if (const Instruction *I = dyn_cast<Instruction>(this))
42 return I->getOpcode();
43 return cast<ConstantExpr>(this)->getOpcode();
46 /// If V is an Instruction or ConstantExpr, return its opcode.
47 /// Otherwise return UserOp1.
48 static unsigned getOpcode(const Value *V) {
49 if (const Instruction *I = dyn_cast<Instruction>(V))
50 return I->getOpcode();
51 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
52 return CE->getOpcode();
53 return Instruction::UserOp1;
56 static bool classof(const Instruction *) { return true; }
57 static bool classof(const ConstantExpr *) { return true; }
58 static bool classof(const Value *V) {
59 return isa<Instruction>(V) || isa<ConstantExpr>(V);
63 /// Utility class for integer operators which may exhibit overflow - Add, Sub,
64 /// Mul, and Shl. It does not include SDiv, despite that operator having the
65 /// potential for overflow.
66 class OverflowingBinaryOperator : public Operator {
67 public:
68 enum {
69 NoUnsignedWrap = (1 << 0),
70 NoSignedWrap = (1 << 1)
73 private:
74 friend class Instruction;
75 friend class ConstantExpr;
77 void setHasNoUnsignedWrap(bool B) {
78 SubclassOptionalData =
79 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
81 void setHasNoSignedWrap(bool B) {
82 SubclassOptionalData =
83 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
86 public:
87 /// Test whether this operation is known to never
88 /// undergo unsigned overflow, aka the nuw property.
89 bool hasNoUnsignedWrap() const {
90 return SubclassOptionalData & NoUnsignedWrap;
93 /// Test whether this operation is known to never
94 /// undergo signed overflow, aka the nsw property.
95 bool hasNoSignedWrap() const {
96 return (SubclassOptionalData & NoSignedWrap) != 0;
99 static bool classof(const Instruction *I) {
100 return I->getOpcode() == Instruction::Add ||
101 I->getOpcode() == Instruction::Sub ||
102 I->getOpcode() == Instruction::Mul ||
103 I->getOpcode() == Instruction::Shl;
105 static bool classof(const ConstantExpr *CE) {
106 return CE->getOpcode() == Instruction::Add ||
107 CE->getOpcode() == Instruction::Sub ||
108 CE->getOpcode() == Instruction::Mul ||
109 CE->getOpcode() == Instruction::Shl;
111 static bool classof(const Value *V) {
112 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
113 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
117 /// A udiv or sdiv instruction, which can be marked as "exact",
118 /// indicating that no bits are destroyed.
119 class PossiblyExactOperator : public Operator {
120 public:
121 enum {
122 IsExact = (1 << 0)
125 private:
126 friend class Instruction;
127 friend class ConstantExpr;
129 void setIsExact(bool B) {
130 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
133 public:
134 /// Test whether this division is known to be exact, with zero remainder.
135 bool isExact() const {
136 return SubclassOptionalData & IsExact;
139 static bool isPossiblyExactOpcode(unsigned OpC) {
140 return OpC == Instruction::SDiv ||
141 OpC == Instruction::UDiv ||
142 OpC == Instruction::AShr ||
143 OpC == Instruction::LShr;
146 static bool classof(const ConstantExpr *CE) {
147 return isPossiblyExactOpcode(CE->getOpcode());
149 static bool classof(const Instruction *I) {
150 return isPossiblyExactOpcode(I->getOpcode());
152 static bool classof(const Value *V) {
153 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
154 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
158 /// Convenience struct for specifying and reasoning about fast-math flags.
159 class FastMathFlags {
160 private:
161 friend class FPMathOperator;
163 unsigned Flags = 0;
165 FastMathFlags(unsigned F) {
166 // If all 7 bits are set, turn this into -1. If the number of bits grows,
167 // this must be updated. This is intended to provide some forward binary
168 // compatibility insurance for the meaning of 'fast' in case bits are added.
169 if (F == 0x7F) Flags = ~0U;
170 else Flags = F;
173 public:
174 // This is how the bits are used in Value::SubclassOptionalData so they
175 // should fit there too.
176 // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
177 // functionality will require a change in how this information is stored.
178 enum {
179 AllowReassoc = (1 << 0),
180 NoNaNs = (1 << 1),
181 NoInfs = (1 << 2),
182 NoSignedZeros = (1 << 3),
183 AllowReciprocal = (1 << 4),
184 AllowContract = (1 << 5),
185 ApproxFunc = (1 << 6)
188 FastMathFlags() = default;
190 static FastMathFlags getFast() {
191 FastMathFlags FMF;
192 FMF.setFast();
193 return FMF;
196 bool any() const { return Flags != 0; }
197 bool none() const { return Flags == 0; }
198 bool all() const { return Flags == ~0U; }
200 void clear() { Flags = 0; }
201 void set() { Flags = ~0U; }
203 /// Flag queries
204 bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
205 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
206 bool noInfs() const { return 0 != (Flags & NoInfs); }
207 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
208 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
209 bool allowContract() const { return 0 != (Flags & AllowContract); }
210 bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
211 /// 'Fast' means all bits are set.
212 bool isFast() const { return all(); }
214 /// Flag setters
215 void setAllowReassoc(bool B = true) {
216 Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
218 void setNoNaNs(bool B = true) {
219 Flags = (Flags & ~NoNaNs) | B * NoNaNs;
221 void setNoInfs(bool B = true) {
222 Flags = (Flags & ~NoInfs) | B * NoInfs;
224 void setNoSignedZeros(bool B = true) {
225 Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
227 void setAllowReciprocal(bool B = true) {
228 Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
230 void setAllowContract(bool B = true) {
231 Flags = (Flags & ~AllowContract) | B * AllowContract;
233 void setApproxFunc(bool B = true) {
234 Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
236 void setFast(bool B = true) { B ? set() : clear(); }
238 void operator&=(const FastMathFlags &OtherFlags) {
239 Flags &= OtherFlags.Flags;
243 /// Utility class for floating point operations which can have
244 /// information about relaxed accuracy requirements attached to them.
245 class FPMathOperator : public Operator {
246 private:
247 friend class Instruction;
249 /// 'Fast' means all bits are set.
250 void setFast(bool B) {
251 setHasAllowReassoc(B);
252 setHasNoNaNs(B);
253 setHasNoInfs(B);
254 setHasNoSignedZeros(B);
255 setHasAllowReciprocal(B);
256 setHasAllowContract(B);
257 setHasApproxFunc(B);
260 void setHasAllowReassoc(bool B) {
261 SubclassOptionalData =
262 (SubclassOptionalData & ~FastMathFlags::AllowReassoc) |
263 (B * FastMathFlags::AllowReassoc);
266 void setHasNoNaNs(bool B) {
267 SubclassOptionalData =
268 (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
269 (B * FastMathFlags::NoNaNs);
272 void setHasNoInfs(bool B) {
273 SubclassOptionalData =
274 (SubclassOptionalData & ~FastMathFlags::NoInfs) |
275 (B * FastMathFlags::NoInfs);
278 void setHasNoSignedZeros(bool B) {
279 SubclassOptionalData =
280 (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
281 (B * FastMathFlags::NoSignedZeros);
284 void setHasAllowReciprocal(bool B) {
285 SubclassOptionalData =
286 (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
287 (B * FastMathFlags::AllowReciprocal);
290 void setHasAllowContract(bool B) {
291 SubclassOptionalData =
292 (SubclassOptionalData & ~FastMathFlags::AllowContract) |
293 (B * FastMathFlags::AllowContract);
296 void setHasApproxFunc(bool B) {
297 SubclassOptionalData =
298 (SubclassOptionalData & ~FastMathFlags::ApproxFunc) |
299 (B * FastMathFlags::ApproxFunc);
302 /// Convenience function for setting multiple fast-math flags.
303 /// FMF is a mask of the bits to set.
304 void setFastMathFlags(FastMathFlags FMF) {
305 SubclassOptionalData |= FMF.Flags;
308 /// Convenience function for copying all fast-math flags.
309 /// All values in FMF are transferred to this operator.
310 void copyFastMathFlags(FastMathFlags FMF) {
311 SubclassOptionalData = FMF.Flags;
314 public:
315 /// Test if this operation allows all non-strict floating-point transforms.
316 bool isFast() const {
317 return ((SubclassOptionalData & FastMathFlags::AllowReassoc) != 0 &&
318 (SubclassOptionalData & FastMathFlags::NoNaNs) != 0 &&
319 (SubclassOptionalData & FastMathFlags::NoInfs) != 0 &&
320 (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0 &&
321 (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0 &&
322 (SubclassOptionalData & FastMathFlags::AllowContract) != 0 &&
323 (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0);
326 /// Test if this operation may be simplified with reassociative transforms.
327 bool hasAllowReassoc() const {
328 return (SubclassOptionalData & FastMathFlags::AllowReassoc) != 0;
331 /// Test if this operation's arguments and results are assumed not-NaN.
332 bool hasNoNaNs() const {
333 return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
336 /// Test if this operation's arguments and results are assumed not-infinite.
337 bool hasNoInfs() const {
338 return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
341 /// Test if this operation can ignore the sign of zero.
342 bool hasNoSignedZeros() const {
343 return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
346 /// Test if this operation can use reciprocal multiply instead of division.
347 bool hasAllowReciprocal() const {
348 return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
351 /// Test if this operation can be floating-point contracted (FMA).
352 bool hasAllowContract() const {
353 return (SubclassOptionalData & FastMathFlags::AllowContract) != 0;
356 /// Test if this operation allows approximations of math library functions or
357 /// intrinsics.
358 bool hasApproxFunc() const {
359 return (SubclassOptionalData & FastMathFlags::ApproxFunc) != 0;
362 /// Convenience function for getting all the fast-math flags
363 FastMathFlags getFastMathFlags() const {
364 return FastMathFlags(SubclassOptionalData);
367 /// Get the maximum error permitted by this operation in ULPs. An accuracy of
368 /// 0.0 means that the operation should be performed with the default
369 /// precision.
370 float getFPAccuracy() const;
372 static bool classof(const Value *V) {
373 unsigned Opcode;
374 if (auto *I = dyn_cast<Instruction>(V))
375 Opcode = I->getOpcode();
376 else if (auto *CE = dyn_cast<ConstantExpr>(V))
377 Opcode = CE->getOpcode();
378 else
379 return false;
381 switch (Opcode) {
382 // FIXME: To clean up and correct the semantics of fast-math-flags, FCmp
383 // should not be treated as a math op, but the other opcodes should.
384 // This would make things consistent with Select/PHI (FP value type
385 // determines whether they are math ops and, therefore, capable of
386 // having fast-math-flags).
387 case Instruction::FCmp:
388 return true;
389 // non math FP Operators (no FMF)
390 case Instruction::ExtractElement:
391 case Instruction::ShuffleVector:
392 case Instruction::InsertElement:
393 return false;
394 default:
395 return V->getType()->isFPOrFPVectorTy();
400 /// A helper template for defining operators for individual opcodes.
401 template<typename SuperClass, unsigned Opc>
402 class ConcreteOperator : public SuperClass {
403 public:
404 static bool classof(const Instruction *I) {
405 return I->getOpcode() == Opc;
407 static bool classof(const ConstantExpr *CE) {
408 return CE->getOpcode() == Opc;
410 static bool classof(const Value *V) {
411 return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
412 (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
416 class AddOperator
417 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
419 class SubOperator
420 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
422 class MulOperator
423 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
425 class ShlOperator
426 : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
429 class SDivOperator
430 : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
432 class UDivOperator
433 : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
435 class AShrOperator
436 : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
438 class LShrOperator
439 : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
442 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
444 class GEPOperator
445 : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
446 friend class GetElementPtrInst;
447 friend class ConstantExpr;
449 enum {
450 IsInBounds = (1 << 0),
451 // InRangeIndex: bits 1-6
454 void setIsInBounds(bool B) {
455 SubclassOptionalData =
456 (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
459 public:
460 /// Test whether this is an inbounds GEP, as defined by LangRef.html.
461 bool isInBounds() const {
462 return SubclassOptionalData & IsInBounds;
465 /// Returns the offset of the index with an inrange attachment, or None if
466 /// none.
467 Optional<unsigned> getInRangeIndex() const {
468 if (SubclassOptionalData >> 1 == 0) return None;
469 return (SubclassOptionalData >> 1) - 1;
472 inline op_iterator idx_begin() { return op_begin()+1; }
473 inline const_op_iterator idx_begin() const { return op_begin()+1; }
474 inline op_iterator idx_end() { return op_end(); }
475 inline const_op_iterator idx_end() const { return op_end(); }
477 Value *getPointerOperand() {
478 return getOperand(0);
480 const Value *getPointerOperand() const {
481 return getOperand(0);
483 static unsigned getPointerOperandIndex() {
484 return 0U; // get index for modifying correct operand
487 /// Method to return the pointer operand as a PointerType.
488 Type *getPointerOperandType() const {
489 return getPointerOperand()->getType();
492 Type *getSourceElementType() const;
493 Type *getResultElementType() const;
495 /// Method to return the address space of the pointer operand.
496 unsigned getPointerAddressSpace() const {
497 return getPointerOperandType()->getPointerAddressSpace();
500 unsigned getNumIndices() const { // Note: always non-negative
501 return getNumOperands() - 1;
504 bool hasIndices() const {
505 return getNumOperands() > 1;
508 /// Return true if all of the indices of this GEP are zeros.
509 /// If so, the result pointer and the first operand have the same
510 /// value, just potentially different types.
511 bool hasAllZeroIndices() const {
512 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
513 if (ConstantInt *C = dyn_cast<ConstantInt>(I))
514 if (C->isZero())
515 continue;
516 return false;
518 return true;
521 /// Return true if all of the indices of this GEP are constant integers.
522 /// If so, the result pointer and the first operand have
523 /// a constant offset between them.
524 bool hasAllConstantIndices() const {
525 for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
526 if (!isa<ConstantInt>(I))
527 return false;
529 return true;
532 unsigned countNonConstantIndices() const {
533 return count_if(make_range(idx_begin(), idx_end()), [](const Use& use) {
534 return !isa<ConstantInt>(*use);
538 /// Accumulate the constant address offset of this GEP if possible.
540 /// This routine accepts an APInt into which it will accumulate the constant
541 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
542 /// all-constant, it returns false and the value of the offset APInt is
543 /// undefined (it is *not* preserved!). The APInt passed into this routine
544 /// must be at exactly as wide as the IntPtr type for the address space of the
545 /// base GEP pointer.
546 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
549 class PtrToIntOperator
550 : public ConcreteOperator<Operator, Instruction::PtrToInt> {
551 friend class PtrToInt;
552 friend class ConstantExpr;
554 public:
555 Value *getPointerOperand() {
556 return getOperand(0);
558 const Value *getPointerOperand() const {
559 return getOperand(0);
562 static unsigned getPointerOperandIndex() {
563 return 0U; // get index for modifying correct operand
566 /// Method to return the pointer operand as a PointerType.
567 Type *getPointerOperandType() const {
568 return getPointerOperand()->getType();
571 /// Method to return the address space of the pointer operand.
572 unsigned getPointerAddressSpace() const {
573 return cast<PointerType>(getPointerOperandType())->getAddressSpace();
577 class BitCastOperator
578 : public ConcreteOperator<Operator, Instruction::BitCast> {
579 friend class BitCastInst;
580 friend class ConstantExpr;
582 public:
583 Type *getSrcTy() const {
584 return getOperand(0)->getType();
587 Type *getDestTy() const {
588 return getType();
592 } // end namespace llvm
594 #endif // LLVM_IR_OPERATOR_H