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