LLVM  3.7.0
Operator.h
Go to the documentation of this file.
1 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines various classes for working with Instructions and
11 // ConstantExprs.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_OPERATOR_H
16 #define LLVM_IR_OPERATOR_H
17 
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Type.h"
23 
24 namespace llvm {
25 
26 class GetElementPtrInst;
27 class BinaryOperator;
28 class ConstantExpr;
29 
30 /// This is a utility class that provides an abstraction for the common
31 /// functionality between Instructions and ConstantExprs.
32 class Operator : public User {
33 private:
34  // The Operator class is intended to be used as a utility, and is never itself
35  // instantiated.
36  void *operator new(size_t, unsigned) = delete;
37  void *operator new(size_t s) = delete;
38  Operator() = delete;
39 
40 protected:
41  // NOTE: Cannot use = delete because it's not legal to delete
42  // an overridden method that's not deleted in the base class. Cannot leave
43  // this unimplemented because that leads to an ODR-violation.
44  ~Operator() override;
45 
46 public:
47  /// Return the opcode for this Instruction or ConstantExpr.
48  unsigned getOpcode() const {
49  if (const Instruction *I = dyn_cast<Instruction>(this))
50  return I->getOpcode();
51  return cast<ConstantExpr>(this)->getOpcode();
52  }
53 
54  /// If V is an Instruction or ConstantExpr, return its opcode.
55  /// Otherwise return UserOp1.
56  static unsigned getOpcode(const Value *V) {
57  if (const Instruction *I = dyn_cast<Instruction>(V))
58  return I->getOpcode();
59  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
60  return CE->getOpcode();
61  return Instruction::UserOp1;
62  }
63 
64  static inline bool classof(const Instruction *) { return true; }
65  static inline bool classof(const ConstantExpr *) { return true; }
66  static inline bool classof(const Value *V) {
67  return isa<Instruction>(V) || isa<ConstantExpr>(V);
68  }
69 };
70 
71 /// Utility class for integer arithmetic operators which may exhibit overflow -
72 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
73 /// the potential for overflow.
75 public:
76  enum {
77  NoUnsignedWrap = (1 << 0),
78  NoSignedWrap = (1 << 1)
79  };
80 
81 private:
82  friend class BinaryOperator;
83  friend class ConstantExpr;
84  void setHasNoUnsignedWrap(bool B) {
87  }
88  void setHasNoSignedWrap(bool B) {
91  }
92 
93 public:
94  /// Test whether this operation is known to never
95  /// undergo unsigned overflow, aka the nuw property.
96  bool hasNoUnsignedWrap() const {
98  }
99 
100  /// Test whether this operation is known to never
101  /// undergo signed overflow, aka the nsw property.
102  bool hasNoSignedWrap() const {
103  return (SubclassOptionalData & NoSignedWrap) != 0;
104  }
105 
106  static inline bool classof(const Instruction *I) {
107  return I->getOpcode() == Instruction::Add ||
108  I->getOpcode() == Instruction::Sub ||
109  I->getOpcode() == Instruction::Mul ||
110  I->getOpcode() == Instruction::Shl;
111  }
112  static inline bool classof(const ConstantExpr *CE) {
113  return CE->getOpcode() == Instruction::Add ||
114  CE->getOpcode() == Instruction::Sub ||
115  CE->getOpcode() == Instruction::Mul ||
116  CE->getOpcode() == Instruction::Shl;
117  }
118  static inline bool classof(const Value *V) {
119  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
120  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
121  }
122 };
123 
124 /// A udiv or sdiv instruction, which can be marked as "exact",
125 /// indicating that no bits are destroyed.
127 public:
128  enum {
129  IsExact = (1 << 0)
130  };
131 
132 private:
133  friend class BinaryOperator;
134  friend class ConstantExpr;
135  void setIsExact(bool B) {
137  }
138 
139 public:
140  /// Test whether this division is known to be exact, with zero remainder.
141  bool isExact() const {
142  return SubclassOptionalData & IsExact;
143  }
144 
145  static bool isPossiblyExactOpcode(unsigned OpC) {
146  return OpC == Instruction::SDiv ||
147  OpC == Instruction::UDiv ||
148  OpC == Instruction::AShr ||
149  OpC == Instruction::LShr;
150  }
151  static inline bool classof(const ConstantExpr *CE) {
152  return isPossiblyExactOpcode(CE->getOpcode());
153  }
154  static inline bool classof(const Instruction *I) {
155  return isPossiblyExactOpcode(I->getOpcode());
156  }
157  static inline bool classof(const Value *V) {
158  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
159  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
160  }
161 };
162 
163 /// Convenience struct for specifying and reasoning about fast-math flags.
165 private:
166  friend class FPMathOperator;
167  unsigned Flags;
168  FastMathFlags(unsigned F) : Flags(F) { }
169 
170 public:
171  enum {
172  UnsafeAlgebra = (1 << 0),
173  NoNaNs = (1 << 1),
174  NoInfs = (1 << 2),
175  NoSignedZeros = (1 << 3),
176  AllowReciprocal = (1 << 4)
177  };
178 
180  { }
181 
182  /// Whether any flag is set
183  bool any() const { return Flags != 0; }
184 
185  /// Set all the flags to false
186  void clear() { Flags = 0; }
187 
188  /// Flag queries
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 unsafeAlgebra() const { return 0 != (Flags & UnsafeAlgebra); }
194 
195  /// Flag setters
196  void setNoNaNs() { Flags |= NoNaNs; }
197  void setNoInfs() { Flags |= NoInfs; }
198  void setNoSignedZeros() { Flags |= NoSignedZeros; }
199  void setAllowReciprocal() { Flags |= AllowReciprocal; }
201  Flags |= UnsafeAlgebra;
202  setNoNaNs();
203  setNoInfs();
204  setNoSignedZeros();
205  setAllowReciprocal();
206  }
207 
208  void operator&=(const FastMathFlags &OtherFlags) {
209  Flags &= OtherFlags.Flags;
210  }
211 };
212 
213 
214 /// Utility class for floating point operations which can have
215 /// information about relaxed accuracy requirements attached to them.
216 class FPMathOperator : public Operator {
217 private:
218  friend class Instruction;
219 
220  void setHasUnsafeAlgebra(bool B) {
222  (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
224 
225  // Unsafe algebra implies all the others
226  if (B) {
227  setHasNoNaNs(true);
228  setHasNoInfs(true);
229  setHasNoSignedZeros(true);
230  setHasAllowReciprocal(true);
231  }
232  }
233  void setHasNoNaNs(bool B) {
235  (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
236  (B * FastMathFlags::NoNaNs);
237  }
238  void setHasNoInfs(bool B) {
240  (SubclassOptionalData & ~FastMathFlags::NoInfs) |
241  (B * FastMathFlags::NoInfs);
242  }
243  void setHasNoSignedZeros(bool B) {
245  (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
247  }
248  void setHasAllowReciprocal(bool B) {
250  (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
252  }
253 
254  /// Convenience function for setting multiple fast-math flags.
255  /// FMF is a mask of the bits to set.
256  void setFastMathFlags(FastMathFlags FMF) {
257  SubclassOptionalData |= FMF.Flags;
258  }
259 
260  /// Convenience function for copying all fast-math flags.
261  /// All values in FMF are transferred to this operator.
262  void copyFastMathFlags(FastMathFlags FMF) {
263  SubclassOptionalData = FMF.Flags;
264  }
265 
266 public:
267  /// Test whether this operation is permitted to be
268  /// algebraically transformed, aka the 'A' fast-math property.
269  bool hasUnsafeAlgebra() const {
270  return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
271  }
272 
273  /// Test whether this operation's arguments and results are to be
274  /// treated as non-NaN, aka the 'N' fast-math property.
275  bool hasNoNaNs() const {
277  }
278 
279  /// Test whether this operation's arguments and results are to be
280  /// treated as NoN-Inf, aka the 'I' fast-math property.
281  bool hasNoInfs() const {
283  }
284 
285  /// Test whether this operation can treat the sign of zero
286  /// as insignificant, aka the 'S' fast-math property.
287  bool hasNoSignedZeros() const {
289  }
290 
291  /// Test whether this operation is permitted to use
292  /// reciprocal instead of division, aka the 'R' fast-math property.
293  bool hasAllowReciprocal() const {
295  }
296 
297  /// Convenience function for getting all the fast-math flags
300  }
301 
302  /// \brief Get the maximum error permitted by this operation in ULPs. An
303  /// accuracy of 0.0 means that the operation should be performed with the
304  /// default precision.
305  float getFPAccuracy() const;
306 
307  static inline bool classof(const Instruction *I) {
308  return I->getType()->isFPOrFPVectorTy() ||
309  I->getOpcode() == Instruction::FCmp;
310  }
311  static inline bool classof(const Value *V) {
312  return isa<Instruction>(V) && classof(cast<Instruction>(V));
313  }
314 };
315 
316 
317 /// A helper template for defining operators for individual opcodes.
318 template<typename SuperClass, unsigned Opc>
319 class ConcreteOperator : public SuperClass {
320 public:
321  static inline bool classof(const Instruction *I) {
322  return I->getOpcode() == Opc;
323  }
324  static inline bool classof(const ConstantExpr *CE) {
325  return CE->getOpcode() == Opc;
326  }
327  static inline bool classof(const Value *V) {
328  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
329  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
330  }
331 };
332 
334  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
335 };
337  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
338 };
340  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
341 };
343  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
344 };
345 
346 
348  : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
349 };
351  : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
352 };
354  : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
355 };
357  : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
358 };
359 
360 
361 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
362 
363 
365  : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
366  enum {
367  IsInBounds = (1 << 0)
368  };
369 
370  friend class GetElementPtrInst;
371  friend class ConstantExpr;
372  void setIsInBounds(bool B) {
374  (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
375  }
376 
377 public:
378  /// Test whether this is an inbounds GEP, as defined by LangRef.html.
379  bool isInBounds() const {
380  return SubclassOptionalData & IsInBounds;
381  }
382 
383  inline op_iterator idx_begin() { return op_begin()+1; }
384  inline const_op_iterator idx_begin() const { return op_begin()+1; }
385  inline op_iterator idx_end() { return op_end(); }
386  inline const_op_iterator idx_end() const { return op_end(); }
387 
389  return getOperand(0);
390  }
391  const Value *getPointerOperand() const {
392  return getOperand(0);
393  }
394  static unsigned getPointerOperandIndex() {
395  return 0U; // get index for modifying correct operand
396  }
397 
398  /// Method to return the pointer operand as a PointerType.
400  return getPointerOperand()->getType();
401  }
402 
403  Type *getSourceElementType() const;
404 
405  /// Method to return the address space of the pointer operand.
406  unsigned getPointerAddressSpace() const {
407  return getPointerOperandType()->getPointerAddressSpace();
408  }
409 
410  unsigned getNumIndices() const { // Note: always non-negative
411  return getNumOperands() - 1;
412  }
413 
414  bool hasIndices() const {
415  return getNumOperands() > 1;
416  }
417 
418  /// Return true if all of the indices of this GEP are zeros.
419  /// If so, the result pointer and the first operand have the same
420  /// value, just potentially different types.
421  bool hasAllZeroIndices() const {
422  for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
423  if (ConstantInt *C = dyn_cast<ConstantInt>(I))
424  if (C->isZero())
425  continue;
426  return false;
427  }
428  return true;
429  }
430 
431  /// Return true if all of the indices of this GEP are constant integers.
432  /// If so, the result pointer and the first operand have
433  /// a constant offset between them.
434  bool hasAllConstantIndices() const {
435  for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
436  if (!isa<ConstantInt>(I))
437  return false;
438  }
439  return true;
440  }
441 
442  /// \brief Accumulate the constant address offset of this GEP if possible.
443  ///
444  /// This routine accepts an APInt into which it will accumulate the constant
445  /// offset of this GEP if the GEP is in fact constant. If the GEP is not
446  /// all-constant, it returns false and the value of the offset APInt is
447  /// undefined (it is *not* preserved!). The APInt passed into this routine
448  /// must be at exactly as wide as the IntPtr type for the address space of the
449  /// base GEP pointer.
450  bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
451 };
452 
454  : public ConcreteOperator<Operator, Instruction::PtrToInt> {
455  friend class PtrToInt;
456  friend class ConstantExpr;
457 
458 public:
460  return getOperand(0);
461  }
462  const Value *getPointerOperand() const {
463  return getOperand(0);
464  }
465  static unsigned getPointerOperandIndex() {
466  return 0U; // get index for modifying correct operand
467  }
468 
469  /// Method to return the pointer operand as a PointerType.
471  return getPointerOperand()->getType();
472  }
473 
474  /// Method to return the address space of the pointer operand.
475  unsigned getPointerAddressSpace() const {
476  return cast<PointerType>(getPointerOperandType())->getAddressSpace();
477  }
478 };
479 
481  : public ConcreteOperator<Operator, Instruction::BitCast> {
482  friend class BitCastInst;
483  friend class ConstantExpr;
484 
485 public:
486  Type *getSrcTy() const {
487  return getOperand(0)->getType();
488  }
489 
490  Type *getDestTy() const {
491  return getType();
492  }
493 };
494 
495 } // End llvm namespace
496 
497 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static bool classof(const Instruction *I)
Definition: Operator.h:321
static bool classof(const Value *V)
Definition: Operator.h:66
void setNoSignedZeros()
Definition: Operator.h:198
void setNoNaNs()
Flag setters.
Definition: Operator.h:196
static bool classof(const ConstantExpr *)
Definition: Operator.h:65
unsigned getNumOperands() const
Definition: User.h:138
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
Definition: Operator.h:102
static unsigned getOpcode(const Value *V)
If V is an Instruction or ConstantExpr, return its opcode.
Definition: Operator.h:56
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
Definition: Operator.h:298
bool hasNoSignedZeros() const
Test whether this operation can treat the sign of zero as insignificant, aka the 'S' fast-math proper...
Definition: Operator.h:287
static bool classof(const Instruction *)
Definition: Operator.h:64
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:475
F(f)
op_iterator idx_end()
Definition: Operator.h:385
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:141
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1144
op_iterator op_begin()
Definition: User.h:183
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
Definition: Operator.h:434
Type * getPointerOperandType() const
Method to return the pointer operand as a PointerType.
Definition: Operator.h:399
static Value * getPointerOperand(Instruction &Inst)
bool hasAllowReciprocal() const
Test whether this operation is permitted to use reciprocal instead of division, aka the 'R' fast-math...
Definition: Operator.h:293
bool noSignedZeros() const
Definition: Operator.h:191
static bool classof(const Value *V)
Definition: Operator.h:118
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
static bool classof(const Instruction *I)
Definition: Operator.h:106
static bool classof(const Value *V)
Definition: Operator.h:311
Type * getDestTy() const
Definition: Operator.h:490
Value * getPointerOperand()
Definition: Operator.h:459
static unsigned getPointerOperandIndex()
Definition: Operator.h:394
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:112
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static bool classof(const Value *V)
Definition: Operator.h:157
This class represents a no-op cast from one type to another.
static bool classof(const Instruction *I)
Definition: Operator.h:307
bool allowReciprocal() const
Definition: Operator.h:192
void operator&=(const FastMathFlags &OtherFlags)
Definition: Operator.h:208
bool noInfs() const
Definition: Operator.h:190
~Operator() override
Definition: User.cpp:143
bool hasNoNaNs() const
Test whether this operation's arguments and results are to be treated as non-NaN, aka the 'N' fast-ma...
Definition: Operator.h:275
bool unsafeAlgebra() const
Definition: Operator.h:193
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
bool hasNoInfs() const
Test whether this operation's arguments and results are to be treated as NoN-Inf, aka the 'I' fast-ma...
Definition: Operator.h:281
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const_op_iterator idx_begin() const
Definition: Operator.h:384
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed...
Definition: Operator.h:126
void setUnsafeAlgebra()
Definition: Operator.h:200
bool hasIndices() const
Definition: Operator.h:414
op_iterator op_end()
Definition: User.h:185
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
Definition: Operator.h:74
Value * getPointerOperand()
Definition: Operator.h:388
Value * getOperand(unsigned i) const
Definition: User.h:118
Type * getSrcTy() const
Definition: Operator.h:486
Type * getPointerOperandType() const
Method to return the pointer operand as a PointerType.
Definition: Operator.h:470
static bool classof(const Value *V)
Definition: Operator.h:327
static bool isPossiblyExactOpcode(unsigned OpC)
Definition: Operator.h:145
const Value * getPointerOperand() const
Definition: Operator.h:462
bool isFPOrFPVectorTy() const
isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
Definition: Type.h:183
op_iterator idx_begin()
Definition: Operator.h:383
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:84
const Value * getPointerOperand() const
Definition: Operator.h:391
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:151
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
unsigned getNumIndices() const
Definition: Operator.h:410
A helper template for defining operators for individual opcodes.
Definition: Operator.h:319
void setAllowReciprocal()
Definition: Operator.h:199
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:216
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
Definition: Operator.h:421
bool any() const
Whether any flag is set.
Definition: Operator.h:183
Class for arbitrary precision integers.
Definition: APInt.h:73
bool hasUnsafeAlgebra() const
Test whether this operation is permitted to be algebraically transformed, aka the 'A' fast-math prope...
Definition: Operator.h:269
static bool classof(const Instruction *I)
Definition: Operator.h:154
void clear()
Set all the flags to false.
Definition: Operator.h:186
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:48
const_op_iterator idx_end() const
Definition: Operator.h:386
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:379
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerAddressSpace() const
Method to return the address space of the pointer operand.
Definition: Operator.h:406
static unsigned getPointerOperandIndex()
Definition: Operator.h:465
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:324
LLVM Value Representation.
Definition: Value.h:69
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:112
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:164
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property...
Definition: Operator.h:96
bool noNaNs() const
Flag queries.
Definition: Operator.h:189