• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GetElementPtrTypeIterator.h"
22 #include "llvm/IR/Instruction.h"
23 #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;
40   Operator() 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   ///
getOpcode()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   ///
getOpcode(const Value * V)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 
classof(const Instruction *)68   static inline bool classof(const Instruction *) { return true; }
classof(const ConstantExpr *)69   static inline bool classof(const ConstantExpr *) { return true; }
classof(const Value * V)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 ///
79 class OverflowingBinaryOperator : public Operator {
80 public:
81   enum {
82     NoUnsignedWrap = (1 << 0),
83     NoSignedWrap   = (1 << 1)
84   };
85 
86 private:
87   friend class BinaryOperator;
88   friend class ConstantExpr;
setHasNoUnsignedWrap(bool B)89   void setHasNoUnsignedWrap(bool B) {
90     SubclassOptionalData =
91       (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
92   }
setHasNoSignedWrap(bool B)93   void setHasNoSignedWrap(bool B) {
94     SubclassOptionalData =
95       (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
96   }
97 
98 public:
99   /// hasNoUnsignedWrap - Test whether this operation is known to never
100   /// undergo unsigned overflow, aka the nuw property.
hasNoUnsignedWrap()101   bool hasNoUnsignedWrap() const {
102     return SubclassOptionalData & NoUnsignedWrap;
103   }
104 
105   /// hasNoSignedWrap - Test whether this operation is known to never
106   /// undergo signed overflow, aka the nsw property.
hasNoSignedWrap()107   bool hasNoSignedWrap() const {
108     return (SubclassOptionalData & NoSignedWrap) != 0;
109   }
110 
classof(const Instruction * I)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   }
classof(const ConstantExpr * CE)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   }
classof(const Value * V)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.
131 class PossiblyExactOperator : public Operator {
132 public:
133   enum {
134     IsExact = (1 << 0)
135   };
136 
137 private:
138   friend class BinaryOperator;
139   friend class ConstantExpr;
setIsExact(bool B)140   void setIsExact(bool B) {
141     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
142   }
143 
144 public:
145   /// isExact - Test whether this division is known to be exact, with
146   /// zero remainder.
isExact()147   bool isExact() const {
148     return SubclassOptionalData & IsExact;
149   }
150 
isPossiblyExactOpcode(unsigned OpC)151   static bool isPossiblyExactOpcode(unsigned OpC) {
152     return OpC == Instruction::SDiv ||
153            OpC == Instruction::UDiv ||
154            OpC == Instruction::AShr ||
155            OpC == Instruction::LShr;
156   }
classof(const ConstantExpr * CE)157   static inline bool classof(const ConstantExpr *CE) {
158     return isPossiblyExactOpcode(CE->getOpcode());
159   }
classof(const Instruction * I)160   static inline bool classof(const Instruction *I) {
161     return isPossiblyExactOpcode(I->getOpcode());
162   }
classof(const Value * V)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.
170 class FastMathFlags {
171 private:
172   friend class FPMathOperator;
173   unsigned Flags;
FastMathFlags(unsigned F)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 
FastMathFlags()185   FastMathFlags() : Flags(0)
186   { }
187 
188   /// Whether any flag is set
any()189   bool any() { return Flags != 0; }
190 
191   /// Set all the flags to false
clear()192   void clear() { Flags = 0; }
193 
194   /// Flag queries
noNaNs()195   bool noNaNs()          { return 0 != (Flags & NoNaNs); }
noInfs()196   bool noInfs()          { return 0 != (Flags & NoInfs); }
noSignedZeros()197   bool noSignedZeros()   { return 0 != (Flags & NoSignedZeros); }
allowReciprocal()198   bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
unsafeAlgebra()199   bool unsafeAlgebra()   { return 0 != (Flags & UnsafeAlgebra); }
200 
201   /// Flag setters
setNoNaNs()202   void setNoNaNs()          { Flags |= NoNaNs; }
setNoInfs()203   void setNoInfs()          { Flags |= NoInfs; }
setNoSignedZeros()204   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
setAllowReciprocal()205   void setAllowReciprocal() { Flags |= AllowReciprocal; }
setUnsafeAlgebra()206   void setUnsafeAlgebra() {
207     Flags |= UnsafeAlgebra;
208     setNoNaNs();
209     setNoInfs();
210     setNoSignedZeros();
211     setAllowReciprocal();
212   }
213 
214   void operator&=(const FastMathFlags &OtherFlags) {
215     Flags &= OtherFlags.Flags;
216   }
217 };
218 
219 
220 /// FPMathOperator - Utility class for floating point operations which can have
221 /// information about relaxed accuracy requirements attached to them.
222 class FPMathOperator : public Operator {
223 private:
224   friend class Instruction;
225 
setHasUnsafeAlgebra(bool B)226   void setHasUnsafeAlgebra(bool B) {
227     SubclassOptionalData =
228       (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
229       (B * FastMathFlags::UnsafeAlgebra);
230 
231     // Unsafe algebra implies all the others
232     if (B) {
233       setHasNoNaNs(true);
234       setHasNoInfs(true);
235       setHasNoSignedZeros(true);
236       setHasAllowReciprocal(true);
237     }
238   }
setHasNoNaNs(bool B)239   void setHasNoNaNs(bool B) {
240     SubclassOptionalData =
241       (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
242       (B * FastMathFlags::NoNaNs);
243   }
setHasNoInfs(bool B)244   void setHasNoInfs(bool B) {
245     SubclassOptionalData =
246       (SubclassOptionalData & ~FastMathFlags::NoInfs) |
247       (B * FastMathFlags::NoInfs);
248   }
setHasNoSignedZeros(bool B)249   void setHasNoSignedZeros(bool B) {
250     SubclassOptionalData =
251       (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
252       (B * FastMathFlags::NoSignedZeros);
253   }
setHasAllowReciprocal(bool B)254   void setHasAllowReciprocal(bool B) {
255     SubclassOptionalData =
256       (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
257       (B * FastMathFlags::AllowReciprocal);
258   }
259 
260   /// Convenience function for setting all the fast-math flags
setFastMathFlags(FastMathFlags FMF)261   void setFastMathFlags(FastMathFlags FMF) {
262     SubclassOptionalData |= FMF.Flags;
263   }
264 
265 public:
266   /// Test whether this operation is permitted to be
267   /// algebraically transformed, aka the 'A' fast-math property.
hasUnsafeAlgebra()268   bool hasUnsafeAlgebra() const {
269     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
270   }
271 
272   /// Test whether this operation's arguments and results are to be
273   /// treated as non-NaN, aka the 'N' fast-math property.
hasNoNaNs()274   bool hasNoNaNs() const {
275     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
276   }
277 
278   /// Test whether this operation's arguments and results are to be
279   /// treated as NoN-Inf, aka the 'I' fast-math property.
hasNoInfs()280   bool hasNoInfs() const {
281     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
282   }
283 
284   /// Test whether this operation can treat the sign of zero
285   /// as insignificant, aka the 'S' fast-math property.
hasNoSignedZeros()286   bool hasNoSignedZeros() const {
287     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
288   }
289 
290   /// Test whether this operation is permitted to use
291   /// reciprocal instead of division, aka the 'R' fast-math property.
hasAllowReciprocal()292   bool hasAllowReciprocal() const {
293     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
294   }
295 
296   /// Convenience function for getting all the fast-math flags
getFastMathFlags()297   FastMathFlags getFastMathFlags() const {
298     return FastMathFlags(SubclassOptionalData);
299   }
300 
301   /// \brief Get the maximum error permitted by this operation in ULPs.  An
302   /// accuracy of 0.0 means that the operation should be performed with the
303   /// default precision.
304   float getFPAccuracy() const;
305 
classof(const Instruction * I)306   static inline bool classof(const Instruction *I) {
307     return I->getType()->isFPOrFPVectorTy();
308   }
classof(const Value * V)309   static inline bool classof(const Value *V) {
310     return isa<Instruction>(V) && classof(cast<Instruction>(V));
311   }
312 };
313 
314 
315 /// ConcreteOperator - A helper template for defining operators for individual
316 /// opcodes.
317 template<typename SuperClass, unsigned Opc>
318 class ConcreteOperator : public SuperClass {
319 public:
classof(const Instruction * I)320   static inline bool classof(const Instruction *I) {
321     return I->getOpcode() == Opc;
322   }
classof(const ConstantExpr * CE)323   static inline bool classof(const ConstantExpr *CE) {
324     return CE->getOpcode() == Opc;
325   }
classof(const Value * V)326   static inline bool classof(const Value *V) {
327     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
328            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
329   }
330 };
331 
332 class AddOperator
333   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
334 };
335 class SubOperator
336   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
337 };
338 class MulOperator
339   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
340 };
341 class ShlOperator
342   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
343 };
344 
345 
346 class SDivOperator
347   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
348 };
349 class UDivOperator
350   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
351 };
352 class AShrOperator
353   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
354 };
355 class LShrOperator
356   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
357 };
358 
359 
360 
361 class GEPOperator
362   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
363   enum {
364     IsInBounds = (1 << 0)
365   };
366 
367   friend class GetElementPtrInst;
368   friend class ConstantExpr;
setIsInBounds(bool B)369   void setIsInBounds(bool B) {
370     SubclassOptionalData =
371       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
372   }
373 
374 public:
375   /// isInBounds - Test whether this is an inbounds GEP, as defined
376   /// by LangRef.html.
isInBounds()377   bool isInBounds() const {
378     return SubclassOptionalData & IsInBounds;
379   }
380 
idx_begin()381   inline op_iterator       idx_begin()       { return op_begin()+1; }
idx_begin()382   inline const_op_iterator idx_begin() const { return op_begin()+1; }
idx_end()383   inline op_iterator       idx_end()         { return op_end(); }
idx_end()384   inline const_op_iterator idx_end()   const { return op_end(); }
385 
getPointerOperand()386   Value *getPointerOperand() {
387     return getOperand(0);
388   }
getPointerOperand()389   const Value *getPointerOperand() const {
390     return getOperand(0);
391   }
getPointerOperandIndex()392   static unsigned getPointerOperandIndex() {
393     return 0U;                      // get index for modifying correct operand
394   }
395 
396   /// getPointerOperandType - Method to return the pointer operand as a
397   /// PointerType.
getPointerOperandType()398   Type *getPointerOperandType() const {
399     return getPointerOperand()->getType();
400   }
401 
402   /// getPointerAddressSpace - Method to return the address space of the
403   /// pointer operand.
getPointerAddressSpace()404   unsigned getPointerAddressSpace() const {
405     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
406   }
407 
getNumIndices()408   unsigned getNumIndices() const {  // Note: always non-negative
409     return getNumOperands() - 1;
410   }
411 
hasIndices()412   bool hasIndices() const {
413     return getNumOperands() > 1;
414   }
415 
416   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
417   /// zeros.  If so, the result pointer and the first operand have the same
418   /// value, just potentially different types.
hasAllZeroIndices()419   bool hasAllZeroIndices() const {
420     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
421       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
422         if (C->isZero())
423           continue;
424       return false;
425     }
426     return true;
427   }
428 
429   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
430   /// constant integers.  If so, the result pointer and the first operand have
431   /// a constant offset between them.
hasAllConstantIndices()432   bool hasAllConstantIndices() const {
433     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
434       if (!isa<ConstantInt>(I))
435         return false;
436     }
437     return true;
438   }
439 
440   /// \brief Accumulate the constant address offset of this GEP if possible.
441   ///
442   /// This routine accepts an APInt into which it will accumulate the constant
443   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
444   /// all-constant, it returns false and the value of the offset APInt is
445   /// undefined (it is *not* preserved!). The APInt passed into this routine
446   /// must be at exactly as wide as the IntPtr type for the address space of the
447   /// base GEP pointer.
accumulateConstantOffset(const DataLayout & DL,APInt & Offset)448   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
449     assert(Offset.getBitWidth() ==
450            DL.getPointerSizeInBits(getPointerAddressSpace()) &&
451            "The offset must have exactly as many bits as our pointer.");
452 
453     for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
454          GTI != GTE; ++GTI) {
455       ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
456       if (!OpC)
457         return false;
458       if (OpC->isZero())
459         continue;
460 
461       // Handle a struct index, which adds its field offset to the pointer.
462       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
463         unsigned ElementIdx = OpC->getZExtValue();
464         const StructLayout *SL = DL.getStructLayout(STy);
465         Offset += APInt(Offset.getBitWidth(),
466                         SL->getElementOffset(ElementIdx));
467         continue;
468       }
469 
470       // For array or vector indices, scale the index by the size of the type.
471       APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
472       Offset += Index * APInt(Offset.getBitWidth(),
473                               DL.getTypeAllocSize(GTI.getIndexedType()));
474     }
475     return true;
476   }
477 
478 };
479 
480 class PtrToIntOperator
481     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
482   friend class PtrToInt;
483   friend class ConstantExpr;
484 
485 public:
getPointerOperand()486   Value *getPointerOperand() {
487     return getOperand(0);
488   }
getPointerOperand()489   const Value *getPointerOperand() const {
490     return getOperand(0);
491   }
getPointerOperandIndex()492   static unsigned getPointerOperandIndex() {
493     return 0U;                      // get index for modifying correct operand
494   }
495 
496   /// getPointerOperandType - Method to return the pointer operand as a
497   /// PointerType.
getPointerOperandType()498   Type *getPointerOperandType() const {
499     return getPointerOperand()->getType();
500   }
501 
502   /// getPointerAddressSpace - Method to return the address space of the
503   /// pointer operand.
getPointerAddressSpace()504   unsigned getPointerAddressSpace() const {
505     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
506   }
507 };
508 
509 
510 } // End llvm namespace
511 
512 #endif
513