Attributes.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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/Optional.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/Twine.h"
  25. #include "llvm/Config/llvm-config.h"
  26. #include "llvm/IR/Function.h"
  27. #include "llvm/IR/LLVMContext.h"
  28. #include "llvm/IR/Type.h"
  29. #include "llvm/Support/Compiler.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <climits>
  37. #include <cstddef>
  38. #include <cstdint>
  39. #include <limits>
  40. #include <string>
  41. #include <tuple>
  42. #include <utility>
  43. using namespace llvm;
  44. //===----------------------------------------------------------------------===//
  45. // Attribute Construction Methods
  46. //===----------------------------------------------------------------------===//
  47. // allocsize has two integer arguments, but because they're both 32 bits, we can
  48. // pack them into one 64-bit value, at the cost of making said value
  49. // nonsensical.
  50. //
  51. // In order to do this, we need to reserve one value of the second (optional)
  52. // allocsize argument to signify "not present."
  53. static const unsigned AllocSizeNumElemsNotPresent = -1;
  54. static uint64_t packAllocSizeArgs(unsigned ElemSizeArg,
  55. const Optional<unsigned> &NumElemsArg) {
  56. assert((!NumElemsArg.hasValue() ||
  57. *NumElemsArg != AllocSizeNumElemsNotPresent) &&
  58. "Attempting to pack a reserved value");
  59. return uint64_t(ElemSizeArg) << 32 |
  60. NumElemsArg.getValueOr(AllocSizeNumElemsNotPresent);
  61. }
  62. static std::pair<unsigned, Optional<unsigned>>
  63. unpackAllocSizeArgs(uint64_t Num) {
  64. unsigned NumElems = Num & std::numeric_limits<unsigned>::max();
  65. unsigned ElemSizeArg = Num >> 32;
  66. Optional<unsigned> NumElemsArg;
  67. if (NumElems != AllocSizeNumElemsNotPresent)
  68. NumElemsArg = NumElems;
  69. return std::make_pair(ElemSizeArg, NumElemsArg);
  70. }
  71. Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
  72. uint64_t Val) {
  73. LLVMContextImpl *pImpl = Context.pImpl;
  74. FoldingSetNodeID ID;
  75. ID.AddInteger(Kind);
  76. if (Val) ID.AddInteger(Val);
  77. void *InsertPoint;
  78. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  79. if (!PA) {
  80. // If we didn't find any existing attributes of the same shape then create a
  81. // new one and insert it.
  82. if (!Val)
  83. PA = new EnumAttributeImpl(Kind);
  84. else
  85. PA = new IntAttributeImpl(Kind, Val);
  86. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  87. }
  88. // Return the Attribute that we found or created.
  89. return Attribute(PA);
  90. }
  91. Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
  92. LLVMContextImpl *pImpl = Context.pImpl;
  93. FoldingSetNodeID ID;
  94. ID.AddString(Kind);
  95. if (!Val.empty()) ID.AddString(Val);
  96. void *InsertPoint;
  97. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  98. if (!PA) {
  99. // If we didn't find any existing attributes of the same shape then create a
  100. // new one and insert it.
  101. PA = new StringAttributeImpl(Kind, Val);
  102. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  103. }
  104. // Return the Attribute that we found or created.
  105. return Attribute(PA);
  106. }
  107. Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
  108. Type *Ty) {
  109. LLVMContextImpl *pImpl = Context.pImpl;
  110. FoldingSetNodeID ID;
  111. ID.AddInteger(Kind);
  112. ID.AddPointer(Ty);
  113. void *InsertPoint;
  114. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  115. if (!PA) {
  116. // If we didn't find any existing attributes of the same shape then create a
  117. // new one and insert it.
  118. PA = new TypeAttributeImpl(Kind, Ty);
  119. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  120. }
  121. // Return the Attribute that we found or created.
  122. return Attribute(PA);
  123. }
  124. Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
  125. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  126. assert(Align <= 0x40000000 && "Alignment too large.");
  127. return get(Context, Alignment, Align);
  128. }
  129. Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
  130. uint64_t Align) {
  131. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  132. assert(Align <= 0x100 && "Alignment too large.");
  133. return get(Context, StackAlignment, Align);
  134. }
  135. Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
  136. uint64_t Bytes) {
  137. assert(Bytes && "Bytes must be non-zero.");
  138. return get(Context, Dereferenceable, Bytes);
  139. }
  140. Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
  141. uint64_t Bytes) {
  142. assert(Bytes && "Bytes must be non-zero.");
  143. return get(Context, DereferenceableOrNull, Bytes);
  144. }
  145. Attribute Attribute::getWithByValType(LLVMContext &Context, Type *Ty) {
  146. return get(Context, ByVal, Ty);
  147. }
  148. Attribute
  149. Attribute::getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg,
  150. const Optional<unsigned> &NumElemsArg) {
  151. assert(!(ElemSizeArg == 0 && NumElemsArg && *NumElemsArg == 0) &&
  152. "Invalid allocsize arguments -- given allocsize(0, 0)");
  153. return get(Context, AllocSize, packAllocSizeArgs(ElemSizeArg, NumElemsArg));
  154. }
  155. //===----------------------------------------------------------------------===//
  156. // Attribute Accessor Methods
  157. //===----------------------------------------------------------------------===//
  158. bool Attribute::isEnumAttribute() const {
  159. return pImpl && pImpl->isEnumAttribute();
  160. }
  161. bool Attribute::isIntAttribute() const {
  162. return pImpl && pImpl->isIntAttribute();
  163. }
  164. bool Attribute::isStringAttribute() const {
  165. return pImpl && pImpl->isStringAttribute();
  166. }
  167. bool Attribute::isTypeAttribute() const {
  168. return pImpl && pImpl->isTypeAttribute();
  169. }
  170. Attribute::AttrKind Attribute::getKindAsEnum() const {
  171. if (!pImpl) return None;
  172. assert((isEnumAttribute() || isIntAttribute() || isTypeAttribute()) &&
  173. "Invalid attribute type to get the kind as an enum!");
  174. return pImpl->getKindAsEnum();
  175. }
  176. uint64_t Attribute::getValueAsInt() const {
  177. if (!pImpl) return 0;
  178. assert(isIntAttribute() &&
  179. "Expected the attribute to be an integer attribute!");
  180. return pImpl->getValueAsInt();
  181. }
  182. StringRef Attribute::getKindAsString() const {
  183. if (!pImpl) return {};
  184. assert(isStringAttribute() &&
  185. "Invalid attribute type to get the kind as a string!");
  186. return pImpl->getKindAsString();
  187. }
  188. StringRef Attribute::getValueAsString() const {
  189. if (!pImpl) return {};
  190. assert(isStringAttribute() &&
  191. "Invalid attribute type to get the value as a string!");
  192. return pImpl->getValueAsString();
  193. }
  194. Type *Attribute::getValueAsType() const {
  195. if (!pImpl) return {};
  196. assert(isTypeAttribute() &&
  197. "Invalid attribute type to get the value as a type!");
  198. return pImpl->getValueAsType();
  199. }
  200. bool Attribute::hasAttribute(AttrKind Kind) const {
  201. return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
  202. }
  203. bool Attribute::hasAttribute(StringRef Kind) const {
  204. if (!isStringAttribute()) return false;
  205. return pImpl && pImpl->hasAttribute(Kind);
  206. }
  207. unsigned Attribute::getAlignment() const {
  208. assert(hasAttribute(Attribute::Alignment) &&
  209. "Trying to get alignment from non-alignment attribute!");
  210. return pImpl->getValueAsInt();
  211. }
  212. unsigned Attribute::getStackAlignment() const {
  213. assert(hasAttribute(Attribute::StackAlignment) &&
  214. "Trying to get alignment from non-alignment attribute!");
  215. return pImpl->getValueAsInt();
  216. }
  217. uint64_t Attribute::getDereferenceableBytes() const {
  218. assert(hasAttribute(Attribute::Dereferenceable) &&
  219. "Trying to get dereferenceable bytes from "
  220. "non-dereferenceable attribute!");
  221. return pImpl->getValueAsInt();
  222. }
  223. uint64_t Attribute::getDereferenceableOrNullBytes() const {
  224. assert(hasAttribute(Attribute::DereferenceableOrNull) &&
  225. "Trying to get dereferenceable bytes from "
  226. "non-dereferenceable attribute!");
  227. return pImpl->getValueAsInt();
  228. }
  229. std::pair<unsigned, Optional<unsigned>> Attribute::getAllocSizeArgs() const {
  230. assert(hasAttribute(Attribute::AllocSize) &&
  231. "Trying to get allocsize args from non-allocsize attribute");
  232. return unpackAllocSizeArgs(pImpl->getValueAsInt());
  233. }
  234. std::string Attribute::getAsString(bool InAttrGrp) const {
  235. if (!pImpl) return {};
  236. if (hasAttribute(Attribute::SanitizeAddress))
  237. return "sanitize_address";
  238. if (hasAttribute(Attribute::SanitizeHWAddress))
  239. return "sanitize_hwaddress";
  240. if (hasAttribute(Attribute::SanitizeMemTag))
  241. return "sanitize_memtag";
  242. if (hasAttribute(Attribute::AlwaysInline))
  243. return "alwaysinline";
  244. if (hasAttribute(Attribute::ArgMemOnly))
  245. return "argmemonly";
  246. if (hasAttribute(Attribute::Builtin))
  247. return "builtin";
  248. if (hasAttribute(Attribute::Convergent))
  249. return "convergent";
  250. if (hasAttribute(Attribute::SwiftError))
  251. return "swifterror";
  252. if (hasAttribute(Attribute::SwiftSelf))
  253. return "swiftself";
  254. if (hasAttribute(Attribute::InaccessibleMemOnly))
  255. return "inaccessiblememonly";
  256. if (hasAttribute(Attribute::InaccessibleMemOrArgMemOnly))
  257. return "inaccessiblemem_or_argmemonly";
  258. if (hasAttribute(Attribute::InAlloca))
  259. return "inalloca";
  260. if (hasAttribute(Attribute::InlineHint))
  261. return "inlinehint";
  262. if (hasAttribute(Attribute::InReg))
  263. return "inreg";
  264. if (hasAttribute(Attribute::JumpTable))
  265. return "jumptable";
  266. if (hasAttribute(Attribute::MinSize))
  267. return "minsize";
  268. if (hasAttribute(Attribute::Naked))
  269. return "naked";
  270. if (hasAttribute(Attribute::Nest))
  271. return "nest";
  272. if (hasAttribute(Attribute::NoAlias))
  273. return "noalias";
  274. if (hasAttribute(Attribute::NoBuiltin))
  275. return "nobuiltin";
  276. if (hasAttribute(Attribute::NoCapture))
  277. return "nocapture";
  278. if (hasAttribute(Attribute::NoDuplicate))
  279. return "noduplicate";
  280. if (hasAttribute(Attribute::NoFree))
  281. return "nofree";
  282. if (hasAttribute(Attribute::NoImplicitFloat))
  283. return "noimplicitfloat";
  284. if (hasAttribute(Attribute::NoInline))
  285. return "noinline";
  286. if (hasAttribute(Attribute::NonLazyBind))
  287. return "nonlazybind";
  288. if (hasAttribute(Attribute::NonNull))
  289. return "nonnull";
  290. if (hasAttribute(Attribute::NoRedZone))
  291. return "noredzone";
  292. if (hasAttribute(Attribute::NoReturn))
  293. return "noreturn";
  294. if (hasAttribute(Attribute::NoSync))
  295. return "nosync";
  296. if (hasAttribute(Attribute::WillReturn))
  297. return "willreturn";
  298. if (hasAttribute(Attribute::NoCfCheck))
  299. return "nocf_check";
  300. if (hasAttribute(Attribute::NoRecurse))
  301. return "norecurse";
  302. if (hasAttribute(Attribute::NoUnwind))
  303. return "nounwind";
  304. if (hasAttribute(Attribute::OptForFuzzing))
  305. return "optforfuzzing";
  306. if (hasAttribute(Attribute::OptimizeNone))
  307. return "optnone";
  308. if (hasAttribute(Attribute::OptimizeForSize))
  309. return "optsize";
  310. if (hasAttribute(Attribute::ReadNone))
  311. return "readnone";
  312. if (hasAttribute(Attribute::ReadOnly))
  313. return "readonly";
  314. if (hasAttribute(Attribute::WriteOnly))
  315. return "writeonly";
  316. if (hasAttribute(Attribute::Returned))
  317. return "returned";
  318. if (hasAttribute(Attribute::ReturnsTwice))
  319. return "returns_twice";
  320. if (hasAttribute(Attribute::SExt))
  321. return "signext";
  322. if (hasAttribute(Attribute::SpeculativeLoadHardening))
  323. return "speculative_load_hardening";
  324. if (hasAttribute(Attribute::Speculatable))
  325. return "speculatable";
  326. if (hasAttribute(Attribute::StackProtect))
  327. return "ssp";
  328. if (hasAttribute(Attribute::StackProtectReq))
  329. return "sspreq";
  330. if (hasAttribute(Attribute::StackProtectStrong))
  331. return "sspstrong";
  332. if (hasAttribute(Attribute::SafeStack))
  333. return "safestack";
  334. if (hasAttribute(Attribute::ShadowCallStack))
  335. return "shadowcallstack";
  336. if (hasAttribute(Attribute::StrictFP))
  337. return "strictfp";
  338. if (hasAttribute(Attribute::StructRet))
  339. return "sret";
  340. if (hasAttribute(Attribute::SanitizeThread))
  341. return "sanitize_thread";
  342. if (hasAttribute(Attribute::SanitizeMemory))
  343. return "sanitize_memory";
  344. if (hasAttribute(Attribute::UWTable))
  345. return "uwtable";
  346. if (hasAttribute(Attribute::ZExt))
  347. return "zeroext";
  348. if (hasAttribute(Attribute::Cold))
  349. return "cold";
  350. if (hasAttribute(Attribute::ImmArg))
  351. return "immarg";
  352. if (hasAttribute(Attribute::ByVal)) {
  353. std::string Result;
  354. Result += "byval";
  355. if (Type *Ty = getValueAsType()) {
  356. raw_string_ostream OS(Result);
  357. Result += '(';
  358. Ty->print(OS, false, true);
  359. OS.flush();
  360. Result += ')';
  361. }
  362. return Result;
  363. }
  364. // FIXME: These should be output like this:
  365. //
  366. // align=4
  367. // alignstack=8
  368. //
  369. if (hasAttribute(Attribute::Alignment)) {
  370. std::string Result;
  371. Result += "align";
  372. Result += (InAttrGrp) ? "=" : " ";
  373. Result += utostr(getValueAsInt());
  374. return Result;
  375. }
  376. auto AttrWithBytesToString = [&](const char *Name) {
  377. std::string Result;
  378. Result += Name;
  379. if (InAttrGrp) {
  380. Result += "=";
  381. Result += utostr(getValueAsInt());
  382. } else {
  383. Result += "(";
  384. Result += utostr(getValueAsInt());
  385. Result += ")";
  386. }
  387. return Result;
  388. };
  389. if (hasAttribute(Attribute::StackAlignment))
  390. return AttrWithBytesToString("alignstack");
  391. if (hasAttribute(Attribute::Dereferenceable))
  392. return AttrWithBytesToString("dereferenceable");
  393. if (hasAttribute(Attribute::DereferenceableOrNull))
  394. return AttrWithBytesToString("dereferenceable_or_null");
  395. if (hasAttribute(Attribute::AllocSize)) {
  396. unsigned ElemSize;
  397. Optional<unsigned> NumElems;
  398. std::tie(ElemSize, NumElems) = getAllocSizeArgs();
  399. std::string Result = "allocsize(";
  400. Result += utostr(ElemSize);
  401. if (NumElems.hasValue()) {
  402. Result += ',';
  403. Result += utostr(*NumElems);
  404. }
  405. Result += ')';
  406. return Result;
  407. }
  408. // Convert target-dependent attributes to strings of the form:
  409. //
  410. // "kind"
  411. // "kind" = "value"
  412. //
  413. if (isStringAttribute()) {
  414. std::string Result;
  415. Result += (Twine('"') + getKindAsString() + Twine('"')).str();
  416. std::string AttrVal = pImpl->getValueAsString();
  417. if (AttrVal.empty()) return Result;
  418. // Since some attribute strings contain special characters that cannot be
  419. // printable, those have to be escaped to make the attribute value printable
  420. // as is. e.g. "\01__gnu_mcount_nc"
  421. {
  422. raw_string_ostream OS(Result);
  423. OS << "=\"";
  424. printEscapedString(AttrVal, OS);
  425. OS << "\"";
  426. }
  427. return Result;
  428. }
  429. llvm_unreachable("Unknown attribute");
  430. }
  431. bool Attribute::operator<(Attribute A) const {
  432. if (!pImpl && !A.pImpl) return false;
  433. if (!pImpl) return true;
  434. if (!A.pImpl) return false;
  435. return *pImpl < *A.pImpl;
  436. }
  437. //===----------------------------------------------------------------------===//
  438. // AttributeImpl Definition
  439. //===----------------------------------------------------------------------===//
  440. // Pin the vtables to this file.
  441. AttributeImpl::~AttributeImpl() = default;
  442. void EnumAttributeImpl::anchor() {}
  443. void IntAttributeImpl::anchor() {}
  444. void StringAttributeImpl::anchor() {}
  445. void TypeAttributeImpl::anchor() {}
  446. bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
  447. if (isStringAttribute()) return false;
  448. return getKindAsEnum() == A;
  449. }
  450. bool AttributeImpl::hasAttribute(StringRef Kind) const {
  451. if (!isStringAttribute()) return false;
  452. return getKindAsString() == Kind;
  453. }
  454. Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
  455. assert(isEnumAttribute() || isIntAttribute() || isTypeAttribute());
  456. return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
  457. }
  458. uint64_t AttributeImpl::getValueAsInt() const {
  459. assert(isIntAttribute());
  460. return static_cast<const IntAttributeImpl *>(this)->getValue();
  461. }
  462. StringRef AttributeImpl::getKindAsString() const {
  463. assert(isStringAttribute());
  464. return static_cast<const StringAttributeImpl *>(this)->getStringKind();
  465. }
  466. StringRef AttributeImpl::getValueAsString() const {
  467. assert(isStringAttribute());
  468. return static_cast<const StringAttributeImpl *>(this)->getStringValue();
  469. }
  470. Type *AttributeImpl::getValueAsType() const {
  471. assert(isTypeAttribute());
  472. return static_cast<const TypeAttributeImpl *>(this)->getTypeValue();
  473. }
  474. bool AttributeImpl::operator<(const AttributeImpl &AI) const {
  475. // This sorts the attributes with Attribute::AttrKinds coming first (sorted
  476. // relative to their enum value) and then strings.
  477. if (isEnumAttribute()) {
  478. if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
  479. if (AI.isIntAttribute()) return true;
  480. if (AI.isStringAttribute()) return true;
  481. if (AI.isTypeAttribute()) return true;
  482. }
  483. if (isTypeAttribute()) {
  484. if (AI.isEnumAttribute()) return false;
  485. if (AI.isTypeAttribute()) {
  486. assert(getKindAsEnum() != AI.getKindAsEnum() &&
  487. "Comparison of types would be unstable");
  488. return getKindAsEnum() < AI.getKindAsEnum();
  489. }
  490. if (AI.isIntAttribute()) return true;
  491. if (AI.isStringAttribute()) return true;
  492. }
  493. if (isIntAttribute()) {
  494. if (AI.isEnumAttribute()) return false;
  495. if (AI.isTypeAttribute()) return false;
  496. if (AI.isIntAttribute()) {
  497. if (getKindAsEnum() == AI.getKindAsEnum())
  498. return getValueAsInt() < AI.getValueAsInt();
  499. return getKindAsEnum() < AI.getKindAsEnum();
  500. }
  501. if (AI.isStringAttribute()) return true;
  502. }
  503. assert(isStringAttribute());
  504. if (AI.isEnumAttribute()) return false;
  505. if (AI.isTypeAttribute()) return false;
  506. if (AI.isIntAttribute()) return false;
  507. if (getKindAsString() == AI.getKindAsString())
  508. return getValueAsString() < AI.getValueAsString();
  509. return getKindAsString() < AI.getKindAsString();
  510. }
  511. //===----------------------------------------------------------------------===//
  512. // AttributeSet Definition
  513. //===----------------------------------------------------------------------===//
  514. AttributeSet AttributeSet::get(LLVMContext &C, const AttrBuilder &B) {
  515. return AttributeSet(AttributeSetNode::get(C, B));
  516. }
  517. AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<Attribute> Attrs) {
  518. return AttributeSet(AttributeSetNode::get(C, Attrs));
  519. }
  520. AttributeSet AttributeSet::addAttribute(LLVMContext &C,
  521. Attribute::AttrKind Kind) const {
  522. if (hasAttribute(Kind)) return *this;
  523. AttrBuilder B;
  524. B.addAttribute(Kind);
  525. return addAttributes(C, AttributeSet::get(C, B));
  526. }
  527. AttributeSet AttributeSet::addAttribute(LLVMContext &C, StringRef Kind,
  528. StringRef Value) const {
  529. AttrBuilder B;
  530. B.addAttribute(Kind, Value);
  531. return addAttributes(C, AttributeSet::get(C, B));
  532. }
  533. AttributeSet AttributeSet::addAttributes(LLVMContext &C,
  534. const AttributeSet AS) const {
  535. if (!hasAttributes())
  536. return AS;
  537. if (!AS.hasAttributes())
  538. return *this;
  539. AttrBuilder B(AS);
  540. for (const auto I : *this)
  541. B.addAttribute(I);
  542. return get(C, B);
  543. }
  544. AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
  545. Attribute::AttrKind Kind) const {
  546. if (!hasAttribute(Kind)) return *this;
  547. AttrBuilder B(*this);
  548. B.removeAttribute(Kind);
  549. return get(C, B);
  550. }
  551. AttributeSet AttributeSet::removeAttribute(LLVMContext &C,
  552. StringRef Kind) const {
  553. if (!hasAttribute(Kind)) return *this;
  554. AttrBuilder B(*this);
  555. B.removeAttribute(Kind);
  556. return get(C, B);
  557. }
  558. AttributeSet AttributeSet::removeAttributes(LLVMContext &C,
  559. const AttrBuilder &Attrs) const {
  560. AttrBuilder B(*this);
  561. B.remove(Attrs);
  562. return get(C, B);
  563. }
  564. unsigned AttributeSet::getNumAttributes() const {
  565. return SetNode ? SetNode->getNumAttributes() : 0;
  566. }
  567. bool AttributeSet::hasAttribute(Attribute::AttrKind Kind) const {
  568. return SetNode ? SetNode->hasAttribute(Kind) : false;
  569. }
  570. bool AttributeSet::hasAttribute(StringRef Kind) const {
  571. return SetNode ? SetNode->hasAttribute(Kind) : false;
  572. }
  573. Attribute AttributeSet::getAttribute(Attribute::AttrKind Kind) const {
  574. return SetNode ? SetNode->getAttribute(Kind) : Attribute();
  575. }
  576. Attribute AttributeSet::getAttribute(StringRef Kind) const {
  577. return SetNode ? SetNode->getAttribute(Kind) : Attribute();
  578. }
  579. unsigned AttributeSet::getAlignment() const {
  580. return SetNode ? SetNode->getAlignment() : 0;
  581. }
  582. unsigned AttributeSet::getStackAlignment() const {
  583. return SetNode ? SetNode->getStackAlignment() : 0;
  584. }
  585. uint64_t AttributeSet::getDereferenceableBytes() const {
  586. return SetNode ? SetNode->getDereferenceableBytes() : 0;
  587. }
  588. uint64_t AttributeSet::getDereferenceableOrNullBytes() const {
  589. return SetNode ? SetNode->getDereferenceableOrNullBytes() : 0;
  590. }
  591. Type *AttributeSet::getByValType() const {
  592. return SetNode ? SetNode->getByValType() : nullptr;
  593. }
  594. std::pair<unsigned, Optional<unsigned>> AttributeSet::getAllocSizeArgs() const {
  595. return SetNode ? SetNode->getAllocSizeArgs()
  596. : std::pair<unsigned, Optional<unsigned>>(0, 0);
  597. }
  598. std::string AttributeSet::getAsString(bool InAttrGrp) const {
  599. return SetNode ? SetNode->getAsString(InAttrGrp) : "";
  600. }
  601. AttributeSet::iterator AttributeSet::begin() const {
  602. return SetNode ? SetNode->begin() : nullptr;
  603. }
  604. AttributeSet::iterator AttributeSet::end() const {
  605. return SetNode ? SetNode->end() : nullptr;
  606. }
  607. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  608. LLVM_DUMP_METHOD void AttributeSet::dump() const {
  609. dbgs() << "AS =\n";
  610. dbgs() << " { ";
  611. dbgs() << getAsString(true) << " }\n";
  612. }
  613. #endif
  614. //===----------------------------------------------------------------------===//
  615. // AttributeSetNode Definition
  616. //===----------------------------------------------------------------------===//
  617. AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
  618. : NumAttrs(Attrs.size()) {
  619. // There's memory after the node where we can store the entries in.
  620. llvm::copy(Attrs, getTrailingObjects<Attribute>());
  621. static_assert(Attribute::EndAttrKinds <=
  622. sizeof(AvailableAttrs) * CHAR_BIT,
  623. "Too many attributes");
  624. for (const auto I : *this) {
  625. if (!I.isStringAttribute()) {
  626. Attribute::AttrKind Kind = I.getKindAsEnum();
  627. AvailableAttrs[Kind / 8] |= 1ULL << (Kind % 8);
  628. }
  629. }
  630. }
  631. AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
  632. ArrayRef<Attribute> Attrs) {
  633. if (Attrs.empty())
  634. return nullptr;
  635. // Otherwise, build a key to look up the existing attributes.
  636. LLVMContextImpl *pImpl = C.pImpl;
  637. FoldingSetNodeID ID;
  638. SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
  639. llvm::sort(SortedAttrs);
  640. for (const auto Attr : SortedAttrs)
  641. Attr.Profile(ID);
  642. void *InsertPoint;
  643. AttributeSetNode *PA =
  644. pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
  645. // If we didn't find any existing attributes of the same shape then create a
  646. // new one and insert it.
  647. if (!PA) {
  648. // Coallocate entries after the AttributeSetNode itself.
  649. void *Mem = ::operator new(totalSizeToAlloc<Attribute>(SortedAttrs.size()));
  650. PA = new (Mem) AttributeSetNode(SortedAttrs);
  651. pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
  652. }
  653. // Return the AttributeSetNode that we found or created.
  654. return PA;
  655. }
  656. AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
  657. // Add target-independent attributes.
  658. SmallVector<Attribute, 8> Attrs;
  659. for (Attribute::AttrKind Kind = Attribute::None;
  660. Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
  661. if (!B.contains(Kind))
  662. continue;
  663. Attribute Attr;
  664. switch (Kind) {
  665. case Attribute::ByVal:
  666. Attr = Attribute::getWithByValType(C, B.getByValType());
  667. break;
  668. case Attribute::Alignment:
  669. Attr = Attribute::getWithAlignment(C, B.getAlignment());
  670. break;
  671. case Attribute::StackAlignment:
  672. Attr = Attribute::getWithStackAlignment(C, B.getStackAlignment());
  673. break;
  674. case Attribute::Dereferenceable:
  675. Attr = Attribute::getWithDereferenceableBytes(
  676. C, B.getDereferenceableBytes());
  677. break;
  678. case Attribute::DereferenceableOrNull:
  679. Attr = Attribute::getWithDereferenceableOrNullBytes(
  680. C, B.getDereferenceableOrNullBytes());
  681. break;
  682. case Attribute::AllocSize: {
  683. auto A = B.getAllocSizeArgs();
  684. Attr = Attribute::getWithAllocSizeArgs(C, A.first, A.second);
  685. break;
  686. }
  687. default:
  688. Attr = Attribute::get(C, Kind);
  689. }
  690. Attrs.push_back(Attr);
  691. }
  692. // Add target-dependent (string) attributes.
  693. for (const auto &TDA : B.td_attrs())
  694. Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));
  695. return get(C, Attrs);
  696. }
  697. bool AttributeSetNode::hasAttribute(StringRef Kind) const {
  698. for (const auto I : *this)
  699. if (I.hasAttribute(Kind))
  700. return true;
  701. return false;
  702. }
  703. Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
  704. if (hasAttribute(Kind)) {
  705. for (const auto I : *this)
  706. if (I.hasAttribute(Kind))
  707. return I;
  708. }
  709. return {};
  710. }
  711. Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
  712. for (const auto I : *this)
  713. if (I.hasAttribute(Kind))
  714. return I;
  715. return {};
  716. }
  717. unsigned AttributeSetNode::getAlignment() const {
  718. for (const auto I : *this)
  719. if (I.hasAttribute(Attribute::Alignment))
  720. return I.getAlignment();
  721. return 0;
  722. }
  723. unsigned AttributeSetNode::getStackAlignment() const {
  724. for (const auto I : *this)
  725. if (I.hasAttribute(Attribute::StackAlignment))
  726. return I.getStackAlignment();
  727. return 0;
  728. }
  729. Type *AttributeSetNode::getByValType() const {
  730. for (const auto I : *this)
  731. if (I.hasAttribute(Attribute::ByVal))
  732. return I.getValueAsType();
  733. return 0;
  734. }
  735. uint64_t AttributeSetNode::getDereferenceableBytes() const {
  736. for (const auto I : *this)
  737. if (I.hasAttribute(Attribute::Dereferenceable))
  738. return I.getDereferenceableBytes();
  739. return 0;
  740. }
  741. uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
  742. for (const auto I : *this)
  743. if (I.hasAttribute(Attribute::DereferenceableOrNull))
  744. return I.getDereferenceableOrNullBytes();
  745. return 0;
  746. }
  747. std::pair<unsigned, Optional<unsigned>>
  748. AttributeSetNode::getAllocSizeArgs() const {
  749. for (const auto I : *this)
  750. if (I.hasAttribute(Attribute::AllocSize))
  751. return I.getAllocSizeArgs();
  752. return std::make_pair(0, 0);
  753. }
  754. std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
  755. std::string Str;
  756. for (iterator I = begin(), E = end(); I != E; ++I) {
  757. if (I != begin())
  758. Str += ' ';
  759. Str += I->getAsString(InAttrGrp);
  760. }
  761. return Str;
  762. }
  763. //===----------------------------------------------------------------------===//
  764. // AttributeListImpl Definition
  765. //===----------------------------------------------------------------------===//
  766. /// Map from AttributeList index to the internal array index. Adding one happens
  767. /// to work, but it relies on unsigned integer wrapping. MSVC warns about
  768. /// unsigned wrapping in constexpr functions, so write out the conditional. LLVM
  769. /// folds it to add anyway.
  770. static constexpr unsigned attrIdxToArrayIdx(unsigned Index) {
  771. return Index == AttributeList::FunctionIndex ? 0 : Index + 1;
  772. }
  773. AttributeListImpl::AttributeListImpl(LLVMContext &C,
  774. ArrayRef<AttributeSet> Sets)
  775. : Context(C), NumAttrSets(Sets.size()) {
  776. assert(!Sets.empty() && "pointless AttributeListImpl");
  777. // There's memory after the node where we can store the entries in.
  778. llvm::copy(Sets, getTrailingObjects<AttributeSet>());
  779. // Initialize AvailableFunctionAttrs summary bitset.
  780. static_assert(Attribute::EndAttrKinds <=
  781. sizeof(AvailableFunctionAttrs) * CHAR_BIT,
  782. "Too many attributes");
  783. static_assert(attrIdxToArrayIdx(AttributeList::FunctionIndex) == 0U,
  784. "function should be stored in slot 0");
  785. for (const auto I : Sets[0]) {
  786. if (!I.isStringAttribute()) {
  787. Attribute::AttrKind Kind = I.getKindAsEnum();
  788. AvailableFunctionAttrs[Kind / 8] |= 1ULL << (Kind % 8);
  789. }
  790. }
  791. }
  792. void AttributeListImpl::Profile(FoldingSetNodeID &ID) const {
  793. Profile(ID, makeArrayRef(begin(), end()));
  794. }
  795. void AttributeListImpl::Profile(FoldingSetNodeID &ID,
  796. ArrayRef<AttributeSet> Sets) {
  797. for (const auto &Set : Sets)
  798. ID.AddPointer(Set.SetNode);
  799. }
  800. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  801. LLVM_DUMP_METHOD void AttributeListImpl::dump() const {
  802. AttributeList(const_cast<AttributeListImpl *>(this)).dump();
  803. }
  804. #endif
  805. //===----------------------------------------------------------------------===//
  806. // AttributeList Construction and Mutation Methods
  807. //===----------------------------------------------------------------------===//
  808. AttributeList AttributeList::getImpl(LLVMContext &C,
  809. ArrayRef<AttributeSet> AttrSets) {
  810. assert(!AttrSets.empty() && "pointless AttributeListImpl");
  811. LLVMContextImpl *pImpl = C.pImpl;
  812. FoldingSetNodeID ID;
  813. AttributeListImpl::Profile(ID, AttrSets);
  814. void *InsertPoint;
  815. AttributeListImpl *PA =
  816. pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
  817. // If we didn't find any existing attributes of the same shape then
  818. // create a new one and insert it.
  819. if (!PA) {
  820. // Coallocate entries after the AttributeListImpl itself.
  821. void *Mem = ::operator new(
  822. AttributeListImpl::totalSizeToAlloc<AttributeSet>(AttrSets.size()));
  823. PA = new (Mem) AttributeListImpl(C, AttrSets);
  824. pImpl->AttrsLists.InsertNode(PA, InsertPoint);
  825. }
  826. // Return the AttributesList that we found or created.
  827. return AttributeList(PA);
  828. }
  829. AttributeList
  830. AttributeList::get(LLVMContext &C,
  831. ArrayRef<std::pair<unsigned, Attribute>> Attrs) {
  832. // If there are no attributes then return a null AttributesList pointer.
  833. if (Attrs.empty())
  834. return {};
  835. assert(std::is_sorted(Attrs.begin(), Attrs.end(),
  836. [](const std::pair<unsigned, Attribute> &LHS,
  837. const std::pair<unsigned, Attribute> &RHS) {
  838. return LHS.first < RHS.first;
  839. }) && "Misordered Attributes list!");
  840. assert(llvm::none_of(Attrs,
  841. [](const std::pair<unsigned, Attribute> &Pair) {
  842. return Pair.second.hasAttribute(Attribute::None);
  843. }) &&
  844. "Pointless attribute!");
  845. // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
  846. // list.
  847. SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrPairVec;
  848. for (ArrayRef<std::pair<unsigned, Attribute>>::iterator I = Attrs.begin(),
  849. E = Attrs.end(); I != E; ) {
  850. unsigned Index = I->first;
  851. SmallVector<Attribute, 4> AttrVec;
  852. while (I != E && I->first == Index) {
  853. AttrVec.push_back(I->second);
  854. ++I;
  855. }
  856. AttrPairVec.emplace_back(Index, AttributeSet::get(C, AttrVec));
  857. }
  858. return get(C, AttrPairVec);
  859. }
  860. AttributeList
  861. AttributeList::get(LLVMContext &C,
  862. ArrayRef<std::pair<unsigned, AttributeSet>> Attrs) {
  863. // If there are no attributes then return a null AttributesList pointer.
  864. if (Attrs.empty())
  865. return {};
  866. assert(std::is_sorted(Attrs.begin(), Attrs.end(),
  867. [](const std::pair<unsigned, AttributeSet> &LHS,
  868. const std::pair<unsigned, AttributeSet> &RHS) {
  869. return LHS.first < RHS.first;
  870. }) &&
  871. "Misordered Attributes list!");
  872. assert(llvm::none_of(Attrs,
  873. [](const std::pair<unsigned, AttributeSet> &Pair) {
  874. return !Pair.second.hasAttributes();
  875. }) &&
  876. "Pointless attribute!");
  877. unsigned MaxIndex = Attrs.back().first;
  878. // If the MaxIndex is FunctionIndex and there are other indices in front
  879. // of it, we need to use the largest of those to get the right size.
  880. if (MaxIndex == FunctionIndex && Attrs.size() > 1)
  881. MaxIndex = Attrs[Attrs.size() - 2].first;
  882. SmallVector<AttributeSet, 4> AttrVec(attrIdxToArrayIdx(MaxIndex) + 1);
  883. for (const auto Pair : Attrs)
  884. AttrVec[attrIdxToArrayIdx(Pair.first)] = Pair.second;
  885. return getImpl(C, AttrVec);
  886. }
  887. AttributeList AttributeList::get(LLVMContext &C, AttributeSet FnAttrs,
  888. AttributeSet RetAttrs,
  889. ArrayRef<AttributeSet> ArgAttrs) {
  890. // Scan from the end to find the last argument with attributes. Most
  891. // arguments don't have attributes, so it's nice if we can have fewer unique
  892. // AttributeListImpls by dropping empty attribute sets at the end of the list.
  893. unsigned NumSets = 0;
  894. for (size_t I = ArgAttrs.size(); I != 0; --I) {
  895. if (ArgAttrs[I - 1].hasAttributes()) {
  896. NumSets = I + 2;
  897. break;
  898. }
  899. }
  900. if (NumSets == 0) {
  901. // Check function and return attributes if we didn't have argument
  902. // attributes.
  903. if (RetAttrs.hasAttributes())
  904. NumSets = 2;
  905. else if (FnAttrs.hasAttributes())
  906. NumSets = 1;
  907. }
  908. // If all attribute sets were empty, we can use the empty attribute list.
  909. if (NumSets == 0)
  910. return {};
  911. SmallVector<AttributeSet, 8> AttrSets;
  912. AttrSets.reserve(NumSets);
  913. // If we have any attributes, we always have function attributes.
  914. AttrSets.push_back(FnAttrs);
  915. if (NumSets > 1)
  916. AttrSets.push_back(RetAttrs);
  917. if (NumSets > 2) {
  918. // Drop the empty argument attribute sets at the end.
  919. ArgAttrs = ArgAttrs.take_front(NumSets - 2);
  920. AttrSets.insert(AttrSets.end(), ArgAttrs.begin(), ArgAttrs.end());
  921. }
  922. return getImpl(C, AttrSets);
  923. }
  924. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  925. const AttrBuilder &B) {
  926. if (!B.hasAttributes())
  927. return {};
  928. Index = attrIdxToArrayIdx(Index);
  929. SmallVector<AttributeSet, 8> AttrSets(Index + 1);
  930. AttrSets[Index] = AttributeSet::get(C, B);
  931. return getImpl(C, AttrSets);
  932. }
  933. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  934. ArrayRef<Attribute::AttrKind> Kinds) {
  935. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  936. for (const auto K : Kinds)
  937. Attrs.emplace_back(Index, Attribute::get(C, K));
  938. return get(C, Attrs);
  939. }
  940. AttributeList AttributeList::get(LLVMContext &C, unsigned Index,
  941. ArrayRef<StringRef> Kinds) {
  942. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  943. for (const auto K : Kinds)
  944. Attrs.emplace_back(Index, Attribute::get(C, K));
  945. return get(C, Attrs);
  946. }
  947. AttributeList AttributeList::get(LLVMContext &C,
  948. ArrayRef<AttributeList> Attrs) {
  949. if (Attrs.empty())
  950. return {};
  951. if (Attrs.size() == 1)
  952. return Attrs[0];
  953. unsigned MaxSize = 0;
  954. for (const auto List : Attrs)
  955. MaxSize = std::max(MaxSize, List.getNumAttrSets());
  956. // If every list was empty, there is no point in merging the lists.
  957. if (MaxSize == 0)
  958. return {};
  959. SmallVector<AttributeSet, 8> NewAttrSets(MaxSize);
  960. for (unsigned I = 0; I < MaxSize; ++I) {
  961. AttrBuilder CurBuilder;
  962. for (const auto List : Attrs)
  963. CurBuilder.merge(List.getAttributes(I - 1));
  964. NewAttrSets[I] = AttributeSet::get(C, CurBuilder);
  965. }
  966. return getImpl(C, NewAttrSets);
  967. }
  968. AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
  969. Attribute::AttrKind Kind) const {
  970. if (hasAttribute(Index, Kind)) return *this;
  971. AttrBuilder B;
  972. B.addAttribute(Kind);
  973. return addAttributes(C, Index, B);
  974. }
  975. AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
  976. StringRef Kind,
  977. StringRef Value) const {
  978. AttrBuilder B;
  979. B.addAttribute(Kind, Value);
  980. return addAttributes(C, Index, B);
  981. }
  982. AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
  983. Attribute A) const {
  984. AttrBuilder B;
  985. B.addAttribute(A);
  986. return addAttributes(C, Index, B);
  987. }
  988. AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
  989. const AttrBuilder &B) const {
  990. if (!B.hasAttributes())
  991. return *this;
  992. if (!pImpl)
  993. return AttributeList::get(C, {{Index, AttributeSet::get(C, B)}});
  994. #ifndef NDEBUG
  995. // FIXME it is not obvious how this should work for alignment. For now, say
  996. // we can't change a known alignment.
  997. unsigned OldAlign = getAttributes(Index).getAlignment();
  998. unsigned NewAlign = B.getAlignment();
  999. assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
  1000. "Attempt to change alignment!");
  1001. #endif
  1002. Index = attrIdxToArrayIdx(Index);
  1003. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1004. if (Index >= AttrSets.size())
  1005. AttrSets.resize(Index + 1);
  1006. AttrBuilder Merged(AttrSets[Index]);
  1007. Merged.merge(B);
  1008. AttrSets[Index] = AttributeSet::get(C, Merged);
  1009. return getImpl(C, AttrSets);
  1010. }
  1011. AttributeList AttributeList::addParamAttribute(LLVMContext &C,
  1012. ArrayRef<unsigned> ArgNos,
  1013. Attribute A) const {
  1014. assert(std::is_sorted(ArgNos.begin(), ArgNos.end()));
  1015. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1016. unsigned MaxIndex = attrIdxToArrayIdx(ArgNos.back() + FirstArgIndex);
  1017. if (MaxIndex >= AttrSets.size())
  1018. AttrSets.resize(MaxIndex + 1);
  1019. for (unsigned ArgNo : ArgNos) {
  1020. unsigned Index = attrIdxToArrayIdx(ArgNo + FirstArgIndex);
  1021. AttrBuilder B(AttrSets[Index]);
  1022. B.addAttribute(A);
  1023. AttrSets[Index] = AttributeSet::get(C, B);
  1024. }
  1025. return getImpl(C, AttrSets);
  1026. }
  1027. AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
  1028. Attribute::AttrKind Kind) const {
  1029. if (!hasAttribute(Index, Kind)) return *this;
  1030. Index = attrIdxToArrayIdx(Index);
  1031. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1032. assert(Index < AttrSets.size());
  1033. AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
  1034. return getImpl(C, AttrSets);
  1035. }
  1036. AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
  1037. StringRef Kind) const {
  1038. if (!hasAttribute(Index, Kind)) return *this;
  1039. Index = attrIdxToArrayIdx(Index);
  1040. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1041. assert(Index < AttrSets.size());
  1042. AttrSets[Index] = AttrSets[Index].removeAttribute(C, Kind);
  1043. return getImpl(C, AttrSets);
  1044. }
  1045. AttributeList
  1046. AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
  1047. const AttrBuilder &AttrsToRemove) const {
  1048. if (!pImpl)
  1049. return {};
  1050. Index = attrIdxToArrayIdx(Index);
  1051. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1052. if (Index >= AttrSets.size())
  1053. AttrSets.resize(Index + 1);
  1054. AttrSets[Index] = AttrSets[Index].removeAttributes(C, AttrsToRemove);
  1055. return getImpl(C, AttrSets);
  1056. }
  1057. AttributeList AttributeList::removeAttributes(LLVMContext &C,
  1058. unsigned WithoutIndex) const {
  1059. if (!pImpl)
  1060. return {};
  1061. WithoutIndex = attrIdxToArrayIdx(WithoutIndex);
  1062. if (WithoutIndex >= getNumAttrSets())
  1063. return *this;
  1064. SmallVector<AttributeSet, 4> AttrSets(this->begin(), this->end());
  1065. AttrSets[WithoutIndex] = AttributeSet();
  1066. return getImpl(C, AttrSets);
  1067. }
  1068. AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
  1069. unsigned Index,
  1070. uint64_t Bytes) const {
  1071. AttrBuilder B;
  1072. B.addDereferenceableAttr(Bytes);
  1073. return addAttributes(C, Index, B);
  1074. }
  1075. AttributeList
  1076. AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
  1077. uint64_t Bytes) const {
  1078. AttrBuilder B;
  1079. B.addDereferenceableOrNullAttr(Bytes);
  1080. return addAttributes(C, Index, B);
  1081. }
  1082. AttributeList
  1083. AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
  1084. unsigned ElemSizeArg,
  1085. const Optional<unsigned> &NumElemsArg) {
  1086. AttrBuilder B;
  1087. B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
  1088. return addAttributes(C, Index, B);
  1089. }
  1090. //===----------------------------------------------------------------------===//
  1091. // AttributeList Accessor Methods
  1092. //===----------------------------------------------------------------------===//
  1093. LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
  1094. AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
  1095. return getAttributes(ArgNo + FirstArgIndex);
  1096. }
  1097. AttributeSet AttributeList::getRetAttributes() const {
  1098. return getAttributes(ReturnIndex);
  1099. }
  1100. AttributeSet AttributeList::getFnAttributes() const {
  1101. return getAttributes(FunctionIndex);
  1102. }
  1103. bool AttributeList::hasAttribute(unsigned Index,
  1104. Attribute::AttrKind Kind) const {
  1105. return getAttributes(Index).hasAttribute(Kind);
  1106. }
  1107. bool AttributeList::hasAttribute(unsigned Index, StringRef Kind) const {
  1108. return getAttributes(Index).hasAttribute(Kind);
  1109. }
  1110. bool AttributeList::hasAttributes(unsigned Index) const {
  1111. return getAttributes(Index).hasAttributes();
  1112. }
  1113. bool AttributeList::hasFnAttribute(Attribute::AttrKind Kind) const {
  1114. return pImpl && pImpl->hasFnAttribute(Kind);
  1115. }
  1116. bool AttributeList::hasFnAttribute(StringRef Kind) const {
  1117. return hasAttribute(AttributeList::FunctionIndex, Kind);
  1118. }
  1119. bool AttributeList::hasParamAttribute(unsigned ArgNo,
  1120. Attribute::AttrKind Kind) const {
  1121. return hasAttribute(ArgNo + FirstArgIndex, Kind);
  1122. }
  1123. bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
  1124. unsigned *Index) const {
  1125. if (!pImpl) return false;
  1126. for (unsigned I = index_begin(), E = index_end(); I != E; ++I) {
  1127. if (hasAttribute(I, Attr)) {
  1128. if (Index)
  1129. *Index = I;
  1130. return true;
  1131. }
  1132. }
  1133. return false;
  1134. }
  1135. Attribute AttributeList::getAttribute(unsigned Index,
  1136. Attribute::AttrKind Kind) const {
  1137. return getAttributes(Index).getAttribute(Kind);
  1138. }
  1139. Attribute AttributeList::getAttribute(unsigned Index, StringRef Kind) const {
  1140. return getAttributes(Index).getAttribute(Kind);
  1141. }
  1142. unsigned AttributeList::getRetAlignment() const {
  1143. return getAttributes(ReturnIndex).getAlignment();
  1144. }
  1145. unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
  1146. return getAttributes(ArgNo + FirstArgIndex).getAlignment();
  1147. }
  1148. Type *AttributeList::getParamByValType(unsigned Index) const {
  1149. return getAttributes(Index+FirstArgIndex).getByValType();
  1150. }
  1151. unsigned AttributeList::getStackAlignment(unsigned Index) const {
  1152. return getAttributes(Index).getStackAlignment();
  1153. }
  1154. uint64_t AttributeList::getDereferenceableBytes(unsigned Index) const {
  1155. return getAttributes(Index).getDereferenceableBytes();
  1156. }
  1157. uint64_t AttributeList::getDereferenceableOrNullBytes(unsigned Index) const {
  1158. return getAttributes(Index).getDereferenceableOrNullBytes();
  1159. }
  1160. std::pair<unsigned, Optional<unsigned>>
  1161. AttributeList::getAllocSizeArgs(unsigned Index) const {
  1162. return getAttributes(Index).getAllocSizeArgs();
  1163. }
  1164. std::string AttributeList::getAsString(unsigned Index, bool InAttrGrp) const {
  1165. return getAttributes(Index).getAsString(InAttrGrp);
  1166. }
  1167. AttributeSet AttributeList::getAttributes(unsigned Index) const {
  1168. Index = attrIdxToArrayIdx(Index);
  1169. if (!pImpl || Index >= getNumAttrSets())
  1170. return {};
  1171. return pImpl->begin()[Index];
  1172. }
  1173. AttributeList::iterator AttributeList::begin() const {
  1174. return pImpl ? pImpl->begin() : nullptr;
  1175. }
  1176. AttributeList::iterator AttributeList::end() const {
  1177. return pImpl ? pImpl->end() : nullptr;
  1178. }
  1179. //===----------------------------------------------------------------------===//
  1180. // AttributeList Introspection Methods
  1181. //===----------------------------------------------------------------------===//
  1182. unsigned AttributeList::getNumAttrSets() const {
  1183. return pImpl ? pImpl->NumAttrSets : 0;
  1184. }
  1185. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  1186. LLVM_DUMP_METHOD void AttributeList::dump() const {
  1187. dbgs() << "PAL[\n";
  1188. for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
  1189. if (getAttributes(i).hasAttributes())
  1190. dbgs() << " { " << i << " => " << getAsString(i) << " }\n";
  1191. }
  1192. dbgs() << "]\n";
  1193. }
  1194. #endif
  1195. //===----------------------------------------------------------------------===//
  1196. // AttrBuilder Method Implementations
  1197. //===----------------------------------------------------------------------===//
  1198. // FIXME: Remove this ctor, use AttributeSet.
  1199. AttrBuilder::AttrBuilder(AttributeList AL, unsigned Index) {
  1200. AttributeSet AS = AL.getAttributes(Index);
  1201. for (const auto &A : AS)
  1202. addAttribute(A);
  1203. }
  1204. AttrBuilder::AttrBuilder(AttributeSet AS) {
  1205. for (const auto &A : AS)
  1206. addAttribute(A);
  1207. }
  1208. void AttrBuilder::clear() {
  1209. Attrs.reset();
  1210. TargetDepAttrs.clear();
  1211. Alignment.reset();
  1212. StackAlignment.reset();
  1213. DerefBytes = DerefOrNullBytes = 0;
  1214. AllocSizeArgs = 0;
  1215. ByValType = nullptr;
  1216. }
  1217. AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
  1218. assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
  1219. assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
  1220. Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
  1221. "Adding integer attribute without adding a value!");
  1222. Attrs[Val] = true;
  1223. return *this;
  1224. }
  1225. AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
  1226. if (Attr.isStringAttribute()) {
  1227. addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
  1228. return *this;
  1229. }
  1230. Attribute::AttrKind Kind = Attr.getKindAsEnum();
  1231. Attrs[Kind] = true;
  1232. if (Kind == Attribute::Alignment)
  1233. Alignment = MaybeAlign(Attr.getAlignment());
  1234. else if (Kind == Attribute::StackAlignment)
  1235. StackAlignment = MaybeAlign(Attr.getStackAlignment());
  1236. else if (Kind == Attribute::ByVal)
  1237. ByValType = Attr.getValueAsType();
  1238. else if (Kind == Attribute::Dereferenceable)
  1239. DerefBytes = Attr.getDereferenceableBytes();
  1240. else if (Kind == Attribute::DereferenceableOrNull)
  1241. DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
  1242. else if (Kind == Attribute::AllocSize)
  1243. AllocSizeArgs = Attr.getValueAsInt();
  1244. return *this;
  1245. }
  1246. AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
  1247. TargetDepAttrs[A] = V;
  1248. return *this;
  1249. }
  1250. AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
  1251. assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
  1252. Attrs[Val] = false;
  1253. if (Val == Attribute::Alignment)
  1254. Alignment.reset();
  1255. else if (Val == Attribute::StackAlignment)
  1256. StackAlignment.reset();
  1257. else if (Val == Attribute::ByVal)
  1258. ByValType = nullptr;
  1259. else if (Val == Attribute::Dereferenceable)
  1260. DerefBytes = 0;
  1261. else if (Val == Attribute::DereferenceableOrNull)
  1262. DerefOrNullBytes = 0;
  1263. else if (Val == Attribute::AllocSize)
  1264. AllocSizeArgs = 0;
  1265. return *this;
  1266. }
  1267. AttrBuilder &AttrBuilder::removeAttributes(AttributeList A, uint64_t Index) {
  1268. remove(A.getAttributes(Index));
  1269. return *this;
  1270. }
  1271. AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
  1272. auto I = TargetDepAttrs.find(A);
  1273. if (I != TargetDepAttrs.end())
  1274. TargetDepAttrs.erase(I);
  1275. return *this;
  1276. }
  1277. std::pair<unsigned, Optional<unsigned>> AttrBuilder::getAllocSizeArgs() const {
  1278. return unpackAllocSizeArgs(AllocSizeArgs);
  1279. }
  1280. AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned A) {
  1281. MaybeAlign Align(A);
  1282. if (!Align)
  1283. return *this;
  1284. assert(*Align <= 0x40000000 && "Alignment too large.");
  1285. Attrs[Attribute::Alignment] = true;
  1286. Alignment = Align;
  1287. return *this;
  1288. }
  1289. AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned A) {
  1290. MaybeAlign Align(A);
  1291. // Default alignment, allow the target to define how to align it.
  1292. if (!Align)
  1293. return *this;
  1294. assert(*Align <= 0x100 && "Alignment too large.");
  1295. Attrs[Attribute::StackAlignment] = true;
  1296. StackAlignment = Align;
  1297. return *this;
  1298. }
  1299. AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
  1300. if (Bytes == 0) return *this;
  1301. Attrs[Attribute::Dereferenceable] = true;
  1302. DerefBytes = Bytes;
  1303. return *this;
  1304. }
  1305. AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
  1306. if (Bytes == 0)
  1307. return *this;
  1308. Attrs[Attribute::DereferenceableOrNull] = true;
  1309. DerefOrNullBytes = Bytes;
  1310. return *this;
  1311. }
  1312. AttrBuilder &AttrBuilder::addAllocSizeAttr(unsigned ElemSize,
  1313. const Optional<unsigned> &NumElems) {
  1314. return addAllocSizeAttrFromRawRepr(packAllocSizeArgs(ElemSize, NumElems));
  1315. }
  1316. AttrBuilder &AttrBuilder::addAllocSizeAttrFromRawRepr(uint64_t RawArgs) {
  1317. // (0, 0) is our "not present" value, so we need to check for it here.
  1318. assert(RawArgs && "Invalid allocsize arguments -- given allocsize(0, 0)");
  1319. Attrs[Attribute::AllocSize] = true;
  1320. // Reuse existing machinery to store this as a single 64-bit integer so we can
  1321. // save a few bytes over using a pair<unsigned, Optional<unsigned>>.
  1322. AllocSizeArgs = RawArgs;
  1323. return *this;
  1324. }
  1325. AttrBuilder &AttrBuilder::addByValAttr(Type *Ty) {
  1326. Attrs[Attribute::ByVal] = true;
  1327. ByValType = Ty;
  1328. return *this;
  1329. }
  1330. AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
  1331. // FIXME: What if both have alignments, but they don't match?!
  1332. if (!Alignment)
  1333. Alignment = B.Alignment;
  1334. if (!StackAlignment)
  1335. StackAlignment = B.StackAlignment;
  1336. if (!DerefBytes)
  1337. DerefBytes = B.DerefBytes;
  1338. if (!DerefOrNullBytes)
  1339. DerefOrNullBytes = B.DerefOrNullBytes;
  1340. if (!AllocSizeArgs)
  1341. AllocSizeArgs = B.AllocSizeArgs;
  1342. if (!ByValType)
  1343. ByValType = B.ByValType;
  1344. Attrs |= B.Attrs;
  1345. for (auto I : B.td_attrs())
  1346. TargetDepAttrs[I.first] = I.second;
  1347. return *this;
  1348. }
  1349. AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
  1350. // FIXME: What if both have alignments, but they don't match?!
  1351. if (B.Alignment)
  1352. Alignment.reset();
  1353. if (B.StackAlignment)
  1354. StackAlignment.reset();
  1355. if (B.DerefBytes)
  1356. DerefBytes = 0;
  1357. if (B.DerefOrNullBytes)
  1358. DerefOrNullBytes = 0;
  1359. if (B.AllocSizeArgs)
  1360. AllocSizeArgs = 0;
  1361. if (B.ByValType)
  1362. ByValType = nullptr;
  1363. Attrs &= ~B.Attrs;
  1364. for (auto I : B.td_attrs())
  1365. TargetDepAttrs.erase(I.first);
  1366. return *this;
  1367. }
  1368. bool AttrBuilder::overlaps(const AttrBuilder &B) const {
  1369. // First check if any of the target independent attributes overlap.
  1370. if ((Attrs & B.Attrs).any())
  1371. return true;
  1372. // Then check if any target dependent ones do.
  1373. for (const auto &I : td_attrs())
  1374. if (B.contains(I.first))
  1375. return true;
  1376. return false;
  1377. }
  1378. bool AttrBuilder::contains(StringRef A) const {
  1379. return TargetDepAttrs.find(A) != TargetDepAttrs.end();
  1380. }
  1381. bool AttrBuilder::hasAttributes() const {
  1382. return !Attrs.none() || !TargetDepAttrs.empty();
  1383. }
  1384. bool AttrBuilder::hasAttributes(AttributeList AL, uint64_t Index) const {
  1385. AttributeSet AS = AL.getAttributes(Index);
  1386. for (const auto Attr : AS) {
  1387. if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
  1388. if (contains(Attr.getKindAsEnum()))
  1389. return true;
  1390. } else {
  1391. assert(Attr.isStringAttribute() && "Invalid attribute kind!");
  1392. return contains(Attr.getKindAsString());
  1393. }
  1394. }
  1395. return false;
  1396. }
  1397. bool AttrBuilder::hasAlignmentAttr() const {
  1398. return Alignment != 0;
  1399. }
  1400. bool AttrBuilder::operator==(const AttrBuilder &B) {
  1401. if (Attrs != B.Attrs)
  1402. return false;
  1403. for (td_const_iterator I = TargetDepAttrs.begin(),
  1404. E = TargetDepAttrs.end(); I != E; ++I)
  1405. if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
  1406. return false;
  1407. return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
  1408. DerefBytes == B.DerefBytes && ByValType == B.ByValType;
  1409. }
  1410. //===----------------------------------------------------------------------===//
  1411. // AttributeFuncs Function Defintions
  1412. //===----------------------------------------------------------------------===//
  1413. /// Which attributes cannot be applied to a type.
  1414. AttrBuilder AttributeFuncs::typeIncompatible(Type *Ty) {
  1415. AttrBuilder Incompatible;
  1416. if (!Ty->isIntegerTy())
  1417. // Attribute that only apply to integers.
  1418. Incompatible.addAttribute(Attribute::SExt)
  1419. .addAttribute(Attribute::ZExt);
  1420. if (!Ty->isPointerTy())
  1421. // Attribute that only apply to pointers.
  1422. Incompatible.addAttribute(Attribute::ByVal)
  1423. .addAttribute(Attribute::Nest)
  1424. .addAttribute(Attribute::NoAlias)
  1425. .addAttribute(Attribute::NoCapture)
  1426. .addAttribute(Attribute::NonNull)
  1427. .addDereferenceableAttr(1) // the int here is ignored
  1428. .addDereferenceableOrNullAttr(1) // the int here is ignored
  1429. .addAttribute(Attribute::ReadNone)
  1430. .addAttribute(Attribute::ReadOnly)
  1431. .addAttribute(Attribute::StructRet)
  1432. .addAttribute(Attribute::InAlloca);
  1433. return Incompatible;
  1434. }
  1435. template<typename AttrClass>
  1436. static bool isEqual(const Function &Caller, const Function &Callee) {
  1437. return Caller.getFnAttribute(AttrClass::getKind()) ==
  1438. Callee.getFnAttribute(AttrClass::getKind());
  1439. }
  1440. /// Compute the logical AND of the attributes of the caller and the
  1441. /// callee.
  1442. ///
  1443. /// This function sets the caller's attribute to false if the callee's attribute
  1444. /// is false.
  1445. template<typename AttrClass>
  1446. static void setAND(Function &Caller, const Function &Callee) {
  1447. if (AttrClass::isSet(Caller, AttrClass::getKind()) &&
  1448. !AttrClass::isSet(Callee, AttrClass::getKind()))
  1449. AttrClass::set(Caller, AttrClass::getKind(), false);
  1450. }
  1451. /// Compute the logical OR of the attributes of the caller and the
  1452. /// callee.
  1453. ///
  1454. /// This function sets the caller's attribute to true if the callee's attribute
  1455. /// is true.
  1456. template<typename AttrClass>
  1457. static void setOR(Function &Caller, const Function &Callee) {
  1458. if (!AttrClass::isSet(Caller, AttrClass::getKind()) &&
  1459. AttrClass::isSet(Callee, AttrClass::getKind()))
  1460. AttrClass::set(Caller, AttrClass::getKind(), true);
  1461. }
  1462. /// If the inlined function had a higher stack protection level than the
  1463. /// calling function, then bump up the caller's stack protection level.
  1464. static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
  1465. // If upgrading the SSP attribute, clear out the old SSP Attributes first.
  1466. // Having multiple SSP attributes doesn't actually hurt, but it adds useless
  1467. // clutter to the IR.
  1468. AttrBuilder OldSSPAttr;
  1469. OldSSPAttr.addAttribute(Attribute::StackProtect)
  1470. .addAttribute(Attribute::StackProtectStrong)
  1471. .addAttribute(Attribute::StackProtectReq);
  1472. if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
  1473. Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
  1474. Caller.addFnAttr(Attribute::StackProtectReq);
  1475. } else if (Callee.hasFnAttribute(Attribute::StackProtectStrong) &&
  1476. !Caller.hasFnAttribute(Attribute::StackProtectReq)) {
  1477. Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);
  1478. Caller.addFnAttr(Attribute::StackProtectStrong);
  1479. } else if (Callee.hasFnAttribute(Attribute::StackProtect) &&
  1480. !Caller.hasFnAttribute(Attribute::StackProtectReq) &&
  1481. !Caller.hasFnAttribute(Attribute::StackProtectStrong))
  1482. Caller.addFnAttr(Attribute::StackProtect);
  1483. }
  1484. /// If the inlined function required stack probes, then ensure that
  1485. /// the calling function has those too.
  1486. static void adjustCallerStackProbes(Function &Caller, const Function &Callee) {
  1487. if (!Caller.hasFnAttribute("probe-stack") &&
  1488. Callee.hasFnAttribute("probe-stack")) {
  1489. Caller.addFnAttr(Callee.getFnAttribute("probe-stack"));
  1490. }
  1491. }
  1492. /// If the inlined function defines the size of guard region
  1493. /// on the stack, then ensure that the calling function defines a guard region
  1494. /// that is no larger.
  1495. static void
  1496. adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
  1497. if (Callee.hasFnAttribute("stack-probe-size")) {
  1498. uint64_t CalleeStackProbeSize;
  1499. Callee.getFnAttribute("stack-probe-size")
  1500. .getValueAsString()
  1501. .getAsInteger(0, CalleeStackProbeSize);
  1502. if (Caller.hasFnAttribute("stack-probe-size")) {
  1503. uint64_t CallerStackProbeSize;
  1504. Caller.getFnAttribute("stack-probe-size")
  1505. .getValueAsString()
  1506. .getAsInteger(0, CallerStackProbeSize);
  1507. if (CallerStackProbeSize > CalleeStackProbeSize) {
  1508. Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
  1509. }
  1510. } else {
  1511. Caller.addFnAttr(Callee.getFnAttribute("stack-probe-size"));
  1512. }
  1513. }
  1514. }
  1515. /// If the inlined function defines a min legal vector width, then ensure
  1516. /// the calling function has the same or larger min legal vector width. If the
  1517. /// caller has the attribute, but the callee doesn't, we need to remove the
  1518. /// attribute from the caller since we can't make any guarantees about the
  1519. /// caller's requirements.
  1520. /// This function is called after the inlining decision has been made so we have
  1521. /// to merge the attribute this way. Heuristics that would use
  1522. /// min-legal-vector-width to determine inline compatibility would need to be
  1523. /// handled as part of inline cost analysis.
  1524. static void
  1525. adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
  1526. if (Caller.hasFnAttribute("min-legal-vector-width")) {
  1527. if (Callee.hasFnAttribute("min-legal-vector-width")) {
  1528. uint64_t CallerVectorWidth;
  1529. Caller.getFnAttribute("min-legal-vector-width")
  1530. .getValueAsString()
  1531. .getAsInteger(0, CallerVectorWidth);
  1532. uint64_t CalleeVectorWidth;
  1533. Callee.getFnAttribute("min-legal-vector-width")
  1534. .getValueAsString()
  1535. .getAsInteger(0, CalleeVectorWidth);
  1536. if (CallerVectorWidth < CalleeVectorWidth)
  1537. Caller.addFnAttr(Callee.getFnAttribute("min-legal-vector-width"));
  1538. } else {
  1539. // If the callee doesn't have the attribute then we don't know anything
  1540. // and must drop the attribute from the caller.
  1541. Caller.removeFnAttr("min-legal-vector-width");
  1542. }
  1543. }
  1544. }
  1545. /// If the inlined function has "null-pointer-is-valid=true" attribute,
  1546. /// set this attribute in the caller post inlining.
  1547. static void
  1548. adjustNullPointerValidAttr(Function &Caller, const Function &Callee) {
  1549. if (Callee.nullPointerIsDefined() && !Caller.nullPointerIsDefined()) {
  1550. Caller.addFnAttr(Callee.getFnAttribute("null-pointer-is-valid"));
  1551. }
  1552. }
  1553. #define GET_ATTR_COMPAT_FUNC
  1554. #include "AttributesCompatFunc.inc"
  1555. bool AttributeFuncs::areInlineCompatible(const Function &Caller,
  1556. const Function &Callee) {
  1557. return hasCompatibleFnAttrs(Caller, Callee);
  1558. }
  1559. void AttributeFuncs::mergeAttributesForInlining(Function &Caller,
  1560. const Function &Callee) {
  1561. mergeFnAttrs(Caller, Callee);
  1562. }