Constants.h 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. //===-- llvm/Constants.h - Constant class subclass definitions --*- 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. /// @file
  11. /// This file contains the declarations for the subclasses of Constant,
  12. /// which represent the different flavors of constant values that live in LLVM.
  13. /// Note that Constants are immutable (once created they never change) and are
  14. /// fully shared by structural equivalence. This means that two structurally
  15. /// equivalent constants will always have the same address. Constants are
  16. /// created on demand as needed and never deleted: thus clients don't have to
  17. /// worry about the lifetime of the objects.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_IR_CONSTANTS_H
  21. #define LLVM_IR_CONSTANTS_H
  22. #include "llvm/ADT/APFloat.h"
  23. #include "llvm/ADT/APInt.h"
  24. #include "llvm/ADT/ArrayRef.h"
  25. #include "llvm/IR/Constant.h"
  26. #include "llvm/IR/DerivedTypes.h"
  27. #include "llvm/IR/OperandTraits.h"
  28. namespace llvm {
  29. class ArrayType;
  30. class IntegerType;
  31. class StructType;
  32. class PointerType;
  33. class VectorType;
  34. class SequentialType;
  35. struct ConstantExprKeyType;
  36. template <class ConstantClass> struct ConstantAggrKeyType;
  37. //===----------------------------------------------------------------------===//
  38. /// This is the shared class of boolean and integer constants. This class
  39. /// represents both boolean and integral constants.
  40. /// @brief Class for constant integers.
  41. class ConstantInt : public Constant {
  42. void anchor() override;
  43. void *operator new(size_t, unsigned) = delete;
  44. ConstantInt(const ConstantInt &) = delete;
  45. ConstantInt(IntegerType *Ty, const APInt& V);
  46. APInt Val;
  47. friend class Constant;
  48. void destroyConstantImpl();
  49. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  50. protected:
  51. // allocate space for exactly zero operands
  52. void *operator new(size_t s) {
  53. return User::operator new(s, 0);
  54. }
  55. public:
  56. static ConstantInt *getTrue(LLVMContext &Context);
  57. static ConstantInt *getFalse(LLVMContext &Context);
  58. static Constant *getTrue(Type *Ty);
  59. static Constant *getFalse(Type *Ty);
  60. /// If Ty is a vector type, return a Constant with a splat of the given
  61. /// value. Otherwise return a ConstantInt for the given value.
  62. static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
  63. /// Return a ConstantInt with the specified integer value for the specified
  64. /// type. If the type is wider than 64 bits, the value will be zero-extended
  65. /// to fit the type, unless isSigned is true, in which case the value will
  66. /// be interpreted as a 64-bit signed integer and sign-extended to fit
  67. /// the type.
  68. /// @brief Get a ConstantInt for a specific value.
  69. static ConstantInt *get(IntegerType *Ty, uint64_t V,
  70. bool isSigned = false);
  71. /// Return a ConstantInt with the specified value for the specified type. The
  72. /// value V will be canonicalized to a an unsigned APInt. Accessing it with
  73. /// either getSExtValue() or getZExtValue() will yield a correctly sized and
  74. /// signed value for the type Ty.
  75. /// @brief Get a ConstantInt for a specific signed value.
  76. static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
  77. static Constant *getSigned(Type *Ty, int64_t V);
  78. /// Return a ConstantInt with the specified value and an implied Type. The
  79. /// type is the integer type that corresponds to the bit width of the value.
  80. static ConstantInt *get(LLVMContext &Context, const APInt &V);
  81. /// Return a ConstantInt constructed from the string strStart with the given
  82. /// radix.
  83. static ConstantInt *get(IntegerType *Ty, StringRef Str,
  84. uint8_t radix);
  85. /// If Ty is a vector type, return a Constant with a splat of the given
  86. /// value. Otherwise return a ConstantInt for the given value.
  87. static Constant *get(Type* Ty, const APInt& V);
  88. /// Return the constant as an APInt value reference. This allows clients to
  89. /// obtain a copy of the value, with all its precision in tact.
  90. /// @brief Return the constant's value.
  91. inline const APInt &getValue() const {
  92. return Val;
  93. }
  94. /// getBitWidth - Return the bitwidth of this constant.
  95. unsigned getBitWidth() const { return Val.getBitWidth(); }
  96. /// Return the constant as a 64-bit unsigned integer value after it
  97. /// has been zero extended as appropriate for the type of this constant. Note
  98. /// that this method can assert if the value does not fit in 64 bits.
  99. /// @brief Return the zero extended value.
  100. inline uint64_t getZExtValue() const {
  101. return Val.getZExtValue();
  102. }
  103. /// Return the constant as a 64-bit integer value after it has been sign
  104. /// extended as appropriate for the type of this constant. Note that
  105. /// this method can assert if the value does not fit in 64 bits.
  106. /// @brief Return the sign extended value.
  107. inline int64_t getSExtValue() const {
  108. return Val.getSExtValue();
  109. }
  110. /// A helper method that can be used to determine if the constant contained
  111. /// within is equal to a constant. This only works for very small values,
  112. /// because this is all that can be represented with all types.
  113. /// @brief Determine if this constant's value is same as an unsigned char.
  114. bool equalsInt(uint64_t V) const {
  115. return Val == V;
  116. }
  117. /// getType - Specialize the getType() method to always return an IntegerType,
  118. /// which reduces the amount of casting needed in parts of the compiler.
  119. ///
  120. inline IntegerType *getType() const {
  121. return cast<IntegerType>(Value::getType());
  122. }
  123. /// This static method returns true if the type Ty is big enough to
  124. /// represent the value V. This can be used to avoid having the get method
  125. /// assert when V is larger than Ty can represent. Note that there are two
  126. /// versions of this method, one for unsigned and one for signed integers.
  127. /// Although ConstantInt canonicalizes everything to an unsigned integer,
  128. /// the signed version avoids callers having to convert a signed quantity
  129. /// to the appropriate unsigned type before calling the method.
  130. /// @returns true if V is a valid value for type Ty
  131. /// @brief Determine if the value is in range for the given type.
  132. static bool isValueValidForType(Type *Ty, uint64_t V);
  133. static bool isValueValidForType(Type *Ty, int64_t V);
  134. bool isNegative() const { return Val.isNegative(); }
  135. /// This is just a convenience method to make client code smaller for a
  136. /// common code. It also correctly performs the comparison without the
  137. /// potential for an assertion from getZExtValue().
  138. bool isZero() const {
  139. return Val == 0;
  140. }
  141. /// This is just a convenience method to make client code smaller for a
  142. /// common case. It also correctly performs the comparison without the
  143. /// potential for an assertion from getZExtValue().
  144. /// @brief Determine if the value is one.
  145. bool isOne() const {
  146. return Val == 1;
  147. }
  148. /// This function will return true iff every bit in this constant is set
  149. /// to true.
  150. /// @returns true iff this constant's bits are all set to true.
  151. /// @brief Determine if the value is all ones.
  152. bool isMinusOne() const {
  153. return Val.isAllOnesValue();
  154. }
  155. /// This function will return true iff this constant represents the largest
  156. /// value that may be represented by the constant's type.
  157. /// @returns true iff this is the largest value that may be represented
  158. /// by this type.
  159. /// @brief Determine if the value is maximal.
  160. bool isMaxValue(bool isSigned) const {
  161. if (isSigned)
  162. return Val.isMaxSignedValue();
  163. else
  164. return Val.isMaxValue();
  165. }
  166. /// This function will return true iff this constant represents the smallest
  167. /// value that may be represented by this constant's type.
  168. /// @returns true if this is the smallest value that may be represented by
  169. /// this type.
  170. /// @brief Determine if the value is minimal.
  171. bool isMinValue(bool isSigned) const {
  172. if (isSigned)
  173. return Val.isMinSignedValue();
  174. else
  175. return Val.isMinValue();
  176. }
  177. /// This function will return true iff this constant represents a value with
  178. /// active bits bigger than 64 bits or a value greater than the given uint64_t
  179. /// value.
  180. /// @returns true iff this constant is greater or equal to the given number.
  181. /// @brief Determine if the value is greater or equal to the given number.
  182. bool uge(uint64_t Num) const {
  183. return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
  184. }
  185. /// getLimitedValue - If the value is smaller than the specified limit,
  186. /// return it, otherwise return the limit value. This causes the value
  187. /// to saturate to the limit.
  188. /// @returns the min of the value of the constant and the specified value
  189. /// @brief Get the constant's value with a saturation limit
  190. uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
  191. return Val.getLimitedValue(Limit);
  192. }
  193. /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
  194. static bool classof(const Value *V) {
  195. return V->getValueID() == ConstantIntVal;
  196. }
  197. };
  198. //===----------------------------------------------------------------------===//
  199. /// ConstantFP - Floating Point Values [float, double]
  200. ///
  201. class ConstantFP : public Constant {
  202. APFloat Val;
  203. void anchor() override;
  204. void *operator new(size_t, unsigned) = delete;
  205. ConstantFP(const ConstantFP &) = delete;
  206. friend class LLVMContextImpl;
  207. friend class Constant;
  208. void destroyConstantImpl();
  209. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  210. protected:
  211. ConstantFP(Type *Ty, const APFloat& V);
  212. protected:
  213. // allocate space for exactly zero operands
  214. void *operator new(size_t s) {
  215. return User::operator new(s, 0);
  216. }
  217. public:
  218. /// Floating point negation must be implemented with f(x) = -0.0 - x. This
  219. /// method returns the negative zero constant for floating point or vector
  220. /// floating point types; for all other types, it returns the null value.
  221. static Constant *getZeroValueForNegation(Type *Ty);
  222. /// get() - This returns a ConstantFP, or a vector containing a splat of a
  223. /// ConstantFP, for the specified value in the specified type. This should
  224. /// only be used for simple constant values like 2.0/1.0 etc, that are
  225. /// known-valid both as host double and as the target format.
  226. static Constant *get(Type* Ty, double V);
  227. static Constant *get(Type* Ty, StringRef Str);
  228. static ConstantFP *get(LLVMContext &Context, const APFloat &V);
  229. static Constant *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
  230. static Constant *getNegativeZero(Type *Ty);
  231. static Constant *getInfinity(Type *Ty, bool Negative = false);
  232. /// isValueValidForType - return true if Ty is big enough to represent V.
  233. static bool isValueValidForType(Type *Ty, const APFloat &V);
  234. inline const APFloat &getValueAPF() const { return Val; }
  235. /// isZero - Return true if the value is positive or negative zero.
  236. bool isZero() const { return Val.isZero(); }
  237. /// isNegative - Return true if the sign bit is set.
  238. bool isNegative() const { return Val.isNegative(); }
  239. /// isInfinity - Return true if the value is infinity
  240. bool isInfinity() const { return Val.isInfinity(); }
  241. /// isNaN - Return true if the value is a NaN.
  242. bool isNaN() const { return Val.isNaN(); }
  243. /// isExactlyValue - We don't rely on operator== working on double values, as
  244. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  245. /// As such, this method can be used to do an exact bit-for-bit comparison of
  246. /// two floating point values. The version with a double operand is retained
  247. /// because it's so convenient to write isExactlyValue(2.0), but please use
  248. /// it only for simple constants.
  249. bool isExactlyValue(const APFloat &V) const;
  250. bool isExactlyValue(double V) const {
  251. bool ignored;
  252. APFloat FV(V);
  253. FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
  254. return isExactlyValue(FV);
  255. }
  256. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  257. static bool classof(const Value *V) {
  258. return V->getValueID() == ConstantFPVal;
  259. }
  260. };
  261. //===----------------------------------------------------------------------===//
  262. /// ConstantAggregateZero - All zero aggregate value
  263. ///
  264. class ConstantAggregateZero : public Constant {
  265. void *operator new(size_t, unsigned) = delete;
  266. ConstantAggregateZero(const ConstantAggregateZero &) = delete;
  267. friend class Constant;
  268. void destroyConstantImpl();
  269. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  270. protected:
  271. explicit ConstantAggregateZero(Type *ty)
  272. : Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
  273. protected:
  274. // allocate space for exactly zero operands
  275. void *operator new(size_t s) {
  276. return User::operator new(s, 0);
  277. }
  278. public:
  279. static ConstantAggregateZero *get(Type *Ty);
  280. /// getSequentialElement - If this CAZ has array or vector type, return a zero
  281. /// with the right element type.
  282. Constant *getSequentialElement() const;
  283. /// getStructElement - If this CAZ has struct type, return a zero with the
  284. /// right element type for the specified element.
  285. Constant *getStructElement(unsigned Elt) const;
  286. /// getElementValue - Return a zero of the right value for the specified GEP
  287. /// index.
  288. Constant *getElementValue(Constant *C) const;
  289. /// getElementValue - Return a zero of the right value for the specified GEP
  290. /// index.
  291. Constant *getElementValue(unsigned Idx) const;
  292. /// \brief Return the number of elements in the array, vector, or struct.
  293. unsigned getNumElements() const;
  294. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  295. ///
  296. static bool classof(const Value *V) {
  297. return V->getValueID() == ConstantAggregateZeroVal;
  298. }
  299. };
  300. //===----------------------------------------------------------------------===//
  301. /// ConstantArray - Constant Array Declarations
  302. ///
  303. class ConstantArray : public Constant {
  304. friend struct ConstantAggrKeyType<ConstantArray>;
  305. ConstantArray(const ConstantArray &) = delete;
  306. friend class Constant;
  307. void destroyConstantImpl();
  308. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  309. protected:
  310. ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
  311. public:
  312. // ConstantArray accessors
  313. static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
  314. private:
  315. static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
  316. public:
  317. /// Transparently provide more efficient getOperand methods.
  318. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
  319. /// getType - Specialize the getType() method to always return an ArrayType,
  320. /// which reduces the amount of casting needed in parts of the compiler.
  321. ///
  322. inline ArrayType *getType() const {
  323. return cast<ArrayType>(Value::getType());
  324. }
  325. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  326. static bool classof(const Value *V) {
  327. return V->getValueID() == ConstantArrayVal;
  328. }
  329. };
  330. template <>
  331. struct OperandTraits<ConstantArray> :
  332. public VariadicOperandTraits<ConstantArray> {
  333. };
  334. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
  335. //===----------------------------------------------------------------------===//
  336. // ConstantStruct - Constant Struct Declarations
  337. //
  338. class ConstantStruct : public Constant {
  339. friend struct ConstantAggrKeyType<ConstantStruct>;
  340. ConstantStruct(const ConstantStruct &) = delete;
  341. friend class Constant;
  342. void destroyConstantImpl();
  343. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  344. protected:
  345. ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
  346. public:
  347. // ConstantStruct accessors
  348. static Constant *get(StructType *T, ArrayRef<Constant*> V);
  349. static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
  350. /// getAnon - Return an anonymous struct that has the specified
  351. /// elements. If the struct is possibly empty, then you must specify a
  352. /// context.
  353. static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
  354. return get(getTypeForElements(V, Packed), V);
  355. }
  356. static Constant *getAnon(LLVMContext &Ctx,
  357. ArrayRef<Constant*> V, bool Packed = false) {
  358. return get(getTypeForElements(Ctx, V, Packed), V);
  359. }
  360. /// getTypeForElements - Return an anonymous struct type to use for a constant
  361. /// with the specified set of elements. The list must not be empty.
  362. static StructType *getTypeForElements(ArrayRef<Constant*> V,
  363. bool Packed = false);
  364. /// getTypeForElements - This version of the method allows an empty list.
  365. static StructType *getTypeForElements(LLVMContext &Ctx,
  366. ArrayRef<Constant*> V,
  367. bool Packed = false);
  368. /// Transparently provide more efficient getOperand methods.
  369. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
  370. /// getType() specialization - Reduce amount of casting...
  371. ///
  372. inline StructType *getType() const {
  373. return cast<StructType>(Value::getType());
  374. }
  375. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  376. static bool classof(const Value *V) {
  377. return V->getValueID() == ConstantStructVal;
  378. }
  379. };
  380. template <>
  381. struct OperandTraits<ConstantStruct> :
  382. public VariadicOperandTraits<ConstantStruct> {
  383. };
  384. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
  385. //===----------------------------------------------------------------------===//
  386. /// ConstantVector - Constant Vector Declarations
  387. ///
  388. class ConstantVector : public Constant {
  389. friend struct ConstantAggrKeyType<ConstantVector>;
  390. ConstantVector(const ConstantVector &) = delete;
  391. friend class Constant;
  392. void destroyConstantImpl();
  393. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  394. protected:
  395. ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
  396. public:
  397. // ConstantVector accessors
  398. static Constant *get(ArrayRef<Constant*> V);
  399. private:
  400. static Constant *getImpl(ArrayRef<Constant *> V);
  401. public:
  402. /// getSplat - Return a ConstantVector with the specified constant in each
  403. /// element.
  404. static Constant *getSplat(unsigned NumElts, Constant *Elt);
  405. /// Transparently provide more efficient getOperand methods.
  406. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
  407. /// getType - Specialize the getType() method to always return a VectorType,
  408. /// which reduces the amount of casting needed in parts of the compiler.
  409. ///
  410. inline VectorType *getType() const {
  411. return cast<VectorType>(Value::getType());
  412. }
  413. /// getSplatValue - If this is a splat constant, meaning that all of the
  414. /// elements have the same value, return that value. Otherwise return NULL.
  415. Constant *getSplatValue() const;
  416. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  417. static bool classof(const Value *V) {
  418. return V->getValueID() == ConstantVectorVal;
  419. }
  420. };
  421. template <>
  422. struct OperandTraits<ConstantVector> :
  423. public VariadicOperandTraits<ConstantVector> {
  424. };
  425. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
  426. //===----------------------------------------------------------------------===//
  427. /// ConstantPointerNull - a constant pointer value that points to null
  428. ///
  429. class ConstantPointerNull : public Constant {
  430. void *operator new(size_t, unsigned) = delete;
  431. ConstantPointerNull(const ConstantPointerNull &) = delete;
  432. friend class Constant;
  433. void destroyConstantImpl();
  434. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  435. protected:
  436. explicit ConstantPointerNull(PointerType *T)
  437. : Constant(T,
  438. Value::ConstantPointerNullVal, nullptr, 0) {}
  439. protected:
  440. // allocate space for exactly zero operands
  441. void *operator new(size_t s) {
  442. return User::operator new(s, 0);
  443. }
  444. public:
  445. /// get() - Static factory methods - Return objects of the specified value
  446. static ConstantPointerNull *get(PointerType *T);
  447. /// getType - Specialize the getType() method to always return an PointerType,
  448. /// which reduces the amount of casting needed in parts of the compiler.
  449. ///
  450. inline PointerType *getType() const {
  451. return cast<PointerType>(Value::getType());
  452. }
  453. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  454. static bool classof(const Value *V) {
  455. return V->getValueID() == ConstantPointerNullVal;
  456. }
  457. };
  458. //===----------------------------------------------------------------------===//
  459. /// ConstantDataSequential - A vector or array constant whose element type is a
  460. /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
  461. /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
  462. /// operands because it stores all of the elements of the constant as densely
  463. /// packed data, instead of as Value*'s.
  464. ///
  465. /// This is the common base class of ConstantDataArray and ConstantDataVector.
  466. ///
  467. class ConstantDataSequential : public Constant {
  468. friend class LLVMContextImpl;
  469. /// DataElements - A pointer to the bytes underlying this constant (which is
  470. /// owned by the uniquing StringMap).
  471. const char *DataElements;
  472. /// Next - This forms a link list of ConstantDataSequential nodes that have
  473. /// the same value but different type. For example, 0,0,0,1 could be a 4
  474. /// element array of i8, or a 1-element array of i32. They'll both end up in
  475. /// the same StringMap bucket, linked up.
  476. ConstantDataSequential *Next;
  477. void *operator new(size_t, unsigned) = delete;
  478. ConstantDataSequential(const ConstantDataSequential &) = delete;
  479. friend class Constant;
  480. void destroyConstantImpl();
  481. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  482. protected:
  483. explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
  484. : Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
  485. ~ConstantDataSequential() override { delete Next; }
  486. static Constant *getImpl(StringRef Bytes, Type *Ty);
  487. protected:
  488. // allocate space for exactly zero operands.
  489. void *operator new(size_t s) {
  490. return User::operator new(s, 0);
  491. }
  492. public:
  493. /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
  494. /// formed with a vector or array of the specified element type.
  495. /// ConstantDataArray only works with normal float and int types that are
  496. /// stored densely in memory, not with things like i42 or x86_f80.
  497. static bool isElementTypeCompatible(const Type *Ty);
  498. /// getElementAsInteger - If this is a sequential container of integers (of
  499. /// any size), return the specified element in the low bits of a uint64_t.
  500. uint64_t getElementAsInteger(unsigned i) const;
  501. /// getElementAsAPFloat - If this is a sequential container of floating point
  502. /// type, return the specified element as an APFloat.
  503. APFloat getElementAsAPFloat(unsigned i) const;
  504. /// getElementAsFloat - If this is an sequential container of floats, return
  505. /// the specified element as a float.
  506. float getElementAsFloat(unsigned i) const;
  507. /// getElementAsDouble - If this is an sequential container of doubles, return
  508. /// the specified element as a double.
  509. double getElementAsDouble(unsigned i) const;
  510. /// getElementAsConstant - Return a Constant for a specified index's element.
  511. /// Note that this has to compute a new constant to return, so it isn't as
  512. /// efficient as getElementAsInteger/Float/Double.
  513. Constant *getElementAsConstant(unsigned i) const;
  514. /// getType - Specialize the getType() method to always return a
  515. /// SequentialType, which reduces the amount of casting needed in parts of the
  516. /// compiler.
  517. inline SequentialType *getType() const {
  518. return cast<SequentialType>(Value::getType());
  519. }
  520. /// getElementType - Return the element type of the array/vector.
  521. Type *getElementType() const;
  522. /// getNumElements - Return the number of elements in the array or vector.
  523. unsigned getNumElements() const;
  524. /// getElementByteSize - Return the size (in bytes) of each element in the
  525. /// array/vector. The size of the elements is known to be a multiple of one
  526. /// byte.
  527. uint64_t getElementByteSize() const;
  528. /// isString - This method returns true if this is an array of i8.
  529. bool isString() const;
  530. /// isCString - This method returns true if the array "isString", ends with a
  531. /// nul byte, and does not contains any other nul bytes.
  532. bool isCString() const;
  533. /// getAsString - If this array is isString(), then this method returns the
  534. /// array as a StringRef. Otherwise, it asserts out.
  535. ///
  536. StringRef getAsString() const {
  537. assert(isString() && "Not a string");
  538. return getRawDataValues();
  539. }
  540. /// getAsCString - If this array is isCString(), then this method returns the
  541. /// array (without the trailing null byte) as a StringRef. Otherwise, it
  542. /// asserts out.
  543. ///
  544. StringRef getAsCString() const {
  545. assert(isCString() && "Isn't a C string");
  546. StringRef Str = getAsString();
  547. return Str.substr(0, Str.size()-1);
  548. }
  549. /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
  550. /// that this is an extremely tricky thing to work with, as it exposes the
  551. /// host endianness of the data elements.
  552. StringRef getRawDataValues() const;
  553. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  554. ///
  555. static bool classof(const Value *V) {
  556. return V->getValueID() == ConstantDataArrayVal ||
  557. V->getValueID() == ConstantDataVectorVal;
  558. }
  559. private:
  560. const char *getElementPointer(unsigned Elt) const;
  561. };
  562. //===----------------------------------------------------------------------===//
  563. /// ConstantDataArray - An array constant whose element type is a simple
  564. /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
  565. /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
  566. /// operands because it stores all of the elements of the constant as densely
  567. /// packed data, instead of as Value*'s.
  568. class ConstantDataArray : public ConstantDataSequential {
  569. void *operator new(size_t, unsigned) = delete;
  570. ConstantDataArray(const ConstantDataArray &) = delete;
  571. void anchor() override;
  572. friend class ConstantDataSequential;
  573. explicit ConstantDataArray(Type *ty, const char *Data)
  574. : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
  575. protected:
  576. // allocate space for exactly zero operands.
  577. void *operator new(size_t s) {
  578. return User::operator new(s, 0);
  579. }
  580. public:
  581. /// get() constructors - Return a constant with array type with an element
  582. /// count and element type matching the ArrayRef passed in. Note that this
  583. /// can return a ConstantAggregateZero object.
  584. static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
  585. static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
  586. static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
  587. static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
  588. static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
  589. static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
  590. /// getFP() constructors - Return a constant with array type with an element
  591. /// count and element type of float with precision matching the number of
  592. /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
  593. /// double for 64bits) Note that this can return a ConstantAggregateZero
  594. /// object.
  595. static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
  596. static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
  597. static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
  598. /// getString - This method constructs a CDS and initializes it with a text
  599. /// string. The default behavior (AddNull==true) causes a null terminator to
  600. /// be placed at the end of the array (increasing the length of the string by
  601. /// one more than the StringRef would normally indicate. Pass AddNull=false
  602. /// to disable this behavior.
  603. static Constant *getString(LLVMContext &Context, StringRef Initializer,
  604. bool AddNull = true);
  605. /// getType - Specialize the getType() method to always return an ArrayType,
  606. /// which reduces the amount of casting needed in parts of the compiler.
  607. ///
  608. inline ArrayType *getType() const {
  609. return cast<ArrayType>(Value::getType());
  610. }
  611. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  612. ///
  613. static bool classof(const Value *V) {
  614. return V->getValueID() == ConstantDataArrayVal;
  615. }
  616. };
  617. //===----------------------------------------------------------------------===//
  618. /// ConstantDataVector - A vector constant whose element type is a simple
  619. /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
  620. /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
  621. /// operands because it stores all of the elements of the constant as densely
  622. /// packed data, instead of as Value*'s.
  623. class ConstantDataVector : public ConstantDataSequential {
  624. void *operator new(size_t, unsigned) = delete;
  625. ConstantDataVector(const ConstantDataVector &) = delete;
  626. void anchor() override;
  627. friend class ConstantDataSequential;
  628. explicit ConstantDataVector(Type *ty, const char *Data)
  629. : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
  630. protected:
  631. // allocate space for exactly zero operands.
  632. void *operator new(size_t s) {
  633. return User::operator new(s, 0);
  634. }
  635. public:
  636. /// get() constructors - Return a constant with vector type with an element
  637. /// count and element type matching the ArrayRef passed in. Note that this
  638. /// can return a ConstantAggregateZero object.
  639. static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
  640. static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
  641. static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
  642. static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
  643. static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
  644. static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
  645. /// getFP() constructors - Return a constant with vector type with an element
  646. /// count and element type of float with the precision matching the number of
  647. /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
  648. /// double for 64bits) Note that this can return a ConstantAggregateZero
  649. /// object.
  650. static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
  651. static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
  652. static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
  653. /// getSplat - Return a ConstantVector with the specified constant in each
  654. /// element. The specified constant has to be a of a compatible type (i8/i16/
  655. /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
  656. static Constant *getSplat(unsigned NumElts, Constant *Elt);
  657. /// getSplatValue - If this is a splat constant, meaning that all of the
  658. /// elements have the same value, return that value. Otherwise return NULL.
  659. Constant *getSplatValue() const;
  660. /// getType - Specialize the getType() method to always return a VectorType,
  661. /// which reduces the amount of casting needed in parts of the compiler.
  662. ///
  663. inline VectorType *getType() const {
  664. return cast<VectorType>(Value::getType());
  665. }
  666. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  667. ///
  668. static bool classof(const Value *V) {
  669. return V->getValueID() == ConstantDataVectorVal;
  670. }
  671. };
  672. /// BlockAddress - The address of a basic block.
  673. ///
  674. class BlockAddress : public Constant {
  675. void *operator new(size_t, unsigned) = delete;
  676. void *operator new(size_t s) { return User::operator new(s, 2); }
  677. BlockAddress(Function *F, BasicBlock *BB);
  678. friend class Constant;
  679. void destroyConstantImpl();
  680. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  681. public:
  682. /// get - Return a BlockAddress for the specified function and basic block.
  683. static BlockAddress *get(Function *F, BasicBlock *BB);
  684. /// get - Return a BlockAddress for the specified basic block. The basic
  685. /// block must be embedded into a function.
  686. static BlockAddress *get(BasicBlock *BB);
  687. /// \brief Lookup an existing \c BlockAddress constant for the given
  688. /// BasicBlock.
  689. ///
  690. /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
  691. static BlockAddress *lookup(const BasicBlock *BB);
  692. /// Transparently provide more efficient getOperand methods.
  693. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  694. Function *getFunction() const { return (Function*)Op<0>().get(); }
  695. BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
  696. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  697. static inline bool classof(const Value *V) {
  698. return V->getValueID() == BlockAddressVal;
  699. }
  700. };
  701. template <>
  702. struct OperandTraits<BlockAddress> :
  703. public FixedNumOperandTraits<BlockAddress, 2> {
  704. };
  705. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
  706. //===----------------------------------------------------------------------===//
  707. /// ConstantExpr - a constant value that is initialized with an expression using
  708. /// other constant values.
  709. ///
  710. /// This class uses the standard Instruction opcodes to define the various
  711. /// constant expressions. The Opcode field for the ConstantExpr class is
  712. /// maintained in the Value::SubclassData field.
  713. class ConstantExpr : public Constant {
  714. friend struct ConstantExprKeyType;
  715. friend class Constant;
  716. void destroyConstantImpl();
  717. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  718. protected:
  719. ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
  720. : Constant(ty, ConstantExprVal, Ops, NumOps) {
  721. // Operation type (an Instruction opcode) is stored as the SubclassData.
  722. setValueSubclassData(Opcode);
  723. }
  724. public:
  725. // Static methods to construct a ConstantExpr of different kinds. Note that
  726. // these methods may return a object that is not an instance of the
  727. // ConstantExpr class, because they will attempt to fold the constant
  728. // expression into something simpler if possible.
  729. /// getAlignOf constant expr - computes the alignment of a type in a target
  730. /// independent way (Note: the return type is an i64).
  731. static Constant *getAlignOf(Type *Ty);
  732. /// getSizeOf constant expr - computes the (alloc) size of a type (in
  733. /// address-units, not bits) in a target independent way (Note: the return
  734. /// type is an i64).
  735. ///
  736. static Constant *getSizeOf(Type *Ty);
  737. /// getOffsetOf constant expr - computes the offset of a struct field in a
  738. /// target independent way (Note: the return type is an i64).
  739. ///
  740. static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
  741. /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
  742. /// which supports any aggregate type, and any Constant index.
  743. ///
  744. static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
  745. static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
  746. static Constant *getFNeg(Constant *C);
  747. static Constant *getNot(Constant *C);
  748. static Constant *getAdd(Constant *C1, Constant *C2,
  749. bool HasNUW = false, bool HasNSW = false);
  750. static Constant *getFAdd(Constant *C1, Constant *C2);
  751. static Constant *getSub(Constant *C1, Constant *C2,
  752. bool HasNUW = false, bool HasNSW = false);
  753. static Constant *getFSub(Constant *C1, Constant *C2);
  754. static Constant *getMul(Constant *C1, Constant *C2,
  755. bool HasNUW = false, bool HasNSW = false);
  756. static Constant *getFMul(Constant *C1, Constant *C2);
  757. static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
  758. static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
  759. static Constant *getFDiv(Constant *C1, Constant *C2);
  760. static Constant *getURem(Constant *C1, Constant *C2);
  761. static Constant *getSRem(Constant *C1, Constant *C2);
  762. static Constant *getFRem(Constant *C1, Constant *C2);
  763. static Constant *getAnd(Constant *C1, Constant *C2);
  764. static Constant *getOr(Constant *C1, Constant *C2);
  765. static Constant *getXor(Constant *C1, Constant *C2);
  766. static Constant *getShl(Constant *C1, Constant *C2,
  767. bool HasNUW = false, bool HasNSW = false);
  768. static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
  769. static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
  770. static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  771. static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  772. static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  773. static Constant *getFPTrunc(Constant *C, Type *Ty,
  774. bool OnlyIfReduced = false);
  775. static Constant *getFPExtend(Constant *C, Type *Ty,
  776. bool OnlyIfReduced = false);
  777. static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  778. static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  779. static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  780. static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
  781. static Constant *getPtrToInt(Constant *C, Type *Ty,
  782. bool OnlyIfReduced = false);
  783. static Constant *getIntToPtr(Constant *C, Type *Ty,
  784. bool OnlyIfReduced = false);
  785. static Constant *getBitCast(Constant *C, Type *Ty,
  786. bool OnlyIfReduced = false);
  787. static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
  788. bool OnlyIfReduced = false);
  789. static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
  790. static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
  791. static Constant *getNSWAdd(Constant *C1, Constant *C2) {
  792. return getAdd(C1, C2, false, true);
  793. }
  794. static Constant *getNUWAdd(Constant *C1, Constant *C2) {
  795. return getAdd(C1, C2, true, false);
  796. }
  797. static Constant *getNSWSub(Constant *C1, Constant *C2) {
  798. return getSub(C1, C2, false, true);
  799. }
  800. static Constant *getNUWSub(Constant *C1, Constant *C2) {
  801. return getSub(C1, C2, true, false);
  802. }
  803. static Constant *getNSWMul(Constant *C1, Constant *C2) {
  804. return getMul(C1, C2, false, true);
  805. }
  806. static Constant *getNUWMul(Constant *C1, Constant *C2) {
  807. return getMul(C1, C2, true, false);
  808. }
  809. static Constant *getNSWShl(Constant *C1, Constant *C2) {
  810. return getShl(C1, C2, false, true);
  811. }
  812. static Constant *getNUWShl(Constant *C1, Constant *C2) {
  813. return getShl(C1, C2, true, false);
  814. }
  815. static Constant *getExactSDiv(Constant *C1, Constant *C2) {
  816. return getSDiv(C1, C2, true);
  817. }
  818. static Constant *getExactUDiv(Constant *C1, Constant *C2) {
  819. return getUDiv(C1, C2, true);
  820. }
  821. static Constant *getExactAShr(Constant *C1, Constant *C2) {
  822. return getAShr(C1, C2, true);
  823. }
  824. static Constant *getExactLShr(Constant *C1, Constant *C2) {
  825. return getLShr(C1, C2, true);
  826. }
  827. /// getBinOpIdentity - Return the identity for the given binary operation,
  828. /// i.e. a constant C such that X op C = X and C op X = X for every X. It
  829. /// returns null if the operator doesn't have an identity.
  830. static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
  831. /// getBinOpAbsorber - Return the absorbing element for the given binary
  832. /// operation, i.e. a constant C such that X op C = C and C op X = C for
  833. /// every X. For example, this returns zero for integer multiplication.
  834. /// It returns null if the operator doesn't have an absorbing element.
  835. static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
  836. /// Transparently provide more efficient getOperand methods.
  837. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
  838. /// \brief Convenience function for getting a Cast operation.
  839. ///
  840. /// \param ops The opcode for the conversion
  841. /// \param C The constant to be converted
  842. /// \param Ty The type to which the constant is converted
  843. /// \param OnlyIfReduced see \a getWithOperands() docs.
  844. static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
  845. bool OnlyIfReduced = false);
  846. // @brief Create a ZExt or BitCast cast constant expression
  847. static Constant *getZExtOrBitCast(
  848. Constant *C, ///< The constant to zext or bitcast
  849. Type *Ty ///< The type to zext or bitcast C to
  850. );
  851. // @brief Create a SExt or BitCast cast constant expression
  852. static Constant *getSExtOrBitCast(
  853. Constant *C, ///< The constant to sext or bitcast
  854. Type *Ty ///< The type to sext or bitcast C to
  855. );
  856. // @brief Create a Trunc or BitCast cast constant expression
  857. static Constant *getTruncOrBitCast(
  858. Constant *C, ///< The constant to trunc or bitcast
  859. Type *Ty ///< The type to trunc or bitcast C to
  860. );
  861. /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
  862. /// expression.
  863. static Constant *getPointerCast(
  864. Constant *C, ///< The pointer value to be casted (operand 0)
  865. Type *Ty ///< The type to which cast should be made
  866. );
  867. /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
  868. /// the address space.
  869. static Constant *getPointerBitCastOrAddrSpaceCast(
  870. Constant *C, ///< The constant to addrspacecast or bitcast
  871. Type *Ty ///< The type to bitcast or addrspacecast C to
  872. );
  873. /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
  874. static Constant *getIntegerCast(
  875. Constant *C, ///< The integer constant to be casted
  876. Type *Ty, ///< The integer type to cast to
  877. bool isSigned ///< Whether C should be treated as signed or not
  878. );
  879. /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
  880. static Constant *getFPCast(
  881. Constant *C, ///< The integer constant to be casted
  882. Type *Ty ///< The integer type to cast to
  883. );
  884. /// @brief Return true if this is a convert constant expression
  885. bool isCast() const;
  886. /// @brief Return true if this is a compare constant expression
  887. bool isCompare() const;
  888. /// @brief Return true if this is an insertvalue or extractvalue expression,
  889. /// and the getIndices() method may be used.
  890. bool hasIndices() const;
  891. /// @brief Return true if this is a getelementptr expression and all
  892. /// the index operands are compile-time known integers within the
  893. /// corresponding notional static array extents. Note that this is
  894. /// not equivalant to, a subset of, or a superset of the "inbounds"
  895. /// property.
  896. bool isGEPWithNoNotionalOverIndexing() const;
  897. /// Select constant expr
  898. ///
  899. /// \param OnlyIfReducedTy see \a getWithOperands() docs.
  900. static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
  901. Type *OnlyIfReducedTy = nullptr);
  902. /// get - Return a binary or shift operator constant expression,
  903. /// folding if possible.
  904. ///
  905. /// \param OnlyIfReducedTy see \a getWithOperands() docs.
  906. static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
  907. unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
  908. /// \brief Return an ICmp or FCmp comparison operator constant expression.
  909. ///
  910. /// \param OnlyIfReduced see \a getWithOperands() docs.
  911. static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
  912. bool OnlyIfReduced = false);
  913. /// get* - Return some common constants without having to
  914. /// specify the full Instruction::OPCODE identifier.
  915. ///
  916. static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
  917. bool OnlyIfReduced = false);
  918. static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
  919. bool OnlyIfReduced = false);
  920. /// Getelementptr form. Value* is only accepted for convenience;
  921. /// all elements must be Constants.
  922. ///
  923. /// \param OnlyIfReducedTy see \a getWithOperands() docs.
  924. static Constant *getGetElementPtr(Type *Ty, Constant *C,
  925. ArrayRef<Constant *> IdxList,
  926. bool InBounds = false,
  927. Type *OnlyIfReducedTy = nullptr) {
  928. return getGetElementPtr(
  929. Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
  930. InBounds, OnlyIfReducedTy);
  931. }
  932. static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
  933. bool InBounds = false,
  934. Type *OnlyIfReducedTy = nullptr) {
  935. // This form of the function only exists to avoid ambiguous overload
  936. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  937. // ArrayRef<Value *>.
  938. return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, OnlyIfReducedTy);
  939. }
  940. static Constant *getGetElementPtr(Type *Ty, Constant *C,
  941. ArrayRef<Value *> IdxList,
  942. bool InBounds = false,
  943. Type *OnlyIfReducedTy = nullptr);
  944. /// Create an "inbounds" getelementptr. See the documentation for the
  945. /// "inbounds" flag in LangRef.html for details.
  946. static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
  947. ArrayRef<Constant *> IdxList) {
  948. return getGetElementPtr(Ty, C, IdxList, true);
  949. }
  950. static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
  951. Constant *Idx) {
  952. // This form of the function only exists to avoid ambiguous overload
  953. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  954. // ArrayRef<Value *>.
  955. return getGetElementPtr(Ty, C, Idx, true);
  956. }
  957. static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
  958. ArrayRef<Value *> IdxList) {
  959. return getGetElementPtr(Ty, C, IdxList, true);
  960. }
  961. static Constant *getExtractElement(Constant *Vec, Constant *Idx,
  962. Type *OnlyIfReducedTy = nullptr);
  963. static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
  964. Type *OnlyIfReducedTy = nullptr);
  965. static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
  966. Type *OnlyIfReducedTy = nullptr);
  967. static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
  968. Type *OnlyIfReducedTy = nullptr);
  969. static Constant *getInsertValue(Constant *Agg, Constant *Val,
  970. ArrayRef<unsigned> Idxs,
  971. Type *OnlyIfReducedTy = nullptr);
  972. /// getOpcode - Return the opcode at the root of this constant expression
  973. unsigned getOpcode() const { return getSubclassDataFromValue(); }
  974. /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
  975. /// not an ICMP or FCMP constant expression.
  976. unsigned getPredicate() const;
  977. /// getIndices - Assert that this is an insertvalue or exactvalue
  978. /// expression and return the list of indices.
  979. ArrayRef<unsigned> getIndices() const;
  980. /// getOpcodeName - Return a string representation for an opcode.
  981. const char *getOpcodeName() const;
  982. /// getWithOperandReplaced - Return a constant expression identical to this
  983. /// one, but with the specified operand set to the specified value.
  984. Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
  985. /// getWithOperands - This returns the current constant expression with the
  986. /// operands replaced with the specified values. The specified array must
  987. /// have the same number of operands as our current one.
  988. Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
  989. return getWithOperands(Ops, getType());
  990. }
  991. /// \brief Get the current expression with the operands replaced.
  992. ///
  993. /// Return the current constant expression with the operands replaced with \c
  994. /// Ops and the type with \c Ty. The new operands must have the same number
  995. /// as the current ones.
  996. ///
  997. /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
  998. /// gets constant-folded, the type changes, or the expression is otherwise
  999. /// canonicalized. This parameter should almost always be \c false.
  1000. Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
  1001. bool OnlyIfReduced = false) const;
  1002. /// getAsInstruction - Returns an Instruction which implements the same
  1003. /// operation as this ConstantExpr. The instruction is not linked to any basic
  1004. /// block.
  1005. ///
  1006. /// A better approach to this could be to have a constructor for Instruction
  1007. /// which would take a ConstantExpr parameter, but that would have spread
  1008. /// implementation details of ConstantExpr outside of Constants.cpp, which
  1009. /// would make it harder to remove ConstantExprs altogether.
  1010. Instruction *getAsInstruction();
  1011. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  1012. static inline bool classof(const Value *V) {
  1013. return V->getValueID() == ConstantExprVal;
  1014. }
  1015. private:
  1016. // Shadow Value::setValueSubclassData with a private forwarding method so that
  1017. // subclasses cannot accidentally use it.
  1018. void setValueSubclassData(unsigned short D) {
  1019. Value::setValueSubclassData(D);
  1020. }
  1021. };
  1022. template <>
  1023. struct OperandTraits<ConstantExpr> :
  1024. public VariadicOperandTraits<ConstantExpr, 1> {
  1025. };
  1026. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
  1027. // //
  1028. ///////////////////////////////////////////////////////////////////////////////
  1029. /// UndefValue - 'undef' values are things that do not have specified contents.
  1030. /// These are used for a variety of purposes, including global variable
  1031. /// initializers and operands to instructions. 'undef' values can occur with
  1032. /// any first-class type.
  1033. ///
  1034. /// Undef values aren't exactly constants; if they have multiple uses, they
  1035. /// can appear to have different bit patterns at each use. See
  1036. /// LangRef.html#undefvalues for details.
  1037. ///
  1038. class UndefValue : public Constant {
  1039. void *operator new(size_t, unsigned) = delete;
  1040. UndefValue(const UndefValue &) = delete;
  1041. friend class Constant;
  1042. void destroyConstantImpl();
  1043. Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
  1044. protected:
  1045. explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
  1046. protected:
  1047. // allocate space for exactly zero operands
  1048. void *operator new(size_t s) {
  1049. return User::operator new(s, 0);
  1050. }
  1051. public:
  1052. /// get() - Static factory methods - Return an 'undef' object of the specified
  1053. /// type.
  1054. ///
  1055. static UndefValue *get(Type *T);
  1056. /// getSequentialElement - If this Undef has array or vector type, return a
  1057. /// undef with the right element type.
  1058. UndefValue *getSequentialElement() const;
  1059. /// getStructElement - If this undef has struct type, return a undef with the
  1060. /// right element type for the specified element.
  1061. UndefValue *getStructElement(unsigned Elt) const;
  1062. /// getElementValue - Return an undef of the right value for the specified GEP
  1063. /// index.
  1064. UndefValue *getElementValue(Constant *C) const;
  1065. /// getElementValue - Return an undef of the right value for the specified GEP
  1066. /// index.
  1067. UndefValue *getElementValue(unsigned Idx) const;
  1068. /// \brief Return the number of elements in the array, vector, or struct.
  1069. unsigned getNumElements() const;
  1070. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  1071. static bool classof(const Value *V) {
  1072. return V->getValueID() == UndefValueVal;
  1073. }
  1074. };
  1075. } // End llvm namespace
  1076. #endif