2
0

Operator.h 16 KB

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