Metadata.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/Metadata.h - Metadata 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 metadata subclasses.
  16. /// They represent the different flavors of metadata that live in LLVM.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_METADATA_H
  20. #define LLVM_IR_METADATA_H
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/DenseMapInfo.h"
  24. #include "llvm/ADT/None.h"
  25. #include "llvm/ADT/PointerUnion.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. #include "llvm/ADT/StringMap.h"
  29. #include "llvm/ADT/StringRef.h"
  30. #include "llvm/ADT/ilist_node.h"
  31. #include "llvm/ADT/iterator_range.h"
  32. #include "llvm/IR/Constant.h"
  33. #include "llvm/IR/LLVMContext.h"
  34. #include "llvm/IR/Value.h"
  35. #include "llvm/Support/CBindingWrapping.h"
  36. #include "llvm/Support/Casting.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include <cassert>
  39. #include <cstddef>
  40. #include <cstdint>
  41. #include <iterator>
  42. #include <memory>
  43. #include <string>
  44. #include <type_traits>
  45. #include <utility>
  46. namespace llvm {
  47. class Module;
  48. class ModuleSlotTracker;
  49. class raw_ostream;
  50. class Type;
  51. enum LLVMConstants : uint32_t {
  52. DEBUG_METADATA_VERSION = 3 // Current debug info version number.
  53. };
  54. /// Root of the metadata hierarchy.
  55. ///
  56. /// This is a root class for typeless data in the IR.
  57. class Metadata {
  58. friend class ReplaceableMetadataImpl;
  59. /// RTTI.
  60. const unsigned char SubclassID;
  61. protected:
  62. /// Active type of storage.
  63. enum StorageType { Uniqued, Distinct, Temporary };
  64. /// Storage flag for non-uniqued, otherwise unowned, metadata.
  65. unsigned char Storage : 7;
  66. // TODO: expose remaining bits to subclasses.
  67. unsigned char ImplicitCode : 1;
  68. unsigned short SubclassData16 = 0;
  69. unsigned SubclassData32 = 0;
  70. public:
  71. enum MetadataKind {
  72. #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
  73. #include "llvm/IR/Metadata.def"
  74. };
  75. protected:
  76. Metadata(unsigned ID, StorageType Storage)
  77. : SubclassID(ID), Storage(Storage), ImplicitCode(false) {
  78. static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
  79. }
  80. ~Metadata() = default;
  81. /// Default handling of a changed operand, which asserts.
  82. ///
  83. /// If subclasses pass themselves in as owners to a tracking node reference,
  84. /// they must provide an implementation of this method.
  85. void handleChangedOperand(void *, Metadata *) {
  86. llvm_unreachable("Unimplemented in Metadata subclass");
  87. }
  88. public:
  89. unsigned getMetadataID() const { return SubclassID; }
  90. /// User-friendly dump.
  91. ///
  92. /// If \c M is provided, metadata nodes will be numbered canonically;
  93. /// otherwise, pointer addresses are substituted.
  94. ///
  95. /// Note: this uses an explicit overload instead of default arguments so that
  96. /// the nullptr version is easy to call from a debugger.
  97. ///
  98. /// @{
  99. void dump() const;
  100. void dump(const Module *M) const;
  101. /// @}
  102. /// Print.
  103. ///
  104. /// Prints definition of \c this.
  105. ///
  106. /// If \c M is provided, metadata nodes will be numbered canonically;
  107. /// otherwise, pointer addresses are substituted.
  108. /// @{
  109. void print(raw_ostream &OS, const Module *M = nullptr,
  110. bool IsForDebug = false) const;
  111. void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr,
  112. bool IsForDebug = false) const;
  113. /// @}
  114. /// Print as operand.
  115. ///
  116. /// Prints reference of \c this.
  117. ///
  118. /// If \c M is provided, metadata nodes will be numbered canonically;
  119. /// otherwise, pointer addresses are substituted.
  120. /// @{
  121. void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
  122. void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
  123. const Module *M = nullptr) const;
  124. /// @}
  125. };
  126. // Create wrappers for C Binding types (see CBindingWrapping.h).
  127. DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
  128. // Specialized opaque metadata conversions.
  129. inline Metadata **unwrap(LLVMMetadataRef *MDs) {
  130. return reinterpret_cast<Metadata**>(MDs);
  131. }
  132. #define HANDLE_METADATA(CLASS) class CLASS;
  133. #include "llvm/IR/Metadata.def"
  134. // Provide specializations of isa so that we don't need definitions of
  135. // subclasses to see if the metadata is a subclass.
  136. #define HANDLE_METADATA_LEAF(CLASS) \
  137. template <> struct isa_impl<CLASS, Metadata> { \
  138. static inline bool doit(const Metadata &MD) { \
  139. return MD.getMetadataID() == Metadata::CLASS##Kind; \
  140. } \
  141. };
  142. #include "llvm/IR/Metadata.def"
  143. inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
  144. MD.print(OS);
  145. return OS;
  146. }
  147. /// Metadata wrapper in the Value hierarchy.
  148. ///
  149. /// A member of the \a Value hierarchy to represent a reference to metadata.
  150. /// This allows, e.g., instrinsics to have metadata as operands.
  151. ///
  152. /// Notably, this is the only thing in either hierarchy that is allowed to
  153. /// reference \a LocalAsMetadata.
  154. class MetadataAsValue : public Value {
  155. friend class ReplaceableMetadataImpl;
  156. friend class LLVMContextImpl;
  157. Metadata *MD;
  158. MetadataAsValue(Type *Ty, Metadata *MD);
  159. /// Drop use of metadata (during teardown).
  160. void dropUse() { MD = nullptr; }
  161. public:
  162. ~MetadataAsValue();
  163. static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
  164. static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
  165. Metadata *getMetadata() const { return MD; }
  166. static bool classof(const Value *V) {
  167. return V->getValueID() == MetadataAsValueVal;
  168. }
  169. private:
  170. void handleChangedMetadata(Metadata *MD);
  171. void track();
  172. void untrack();
  173. };
  174. /// API for tracking metadata references through RAUW and deletion.
  175. ///
  176. /// Shared API for updating \a Metadata pointers in subclasses that support
  177. /// RAUW.
  178. ///
  179. /// This API is not meant to be used directly. See \a TrackingMDRef for a
  180. /// user-friendly tracking reference.
  181. class MetadataTracking {
  182. public:
  183. /// Track the reference to metadata.
  184. ///
  185. /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
  186. /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
  187. /// deleted, \c MD will be set to \c nullptr.
  188. ///
  189. /// If tracking isn't supported, \c *MD will not change.
  190. ///
  191. /// \return true iff tracking is supported by \c MD.
  192. static bool track(Metadata *&MD) {
  193. return track(&MD, *MD, static_cast<Metadata *>(nullptr));
  194. }
  195. /// Track the reference to metadata for \a Metadata.
  196. ///
  197. /// As \a track(Metadata*&), but with support for calling back to \c Owner to
  198. /// tell it that its operand changed. This could trigger \c Owner being
  199. /// re-uniqued.
  200. static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
  201. return track(Ref, MD, &Owner);
  202. }
  203. /// Track the reference to metadata for \a MetadataAsValue.
  204. ///
  205. /// As \a track(Metadata*&), but with support for calling back to \c Owner to
  206. /// tell it that its operand changed. This could trigger \c Owner being
  207. /// re-uniqued.
  208. static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
  209. return track(Ref, MD, &Owner);
  210. }
  211. /// Stop tracking a reference to metadata.
  212. ///
  213. /// Stops \c *MD from tracking \c MD.
  214. static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
  215. static void untrack(void *Ref, Metadata &MD);
  216. /// Move tracking from one reference to another.
  217. ///
  218. /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
  219. /// except that ownership callbacks are maintained.
  220. ///
  221. /// Note: it is an error if \c *MD does not equal \c New.
  222. ///
  223. /// \return true iff tracking is supported by \c MD.
  224. static bool retrack(Metadata *&MD, Metadata *&New) {
  225. return retrack(&MD, *MD, &New);
  226. }
  227. static bool retrack(void *Ref, Metadata &MD, void *New);
  228. /// Check whether metadata is replaceable.
  229. static bool isReplaceable(const Metadata &MD);
  230. using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *>;
  231. private:
  232. /// Track a reference to metadata for an owner.
  233. ///
  234. /// Generalized version of tracking.
  235. static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
  236. };
  237. /// Shared implementation of use-lists for replaceable metadata.
  238. ///
  239. /// Most metadata cannot be RAUW'ed. This is a shared implementation of
  240. /// use-lists and associated API for the two that support it (\a ValueAsMetadata
  241. /// and \a TempMDNode).
  242. class ReplaceableMetadataImpl {
  243. friend class MetadataTracking;
  244. public:
  245. using OwnerTy = MetadataTracking::OwnerTy;
  246. private:
  247. LLVMContext &Context;
  248. uint64_t NextIndex = 0;
  249. SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
  250. public:
  251. ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
  252. ~ReplaceableMetadataImpl() {
  253. assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
  254. }
  255. LLVMContext &getContext() const { return Context; }
  256. /// Replace all uses of this with MD.
  257. ///
  258. /// Replace all uses of this with \c MD, which is allowed to be null.
  259. void replaceAllUsesWith(Metadata *MD);
  260. /// Resolve all uses of this.
  261. ///
  262. /// Resolve all uses of this, turning off RAUW permanently. If \c
  263. /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
  264. /// is resolved.
  265. void resolveAllUses(bool ResolveUsers = true);
  266. private:
  267. void addRef(void *Ref, OwnerTy Owner);
  268. void dropRef(void *Ref);
  269. void moveRef(void *Ref, void *New, const Metadata &MD);
  270. /// Lazily construct RAUW support on MD.
  271. ///
  272. /// If this is an unresolved MDNode, RAUW support will be created on-demand.
  273. /// ValueAsMetadata always has RAUW support.
  274. static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
  275. /// Get RAUW support on MD, if it exists.
  276. static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
  277. /// Check whether this node will support RAUW.
  278. ///
  279. /// Returns \c true unless getOrCreate() would return null.
  280. static bool isReplaceable(const Metadata &MD);
  281. };
  282. /// Value wrapper in the Metadata hierarchy.
  283. ///
  284. /// This is a custom value handle that allows other metadata to refer to
  285. /// classes in the Value hierarchy.
  286. ///
  287. /// Because of full uniquing support, each value is only wrapped by a single \a
  288. /// ValueAsMetadata object, so the lookup maps are far more efficient than
  289. /// those using ValueHandleBase.
  290. class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
  291. friend class ReplaceableMetadataImpl;
  292. friend class LLVMContextImpl;
  293. Value *V;
  294. /// Drop users without RAUW (during teardown).
  295. void dropUsers() {
  296. ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
  297. }
  298. protected:
  299. ValueAsMetadata(unsigned ID, Value *V)
  300. : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
  301. assert(V && "Expected valid value");
  302. }
  303. ~ValueAsMetadata() = default;
  304. public:
  305. static ValueAsMetadata *get(Value *V);
  306. static ConstantAsMetadata *getConstant(Value *C) {
  307. return cast<ConstantAsMetadata>(get(C));
  308. }
  309. static LocalAsMetadata *getLocal(Value *Local) {
  310. return cast<LocalAsMetadata>(get(Local));
  311. }
  312. static ValueAsMetadata *getIfExists(Value *V);
  313. static ConstantAsMetadata *getConstantIfExists(Value *C) {
  314. return cast_or_null<ConstantAsMetadata>(getIfExists(C));
  315. }
  316. static LocalAsMetadata *getLocalIfExists(Value *Local) {
  317. return cast_or_null<LocalAsMetadata>(getIfExists(Local));
  318. }
  319. Value *getValue() const { return V; }
  320. Type *getType() const { return V->getType(); }
  321. LLVMContext &getContext() const { return V->getContext(); }
  322. static void handleDeletion(Value *V);
  323. static void handleRAUW(Value *From, Value *To);
  324. protected:
  325. /// Handle collisions after \a Value::replaceAllUsesWith().
  326. ///
  327. /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
  328. /// \a Value gets RAUW'ed and the target already exists, this is used to
  329. /// merge the two metadata nodes.
  330. void replaceAllUsesWith(Metadata *MD) {
  331. ReplaceableMetadataImpl::replaceAllUsesWith(MD);
  332. }
  333. public:
  334. static bool classof(const Metadata *MD) {
  335. return MD->getMetadataID() == LocalAsMetadataKind ||
  336. MD->getMetadataID() == ConstantAsMetadataKind;
  337. }
  338. };
  339. class ConstantAsMetadata : public ValueAsMetadata {
  340. friend class ValueAsMetadata;
  341. ConstantAsMetadata(Constant *C)
  342. : ValueAsMetadata(ConstantAsMetadataKind, C) {}
  343. public:
  344. static ConstantAsMetadata *get(Constant *C) {
  345. return ValueAsMetadata::getConstant(C);
  346. }
  347. static ConstantAsMetadata *getIfExists(Constant *C) {
  348. return ValueAsMetadata::getConstantIfExists(C);
  349. }
  350. Constant *getValue() const {
  351. return cast<Constant>(ValueAsMetadata::getValue());
  352. }
  353. static bool classof(const Metadata *MD) {
  354. return MD->getMetadataID() == ConstantAsMetadataKind;
  355. }
  356. };
  357. class LocalAsMetadata : public ValueAsMetadata {
  358. friend class ValueAsMetadata;
  359. LocalAsMetadata(Value *Local)
  360. : ValueAsMetadata(LocalAsMetadataKind, Local) {
  361. assert(!isa<Constant>(Local) && "Expected local value");
  362. }
  363. public:
  364. static LocalAsMetadata *get(Value *Local) {
  365. return ValueAsMetadata::getLocal(Local);
  366. }
  367. static LocalAsMetadata *getIfExists(Value *Local) {
  368. return ValueAsMetadata::getLocalIfExists(Local);
  369. }
  370. static bool classof(const Metadata *MD) {
  371. return MD->getMetadataID() == LocalAsMetadataKind;
  372. }
  373. };
  374. /// Transitional API for extracting constants from Metadata.
  375. ///
  376. /// This namespace contains transitional functions for metadata that points to
  377. /// \a Constants.
  378. ///
  379. /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
  380. /// operands could refer to any \a Value. There's was a lot of code like this:
  381. ///
  382. /// \code
  383. /// MDNode *N = ...;
  384. /// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
  385. /// \endcode
  386. ///
  387. /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
  388. /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
  389. /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
  390. /// cast in the \a Value hierarchy. Besides creating boiler-plate, this
  391. /// requires subtle control flow changes.
  392. ///
  393. /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
  394. /// so that metadata can refer to numbers without traversing a bridge to the \a
  395. /// Value hierarchy. In this final state, the code above would look like this:
  396. ///
  397. /// \code
  398. /// MDNode *N = ...;
  399. /// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
  400. /// \endcode
  401. ///
  402. /// The API in this namespace supports the transition. \a MDInt doesn't exist
  403. /// yet, and even once it does, changing each metadata schema to use it is its
  404. /// own mini-project. In the meantime this API prevents us from introducing
  405. /// complex and bug-prone control flow that will disappear in the end. In
  406. /// particular, the above code looks like this:
  407. ///
  408. /// \code
  409. /// MDNode *N = ...;
  410. /// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
  411. /// \endcode
  412. ///
  413. /// The full set of provided functions includes:
  414. ///
  415. /// mdconst::hasa <=> isa
  416. /// mdconst::extract <=> cast
  417. /// mdconst::extract_or_null <=> cast_or_null
  418. /// mdconst::dyn_extract <=> dyn_cast
  419. /// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
  420. ///
  421. /// The target of the cast must be a subclass of \a Constant.
  422. namespace mdconst {
  423. namespace detail {
  424. template <class T> T &make();
  425. template <class T, class Result> struct HasDereference {
  426. using Yes = char[1];
  427. using No = char[2];
  428. template <size_t N> struct SFINAE {};
  429. template <class U, class V>
  430. static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
  431. template <class U, class V> static No &hasDereference(...);
  432. static const bool value =
  433. sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
  434. };
  435. template <class V, class M> struct IsValidPointer {
  436. static const bool value = std::is_base_of<Constant, V>::value &&
  437. HasDereference<M, const Metadata &>::value;
  438. };
  439. template <class V, class M> struct IsValidReference {
  440. static const bool value = std::is_base_of<Constant, V>::value &&
  441. std::is_convertible<M, const Metadata &>::value;
  442. };
  443. } // end namespace detail
  444. /// Check whether Metadata has a Value.
  445. ///
  446. /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
  447. /// type \c X.
  448. template <class X, class Y>
  449. inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, bool>
  450. hasa(Y &&MD) {
  451. assert(MD && "Null pointer sent into hasa");
  452. if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
  453. return isa<X>(V->getValue());
  454. return false;
  455. }
  456. template <class X, class Y>
  457. inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, bool>
  458. hasa(Y &MD) {
  459. return hasa(&MD);
  460. }
  461. /// Extract a Value from Metadata.
  462. ///
  463. /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
  464. template <class X, class Y>
  465. inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
  466. extract(Y &&MD) {
  467. return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
  468. }
  469. template <class X, class Y>
  470. inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, X *>
  471. extract(Y &MD) {
  472. return extract(&MD);
  473. }
  474. /// Extract a Value from Metadata, allowing null.
  475. ///
  476. /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
  477. /// from \c MD, allowing \c MD to be null.
  478. template <class X, class Y>
  479. inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
  480. extract_or_null(Y &&MD) {
  481. if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
  482. return cast<X>(V->getValue());
  483. return nullptr;
  484. }
  485. /// Extract a Value from Metadata, if any.
  486. ///
  487. /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
  488. /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
  489. /// Value it does contain is of the wrong subclass.
  490. template <class X, class Y>
  491. inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
  492. dyn_extract(Y &&MD) {
  493. if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
  494. return dyn_cast<X>(V->getValue());
  495. return nullptr;
  496. }
  497. /// Extract a Value from Metadata, if any, allowing null.
  498. ///
  499. /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
  500. /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
  501. /// Value it does contain is of the wrong subclass, allowing \c MD to be null.
  502. template <class X, class Y>
  503. inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
  504. dyn_extract_or_null(Y &&MD) {
  505. if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
  506. return dyn_cast<X>(V->getValue());
  507. return nullptr;
  508. }
  509. } // end namespace mdconst
  510. //===----------------------------------------------------------------------===//
  511. /// A single uniqued string.
  512. ///
  513. /// These are used to efficiently contain a byte sequence for metadata.
  514. /// MDString is always unnamed.
  515. class MDString : public Metadata {
  516. friend class StringMapEntryStorage<MDString>;
  517. StringMapEntry<MDString> *Entry = nullptr;
  518. MDString() : Metadata(MDStringKind, Uniqued) {}
  519. public:
  520. MDString(const MDString &) = delete;
  521. MDString &operator=(MDString &&) = delete;
  522. MDString &operator=(const MDString &) = delete;
  523. static MDString *get(LLVMContext &Context, StringRef Str);
  524. static MDString *get(LLVMContext &Context, const char *Str) {
  525. return get(Context, Str ? StringRef(Str) : StringRef());
  526. }
  527. StringRef getString() const;
  528. unsigned getLength() const { return (unsigned)getString().size(); }
  529. using iterator = StringRef::iterator;
  530. /// Pointer to the first byte of the string.
  531. iterator begin() const { return getString().begin(); }
  532. /// Pointer to one byte past the end of the string.
  533. iterator end() const { return getString().end(); }
  534. const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
  535. const unsigned char *bytes_end() const { return getString().bytes_end(); }
  536. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  537. static bool classof(const Metadata *MD) {
  538. return MD->getMetadataID() == MDStringKind;
  539. }
  540. };
  541. /// A collection of metadata nodes that might be associated with a
  542. /// memory access used by the alias-analysis infrastructure.
  543. struct AAMDNodes {
  544. explicit AAMDNodes() = default;
  545. explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
  546. : TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N) {}
  547. bool operator==(const AAMDNodes &A) const {
  548. return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
  549. NoAlias == A.NoAlias;
  550. }
  551. bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
  552. explicit operator bool() const {
  553. return TBAA || TBAAStruct || Scope || NoAlias;
  554. }
  555. /// The tag for type-based alias analysis.
  556. MDNode *TBAA = nullptr;
  557. /// The tag for type-based alias analysis (tbaa struct).
  558. MDNode *TBAAStruct = nullptr;
  559. /// The tag for alias scope specification (used with noalias).
  560. MDNode *Scope = nullptr;
  561. /// The tag specifying the noalias scope.
  562. MDNode *NoAlias = nullptr;
  563. // Shift tbaa Metadata node to start off bytes later
  564. static MDNode *ShiftTBAA(MDNode *M, size_t off);
  565. // Shift tbaa.struct Metadata node to start off bytes later
  566. static MDNode *ShiftTBAAStruct(MDNode *M, size_t off);
  567. /// Given two sets of AAMDNodes that apply to the same pointer,
  568. /// give the best AAMDNodes that are compatible with both (i.e. a set of
  569. /// nodes whose allowable aliasing conclusions are a subset of those
  570. /// allowable by both of the inputs). However, for efficiency
  571. /// reasons, do not create any new MDNodes.
  572. AAMDNodes intersect(const AAMDNodes &Other) {
  573. AAMDNodes Result;
  574. Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
  575. Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
  576. Result.Scope = Other.Scope == Scope ? Scope : nullptr;
  577. Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
  578. return Result;
  579. }
  580. /// Create a new AAMDNode that describes this AAMDNode after applying a
  581. /// constant offset to the start of the pointer
  582. AAMDNodes shift(size_t Offset) {
  583. AAMDNodes Result;
  584. Result.TBAA = TBAA ? ShiftTBAA(TBAA, Offset) : nullptr;
  585. Result.TBAAStruct =
  586. TBAAStruct ? ShiftTBAAStruct(TBAAStruct, Offset) : nullptr;
  587. Result.Scope = Scope;
  588. Result.NoAlias = NoAlias;
  589. return Result;
  590. }
  591. };
  592. // Specialize DenseMapInfo for AAMDNodes.
  593. template<>
  594. struct DenseMapInfo<AAMDNodes> {
  595. static inline AAMDNodes getEmptyKey() {
  596. return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
  597. nullptr, nullptr, nullptr);
  598. }
  599. static inline AAMDNodes getTombstoneKey() {
  600. return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
  601. nullptr, nullptr, nullptr);
  602. }
  603. static unsigned getHashValue(const AAMDNodes &Val) {
  604. return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
  605. DenseMapInfo<MDNode *>::getHashValue(Val.TBAAStruct) ^
  606. DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
  607. DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
  608. }
  609. static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
  610. return LHS == RHS;
  611. }
  612. };
  613. /// Tracking metadata reference owned by Metadata.
  614. ///
  615. /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
  616. /// of \a Metadata, which has the option of registering itself for callbacks to
  617. /// re-unique itself.
  618. ///
  619. /// In particular, this is used by \a MDNode.
  620. class MDOperand {
  621. Metadata *MD = nullptr;
  622. public:
  623. MDOperand() = default;
  624. MDOperand(MDOperand &&) = delete;
  625. MDOperand(const MDOperand &) = delete;
  626. MDOperand &operator=(MDOperand &&) = delete;
  627. MDOperand &operator=(const MDOperand &) = delete;
  628. ~MDOperand() { untrack(); }
  629. Metadata *get() const { return MD; }
  630. operator Metadata *() const { return get(); }
  631. Metadata *operator->() const { return get(); }
  632. Metadata &operator*() const { return *get(); }
  633. void reset() {
  634. untrack();
  635. MD = nullptr;
  636. }
  637. void reset(Metadata *MD, Metadata *Owner) {
  638. untrack();
  639. this->MD = MD;
  640. track(Owner);
  641. }
  642. private:
  643. void track(Metadata *Owner) {
  644. if (MD) {
  645. if (Owner)
  646. MetadataTracking::track(this, *MD, *Owner);
  647. else
  648. MetadataTracking::track(MD);
  649. }
  650. }
  651. void untrack() {
  652. assert(static_cast<void *>(this) == &MD && "Expected same address");
  653. if (MD)
  654. MetadataTracking::untrack(MD);
  655. }
  656. };
  657. template <> struct simplify_type<MDOperand> {
  658. using SimpleType = Metadata *;
  659. static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
  660. };
  661. template <> struct simplify_type<const MDOperand> {
  662. using SimpleType = Metadata *;
  663. static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
  664. };
  665. /// Pointer to the context, with optional RAUW support.
  666. ///
  667. /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
  668. /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
  669. class ContextAndReplaceableUses {
  670. PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
  671. public:
  672. ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
  673. ContextAndReplaceableUses(
  674. std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
  675. : Ptr(ReplaceableUses.release()) {
  676. assert(getReplaceableUses() && "Expected non-null replaceable uses");
  677. }
  678. ContextAndReplaceableUses() = delete;
  679. ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete;
  680. ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete;
  681. ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete;
  682. ContextAndReplaceableUses &
  683. operator=(const ContextAndReplaceableUses &) = delete;
  684. ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
  685. operator LLVMContext &() { return getContext(); }
  686. /// Whether this contains RAUW support.
  687. bool hasReplaceableUses() const {
  688. return Ptr.is<ReplaceableMetadataImpl *>();
  689. }
  690. LLVMContext &getContext() const {
  691. if (hasReplaceableUses())
  692. return getReplaceableUses()->getContext();
  693. return *Ptr.get<LLVMContext *>();
  694. }
  695. ReplaceableMetadataImpl *getReplaceableUses() const {
  696. if (hasReplaceableUses())
  697. return Ptr.get<ReplaceableMetadataImpl *>();
  698. return nullptr;
  699. }
  700. /// Ensure that this has RAUW support, and then return it.
  701. ReplaceableMetadataImpl *getOrCreateReplaceableUses() {
  702. if (!hasReplaceableUses())
  703. makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext()));
  704. return getReplaceableUses();
  705. }
  706. /// Assign RAUW support to this.
  707. ///
  708. /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
  709. /// not be null).
  710. void
  711. makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
  712. assert(ReplaceableUses && "Expected non-null replaceable uses");
  713. assert(&ReplaceableUses->getContext() == &getContext() &&
  714. "Expected same context");
  715. delete getReplaceableUses();
  716. Ptr = ReplaceableUses.release();
  717. }
  718. /// Drop RAUW support.
  719. ///
  720. /// Cede ownership of RAUW support, returning it.
  721. std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
  722. assert(hasReplaceableUses() && "Expected to own replaceable uses");
  723. std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
  724. getReplaceableUses());
  725. Ptr = &ReplaceableUses->getContext();
  726. return ReplaceableUses;
  727. }
  728. };
  729. struct TempMDNodeDeleter {
  730. inline void operator()(MDNode *Node) const;
  731. };
  732. #define HANDLE_MDNODE_LEAF(CLASS) \
  733. using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
  734. #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
  735. #include "llvm/IR/Metadata.def"
  736. /// Metadata node.
  737. ///
  738. /// Metadata nodes can be uniqued, like constants, or distinct. Temporary
  739. /// metadata nodes (with full support for RAUW) can be used to delay uniquing
  740. /// until forward references are known. The basic metadata node is an \a
  741. /// MDTuple.
  742. ///
  743. /// There is limited support for RAUW at construction time. At construction
  744. /// time, if any operand is a temporary node (or an unresolved uniqued node,
  745. /// which indicates a transitive temporary operand), the node itself will be
  746. /// unresolved. As soon as all operands become resolved, it will drop RAUW
  747. /// support permanently.
  748. ///
  749. /// If an unresolved node is part of a cycle, \a resolveCycles() needs
  750. /// to be called on some member of the cycle once all temporary nodes have been
  751. /// replaced.
  752. class MDNode : public Metadata {
  753. friend class ReplaceableMetadataImpl;
  754. friend class LLVMContextImpl;
  755. unsigned NumOperands;
  756. unsigned NumUnresolved;
  757. ContextAndReplaceableUses Context;
  758. protected:
  759. MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
  760. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None);
  761. ~MDNode() = default;
  762. void *operator new(size_t Size, unsigned NumOps);
  763. void operator delete(void *Mem);
  764. /// Required by std, but never called.
  765. void operator delete(void *, unsigned) {
  766. llvm_unreachable("Constructor throws?");
  767. }
  768. /// Required by std, but never called.
  769. void operator delete(void *, unsigned, bool) {
  770. llvm_unreachable("Constructor throws?");
  771. }
  772. void dropAllReferences();
  773. MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
  774. MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
  775. using mutable_op_range = iterator_range<MDOperand *>;
  776. mutable_op_range mutable_operands() {
  777. return mutable_op_range(mutable_begin(), mutable_end());
  778. }
  779. public:
  780. MDNode(const MDNode &) = delete;
  781. void operator=(const MDNode &) = delete;
  782. void *operator new(size_t) = delete;
  783. static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
  784. static inline MDTuple *getIfExists(LLVMContext &Context,
  785. ArrayRef<Metadata *> MDs);
  786. static inline MDTuple *getDistinct(LLVMContext &Context,
  787. ArrayRef<Metadata *> MDs);
  788. static inline TempMDTuple getTemporary(LLVMContext &Context,
  789. ArrayRef<Metadata *> MDs);
  790. /// Create a (temporary) clone of this.
  791. TempMDNode clone() const;
  792. /// Deallocate a node created by getTemporary.
  793. ///
  794. /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
  795. /// references will be reset.
  796. static void deleteTemporary(MDNode *N);
  797. LLVMContext &getContext() const { return Context.getContext(); }
  798. /// Replace a specific operand.
  799. void replaceOperandWith(unsigned I, Metadata *New);
  800. /// Check if node is fully resolved.
  801. ///
  802. /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
  803. /// this always returns \c true.
  804. ///
  805. /// If \a isUniqued(), returns \c true if this has already dropped RAUW
  806. /// support (because all operands are resolved).
  807. ///
  808. /// As forward declarations are resolved, their containers should get
  809. /// resolved automatically. However, if this (or one of its operands) is
  810. /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
  811. bool isResolved() const { return !isTemporary() && !NumUnresolved; }
  812. bool isUniqued() const { return Storage == Uniqued; }
  813. bool isDistinct() const { return Storage == Distinct; }
  814. bool isTemporary() const { return Storage == Temporary; }
  815. /// RAUW a temporary.
  816. ///
  817. /// \pre \a isTemporary() must be \c true.
  818. void replaceAllUsesWith(Metadata *MD) {
  819. assert(isTemporary() && "Expected temporary node");
  820. if (Context.hasReplaceableUses())
  821. Context.getReplaceableUses()->replaceAllUsesWith(MD);
  822. }
  823. /// Resolve cycles.
  824. ///
  825. /// Once all forward declarations have been resolved, force cycles to be
  826. /// resolved.
  827. ///
  828. /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
  829. void resolveCycles();
  830. /// Resolve a unique, unresolved node.
  831. void resolve();
  832. /// Replace a temporary node with a permanent one.
  833. ///
  834. /// Try to create a uniqued version of \c N -- in place, if possible -- and
  835. /// return it. If \c N cannot be uniqued, return a distinct node instead.
  836. template <class T>
  837. static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
  838. replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
  839. return cast<T>(N.release()->replaceWithPermanentImpl());
  840. }
  841. /// Replace a temporary node with a uniqued one.
  842. ///
  843. /// Create a uniqued version of \c N -- in place, if possible -- and return
  844. /// it. Takes ownership of the temporary node.
  845. ///
  846. /// \pre N does not self-reference.
  847. template <class T>
  848. static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
  849. replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
  850. return cast<T>(N.release()->replaceWithUniquedImpl());
  851. }
  852. /// Replace a temporary node with a distinct one.
  853. ///
  854. /// Create a distinct version of \c N -- in place, if possible -- and return
  855. /// it. Takes ownership of the temporary node.
  856. template <class T>
  857. static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
  858. replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
  859. return cast<T>(N.release()->replaceWithDistinctImpl());
  860. }
  861. private:
  862. MDNode *replaceWithPermanentImpl();
  863. MDNode *replaceWithUniquedImpl();
  864. MDNode *replaceWithDistinctImpl();
  865. protected:
  866. /// Set an operand.
  867. ///
  868. /// Sets the operand directly, without worrying about uniquing.
  869. void setOperand(unsigned I, Metadata *New);
  870. void storeDistinctInContext();
  871. template <class T, class StoreT>
  872. static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
  873. template <class T> static T *storeImpl(T *N, StorageType Storage);
  874. private:
  875. void handleChangedOperand(void *Ref, Metadata *New);
  876. /// Drop RAUW support, if any.
  877. void dropReplaceableUses();
  878. void resolveAfterOperandChange(Metadata *Old, Metadata *New);
  879. void decrementUnresolvedOperandCount();
  880. void countUnresolvedOperands();
  881. /// Mutate this to be "uniqued".
  882. ///
  883. /// Mutate this so that \a isUniqued().
  884. /// \pre \a isTemporary().
  885. /// \pre already added to uniquing set.
  886. void makeUniqued();
  887. /// Mutate this to be "distinct".
  888. ///
  889. /// Mutate this so that \a isDistinct().
  890. /// \pre \a isTemporary().
  891. void makeDistinct();
  892. void deleteAsSubclass();
  893. MDNode *uniquify();
  894. void eraseFromStore();
  895. template <class NodeTy> struct HasCachedHash;
  896. template <class NodeTy>
  897. static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
  898. N->recalculateHash();
  899. }
  900. template <class NodeTy>
  901. static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
  902. template <class NodeTy>
  903. static void dispatchResetHash(NodeTy *N, std::true_type) {
  904. N->setHash(0);
  905. }
  906. template <class NodeTy>
  907. static void dispatchResetHash(NodeTy *, std::false_type) {}
  908. public:
  909. using op_iterator = const MDOperand *;
  910. using op_range = iterator_range<op_iterator>;
  911. op_iterator op_begin() const {
  912. return const_cast<MDNode *>(this)->mutable_begin();
  913. }
  914. op_iterator op_end() const {
  915. return const_cast<MDNode *>(this)->mutable_end();
  916. }
  917. op_range operands() const { return op_range(op_begin(), op_end()); }
  918. const MDOperand &getOperand(unsigned I) const {
  919. assert(I < NumOperands && "Out of range");
  920. return op_begin()[I];
  921. }
  922. /// Return number of MDNode operands.
  923. unsigned getNumOperands() const { return NumOperands; }
  924. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  925. static bool classof(const Metadata *MD) {
  926. switch (MD->getMetadataID()) {
  927. default:
  928. return false;
  929. #define HANDLE_MDNODE_LEAF(CLASS) \
  930. case CLASS##Kind: \
  931. return true;
  932. #include "llvm/IR/Metadata.def"
  933. }
  934. }
  935. /// Check whether MDNode is a vtable access.
  936. bool isTBAAVtableAccess() const;
  937. /// Methods for metadata merging.
  938. static MDNode *concatenate(MDNode *A, MDNode *B);
  939. static MDNode *intersect(MDNode *A, MDNode *B);
  940. static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
  941. static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
  942. static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
  943. static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
  944. static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
  945. };
  946. /// Tuple of metadata.
  947. ///
  948. /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
  949. /// default based on their operands.
  950. class MDTuple : public MDNode {
  951. friend class LLVMContextImpl;
  952. friend class MDNode;
  953. MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
  954. ArrayRef<Metadata *> Vals)
  955. : MDNode(C, MDTupleKind, Storage, Vals) {
  956. setHash(Hash);
  957. }
  958. ~MDTuple() { dropAllReferences(); }
  959. void setHash(unsigned Hash) { SubclassData32 = Hash; }
  960. void recalculateHash();
  961. static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
  962. StorageType Storage, bool ShouldCreate = true);
  963. TempMDTuple cloneImpl() const {
  964. return getTemporary(getContext(), SmallVector<Metadata *, 4>(operands()));
  965. }
  966. public:
  967. /// Get the hash, if any.
  968. unsigned getHash() const { return SubclassData32; }
  969. static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  970. return getImpl(Context, MDs, Uniqued);
  971. }
  972. static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  973. return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
  974. }
  975. /// Return a distinct node.
  976. ///
  977. /// Return a distinct node -- i.e., a node that is not uniqued.
  978. static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  979. return getImpl(Context, MDs, Distinct);
  980. }
  981. /// Return a temporary node.
  982. ///
  983. /// For use in constructing cyclic MDNode structures. A temporary MDNode is
  984. /// not uniqued, may be RAUW'd, and must be manually deleted with
  985. /// deleteTemporary.
  986. static TempMDTuple getTemporary(LLVMContext &Context,
  987. ArrayRef<Metadata *> MDs) {
  988. return TempMDTuple(getImpl(Context, MDs, Temporary));
  989. }
  990. /// Return a (temporary) clone of this.
  991. TempMDTuple clone() const { return cloneImpl(); }
  992. static bool classof(const Metadata *MD) {
  993. return MD->getMetadataID() == MDTupleKind;
  994. }
  995. };
  996. MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  997. return MDTuple::get(Context, MDs);
  998. }
  999. MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  1000. return MDTuple::getIfExists(Context, MDs);
  1001. }
  1002. MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  1003. return MDTuple::getDistinct(Context, MDs);
  1004. }
  1005. TempMDTuple MDNode::getTemporary(LLVMContext &Context,
  1006. ArrayRef<Metadata *> MDs) {
  1007. return MDTuple::getTemporary(Context, MDs);
  1008. }
  1009. void TempMDNodeDeleter::operator()(MDNode *Node) const {
  1010. MDNode::deleteTemporary(Node);
  1011. }
  1012. /// This is a simple wrapper around an MDNode which provides a higher-level
  1013. /// interface by hiding the details of how alias analysis information is encoded
  1014. /// in its operands.
  1015. class AliasScopeNode {
  1016. const MDNode *Node = nullptr;
  1017. public:
  1018. AliasScopeNode() = default;
  1019. explicit AliasScopeNode(const MDNode *N) : Node(N) {}
  1020. /// Get the MDNode for this AliasScopeNode.
  1021. const MDNode *getNode() const { return Node; }
  1022. /// Get the MDNode for this AliasScopeNode's domain.
  1023. const MDNode *getDomain() const {
  1024. if (Node->getNumOperands() < 2)
  1025. return nullptr;
  1026. return dyn_cast_or_null<MDNode>(Node->getOperand(1));
  1027. }
  1028. StringRef getName() const {
  1029. if (Node->getNumOperands() > 2)
  1030. if (MDString *N = dyn_cast_or_null<MDString>(Node->getOperand(2)))
  1031. return N->getString();
  1032. return StringRef();
  1033. }
  1034. };
  1035. /// Typed iterator through MDNode operands.
  1036. ///
  1037. /// An iterator that transforms an \a MDNode::iterator into an iterator over a
  1038. /// particular Metadata subclass.
  1039. template <class T>
  1040. class TypedMDOperandIterator
  1041. : public std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void,
  1042. T *> {
  1043. MDNode::op_iterator I = nullptr;
  1044. public:
  1045. TypedMDOperandIterator() = default;
  1046. explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
  1047. T *operator*() const { return cast_or_null<T>(*I); }
  1048. TypedMDOperandIterator &operator++() {
  1049. ++I;
  1050. return *this;
  1051. }
  1052. TypedMDOperandIterator operator++(int) {
  1053. TypedMDOperandIterator Temp(*this);
  1054. ++I;
  1055. return Temp;
  1056. }
  1057. bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
  1058. bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
  1059. };
  1060. /// Typed, array-like tuple of metadata.
  1061. ///
  1062. /// This is a wrapper for \a MDTuple that makes it act like an array holding a
  1063. /// particular type of metadata.
  1064. template <class T> class MDTupleTypedArrayWrapper {
  1065. const MDTuple *N = nullptr;
  1066. public:
  1067. MDTupleTypedArrayWrapper() = default;
  1068. MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
  1069. template <class U>
  1070. MDTupleTypedArrayWrapper(
  1071. const MDTupleTypedArrayWrapper<U> &Other,
  1072. std::enable_if_t<std::is_convertible<U *, T *>::value> * = nullptr)
  1073. : N(Other.get()) {}
  1074. template <class U>
  1075. explicit MDTupleTypedArrayWrapper(
  1076. const MDTupleTypedArrayWrapper<U> &Other,
  1077. std::enable_if_t<!std::is_convertible<U *, T *>::value> * = nullptr)
  1078. : N(Other.get()) {}
  1079. explicit operator bool() const { return get(); }
  1080. explicit operator MDTuple *() const { return get(); }
  1081. MDTuple *get() const { return const_cast<MDTuple *>(N); }
  1082. MDTuple *operator->() const { return get(); }
  1083. MDTuple &operator*() const { return *get(); }
  1084. // FIXME: Fix callers and remove condition on N.
  1085. unsigned size() const { return N ? N->getNumOperands() : 0u; }
  1086. bool empty() const { return N ? N->getNumOperands() == 0 : true; }
  1087. T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
  1088. // FIXME: Fix callers and remove condition on N.
  1089. using iterator = TypedMDOperandIterator<T>;
  1090. iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
  1091. iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
  1092. };
  1093. #define HANDLE_METADATA(CLASS) \
  1094. using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
  1095. #include "llvm/IR/Metadata.def"
  1096. /// Placeholder metadata for operands of distinct MDNodes.
  1097. ///
  1098. /// This is a lightweight placeholder for an operand of a distinct node. It's
  1099. /// purpose is to help track forward references when creating a distinct node.
  1100. /// This allows distinct nodes involved in a cycle to be constructed before
  1101. /// their operands without requiring a heavyweight temporary node with
  1102. /// full-blown RAUW support.
  1103. ///
  1104. /// Each placeholder supports only a single MDNode user. Clients should pass
  1105. /// an ID, retrieved via \a getID(), to indicate the "real" operand that this
  1106. /// should be replaced with.
  1107. ///
  1108. /// While it would be possible to implement move operators, they would be
  1109. /// fairly expensive. Leave them unimplemented to discourage their use
  1110. /// (clients can use std::deque, std::list, BumpPtrAllocator, etc.).
  1111. class DistinctMDOperandPlaceholder : public Metadata {
  1112. friend class MetadataTracking;
  1113. Metadata **Use = nullptr;
  1114. public:
  1115. explicit DistinctMDOperandPlaceholder(unsigned ID)
  1116. : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
  1117. SubclassData32 = ID;
  1118. }
  1119. DistinctMDOperandPlaceholder() = delete;
  1120. DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete;
  1121. DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete;
  1122. ~DistinctMDOperandPlaceholder() {
  1123. if (Use)
  1124. *Use = nullptr;
  1125. }
  1126. unsigned getID() const { return SubclassData32; }
  1127. /// Replace the use of this with MD.
  1128. void replaceUseWith(Metadata *MD) {
  1129. if (!Use)
  1130. return;
  1131. *Use = MD;
  1132. if (*Use)
  1133. MetadataTracking::track(*Use);
  1134. Metadata *T = cast<Metadata>(this);
  1135. MetadataTracking::untrack(T);
  1136. assert(!Use && "Use is still being tracked despite being untracked!");
  1137. }
  1138. };
  1139. //===----------------------------------------------------------------------===//
  1140. /// A tuple of MDNodes.
  1141. ///
  1142. /// Despite its name, a NamedMDNode isn't itself an MDNode.
  1143. ///
  1144. /// NamedMDNodes are named module-level entities that contain lists of MDNodes.
  1145. ///
  1146. /// It is illegal for a NamedMDNode to appear as an operand of an MDNode.
  1147. class NamedMDNode : public ilist_node<NamedMDNode> {
  1148. friend class LLVMContextImpl;
  1149. friend class Module;
  1150. std::string Name;
  1151. Module *Parent = nullptr;
  1152. void *Operands; // SmallVector<TrackingMDRef, 4>
  1153. void setParent(Module *M) { Parent = M; }
  1154. explicit NamedMDNode(const Twine &N);
  1155. template<class T1, class T2>
  1156. class op_iterator_impl :
  1157. public std::iterator<std::bidirectional_iterator_tag, T2> {
  1158. friend class NamedMDNode;
  1159. const NamedMDNode *Node = nullptr;
  1160. unsigned Idx = 0;
  1161. op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {}
  1162. public:
  1163. op_iterator_impl() = default;
  1164. bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
  1165. bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
  1166. op_iterator_impl &operator++() {
  1167. ++Idx;
  1168. return *this;
  1169. }
  1170. op_iterator_impl operator++(int) {
  1171. op_iterator_impl tmp(*this);
  1172. operator++();
  1173. return tmp;
  1174. }
  1175. op_iterator_impl &operator--() {
  1176. --Idx;
  1177. return *this;
  1178. }
  1179. op_iterator_impl operator--(int) {
  1180. op_iterator_impl tmp(*this);
  1181. operator--();
  1182. return tmp;
  1183. }
  1184. T1 operator*() const { return Node->getOperand(Idx); }
  1185. };
  1186. public:
  1187. NamedMDNode(const NamedMDNode &) = delete;
  1188. ~NamedMDNode();
  1189. /// Drop all references and remove the node from parent module.
  1190. void eraseFromParent();
  1191. /// Remove all uses and clear node vector.
  1192. void dropAllReferences() { clearOperands(); }
  1193. /// Drop all references to this node's operands.
  1194. void clearOperands();
  1195. /// Get the module that holds this named metadata collection.
  1196. inline Module *getParent() { return Parent; }
  1197. inline const Module *getParent() const { return Parent; }
  1198. MDNode *getOperand(unsigned i) const;
  1199. unsigned getNumOperands() const;
  1200. void addOperand(MDNode *M);
  1201. void setOperand(unsigned I, MDNode *New);
  1202. StringRef getName() const;
  1203. void print(raw_ostream &ROS, bool IsForDebug = false) const;
  1204. void print(raw_ostream &ROS, ModuleSlotTracker &MST,
  1205. bool IsForDebug = false) const;
  1206. void dump() const;
  1207. // ---------------------------------------------------------------------------
  1208. // Operand Iterator interface...
  1209. //
  1210. using op_iterator = op_iterator_impl<MDNode *, MDNode>;
  1211. op_iterator op_begin() { return op_iterator(this, 0); }
  1212. op_iterator op_end() { return op_iterator(this, getNumOperands()); }
  1213. using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>;
  1214. const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
  1215. const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
  1216. inline iterator_range<op_iterator> operands() {
  1217. return make_range(op_begin(), op_end());
  1218. }
  1219. inline iterator_range<const_op_iterator> operands() const {
  1220. return make_range(op_begin(), op_end());
  1221. }
  1222. };
  1223. // Create wrappers for C Binding types (see CBindingWrapping.h).
  1224. DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef)
  1225. } // end namespace llvm
  1226. #endif // LLVM_IR_METADATA_H
  1227. #ifdef __GNUC__
  1228. #pragma GCC diagnostic pop
  1229. #endif