2003-10-13 03:32:08 +00:00
|
|
|
//===-- Function.cpp - Implement the Global object classes ----------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2007-02-05 20:47:22 +00:00
|
|
|
// This file implements the Function class for the VMCore library.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-09-06 20:46:32 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-10-12 04:20:25 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2007-08-20 19:23:34 +00:00
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2007-04-22 05:46:44 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2002-06-25 16:13:24 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
2007-12-03 20:06:50 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2004-12-05 06:43:27 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2005-01-30 00:09:23 +00:00
|
|
|
BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
|
2002-09-08 18:59:35 +00:00
|
|
|
BasicBlock *Ret = new BasicBlock();
|
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-09-06 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
|
|
|
|
return F->getBasicBlockList();
|
|
|
|
}
|
|
|
|
|
2005-01-30 00:09:23 +00:00
|
|
|
Argument *ilist_traits<Argument>::createSentinel() {
|
2006-12-31 05:26:44 +00:00
|
|
|
Argument *Ret = new Argument(Type::Int32Ty);
|
2002-09-08 18:59:35 +00:00
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 16:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
|
|
|
|
return F->getArgumentList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
|
|
|
// are not in the public header file...
|
2007-04-17 03:26:42 +00:00
|
|
|
template class SymbolTableListTraits<Argument, Function>;
|
|
|
|
template class SymbolTableListTraits<BasicBlock, Function>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-09 19:39:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Argument Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
|
2007-02-12 05:18:08 +00:00
|
|
|
: Value(Ty, Value::ArgumentVal) {
|
2002-09-06 21:33:15 +00:00
|
|
|
Parent = 0;
|
2002-09-08 18:59:35 +00:00
|
|
|
|
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
if (Par)
|
|
|
|
Par->getArgumentList().push_back(this);
|
2007-02-12 05:18:08 +00:00
|
|
|
setName(Name);
|
2002-09-06 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Argument::setParent(Function *parent) {
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2002-09-06 21:33:15 +00:00
|
|
|
Parent = parent;
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2002-09-06 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 15:01:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ParamAttrsList Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
ParamAttrsList::getParamAttrs(uint16_t Index) const {
|
|
|
|
unsigned limit = attrs.size();
|
2007-11-30 18:20:58 +00:00
|
|
|
for (unsigned i = 0; i < limit && attrs[i].index <= Index; ++i)
|
2007-04-09 15:01:12 +00:00
|
|
|
if (attrs[i].index == Index)
|
|
|
|
return attrs[i].attrs;
|
2007-04-11 02:44:20 +00:00
|
|
|
return ParamAttr::None;
|
2007-04-09 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ParamAttrsList::getParamAttrsText(uint16_t Attrs) {
|
|
|
|
std::string Result;
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::ZExt)
|
2007-07-19 23:13:04 +00:00
|
|
|
Result += "zeroext ";
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::SExt)
|
2007-07-19 23:13:04 +00:00
|
|
|
Result += "signext ";
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::NoReturn)
|
2007-04-09 15:01:12 +00:00
|
|
|
Result += "noreturn ";
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::NoUnwind)
|
2007-04-09 15:01:12 +00:00
|
|
|
Result += "nounwind ";
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::InReg)
|
2007-04-09 15:01:12 +00:00
|
|
|
Result += "inreg ";
|
2007-06-05 05:28:26 +00:00
|
|
|
if (Attrs & ParamAttr::NoAlias)
|
|
|
|
Result += "noalias ";
|
2007-04-11 02:44:20 +00:00
|
|
|
if (Attrs & ParamAttr::StructRet)
|
2007-04-09 15:01:12 +00:00
|
|
|
Result += "sret ";
|
2007-07-06 10:57:03 +00:00
|
|
|
if (Attrs & ParamAttr::ByVal)
|
|
|
|
Result += "byval ";
|
2007-07-27 12:58:54 +00:00
|
|
|
if (Attrs & ParamAttr::Nest)
|
|
|
|
Result += "nest ";
|
2007-11-22 20:23:04 +00:00
|
|
|
if (Attrs & ParamAttr::ReadNone)
|
|
|
|
Result += "readnone ";
|
|
|
|
if (Attrs & ParamAttr::ReadOnly)
|
|
|
|
Result += "readonly ";
|
2007-04-09 15:01:12 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2007-11-25 14:10:56 +00:00
|
|
|
/// onlyInformative - Returns whether only informative attributes are set.
|
|
|
|
static inline bool onlyInformative(uint16_t attrs) {
|
|
|
|
return !(attrs & ~ParamAttr::Informative);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ParamAttrsList::areCompatible(const ParamAttrsList *A, const ParamAttrsList *B){
|
|
|
|
if (A == B)
|
|
|
|
return true;
|
|
|
|
unsigned ASize = A ? A->size() : 0;
|
|
|
|
unsigned BSize = B ? B->size() : 0;
|
|
|
|
unsigned AIndex = 0;
|
|
|
|
unsigned BIndex = 0;
|
|
|
|
|
|
|
|
while (AIndex < ASize && BIndex < BSize) {
|
|
|
|
uint16_t AIdx = A->getParamIndex(AIndex);
|
|
|
|
uint16_t BIdx = B->getParamIndex(BIndex);
|
|
|
|
uint16_t AAttrs = A->getParamAttrsAtIndex(AIndex);
|
|
|
|
uint16_t BAttrs = B->getParamAttrsAtIndex(AIndex);
|
|
|
|
|
|
|
|
if (AIdx < BIdx) {
|
|
|
|
if (!onlyInformative(AAttrs))
|
|
|
|
return false;
|
|
|
|
++AIndex;
|
|
|
|
} else if (BIdx < AIdx) {
|
|
|
|
if (!onlyInformative(BAttrs))
|
|
|
|
return false;
|
|
|
|
++BIndex;
|
|
|
|
} else {
|
|
|
|
if (!onlyInformative(AAttrs ^ BAttrs))
|
|
|
|
return false;
|
|
|
|
++AIndex;
|
|
|
|
++BIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; AIndex < ASize; ++AIndex)
|
|
|
|
if (!onlyInformative(A->getParamAttrsAtIndex(AIndex)))
|
|
|
|
return false;
|
|
|
|
for (; BIndex < BSize; ++BIndex)
|
|
|
|
if (!onlyInformative(B->getParamAttrsAtIndex(AIndex)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:46:44 +00:00
|
|
|
void
|
|
|
|
ParamAttrsList::Profile(FoldingSetNodeID &ID) const {
|
|
|
|
for (unsigned i = 0; i < attrs.size(); ++i) {
|
2007-11-27 13:23:08 +00:00
|
|
|
uint32_t val = uint32_t(attrs[i].attrs) << 16 | attrs[i].index;
|
2007-04-22 05:46:44 +00:00
|
|
|
ID.AddInteger(val);
|
|
|
|
}
|
2007-04-09 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
2007-04-22 05:46:44 +00:00
|
|
|
static ManagedStatic<FoldingSet<ParamAttrsList> > ParamAttrsLists;
|
|
|
|
|
2007-11-30 18:19:18 +00:00
|
|
|
const ParamAttrsList *
|
2007-04-22 05:46:44 +00:00
|
|
|
ParamAttrsList::get(const ParamAttrsVector &attrVec) {
|
2007-11-27 13:23:08 +00:00
|
|
|
// If there are no attributes then return a null ParamAttrsList pointer.
|
|
|
|
if (attrVec.empty())
|
|
|
|
return 0;
|
|
|
|
|
2007-09-11 14:40:04 +00:00
|
|
|
#ifndef NDEBUG
|
2007-11-20 14:09:29 +00:00
|
|
|
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!");
|
|
|
|
}
|
2007-09-11 14:40:04 +00:00
|
|
|
#endif
|
2007-11-27 13:23:08 +00:00
|
|
|
|
|
|
|
// Otherwise, build a key to look up the existing attributes.
|
2007-04-22 05:46:44 +00:00
|
|
|
ParamAttrsList key(attrVec);
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
key.Profile(ID);
|
|
|
|
void *InsertPos;
|
|
|
|
ParamAttrsList* PAL = ParamAttrsLists->FindNodeOrInsertPos(ID, InsertPos);
|
2007-11-27 13:23:08 +00:00
|
|
|
|
|
|
|
// If we didn't find any existing attributes of the same shape then
|
|
|
|
// create a new one and insert it.
|
2007-04-22 05:46:44 +00:00
|
|
|
if (!PAL) {
|
|
|
|
PAL = new ParamAttrsList(attrVec);
|
|
|
|
ParamAttrsLists->InsertNode(PAL, InsertPos);
|
|
|
|
}
|
2007-11-27 13:23:08 +00:00
|
|
|
|
|
|
|
// Return the ParamAttrsList that we found or created.
|
2007-04-22 05:46:44 +00:00
|
|
|
return PAL;
|
2007-04-09 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
2007-11-30 18:19:18 +00:00
|
|
|
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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-04-22 17:28:03 +00:00
|
|
|
ParamAttrsList::~ParamAttrsList() {
|
|
|
|
ParamAttrsLists->RemoveNode(this);
|
|
|
|
}
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-26 18:01:55 +00:00
|
|
|
// Function Implementation
|
2001-09-10 07:58:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-04-16 20:28:45 +00:00
|
|
|
Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
|
2002-09-06 20:46:32 +00:00
|
|
|
const std::string &name, Module *ParentModule)
|
2007-11-27 13:23:08 +00:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name),
|
|
|
|
ParamAttrs(0) {
|
2007-02-05 20:47:22 +00:00
|
|
|
SymTab = new ValueSymbolTable();
|
2002-09-06 20:46:32 +00:00
|
|
|
|
2003-11-21 22:32:23 +00:00
|
|
|
assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
|
|
|
|
&& "LLVM functions cannot return aggregate values!");
|
|
|
|
|
2007-08-18 06:14:52 +00:00
|
|
|
// If the function has arguments, mark them as lazily built.
|
|
|
|
if (Ty->getNumParams())
|
|
|
|
SubclassData = 1; // Set the "has lazy arguments" bit.
|
|
|
|
|
2002-09-08 18:59:35 +00:00
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-06 20:46:32 +00:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getFunctionList().push_back(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
Function::~Function() {
|
|
|
|
dropAllReferences(); // After this it is safe to delete instructions.
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Delete all of the method arguments and unlink from symbol table...
|
2007-12-10 02:14:30 +00:00
|
|
|
ArgumentList.clear();
|
|
|
|
delete SymTab;
|
2007-04-22 17:28:03 +00:00
|
|
|
|
|
|
|
// Drop our reference to the parameter attributes, if any.
|
2007-12-10 02:14:30 +00:00
|
|
|
if (ParamAttrs)
|
|
|
|
ParamAttrs->dropRef();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 06:14:52 +00:00
|
|
|
void Function::BuildLazyArguments() const {
|
|
|
|
// Create the arguments vector, all arguments start out unnamed.
|
|
|
|
const FunctionType *FT = getFunctionType();
|
|
|
|
for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
|
|
|
|
assert(FT->getParamType(i) != Type::VoidTy &&
|
|
|
|
"Cannot have void typed arguments!");
|
|
|
|
ArgumentList.push_back(new Argument(FT->getParamType(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the lazy arguments bit.
|
|
|
|
const_cast<Function*>(this)->SubclassData &= ~1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Function::arg_size() const {
|
|
|
|
return getFunctionType()->getNumParams();
|
|
|
|
}
|
|
|
|
bool Function::arg_empty() const {
|
|
|
|
return getFunctionType()->getNumParams() == 0;
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
void Function::setParent(Module *parent) {
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
Parent = parent;
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
void Function::setParamAttrs(const ParamAttrsList *attrs) {
|
|
|
|
// Avoid deleting the ParamAttrsList if they are setting the
|
|
|
|
// attributes to the same list.
|
|
|
|
if (ParamAttrs == attrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Drop reference on the old ParamAttrsList
|
2007-04-22 17:28:03 +00:00
|
|
|
if (ParamAttrs)
|
|
|
|
ParamAttrs->dropRef();
|
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
// Add reference to the new ParamAttrsList
|
2007-04-22 17:28:03 +00:00
|
|
|
if (attrs)
|
|
|
|
attrs->addRef();
|
|
|
|
|
2007-11-27 13:23:08 +00:00
|
|
|
// Set the new ParamAttrsList.
|
2007-04-22 17:28:03 +00:00
|
|
|
ParamAttrs = attrs;
|
|
|
|
}
|
|
|
|
|
2002-03-29 03:44:36 +00:00
|
|
|
const FunctionType *Function::getFunctionType() const {
|
|
|
|
return cast<FunctionType>(getType()->getElementType());
|
2001-10-03 14:53:21 +00:00
|
|
|
}
|
|
|
|
|
2005-01-07 07:40:32 +00:00
|
|
|
bool Function::isVarArg() const {
|
|
|
|
return getFunctionType()->isVarArg();
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
const Type *Function::getReturnType() const {
|
2002-03-29 03:44:36 +00:00
|
|
|
return getFunctionType()->getReturnType();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-10-11 22:21:39 +00:00
|
|
|
void Function::removeFromParent() {
|
|
|
|
getParent()->getFunctionList().remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Function::eraseFromParent() {
|
|
|
|
getParent()->getFunctionList().erase(this);
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// dropAllReferences() - This function causes all the subinstructions to "let
|
|
|
|
// go" of all references that they are maintaining. This allows one to
|
|
|
|
// 'delete' a whole class at a time, even though there may be circular
|
|
|
|
// references... first all references are dropped, and all use counts go to
|
2003-10-10 17:54:14 +00:00
|
|
|
// zero. Then everything is deleted for real. Note that no operations are
|
2005-04-21 23:48:37 +00:00
|
|
|
// valid on an object that has "dropped all references", except operator
|
2001-06-06 20:29:01 +00:00
|
|
|
// delete.
|
|
|
|
//
|
2002-03-23 22:51:58 +00:00
|
|
|
void Function::dropAllReferences() {
|
2002-06-25 16:13:24 +00:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2003-09-17 04:58:59 +00:00
|
|
|
BasicBlocks.clear(); // Delete all basic blocks...
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2003-05-08 03:47:33 +00:00
|
|
|
/// getIntrinsicID - This method returns the ID number of the specified
|
2003-11-11 22:41:34 +00:00
|
|
|
/// function, or Intrinsic::not_intrinsic if the function is not an
|
2003-10-10 17:54:14 +00:00
|
|
|
/// intrinsic, or if the pointer is null. This value is always defined to be
|
2003-05-08 03:47:33 +00:00
|
|
|
/// zero to allow easy checking for whether a function is intrinsic or not. The
|
|
|
|
/// particular intrinsic functions which correspond to this value are defined in
|
|
|
|
/// llvm/Intrinsics.h.
|
|
|
|
///
|
2007-04-16 06:54:34 +00:00
|
|
|
unsigned Function::getIntrinsicID(bool noAssert) const {
|
2007-02-15 19:17:16 +00:00
|
|
|
const ValueName *ValName = this->getValueName();
|
2007-04-16 07:08:44 +00:00
|
|
|
if (!ValName)
|
|
|
|
return 0;
|
2007-02-15 19:17:16 +00:00
|
|
|
unsigned Len = ValName->getKeyLength();
|
|
|
|
const char *Name = ValName->getKeyData();
|
|
|
|
|
2007-04-16 16:56:54 +00:00
|
|
|
if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
|
For PR411:
This patch is an incremental step towards supporting a flat symbol table.
It de-overloads the intrinsic functions by providing type-specific intrinsics
and arranging for automatically upgrading from the old overloaded name to
the new non-overloaded name. Specifically:
llvm.isunordered -> llvm.isunordered.f32, llvm.isunordered.f64
llvm.sqrt -> llvm.sqrt.f32, llvm.sqrt.f64
llvm.ctpop -> llvm.ctpop.i8, llvm.ctpop.i16, llvm.ctpop.i32, llvm.ctpop.i64
llvm.ctlz -> llvm.ctlz.i8, llvm.ctlz.i16, llvm.ctlz.i32, llvm.ctlz.i64
llvm.cttz -> llvm.cttz.i8, llvm.cttz.i16, llvm.cttz.i32, llvm.cttz.i64
New code should not use the overloaded intrinsic names. Warnings will be
emitted if they are used.
llvm-svn: 25366
2006-01-16 21:12:35 +00:00
|
|
|
|| Name[2] != 'v' || Name[3] != 'm')
|
2003-05-08 03:47:33 +00:00
|
|
|
return 0; // All intrinsics start with 'llvm.'
|
2003-09-19 19:31:41 +00:00
|
|
|
|
2007-04-16 06:54:34 +00:00
|
|
|
assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-03-09 20:35:01 +00:00
|
|
|
#define GET_FUNCTION_RECOGNIZER
|
|
|
|
#include "llvm/Intrinsics.gen"
|
|
|
|
#undef GET_FUNCTION_RECOGNIZER
|
2007-04-16 06:54:34 +00:00
|
|
|
assert(noAssert && "Invalid LLVM intrinsic name");
|
2003-05-08 03:47:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-01 07:25:33 +00:00
|
|
|
std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
|
2006-03-25 06:32:47 +00:00
|
|
|
assert(id < num_intrinsics && "Invalid intrinsic ID!");
|
|
|
|
const char * const Table[] = {
|
|
|
|
"not_intrinsic",
|
|
|
|
#define GET_INTRINSIC_NAME_TABLE
|
|
|
|
#include "llvm/Intrinsics.gen"
|
|
|
|
#undef GET_INTRINSIC_NAME_TABLE
|
|
|
|
};
|
2007-04-01 07:25:33 +00:00
|
|
|
if (numTys == 0)
|
|
|
|
return Table[id];
|
|
|
|
std::string Result(Table[id]);
|
|
|
|
for (unsigned i = 0; i < numTys; ++i)
|
|
|
|
if (Tys[i])
|
2007-08-20 19:23:34 +00:00
|
|
|
Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
|
2007-04-01 07:25:33 +00:00
|
|
|
return Result;
|
2006-03-25 06:32:47 +00:00
|
|
|
}
|
|
|
|
|
2007-04-01 07:25:33 +00:00
|
|
|
const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
|
2007-06-05 23:49:06 +00:00
|
|
|
unsigned numTys) {
|
2007-02-07 20:38:26 +00:00
|
|
|
const Type *ResultTy = NULL;
|
|
|
|
std::vector<const Type*> ArgTys;
|
|
|
|
bool IsVarArg = false;
|
|
|
|
|
|
|
|
#define GET_INTRINSIC_GENERATOR
|
|
|
|
#include "llvm/Intrinsics.gen"
|
|
|
|
#undef GET_INTRINSIC_GENERATOR
|
|
|
|
|
2007-04-09 06:11:23 +00:00
|
|
|
return FunctionType::get(ResultTy, ArgTys, IsVarArg);
|
2007-02-07 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2007-12-03 20:06:50 +00:00
|
|
|
const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
|
|
|
|
static const ParamAttrsList *IntrinsicAttributes[Intrinsic::num_intrinsics];
|
|
|
|
|
|
|
|
if (IntrinsicAttributes[id])
|
|
|
|
return IntrinsicAttributes[id];
|
|
|
|
|
|
|
|
ParamAttrsVector Attrs;
|
|
|
|
uint16_t Attr = ParamAttr::None;
|
|
|
|
|
|
|
|
#define GET_INTRINSIC_ATTRIBUTES
|
|
|
|
#include "llvm/Intrinsics.gen"
|
|
|
|
#undef GET_INTRINSIC_ATTRIBUTES
|
|
|
|
|
|
|
|
// Intrinsics cannot throw exceptions.
|
|
|
|
Attr |= ParamAttr::NoUnwind;
|
|
|
|
|
|
|
|
Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
|
|
|
|
IntrinsicAttributes[id] = ParamAttrsList::get(Attrs);
|
|
|
|
return IntrinsicAttributes[id];
|
|
|
|
}
|
|
|
|
|
2007-04-01 07:25:33 +00:00
|
|
|
Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
|
|
|
|
unsigned numTys) {
|
2007-12-03 20:06:50 +00:00
|
|
|
// There can never be multiple globals with the same name of different types,
|
|
|
|
// because intrinsics must be a specific type.
|
|
|
|
Function *F =
|
|
|
|
cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
|
|
|
|
getType(id, Tys, numTys)));
|
|
|
|
F->setParamAttrs(getParamAttrs(id));
|
|
|
|
return F;
|
2007-02-07 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2004-10-12 04:32:37 +00:00
|
|
|
Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
|
2004-10-12 04:20:25 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CE->getOpcode() == Instruction::BitCast) {
|
2004-10-12 04:20:25 +00:00
|
|
|
if (isa<PointerType>(CE->getOperand(0)->getType()))
|
|
|
|
return StripPointerCasts(CE->getOperand(0));
|
|
|
|
} else if (CE->getOpcode() == Instruction::GetElementPtr) {
|
|
|
|
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
if (!CE->getOperand(i)->isNullValue())
|
|
|
|
return Ptr;
|
|
|
|
return StripPointerCasts(CE->getOperand(0));
|
|
|
|
}
|
|
|
|
return Ptr;
|
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
|
2004-10-12 04:20:25 +00:00
|
|
|
if (isa<PointerType>(CI->getOperand(0)->getType()))
|
|
|
|
return StripPointerCasts(CI->getOperand(0));
|
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
|
2007-04-25 05:49:09 +00:00
|
|
|
if (GEP->hasAllZeroIndices())
|
|
|
|
return StripPointerCasts(GEP->getOperand(0));
|
2004-10-12 04:20:25 +00:00
|
|
|
}
|
|
|
|
return Ptr;
|
|
|
|
}
|
2003-05-08 03:47:33 +00:00
|
|
|
|
2004-07-17 23:50:19 +00:00
|
|
|
// vim: sw=2 ai
|