Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
10 KiB

  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. #ifndef LLVM_OPERATOR_H
  15. #define LLVM_OPERATOR_H
  16. #include "llvm/Constants.h"
  17. #include "llvm/DerivedTypes.h"
  18. #include "llvm/Instruction.h"
  19. #include "llvm/Type.h"
  20. namespace llvm {
  21. class GetElementPtrInst;
  22. class BinaryOperator;
  23. class ConstantExpr;
  24. /// Operator - This is a utility class that provides an abstraction for the
  25. /// common functionality between Instructions and ConstantExprs.
  26. ///
  27. class Operator : public User {
  28. private:
  29. // Do not implement any of these. The Operator class is intended to be used
  30. // as a utility, and is never itself instantiated.
  31. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  32. void *operator new(size_t s) LLVM_DELETED_FUNCTION;
  33. Operator() LLVM_DELETED_FUNCTION;
  34. // NOTE: cannot use LLVM_DELETED_FUNCTION because gcc errors when deleting
  35. // an override of a non-deleted function.
  36. ~Operator();
  37. public:
  38. /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
  39. ///
  40. unsigned getOpcode() const {
  41. if (const Instruction *I = dyn_cast<Instruction>(this))
  42. return I->getOpcode();
  43. return cast<ConstantExpr>(this)->getOpcode();
  44. }
  45. /// getOpcode - If V is an Instruction or ConstantExpr, return its
  46. /// opcode. Otherwise return UserOp1.
  47. ///
  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;
  54. }
  55. static inline bool classof(const Operator *) { return true; }
  56. static inline bool classof(const Instruction *) { return true; }
  57. static inline bool classof(const ConstantExpr *) { return true; }
  58. static inline bool classof(const Value *V) {
  59. return isa<Instruction>(V) || isa<ConstantExpr>(V);
  60. }
  61. };
  62. /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
  63. /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
  64. /// despite that operator having the potential for overflow.
  65. ///
  66. class OverflowingBinaryOperator : public Operator {
  67. public:
  68. enum {
  69. NoUnsignedWrap = (1 << 0),
  70. NoSignedWrap = (1 << 1)
  71. };
  72. private:
  73. ~OverflowingBinaryOperator(); // DO NOT IMPLEMENT
  74. friend class BinaryOperator;
  75. friend class ConstantExpr;
  76. void setHasNoUnsignedWrap(bool B) {
  77. SubclassOptionalData =
  78. (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
  79. }
  80. void setHasNoSignedWrap(bool B) {
  81. SubclassOptionalData =
  82. (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
  83. }
  84. public:
  85. /// hasNoUnsignedWrap - Test whether this operation is known to never
  86. /// undergo unsigned overflow, aka the nuw property.
  87. bool hasNoUnsignedWrap() const {
  88. return SubclassOptionalData & NoUnsignedWrap;
  89. }
  90. /// hasNoSignedWrap - Test whether this operation is known to never
  91. /// undergo signed overflow, aka the nsw property.
  92. bool hasNoSignedWrap() const {
  93. return (SubclassOptionalData & NoSignedWrap) != 0;
  94. }
  95. static inline bool classof(const OverflowingBinaryOperator *) { return true; }
  96. static inline bool classof(const Instruction *I) {
  97. return I->getOpcode() == Instruction::Add ||
  98. I->getOpcode() == Instruction::Sub ||
  99. I->getOpcode() == Instruction::Mul ||
  100. I->getOpcode() == Instruction::Shl;
  101. }
  102. static inline bool classof(const ConstantExpr *CE) {
  103. return CE->getOpcode() == Instruction::Add ||
  104. CE->getOpcode() == Instruction::Sub ||
  105. CE->getOpcode() == Instruction::Mul ||
  106. CE->getOpcode() == Instruction::Shl;
  107. }
  108. static inline bool classof(const Value *V) {
  109. return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
  110. (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
  111. }
  112. };
  113. /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
  114. /// "exact", indicating that no bits are destroyed.
  115. class PossiblyExactOperator : public Operator {
  116. public:
  117. enum {
  118. IsExact = (1 << 0)
  119. };
  120. private:
  121. ~PossiblyExactOperator(); // DO NOT IMPLEMENT
  122. friend class BinaryOperator;
  123. friend class ConstantExpr;
  124. void setIsExact(bool B) {
  125. SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
  126. }
  127. public:
  128. /// isExact - Test whether this division is known to be exact, with
  129. /// zero remainder.
  130. bool isExact() const {
  131. return SubclassOptionalData & IsExact;
  132. }
  133. static bool isPossiblyExactOpcode(unsigned OpC) {
  134. return OpC == Instruction::SDiv ||
  135. OpC == Instruction::UDiv ||
  136. OpC == Instruction::AShr ||
  137. OpC == Instruction::LShr;
  138. }
  139. static inline bool classof(const ConstantExpr *CE) {
  140. return isPossiblyExactOpcode(CE->getOpcode());
  141. }
  142. static inline bool classof(const Instruction *I) {
  143. return isPossiblyExactOpcode(I->getOpcode());
  144. }
  145. static inline bool classof(const Value *V) {
  146. return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
  147. (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
  148. }
  149. };
  150. /// FPMathOperator - Utility class for floating point operations which can have
  151. /// information about relaxed accuracy requirements attached to them.
  152. class FPMathOperator : public Operator {
  153. private:
  154. ~FPMathOperator(); // DO NOT IMPLEMENT
  155. public:
  156. /// \brief Get the maximum error permitted by this operation in ULPs. An
  157. /// accuracy of 0.0 means that the operation should be performed with the
  158. /// default precision.
  159. float getFPAccuracy() const;
  160. static inline bool classof(const FPMathOperator *) { return true; }
  161. static inline bool classof(const Instruction *I) {
  162. return I->getType()->isFPOrFPVectorTy();
  163. }
  164. static inline bool classof(const Value *V) {
  165. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  166. }
  167. };
  168. /// ConcreteOperator - A helper template for defining operators for individual
  169. /// opcodes.
  170. template<typename SuperClass, unsigned Opc>
  171. class ConcreteOperator : public SuperClass {
  172. ~ConcreteOperator() LLVM_DELETED_FUNCTION;
  173. public:
  174. static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
  175. return true;
  176. }
  177. static inline bool classof(const Instruction *I) {
  178. return I->getOpcode() == Opc;
  179. }
  180. static inline bool classof(const ConstantExpr *CE) {
  181. return CE->getOpcode() == Opc;
  182. }
  183. static inline bool classof(const Value *V) {
  184. return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
  185. (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
  186. }
  187. };
  188. class AddOperator
  189. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
  190. ~AddOperator() LLVM_DELETED_FUNCTION;
  191. };
  192. class SubOperator
  193. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
  194. ~SubOperator() LLVM_DELETED_FUNCTION;
  195. };
  196. class MulOperator
  197. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
  198. ~MulOperator() LLVM_DELETED_FUNCTION;
  199. };
  200. class ShlOperator
  201. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
  202. ~ShlOperator() LLVM_DELETED_FUNCTION;
  203. };
  204. class SDivOperator
  205. : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
  206. ~SDivOperator() LLVM_DELETED_FUNCTION;
  207. };
  208. class UDivOperator
  209. : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
  210. ~UDivOperator() LLVM_DELETED_FUNCTION;
  211. };
  212. class AShrOperator
  213. : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
  214. ~AShrOperator() LLVM_DELETED_FUNCTION;
  215. };
  216. class LShrOperator
  217. : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
  218. ~LShrOperator() LLVM_DELETED_FUNCTION;
  219. };
  220. class GEPOperator
  221. : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
  222. ~GEPOperator() LLVM_DELETED_FUNCTION;
  223. enum {
  224. IsInBounds = (1 << 0)
  225. };
  226. friend class GetElementPtrInst;
  227. friend class ConstantExpr;
  228. void setIsInBounds(bool B) {
  229. SubclassOptionalData =
  230. (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
  231. }
  232. public:
  233. /// isInBounds - Test whether this is an inbounds GEP, as defined
  234. /// by LangRef.html.
  235. bool isInBounds() const {
  236. return SubclassOptionalData & IsInBounds;
  237. }
  238. inline op_iterator idx_begin() { return op_begin()+1; }
  239. inline const_op_iterator idx_begin() const { return op_begin()+1; }
  240. inline op_iterator idx_end() { return op_end(); }
  241. inline const_op_iterator idx_end() const { return op_end(); }
  242. Value *getPointerOperand() {
  243. return getOperand(0);
  244. }
  245. const Value *getPointerOperand() const {
  246. return getOperand(0);
  247. }
  248. static unsigned getPointerOperandIndex() {
  249. return 0U; // get index for modifying correct operand
  250. }
  251. /// getPointerOperandType - Method to return the pointer operand as a
  252. /// PointerType.
  253. Type *getPointerOperandType() const {
  254. return getPointerOperand()->getType();
  255. }
  256. /// getPointerAddressSpace - Method to return the address space of the
  257. /// pointer operand.
  258. unsigned getPointerAddressSpace() const {
  259. return cast<PointerType>(getPointerOperandType())->getAddressSpace();
  260. }
  261. unsigned getNumIndices() const { // Note: always non-negative
  262. return getNumOperands() - 1;
  263. }
  264. bool hasIndices() const {
  265. return getNumOperands() > 1;
  266. }
  267. /// hasAllZeroIndices - Return true if all of the indices of this GEP are
  268. /// zeros. If so, the result pointer and the first operand have the same
  269. /// value, just potentially different types.
  270. bool hasAllZeroIndices() const {
  271. for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
  272. if (ConstantInt *C = dyn_cast<ConstantInt>(I))
  273. if (C->isZero())
  274. continue;
  275. return false;
  276. }
  277. return true;
  278. }
  279. /// hasAllConstantIndices - Return true if all of the indices of this GEP are
  280. /// constant integers. If so, the result pointer and the first operand have
  281. /// a constant offset between them.
  282. bool hasAllConstantIndices() const {
  283. for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
  284. if (!isa<ConstantInt>(I))
  285. return false;
  286. }
  287. return true;
  288. }
  289. };
  290. } // End llvm namespace
  291. #endif