summaryrefslogtreecommitdiff
path: root/lib/VMCore/ParameterAttributes.cpp
blob: d16f54de23aacf6dbe1ba94aff7114e64084c3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//===-- ParameterAttributes.cpp - Implement ParamAttrsList ----------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the ParamAttrsList class and ParamAttr utilities.
//
//===----------------------------------------------------------------------===//

#include "llvm/ParameterAttributes.h"
#include "llvm/Type.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/Streams.h"
#include "llvm/Support/ManagedStatic.h"
using namespace llvm;

//===----------------------------------------------------------------------===//
// ParamAttr Function Definitions
//===----------------------------------------------------------------------===//

std::string ParamAttr::getAsString(ParameterAttributes Attrs) {
  std::string Result;
  if (Attrs & ParamAttr::ZExt)
    Result += "zeroext ";
  if (Attrs & ParamAttr::SExt)
    Result += "signext ";
  if (Attrs & ParamAttr::NoReturn)
    Result += "noreturn ";
  if (Attrs & ParamAttr::NoUnwind)
    Result += "nounwind ";
  if (Attrs & ParamAttr::InReg)
    Result += "inreg ";
  if (Attrs & ParamAttr::NoAlias)
    Result += "noalias ";
  if (Attrs & ParamAttr::StructRet)
    Result += "sret ";  
  if (Attrs & ParamAttr::ByVal)
    Result += "byval ";
  if (Attrs & ParamAttr::Nest)
    Result += "nest ";
  if (Attrs & ParamAttr::ReadNone)
    Result += "readnone ";
  if (Attrs & ParamAttr::ReadOnly)
    Result += "readonly ";
  if (Attrs & ParamAttr::Alignment) {
    Result += "align ";
    Result += utostr((Attrs & ParamAttr::Alignment) >> 16);
    Result += " ";
  }
  // Trim the trailing space.
  Result.erase(Result.end()-1);
  return Result;
}

ParameterAttributes ParamAttr::typeIncompatible(const Type *Ty) {
  ParameterAttributes Incompatible = None;
  
  if (!Ty->isInteger())
    // Attributes that only apply to integers.
    Incompatible |= SExt | ZExt;
  
  if (!isa<PointerType>(Ty))
    // Attributes that only apply to pointers.
    Incompatible |= ByVal | Nest | NoAlias | StructRet;
  
  return Incompatible;
}

//===----------------------------------------------------------------------===//
// ParamAttributeListImpl Definition
//===----------------------------------------------------------------------===//

namespace llvm {
class ParamAttributeListImpl : public FoldingSetNode {
  unsigned RefCount;
  
  // ParamAttrsList is uniqued, these should not be publicly available.
  void operator=(const ParamAttributeListImpl &); // Do not implement
  ParamAttributeListImpl(const ParamAttributeListImpl &); // Do not implement
  ~ParamAttributeListImpl();                        // Private implementation
public:
  SmallVector<ParamAttrsWithIndex, 4> Attrs;
  
  ParamAttributeListImpl(const ParamAttrsWithIndex *Attr, unsigned NumAttrs)
    : Attrs(Attr, Attr+NumAttrs) {
    RefCount = 0;
  }
  
  void AddRef() { ++RefCount; }
  void DropRef() { if (--RefCount == 0) delete this; }
  
  void Profile(FoldingSetNodeID &ID) const {
    Profile(ID, &Attrs[0], Attrs.size());
  }
  static void Profile(FoldingSetNodeID &ID, const ParamAttrsWithIndex *Attr,
                      unsigned NumAttrs) {
    for (unsigned i = 0; i != NumAttrs; ++i)
      ID.AddInteger(uint64_t(Attr[i].Attrs) << 32 | unsigned(Attr[i].Index));
  }
};
}

static ManagedStatic<FoldingSet<ParamAttributeListImpl> > ParamAttrsLists;

ParamAttributeListImpl::~ParamAttributeListImpl() {
  ParamAttrsLists->RemoveNode(this);
}


PAListPtr PAListPtr::get(const ParamAttrsWithIndex *Attrs, unsigned NumAttrs) {
  // If there are no attributes then return a null ParamAttrsList pointer.
  if (NumAttrs == 0)
    return PAListPtr();
  
#ifndef NDEBUG
  for (unsigned i = 0; i != NumAttrs; ++i) {
    assert(Attrs[i].Attrs != ParamAttr::None && 
           "Pointless parameter attribute!");
    assert((!i || Attrs[i-1].Index < Attrs[i].Index) &&
           "Misordered ParamAttrsList!");
  }
#endif
  
  // Otherwise, build a key to look up the existing attributes.
  FoldingSetNodeID ID;
  ParamAttributeListImpl::Profile(ID, Attrs, NumAttrs);
  void *InsertPos;
  ParamAttributeListImpl *PAL =
    ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
  
  // If we didn't find any existing attributes of the same shape then
  // create a new one and insert it.
  if (!PAL) {
    PAL = new ParamAttributeListImpl(Attrs, NumAttrs);
    ParamAttrsLists->InsertNode(PAL, InsertPos);
  }
  
  // Return the ParamAttrsList that we found or created.
  return PAListPtr(PAL);
}


//===----------------------------------------------------------------------===//
// PAListPtr Method Implementations
//===----------------------------------------------------------------------===//

PAListPtr::PAListPtr(ParamAttributeListImpl *LI) : PAList(LI) {
  if (LI) LI->AddRef();
}

PAListPtr::PAListPtr(const PAListPtr &P) : PAList(P.PAList) {
  if (PAList) PAList->AddRef();  
}

const PAListPtr &PAListPtr::operator=(const PAListPtr &RHS) {
  if (PAList == RHS.PAList) return *this;
  if (PAList) PAList->DropRef();
  PAList = RHS.PAList;
  if (PAList) PAList->AddRef();
  return *this;
}

PAListPtr::~PAListPtr() {
  if (PAList) PAList->DropRef();
}

/// getNumSlots - Return the number of slots used in this attribute list. 
/// This is the number of arguments that have an attribute set on them
/// (including the function itself).
unsigned PAListPtr::getNumSlots() const {
  return PAList ? PAList->Attrs.size() : 0;
}

/// getSlot - Return the ParamAttrsWithIndex at the specified slot.  This
/// holds a parameter number plus a set of attributes.
const ParamAttrsWithIndex &PAListPtr::getSlot(unsigned Slot) const {
  assert(PAList && Slot < PAList->Attrs.size() && "Slot # out of range!");
  return PAList->Attrs[Slot];
}


/// getParamAttrs - The parameter attributes for the specified parameter are
/// returned.  Parameters for the result are denoted with Idx = 0.
ParameterAttributes PAListPtr::getParamAttrs(unsigned Idx) const {
  if (PAList == 0) return ParamAttr::None;
  
  const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs;
  for (unsigned i = 0, e = Attrs.size(); i != e && Attrs[i].Index <= Idx; ++i)
    if (Attrs[i].Index == Idx)
      return Attrs[i].Attrs;
  return ParamAttr::None;
}

/// hasAttrSomewhere - Return true if the specified attribute is set for at
/// least one parameter or for the return value.
bool PAListPtr::hasAttrSomewhere(ParameterAttributes Attr) const {
  if (PAList == 0) return false;
  
  const SmallVector<ParamAttrsWithIndex, 4> &Attrs = PAList->Attrs;
  for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
    if (Attrs[i].Attrs & Attr)
      return true;
  return false;
}


PAListPtr PAListPtr::addAttr(unsigned Idx, ParameterAttributes Attrs) const {
  ParameterAttributes OldAttrs = getParamAttrs(Idx);
#ifndef NDEBUG
  // FIXME it is not obvious how this should work for alignment.
  // For now, say we can't change a known alignment.
  ParameterAttributes OldAlign = OldAttrs & ParamAttr::Alignment;
  ParameterAttributes NewAlign = Attrs & ParamAttr::Alignment;
  assert((!OldAlign || !NewAlign || OldAlign == NewAlign) &&
         "Attempt to change alignment!");
#endif
  
  ParameterAttributes NewAttrs = OldAttrs | Attrs;
  if (NewAttrs == OldAttrs)
    return *this;
  
  SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
  if (PAList == 0)
    NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
  else {
    const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs;
    unsigned i = 0, e = OldAttrList.size();
    // Copy attributes for arguments before this one.
    for (; i != e && OldAttrList[i].Index < Idx; ++i)
      NewAttrList.push_back(OldAttrList[i]);

    // If there are attributes already at this index, merge them in.
    if (i != e && OldAttrList[i].Index == Idx) {
      Attrs |= OldAttrList[i].Attrs;
      ++i;
    }
    
    NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
    
    // Copy attributes for arguments after this one.
    NewAttrList.insert(NewAttrList.end(), 
                       OldAttrList.begin()+i, OldAttrList.end());
  }
  
  return get(&NewAttrList[0], NewAttrList.size());
}

PAListPtr PAListPtr::removeAttr(unsigned Idx, ParameterAttributes Attrs) const {
#ifndef NDEBUG
  // FIXME it is not obvious how this should work for alignment.
  // For now, say we can't pass in alignment, which no current use does.
  assert(!(Attrs & ParamAttr::Alignment) && "Attempt to exclude alignment!");
#endif
  if (PAList == 0) return PAListPtr();
  
  ParameterAttributes OldAttrs = getParamAttrs(Idx);
  ParameterAttributes NewAttrs = OldAttrs & ~Attrs;
  if (NewAttrs == OldAttrs)
    return *this;

  SmallVector<ParamAttrsWithIndex, 8> NewAttrList;
  const SmallVector<ParamAttrsWithIndex, 4> &OldAttrList = PAList->Attrs;
  unsigned i = 0, e = OldAttrList.size();
  
  // Copy attributes for arguments before this one.
  for (; i != e && OldAttrList[i].Index < Idx; ++i)
    NewAttrList.push_back(OldAttrList[i]);
  
  // If there are attributes already at this index, merge them in.
  assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
  Attrs = OldAttrList[i].Attrs & ~Attrs;
  ++i;
  if (Attrs)  // If any attributes left for this parameter, add them.
    NewAttrList.push_back(ParamAttrsWithIndex::get(Idx, Attrs));
  
  // Copy attributes for arguments after this one.
  NewAttrList.insert(NewAttrList.end(), 
                     OldAttrList.begin()+i, OldAttrList.end());
  
  return get(&NewAttrList[0], NewAttrList.size());
}

void PAListPtr::dump() const {
  cerr << "PAL[ ";
  for (unsigned i = 0; i < getNumSlots(); ++i) {
    const ParamAttrsWithIndex &PAWI = getSlot(i);
    cerr << "{" << PAWI.Index << "," << PAWI.Attrs << "} ";
  }
  
  cerr << "]\n";
}