Attributes.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. //===- Attributes.cpp - Implement AttributesList --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // \file
  10. // This file implements the Attribute, AttributeImpl, AttrBuilder,
  11. // AttributeListImpl, and AttributeList classes.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/Attributes.h"
  15. #include "AttributeImpl.h"
  16. #include "LLVMContextImpl.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/FoldingSet.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringExtras.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/ADT/StringSwitch.h"
  24. #include "llvm/Config/llvm-config.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/Type.h"
  28. #include "llvm/Support/Compiler.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/ModRef.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <cstddef>
  35. #include <cstdint>
  36. #include <limits>
  37. #include <optional>
  38. #include <string>
  39. #include <tuple>
  40. #include <utility>
  41. using namespace llvm;
  42. //===----------------------------------------------------------------------===//
  43. // Attribute Construction Methods
  44. //===----------------------------------------------------------------------===//
  45. // allocsize has two integer arguments, but because they're both 32 bits, we can
  46. // pack them into one 64-bit value, at the cost of making said value
  47. // nonsensical.
  48. //
  49. // In order to do this, we need to reserve one value of the second (optional)
  50. // allocsize argument to signify "not present."
  51. static const unsigned AllocSizeNumElemsNotPresent = -1;
  52. static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
  53. const std::optional<unsigned> &NumElemsArg) {
  54. assert((!NumElemsArg || *NumElemsArg != AllocSizeNumElemsNotPresent) &&
  55. "Attempting to pack a reserved value");
  56. return uint64_t(ElemSizeArg) << 32 |
  57. NumElemsArg.value_or(AllocSizeNumElemsNotPresent);
  58. }
  59. static std::pair<unsigned, std::optional<unsigned>>
  60. unpackAllocSizeArgs(uint64_t Num) {
  61. unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
  62. unsigned ElemSizeArg = Num >> 32;
  63. std::optional<unsigned> NumElemsArg;
  64. if (NumElems != AllocSizeNumElemsNotPresent)
  65. NumElemsArg = NumElems;
  66. return std::make_pair(ElemSizeArg, NumElemsArg);
  67. }
  68. static uint64_t packVScaleRangeArgs(unsigned MinValue,
  69. std::optional<unsigned> MaxValue) {
  70. return uint64_t(MinValue) << 32 | MaxValue.value_or(0);
  71. }
  72. static std::pair<unsigned, std::optional<unsigned>>
  73. unpackVScaleRangeArgs(uint64_t Value) {
  74. unsigned MaxValue = Value & std::numeric_limits<unsigned>::max();
  75. unsigned MinValue = Value >> 32;
  76. return std::make_pair(MinValue,
  77. MaxValue > 0 ? MaxValue : std::optional<unsigned>());
  78. }
  79. Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
  80. uint64_t Val) {
  81. bool IsIntAttr = Attribute::isIntAttrKind(Kind);
  82. assert((IsIntAttr || Attribute::isEnumAttrKind(Kind)) &&
  83. "Not an enum or int attribute");
  84. LLVMContextImpl *pImpl = Context.pImpl;
  85. FoldingSetNodeID ID;
  86. ID.AddInteger(Kind);
  87. if (IsIntAttr)
  88. ID.AddInteger(Val);
  89. else
  90. assert(Val == 0 && "Value must be zero for enum attributes");
  91. void *InsertPoint;
  92. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  93. if (!PA) {
  94. // If we didn't find any existing attributes of the same shape then create a
  95. // new one and insert it.
  96. if (!IsIntAttr)
  97. PA = new (pImpl->Alloc) EnumAttributeImpl(Kind);
  98. else
  99. PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val);
  100. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  101. }
  102. // Return the Attribute that we found or created.
  103. return Attribute(PA);
  104. }
  105. Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
  106. LLVMContextImpl *pImpl = Context.pImpl;
  107. FoldingSetNodeID ID;
  108. ID.AddString(Kind);
  109. if (!Val.empty()) ID.AddString(Val);
  110. void *InsertPoint;
  111. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  112. if (!PA) {
  113. // If we didn't find any existing attributes of the same shape then create a
  114. // new one and insert it.
  115. void *Mem =
  116. pImpl->Alloc.Allocate(StringAttributeImpl::totalSizeToAlloc(Kind, Val),
  117. alignof(StringAttributeImpl));
  118. PA = new (Mem) StringAttributeImpl(Kind, Val);
  119. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  120. }
  121. // Return the Attribute that we found or created.
  122. return Attribute(PA);
  123. }
  124. Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
  125. Type *Ty) {
  126. assert(Attribute::isTypeAttrKind(Kind) && "Not a type attribute");
  127. LLVMContextImpl *pImpl = Context.pImpl;
  128. FoldingSetNodeID ID;
  129. ID.AddInteger(Kind);
  130. ID.AddPointer(Ty);
  131. void *InsertPoint;
  132. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  133. if (!PA) {
  134. // If we didn't find any existing attributes of the same shape then create a
  135. // new one and insert it.
  136. PA = new (pImpl->Alloc) TypeAttributeImpl(Kind, Ty);
  137. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  138. }
  139. // Return the Attribute that we found or created.
  140. return Attribute(PA);
  141. }
  142. Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) {
  143. assert(A <= llvm::Value::MaximumAlignment && "Alignment too large.");
  144. return get(Context, Alignment, A.value());
  145. }
  146. Attribute Attribute::getWithStackAlignment(LLVMContext &Context, Align A) {
  147. assert(A <= 0x100 && "Alignment too large.");
  148. return get(Context, StackAlignment, A.value());
  149. }
  150. Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
  151. uint64_t Bytes) {
  152. assert(Bytes && "Bytes must be non-zero.");
  153. return get(Context, Dereferenceable, Bytes);
  154. }
  155. Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
  156. uint64_t Bytes) {
  157. assert(Bytes && "Bytes must be non-zero.");
  158. return get(Context, DereferenceableOrNull, Bytes);
  159. }
  160. Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) {
  161. return get(Context, ByVal, Ty);
  162. }
  163. Attribute Attribute::getWithStructRetType(LLVMContext &Context, Type *Ty) {
  164. return get(Context, StructRet, Ty);
  165. }
  166. Attribute Attribute::getWithByRefType(LLVMContext &Context, Type *Ty) {
  167. return get(Context, ByRef, Ty);
  168. }
  169. Attribute Attribute::getWithPreallocatedType(LLVMContext &Context, Type *Ty) {
  170. return get(Context, Preallocated, Ty);
  171. }
  172. Attribute Attribute::getWithInAllocaType(LLVMContext &Context, Type *Ty) {
  173. return get(Context, InAlloca, Ty);
  174. }
  175. Attribute Attribute::getWithUWTableKind(LLVMContext &Context,
  176. UWTableKind Kind) {
  177. return get(Context, UWTable, uint64_t(Kind));
  178. }
  179. Attribute Attribute::getWithMemoryEffects(LLVMContext &Context,
  180. MemoryEffects ME) {
  181. return get(Context, Memory, ME.toIntValue());
  182. }
  183. Attribute
  184. Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
  185. const std::optional<unsigned> &NumElemsArg) {
  186. assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
  187. "Invalid allocsize arguments -- given allocsize(0, 0)");
  188. return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
  189. }
  190. Attribute Attribute::getWithVScaleRangeArgs(LLVMContext &Context,
  191. unsigned MinValue,
  192. unsigned MaxValue) {
  193. return get(Context, VScaleRange, packVScaleRangeArgs(MinValue, MaxValue));
  194. }
  195. Attribute::AttrKind Attribute::getAttrKindFromName(StringRef AttrName) {
  196. return StringSwitch<Attribute::AttrKind>(AttrName)
  197. #define GET_ATTR_NAMES
  198. #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
  199. .Case(#DISPLAY_NAME, Attribute::ENUM_NAME)
  200. #include "llvm/IR/Attributes.inc"
  201. .Default(Attribute::None);
  202. }
  203. StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) {
  204. switch (AttrKind) {
  205. #define GET_ATTR_NAMES
  206. #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
  207. case Attribute::ENUM_NAME: \
  208. return #DISPLAY_NAME;
  209. #include "llvm/IR/Attributes.inc"
  210. case Attribute::None:
  211. return "none";
  212. default:
  213. llvm_unreachable("invalid Kind");
  214. }
  215. }
  216. bool Attribute::isExistingAttribute(StringRef Name) {
  217. return StringSwitch<bool>(Name)
  218. #define GET_ATTR_NAMES
  219. #define ATTRIBUTE_ALL(ENUM_NAME, DISPLAY_NAME) .Case(#DISPLAY_NAME, true)
  220. #include "llvm/IR/Attributes.inc"
  221. .Default(false);
  222. }
  223. //===----------------------------------------------------------------------===//
  224. // Attribute Accessor Methods
  225. //===----------------------------------------------------------------------===//
  226. bool Attribute::isEnumAttribute() const {
  227. return pImpl && pImpl->isEnumAttribute();
  228. }
  229. bool Attribute::isIntAttribute() const {
  230. return pImpl && pImpl->isIntAttribute();
  231. }
  232. bool Attribute::isStringAttribute() const {
  233. return pImpl && pImpl->isStringAttribute();
  234. }
  235. bool Attribute::isTypeAttribute() const {
  236. return pImpl && pImpl->isTypeAttribute();
  237. }
  238. Attribute::AttrKind Attribute::getKindAsEnum() const {
  239. if (!pImpl) return None;
  240. assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) &&
  241. "Invalid attribute type to get the kind as an enum!");
  242. return pImpl->getKindAsEnum();
  243. }
  244. uint64_t Attribute::getValueAsInt() const {
  245. if (!pImpl) return 0;
  246. assert(isIntAttribute() &&
  247. "Expected the attribute to be an integer attribute!");
  248. return pImpl->getValueAsInt();
  249. }
  250. bool Attribute::getValueAsBool() const {
  251. if (!pImpl) return false;
  252. assert(isStringAttribute() &&
  253. "Expected the attribute to be a string attribute!");
  254. return pImpl->getValueAsBool();
  255. }
  256. StringRef Attribute::getKindAsString() const {
  257. if (!pImpl) return {};
  258. assert(isStringAttribute() &&
  259. "Invalid attribute type to get the kind as a string!");
  260. return pImpl->getKindAsString();
  261. }
  262. StringRef Attribute::getValueAsString() const {
  263. if (!pImpl) return {};
  264. assert(isStringAttribute() &&
  265. "Invalid attribute type to get the value as a string!");
  266. return pImpl->getValueAsString();
  267. }
  268. Type *Attribute::getValueAsType() const {
  269. if (!pImpl) return {};
  270. assert(isTypeAttribute() &&
  271. "Invalid attribute type to get the value as a type!");
  272. return pImpl->getValueAsType();
  273. }
  274. bool Attribute::hasAttribute(AttrKind Kind) const {
  275. return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
  276. }
  277. bool Attribute::hasAttribute(StringRef Kind) const {
  278. if (!isStringAttribute()) return false;
  279. return pImpl && pImpl->hasAttribute(Kind);
  280. }
  281. MaybeAlign Attribute::getAlignment() const {
  282. assert(hasAttribute(Attribute::Alignment) &&
  283. "Trying to get alignment from non-alignment attribute!");
  284. return MaybeAlign(pImpl->getValueAsInt());
  285. }
  286. MaybeAlign Attribute::getStackAlignment() const {
  287. assert(hasAttribute(Attribute::StackAlignment) &&
  288. "Trying to get alignment from non-alignment attribute!");
  289. return MaybeAlign(pImpl->getValueAsInt());
  290. }
  291. uint64_t Attribute::getDereferenceableBytes() const {
  292. assert(hasAttribute(Attribute::Dereferenceable) &&
  293. "Trying to get dereferenceable bytes from "
  294. "non-dereferenceable attribute!");
  295. return pImpl->getValueAsInt();
  296. }
  297. uint64_t Attribute::getDereferenceableOrNullBytes() const {
  298. assert(hasAttribute(Attribute::DereferenceableOrNull) &&
  299. "Trying to get dereferenceable bytes from "
  300. "non-dereferenceable attribute!");
  301. return pImpl->getValueAsInt();
  302. }
  303. std::pair<unsigned, std::optional<unsigned>>
  304. Attribute::getAllocSizeArgs() const {
  305. assert(hasAttribute(Attribute::AllocSize) &&
  306. "Trying to get allocsize args from non-allocsize attribute");
  307. return unpackAllocSizeArgs(pImpl->getValueAsInt());
  308. }
  309. unsigned Attribute::getVScaleRangeMin() const {
  310. assert(hasAttribute(Attribute::VScaleRange) &&
  311. "Trying to get vscale args from non-vscale attribute");
  312. return unpackVScaleRangeArgs(pImpl->getValueAsInt()).first;
  313. }
  314. std::optional<unsigned> Attribute::getVScaleRangeMax() const {
  315. assert(hasAttribute(Attribute::VScaleRange) &&
  316. "Trying to get vscale args from non-vscale attribute");
  317. return unpackVScaleRangeArgs(pImpl->getValueAsInt()).second;
  318. }
  319. UWTableKind Attribute::getUWTableKind() const {
  320. assert(hasAttribute(Attribute::UWTable) &&
  321. "Trying to get unwind table kind from non-uwtable attribute");
  322. return UWTableKind(pImpl->getValueAsInt());
  323. }
  324. AllocFnKind Attribute::getAllocKind() const {
  325. assert(hasAttribute(Attribute::AllocKind) &&
  326. "Trying to get allockind value from non-allockind attribute");
  327. return AllocFnKind(pImpl->getValueAsInt());
  328. }
  329. MemoryEffects Attribute::getMemoryEffects() const {
  330. assert(hasAttribute(Attribute::Memory) &&
  331. "Can only call getMemoryEffects() on memory attribute");
  332. return MemoryEffects::createFromIntValue(pImpl->getValueAsInt());
  333. }
  334. static const char *getModRefStr(ModRefInfo MR) {
  335. switch (MR) {
  336. case ModRefInfo::NoModRef:
  337. return "none";
  338. case ModRefInfo::Ref:
  339. return "read";
  340. case ModRefInfo::Mod:
  341. return "write";
  342. case ModRefInfo::ModRef:
  343. return "readwrite";
  344. }
  345. llvm_unreachable("Invalid ModRefInfo");
  346. }
  347. std::string Attribute::getAsString(bool InAttrGrp) const {
  348. if (!pImpl) return {};
  349. if (isEnumAttribute())
  350. return getNameFromAttrKind(getKindAsEnum()).str();
  351. if (isTypeAttribute()) {
  352. std::string Result = getNameFromAttrKind(getKindAsEnum()).str();
  353. Result += '(';
  354. raw_string_ostream OS(Result);
  355. getValueAsType()->print(OS, false, true);
  356. OS.flush();
  357. Result += ')';
  358. return Result;
  359. }
  360. // FIXME: These should be output like this:
  361. //
  362. // align=4
  363. // alignstack=8
  364. //
  365. if (hasAttribute(Attribute::Alignment))
  366. return (InAttrGrp ? "align=" + Twine(getValueAsInt())
  367. : "align " + Twine(getValueAsInt()))
  368. .str();
  369. auto AttrWithBytesToString = [&](const char *Name) {
  370. return (InAttrGrp ? Name + ("=" + Twine(getValueAsInt()))
  371. : Name + ("(" + Twine(getValueAsInt())) + ")")
  372. .str();
  373. };
  374. if (hasAttribute(Attribute::StackAlignment))
  375. return AttrWithBytesToString("alignstack");
  376. if (hasAttribute(Attribute::Dereferenceable))
  377. return AttrWithBytesToString("dereferenceable");
  378. if (hasAttribute(Attribute::DereferenceableOrNull))
  379. return AttrWithBytesToString("dereferenceable_or_null");
  380. if (hasAttribute(Attribute::AllocSize)) {
  381. unsigned ElemSize;
  382. std::optional<unsigned> NumElems;
  383. std::tie(ElemSize, NumElems) = getAllocSizeArgs();
  384. return (NumElems
  385. ? "allocsize(" + Twine(ElemSize) + "," + Twine(*NumElems) + ")"
  386. : "allocsize(" + Twine(ElemSize) + ")")
  387. .str();
  388. }
  389. if (hasAttribute(Attribute::VScaleRange)) {
  390. unsigned MinValue = getVScaleRangeMin();
  391. std::optional<unsigned> MaxValue = getVScaleRangeMax();
  392. return ("vscale_range(" + Twine(MinValue) + "," +
  393. Twine(MaxValue.value_or(0)) + ")")
  394. .str();
  395. }
  396. if (hasAttribute(Attribute::UWTable)) {
  397. UWTableKind Kind = getUWTableKind();
  398. if (Kind != UWTableKind::None) {
  399. return Kind == UWTableKind::Default
  400. ? "uwtable"
  401. : ("uwtable(" +
  402. Twine(Kind == UWTableKind::Sync ? "sync" : "async") + ")")
  403. .str();
  404. }
  405. }
  406. if (hasAttribute(Attribute::AllocKind)) {
  407. AllocFnKind Kind = getAllocKind();
  408. SmallVector<StringRef> parts;
  409. if ((Kind & AllocFnKind::Alloc) != AllocFnKind::Unknown)
  410. parts.push_back("alloc");
  411. if ((Kind & AllocFnKind::Realloc) != AllocFnKind::Unknown)
  412. parts.push_back("realloc");
  413. if ((Kind & AllocFnKind::Free) != AllocFnKind::Unknown)
  414. parts.push_back("free");
  415. if ((Kind & AllocFnKind::Uninitialized) != AllocFnKind::Unknown)
  416. parts.push_back("uninitialized");
  417. if ((Kind & AllocFnKind::Zeroed) != AllocFnKind::Unknown)
  418. parts.push_back("zeroed");
  419. if ((Kind & AllocFnKind::Aligned) != AllocFnKind::Unknown)
  420. parts.push_back("aligned");
  421. return ("allockind(\"" +
  422. Twine(llvm::join(parts.begin(), parts.end(), ",")) + "\")")
  423. .str();
  424. }
  425. if (hasAttribute(Attribute::Memory)) {
  426. std::string Result;
  427. raw_string_ostream OS(Result);
  428. bool First = true;
  429. OS << "memory(";
  430. MemoryEffects ME = getMemoryEffects();
  431. // Print access kind for "other" as the default access kind. This way it
  432. // will apply to any new location kinds that get split out of "other".
  433. ModRefInfo OtherMR = ME.getModRef(MemoryEffects::Other);
  434. if (OtherMR != ModRefInfo::NoModRef || ME.getModRef() == OtherMR) {
  435. First = false;
  436. OS << getModRefStr(OtherMR);
  437. }
  438. for (auto Loc : MemoryEffects::locations()) {
  439. ModRefInfo MR = ME.getModRef(Loc);
  440. if (MR == OtherMR)
  441. continue;
  442. if (!First)
  443. OS << ", ";
  444. First = false;
  445. switch (Loc) {
  446. case MemoryEffects::ArgMem:
  447. OS << "argmem: ";
  448. break;
  449. case MemoryEffects::InaccessibleMem:
  450. OS << "inaccessiblemem: ";
  451. break;
  452. case MemoryEffects::Other:
  453. llvm_unreachable("This is represented as the default access kind");
  454. }
  455. OS << getModRefStr(MR);
  456. }
  457. OS << ")";
  458. OS.flush();
  459. return Result;
  460. }
  461. // Convert target-dependent attributes to strings of the form:
  462. //
  463. // "kind"
  464. // "kind" = "value"
  465. //
  466. if (isStringAttribute()) {
  467. std::string Result;
  468. {
  469. raw_string_ostream OS(Result);
  470. OS << '"' << getKindAsString() << '"';
  471. // Since some attribute strings contain special characters that cannot be
  472. // printable, those have to be escaped to make the attribute value
  473. // printable as is. e.g. "\01__gnu_mcount_nc"
  474. const auto &AttrVal = pImpl->getValueAsString();
  475. if (!AttrVal.empty()) {
  476. OS << "=\"";
  477. printEscapedString(AttrVal, OS);
  478. OS << "\"";
  479. }
  480. }
  481. return Result;
  482. }
  483. llvm_unreachable("Unknown attribute");
  484. }
  485. bool Attribute::hasParentContext(LLVMContext &C) const {
  486. assert(isValid() && "invalid Attribute doesn't refer to any context");
  487. FoldingSetNodeID ID;
  488. pImpl->Profile(ID);
  489. void *Unused;
  490. return C.pImpl->AttrsSet.FindNodeOrInsertPos(ID, Unused) == pImpl;
  491. }
  492. bool Attribute::operator<(Attribute A) const {
  493. if (!pImpl && !A.pImpl) return false;
  494. if (!pImpl) return true;
  495. if (!A.pImpl) return false;
  496. return *pImpl < *A.pImpl;
  497. }
  498. void Attribute::Profile(FoldingSetNodeID &ID) const {
  499. ID.AddPointer(pImpl);
  500. }
  501. enum AttributeProperty {
  502. FnAttr = (1 << 0),
  503. ParamAttr = (1 << 1),
  504. RetAttr = (1 << 2),
  505. };
  506. #define GET_ATTR_PROP_TABLE
  507. #include "llvm/IR/Attributes.inc"
  508. static bool hasAttributeProperty(Attribute::AttrKind Kind,
  509. AttributeProperty Prop) {
  510. unsigned Index = Kind - 1;
  511. assert(Index < std::size(AttrPropTable) && "Invalid attribute kind");
  512. return AttrPropTable[Index] & Prop;
  513. }
  514. bool Attribute::canUseAsFnAttr(AttrKind Kind) {
  515. return hasAttributeProperty(Kind, AttributeProperty::FnAttr);
  516. }
  517. bool Attribute::canUseAsParamAttr(AttrKind Kind) {
  518. return hasAttributeProperty(Kind, AttributeProperty::ParamAttr);
  519. }
  520. bool Attribute::canUseAsRetAttr(AttrKind Kind) {
  521. return hasAttributeProperty(Kind, AttributeProperty::RetAttr);
  522. }
  523. //===----------------------------------------------------------------------===//
  524. // AttributeImpl Definition
  525. //===----------------------------------------------------------------------===//
  526. bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
  527. if (isStringAttribute()) return false;
  528. return getKindAsEnum() == A;
  529. }
  530. bool AttributeImpl::hasAttribute(StringRef Kind) const {
  531. if (!isStringAttribute()) return false;
  532. return getKindAsString() == Kind;
  533. }
  534. Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
  535. assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute());
  536. return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
  537. }
  538. uint64_t AttributeImpl::getValueAsInt() const {
  539. assert(isIntAttribute());
  540. return static_cast<const IntAttributeImpl *>(this)->getValue();
  541. }
  542. bool AttributeImpl::getValueAsBool() const {
  543. assert(getValueAsString().empty() || getValueAsString() == "false" || getValueAsString() == "true");
  544. return getValueAsString() == "true";
  545. }
  546. StringRef AttributeImpl::getKindAsString() const {
  547. assert(isStringAttribute());
  548. return static_cast<const StringAttributeImpl *>(this)->getStringKind();
  549. }
  550. StringRef AttributeImpl::getValueAsString() const {
  551. assert(isStringAttribute());
  552. return static_cast<const StringAttributeImpl *>(this)->getStringValue();
  553. }
  554. Type *AttributeImpl::getValueAsType() const {
  555. assert(isTypeAttribute());
  556. return static_cast<const TypeAttributeImpl *>(this)->getTypeValue();
  557. }
  558. bool AttributeImpl::operator<(const AttributeImpl &AI) const {
  559. if (this == &AI)
  560. return false;
  561. // This sorts the attributes with Attribute::AttrKinds coming first (sorted
  562. // relative to their enum value) and then strings.
  563. if (!isStringAttribute()) {
  564. if (AI.isStringAttribute())
  565. return true;
  566. if (getKindAsEnum() != AI.getKindAsEnum())
  567. return getKindAsEnum() < AI.getKindAsEnum();
  568. assert(!AI.isEnumAttribute() && "Non-unique attribute");
  569. assert(!AI.isTypeAttribute() && "Comparison of types would be unstable");
  570. // TODO: Is this actually needed?
  571. assert(AI.isIntAttribute() && "Only possibility left");
  572. return getValueAsInt() < AI.getValueAsInt();
  573. }
  574. if (!AI.isStringAttribute())
  575. return false;
  576. if (getKindAsString() == AI.getKindAsString())
  577. return getValueAsString() < AI.getValueAsString();
  578. return getKindAsString() < AI.getKindAsString();
  579. }
  580. //===----------------------------------------------------------------------===//
  581. // AttributeSet Definition
  582. //===----------------------------------------------------------------------===//
  583. AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
  584. return AttributeSet(AttributeSetNode::get(C, B));
  585. }
  586. AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
  587. return AttributeSet(AttributeSetNode::get(C, Attrs));
  588. }
  589. AttributeSet AttributeSet::addAttribute(LLVMContext &C,
  590. Attribute::AttrKind Kind) const {
  591. if (hasAttribute(Kind)) return *this;
  592. AttrBuilder B(C);
  593. B.addAttribute(Kind);
  594. return addAttributes(C, AttributeSet::get(C, B));
  595. }
  596. AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
  597. StringRef Value) const {
  598. AttrBuilder B(C);
  599. B.addAttribute(Kind, Value);
  600. return addAttributes(C, AttributeSet::get(C, B));
  601. }
  602. AttributeSet AttributeSet::addAttributes(LLVMContext &C,
  603. const AttributeSet AS) const {
  604. if (!hasAttributes())
  605. return AS;
  606. if (!AS.hasAttributes())
  607. return *this;
  608. AttrBuilder B(C, *this);
  609. B.merge(AttrBuilder(C, AS));
  610. return get(C, B);
  611. }
  612. AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
  613. Attribute::AttrKind Kind) const {
  614. if (!hasAttribute(Kind)) return *this;
  615. AttrBuilder B(C, *this);
  616. B.removeAttribute(Kind);
  617. return get(C, B);
  618. }
  619. AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
  620. StringRef Kind) const {
  621. if (!hasAttribute(Kind)) return *this;
  622. AttrBuilder B(C, *this);
  623. B.removeAttribute(Kind);
  624. return get(C, B);
  625. }
  626. AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
  627. const AttributeMask &Attrs) const {
  628. AttrBuilder B(C, *this);
  629. // If there is nothing to remove, directly return the original set.
  630. if (!B.overlaps(Attrs))
  631. return *this;
  632. B.remove(Attrs);
  633. return get(C, B);
  634. }
  635. unsigned AttributeSet::getNumAttributes() const {
  636. return SetNode ? SetNode->getNumAttributes() : 0;
  637. }
  638. bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
  639. return SetNode ? SetNode->hasAttribute(Kind) : false;
  640. }
  641. bool AttributeSet::hasAttribute(StringRef Kind) const {
  642. return SetNode ? SetNode->hasAttribute(Kind) : false;
  643. }
  644. Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
  645. return SetNode ? SetNode->getAttribute(Kind) : Attribute();
  646. }
  647. Attribute AttributeSet::getAttribute(StringRef Kind) const {
  648. return SetNode ? SetNode->getAttribute(Kind) : Attribute();
  649. }
  650. MaybeAlign AttributeSet::getAlignment() const {
  651. return SetNode ? SetNode->getAlignment() : std::nullopt;
  652. }
  653. MaybeAlign AttributeSet::getStackAlignment() const {
  654. return SetNode ? SetNode->getStackAlignment() : std::nullopt;
  655. }
  656. uint64_t AttributeSet::getDereferenceableBytes() const {
  657. return SetNode ? SetNode->getDereferenceableBytes() : 0;
  658. }
  659. uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
  660. return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
  661. }
  662. Type *AttributeSet::getByRefType() const {
  663. return SetNode ? SetNode->getAttributeType(Attribute::ByRef) : nullptr;
  664. }
  665. Type *AttributeSet::getByValType() const {
  666. return SetNode ? SetNode->getAttributeType(Attribute::ByVal) : nullptr;
  667. }
  668. Type *AttributeSet::getStructRetType() const {
  669. return SetNode ? SetNode->getAttributeType(Attribute::StructRet) : nullptr;
  670. }
  671. Type *AttributeSet::getPreallocatedType() const {
  672. return SetNode ? SetNode->getAttributeType(Attribute::Preallocated) : nullptr;
  673. }
  674. Type *AttributeSet::getInAllocaType() const {
  675. return SetNode ? SetNode->getAttributeType(Attribute::InAlloca) : nullptr;
  676. }
  677. Type *AttributeSet::getElementType() const {
  678. return SetNode ? SetNode->getAttributeType(Attribute::ElementType) : nullptr;
  679. }
  680. std::optional<std::pair<unsigned, std::optional<unsigned>>>
  681. AttributeSet::getAllocSizeArgs() const {
  682. if (SetNode)
  683. return SetNode->getAllocSizeArgs();
  684. return std::nullopt;
  685. }
  686. unsigned AttributeSet::getVScaleRangeMin() const {
  687. return SetNode ? SetNode->getVScaleRangeMin() : 1;
  688. }
  689. std::optional<unsigned> AttributeSet::getVScaleRangeMax() const {
  690. return SetNode ? SetNode->getVScaleRangeMax() : std::nullopt;
  691. }
  692. UWTableKind AttributeSet::getUWTableKind() const {
  693. return SetNode ? SetNode->getUWTableKind() : UWTableKind::None;
  694. }
  695. AllocFnKind AttributeSet::getAllocKind() const {
  696. return SetNode ? SetNode->getAllocKind() : AllocFnKind::Unknown;
  697. }
  698. MemoryEffects AttributeSet::getMemoryEffects() const {
  699. return SetNode ? SetNode->getMemoryEffects() : MemoryEffects::unknown();
  700. }
  701. std::string AttributeSet::getAsString(bool InAttrGrp) const {
  702. return SetNode ? SetNode->getAsString(InAttrGrp) : "";
  703. }
  704. bool AttributeSet::hasParentContext(LLVMContext &C) const {
  705. assert(hasAttributes() && "empty AttributeSet doesn't refer to any context");
  706. FoldingSetNodeID ID;
  707. SetNode->Profile(ID);
  708. void *Unused;
  709. return C.pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, Unused) == SetNode;
  710. }
  711. AttributeSet::iterator AttributeSet::begin() const {
  712. return SetNode ? SetNode->begin() : nullptr;
  713. }
  714. AttributeSet::iterator AttributeSet::end() const {
  715. return SetNode ? SetNode->end() : nullptr;
  716. }
  717. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  718. LLVM_DUMP_METHOD void AttributeSet::dump() const {
  719. dbgs() << "AS =\n";
  720. dbgs() << " { ";
  721. dbgs() << getAsString(true) << " }\n";
  722. }
  723. #endif
  724. //===----------------------------------------------------------------------===//
  725. // AttributeSetNode Definition
  726. //===----------------------------------------------------------------------===//
  727. AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
  728. : NumAttrs(Attrs.size()) {
  729. // There's memory after the node where we can store the entries in.
  730. llvm::copy(Attrs, getTrailingObjects<Attribute>());
  731. for (const auto &I : *this) {
  732. if (I.isStringAttribute())
  733. StringAttrs.insert({ I.getKindAsString(), I });
  734. else
  735. AvailableAttrs.addAttribute(I.getKindAsEnum());
  736. }
  737. }
  738. AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
  739. ArrayRef<Attribute> Attrs) {
  740. SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
  741. llvm::sort(SortedAttrs);
  742. return getSorted(C, SortedAttrs);
  743. }
  744. AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C,
  745. ArrayRef<Attribute> SortedAttrs) {
  746. if (SortedAttrs.empty())
  747. return nullptr;
  748. // Build a key to look up the existing attributes.
  749. LLVMContextImpl *pImpl = C.pImpl;
  750. FoldingSetNodeID ID;
  751. assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!");
  752. for (const auto &Attr : SortedAttrs)
  753. Attr.Profile(ID);
  754. void *InsertPoint;
  755. AttributeSetNode *PA =
  756. pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
  757. // If we didn't find any existing attributes of the same shape then create a
  758. // new one and insert it.
  759. if (!PA) {
  760. // Coallocate entries after the AttributeSetNode itself.
  761. void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
  762. PA = new (Mem) AttributeSetNode(SortedAttrs);
  763. pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
  764. }
  765. // Return the AttributeSetNode that we found or created.
  766. return PA;
  767. }
  768. AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
  769. return getSorted(C, B.attrs());
  770. }
  771. bool AttributeSetNode::hasAttribute(StringRef Kind) const {
  772. return StringAttrs.count(Kind);
  773. }
  774. std::optional<Attribute>
  775. AttributeSetNode::findEnumAttribute(Attribute::AttrKind Kind) const {
  776. // Do a quick presence check.
  777. if (!hasAttribute(Kind))
  778. return std::nullopt;
  779. // Attributes in a set are sorted by enum value, followed by string
  780. // attributes. Binary search the one we want.
  781. const Attribute *I =
  782. std::lower_bound(begin(), end() - StringAttrs.size(), Kind,
  783. [](Attribute A, Attribute::AttrKind Kind) {
  784. return A.getKindAsEnum() < Kind;
  785. });
  786. assert(I != end() && I->hasAttribute(Kind) && "Presence check failed?");
  787. return *I;
  788. }
  789. Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
  790. if (auto A = findEnumAttribute(Kind))
  791. return *A;
  792. return {};
  793. }
  794. Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
  795. return StringAttrs.lookup(Kind);
  796. }
  797. MaybeAlign AttributeSetNode::getAlignment() const {
  798. if (auto A = findEnumAttribute(Attribute::Alignment))
  799. return A->getAlignment();
  800. return std::nullopt;
  801. }
  802. MaybeAlign AttributeSetNode::getStackAlignment() const {
  803. if (auto A = findEnumAttribute(Attribute::StackAlignment))
  804. return A->getStackAlignment();
  805. return std::nullopt;
  806. }
  807. Type *AttributeSetNode::getAttributeType(Attribute::AttrKind Kind) const {
  808. if (auto A = findEnumAttribute(Kind))
  809. return A->getValueAsType();
  810. return nullptr;
  811. }
  812. uint64_t AttributeSetNode::getDereferenceableBytes() const {
  813. if (auto A = findEnumAttribute(Attribute::Dereferenceable))
  814. return A->getDereferenceableBytes();
  815. return 0;
  816. }
  817. uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
  818. if (auto A = findEnumAttribute(Attribute::DereferenceableOrNull))
  819. return A->getDereferenceableOrNullBytes();
  820. return 0;
  821. }
  822. std::optional<std::pair<unsigned, std::optional<unsigned>>>
  823. AttributeSetNode::getAllocSizeArgs() const {
  824. if (auto A = findEnumAttribute(Attribute::AllocSize))
  825. return A->getAllocSizeArgs();
  826. return std::nullopt;
  827. }
  828. unsigned AttributeSetNode::getVScaleRangeMin() const {
  829. if (auto A = findEnumAttribute(Attribute::VScaleRange))
  830. return A->getVScaleRangeMin();
  831. return 1;
  832. }
  833. std::optional<unsigned> AttributeSetNode::getVScaleRangeMax() const {
  834. if (auto A = findEnumAttribute(Attribute::VScaleRange))
  835. return A->getVScaleRangeMax();
  836. return std::nullopt;
  837. }
  838. UWTableKind AttributeSetNode::getUWTableKind() const {
  839. if (auto A = findEnumAttribute(Attribute::UWTable))
  840. return A->getUWTableKind();
  841. return UWTableKind::None;
  842. }
  843. AllocFnKind AttributeSetNode::getAllocKind() const {
  844. if (auto A = findEnumAttribute(Attribute::AllocKind))
  845. return A->getAllocKind();
  846. return AllocFnKind::Unknown;
  847. }
  848. MemoryEffects AttributeSetNode::getMemoryEffects() const {
  849. if (auto A = findEnumAttribute(Attribute::Memory))
  850. return A->getMemoryEffects();
  851. return MemoryEffects::unknown();
  852. }
  853. std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
  854. std::string Str;
  855. for (iterator I = begin(), E = end(); I != E; ++I) {
  856. if (I != begin())
  857. Str += ' ';
  858. Str += I->getAsString(InAttrGrp);
  859. }
  860. return Str;
  861. }
  862. //===----------------------------------------------------------------------===//
  863. // AttributeListImpl Definition
  864. //===----------------------------------------------------------------------===//
  865. /// Map from AttributeList index to the internal array index. Adding one happens
  866. /// to work, because -1 wraps around to 0.
  867. static unsigned attrIdxToArrayIdx(unsigned Index) {
  868. return Index + 1;
  869. }
  870. AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets)
  871. : NumAttrSets(Sets.size()) {
  872. assert(!Sets.empty() && "pointless AttributeListImpl");
  873. // There's memory after the node where we can store the entries in.
  874. llvm::copy(Sets, getTrailingObjects<AttributeSet>());
  875. // Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs
  876. // summary bitsets.
  877. for (const auto &I : Sets[attrIdxToArrayIdx(AttributeList::FunctionIndex)])
  878. if (!I.isStringAttribute())
  879. AvailableFunctionAttrs.addAttribute(I.getKindAsEnum());
  880. for (const auto &Set : Sets)
  881. for (const auto &I : Set)
  882. if (!I.isStringAttribute())
  883. AvailableSomewhereAttrs.addAttribute(I.getKindAsEnum());
  884. }
  885. void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
  886. Profile(ID, ArrayRef(begin(), end()));
  887. }
  888. void AttributeListImpl::Profile(FoldingSetNodeID &ID,
  889. ArrayRef<AttributeSet> Sets) {
  890. for (const auto &Set : Sets)
  891. ID.AddPointer(Set.SetNode);
  892. }
  893. bool AttributeListImpl::hasAttrSomewhere(Attribute::AttrKind Kind,
  894. unsigned *Index) const {
  895. if (!AvailableSomewhereAttrs.hasAttribute(Kind))
  896. return false;
  897. if (Index) {
  898. for (unsigned I = 0, E = NumAttrSets; I != E; ++I) {
  899. if (begin()[I].hasAttribute(Kind)) {
  900. *Index = I - 1;
  901. break;
  902. }
  903. }
  904. }
  905. return true;
  906. }
  907. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  908. LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
  909. AttributeList(const_cast<AttributeListImpl *>(this)).dump();
  910. }
  911. #endif
  912. //===----------------------------------------------------------------------===//
  913. // AttributeList Construction and Mutation Methods
  914. //===----------------------------------------------------------------------===//
  915. AttributeList AttributeList::getImpl(LLVMContext &C,
  916. ArrayRef<AttributeSet> AttrSets) {
  917. assert(!AttrSets.empty() && "pointless AttributeListImpl");
  918. LLVMContextImpl *pImpl = C.pImpl;
  919. FoldingSetNodeID ID;
  920. AttributeListImpl::Profile(ID, AttrSets);
  921. void *InsertPoint;
  922. AttributeListImpl *PA =
  923. pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
  924. // If we didn't find any existing attributes of the same shape then
  925. // create a new one and insert it.
  926. if (!PA) {
  927. // Coallocate entries after the AttributeListImpl itself.
  928. void *Mem = pImpl->Alloc.Allocate(
  929. AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()),
  930. alignof(AttributeListImpl));
  931. PA = new (Mem) AttributeListImpl(AttrSets);
  932. pImpl->AttrsLists.InsertNode(PA, InsertPoint);
  933. }
  934. // Return the AttributesList that we found or created.
  935. return AttributeList(PA);
  936. }
  937. AttributeList
  938. AttributeList::get(LLVMContext &C,
  939. ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
  940. // If there are no attributes then return a null AttributesList pointer.
  941. if (Attrs.empty())
  942. return {};
  943. assert(llvm::is_sorted(Attrs, llvm::less_first()) &&
  944. "Misordered Attributes list!");
  945. assert(llvm::all_of(Attrs,
  946. [](const std::pair<unsigned, Attribute> &Pair) {
  947. return Pair.second.isValid();
  948. }) &&
  949. "Pointless attribute!");
  950. // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
  951. // list.
  952. SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
  953. for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
  954. E = Attrs.end(); I != E; ) {
  955. unsigned Index = I->first;
  956. SmallVector<Attribute, 4> AttrVec;
  957. while (I != E && I->first == Index) {
  958. AttrVec.push_back(I->second);
  959. ++I;
  960. }
  961. AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
  962. }
  963. return get(C, AttrPairVec);
  964. }
  965. AttributeList
  966. AttributeList::get(LLVMContext &C,
  967. ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
  968. // If there are no attributes then return a null AttributesList pointer.
  969. if (Attrs.empty())
  970. return {};
  971. assert(llvm::is_sorted(Attrs, llvm::less_first()) &&
  972. "Misordered Attributes list!");
  973. assert(llvm::none_of(Attrs,
  974. [](const std::pair<unsigned, AttributeSet> &Pair) {
  975. return !Pair.second.hasAttributes();
  976. }) &&
  977. "Pointless attribute!");
  978. unsigned MaxIndex = Attrs.back().first;
  979. // If the MaxIndex is FunctionIndex and there are other indices in front
  980. // of it, we need to use the largest of those to get the right size.
  981. if (MaxIndex == FunctionIndex && Attrs.size() > 1)
  982. MaxIndex = Attrs[Attrs.size() - 2].first;
  983. SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
  984. for (const auto &Pair : Attrs)
  985. AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
  986. return getImpl(C, AttrVec);
  987. }
  988. AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
  989. AttributeSet RetAttrs,
  990. ArrayRef<AttributeSet> ArgAttrs) {
  991. // Scan from the end to find the last argument with attributes. Most
  992. // arguments don't have attributes, so it's nice if we can have fewer unique
  993. // AttributeListImpls by dropping empty attribute sets at the end of the list.
  994. unsigned NumSets = 0;
  995. for (size_t I = ArgAttrs.size(); I != 0; --I) {
  996. if (ArgAttrs[I - 1].hasAttributes()) {
  997. NumSets = I + 2;
  998. break;
  999. }
  1000. }
  1001. if (NumSets == 0) {
  1002. // Check function and return attributes if we didn't have argument
  1003. // attributes.
  1004. if (RetAttrs.hasAttributes())
  1005. NumSets = 2;
  1006. else if (FnAttrs.hasAttributes())
  1007. NumSets = 1;
  1008. }
  1009. // If all attribute sets were empty, we can use the empty attribute list.
  1010. if (NumSets == 0)
  1011. return {};
  1012. SmallVector<AttributeSet, 8> AttrSets;
  1013. AttrSets.reserve(NumSets);
  1014. // If we have any attributes, we always have function attributes.
  1015. AttrSets.push_back(FnAttrs);
  1016. if (NumSets > 1)
  1017. AttrSets.push_back(RetAttrs);
  1018. if (NumSets > 2) {
  1019. // Drop the empty argument attribute sets at the end.
  1020. ArgAttrs = ArgAttrs.take_front(NumSets - 2);
  1021. llvm::append_range(AttrSets, ArgAttrs);
  1022. }
  1023. return getImpl(C, AttrSets);
  1024. }
  1025. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  1026. AttributeSet Attrs) {
  1027. if (!Attrs.hasAttributes())
  1028. return {};
  1029. Index = attrIdxToArrayIdx(Index);
  1030. SmallVector<AttributeSet, 8> AttrSets(Index + 1);
  1031. AttrSets[Index] = Attrs;
  1032. return getImpl(C, AttrSets);
  1033. }
  1034. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  1035. const AttrBuilder &B) {
  1036. return get(C, Index, AttributeSet::get(C, B));
  1037. }
  1038. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  1039. ArrayRef<Attribute::AttrKind> Kinds) {
  1040. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  1041. for (const auto K : Kinds)
  1042. Attrs.emplace_back(Index, Attribute::get(C, K));
  1043. return get(C, Attrs);
  1044. }
  1045. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  1046. ArrayRef<Attribute::AttrKind> Kinds,
  1047. ArrayRef<uint64_t> Values) {
  1048. assert(Kinds.size() == Values.size() && "Mismatched attribute values.");
  1049. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  1050. auto VI = Values.begin();
  1051. for (const auto K : Kinds)
  1052. Attrs.emplace_back(Index, Attribute::get(C, K, *VI++));
  1053. return get(C, Attrs);
  1054. }
  1055. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  1056. ArrayRef<StringRef> Kinds) {
  1057. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  1058. for (const auto &K : Kinds)
  1059. Attrs.emplace_back(Index, Attribute::get(C, K));
  1060. return get(C, Attrs);
  1061. }
  1062. AttributeList AttributeList::get(LLVMContext &C,
  1063. ArrayRef<AttributeList> Attrs) {
  1064. if (Attrs.empty())
  1065. return {};
  1066. if (Attrs.size() == 1)
  1067. return Attrs[0];
  1068. unsigned MaxSize = 0;
  1069. for (const auto &List : Attrs)
  1070. MaxSize = std::max(MaxSize, List.getNumAttrSets());
  1071. // If every list was empty, there is no point in merging the lists.
  1072. if (MaxSize == 0)
  1073. return {};
  1074. SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
  1075. for (unsigned I = 0; I < MaxSize; ++I) {
  1076. AttrBuilder CurBuilder(C);
  1077. for (const auto &List : Attrs)
  1078. CurBuilder.merge(AttrBuilder(C, List.getAttributes(I - 1)));
  1079. NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
  1080. }
  1081. return getImpl(C, NewAttrSets);
  1082. }
  1083. AttributeList
  1084. AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
  1085. Attribute::AttrKind Kind) const {
  1086. AttributeSet Attrs = getAttributes(Index);
  1087. if (Attrs.hasAttribute(Kind))
  1088. return *this;
  1089. // TODO: Insert at correct position and avoid sort.
  1090. SmallVector<Attribute, 8> NewAttrs(Attrs.begin(), Attrs.end());
  1091. NewAttrs.push_back(Attribute::get(C, Kind));
  1092. return setAttributesAtIndex(C, Index, AttributeSet::get(C, NewAttrs));
  1093. }
  1094. AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
  1095. StringRef Kind,
  1096. StringRef Value) const {
  1097. AttrBuilder B(C);
  1098. B.addAttribute(Kind, Value);
  1099. return addAttributesAtIndex(C, Index, B);
  1100. }
  1101. AttributeList AttributeList::addAttributeAtIndex(LLVMContext &C, unsigned Index,
  1102. Attribute A) const {
  1103. AttrBuilder B(C);
  1104. B.addAttribute(A);
  1105. return addAttributesAtIndex(C, Index, B);
  1106. }
  1107. AttributeList AttributeList::setAttributesAtIndex(LLVMContext &C,
  1108. unsigned Index,
  1109. AttributeSet Attrs) const {
  1110. Index = attrIdxToArrayIdx(Index);
  1111. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1112. if (Index >= AttrSets.size())
  1113. AttrSets.resize(Index + 1);
  1114. AttrSets[Index] = Attrs;
  1115. // Remove trailing empty attribute sets.
  1116. while (!AttrSets.empty() && !AttrSets.back().hasAttributes())
  1117. AttrSets.pop_back();
  1118. if (AttrSets.empty())
  1119. return {};
  1120. return AttributeList::getImpl(C, AttrSets);
  1121. }
  1122. AttributeList AttributeList::addAttributesAtIndex(LLVMContext &C,
  1123. unsigned Index,
  1124. const AttrBuilder &B) const {
  1125. if (!B.hasAttributes())
  1126. return *this;
  1127. if (!pImpl)
  1128. return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
  1129. AttrBuilder Merged(C, getAttributes(Index));
  1130. Merged.merge(B);
  1131. return setAttributesAtIndex(C, Index, AttributeSet::get(C, Merged));
  1132. }
  1133. AttributeList AttributeList::addParamAttribute(LLVMContext &C,
  1134. ArrayRef<unsigned> ArgNos,
  1135. Attribute A) const {
  1136. assert(llvm::is_sorted(ArgNos));
  1137. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1138. unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
  1139. if (MaxIndex >= AttrSets.size())
  1140. AttrSets.resize(MaxIndex + 1);
  1141. for (unsigned ArgNo : ArgNos) {
  1142. unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
  1143. AttrBuilder B(C, AttrSets[Index]);
  1144. B.addAttribute(A);
  1145. AttrSets[Index] = AttributeSet::get(C, B);
  1146. }
  1147. return getImpl(C, AttrSets);
  1148. }
  1149. AttributeList
  1150. AttributeList::removeAttributeAtIndex(LLVMContext &C, unsigned Index,
  1151. Attribute::AttrKind Kind) const {
  1152. AttributeSet Attrs = getAttributes(Index);
  1153. AttributeSet NewAttrs = Attrs.removeAttribute(C, Kind);
  1154. if (Attrs == NewAttrs)
  1155. return *this;
  1156. return setAttributesAtIndex(C, Index, NewAttrs);
  1157. }
  1158. AttributeList AttributeList::removeAttributeAtIndex(LLVMContext &C,
  1159. unsigned Index,
  1160. StringRef Kind) const {
  1161. AttributeSet Attrs = getAttributes(Index);
  1162. AttributeSet NewAttrs = Attrs.removeAttribute(C, Kind);
  1163. if (Attrs == NewAttrs)
  1164. return *this;
  1165. return setAttributesAtIndex(C, Index, NewAttrs);
  1166. }
  1167. AttributeList AttributeList::removeAttributesAtIndex(
  1168. LLVMContext &C, unsigned Index, const AttributeMask &AttrsToRemove) const {
  1169. AttributeSet Attrs = getAttributes(Index);
  1170. AttributeSet NewAttrs = Attrs.removeAttributes(C, AttrsToRemove);
  1171. // If nothing was removed, return the original list.
  1172. if (Attrs == NewAttrs)
  1173. return *this;
  1174. return setAttributesAtIndex(C, Index, NewAttrs);
  1175. }
  1176. AttributeList
  1177. AttributeList::removeAttributesAtIndex(LLVMContext &C,
  1178. unsigned WithoutIndex) const {
  1179. if (!pImpl)
  1180. return {};
  1181. if (attrIdxToArrayIdx(WithoutIndex) >= getNumAttrSets())
  1182. return *this;
  1183. return setAttributesAtIndex(C, WithoutIndex, AttributeSet());
  1184. }
  1185. AttributeList AttributeList::addDereferenceableRetAttr(LLVMContext &C,
  1186. uint64_t Bytes) const {
  1187. AttrBuilder B(C);
  1188. B.addDereferenceableAttr(Bytes);
  1189. return addRetAttributes(C, B);
  1190. }
  1191. AttributeList AttributeList::addDereferenceableParamAttr(LLVMContext &C,
  1192. unsigned Index,
  1193. uint64_t Bytes) const {
  1194. AttrBuilder B(C);
  1195. B.addDereferenceableAttr(Bytes);
  1196. return addParamAttributes(C, Index, B);
  1197. }
  1198. AttributeList
  1199. AttributeList::addDereferenceableOrNullParamAttr(LLVMContext &C, unsigned Index,
  1200. uint64_t Bytes) const {
  1201. AttrBuilder B(C);
  1202. B.addDereferenceableOrNullAttr(Bytes);
  1203. return addParamAttributes(C, Index, B);
  1204. }
  1205. AttributeList AttributeList::addAllocSizeParamAttr(
  1206. LLVMContext &C, unsigned Index, unsigned ElemSizeArg,
  1207. const std::optional<unsigned> &NumElemsArg) {
  1208. AttrBuilder B(C);
  1209. B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
  1210. return addParamAttributes(C, Index, B);
  1211. }
  1212. //===----------------------------------------------------------------------===//
  1213. // AttributeList Accessor Methods
  1214. //===----------------------------------------------------------------------===//
  1215. AttributeSet AttributeList::getParamAttrs(unsigned ArgNo) const {
  1216. return getAttributes(ArgNo + FirstArgIndex);
  1217. }
  1218. AttributeSet AttributeList::getRetAttrs() const {
  1219. return getAttributes(ReturnIndex);
  1220. }
  1221. AttributeSet AttributeList::getFnAttrs() const {
  1222. return getAttributes(FunctionIndex);
  1223. }
  1224. bool AttributeList::hasAttributeAtIndex(unsigned Index,
  1225. Attribute::AttrKind Kind) const {
  1226. return getAttributes(Index).hasAttribute(Kind);
  1227. }
  1228. bool AttributeList::hasAttributeAtIndex(unsigned Index, StringRef Kind) const {
  1229. return getAttributes(Index).hasAttribute(Kind);
  1230. }
  1231. bool AttributeList::hasAttributesAtIndex(unsigned Index) const {
  1232. return getAttributes(Index).hasAttributes();
  1233. }
  1234. bool AttributeList::hasFnAttr(Attribute::AttrKind Kind) const {
  1235. return pImpl && pImpl->hasFnAttribute(Kind);
  1236. }
  1237. bool AttributeList::hasFnAttr(StringRef Kind) const {
  1238. return hasAttributeAtIndex(AttributeList::FunctionIndex, Kind);
  1239. }
  1240. bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
  1241. unsigned *Index) const {
  1242. return pImpl && pImpl->hasAttrSomewhere(Attr, Index);
  1243. }
  1244. Attribute AttributeList::getAttributeAtIndex(unsigned Index,
  1245. Attribute::AttrKind Kind) const {
  1246. return getAttributes(Index).getAttribute(Kind);
  1247. }
  1248. Attribute AttributeList::getAttributeAtIndex(unsigned Index,
  1249. StringRef Kind) const {
  1250. return getAttributes(Index).getAttribute(Kind);
  1251. }
  1252. MaybeAlign AttributeList::getRetAlignment() const {
  1253. return getAttributes(ReturnIndex).getAlignment();
  1254. }
  1255. MaybeAlign AttributeList::getParamAlignment(unsigned ArgNo) const {
  1256. return getAttributes(ArgNo + FirstArgIndex).getAlignment();
  1257. }
  1258. MaybeAlign AttributeList::getParamStackAlignment(unsigned ArgNo) const {
  1259. return getAttributes(ArgNo + FirstArgIndex).getStackAlignment();
  1260. }
  1261. Type *AttributeList::getParamByValType(unsigned Index) const {
  1262. return getAttributes(Index+FirstArgIndex).getByValType();
  1263. }
  1264. Type *AttributeList::getParamStructRetType(unsigned Index) const {
  1265. return getAttributes(Index + FirstArgIndex).getStructRetType();
  1266. }
  1267. Type *AttributeList::getParamByRefType(unsigned Index) const {
  1268. return getAttributes(Index + FirstArgIndex).getByRefType();
  1269. }
  1270. Type *AttributeList::getParamPreallocatedType(unsigned Index) const {
  1271. return getAttributes(Index + FirstArgIndex).getPreallocatedType();
  1272. }
  1273. Type *AttributeList::getParamInAllocaType(unsigned Index) const {
  1274. return getAttributes(Index + FirstArgIndex).getInAllocaType();
  1275. }
  1276. Type *AttributeList::getParamElementType(unsigned Index) const {
  1277. return getAttributes(Index + FirstArgIndex).getElementType();
  1278. }
  1279. MaybeAlign AttributeList::getFnStackAlignment() const {
  1280. return getFnAttrs().getStackAlignment();
  1281. }
  1282. MaybeAlign AttributeList::getRetStackAlignment() const {
  1283. return getRetAttrs().getStackAlignment();
  1284. }
  1285. uint64_t AttributeList::getRetDereferenceableBytes() const {
  1286. return getRetAttrs().getDereferenceableBytes();
  1287. }
  1288. uint64_t AttributeList::getParamDereferenceableBytes(unsigned Index) const {
  1289. return getParamAttrs(Index).getDereferenceableBytes();
  1290. }
  1291. uint64_t AttributeList::getRetDereferenceableOrNullBytes() const {
  1292. return getRetAttrs().getDereferenceableOrNullBytes();
  1293. }
  1294. uint64_t
  1295. AttributeList::getParamDereferenceableOrNullBytes(unsigned Index) const {
  1296. return getParamAttrs(Index).getDereferenceableOrNullBytes();
  1297. }
  1298. UWTableKind AttributeList::getUWTableKind() const {
  1299. return getFnAttrs().getUWTableKind();
  1300. }
  1301. AllocFnKind AttributeList::getAllocKind() const {
  1302. return getFnAttrs().getAllocKind();
  1303. }
  1304. MemoryEffects AttributeList::getMemoryEffects() const {
  1305. return getFnAttrs().getMemoryEffects();
  1306. }
  1307. std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
  1308. return getAttributes(Index).getAsString(InAttrGrp);
  1309. }
  1310. AttributeSet AttributeList::getAttributes(unsigned Index) const {
  1311. Index = attrIdxToArrayIdx(Index);
  1312. if (!pImpl || Index >= getNumAttrSets())
  1313. return {};
  1314. return pImpl->begin()[Index];
  1315. }
  1316. bool AttributeList::hasParentContext(LLVMContext &C) const {
  1317. assert(!isEmpty() && "an empty attribute list has no parent context");
  1318. FoldingSetNodeID ID;
  1319. pImpl->Profile(ID);
  1320. void *Unused;
  1321. return C.pImpl->AttrsLists.FindNodeOrInsertPos(ID, Unused) == pImpl;
  1322. }
  1323. AttributeList::iterator AttributeList::begin() const {
  1324. return pImpl ? pImpl->begin() : nullptr;
  1325. }
  1326. AttributeList::iterator AttributeList::end() const {
  1327. return pImpl ? pImpl->end() : nullptr;
  1328. }
  1329. //===----------------------------------------------------------------------===//
  1330. // AttributeList Introspection Methods
  1331. //===----------------------------------------------------------------------===//
  1332. unsigned AttributeList::getNumAttrSets() const {
  1333. return pImpl ? pImpl->NumAttrSets : 0;
  1334. }
  1335. void AttributeList::print(raw_ostream &O) const {
  1336. O << "AttributeList[\n";
  1337. for (unsigned i : indexes()) {
  1338. if (!getAttributes(i).hasAttributes())
  1339. continue;
  1340. O << " { ";
  1341. switch (i) {
  1342. case AttrIndex::ReturnIndex:
  1343. O << "return";
  1344. break;
  1345. case AttrIndex::FunctionIndex:
  1346. O << "function";
  1347. break;
  1348. default:
  1349. O << "arg(" << i - AttrIndex::FirstArgIndex << ")";
  1350. }
  1351. O << " => " << getAsString(i) << " }\n";
  1352. }
  1353. O << "]\n";
  1354. }
  1355. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  1356. LLVM_DUMP_METHOD void AttributeList::dump() const { print(dbgs()); }
  1357. #endif
  1358. //===----------------------------------------------------------------------===//
  1359. // AttrBuilder Method Implementations
  1360. //===----------------------------------------------------------------------===//
  1361. AttrBuilder::AttrBuilder(LLVMContext &Ctx, AttributeSet AS) : Ctx(Ctx) {
  1362. append_range(Attrs, AS);
  1363. assert(is_sorted(Attrs) && "AttributeSet should be sorted");
  1364. }
  1365. void AttrBuilder::clear() { Attrs.clear(); }
  1366. /// Attribute comparator that only compares attribute keys. Enum attributes are
  1367. /// sorted before string attributes.
  1368. struct AttributeComparator {
  1369. bool operator()(Attribute A0, Attribute A1) const {
  1370. bool A0IsString = A0.isStringAttribute();
  1371. bool A1IsString = A1.isStringAttribute();
  1372. if (A0IsString) {
  1373. if (A1IsString)
  1374. return A0.getKindAsString() < A1.getKindAsString();
  1375. else
  1376. return false;
  1377. }
  1378. if (A1IsString)
  1379. return true;
  1380. return A0.getKindAsEnum() < A1.getKindAsEnum();
  1381. }
  1382. bool operator()(Attribute A0, Attribute::AttrKind Kind) const {
  1383. if (A0.isStringAttribute())
  1384. return false;
  1385. return A0.getKindAsEnum() < Kind;
  1386. }
  1387. bool operator()(Attribute A0, StringRef Kind) const {
  1388. if (A0.isStringAttribute())
  1389. return A0.getKindAsString() < Kind;
  1390. return true;
  1391. }
  1392. };
  1393. template <typename K>
  1394. static void addAttributeImpl(SmallVectorImpl<Attribute> &Attrs, K Kind,
  1395. Attribute Attr) {
  1396. auto It = lower_bound(Attrs, Kind, AttributeComparator());
  1397. if (It != Attrs.end() && It->hasAttribute(Kind))
  1398. std::swap(*It, Attr);
  1399. else
  1400. Attrs.insert(It, Attr);
  1401. }
  1402. AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
  1403. if (Attr.isStringAttribute())
  1404. addAttributeImpl(Attrs, Attr.getKindAsString(), Attr);
  1405. else
  1406. addAttributeImpl(Attrs, Attr.getKindAsEnum(), Attr);
  1407. return *this;
  1408. }
  1409. AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Kind) {
  1410. addAttributeImpl(Attrs, Kind, Attribute::get(Ctx, Kind));
  1411. return *this;
  1412. }
  1413. AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
  1414. addAttributeImpl(Attrs, A, Attribute::get(Ctx, A, V));
  1415. return *this;
  1416. }
  1417. AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
  1418. assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
  1419. auto It = lower_bound(Attrs, Val, AttributeComparator());
  1420. if (It != Attrs.end() && It->hasAttribute(Val))
  1421. Attrs.erase(It);
  1422. return *this;
  1423. }
  1424. AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
  1425. auto It = lower_bound(Attrs, A, AttributeComparator());
  1426. if (It != Attrs.end() && It->hasAttribute(A))
  1427. Attrs.erase(It);
  1428. return *this;
  1429. }
  1430. std::optional<uint64_t>
  1431. AttrBuilder::getRawIntAttr(Attribute::AttrKind Kind) const {
  1432. assert(Attribute::isIntAttrKind(Kind) && "Not an int attribute");
  1433. Attribute A = getAttribute(Kind);
  1434. if (A.isValid())
  1435. return A.getValueAsInt();
  1436. return std::nullopt;
  1437. }
  1438. AttrBuilder &AttrBuilder::addRawIntAttr(Attribute::AttrKind Kind,
  1439. uint64_t Value) {
  1440. return addAttribute(Attribute::get(Ctx, Kind, Value));
  1441. }
  1442. std::optional<std::pair<unsigned, std::optional<unsigned>>>
  1443. AttrBuilder::getAllocSizeArgs() const {
  1444. Attribute A = getAttribute(Attribute::AllocSize);
  1445. if (A.isValid())
  1446. return A.getAllocSizeArgs();
  1447. return std::nullopt;
  1448. }
  1449. AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
  1450. if (!Align)
  1451. return *this;
  1452. assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large.");
  1453. return addRawIntAttr(Attribute::Alignment, Align->value());
  1454. }
  1455. AttrBuilder &AttrBuilder::addStackAlignmentAttr(MaybeAlign Align) {
  1456. // Default alignment, allow the target to define how to align it.
  1457. if (!Align)
  1458. return *this;
  1459. assert(*Align <= 0x100 && "Alignment too large.");
  1460. return addRawIntAttr(Attribute::StackAlignment, Align->value());
  1461. }
  1462. AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
  1463. if (Bytes == 0) return *this;
  1464. return addRawIntAttr(Attribute::Dereferenceable, Bytes);
  1465. }
  1466. AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
  1467. if (Bytes == 0)
  1468. return *this;
  1469. return addRawIntAttr(Attribute::DereferenceableOrNull, Bytes);
  1470. }
  1471. AttrBuilder &
  1472. AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
  1473. const std::optional<unsigned> &NumElems) {
  1474. return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
  1475. }
  1476. AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
  1477. // (0, 0) is our "not present" value, so we need to check for it here.
  1478. assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
  1479. return addRawIntAttr(Attribute::AllocSize, RawArgs);
  1480. }
  1481. AttrBuilder &AttrBuilder::addVScaleRangeAttr(unsigned MinValue,
  1482. std::optional<unsigned> MaxValue) {
  1483. return addVScaleRangeAttrFromRawRepr(packVScaleRangeArgs(MinValue, MaxValue));
  1484. }
  1485. AttrBuilder &AttrBuilder::addVScaleRangeAttrFromRawRepr(uint64_t RawArgs) {
  1486. // (0, 0) is not present hence ignore this case
  1487. if (RawArgs == 0)
  1488. return *this;
  1489. return addRawIntAttr(Attribute::VScaleRange, RawArgs);
  1490. }
  1491. AttrBuilder &AttrBuilder::addUWTableAttr(UWTableKind Kind) {
  1492. if (Kind == UWTableKind::None)
  1493. return *this;
  1494. return addRawIntAttr(Attribute::UWTable, uint64_t(Kind));
  1495. }
  1496. AttrBuilder &AttrBuilder::addMemoryAttr(MemoryEffects ME) {
  1497. return addRawIntAttr(Attribute::Memory, ME.toIntValue());
  1498. }
  1499. AttrBuilder &AttrBuilder::addAllocKindAttr(AllocFnKind Kind) {
  1500. return addRawIntAttr(Attribute::AllocKind, static_cast<uint64_t>(Kind));
  1501. }
  1502. Type *AttrBuilder::getTypeAttr(Attribute::AttrKind Kind) const {
  1503. assert(Attribute::isTypeAttrKind(Kind) && "Not a type attribute");
  1504. Attribute A = getAttribute(Kind);
  1505. return A.isValid() ? A.getValueAsType() : nullptr;
  1506. }
  1507. AttrBuilder &AttrBuilder::addTypeAttr(Attribute::AttrKind Kind, Type *Ty) {
  1508. return addAttribute(Attribute::get(Ctx, Kind, Ty));
  1509. }
  1510. AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) {
  1511. return addTypeAttr(Attribute::ByVal, Ty);
  1512. }
  1513. AttrBuilder &AttrBuilder::addStructRetAttr(Type *Ty) {
  1514. return addTypeAttr(Attribute::StructRet, Ty);
  1515. }
  1516. AttrBuilder &AttrBuilder::addByRefAttr(Type *Ty) {
  1517. return addTypeAttr(Attribute::ByRef, Ty);
  1518. }
  1519. AttrBuilder &AttrBuilder::addPreallocatedAttr(Type *Ty) {
  1520. return addTypeAttr(Attribute::Preallocated, Ty);
  1521. }
  1522. AttrBuilder &AttrBuilder::addInAllocaAttr(Type *Ty) {
  1523. return addTypeAttr(Attribute::InAlloca, Ty);
  1524. }
  1525. AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
  1526. // TODO: Could make this O(n) as we're merging two sorted lists.
  1527. for (const auto &I : B.attrs())
  1528. addAttribute(I);
  1529. return *this;
  1530. }
  1531. AttrBuilder &AttrBuilder::remove(const AttributeMask &AM) {
  1532. erase_if(Attrs, [&](Attribute A) { return AM.contains(A); });
  1533. return *this;
  1534. }
  1535. bool AttrBuilder::overlaps(const AttributeMask &AM) const {
  1536. return any_of(Attrs, [&](Attribute A) { return AM.contains(A); });
  1537. }
  1538. Attribute AttrBuilder::getAttribute(Attribute::AttrKind A) const {
  1539. assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
  1540. auto It = lower_bound(Attrs, A, AttributeComparator());
  1541. if (It != Attrs.end() && It->hasAttribute(A))
  1542. return *It;
  1543. return {};
  1544. }
  1545. Attribute AttrBuilder::getAttribute(StringRef A) const {
  1546. auto It = lower_bound(Attrs, A, AttributeComparator());
  1547. if (It != Attrs.end() && It->hasAttribute(A))
  1548. return *It;
  1549. return {};
  1550. }
  1551. bool AttrBuilder::contains(Attribute::AttrKind A) const {
  1552. return getAttribute(A).isValid();
  1553. }
  1554. bool AttrBuilder::contains(StringRef A) const {
  1555. return getAttribute(A).isValid();
  1556. }
  1557. bool AttrBuilder::operator==(const AttrBuilder &B) const {
  1558. return Attrs == B.Attrs;
  1559. }
  1560. //===----------------------------------------------------------------------===//
  1561. // AttributeFuncs Function Defintions
  1562. //===----------------------------------------------------------------------===//
  1563. /// Which attributes cannot be applied to a type.
  1564. AttributeMask AttributeFuncs::typeIncompatible(Type *Ty,
  1565. AttributeSafetyKind ASK) {
  1566. AttributeMask Incompatible;
  1567. if (!Ty->isIntegerTy()) {
  1568. // Attributes that only apply to integers.
  1569. if (ASK & ASK_SAFE_TO_DROP)
  1570. Incompatible.addAttribute(Attribute::AllocAlign);
  1571. if (ASK & ASK_UNSAFE_TO_DROP)
  1572. Incompatible.addAttribute(Attribute::SExt).addAttribute(Attribute::ZExt);
  1573. }
  1574. if (!Ty->isPointerTy()) {
  1575. // Attributes that only apply to pointers.
  1576. if (ASK & ASK_SAFE_TO_DROP)
  1577. Incompatible.addAttribute(Attribute::NoAlias)
  1578. .addAttribute(Attribute::NoCapture)
  1579. .addAttribute(Attribute::NonNull)
  1580. .addAttribute(Attribute::ReadNone)
  1581. .addAttribute(Attribute::ReadOnly)
  1582. .addAttribute(Attribute::Dereferenceable)
  1583. .addAttribute(Attribute::DereferenceableOrNull);
  1584. if (ASK & ASK_UNSAFE_TO_DROP)
  1585. Incompatible.addAttribute(Attribute::Nest)
  1586. .addAttribute(Attribute::SwiftError)
  1587. .addAttribute(Attribute::Preallocated)
  1588. .addAttribute(Attribute::InAlloca)
  1589. .addAttribute(Attribute::ByVal)
  1590. .addAttribute(Attribute::StructRet)
  1591. .addAttribute(Attribute::ByRef)
  1592. .addAttribute(Attribute::ElementType)
  1593. .addAttribute(Attribute::AllocatedPointer);
  1594. }
  1595. // Attributes that only apply to pointers or vectors of pointers.
  1596. if (!Ty->isPtrOrPtrVectorTy()) {
  1597. if (ASK & ASK_SAFE_TO_DROP)
  1598. Incompatible.addAttribute(Attribute::Alignment);
  1599. }
  1600. // Some attributes can apply to all "values" but there are no `void` values.
  1601. if (Ty->isVoidTy()) {
  1602. if (ASK & ASK_SAFE_TO_DROP)
  1603. Incompatible.addAttribute(Attribute::NoUndef);
  1604. }
  1605. return Incompatible;
  1606. }
  1607. AttributeMask AttributeFuncs::getUBImplyingAttributes() {
  1608. AttributeMask AM;
  1609. AM.addAttribute(Attribute::NoUndef);
  1610. AM.addAttribute(Attribute::Dereferenceable);
  1611. AM.addAttribute(Attribute::DereferenceableOrNull);
  1612. return AM;
  1613. }
  1614. template<typename AttrClass>
  1615. static bool isEqual(const Function &Caller, const Function &Callee) {
  1616. return Caller.getFnAttribute(AttrClass::getKind()) ==
  1617. Callee.getFnAttribute(AttrClass::getKind());
  1618. }
  1619. /// Compute the logical AND of the attributes of the caller and the
  1620. /// callee.
  1621. ///
  1622. /// This function sets the caller's attribute to false if the callee's attribute
  1623. /// is false.
  1624. template<typename AttrClass>
  1625. static void setAND(Function &Caller, const Function &Callee) {
  1626. if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
  1627. !AttrClass::isSet(Callee, AttrClass::getKind()))
  1628. AttrClass::set(Caller, AttrClass::getKind(), false);
  1629. }
  1630. /// Compute the logical OR of the attributes of the caller and the
  1631. /// callee.
  1632. ///
  1633. /// This function sets the caller's attribute to true if the callee's attribute
  1634. /// is true.
  1635. template<typename AttrClass>
  1636. static void setOR(Function &Caller, const Function &Callee) {
  1637. if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
  1638. AttrClass::isSet(Callee, AttrClass::getKind()))
  1639. AttrClass::set(Caller, AttrClass::getKind(), true);
  1640. }
  1641. /// If the inlined function had a higher stack protection level than the
  1642. /// calling function, then bump up the caller's stack protection level.
  1643. static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
  1644. // If the calling function has *no* stack protection level (e.g. it was built
  1645. // with Clang's -fno-stack-protector or no_stack_protector attribute), don't
  1646. // change it as that could change the program's semantics.
  1647. if (!Caller.hasStackProtectorFnAttr())
  1648. return;
  1649. // If upgrading the SSP attribute, clear out the old SSP Attributes first.
  1650. // Having multiple SSP attributes doesn't actually hurt, but it adds useless
  1651. // clutter to the IR.
  1652. AttributeMask OldSSPAttr;
  1653. OldSSPAttr.addAttribute(Attribute::StackProtect)
  1654. .addAttribute(Attribute::StackProtectStrong)
  1655. .addAttribute(Attribute::StackProtectReq);
  1656. if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
  1657. Caller.removeFnAttrs(OldSSPAttr);
  1658. Caller.addFnAttr(Attribute::StackProtectReq);
  1659. } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
  1660. !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
  1661. Caller.removeFnAttrs(OldSSPAttr);
  1662. Caller.addFnAttr(Attribute::StackProtectStrong);
  1663. } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
  1664. !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
  1665. !Caller.hasFnAttribute(Attribute::StackProtectStrong))
  1666. Caller.addFnAttr(Attribute::StackProtect);
  1667. }
  1668. /// If the inlined function required stack probes, then ensure that
  1669. /// the calling function has those too.
  1670. static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
  1671. if (!Caller.hasFnAttribute("probe-stack") &&
  1672. Callee.hasFnAttribute("probe-stack")) {
  1673. Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
  1674. }
  1675. }
  1676. /// If the inlined function defines the size of guard region
  1677. /// on the stack, then ensure that the calling function defines a guard region
  1678. /// that is no larger.
  1679. static void
  1680. adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
  1681. Attribute CalleeAttr = Callee.getFnAttribute("stack-probe-size");
  1682. if (CalleeAttr.isValid()) {
  1683. Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size");
  1684. if (CallerAttr.isValid()) {
  1685. uint64_t CallerStackProbeSize, CalleeStackProbeSize;
  1686. CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize);
  1687. CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize);
  1688. if (CallerStackProbeSize > CalleeStackProbeSize) {
  1689. Caller.addFnAttr(CalleeAttr);
  1690. }
  1691. } else {
  1692. Caller.addFnAttr(CalleeAttr);
  1693. }
  1694. }
  1695. }
  1696. /// If the inlined function defines a min legal vector width, then ensure
  1697. /// the calling function has the same or larger min legal vector width. If the
  1698. /// caller has the attribute, but the callee doesn't, we need to remove the
  1699. /// attribute from the caller since we can't make any guarantees about the
  1700. /// caller's requirements.
  1701. /// This function is called after the inlining decision has been made so we have
  1702. /// to merge the attribute this way. Heuristics that would use
  1703. /// min-legal-vector-width to determine inline compatibility would need to be
  1704. /// handled as part of inline cost analysis.
  1705. static void
  1706. adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
  1707. Attribute CallerAttr = Caller.getFnAttribute("min-legal-vector-width");
  1708. if (CallerAttr.isValid()) {
  1709. Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width");
  1710. if (CalleeAttr.isValid()) {
  1711. uint64_t CallerVectorWidth, CalleeVectorWidth;
  1712. CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth);
  1713. CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth);
  1714. if (CallerVectorWidth < CalleeVectorWidth)
  1715. Caller.addFnAttr(CalleeAttr);
  1716. } else {
  1717. // If the callee doesn't have the attribute then we don't know anything
  1718. // and must drop the attribute from the caller.
  1719. Caller.removeFnAttr("min-legal-vector-width");
  1720. }
  1721. }
  1722. }
  1723. /// If the inlined function has null_pointer_is_valid attribute,
  1724. /// set this attribute in the caller post inlining.
  1725. static void
  1726. adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
  1727. if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
  1728. Caller.addFnAttr(Attribute::NullPointerIsValid);
  1729. }
  1730. }
  1731. struct EnumAttr {
  1732. static bool isSet(const Function &Fn,
  1733. Attribute::AttrKind Kind) {
  1734. return Fn.hasFnAttribute(Kind);
  1735. }
  1736. static void set(Function &Fn,
  1737. Attribute::AttrKind Kind, bool Val) {
  1738. if (Val)
  1739. Fn.addFnAttr(Kind);
  1740. else
  1741. Fn.removeFnAttr(Kind);
  1742. }
  1743. };
  1744. struct StrBoolAttr {
  1745. static bool isSet(const Function &Fn,
  1746. StringRef Kind) {
  1747. auto A = Fn.getFnAttribute(Kind);
  1748. return A.getValueAsString().equals("true");
  1749. }
  1750. static void set(Function &Fn,
  1751. StringRef Kind, bool Val) {
  1752. Fn.addFnAttr(Kind, Val ? "true" : "false");
  1753. }
  1754. };
  1755. #define GET_ATTR_NAMES
  1756. #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
  1757. struct ENUM_NAME##Attr : EnumAttr { \
  1758. static enum Attribute::AttrKind getKind() { \
  1759. return llvm::Attribute::ENUM_NAME; \
  1760. } \
  1761. };
  1762. #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
  1763. struct ENUM_NAME##Attr : StrBoolAttr { \
  1764. static StringRef getKind() { return #DISPLAY_NAME; } \
  1765. };
  1766. #include "llvm/IR/Attributes.inc"
  1767. #define GET_ATTR_COMPAT_FUNC
  1768. #include "llvm/IR/Attributes.inc"
  1769. bool AttributeFuncs::areInlineCompatible(const Function &Caller,
  1770. const Function &Callee) {
  1771. return hasCompatibleFnAttrs(Caller, Callee);
  1772. }
  1773. bool AttributeFuncs::areOutlineCompatible(const Function &A,
  1774. const Function &B) {
  1775. return hasCompatibleFnAttrs(A, B);
  1776. }
  1777. void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
  1778. const Function &Callee) {
  1779. mergeFnAttrs(Caller, Callee);
  1780. }
  1781. void AttributeFuncs::mergeAttributesForOutlining(Function &Base,
  1782. const Function &ToMerge) {
  1783. // We merge functions so that they meet the most general case.
  1784. // For example, if the NoNansFPMathAttr is set in one function, but not in
  1785. // the other, in the merged function we can say that the NoNansFPMathAttr
  1786. // is not set.
  1787. // However if we have the SpeculativeLoadHardeningAttr set true in one
  1788. // function, but not the other, we make sure that the function retains
  1789. // that aspect in the merged function.
  1790. mergeFnAttrs(Base, ToMerge);
  1791. }
  1792. void AttributeFuncs::updateMinLegalVectorWidthAttr(Function &Fn,
  1793. uint64_t Width) {
  1794. Attribute Attr = Fn.getFnAttribute("min-legal-vector-width");
  1795. if (Attr.isValid()) {
  1796. uint64_t OldWidth;
  1797. Attr.getValueAsString().getAsInteger(0, OldWidth);
  1798. if (Width > OldWidth)
  1799. Fn.addFnAttr("min-legal-vector-width", llvm::utostr(Width));
  1800. }
  1801. }