Value.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the Value class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_VALUE_H
  14. #define LLVM_IR_VALUE_H
  15. #include "llvm-c/Core.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/IR/Use.h"
  18. #include "llvm/Support/CBindingWrapping.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/Compiler.h"
  21. namespace llvm {
  22. class APInt;
  23. class Argument;
  24. class AssemblyAnnotationWriter;
  25. class BasicBlock;
  26. class Constant;
  27. class DataLayout;
  28. class Function;
  29. class GlobalAlias;
  30. class GlobalObject;
  31. class GlobalValue;
  32. class GlobalVariable;
  33. class InlineAsm;
  34. class Instruction;
  35. class LLVMContext;
  36. class Module;
  37. class ModuleSlotTracker;
  38. class StringRef;
  39. class Twine;
  40. class Type;
  41. class ValueHandleBase;
  42. class ValueSymbolTable;
  43. class raw_ostream;
  44. template<typename ValueTy> class StringMapEntry;
  45. typedef StringMapEntry<Value*> ValueName;
  46. //===----------------------------------------------------------------------===//
  47. // Value Class
  48. // //
  49. ///////////////////////////////////////////////////////////////////////////////
  50. /// \brief LLVM Value Representation
  51. ///
  52. /// This is a very important LLVM class. It is the base class of all values
  53. /// computed by a program that may be used as operands to other values. Value is
  54. /// the super class of other important classes such as Instruction and Function.
  55. /// All Values have a Type. Type is not a subclass of Value. Some values can
  56. /// have a name and they belong to some Module. Setting the name on the Value
  57. /// automatically updates the module's symbol table.
  58. ///
  59. /// Every value has a "use list" that keeps track of which other Values are
  60. /// using this Value. A Value can also have an arbitrary number of ValueHandle
  61. /// objects that watch it and listen to RAUW and Destroy events. See
  62. /// llvm/IR/ValueHandle.h for details.
  63. class Value {
  64. Type *VTy;
  65. Use *UseList;
  66. friend class ValueAsMetadata; // Allow access to IsUsedByMD.
  67. friend class ValueHandleBase;
  68. const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
  69. unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
  70. protected:
  71. /// \brief Hold subclass data that can be dropped.
  72. ///
  73. /// This member is similar to SubclassData, however it is for holding
  74. /// information which may be used to aid optimization, but which may be
  75. /// cleared to zero without affecting conservative interpretation.
  76. unsigned char SubclassOptionalData : 7;
  77. private:
  78. /// \brief Hold arbitrary subclass data.
  79. ///
  80. /// This member is defined by this class, but is not used for anything.
  81. /// Subclasses can use it to hold whatever state they find useful. This
  82. /// field is initialized to zero by the ctor.
  83. unsigned short SubclassData;
  84. protected:
  85. /// \brief The number of operands in the subclass.
  86. ///
  87. /// This member is defined by this class, but not used for anything.
  88. /// Subclasses can use it to store their number of operands, if they have
  89. /// any.
  90. ///
  91. /// This is stored here to save space in User on 64-bit hosts. Since most
  92. /// instances of Value have operands, 32-bit hosts aren't significantly
  93. /// affected.
  94. ///
  95. /// Note, this should *NOT* be used directly by any class other than User.
  96. /// User uses this value to find the Use list.
  97. enum : unsigned { NumUserOperandsBits = 29 };
  98. unsigned NumUserOperands : NumUserOperandsBits;
  99. bool IsUsedByMD : 1;
  100. bool HasName : 1;
  101. bool HasHungOffUses : 1;
  102. private:
  103. template <typename UseT> // UseT == 'Use' or 'const Use'
  104. class use_iterator_impl
  105. : public std::iterator<std::forward_iterator_tag, UseT *> {
  106. UseT *U;
  107. explicit use_iterator_impl(UseT *u) : U(u) {}
  108. friend class Value;
  109. public:
  110. use_iterator_impl() : U() {}
  111. bool operator==(const use_iterator_impl &x) const { return U == x.U; }
  112. bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
  113. use_iterator_impl &operator++() { // Preincrement
  114. assert(U && "Cannot increment end iterator!");
  115. U = U->getNext();
  116. return *this;
  117. }
  118. use_iterator_impl operator++(int) { // Postincrement
  119. auto tmp = *this;
  120. ++*this;
  121. return tmp;
  122. }
  123. UseT &operator*() const {
  124. assert(U && "Cannot dereference end iterator!");
  125. return *U;
  126. }
  127. UseT *operator->() const { return &operator*(); }
  128. operator use_iterator_impl<const UseT>() const {
  129. return use_iterator_impl<const UseT>(U);
  130. }
  131. };
  132. template <typename UserTy> // UserTy == 'User' or 'const User'
  133. class user_iterator_impl
  134. : public std::iterator<std::forward_iterator_tag, UserTy *> {
  135. use_iterator_impl<Use> UI;
  136. explicit user_iterator_impl(Use *U) : UI(U) {}
  137. friend class Value;
  138. public:
  139. user_iterator_impl() {}
  140. bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
  141. bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
  142. /// \brief Returns true if this iterator is equal to user_end() on the value.
  143. bool atEnd() const { return *this == user_iterator_impl(); }
  144. user_iterator_impl &operator++() { // Preincrement
  145. ++UI;
  146. return *this;
  147. }
  148. user_iterator_impl operator++(int) { // Postincrement
  149. auto tmp = *this;
  150. ++*this;
  151. return tmp;
  152. }
  153. // Retrieve a pointer to the current User.
  154. UserTy *operator*() const {
  155. return UI->getUser();
  156. }
  157. UserTy *operator->() const { return operator*(); }
  158. operator user_iterator_impl<const UserTy>() const {
  159. return user_iterator_impl<const UserTy>(*UI);
  160. }
  161. Use &getUse() const { return *UI; }
  162. };
  163. void operator=(const Value &) = delete;
  164. Value(const Value &) = delete;
  165. protected:
  166. Value(Type *Ty, unsigned scid);
  167. public:
  168. virtual ~Value();
  169. /// \brief Support for debugging, callable in GDB: V->dump()
  170. void dump() const;
  171. /// \brief Implement operator<< on Value.
  172. /// @{
  173. void print(raw_ostream &O) const;
  174. void print(raw_ostream &O, ModuleSlotTracker &MST) const;
  175. /// @}
  176. /// \brief Print the name of this Value out to the specified raw_ostream.
  177. ///
  178. /// This is useful when you just want to print 'int %reg126', not the
  179. /// instruction that generated it. If you specify a Module for context, then
  180. /// even constanst get pretty-printed; for example, the type of a null
  181. /// pointer is printed symbolically.
  182. /// @{
  183. void printAsOperand(raw_ostream &O, bool PrintType = true,
  184. const Module *M = nullptr) const;
  185. void printAsOperand(raw_ostream &O, bool PrintType,
  186. ModuleSlotTracker &MST) const;
  187. /// @}
  188. /// \brief All values are typed, get the type of this value.
  189. Type *getType() const { return VTy; }
  190. /// \brief All values hold a context through their type.
  191. LLVMContext &getContext() const;
  192. // \brief All values can potentially be named.
  193. bool hasName() const { return HasName; }
  194. ValueName *getValueName() const;
  195. void setValueName(ValueName *VN);
  196. private:
  197. void destroyValueName();
  198. void setNameImpl(const Twine &Name);
  199. public:
  200. /// \brief Return a constant reference to the value's name.
  201. ///
  202. /// This is cheap and guaranteed to return the same reference as long as the
  203. /// value is not modified.
  204. StringRef getName() const;
  205. /// \brief Change the name of the value.
  206. ///
  207. /// Choose a new unique name if the provided name is taken.
  208. ///
  209. /// \param Name The new name; or "" if the value's name should be removed.
  210. void setName(const Twine &Name);
  211. /// \brief Transfer the name from V to this value.
  212. ///
  213. /// After taking V's name, sets V's name to empty.
  214. ///
  215. /// \note It is an error to call V->takeName(V).
  216. void takeName(Value *V);
  217. /// \brief Change all uses of this to point to a new Value.
  218. ///
  219. /// Go through the uses list for this definition and make each use point to
  220. /// "V" instead of "this". After this completes, 'this's use list is
  221. /// guaranteed to be empty.
  222. void replaceAllUsesWith(Value *V);
  223. /// replaceUsesOutsideBlock - Go through the uses list for this definition and
  224. /// make each use point to "V" instead of "this" when the use is outside the
  225. /// block. 'This's use list is expected to have at least one element.
  226. /// Unlike replaceAllUsesWith this function does not support basic block
  227. /// values or constant users.
  228. void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
  229. //----------------------------------------------------------------------
  230. // Methods for handling the chain of uses of this Value.
  231. //
  232. bool use_empty() const { return UseList == nullptr; }
  233. typedef use_iterator_impl<Use> use_iterator;
  234. typedef use_iterator_impl<const Use> const_use_iterator;
  235. use_iterator use_begin() { return use_iterator(UseList); }
  236. const_use_iterator use_begin() const { return const_use_iterator(UseList); }
  237. use_iterator use_end() { return use_iterator(); }
  238. const_use_iterator use_end() const { return const_use_iterator(); }
  239. iterator_range<use_iterator> uses() {
  240. return iterator_range<use_iterator>(use_begin(), use_end());
  241. }
  242. iterator_range<const_use_iterator> uses() const {
  243. return iterator_range<const_use_iterator>(use_begin(), use_end());
  244. }
  245. bool user_empty() const { return UseList == nullptr; }
  246. typedef user_iterator_impl<User> user_iterator;
  247. typedef user_iterator_impl<const User> const_user_iterator;
  248. user_iterator user_begin() { return user_iterator(UseList); }
  249. const_user_iterator user_begin() const { return const_user_iterator(UseList); }
  250. user_iterator user_end() { return user_iterator(); }
  251. const_user_iterator user_end() const { return const_user_iterator(); }
  252. User *user_back() { return *user_begin(); }
  253. const User *user_back() const { return *user_begin(); }
  254. iterator_range<user_iterator> users() {
  255. return iterator_range<user_iterator>(user_begin(), user_end());
  256. }
  257. iterator_range<const_user_iterator> users() const {
  258. return iterator_range<const_user_iterator>(user_begin(), user_end());
  259. }
  260. /// \brief Return true if there is exactly one user of this value.
  261. ///
  262. /// This is specialized because it is a common request and does not require
  263. /// traversing the whole use list.
  264. bool hasOneUse() const {
  265. const_use_iterator I = use_begin(), E = use_end();
  266. if (I == E) return false;
  267. return ++I == E;
  268. }
  269. /// \brief Return true if this Value has exactly N users.
  270. bool hasNUses(unsigned N) const;
  271. /// \brief Return true if this value has N users or more.
  272. ///
  273. /// This is logically equivalent to getNumUses() >= N.
  274. bool hasNUsesOrMore(unsigned N) const;
  275. /// \brief Check if this value is used in the specified basic block.
  276. bool isUsedInBasicBlock(const BasicBlock *BB) const;
  277. /// \brief This method computes the number of uses of this Value.
  278. ///
  279. /// This is a linear time operation. Use hasOneUse, hasNUses, or
  280. /// hasNUsesOrMore to check for specific values.
  281. unsigned getNumUses() const;
  282. /// \brief This method should only be used by the Use class.
  283. void addUse(Use &U) { U.addToList(&UseList); }
  284. /// \brief Concrete subclass of this.
  285. ///
  286. /// An enumeration for keeping track of the concrete subclass of Value that
  287. /// is actually instantiated. Values of this enumeration are kept in the
  288. /// Value classes SubclassID field. They are used for concrete type
  289. /// identification.
  290. enum ValueTy {
  291. #define HANDLE_VALUE(Name) Name##Val,
  292. #include "llvm/IR/Value.def"
  293. // Markers:
  294. #define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
  295. #include "llvm/IR/Value.def"
  296. };
  297. /// \brief Return an ID for the concrete type of this object.
  298. ///
  299. /// This is used to implement the classof checks. This should not be used
  300. /// for any other purpose, as the values may change as LLVM evolves. Also,
  301. /// note that for instructions, the Instruction's opcode is added to
  302. /// InstructionVal. So this means three things:
  303. /// # there is no value with code InstructionVal (no opcode==0).
  304. /// # there are more possible values for the value type than in ValueTy enum.
  305. /// # the InstructionVal enumerator must be the highest valued enumerator in
  306. /// the ValueTy enum.
  307. unsigned getValueID() const {
  308. return SubclassID;
  309. }
  310. /// \brief Return the raw optional flags value contained in this value.
  311. ///
  312. /// This should only be used when testing two Values for equivalence.
  313. unsigned getRawSubclassOptionalData() const {
  314. return SubclassOptionalData;
  315. }
  316. /// \brief Clear the optional flags contained in this value.
  317. void clearSubclassOptionalData() {
  318. SubclassOptionalData = 0;
  319. }
  320. /// \brief Check the optional flags for equality.
  321. bool hasSameSubclassOptionalData(const Value *V) const {
  322. return SubclassOptionalData == V->SubclassOptionalData;
  323. }
  324. /// \brief Clear any optional flags not set in the given Value.
  325. void intersectOptionalDataWith(const Value *V) {
  326. SubclassOptionalData &= V->SubclassOptionalData;
  327. }
  328. /// \brief Return true if there is a value handle associated with this value.
  329. bool hasValueHandle() const { return HasValueHandle; }
  330. /// \brief Return true if there is metadata referencing this value.
  331. bool isUsedByMetadata() const { return IsUsedByMD; }
  332. /// \brief Strip off pointer casts, all-zero GEPs, and aliases.
  333. ///
  334. /// Returns the original uncasted value. If this is called on a non-pointer
  335. /// value, it returns 'this'.
  336. Value *stripPointerCasts();
  337. const Value *stripPointerCasts() const {
  338. return const_cast<Value*>(this)->stripPointerCasts();
  339. }
  340. /// \brief Strip off pointer casts and all-zero GEPs.
  341. ///
  342. /// Returns the original uncasted value. If this is called on a non-pointer
  343. /// value, it returns 'this'.
  344. Value *stripPointerCastsNoFollowAliases();
  345. const Value *stripPointerCastsNoFollowAliases() const {
  346. return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
  347. }
  348. /// \brief Strip off pointer casts and all-constant inbounds GEPs.
  349. ///
  350. /// Returns the original pointer value. If this is called on a non-pointer
  351. /// value, it returns 'this'.
  352. Value *stripInBoundsConstantOffsets();
  353. const Value *stripInBoundsConstantOffsets() const {
  354. return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
  355. }
  356. /// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
  357. ///
  358. /// Stores the resulting constant offset stripped into the APInt provided.
  359. /// The provided APInt will be extended or truncated as needed to be the
  360. /// correct bitwidth for an offset of this pointer type.
  361. ///
  362. /// If this is called on a non-pointer value, it returns 'this'.
  363. Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
  364. APInt &Offset);
  365. const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
  366. APInt &Offset) const {
  367. return const_cast<Value *>(this)
  368. ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
  369. }
  370. /// \brief Strip off pointer casts and inbounds GEPs.
  371. ///
  372. /// Returns the original pointer value. If this is called on a non-pointer
  373. /// value, it returns 'this'.
  374. Value *stripInBoundsOffsets();
  375. const Value *stripInBoundsOffsets() const {
  376. return const_cast<Value*>(this)->stripInBoundsOffsets();
  377. }
  378. /// \brief Translate PHI node to its predecessor from the given basic block.
  379. ///
  380. /// If this value is a PHI node with CurBB as its parent, return the value in
  381. /// the PHI node corresponding to PredBB. If not, return ourself. This is
  382. /// useful if you want to know the value something has in a predecessor
  383. /// block.
  384. Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
  385. const Value *DoPHITranslation(const BasicBlock *CurBB,
  386. const BasicBlock *PredBB) const{
  387. return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
  388. }
  389. /// \brief The maximum alignment for instructions.
  390. ///
  391. /// This is the greatest alignment value supported by load, store, and alloca
  392. /// instructions, and global values.
  393. static const unsigned MaxAlignmentExponent = 29;
  394. static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
  395. /// \brief Mutate the type of this Value to be of the specified type.
  396. ///
  397. /// Note that this is an extremely dangerous operation which can create
  398. /// completely invalid IR very easily. It is strongly recommended that you
  399. /// recreate IR objects with the right types instead of mutating them in
  400. /// place.
  401. void mutateType(Type *Ty) {
  402. VTy = Ty;
  403. }
  404. /// \brief Sort the use-list.
  405. ///
  406. /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
  407. /// expected to compare two \a Use references.
  408. template <class Compare> void sortUseList(Compare Cmp);
  409. /// \brief Reverse the use-list.
  410. void reverseUseList();
  411. private:
  412. /// \brief Merge two lists together.
  413. ///
  414. /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
  415. /// "equal" items from L before items from R.
  416. ///
  417. /// \return the first element in the list.
  418. ///
  419. /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
  420. template <class Compare>
  421. static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
  422. Use *Merged;
  423. mergeUseListsImpl(L, R, &Merged, Cmp);
  424. return Merged;
  425. }
  426. /// \brief Tail-recursive helper for \a mergeUseLists().
  427. ///
  428. /// \param[out] Next the first element in the list.
  429. template <class Compare>
  430. static void mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp);
  431. protected:
  432. unsigned short getSubclassDataFromValue() const { return SubclassData; }
  433. void setValueSubclassData(unsigned short D) { SubclassData = D; }
  434. };
  435. inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
  436. V.print(OS);
  437. return OS;
  438. }
  439. void Use::set(Value *V) {
  440. if (Val) removeFromList();
  441. Val = V;
  442. if (V) V->addUse(*this);
  443. }
  444. template <class Compare> void Value::sortUseList(Compare Cmp) {
  445. if (!UseList || !UseList->Next)
  446. // No need to sort 0 or 1 uses.
  447. return;
  448. // Note: this function completely ignores Prev pointers until the end when
  449. // they're fixed en masse.
  450. // Create a binomial vector of sorted lists, visiting uses one at a time and
  451. // merging lists as necessary.
  452. const unsigned MaxSlots = 32;
  453. Use *Slots[MaxSlots];
  454. // Collect the first use, turning it into a single-item list.
  455. Use *Next = UseList->Next;
  456. UseList->Next = nullptr;
  457. unsigned NumSlots = 1;
  458. Slots[0] = UseList;
  459. // Collect all but the last use.
  460. while (Next->Next) {
  461. Use *Current = Next;
  462. Next = Current->Next;
  463. // Turn Current into a single-item list.
  464. Current->Next = nullptr;
  465. // Save Current in the first available slot, merging on collisions.
  466. unsigned I;
  467. for (I = 0; I < NumSlots; ++I) {
  468. if (!Slots[I])
  469. break;
  470. // Merge two lists, doubling the size of Current and emptying slot I.
  471. //
  472. // Since the uses in Slots[I] originally preceded those in Current, send
  473. // Slots[I] in as the left parameter to maintain a stable sort.
  474. Current = mergeUseLists(Slots[I], Current, Cmp);
  475. Slots[I] = nullptr;
  476. }
  477. // Check if this is a new slot.
  478. if (I == NumSlots) {
  479. ++NumSlots;
  480. assert(NumSlots <= MaxSlots && "Use list bigger than 2^32");
  481. }
  482. // Found an open slot.
  483. Slots[I] = Current;
  484. }
  485. // Merge all the lists together.
  486. assert(Next && "Expected one more Use");
  487. assert(!Next->Next && "Expected only one Use");
  488. UseList = Next;
  489. for (unsigned I = 0; I < NumSlots; ++I)
  490. if (Slots[I])
  491. // Since the uses in Slots[I] originally preceded those in UseList, send
  492. // Slots[I] in as the left parameter to maintain a stable sort.
  493. UseList = mergeUseLists(Slots[I], UseList, Cmp);
  494. // Fix the Prev pointers.
  495. for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
  496. I->setPrev(Prev);
  497. Prev = &I->Next;
  498. }
  499. }
  500. template <class Compare>
  501. void Value::mergeUseListsImpl(Use *L, Use *R, Use **Next, Compare Cmp) {
  502. if (!L) {
  503. *Next = R;
  504. return;
  505. }
  506. if (!R) {
  507. *Next = L;
  508. return;
  509. }
  510. if (Cmp(*R, *L)) {
  511. *Next = R;
  512. mergeUseListsImpl(L, R->Next, &R->Next, Cmp);
  513. return;
  514. }
  515. *Next = L;
  516. mergeUseListsImpl(L->Next, R, &L->Next, Cmp);
  517. }
  518. // isa - Provide some specializations of isa so that we don't have to include
  519. // the subtype header files to test to see if the value is a subclass...
  520. //
  521. template <> struct isa_impl<Constant, Value> {
  522. static inline bool doit(const Value &Val) {
  523. return Val.getValueID() >= Value::ConstantFirstVal &&
  524. Val.getValueID() <= Value::ConstantLastVal;
  525. }
  526. };
  527. template <> struct isa_impl<Argument, Value> {
  528. static inline bool doit (const Value &Val) {
  529. return Val.getValueID() == Value::ArgumentVal;
  530. }
  531. };
  532. template <> struct isa_impl<InlineAsm, Value> {
  533. static inline bool doit(const Value &Val) {
  534. return Val.getValueID() == Value::InlineAsmVal;
  535. }
  536. };
  537. template <> struct isa_impl<Instruction, Value> {
  538. static inline bool doit(const Value &Val) {
  539. return Val.getValueID() >= Value::InstructionVal;
  540. }
  541. };
  542. template <> struct isa_impl<BasicBlock, Value> {
  543. static inline bool doit(const Value &Val) {
  544. return Val.getValueID() == Value::BasicBlockVal;
  545. }
  546. };
  547. template <> struct isa_impl<Function, Value> {
  548. static inline bool doit(const Value &Val) {
  549. return Val.getValueID() == Value::FunctionVal;
  550. }
  551. };
  552. template <> struct isa_impl<GlobalVariable, Value> {
  553. static inline bool doit(const Value &Val) {
  554. return Val.getValueID() == Value::GlobalVariableVal;
  555. }
  556. };
  557. template <> struct isa_impl<GlobalAlias, Value> {
  558. static inline bool doit(const Value &Val) {
  559. return Val.getValueID() == Value::GlobalAliasVal;
  560. }
  561. };
  562. template <> struct isa_impl<GlobalValue, Value> {
  563. static inline bool doit(const Value &Val) {
  564. return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val);
  565. }
  566. };
  567. template <> struct isa_impl<GlobalObject, Value> {
  568. static inline bool doit(const Value &Val) {
  569. return isa<GlobalVariable>(Val) || isa<Function>(Val);
  570. }
  571. };
  572. // Value* is only 4-byte aligned.
  573. template<>
  574. class PointerLikeTypeTraits<Value*> {
  575. typedef Value* PT;
  576. public:
  577. static inline void *getAsVoidPointer(PT P) { return P; }
  578. static inline PT getFromVoidPointer(void *P) {
  579. return static_cast<PT>(P);
  580. }
  581. enum { NumLowBitsAvailable = 2 };
  582. };
  583. // Create wrappers for C Binding types (see CBindingWrapping.h).
  584. DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
  585. /* Specialized opaque value conversions.
  586. */
  587. inline Value **unwrap(LLVMValueRef *Vals) {
  588. return reinterpret_cast<Value**>(Vals);
  589. }
  590. template<typename T>
  591. inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
  592. #ifdef DEBUG
  593. for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
  594. cast<T>(*I);
  595. #endif
  596. (void)Length;
  597. return reinterpret_cast<T**>(Vals);
  598. }
  599. inline LLVMValueRef *wrap(const Value **Vals) {
  600. return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
  601. }
  602. } // End llvm namespace
  603. #endif