LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
24 
25 namespace llvm {
26 
27 class GetElementPtrInst;
28 class BinaryOperator;
29 class ConstantExpr;
30 
31 /// Operator - This is a utility class that provides an abstraction for the
32 /// common functionality between Instructions and ConstantExprs.
33 ///
34 class Operator : public User {
35 private:
36  // The Operator class is intended to be used as a utility, and is never itself
37  // instantiated.
38  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
39  void *operator new(size_t s) LLVM_DELETED_FUNCTION;
41 
42 protected:
43  // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
44  // an overridden method that's not deleted in the base class. Cannot leave
45  // this unimplemented because that leads to an ODR-violation.
46  ~Operator();
47 
48 public:
49  /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
50  ///
51  unsigned getOpcode() const {
52  if (const Instruction *I = dyn_cast<Instruction>(this))
53  return I->getOpcode();
54  return cast<ConstantExpr>(this)->getOpcode();
55  }
56 
57  /// getOpcode - If V is an Instruction or ConstantExpr, return its
58  /// opcode. Otherwise return UserOp1.
59  ///
60  static unsigned getOpcode(const Value *V) {
61  if (const Instruction *I = dyn_cast<Instruction>(V))
62  return I->getOpcode();
63  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
64  return CE->getOpcode();
65  return Instruction::UserOp1;
66  }
67 
68  static inline bool classof(const Instruction *) { return true; }
69  static inline bool classof(const ConstantExpr *) { return true; }
70  static inline bool classof(const Value *V) {
71  return isa<Instruction>(V) || isa<ConstantExpr>(V);
72  }
73 };
74 
75 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
76 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
77 /// despite that operator having the potential for overflow.
78 ///
80 public:
81  enum {
82  NoUnsignedWrap = (1 << 0),
83  NoSignedWrap = (1 << 1)
84  };
85 
86 private:
87  friend class BinaryOperator;
88  friend class ConstantExpr;
89  void setHasNoUnsignedWrap(bool B) {
92  }
93  void setHasNoSignedWrap(bool B) {
96  }
97 
98 public:
99  /// hasNoUnsignedWrap - Test whether this operation is known to never
100  /// undergo unsigned overflow, aka the nuw property.
101  bool hasNoUnsignedWrap() const {
103  }
104 
105  /// hasNoSignedWrap - Test whether this operation is known to never
106  /// undergo signed overflow, aka the nsw property.
107  bool hasNoSignedWrap() const {
108  return (SubclassOptionalData & NoSignedWrap) != 0;
109  }
110 
111  static inline bool classof(const Instruction *I) {
112  return I->getOpcode() == Instruction::Add ||
113  I->getOpcode() == Instruction::Sub ||
114  I->getOpcode() == Instruction::Mul ||
115  I->getOpcode() == Instruction::Shl;
116  }
117  static inline bool classof(const ConstantExpr *CE) {
118  return CE->getOpcode() == Instruction::Add ||
119  CE->getOpcode() == Instruction::Sub ||
120  CE->getOpcode() == Instruction::Mul ||
121  CE->getOpcode() == Instruction::Shl;
122  }
123  static inline bool classof(const Value *V) {
124  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
125  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
126  }
127 };
128 
129 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
130 /// "exact", indicating that no bits are destroyed.
132 public:
133  enum {
134  IsExact = (1 << 0)
135  };
136 
137 private:
138  friend class BinaryOperator;
139  friend class ConstantExpr;
140  void setIsExact(bool B) {
142  }
143 
144 public:
145  /// isExact - Test whether this division is known to be exact, with
146  /// zero remainder.
147  bool isExact() const {
148  return SubclassOptionalData & IsExact;
149  }
150 
151  static bool isPossiblyExactOpcode(unsigned OpC) {
152  return OpC == Instruction::SDiv ||
153  OpC == Instruction::UDiv ||
154  OpC == Instruction::AShr ||
155  OpC == Instruction::LShr;
156  }
157  static inline bool classof(const ConstantExpr *CE) {
158  return isPossiblyExactOpcode(CE->getOpcode());
159  }
160  static inline bool classof(const Instruction *I) {
161  return isPossiblyExactOpcode(I->getOpcode());
162  }
163  static inline bool classof(const Value *V) {
164  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
165  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
166  }
167 };
168 
169 /// Convenience struct for specifying and reasoning about fast-math flags.
171 private:
172  friend class FPMathOperator;
173  unsigned Flags;
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 
186  { }
187 
188  /// Whether any flag is set
189  bool any() { return Flags != 0; }
190 
191  /// Set all the flags to false
192  void clear() { Flags = 0; }
193 
194  /// Flag queries
195  bool noNaNs() { return 0 != (Flags & NoNaNs); }
196  bool noInfs() { return 0 != (Flags & NoInfs); }
197  bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); }
198  bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
199  bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); }
200 
201  /// Flag setters
202  void setNoNaNs() { Flags |= NoNaNs; }
203  void setNoInfs() { Flags |= NoInfs; }
204  void setNoSignedZeros() { Flags |= NoSignedZeros; }
205  void setAllowReciprocal() { Flags |= AllowReciprocal; }
207  Flags |= UnsafeAlgebra;
208  setNoNaNs();
209  setNoInfs();
210  setNoSignedZeros();
211  setAllowReciprocal();
212  }
213 };
214 
215 
216 /// FPMathOperator - Utility class for floating point operations which can have
217 /// information about relaxed accuracy requirements attached to them.
218 class FPMathOperator : public Operator {
219 private:
220  friend class Instruction;
221 
222  void setHasUnsafeAlgebra(bool B) {
224  (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
226 
227  // Unsafe algebra implies all the others
228  if (B) {
229  setHasNoNaNs(true);
230  setHasNoInfs(true);
231  setHasNoSignedZeros(true);
232  setHasAllowReciprocal(true);
233  }
234  }
235  void setHasNoNaNs(bool B) {
237  (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
238  (B * FastMathFlags::NoNaNs);
239  }
240  void setHasNoInfs(bool B) {
242  (SubclassOptionalData & ~FastMathFlags::NoInfs) |
243  (B * FastMathFlags::NoInfs);
244  }
245  void setHasNoSignedZeros(bool B) {
247  (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
249  }
250  void setHasAllowReciprocal(bool B) {
252  (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
254  }
255 
256  /// Convenience function for setting all the fast-math flags
257  void setFastMathFlags(FastMathFlags FMF) {
258  SubclassOptionalData |= FMF.Flags;
259  }
260 
261 public:
262  /// Test whether this operation is permitted to be
263  /// algebraically transformed, aka the 'A' fast-math property.
264  bool hasUnsafeAlgebra() const {
265  return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
266  }
267 
268  /// Test whether this operation's arguments and results are to be
269  /// treated as non-NaN, aka the 'N' fast-math property.
270  bool hasNoNaNs() const {
272  }
273 
274  /// Test whether this operation's arguments and results are to be
275  /// treated as NoN-Inf, aka the 'I' fast-math property.
276  bool hasNoInfs() const {
278  }
279 
280  /// Test whether this operation can treat the sign of zero
281  /// as insignificant, aka the 'S' fast-math property.
282  bool hasNoSignedZeros() const {
284  }
285 
286  /// Test whether this operation is permitted to use
287  /// reciprocal instead of division, aka the 'R' fast-math property.
288  bool hasAllowReciprocal() const {
290  }
291 
292  /// Convenience function for getting all the fast-math flags
295  }
296 
297  /// \brief Get the maximum error permitted by this operation in ULPs. An
298  /// accuracy of 0.0 means that the operation should be performed with the
299  /// default precision.
300  float getFPAccuracy() const;
301 
302  static inline bool classof(const Instruction *I) {
303  return I->getType()->isFPOrFPVectorTy();
304  }
305  static inline bool classof(const Value *V) {
306  return isa<Instruction>(V) && classof(cast<Instruction>(V));
307  }
308 };
309 
310 
311 /// ConcreteOperator - A helper template for defining operators for individual
312 /// opcodes.
313 template<typename SuperClass, unsigned Opc>
314 class ConcreteOperator : public SuperClass {
315 public:
316  static inline bool classof(const Instruction *I) {
317  return I->getOpcode() == Opc;
318  }
319  static inline bool classof(const ConstantExpr *CE) {
320  return CE->getOpcode() == Opc;
321  }
322  static inline bool classof(const Value *V) {
323  return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
324  (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
325  }
326 };
327 
329  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
330 };
332  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
333 };
335  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
336 };
338  : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
339 };
340 
341 
343  : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
344 };
346  : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
347 };
349  : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
350 };
352  : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
353 };
354 
355 
356 
358  : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
359  enum {
360  IsInBounds = (1 << 0)
361  };
362 
363  friend class GetElementPtrInst;
364  friend class ConstantExpr;
365  void setIsInBounds(bool B) {
367  (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
368  }
369 
370 public:
371  /// isInBounds - Test whether this is an inbounds GEP, as defined
372  /// by LangRef.html.
373  bool isInBounds() const {
374  return SubclassOptionalData & IsInBounds;
375  }
376 
377  inline op_iterator idx_begin() { return op_begin()+1; }
378  inline const_op_iterator idx_begin() const { return op_begin()+1; }
379  inline op_iterator idx_end() { return op_end(); }
380  inline const_op_iterator idx_end() const { return op_end(); }
381 
383  return getOperand(0);
384  }
385  const Value *getPointerOperand() const {
386  return getOperand(0);
387  }
388  static unsigned getPointerOperandIndex() {
389  return 0U; // get index for modifying correct operand
390  }
391 
392  /// getPointerOperandType - Method to return the pointer operand as a
393  /// PointerType.
395  return getPointerOperand()->getType();
396  }
397 
398  /// getPointerAddressSpace - Method to return the address space of the
399  /// pointer operand.
400  unsigned getPointerAddressSpace() const {
401  return cast<PointerType>(getPointerOperandType())->getAddressSpace();
402  }
403 
404  unsigned getNumIndices() const { // Note: always non-negative
405  return getNumOperands() - 1;
406  }
407 
408  bool hasIndices() const {
409  return getNumOperands() > 1;
410  }
411 
412  /// hasAllZeroIndices - Return true if all of the indices of this GEP are
413  /// zeros. If so, the result pointer and the first operand have the same
414  /// value, just potentially different types.
415  bool hasAllZeroIndices() const {
416  for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
417  if (ConstantInt *C = dyn_cast<ConstantInt>(I))
418  if (C->isZero())
419  continue;
420  return false;
421  }
422  return true;
423  }
424 
425  /// hasAllConstantIndices - Return true if all of the indices of this GEP are
426  /// constant integers. If so, the result pointer and the first operand have
427  /// a constant offset between them.
428  bool hasAllConstantIndices() const {
429  for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
430  if (!isa<ConstantInt>(I))
431  return false;
432  }
433  return true;
434  }
435 
436  /// \brief Accumulate the constant address offset of this GEP if possible.
437  ///
438  /// This routine accepts an APInt into which it will accumulate the constant
439  /// offset of this GEP if the GEP is in fact constant. If the GEP is not
440  /// all-constant, it returns false and the value of the offset APInt is
441  /// undefined (it is *not* preserved!). The APInt passed into this routine
442  /// must be at exactly as wide as the IntPtr type for the address space of the
443  /// base GEP pointer.
444  bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
445  assert(Offset.getBitWidth() ==
446  DL.getPointerSizeInBits(getPointerAddressSpace()) &&
447  "The offset must have exactly as many bits as our pointer.");
448 
449  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
450  GTI != GTE; ++GTI) {
451  ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
452  if (!OpC)
453  return false;
454  if (OpC->isZero())
455  continue;
456 
457  // Handle a struct index, which adds its field offset to the pointer.
458  if (StructType *STy = dyn_cast<StructType>(*GTI)) {
459  unsigned ElementIdx = OpC->getZExtValue();
460  const StructLayout *SL = DL.getStructLayout(STy);
461  Offset += APInt(Offset.getBitWidth(),
462  SL->getElementOffset(ElementIdx));
463  continue;
464  }
465 
466  // For array or vector indices, scale the index by the size of the type.
467  APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
468  Offset += Index * APInt(Offset.getBitWidth(),
469  DL.getTypeAllocSize(GTI.getIndexedType()));
470  }
471  return true;
472  }
473 
474 };
475 
476 } // End llvm namespace
477 
478 #endif
static bool classof(const Instruction *I)
Definition: Operator.h:316
static bool classof(const Value *V)
Definition: Operator.h:70
void setNoSignedZeros()
Definition: Operator.h:204
void setNoNaNs()
Flag setters.
Definition: Operator.h:202
static bool classof(const ConstantExpr *)
Definition: Operator.h:69
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
Definition: User.h:108
static unsigned getOpcode(const Value *V)
Definition: Operator.h:60
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
Definition: Operator.h:293
bool allowReciprocal()
Definition: Operator.h:198
bool hasNoSignedZeros() const
Definition: Operator.h:282
static bool classof(const Instruction *)
Definition: Operator.h:68
gep_type_iterator gep_type_end(const User *GEP)
F(f)
op_iterator idx_end()
Definition: Operator.h:379
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1049
op_iterator op_begin()
Definition: User.h:116
bool hasAllConstantIndices() const
Definition: Operator.h:428
Type * getPointerOperandType() const
Definition: Operator.h:394
static Value * getPointerOperand(Instruction &Inst)
bool hasAllowReciprocal() const
Definition: Operator.h:288
const StructLayout * getStructLayout(StructType *Ty) const
Definition: DataLayout.cpp:445
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
static bool classof(const Value *V)
Definition: Operator.h:123
Definition: Use.h:60
bool unsafeAlgebra()
Definition: Operator.h:199
static bool classof(const Instruction *I)
Definition: Operator.h:111
static bool classof(const Value *V)
Definition: Operator.h:305
bool any()
Whether any flag is set.
Definition: Operator.h:189
static unsigned getPointerOperandIndex()
Definition: Operator.h:388
uint64_t getZExtValue() const
Return the zero extended value.
Definition: Constants.h:116
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:117
static bool classof(const Value *V)
Definition: Operator.h:163
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.h:444
static bool classof(const Instruction *I)
Definition: Operator.h:302
bool hasNoNaNs() const
Definition: Operator.h:270
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:442
bool hasNoInfs() const
Definition: Operator.h:276
const_op_iterator idx_begin() const
Definition: Operator.h:378
void setUnsafeAlgebra()
Definition: Operator.h:206
bool hasIndices() const
Definition: Operator.h:408
op_iterator op_end()
Definition: User.h:118
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1252
Value * getPointerOperand()
Definition: Operator.h:382
Value * getOperand(unsigned i) const
Definition: User.h:88
static bool classof(const Value *V)
Definition: Operator.h:322
static bool isPossiblyExactOpcode(unsigned OpC)
Definition: Operator.h:151
bool isFPOrFPVectorTy() const
Definition: Type.h:186
op_iterator idx_begin()
Definition: Operator.h:377
unsigned char SubclassOptionalData
Definition: Value.h:74
const Value * getPointerOperand() const
Definition: Operator.h:385
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:157
Class for constant integers.
Definition: Constants.h:51
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
unsigned getNumIndices() const
Definition: Operator.h:404
void setAllowReciprocal()
Definition: Operator.h:205
Type * getType() const
Definition: Value.h:111
bool hasAllZeroIndices() const
Definition: Operator.h:415
bool isZero() const
Definition: Constants.h:160
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
Class for arbitrary precision integers.
Definition: APInt.h:75
bool noNaNs()
Flag queries.
Definition: Operator.h:195
bool hasUnsafeAlgebra() const
Definition: Operator.h:264
static bool classof(const Instruction *I)
Definition: Operator.h:160
void clear()
Set all the flags to false.
Definition: Operator.h:192
unsigned getOpcode() const
Definition: Operator.h:51
const_op_iterator idx_end() const
Definition: Operator.h:380
bool isInBounds() const
Definition: Operator.h:373
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getPointerSizeInBits(unsigned AS=0) const
Definition: DataLayout.h:271
unsigned getPointerAddressSpace() const
Definition: Operator.h:400
static bool classof(const ConstantExpr *CE)
Definition: Operator.h:319
bool noSignedZeros()
Definition: Operator.h:197
LLVM Value Representation.
Definition: Value.h:66
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:83
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:170
bool hasNoUnsignedWrap() const
Definition: Operator.h:101
gep_type_iterator gep_type_begin(const User *GEP)