Constants.h 56 KB

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