1 //===-- ParameterAttributes.cpp - Implement ParameterAttrs ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ParamAttrsList class and ParamAttr utilities.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ParameterAttributes.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Support/ManagedStatic.h"
20 static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
22 ParamAttrsList::ParamAttrsList(const ParamAttrsVector &attrVec)
23 : attrs(attrVec), refCount(0) {
26 ParamAttrsList::~ParamAttrsList() {
27 ParamAttrsLists->RemoveNode(this);
31 ParamAttrsList::getParamAttrs(uint16_t Index) const {
32 unsigned limit = attrs.size();
33 for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i)
34 if (attrs[i].index == Index)
35 return attrs[i].attrs;
36 return ParamAttr::None;
39 bool ParamAttrsList::hasAttrSomewhere(ParameterAttributes attr) const {
40 for (unsigned i = 0, e = attrs.size(); i < e; ++i)
41 if (attrs[i].attrs & attr)
47 ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
49 if (Attrs & ParamAttr::ZExt)
51 if (Attrs & ParamAttr::SExt)
53 if (Attrs & ParamAttr::NoReturn)
54 Result += "noreturn ";
55 if (Attrs & ParamAttr::NoUnwind)
56 Result += "nounwind ";
57 if (Attrs & ParamAttr::InReg)
59 if (Attrs & ParamAttr::NoAlias)
61 if (Attrs & ParamAttr::StructRet)
63 if (Attrs & ParamAttr::ByVal)
65 if (Attrs & ParamAttr::Nest)
67 if (Attrs & ParamAttr::ReadNone)
68 Result += "readnone ";
69 if (Attrs & ParamAttr::ReadOnly)
70 Result += "readonly ";
74 void ParamAttrsList::Profile(FoldingSetNodeID &ID,
75 const ParamAttrsVector &Attrs) {
76 for (unsigned i = 0; i < Attrs.size(); ++i)
77 ID.AddInteger(unsigned(Attrs[i].attrs) << 16 | unsigned(Attrs[i].index));
80 const ParamAttrsList *
81 ParamAttrsList::get(const ParamAttrsVector &attrVec) {
82 // If there are no attributes then return a null ParamAttrsList pointer.
87 for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
88 assert(attrVec[i].attrs != ParamAttr::None
89 && "Pointless parameter attribute!");
90 assert((!i || attrVec[i-1].index < attrVec[i].index)
91 && "Misordered ParamAttrsList!");
95 // Otherwise, build a key to look up the existing attributes.
97 ParamAttrsList::Profile(ID, attrVec);
99 ParamAttrsList *PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
101 // If we didn't find any existing attributes of the same shape then
102 // create a new one and insert it.
104 PAL = new ParamAttrsList(attrVec);
105 ParamAttrsLists->InsertNode(PAL, InsertPos);
108 // Return the ParamAttrsList that we found or created.
112 const ParamAttrsList *
113 ParamAttrsList::getModified(const ParamAttrsList *PAL,
114 const ParamAttrsVector &modVec) {
119 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
120 assert((!i || modVec[i-1].index < modVec[i].index)
121 && "Misordered ParamAttrsList!");
125 // Strip any instances of ParamAttr::None from modVec before calling 'get'.
126 ParamAttrsVector newVec;
127 for (unsigned i = 0, e = modVec.size(); i < e; ++i)
128 if (modVec[i].attrs != ParamAttr::None)
129 newVec.push_back(modVec[i]);
133 const ParamAttrsVector &oldVec = PAL->attrs;
135 ParamAttrsVector newVec;
138 unsigned oldE = oldVec.size();
139 unsigned modE = modVec.size();
141 while (oldI < oldE && modI < modE) {
142 uint16_t oldIndex = oldVec[oldI].index;
143 uint16_t modIndex = modVec[modI].index;
145 if (oldIndex < modIndex) {
146 newVec.push_back(oldVec[oldI]);
148 } else if (modIndex < oldIndex) {
149 if (modVec[modI].attrs != ParamAttr::None)
150 newVec.push_back(modVec[modI]);
153 // Same index - overwrite or delete existing attributes.
154 if (modVec[modI].attrs != ParamAttr::None)
155 newVec.push_back(modVec[modI]);
161 for (; oldI < oldE; ++oldI)
162 newVec.push_back(oldVec[oldI]);
163 for (; modI < modE; ++modI)
164 if (modVec[modI].attrs != ParamAttr::None)
165 newVec.push_back(modVec[modI]);
170 const ParamAttrsList *
171 ParamAttrsList::includeAttrs(const ParamAttrsList *PAL,
172 uint16_t idx, uint16_t attrs) {
173 uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0;
174 uint16_t NewAttrs = OldAttrs | attrs;
175 if (NewAttrs == OldAttrs)
178 ParamAttrsVector modVec;
179 modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs));
180 return getModified(PAL, modVec);
183 const ParamAttrsList *
184 ParamAttrsList::excludeAttrs(const ParamAttrsList *PAL,
185 uint16_t idx, uint16_t attrs) {
186 uint16_t OldAttrs = PAL ? PAL->getParamAttrs(idx) : 0;
187 uint16_t NewAttrs = OldAttrs & ~attrs;
188 if (NewAttrs == OldAttrs)
191 ParamAttrsVector modVec;
192 modVec.push_back(ParamAttrsWithIndex::get(idx, NewAttrs));
193 return getModified(PAL, modVec);
196 uint16_t ParamAttr::typeIncompatible (const Type *Ty) {
197 uint16_t Incompatible = None;
199 if (!Ty->isInteger())
200 // Attributes that only apply to integers.
201 Incompatible |= SExt | ZExt;
203 if (!isa<PointerType>(Ty))
204 // Attributes that only apply to pointers.
205 Incompatible |= ByVal | Nest | NoAlias | StructRet;