2008-01-02 23:42:30 +00:00
|
|
|
//===-- ParameterAttributes.cpp - Implement ParameterAttrs ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-01-06 18:27:01 +00:00
|
|
|
// This file implements the ParamAttrsList class and ParamAttr utilities.
|
2008-01-02 23:42:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ParameterAttributes.h"
|
2008-01-06 18:27:01 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2008-01-02 23:42:30 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2008-02-19 23:51:49 +00:00
|
|
|
#include <sstream>
|
2008-01-03 03:21:18 +00:00
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-01-03 00:10:22 +00:00
|
|
|
static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
|
|
|
|
|
|
|
|
ParamAttrsList::ParamAttrsList(const ParamAttrsVector &attrVec)
|
|
|
|
: attrs(attrVec), refCount(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamAttrsList::~ParamAttrsList() {
|
|
|
|
ParamAttrsLists->RemoveNode(this);
|
|
|
|
}
|
|
|
|
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes
|
2008-01-02 23:42:30 +00:00
|
|
|
ParamAttrsList::getParamAttrs(uint16_t Index) const {
|
|
|
|
unsigned limit = attrs.size();
|
|
|
|
for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i)
|
|
|
|
if (attrs[i].index == Index)
|
|
|
|
return attrs[i].attrs;
|
|
|
|
return ParamAttr::None;
|
|
|
|
}
|
|
|
|
|
2008-01-14 19:52:09 +00:00
|
|
|
bool ParamAttrsList::hasAttrSomewhere(ParameterAttributes attr) const {
|
|
|
|
for (unsigned i = 0, e = attrs.size(); i < e; ++i)
|
|
|
|
if (attrs[i].attrs & attr)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
std::string
|
2008-02-19 21:38:47 +00:00
|
|
|
ParamAttrsList::getParamAttrsText(ParameterAttributes Attrs) {
|
2008-01-02 23:42:30 +00:00
|
|
|
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 ";
|
2008-02-19 23:51:49 +00:00
|
|
|
if (Attrs & ParamAttr::Alignment) {
|
|
|
|
std::stringstream s;
|
|
|
|
s << ((Attrs & ParamAttr::Alignment) >> 16);
|
|
|
|
Result += "align ";
|
|
|
|
Result += s.str();
|
|
|
|
Result += " ";
|
|
|
|
}
|
2008-01-02 23:42:30 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-01-03 00:29:27 +00:00
|
|
|
void ParamAttrsList::Profile(FoldingSetNodeID &ID,
|
|
|
|
const ParamAttrsVector &Attrs) {
|
|
|
|
for (unsigned i = 0; i < Attrs.size(); ++i)
|
2008-02-19 23:51:49 +00:00
|
|
|
ID.AddInteger(uint64_t(Attrs[i].attrs) << 16 | unsigned(Attrs[i].index));
|
2008-01-02 23:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ParamAttrsList *
|
|
|
|
ParamAttrsList::get(const ParamAttrsVector &attrVec) {
|
|
|
|
// If there are no attributes then return a null ParamAttrsList pointer.
|
|
|
|
if (attrVec.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (unsigned i = 0, e = attrVec.size(); i < e; ++i) {
|
|
|
|
assert(attrVec[i].attrs != ParamAttr::None
|
|
|
|
&& "Pointless parameter attribute!");
|
|
|
|
assert((!i || attrVec[i-1].index < attrVec[i].index)
|
|
|
|
&& "Misordered ParamAttrsList!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Otherwise, build a key to look up the existing attributes.
|
|
|
|
FoldingSetNodeID ID;
|
2008-01-03 00:29:27 +00:00
|
|
|
ParamAttrsList::Profile(ID, attrVec);
|
2008-01-02 23:42:30 +00:00
|
|
|
void *InsertPos;
|
2008-01-03 00:29:27 +00:00
|
|
|
ParamAttrsList *PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
|
2008-01-02 23:42:30 +00:00
|
|
|
|
|
|
|
// If we didn't find any existing attributes of the same shape then
|
|
|
|
// create a new one and insert it.
|
|
|
|
if (!PAL) {
|
|
|
|
PAL = new ParamAttrsList(attrVec);
|
|
|
|
ParamAttrsLists->InsertNode(PAL, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the ParamAttrsList that we found or created.
|
|
|
|
return PAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamAttrsList *
|
|
|
|
ParamAttrsList::getModified(const ParamAttrsList *PAL,
|
|
|
|
const ParamAttrsVector &modVec) {
|
|
|
|
if (modVec.empty())
|
|
|
|
return PAL;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (unsigned i = 0, e = modVec.size(); i < e; ++i)
|
|
|
|
assert((!i || modVec[i-1].index < modVec[i].index)
|
|
|
|
&& "Misordered ParamAttrsList!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!PAL) {
|
|
|
|
// Strip any instances of ParamAttr::None from modVec before calling 'get'.
|
|
|
|
ParamAttrsVector newVec;
|
2008-02-16 20:53:06 +00:00
|
|
|
newVec.reserve(modVec.size());
|
2008-01-02 23:42:30 +00:00
|
|
|
for (unsigned i = 0, e = modVec.size(); i < e; ++i)
|
|
|
|
if (modVec[i].attrs != ParamAttr::None)
|
|
|
|
newVec.push_back(modVec[i]);
|
|
|
|
return get(newVec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamAttrsVector &oldVec = PAL->attrs;
|
|
|
|
|
|
|
|
ParamAttrsVector newVec;
|
|
|
|
unsigned oldI = 0;
|
|
|
|
unsigned modI = 0;
|
|
|
|
unsigned oldE = oldVec.size();
|
|
|
|
unsigned modE = modVec.size();
|
|
|
|
|
|
|
|
while (oldI < oldE && modI < modE) {
|
|
|
|
uint16_t oldIndex = oldVec[oldI].index;
|
|
|
|
uint16_t modIndex = modVec[modI].index;
|
|
|
|
|
|
|
|
if (oldIndex < modIndex) {
|
|
|
|
newVec.push_back(oldVec[oldI]);
|
|
|
|
++oldI;
|
|
|
|
} else if (modIndex < oldIndex) {
|
|
|
|
if (modVec[modI].attrs != ParamAttr::None)
|
|
|
|
newVec.push_back(modVec[modI]);
|
|
|
|
++modI;
|
|
|
|
} else {
|
|
|
|
// Same index - overwrite or delete existing attributes.
|
|
|
|
if (modVec[modI].attrs != ParamAttr::None)
|
|
|
|
newVec.push_back(modVec[modI]);
|
|
|
|
++oldI;
|
|
|
|
++modI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; oldI < oldE; ++oldI)
|
|
|
|
newVec.push_back(oldVec[oldI]);
|
|
|
|
for (; modI < modE; ++modI)
|
|
|
|
if (modVec[modI].attrs != ParamAttr::None)
|
|
|
|
newVec.push_back(modVec[modI]);
|
|
|
|
|
|
|
|
return get(newVec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamAttrsList *
|
|
|
|
ParamAttrsList::includeAttrs(const ParamAttrsList *PAL,
|
2008-02-19 21:38:47 +00:00
|
|
|
uint16_t idx, ParameterAttributes attrs) {
|
|
|
|
ParameterAttributes OldAttrs = PAL ? PAL->getParamAttrs(idx) :
|
|
|
|
ParamAttr::None;
|
2008-02-19 23:51:49 +00:00
|
|
|
#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
|
|
|
|
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes NewAttrs = OldAttrs | attrs;
|
2008-01-02 23:42:30 +00:00
|
|
|
if (NewAttrs == OldAttrs)
|
|
|
|
return PAL;
|
|
|
|
|
2008-02-16 20:53:06 +00:00
|
|
|
ParamAttrsVector modVec(1);
|
|
|
|
modVec[0] = ParamAttrsWithIndex::get(idx, NewAttrs);
|
2008-01-02 23:42:30 +00:00
|
|
|
return getModified(PAL, modVec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ParamAttrsList *
|
|
|
|
ParamAttrsList::excludeAttrs(const ParamAttrsList *PAL,
|
2008-02-19 21:38:47 +00:00
|
|
|
uint16_t idx, ParameterAttributes attrs) {
|
2008-02-19 23:51:49 +00:00
|
|
|
#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
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes OldAttrs = PAL ? PAL->getParamAttrs(idx) :
|
|
|
|
ParamAttr::None;
|
|
|
|
ParameterAttributes NewAttrs = OldAttrs & ~attrs;
|
2008-01-02 23:42:30 +00:00
|
|
|
if (NewAttrs == OldAttrs)
|
|
|
|
return PAL;
|
|
|
|
|
2008-02-16 20:53:06 +00:00
|
|
|
ParamAttrsVector modVec(1);
|
|
|
|
modVec[0] = ParamAttrsWithIndex::get(idx, NewAttrs);
|
2008-01-02 23:42:30 +00:00
|
|
|
return getModified(PAL, modVec);
|
|
|
|
}
|
|
|
|
|
2008-02-19 21:38:47 +00:00
|
|
|
ParameterAttributes ParamAttr::typeIncompatible (const Type *Ty) {
|
|
|
|
ParameterAttributes Incompatible = None;
|
2008-01-06 18:27:01 +00:00
|
|
|
|
|
|
|
if (!Ty->isInteger())
|
2008-01-07 17:16:06 +00:00
|
|
|
// Attributes that only apply to integers.
|
|
|
|
Incompatible |= SExt | ZExt;
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2008-01-21 21:37:41 +00:00
|
|
|
if (!isa<PointerType>(Ty))
|
2008-01-07 17:16:06 +00:00
|
|
|
// Attributes that only apply to pointers.
|
|
|
|
Incompatible |= ByVal | Nest | NoAlias | StructRet;
|
2008-01-06 18:27:01 +00:00
|
|
|
|
2008-01-07 17:16:06 +00:00
|
|
|
return Incompatible;
|
2008-01-06 18:27:01 +00:00
|
|
|
}
|