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.

478 lines
15 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_IR_OPERATOR_H
  15. #define LLVM_IR_OPERATOR_H
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DataLayout.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/Instruction.h"
  20. #include "llvm/IR/Type.h"
  21. #include "llvm/Support/GetElementPtrTypeIterator.h"
  22. namespace llvm {
  23. class GetElementPtrInst;
  24. class BinaryOperator;
  25. class ConstantExpr;
  26. /// Operator - This is a utility class that provides an abstraction for the
  27. /// common functionality between Instructions and ConstantExprs.
  28. ///
  29. class Operator : public User {
  30. private:
  31. // The Operator class is intended to be used as a utility, and is never itself
  32. // instantiated.
  33. void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
  34. void *operator new(size_t s) LLVM_DELETED_FUNCTION;
  35. Operator() LLVM_DELETED_FUNCTION;
  36. protected:
  37. // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
  38. // an overridden method that's not deleted in the base class. Cannot leave
  39. // this unimplemented because that leads to an ODR-violation.
  40. ~Operator();
  41. public:
  42. /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
  43. ///
  44. unsigned getOpcode() const {
  45. if (const Instruction *I = dyn_cast<Instruction>(this))
  46. return I->getOpcode();
  47. return cast<ConstantExpr>(this)->getOpcode();
  48. }
  49. /// getOpcode - If V is an Instruction or ConstantExpr, return its
  50. /// opcode. Otherwise return UserOp1.
  51. ///
  52. static unsigned getOpcode(const Value *V) {
  53. if (const Instruction *I = dyn_cast<Instruction>(V))
  54. return I->getOpcode();
  55. if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  56. return CE->getOpcode();
  57. return Instruction::UserOp1;
  58. }
  59. static inline bool classof(const Instruction *) { return true; }
  60. static inline bool classof(const ConstantExpr *) { return true; }
  61. static inline bool classof(const Value *V) {
  62. return isa<Instruction>(V) || isa<ConstantExpr>(V);
  63. }
  64. };
  65. /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
  66. /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
  67. /// despite that operator having the potential for overflow.
  68. ///
  69. class OverflowingBinaryOperator : public Operator {
  70. public:
  71. enum {
  72. NoUnsignedWrap = (1 << 0),
  73. NoSignedWrap = (1 << 1)
  74. };
  75. private:
  76. friend class BinaryOperator;
  77. friend class ConstantExpr;
  78. void setHasNoUnsignedWrap(bool B) {
  79. SubclassOptionalData =
  80. (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
  81. }
  82. void setHasNoSignedWrap(bool B) {
  83. SubclassOptionalData =
  84. (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
  85. }
  86. public:
  87. /// hasNoUnsignedWrap - Test whether this operation is known to never
  88. /// undergo unsigned overflow, aka the nuw property.
  89. bool hasNoUnsignedWrap() const {
  90. return SubclassOptionalData & NoUnsignedWrap;
  91. }
  92. /// hasNoSignedWrap - Test whether this operation is known to never
  93. /// undergo signed overflow, aka the nsw property.
  94. bool hasNoSignedWrap() const {
  95. return (SubclassOptionalData & NoSignedWrap) != 0;
  96. }
  97. static inline bool classof(const Instruction *I) {
  98. return I->getOpcode() == Instruction::Add ||
  99. I->getOpcode() == Instruction::Sub ||
  100. I->getOpcode() == Instruction::Mul ||
  101. I->getOpcode() == Instruction::Shl;
  102. }
  103. static inline bool classof(const ConstantExpr *CE) {
  104. return CE->getOpcode() == Instruction::Add ||
  105. CE->getOpcode() == Instruction::Sub ||
  106. CE->getOpcode() == Instruction::Mul ||
  107. CE->getOpcode() == Instruction::Shl;
  108. }
  109. static inline bool classof(const Value *V) {
  110. return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
  111. (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
  112. }
  113. };
  114. /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
  115. /// "exact", indicating that no bits are destroyed.
  116. class PossiblyExactOperator : public Operator {
  117. public:
  118. enum {
  119. IsExact = (1 << 0)
  120. };
  121. private:
  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. /// Convenience struct for specifying and reasoning about fast-math flags.
  151. class FastMathFlags {
  152. private:
  153. friend class FPMathOperator;
  154. unsigned Flags;
  155. FastMathFlags(unsigned F) : Flags(F) { }
  156. public:
  157. enum {
  158. UnsafeAlgebra = (1 << 0),
  159. NoNaNs = (1 << 1),
  160. NoInfs = (1 << 2),
  161. NoSignedZeros = (1 << 3),
  162. AllowReciprocal = (1 << 4)
  163. };
  164. FastMathFlags() : Flags(0)
  165. { }
  166. /// Whether any flag is set
  167. bool any() { return Flags != 0; }
  168. /// Set all the flags to false
  169. void clear() { Flags = 0; }
  170. /// Flag queries
  171. bool noNaNs() { return 0 != (Flags & NoNaNs); }
  172. bool noInfs() { return 0 != (Flags & NoInfs); }
  173. bool noSignedZeros() { return 0 != (Flags & NoSignedZeros); }
  174. bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
  175. bool unsafeAlgebra() { return 0 != (Flags & UnsafeAlgebra); }
  176. /// Flag setters
  177. void setNoNaNs() { Flags |= NoNaNs; }
  178. void setNoInfs() { Flags |= NoInfs; }
  179. void setNoSignedZeros() { Flags |= NoSignedZeros; }
  180. void setAllowReciprocal() { Flags |= AllowReciprocal; }
  181. void setUnsafeAlgebra() {
  182. Flags |= UnsafeAlgebra;
  183. setNoNaNs();
  184. setNoInfs();
  185. setNoSignedZeros();
  186. setAllowReciprocal();
  187. }
  188. };
  189. /// FPMathOperator - Utility class for floating point operations which can have
  190. /// information about relaxed accuracy requirements attached to them.
  191. class FPMathOperator : public Operator {
  192. private:
  193. friend class Instruction;
  194. void setHasUnsafeAlgebra(bool B) {
  195. SubclassOptionalData =
  196. (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
  197. (B * FastMathFlags::UnsafeAlgebra);
  198. // Unsafe algebra implies all the others
  199. if (B) {
  200. setHasNoNaNs(true);
  201. setHasNoInfs(true);
  202. setHasNoSignedZeros(true);
  203. setHasAllowReciprocal(true);
  204. }
  205. }
  206. void setHasNoNaNs(bool B) {
  207. SubclassOptionalData =
  208. (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
  209. (B * FastMathFlags::NoNaNs);
  210. }
  211. void setHasNoInfs(bool B) {
  212. SubclassOptionalData =
  213. (SubclassOptionalData & ~FastMathFlags::NoInfs) |
  214. (B * FastMathFlags::NoInfs);
  215. }
  216. void setHasNoSignedZeros(bool B) {
  217. SubclassOptionalData =
  218. (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
  219. (B * FastMathFlags::NoSignedZeros);
  220. }
  221. void setHasAllowReciprocal(bool B) {
  222. SubclassOptionalData =
  223. (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
  224. (B * FastMathFlags::AllowReciprocal);
  225. }
  226. /// Convenience function for setting all the fast-math flags
  227. void setFastMathFlags(FastMathFlags FMF) {
  228. SubclassOptionalData |= FMF.Flags;
  229. }
  230. public:
  231. /// Test whether this operation is permitted to be
  232. /// algebraically transformed, aka the 'A' fast-math property.
  233. bool hasUnsafeAlgebra() const {
  234. return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
  235. }
  236. /// Test whether this operation's arguments and results are to be
  237. /// treated as non-NaN, aka the 'N' fast-math property.
  238. bool hasNoNaNs() const {
  239. return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
  240. }
  241. /// Test whether this operation's arguments and results are to be
  242. /// treated as NoN-Inf, aka the 'I' fast-math property.
  243. bool hasNoInfs() const {
  244. return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
  245. }
  246. /// Test whether this operation can treat the sign of zero
  247. /// as insignificant, aka the 'S' fast-math property.
  248. bool hasNoSignedZeros() const {
  249. return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
  250. }
  251. /// Test whether this operation is permitted to use
  252. /// reciprocal instead of division, aka the 'R' fast-math property.
  253. bool hasAllowReciprocal() const {
  254. return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
  255. }
  256. /// Convenience function for getting all the fast-math flags
  257. FastMathFlags getFastMathFlags() const {
  258. return FastMathFlags(SubclassOptionalData);
  259. }
  260. /// \brief Get the maximum error permitted by this operation in ULPs. An
  261. /// accuracy of 0.0 means that the operation should be performed with the
  262. /// default precision.
  263. float getFPAccuracy() const;
  264. static inline bool classof(const Instruction *I) {
  265. return I->getType()->isFPOrFPVectorTy();
  266. }
  267. static inline bool classof(const Value *V) {
  268. return isa<Instruction>(V) && classof(cast<Instruction>(V));
  269. }
  270. };
  271. /// ConcreteOperator - A helper template for defining operators for individual
  272. /// opcodes.
  273. template<typename SuperClass, unsigned Opc>
  274. class ConcreteOperator : public SuperClass {
  275. public:
  276. static inline bool classof(const Instruction *I) {
  277. return I->getOpcode() == Opc;
  278. }
  279. static inline bool classof(const ConstantExpr *CE) {
  280. return CE->getOpcode() == Opc;
  281. }
  282. static inline bool classof(const Value *V) {
  283. return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
  284. (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
  285. }
  286. };
  287. class AddOperator
  288. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
  289. };
  290. class SubOperator
  291. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
  292. };
  293. class MulOperator
  294. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
  295. };
  296. class ShlOperator
  297. : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
  298. };
  299. class SDivOperator
  300. : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
  301. };
  302. class UDivOperator
  303. : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
  304. };
  305. class AShrOperator
  306. : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
  307. };
  308. class LShrOperator
  309. : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
  310. };
  311. class GEPOperator
  312. : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
  313. enum {
  314. IsInBounds = (1 << 0)
  315. };
  316. friend class GetElementPtrInst;
  317. friend class ConstantExpr;
  318. void setIsInBounds(bool B) {
  319. SubclassOptionalData =
  320. (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
  321. }
  322. public:
  323. /// isInBounds - Test whether this is an inbounds GEP, as defined
  324. /// by LangRef.html.
  325. bool isInBounds() const {
  326. return SubclassOptionalData & IsInBounds;
  327. }
  328. inline op_iterator idx_begin() { return op_begin()+1; }
  329. inline const_op_iterator idx_begin() const { return op_begin()+1; }
  330. inline op_iterator idx_end() { return op_end(); }
  331. inline const_op_iterator idx_end() const { return op_end(); }
  332. Value *getPointerOperand() {
  333. return getOperand(0);
  334. }
  335. const Value *getPointerOperand() const {
  336. return getOperand(0);
  337. }
  338. static unsigned getPointerOperandIndex() {
  339. return 0U; // get index for modifying correct operand
  340. }
  341. /// getPointerOperandType - Method to return the pointer operand as a
  342. /// PointerType.
  343. Type *getPointerOperandType() const {
  344. return getPointerOperand()->getType();
  345. }
  346. /// getPointerAddressSpace - Method to return the address space of the
  347. /// pointer operand.
  348. unsigned getPointerAddressSpace() const {
  349. return cast<PointerType>(getPointerOperandType())->getAddressSpace();
  350. }
  351. unsigned getNumIndices() const { // Note: always non-negative
  352. return getNumOperands() - 1;
  353. }
  354. bool hasIndices() const {
  355. return getNumOperands() > 1;
  356. }
  357. /// hasAllZeroIndices - Return true if all of the indices of this GEP are
  358. /// zeros. If so, the result pointer and the first operand have the same
  359. /// value, just potentially different types.
  360. bool hasAllZeroIndices() const {
  361. for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
  362. if (ConstantInt *C = dyn_cast<ConstantInt>(I))
  363. if (C->isZero())
  364. continue;
  365. return false;
  366. }
  367. return true;
  368. }
  369. /// hasAllConstantIndices - Return true if all of the indices of this GEP are
  370. /// constant integers. If so, the result pointer and the first operand have
  371. /// a constant offset between them.
  372. bool hasAllConstantIndices() const {
  373. for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
  374. if (!isa<ConstantInt>(I))
  375. return false;
  376. }
  377. return true;
  378. }
  379. /// \brief Accumulate the constant address offset of this GEP if possible.
  380. ///
  381. /// This routine accepts an APInt into which it will accumulate the constant
  382. /// offset of this GEP if the GEP is in fact constant. If the GEP is not
  383. /// all-constant, it returns false and the value of the offset APInt is
  384. /// undefined (it is *not* preserved!). The APInt passed into this routine
  385. /// must be at least as wide as the IntPtr type for the address space of
  386. /// the base GEP pointer.
  387. bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
  388. assert(Offset.getBitWidth() ==
  389. DL.getPointerSizeInBits(getPointerAddressSpace()) &&
  390. "The offset must have exactly as many bits as our pointer.");
  391. for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
  392. GTI != GTE; ++GTI) {
  393. ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
  394. if (!OpC)
  395. return false;
  396. if (OpC->isZero())
  397. continue;
  398. // Handle a struct index, which adds its field offset to the pointer.
  399. if (StructType *STy = dyn_cast<StructType>(*GTI)) {
  400. unsigned ElementIdx = OpC->getZExtValue();
  401. const StructLayout *SL = DL.getStructLayout(STy);
  402. Offset += APInt(Offset.getBitWidth(),
  403. SL->getElementOffset(ElementIdx));
  404. continue;
  405. }
  406. // For array or vector indices, scale the index by the size of the type.
  407. APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
  408. Offset += Index * APInt(Offset.getBitWidth(),
  409. DL.getTypeAllocSize(GTI.getIndexedType()));
  410. }
  411. return true;
  412. }
  413. };
  414. } // End llvm namespace
  415. #endif