2
0

Attributes.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. //===-- Attributes.cpp - Implement AttributesList -------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // \file
  11. // \brief This file implements the Attribute, AttributeImpl, AttrBuilder,
  12. // AttributeSetImpl, and AttributeSet classes.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/IR/Attributes.h"
  16. #include "AttributeImpl.h"
  17. #include "LLVMContextImpl.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/IR/Type.h"
  21. #include "llvm/Support/Atomic.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/ManagedStatic.h"
  24. #include "llvm/Support/Mutex.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <algorithm>
  27. using namespace llvm;
  28. //===----------------------------------------------------------------------===//
  29. // Attribute Construction Methods
  30. //===----------------------------------------------------------------------===//
  31. Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
  32. uint64_t Val) {
  33. LLVMContextImpl *pImpl = Context.pImpl;
  34. FoldingSetNodeID ID;
  35. ID.AddInteger(Kind);
  36. if (Val) ID.AddInteger(Val);
  37. void *InsertPoint;
  38. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  39. if (!PA) {
  40. // If we didn't find any existing attributes of the same shape then create a
  41. // new one and insert it.
  42. if (!Val)
  43. PA = new EnumAttributeImpl(Kind);
  44. else
  45. PA = new IntAttributeImpl(Kind, Val);
  46. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  47. }
  48. // Return the Attribute that we found or created.
  49. return Attribute(PA);
  50. }
  51. Attribute Attribute::get(LLVMContext &Context, StringRef Kind, StringRef Val) {
  52. LLVMContextImpl *pImpl = Context.pImpl;
  53. FoldingSetNodeID ID;
  54. ID.AddString(Kind);
  55. if (!Val.empty()) ID.AddString(Val);
  56. void *InsertPoint;
  57. AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
  58. if (!PA) {
  59. // If we didn't find any existing attributes of the same shape then create a
  60. // new one and insert it.
  61. PA = new StringAttributeImpl(Kind, Val);
  62. pImpl->AttrsSet.InsertNode(PA, InsertPoint);
  63. }
  64. // Return the Attribute that we found or created.
  65. return Attribute(PA);
  66. }
  67. Attribute Attribute::getWithAlignment(LLVMContext &Context, uint64_t Align) {
  68. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  69. assert(Align <= 0x40000000 && "Alignment too large.");
  70. return get(Context, Alignment, Align);
  71. }
  72. Attribute Attribute::getWithStackAlignment(LLVMContext &Context,
  73. uint64_t Align) {
  74. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  75. assert(Align <= 0x100 && "Alignment too large.");
  76. return get(Context, StackAlignment, Align);
  77. }
  78. Attribute Attribute::getWithDereferenceableBytes(LLVMContext &Context,
  79. uint64_t Bytes) {
  80. assert(Bytes && "Bytes must be non-zero.");
  81. return get(Context, Dereferenceable, Bytes);
  82. }
  83. Attribute Attribute::getWithDereferenceableOrNullBytes(LLVMContext &Context,
  84. uint64_t Bytes) {
  85. assert(Bytes && "Bytes must be non-zero.");
  86. return get(Context, DereferenceableOrNull, Bytes);
  87. }
  88. //===----------------------------------------------------------------------===//
  89. // Attribute Accessor Methods
  90. //===----------------------------------------------------------------------===//
  91. bool Attribute::isEnumAttribute() const {
  92. return pImpl && pImpl->isEnumAttribute();
  93. }
  94. bool Attribute::isIntAttribute() const {
  95. return pImpl && pImpl->isIntAttribute();
  96. }
  97. bool Attribute::isStringAttribute() const {
  98. return pImpl && pImpl->isStringAttribute();
  99. }
  100. Attribute::AttrKind Attribute::getKindAsEnum() const {
  101. if (!pImpl) return None;
  102. assert((isEnumAttribute() || isIntAttribute()) &&
  103. "Invalid attribute type to get the kind as an enum!");
  104. return pImpl ? pImpl->getKindAsEnum() : None;
  105. }
  106. uint64_t Attribute::getValueAsInt() const {
  107. if (!pImpl) return 0;
  108. assert(isIntAttribute() &&
  109. "Expected the attribute to be an integer attribute!");
  110. return pImpl ? pImpl->getValueAsInt() : 0;
  111. }
  112. StringRef Attribute::getKindAsString() const {
  113. if (!pImpl) return StringRef();
  114. assert(isStringAttribute() &&
  115. "Invalid attribute type to get the kind as a string!");
  116. return pImpl ? pImpl->getKindAsString() : StringRef();
  117. }
  118. StringRef Attribute::getValueAsString() const {
  119. if (!pImpl) return StringRef();
  120. assert(isStringAttribute() &&
  121. "Invalid attribute type to get the value as a string!");
  122. return pImpl ? pImpl->getValueAsString() : StringRef();
  123. }
  124. bool Attribute::hasAttribute(AttrKind Kind) const {
  125. return (pImpl && pImpl->hasAttribute(Kind)) || (!pImpl && Kind == None);
  126. }
  127. bool Attribute::hasAttribute(StringRef Kind) const {
  128. if (!isStringAttribute()) return false;
  129. return pImpl && pImpl->hasAttribute(Kind);
  130. }
  131. /// This returns the alignment field of an attribute as a byte alignment value.
  132. unsigned Attribute::getAlignment() const {
  133. assert(hasAttribute(Attribute::Alignment) &&
  134. "Trying to get alignment from non-alignment attribute!");
  135. return pImpl->getValueAsInt();
  136. }
  137. /// This returns the stack alignment field of an attribute as a byte alignment
  138. /// value.
  139. unsigned Attribute::getStackAlignment() const {
  140. assert(hasAttribute(Attribute::StackAlignment) &&
  141. "Trying to get alignment from non-alignment attribute!");
  142. return pImpl->getValueAsInt();
  143. }
  144. /// This returns the number of dereferenceable bytes.
  145. uint64_t Attribute::getDereferenceableBytes() const {
  146. assert(hasAttribute(Attribute::Dereferenceable) &&
  147. "Trying to get dereferenceable bytes from "
  148. "non-dereferenceable attribute!");
  149. return pImpl->getValueAsInt();
  150. }
  151. uint64_t Attribute::getDereferenceableOrNullBytes() const {
  152. assert(hasAttribute(Attribute::DereferenceableOrNull) &&
  153. "Trying to get dereferenceable bytes from "
  154. "non-dereferenceable attribute!");
  155. return pImpl->getValueAsInt();
  156. }
  157. std::string Attribute::getAsString(bool InAttrGrp) const {
  158. if (!pImpl) return "";
  159. if (hasAttribute(Attribute::SanitizeAddress))
  160. return "sanitize_address";
  161. if (hasAttribute(Attribute::AlwaysInline))
  162. return "alwaysinline";
  163. if (hasAttribute(Attribute::ArgMemOnly))
  164. return "argmemonly";
  165. if (hasAttribute(Attribute::Builtin))
  166. return "builtin";
  167. if (hasAttribute(Attribute::ByVal))
  168. return "byval";
  169. if (hasAttribute(Attribute::Convergent))
  170. return "convergent";
  171. if (hasAttribute(Attribute::InAlloca))
  172. return "inalloca";
  173. if (hasAttribute(Attribute::InlineHint))
  174. return "inlinehint";
  175. if (hasAttribute(Attribute::InReg))
  176. return "inreg";
  177. if (hasAttribute(Attribute::JumpTable))
  178. return "jumptable";
  179. if (hasAttribute(Attribute::MinSize))
  180. return "minsize";
  181. if (hasAttribute(Attribute::Naked))
  182. return "naked";
  183. if (hasAttribute(Attribute::Nest))
  184. return "nest";
  185. if (hasAttribute(Attribute::NoAlias))
  186. return "noalias";
  187. if (hasAttribute(Attribute::NoBuiltin))
  188. return "nobuiltin";
  189. if (hasAttribute(Attribute::NoCapture))
  190. return "nocapture";
  191. if (hasAttribute(Attribute::NoDuplicate))
  192. return "noduplicate";
  193. if (hasAttribute(Attribute::NoImplicitFloat))
  194. return "noimplicitfloat";
  195. if (hasAttribute(Attribute::NoInline))
  196. return "noinline";
  197. if (hasAttribute(Attribute::NonLazyBind))
  198. return "nonlazybind";
  199. if (hasAttribute(Attribute::NonNull))
  200. return "nonnull";
  201. if (hasAttribute(Attribute::NoRedZone))
  202. return "noredzone";
  203. if (hasAttribute(Attribute::NoReturn))
  204. return "noreturn";
  205. if (hasAttribute(Attribute::NoUnwind))
  206. return "nounwind";
  207. if (hasAttribute(Attribute::OptimizeNone))
  208. return "optnone";
  209. if (hasAttribute(Attribute::OptimizeForSize))
  210. return "optsize";
  211. if (hasAttribute(Attribute::ReadNone))
  212. return "readnone";
  213. if (hasAttribute(Attribute::ReadOnly))
  214. return "readonly";
  215. if (hasAttribute(Attribute::Returned))
  216. return "returned";
  217. if (hasAttribute(Attribute::ReturnsTwice))
  218. return "returns_twice";
  219. if (hasAttribute(Attribute::SExt))
  220. return "signext";
  221. if (hasAttribute(Attribute::StackProtect))
  222. return "ssp";
  223. if (hasAttribute(Attribute::StackProtectReq))
  224. return "sspreq";
  225. if (hasAttribute(Attribute::StackProtectStrong))
  226. return "sspstrong";
  227. if (hasAttribute(Attribute::SafeStack))
  228. return "safestack";
  229. if (hasAttribute(Attribute::StructRet))
  230. return "sret";
  231. if (hasAttribute(Attribute::SanitizeThread))
  232. return "sanitize_thread";
  233. if (hasAttribute(Attribute::SanitizeMemory))
  234. return "sanitize_memory";
  235. if (hasAttribute(Attribute::UWTable))
  236. return "uwtable";
  237. if (hasAttribute(Attribute::ZExt))
  238. return "zeroext";
  239. if (hasAttribute(Attribute::Cold))
  240. return "cold";
  241. // FIXME: These should be output like this:
  242. //
  243. // align=4
  244. // alignstack=8
  245. //
  246. if (hasAttribute(Attribute::Alignment)) {
  247. std::string Result;
  248. Result += "align";
  249. Result += (InAttrGrp) ? "=" : " ";
  250. Result += utostr(getValueAsInt());
  251. return Result;
  252. }
  253. auto AttrWithBytesToString = [&](const char *Name) {
  254. std::string Result;
  255. Result += Name;
  256. if (InAttrGrp) {
  257. Result += "=";
  258. Result += utostr(getValueAsInt());
  259. } else {
  260. Result += "(";
  261. Result += utostr(getValueAsInt());
  262. Result += ")";
  263. }
  264. return Result;
  265. };
  266. if (hasAttribute(Attribute::StackAlignment))
  267. return AttrWithBytesToString("alignstack");
  268. if (hasAttribute(Attribute::Dereferenceable))
  269. return AttrWithBytesToString("dereferenceable");
  270. if (hasAttribute(Attribute::DereferenceableOrNull))
  271. return AttrWithBytesToString("dereferenceable_or_null");
  272. // Convert target-dependent attributes to strings of the form:
  273. //
  274. // "kind"
  275. // "kind" = "value"
  276. //
  277. if (isStringAttribute()) {
  278. std::string Result;
  279. Result += (Twine('"') + getKindAsString() + Twine('"')).str();
  280. StringRef Val = pImpl->getValueAsString();
  281. if (Val.empty()) return Result;
  282. Result += ("=\"" + Val + Twine('"')).str();
  283. return Result;
  284. }
  285. llvm_unreachable("Unknown attribute");
  286. }
  287. bool Attribute::operator<(Attribute A) const {
  288. if (!pImpl && !A.pImpl) return false;
  289. if (!pImpl) return true;
  290. if (!A.pImpl) return false;
  291. return *pImpl < *A.pImpl;
  292. }
  293. //===----------------------------------------------------------------------===//
  294. // AttributeImpl Definition
  295. //===----------------------------------------------------------------------===//
  296. // Pin the vtables to this file.
  297. AttributeImpl::~AttributeImpl() {}
  298. void EnumAttributeImpl::anchor() {}
  299. void IntAttributeImpl::anchor() {}
  300. void StringAttributeImpl::anchor() {}
  301. bool AttributeImpl::hasAttribute(Attribute::AttrKind A) const {
  302. if (isStringAttribute()) return false;
  303. return getKindAsEnum() == A;
  304. }
  305. bool AttributeImpl::hasAttribute(StringRef Kind) const {
  306. if (!isStringAttribute()) return false;
  307. return getKindAsString() == Kind;
  308. }
  309. Attribute::AttrKind AttributeImpl::getKindAsEnum() const {
  310. assert(isEnumAttribute() || isIntAttribute());
  311. return static_cast<const EnumAttributeImpl *>(this)->getEnumKind();
  312. }
  313. uint64_t AttributeImpl::getValueAsInt() const {
  314. assert(isIntAttribute());
  315. return static_cast<const IntAttributeImpl *>(this)->getValue();
  316. }
  317. StringRef AttributeImpl::getKindAsString() const {
  318. assert(isStringAttribute());
  319. return static_cast<const StringAttributeImpl *>(this)->getStringKind();
  320. }
  321. StringRef AttributeImpl::getValueAsString() const {
  322. assert(isStringAttribute());
  323. return static_cast<const StringAttributeImpl *>(this)->getStringValue();
  324. }
  325. bool AttributeImpl::operator<(const AttributeImpl &AI) const {
  326. // This sorts the attributes with Attribute::AttrKinds coming first (sorted
  327. // relative to their enum value) and then strings.
  328. if (isEnumAttribute()) {
  329. if (AI.isEnumAttribute()) return getKindAsEnum() < AI.getKindAsEnum();
  330. if (AI.isIntAttribute()) return true;
  331. if (AI.isStringAttribute()) return true;
  332. }
  333. if (isIntAttribute()) {
  334. if (AI.isEnumAttribute()) return false;
  335. if (AI.isIntAttribute()) return getValueAsInt() < AI.getValueAsInt();
  336. if (AI.isStringAttribute()) return true;
  337. }
  338. if (AI.isEnumAttribute()) return false;
  339. if (AI.isIntAttribute()) return false;
  340. if (getKindAsString() == AI.getKindAsString())
  341. return getValueAsString() < AI.getValueAsString();
  342. return getKindAsString() < AI.getKindAsString();
  343. }
  344. uint64_t AttributeImpl::getAttrMask(Attribute::AttrKind Val) {
  345. // FIXME: Remove this.
  346. switch (Val) {
  347. case Attribute::EndAttrKinds:
  348. llvm_unreachable("Synthetic enumerators which should never get here");
  349. case Attribute::None: return 0;
  350. case Attribute::ZExt: return 1 << 0;
  351. case Attribute::SExt: return 1 << 1;
  352. case Attribute::NoReturn: return 1 << 2;
  353. case Attribute::InReg: return 1 << 3;
  354. case Attribute::StructRet: return 1 << 4;
  355. case Attribute::NoUnwind: return 1 << 5;
  356. case Attribute::NoAlias: return 1 << 6;
  357. case Attribute::ByVal: return 1 << 7;
  358. case Attribute::Nest: return 1 << 8;
  359. case Attribute::ReadNone: return 1 << 9;
  360. case Attribute::ReadOnly: return 1 << 10;
  361. case Attribute::NoInline: return 1 << 11;
  362. case Attribute::AlwaysInline: return 1 << 12;
  363. case Attribute::OptimizeForSize: return 1 << 13;
  364. case Attribute::StackProtect: return 1 << 14;
  365. case Attribute::StackProtectReq: return 1 << 15;
  366. case Attribute::Alignment: return 31 << 16;
  367. case Attribute::NoCapture: return 1 << 21;
  368. case Attribute::NoRedZone: return 1 << 22;
  369. case Attribute::NoImplicitFloat: return 1 << 23;
  370. case Attribute::Naked: return 1 << 24;
  371. case Attribute::InlineHint: return 1 << 25;
  372. case Attribute::StackAlignment: return 7 << 26;
  373. case Attribute::ReturnsTwice: return 1 << 29;
  374. case Attribute::UWTable: return 1 << 30;
  375. case Attribute::NonLazyBind: return 1U << 31;
  376. case Attribute::SanitizeAddress: return 1ULL << 32;
  377. case Attribute::MinSize: return 1ULL << 33;
  378. case Attribute::NoDuplicate: return 1ULL << 34;
  379. case Attribute::StackProtectStrong: return 1ULL << 35;
  380. case Attribute::SanitizeThread: return 1ULL << 36;
  381. case Attribute::SanitizeMemory: return 1ULL << 37;
  382. case Attribute::NoBuiltin: return 1ULL << 38;
  383. case Attribute::Returned: return 1ULL << 39;
  384. case Attribute::Cold: return 1ULL << 40;
  385. case Attribute::Builtin: return 1ULL << 41;
  386. case Attribute::OptimizeNone: return 1ULL << 42;
  387. case Attribute::InAlloca: return 1ULL << 43;
  388. case Attribute::NonNull: return 1ULL << 44;
  389. case Attribute::JumpTable: return 1ULL << 45;
  390. case Attribute::Convergent: return 1ULL << 46;
  391. case Attribute::SafeStack: return 1ULL << 47;
  392. case Attribute::Dereferenceable:
  393. llvm_unreachable("dereferenceable attribute not supported in raw format");
  394. break;
  395. case Attribute::DereferenceableOrNull:
  396. llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
  397. "format");
  398. break;
  399. case Attribute::ArgMemOnly:
  400. llvm_unreachable("argmemonly attribute not supported in raw format");
  401. break;
  402. }
  403. llvm_unreachable("Unsupported attribute type");
  404. }
  405. //===----------------------------------------------------------------------===//
  406. // AttributeSetNode Definition
  407. //===----------------------------------------------------------------------===//
  408. AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
  409. ArrayRef<Attribute> Attrs) {
  410. if (Attrs.empty())
  411. return nullptr;
  412. // Otherwise, build a key to look up the existing attributes.
  413. LLVMContextImpl *pImpl = C.pImpl;
  414. FoldingSetNodeID ID;
  415. SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
  416. array_pod_sort(SortedAttrs.begin(), SortedAttrs.end());
  417. for (SmallVectorImpl<Attribute>::iterator I = SortedAttrs.begin(),
  418. E = SortedAttrs.end(); I != E; ++I)
  419. I->Profile(ID);
  420. void *InsertPoint;
  421. AttributeSetNode *PA =
  422. pImpl->AttrsSetNodes.FindNodeOrInsertPos(ID, InsertPoint);
  423. // If we didn't find any existing attributes of the same shape then create a
  424. // new one and insert it.
  425. if (!PA) {
  426. // Coallocate entries after the AttributeSetNode itself.
  427. void *Mem = ::operator new(sizeof(AttributeSetNode) +
  428. sizeof(Attribute) * SortedAttrs.size());
  429. PA = new (Mem) AttributeSetNode(SortedAttrs);
  430. pImpl->AttrsSetNodes.InsertNode(PA, InsertPoint);
  431. }
  432. // Return the AttributesListNode that we found or created.
  433. return PA;
  434. }
  435. bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
  436. for (iterator I = begin(), E = end(); I != E; ++I)
  437. if (I->hasAttribute(Kind))
  438. return true;
  439. return false;
  440. }
  441. bool AttributeSetNode::hasAttribute(StringRef Kind) const {
  442. for (iterator I = begin(), E = end(); I != E; ++I)
  443. if (I->hasAttribute(Kind))
  444. return true;
  445. return false;
  446. }
  447. Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
  448. for (iterator I = begin(), E = end(); I != E; ++I)
  449. if (I->hasAttribute(Kind))
  450. return *I;
  451. return Attribute();
  452. }
  453. Attribute AttributeSetNode::getAttribute(StringRef Kind) const {
  454. for (iterator I = begin(), E = end(); I != E; ++I)
  455. if (I->hasAttribute(Kind))
  456. return *I;
  457. return Attribute();
  458. }
  459. unsigned AttributeSetNode::getAlignment() const {
  460. for (iterator I = begin(), E = end(); I != E; ++I)
  461. if (I->hasAttribute(Attribute::Alignment))
  462. return I->getAlignment();
  463. return 0;
  464. }
  465. unsigned AttributeSetNode::getStackAlignment() const {
  466. for (iterator I = begin(), E = end(); I != E; ++I)
  467. if (I->hasAttribute(Attribute::StackAlignment))
  468. return I->getStackAlignment();
  469. return 0;
  470. }
  471. uint64_t AttributeSetNode::getDereferenceableBytes() const {
  472. for (iterator I = begin(), E = end(); I != E; ++I)
  473. if (I->hasAttribute(Attribute::Dereferenceable))
  474. return I->getDereferenceableBytes();
  475. return 0;
  476. }
  477. uint64_t AttributeSetNode::getDereferenceableOrNullBytes() const {
  478. for (iterator I = begin(), E = end(); I != E; ++I)
  479. if (I->hasAttribute(Attribute::DereferenceableOrNull))
  480. return I->getDereferenceableOrNullBytes();
  481. return 0;
  482. }
  483. std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
  484. std::string Str;
  485. for (iterator I = begin(), E = end(); I != E; ++I) {
  486. if (I != begin())
  487. Str += ' ';
  488. Str += I->getAsString(InAttrGrp);
  489. }
  490. return Str;
  491. }
  492. //===----------------------------------------------------------------------===//
  493. // AttributeSetImpl Definition
  494. //===----------------------------------------------------------------------===//
  495. uint64_t AttributeSetImpl::Raw(unsigned Index) const {
  496. for (unsigned I = 0, E = getNumAttributes(); I != E; ++I) {
  497. if (getSlotIndex(I) != Index) continue;
  498. const AttributeSetNode *ASN = getSlotNode(I);
  499. uint64_t Mask = 0;
  500. for (AttributeSetNode::iterator II = ASN->begin(),
  501. IE = ASN->end(); II != IE; ++II) {
  502. Attribute Attr = *II;
  503. // This cannot handle string attributes.
  504. if (Attr.isStringAttribute()) continue;
  505. Attribute::AttrKind Kind = Attr.getKindAsEnum();
  506. if (Kind == Attribute::Alignment)
  507. Mask |= ((uint64_t)(Log2_32(ASN->getAlignment())) + 1) << 16; // HLSL Change: widen to 64-bits before shift not after
  508. else if (Kind == Attribute::StackAlignment)
  509. Mask |= ((uint64_t)(Log2_32(ASN->getStackAlignment())) + 1) << 26; // HLSL Change: widen to 64-bits before shift not after
  510. else if (Kind == Attribute::Dereferenceable)
  511. llvm_unreachable("dereferenceable not supported in bit mask");
  512. else
  513. Mask |= AttributeImpl::getAttrMask(Kind);
  514. }
  515. return Mask;
  516. }
  517. return 0;
  518. }
  519. void AttributeSetImpl::dump() const {
  520. AttributeSet(const_cast<AttributeSetImpl *>(this)).dump();
  521. }
  522. //===----------------------------------------------------------------------===//
  523. // AttributeSet Construction and Mutation Methods
  524. //===----------------------------------------------------------------------===//
  525. AttributeSet
  526. AttributeSet::getImpl(LLVMContext &C,
  527. ArrayRef<std::pair<unsigned, AttributeSetNode*> > Attrs) {
  528. LLVMContextImpl *pImpl = C.pImpl;
  529. FoldingSetNodeID ID;
  530. AttributeSetImpl::Profile(ID, Attrs);
  531. void *InsertPoint;
  532. AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
  533. // If we didn't find any existing attributes of the same shape then
  534. // create a new one and insert it.
  535. if (!PA) {
  536. // Coallocate entries after the AttributeSetImpl itself.
  537. void *Mem = ::operator new(sizeof(AttributeSetImpl) +
  538. sizeof(std::pair<unsigned, AttributeSetNode *>) *
  539. Attrs.size());
  540. PA = new (Mem) AttributeSetImpl(C, Attrs);
  541. pImpl->AttrsLists.InsertNode(PA, InsertPoint);
  542. }
  543. // Return the AttributesList that we found or created.
  544. return AttributeSet(PA);
  545. }
  546. AttributeSet AttributeSet::get(LLVMContext &C,
  547. ArrayRef<std::pair<unsigned, Attribute> > Attrs){
  548. // If there are no attributes then return a null AttributesList pointer.
  549. if (Attrs.empty())
  550. return AttributeSet();
  551. #ifndef NDEBUG
  552. for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
  553. assert((!i || Attrs[i-1].first <= Attrs[i].first) &&
  554. "Misordered Attributes list!");
  555. assert(!Attrs[i].second.hasAttribute(Attribute::None) &&
  556. "Pointless attribute!");
  557. }
  558. #endif
  559. // Create a vector if (unsigned, AttributeSetNode*) pairs from the attributes
  560. // list.
  561. SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrPairVec;
  562. for (ArrayRef<std::pair<unsigned, Attribute> >::iterator I = Attrs.begin(),
  563. E = Attrs.end(); I != E; ) {
  564. unsigned Index = I->first;
  565. SmallVector<Attribute, 4> AttrVec;
  566. while (I != E && I->first == Index) {
  567. AttrVec.push_back(I->second);
  568. ++I;
  569. }
  570. AttrPairVec.push_back(std::make_pair(Index,
  571. AttributeSetNode::get(C, AttrVec)));
  572. }
  573. return getImpl(C, AttrPairVec);
  574. }
  575. AttributeSet AttributeSet::get(LLVMContext &C,
  576. ArrayRef<std::pair<unsigned,
  577. AttributeSetNode*> > Attrs) {
  578. // If there are no attributes then return a null AttributesList pointer.
  579. if (Attrs.empty())
  580. return AttributeSet();
  581. return getImpl(C, Attrs);
  582. }
  583. AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
  584. const AttrBuilder &B) {
  585. if (!B.hasAttributes())
  586. return AttributeSet();
  587. // Add target-independent attributes.
  588. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  589. for (Attribute::AttrKind Kind = Attribute::None;
  590. Kind != Attribute::EndAttrKinds; Kind = Attribute::AttrKind(Kind + 1)) {
  591. if (!B.contains(Kind))
  592. continue;
  593. if (Kind == Attribute::Alignment)
  594. Attrs.push_back(std::make_pair(Index, Attribute::
  595. getWithAlignment(C, B.getAlignment())));
  596. else if (Kind == Attribute::StackAlignment)
  597. Attrs.push_back(std::make_pair(Index, Attribute::
  598. getWithStackAlignment(C, B.getStackAlignment())));
  599. else if (Kind == Attribute::Dereferenceable)
  600. Attrs.push_back(std::make_pair(Index,
  601. Attribute::getWithDereferenceableBytes(C,
  602. B.getDereferenceableBytes())));
  603. else if (Kind == Attribute::DereferenceableOrNull)
  604. Attrs.push_back(
  605. std::make_pair(Index, Attribute::getWithDereferenceableOrNullBytes(
  606. C, B.getDereferenceableOrNullBytes())));
  607. else
  608. Attrs.push_back(std::make_pair(Index, Attribute::get(C, Kind)));
  609. }
  610. // Add target-dependent (string) attributes.
  611. for (const auto &TDA : B.td_attrs())
  612. Attrs.push_back(
  613. std::make_pair(Index, Attribute::get(C, TDA.first, TDA.second)));
  614. return get(C, Attrs);
  615. }
  616. AttributeSet AttributeSet::get(LLVMContext &C, unsigned Index,
  617. ArrayRef<Attribute::AttrKind> Kind) {
  618. SmallVector<std::pair<unsigned, Attribute>, 8> Attrs;
  619. for (ArrayRef<Attribute::AttrKind>::iterator I = Kind.begin(),
  620. E = Kind.end(); I != E; ++I)
  621. Attrs.push_back(std::make_pair(Index, Attribute::get(C, *I)));
  622. return get(C, Attrs);
  623. }
  624. AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
  625. if (Attrs.empty()) return AttributeSet();
  626. if (Attrs.size() == 1) return Attrs[0];
  627. SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
  628. AttributeSetImpl *A0 = Attrs[0].pImpl;
  629. if (A0)
  630. AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
  631. // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
  632. // ordered by index. Because we know that each list in Attrs is ordered by
  633. // index we only need to merge each successive list in rather than doing a
  634. // full sort.
  635. for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
  636. AttributeSetImpl *AS = Attrs[I].pImpl;
  637. if (!AS) continue;
  638. SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
  639. ANVI = AttrNodeVec.begin(), ANVE;
  640. for (const AttributeSetImpl::IndexAttrPair
  641. *AI = AS->getNode(0),
  642. *AE = AS->getNode(AS->getNumAttributes());
  643. AI != AE; ++AI) {
  644. ANVE = AttrNodeVec.end();
  645. while (ANVI != ANVE && ANVI->first <= AI->first)
  646. ++ANVI;
  647. ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
  648. }
  649. }
  650. return getImpl(C, AttrNodeVec);
  651. }
  652. AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
  653. Attribute::AttrKind Attr) const {
  654. if (hasAttribute(Index, Attr)) return *this;
  655. return addAttributes(C, Index, AttributeSet::get(C, Index, Attr));
  656. }
  657. AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
  658. StringRef Kind) const {
  659. llvm::AttrBuilder B;
  660. B.addAttribute(Kind);
  661. return addAttributes(C, Index, AttributeSet::get(C, Index, B));
  662. }
  663. AttributeSet AttributeSet::addAttribute(LLVMContext &C, unsigned Index,
  664. StringRef Kind, StringRef Value) const {
  665. llvm::AttrBuilder B;
  666. B.addAttribute(Kind, Value);
  667. return addAttributes(C, Index, AttributeSet::get(C, Index, B));
  668. }
  669. AttributeSet AttributeSet::addAttributes(LLVMContext &C, unsigned Index,
  670. AttributeSet Attrs) const {
  671. if (!pImpl) return Attrs;
  672. if (!Attrs.pImpl) return *this;
  673. #ifndef NDEBUG
  674. // FIXME it is not obvious how this should work for alignment. For now, say
  675. // we can't change a known alignment.
  676. unsigned OldAlign = getParamAlignment(Index);
  677. unsigned NewAlign = Attrs.getParamAlignment(Index);
  678. assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
  679. "Attempt to change alignment!");
  680. #endif
  681. // Add the attribute slots before the one we're trying to add.
  682. SmallVector<AttributeSet, 4> AttrSet;
  683. uint64_t NumAttrs = pImpl->getNumAttributes();
  684. AttributeSet AS;
  685. uint64_t LastIndex = 0;
  686. for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
  687. if (getSlotIndex(I) >= Index) {
  688. if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
  689. break;
  690. }
  691. LastIndex = I + 1;
  692. AttrSet.push_back(getSlotAttributes(I));
  693. }
  694. // Now add the attribute into the correct slot. There may already be an
  695. // AttributeSet there.
  696. AttrBuilder B(AS, Index);
  697. for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
  698. if (Attrs.getSlotIndex(I) == Index) {
  699. for (AttributeSetImpl::iterator II = Attrs.pImpl->begin(I),
  700. IE = Attrs.pImpl->end(I); II != IE; ++II)
  701. B.addAttribute(*II);
  702. break;
  703. }
  704. AttrSet.push_back(AttributeSet::get(C, Index, B));
  705. // Add the remaining attribute slots.
  706. for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
  707. AttrSet.push_back(getSlotAttributes(I));
  708. return get(C, AttrSet);
  709. }
  710. AttributeSet AttributeSet::removeAttribute(LLVMContext &C, unsigned Index,
  711. Attribute::AttrKind Attr) const {
  712. if (!hasAttribute(Index, Attr)) return *this;
  713. return removeAttributes(C, Index, AttributeSet::get(C, Index, Attr));
  714. }
  715. AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
  716. AttributeSet Attrs) const {
  717. if (!pImpl) return AttributeSet();
  718. if (!Attrs.pImpl) return *this;
  719. // FIXME it is not obvious how this should work for alignment.
  720. // For now, say we can't pass in alignment, which no current use does.
  721. assert(!Attrs.hasAttribute(Index, Attribute::Alignment) &&
  722. "Attempt to change alignment!");
  723. // Add the attribute slots before the one we're trying to add.
  724. SmallVector<AttributeSet, 4> AttrSet;
  725. uint64_t NumAttrs = pImpl->getNumAttributes();
  726. AttributeSet AS;
  727. uint64_t LastIndex = 0;
  728. for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
  729. if (getSlotIndex(I) >= Index) {
  730. if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
  731. break;
  732. }
  733. LastIndex = I + 1;
  734. AttrSet.push_back(getSlotAttributes(I));
  735. }
  736. // Now remove the attribute from the correct slot. There may already be an
  737. // AttributeSet there.
  738. AttrBuilder B(AS, Index);
  739. for (unsigned I = 0, E = Attrs.pImpl->getNumAttributes(); I != E; ++I)
  740. if (Attrs.getSlotIndex(I) == Index) {
  741. B.removeAttributes(Attrs.pImpl->getSlotAttributes(I), Index);
  742. break;
  743. }
  744. AttrSet.push_back(AttributeSet::get(C, Index, B));
  745. // Add the remaining attribute slots.
  746. for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
  747. AttrSet.push_back(getSlotAttributes(I));
  748. return get(C, AttrSet);
  749. }
  750. AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index,
  751. const AttrBuilder &Attrs) const {
  752. if (!pImpl) return AttributeSet();
  753. // FIXME it is not obvious how this should work for alignment.
  754. // For now, say we can't pass in alignment, which no current use does.
  755. assert(!Attrs.hasAlignmentAttr() && "Attempt to change alignment!");
  756. // Add the attribute slots before the one we're trying to add.
  757. SmallVector<AttributeSet, 4> AttrSet;
  758. uint64_t NumAttrs = pImpl->getNumAttributes();
  759. AttributeSet AS;
  760. uint64_t LastIndex = 0;
  761. for (unsigned I = 0, E = NumAttrs; I != E; ++I) {
  762. if (getSlotIndex(I) >= Index) {
  763. if (getSlotIndex(I) == Index) AS = getSlotAttributes(LastIndex++);
  764. break;
  765. }
  766. LastIndex = I + 1;
  767. AttrSet.push_back(getSlotAttributes(I));
  768. }
  769. // Now remove the attribute from the correct slot. There may already be an
  770. // AttributeSet there.
  771. AttrBuilder B(AS, Index);
  772. B.remove(Attrs);
  773. AttrSet.push_back(AttributeSet::get(C, Index, B));
  774. // Add the remaining attribute slots.
  775. for (unsigned I = LastIndex, E = NumAttrs; I < E; ++I)
  776. AttrSet.push_back(getSlotAttributes(I));
  777. return get(C, AttrSet);
  778. }
  779. AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index,
  780. uint64_t Bytes) const {
  781. llvm::AttrBuilder B;
  782. B.addDereferenceableAttr(Bytes);
  783. return addAttributes(C, Index, AttributeSet::get(C, Index, B));
  784. }
  785. AttributeSet AttributeSet::addDereferenceableOrNullAttr(LLVMContext &C,
  786. unsigned Index,
  787. uint64_t Bytes) const {
  788. llvm::AttrBuilder B;
  789. B.addDereferenceableOrNullAttr(Bytes);
  790. return addAttributes(C, Index, AttributeSet::get(C, Index, B));
  791. }
  792. //===----------------------------------------------------------------------===//
  793. // AttributeSet Accessor Methods
  794. //===----------------------------------------------------------------------===//
  795. LLVMContext &AttributeSet::getContext() const {
  796. return pImpl->getContext();
  797. }
  798. AttributeSet AttributeSet::getParamAttributes(unsigned Index) const {
  799. return pImpl && hasAttributes(Index) ?
  800. AttributeSet::get(pImpl->getContext(),
  801. ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
  802. std::make_pair(Index, getAttributes(Index)))) :
  803. AttributeSet();
  804. }
  805. AttributeSet AttributeSet::getRetAttributes() const {
  806. return pImpl && hasAttributes(ReturnIndex) ?
  807. AttributeSet::get(pImpl->getContext(),
  808. ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
  809. std::make_pair(ReturnIndex,
  810. getAttributes(ReturnIndex)))) :
  811. AttributeSet();
  812. }
  813. AttributeSet AttributeSet::getFnAttributes() const {
  814. return pImpl && hasAttributes(FunctionIndex) ?
  815. AttributeSet::get(pImpl->getContext(),
  816. ArrayRef<std::pair<unsigned, AttributeSetNode*> >(
  817. std::make_pair(FunctionIndex,
  818. getAttributes(FunctionIndex)))) :
  819. AttributeSet();
  820. }
  821. bool AttributeSet::hasAttribute(unsigned Index, Attribute::AttrKind Kind) const{
  822. AttributeSetNode *ASN = getAttributes(Index);
  823. return ASN ? ASN->hasAttribute(Kind) : false;
  824. }
  825. bool AttributeSet::hasAttribute(unsigned Index, StringRef Kind) const {
  826. AttributeSetNode *ASN = getAttributes(Index);
  827. return ASN ? ASN->hasAttribute(Kind) : false;
  828. }
  829. bool AttributeSet::hasAttributes(unsigned Index) const {
  830. AttributeSetNode *ASN = getAttributes(Index);
  831. return ASN ? ASN->hasAttributes() : false;
  832. }
  833. /// \brief Return true if the specified attribute is set for at least one
  834. /// parameter or for the return value.
  835. bool AttributeSet::hasAttrSomewhere(Attribute::AttrKind Attr) const {
  836. if (!pImpl) return false;
  837. for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
  838. for (AttributeSetImpl::iterator II = pImpl->begin(I),
  839. IE = pImpl->end(I); II != IE; ++II)
  840. if (II->hasAttribute(Attr))
  841. return true;
  842. return false;
  843. }
  844. Attribute AttributeSet::getAttribute(unsigned Index,
  845. Attribute::AttrKind Kind) const {
  846. AttributeSetNode *ASN = getAttributes(Index);
  847. return ASN ? ASN->getAttribute(Kind) : Attribute();
  848. }
  849. Attribute AttributeSet::getAttribute(unsigned Index,
  850. StringRef Kind) const {
  851. AttributeSetNode *ASN = getAttributes(Index);
  852. return ASN ? ASN->getAttribute(Kind) : Attribute();
  853. }
  854. unsigned AttributeSet::getParamAlignment(unsigned Index) const {
  855. AttributeSetNode *ASN = getAttributes(Index);
  856. return ASN ? ASN->getAlignment() : 0;
  857. }
  858. unsigned AttributeSet::getStackAlignment(unsigned Index) const {
  859. AttributeSetNode *ASN = getAttributes(Index);
  860. return ASN ? ASN->getStackAlignment() : 0;
  861. }
  862. uint64_t AttributeSet::getDereferenceableBytes(unsigned Index) const {
  863. AttributeSetNode *ASN = getAttributes(Index);
  864. return ASN ? ASN->getDereferenceableBytes() : 0;
  865. }
  866. uint64_t AttributeSet::getDereferenceableOrNullBytes(unsigned Index) const {
  867. AttributeSetNode *ASN = getAttributes(Index);
  868. return ASN ? ASN->getDereferenceableOrNullBytes() : 0;
  869. }
  870. std::string AttributeSet::getAsString(unsigned Index,
  871. bool InAttrGrp) const {
  872. AttributeSetNode *ASN = getAttributes(Index);
  873. return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
  874. }
  875. /// \brief The attributes for the specified index are returned.
  876. AttributeSetNode *AttributeSet::getAttributes(unsigned Index) const {
  877. if (!pImpl) return nullptr;
  878. // Loop through to find the attribute node we want.
  879. for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I)
  880. if (pImpl->getSlotIndex(I) == Index)
  881. return pImpl->getSlotNode(I);
  882. return nullptr;
  883. }
  884. AttributeSet::iterator AttributeSet::begin(unsigned Slot) const {
  885. if (!pImpl)
  886. return ArrayRef<Attribute>().begin();
  887. return pImpl->begin(Slot);
  888. }
  889. AttributeSet::iterator AttributeSet::end(unsigned Slot) const {
  890. if (!pImpl)
  891. return ArrayRef<Attribute>().end();
  892. return pImpl->end(Slot);
  893. }
  894. //===----------------------------------------------------------------------===//
  895. // AttributeSet Introspection Methods
  896. //===----------------------------------------------------------------------===//
  897. /// \brief Return the number of slots used in this attribute list. This is the
  898. /// number of arguments that have an attribute set on them (including the
  899. /// function itself).
  900. unsigned AttributeSet::getNumSlots() const {
  901. return pImpl ? pImpl->getNumAttributes() : 0;
  902. }
  903. unsigned AttributeSet::getSlotIndex(unsigned Slot) const {
  904. assert(pImpl && Slot < pImpl->getNumAttributes() &&
  905. "Slot # out of range!");
  906. return pImpl->getSlotIndex(Slot);
  907. }
  908. AttributeSet AttributeSet::getSlotAttributes(unsigned Slot) const {
  909. assert(pImpl && Slot < pImpl->getNumAttributes() &&
  910. "Slot # out of range!");
  911. return pImpl->getSlotAttributes(Slot);
  912. }
  913. uint64_t AttributeSet::Raw(unsigned Index) const {
  914. // FIXME: Remove this.
  915. return pImpl ? pImpl->Raw(Index) : 0;
  916. }
  917. void AttributeSet::dump() const {
  918. dbgs() << "PAL[\n";
  919. for (unsigned i = 0, e = getNumSlots(); i < e; ++i) {
  920. uint64_t Index = getSlotIndex(i);
  921. dbgs() << " { ";
  922. if (Index == ~0U)
  923. dbgs() << "~0U";
  924. else
  925. dbgs() << Index;
  926. dbgs() << " => " << getAsString(Index) << " }\n";
  927. }
  928. dbgs() << "]\n";
  929. }
  930. //===----------------------------------------------------------------------===//
  931. // AttrBuilder Method Implementations
  932. //===----------------------------------------------------------------------===//
  933. AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Index)
  934. : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
  935. DerefOrNullBytes(0) {
  936. AttributeSetImpl *pImpl = AS.pImpl;
  937. if (!pImpl) return;
  938. for (unsigned I = 0, E = pImpl->getNumAttributes(); I != E; ++I) {
  939. if (pImpl->getSlotIndex(I) != Index) continue;
  940. for (AttributeSetImpl::iterator II = pImpl->begin(I),
  941. IE = pImpl->end(I); II != IE; ++II)
  942. addAttribute(*II);
  943. break;
  944. }
  945. }
  946. void AttrBuilder::clear() {
  947. Attrs.reset();
  948. Alignment = StackAlignment = DerefBytes = DerefOrNullBytes = 0;
  949. }
  950. AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
  951. assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
  952. assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
  953. Val != Attribute::Dereferenceable &&
  954. "Adding integer attribute without adding a value!");
  955. Attrs[Val] = true;
  956. return *this;
  957. }
  958. AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
  959. if (Attr.isStringAttribute()) {
  960. addAttribute(Attr.getKindAsString(), Attr.getValueAsString());
  961. return *this;
  962. }
  963. Attribute::AttrKind Kind = Attr.getKindAsEnum();
  964. Attrs[Kind] = true;
  965. if (Kind == Attribute::Alignment)
  966. Alignment = Attr.getAlignment();
  967. else if (Kind == Attribute::StackAlignment)
  968. StackAlignment = Attr.getStackAlignment();
  969. else if (Kind == Attribute::Dereferenceable)
  970. DerefBytes = Attr.getDereferenceableBytes();
  971. else if (Kind == Attribute::DereferenceableOrNull)
  972. DerefOrNullBytes = Attr.getDereferenceableOrNullBytes();
  973. return *this;
  974. }
  975. AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
  976. TargetDepAttrs[A] = V;
  977. return *this;
  978. }
  979. AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
  980. assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
  981. Attrs[Val] = false;
  982. if (Val == Attribute::Alignment)
  983. Alignment = 0;
  984. else if (Val == Attribute::StackAlignment)
  985. StackAlignment = 0;
  986. else if (Val == Attribute::Dereferenceable)
  987. DerefBytes = 0;
  988. else if (Val == Attribute::DereferenceableOrNull)
  989. DerefOrNullBytes = 0;
  990. return *this;
  991. }
  992. AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
  993. unsigned Slot = ~0U;
  994. for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
  995. if (A.getSlotIndex(I) == Index) {
  996. Slot = I;
  997. break;
  998. }
  999. assert(Slot != ~0U && "Couldn't find index in AttributeSet!");
  1000. for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot); I != E; ++I) {
  1001. Attribute Attr = *I;
  1002. if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
  1003. Attribute::AttrKind Kind = I->getKindAsEnum();
  1004. Attrs[Kind] = false;
  1005. if (Kind == Attribute::Alignment)
  1006. Alignment = 0;
  1007. else if (Kind == Attribute::StackAlignment)
  1008. StackAlignment = 0;
  1009. else if (Kind == Attribute::Dereferenceable)
  1010. DerefBytes = 0;
  1011. else if (Kind == Attribute::DereferenceableOrNull)
  1012. DerefOrNullBytes = 0;
  1013. } else {
  1014. assert(Attr.isStringAttribute() && "Invalid attribute type!");
  1015. std::map<std::string, std::string>::iterator
  1016. Iter = TargetDepAttrs.find(Attr.getKindAsString());
  1017. if (Iter != TargetDepAttrs.end())
  1018. TargetDepAttrs.erase(Iter);
  1019. }
  1020. }
  1021. return *this;
  1022. }
  1023. AttrBuilder &AttrBuilder::removeAttribute(StringRef A) {
  1024. std::map<std::string, std::string>::iterator I = TargetDepAttrs.find(A);
  1025. if (I != TargetDepAttrs.end())
  1026. TargetDepAttrs.erase(I);
  1027. return *this;
  1028. }
  1029. AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
  1030. if (Align == 0) return *this;
  1031. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  1032. assert(Align <= 0x40000000 && "Alignment too large.");
  1033. Attrs[Attribute::Alignment] = true;
  1034. Alignment = Align;
  1035. return *this;
  1036. }
  1037. AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
  1038. // Default alignment, allow the target to define how to align it.
  1039. if (Align == 0) return *this;
  1040. assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
  1041. assert(Align <= 0x100 && "Alignment too large.");
  1042. Attrs[Attribute::StackAlignment] = true;
  1043. StackAlignment = Align;
  1044. return *this;
  1045. }
  1046. AttrBuilder &AttrBuilder::addDereferenceableAttr(uint64_t Bytes) {
  1047. if (Bytes == 0) return *this;
  1048. Attrs[Attribute::Dereferenceable] = true;
  1049. DerefBytes = Bytes;
  1050. return *this;
  1051. }
  1052. AttrBuilder &AttrBuilder::addDereferenceableOrNullAttr(uint64_t Bytes) {
  1053. if (Bytes == 0)
  1054. return *this;
  1055. Attrs[Attribute::DereferenceableOrNull] = true;
  1056. DerefOrNullBytes = Bytes;
  1057. return *this;
  1058. }
  1059. AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
  1060. // FIXME: What if both have alignments, but they don't match?!
  1061. if (!Alignment)
  1062. Alignment = B.Alignment;
  1063. if (!StackAlignment)
  1064. StackAlignment = B.StackAlignment;
  1065. if (!DerefBytes)
  1066. DerefBytes = B.DerefBytes;
  1067. if (!DerefOrNullBytes)
  1068. DerefOrNullBytes = B.DerefOrNullBytes;
  1069. Attrs |= B.Attrs;
  1070. for (const auto &I : B.td_attrs())
  1071. TargetDepAttrs[I.first] = I.second;
  1072. return *this;
  1073. }
  1074. AttrBuilder &AttrBuilder::remove(const AttrBuilder &B) {
  1075. // FIXME: What if both have alignments, but they don't match?!
  1076. if (B.Alignment)
  1077. Alignment = 0;
  1078. if (B.StackAlignment)
  1079. StackAlignment = 0;
  1080. if (B.DerefBytes)
  1081. DerefBytes = 0;
  1082. if (B.DerefOrNullBytes)
  1083. DerefOrNullBytes = 0;
  1084. Attrs &= ~B.Attrs;
  1085. for (const auto &I : B.td_attrs())
  1086. TargetDepAttrs.erase(I.first);
  1087. return *this;
  1088. }
  1089. bool AttrBuilder::overlaps(const AttrBuilder &B) const {
  1090. // First check if any of the target independent attributes overlap.
  1091. if ((Attrs & B.Attrs).any())
  1092. return true;
  1093. // Then check if any target dependent ones do.
  1094. for (const auto &I : td_attrs())
  1095. if (B.contains(I.first))
  1096. return true;
  1097. return false;
  1098. }
  1099. bool AttrBuilder::contains(StringRef A) const {
  1100. return TargetDepAttrs.find(A) != TargetDepAttrs.end();
  1101. }
  1102. bool AttrBuilder::hasAttributes() const {
  1103. return !Attrs.none() || !TargetDepAttrs.empty();
  1104. }
  1105. bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
  1106. unsigned Slot = ~0U;
  1107. for (unsigned I = 0, E = A.getNumSlots(); I != E; ++I)
  1108. if (A.getSlotIndex(I) == Index) {
  1109. Slot = I;
  1110. break;
  1111. }
  1112. assert(Slot != ~0U && "Couldn't find the index!");
  1113. for (AttributeSet::iterator I = A.begin(Slot), E = A.end(Slot);
  1114. I != E; ++I) {
  1115. Attribute Attr = *I;
  1116. if (Attr.isEnumAttribute() || Attr.isIntAttribute()) {
  1117. if (Attrs[I->getKindAsEnum()])
  1118. return true;
  1119. } else {
  1120. assert(Attr.isStringAttribute() && "Invalid attribute kind!");
  1121. return TargetDepAttrs.find(Attr.getKindAsString())!=TargetDepAttrs.end();
  1122. }
  1123. }
  1124. return false;
  1125. }
  1126. bool AttrBuilder::hasAlignmentAttr() const {
  1127. return Alignment != 0;
  1128. }
  1129. bool AttrBuilder::operator==(const AttrBuilder &B) {
  1130. if (Attrs != B.Attrs)
  1131. return false;
  1132. for (td_const_iterator I = TargetDepAttrs.begin(),
  1133. E = TargetDepAttrs.end(); I != E; ++I)
  1134. if (B.TargetDepAttrs.find(I->first) == B.TargetDepAttrs.end())
  1135. return false;
  1136. return Alignment == B.Alignment && StackAlignment == B.StackAlignment &&
  1137. DerefBytes == B.DerefBytes;
  1138. }
  1139. AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
  1140. // FIXME: Remove this in 4.0.
  1141. if (!Val) return *this;
  1142. for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
  1143. I = Attribute::AttrKind(I + 1)) {
  1144. if (I == Attribute::Dereferenceable ||
  1145. I == Attribute::DereferenceableOrNull ||
  1146. I == Attribute::ArgMemOnly)
  1147. continue;
  1148. if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
  1149. Attrs[I] = true;
  1150. if (I == Attribute::Alignment)
  1151. Alignment = 1ULL << ((A >> 16) - 1);
  1152. else if (I == Attribute::StackAlignment)
  1153. StackAlignment = 1ULL << ((A >> 26)-1);
  1154. }
  1155. }
  1156. return *this;
  1157. }
  1158. //===----------------------------------------------------------------------===//
  1159. // AttributeFuncs Function Defintions
  1160. //===----------------------------------------------------------------------===//
  1161. /// \brief Which attributes cannot be applied to a type.
  1162. AttrBuilder AttributeFuncs::typeIncompatible(const Type *Ty) {
  1163. AttrBuilder Incompatible;
  1164. if (!Ty->isIntegerTy())
  1165. // Attribute that only apply to integers.
  1166. Incompatible.addAttribute(Attribute::SExt)
  1167. .addAttribute(Attribute::ZExt);
  1168. if (!Ty->isPointerTy())
  1169. // Attribute that only apply to pointers.
  1170. Incompatible.addAttribute(Attribute::ByVal)
  1171. .addAttribute(Attribute::Nest)
  1172. .addAttribute(Attribute::NoAlias)
  1173. .addAttribute(Attribute::NoCapture)
  1174. .addAttribute(Attribute::NonNull)
  1175. .addDereferenceableAttr(1) // the int here is ignored
  1176. .addDereferenceableOrNullAttr(1) // the int here is ignored
  1177. .addAttribute(Attribute::ReadNone)
  1178. .addAttribute(Attribute::ReadOnly)
  1179. .addAttribute(Attribute::StructRet)
  1180. .addAttribute(Attribute::InAlloca);
  1181. return Incompatible;
  1182. }