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
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file 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
|
|
|
//
|
2013-01-02 09:10:48 +00:00
|
|
|
// This file implements the Function class for the IR library.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2013-03-01 18:48:54 +00:00
|
|
|
#include "LLVMContextImpl.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/CodeGen/ValueTypes.h"
|
2014-03-04 11:01:28 +00:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2015-05-13 15:13:45 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-03-04 10:30:26 +00:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-05-13 15:13:45 +00:00
|
|
|
#include "llvm/IR/MDBuilder.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
|
|
|
// are not in the public header file...
|
2015-10-07 20:05:10 +00:00
|
|
|
template class llvm::SymbolTableListTraits<Argument>;
|
|
|
|
template class llvm::SymbolTableListTraits<BasicBlock>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-09 19:39:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Argument Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-20 02:50:00 +00:00
|
|
|
void Argument::anchor() { }
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Argument::Argument(Type *Ty, const Twine &Name, Function *Par)
|
2007-02-12 05:18:08 +00:00
|
|
|
: Value(Ty, Value::ArgumentVal) {
|
2014-04-09 06:08:46 +00:00
|
|
|
Parent = nullptr;
|
2002-09-08 18:59:35 +00:00
|
|
|
|
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) {
|
|
|
|
Parent = parent;
|
|
|
|
}
|
|
|
|
|
2008-01-24 17:47:11 +00:00
|
|
|
/// getArgNo - Return the index of this formal argument in its containing
|
2012-12-17 20:37:55 +00:00
|
|
|
/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
|
2008-01-24 17:47:11 +00:00
|
|
|
unsigned Argument::getArgNo() const {
|
|
|
|
const Function *F = getParent();
|
|
|
|
assert(F && "Argument is not in a function");
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2008-01-24 17:47:11 +00:00
|
|
|
Function::const_arg_iterator AI = F->arg_begin();
|
|
|
|
unsigned ArgIdx = 0;
|
|
|
|
for (; &*AI != this; ++AI)
|
|
|
|
++ArgIdx;
|
|
|
|
|
|
|
|
return ArgIdx;
|
|
|
|
}
|
|
|
|
|
2014-05-20 01:23:40 +00:00
|
|
|
/// hasNonNullAttr - Return true if this argument has the nonnull attribute on
|
2014-07-18 15:51:28 +00:00
|
|
|
/// it in its containing function. Also returns true if at least one byte is
|
|
|
|
/// known to be dereferenceable and the pointer is in addrspace(0).
|
2014-05-20 01:23:40 +00:00
|
|
|
bool Argument::hasNonNullAttr() const {
|
|
|
|
if (!getType()->isPointerTy()) return false;
|
2014-07-18 15:51:28 +00:00
|
|
|
if (getParent()->getAttributes().
|
|
|
|
hasAttribute(getArgNo()+1, Attribute::NonNull))
|
|
|
|
return true;
|
|
|
|
else if (getDereferenceableBytes() > 0 &&
|
|
|
|
getType()->getPointerAddressSpace() == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
2014-05-20 01:23:40 +00:00
|
|
|
}
|
|
|
|
|
2008-01-24 17:47:11 +00:00
|
|
|
/// hasByValAttr - Return true if this argument has the byval attribute on it
|
|
|
|
/// in its containing function.
|
|
|
|
bool Argument::hasByValAttr() const {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::ByVal);
|
2008-01-24 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 17:37:21 +00:00
|
|
|
bool Argument::hasSwiftSelfAttr() const {
|
|
|
|
return getParent()->getAttributes().
|
|
|
|
hasAttribute(getArgNo()+1, Attribute::SwiftSelf);
|
|
|
|
}
|
|
|
|
|
2016-04-01 21:41:15 +00:00
|
|
|
bool Argument::hasSwiftErrorAttr() const {
|
|
|
|
return getParent()->getAttributes().
|
|
|
|
hasAttribute(getArgNo()+1, Attribute::SwiftError);
|
|
|
|
}
|
|
|
|
|
2013-12-19 02:14:12 +00:00
|
|
|
/// \brief Return true if this argument has the inalloca attribute on it in
|
|
|
|
/// its containing function.
|
|
|
|
bool Argument::hasInAllocaAttr() const {
|
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::InAlloca);
|
2013-12-19 02:14:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 23:58:17 +00:00
|
|
|
bool Argument::hasByValOrInAllocaAttr() const {
|
|
|
|
if (!getType()->isPointerTy()) return false;
|
|
|
|
AttributeSet Attrs = getParent()->getAttributes();
|
|
|
|
return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
|
|
|
|
Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:57:23 +00:00
|
|
|
unsigned Argument::getParamAlignment() const {
|
|
|
|
assert(getType()->isPointerTy() && "Only pointers have alignments");
|
|
|
|
return getParent()->getParamAlignment(getArgNo()+1);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2011-05-22 23:57:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 15:51:28 +00:00
|
|
|
uint64_t Argument::getDereferenceableBytes() const {
|
|
|
|
assert(getType()->isPointerTy() &&
|
|
|
|
"Only pointers have dereferenceable bytes");
|
|
|
|
return getParent()->getDereferenceableBytes(getArgNo()+1);
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:41:54 +00:00
|
|
|
uint64_t Argument::getDereferenceableOrNullBytes() const {
|
|
|
|
assert(getType()->isPointerTy() &&
|
|
|
|
"Only pointers have dereferenceable bytes");
|
|
|
|
return getParent()->getDereferenceableOrNullBytes(getArgNo()+1);
|
|
|
|
}
|
|
|
|
|
2009-12-11 08:36:17 +00:00
|
|
|
/// hasNestAttr - Return true if this argument has the nest attribute on
|
|
|
|
/// it in its containing function.
|
|
|
|
bool Argument::hasNestAttr() const {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::Nest);
|
2009-12-11 08:36:17 +00:00
|
|
|
}
|
|
|
|
|
2008-01-24 17:47:11 +00:00
|
|
|
/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
|
|
|
|
/// it in its containing function.
|
|
|
|
bool Argument::hasNoAliasAttr() const {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::NoAlias);
|
2008-01-24 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-31 18:08:59 +00:00
|
|
|
/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
|
|
|
|
/// on it in its containing function.
|
|
|
|
bool Argument::hasNoCaptureAttr() const {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::NoCapture);
|
2008-12-31 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2008-02-17 23:22:28 +00:00
|
|
|
/// hasSRetAttr - Return true if this argument has the sret attribute on
|
|
|
|
/// it in its containing function.
|
|
|
|
bool Argument::hasStructRetAttr() const {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!getType()->isPointerTy()) return false;
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::StructRet);
|
2008-02-17 23:22:28 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 05:14:40 +00:00
|
|
|
/// hasReturnedAttr - Return true if this argument has the returned attribute on
|
|
|
|
/// it in its containing function.
|
|
|
|
bool Argument::hasReturnedAttr() const {
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::Returned);
|
2013-04-20 05:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 05:43:41 +00:00
|
|
|
/// hasZExtAttr - Return true if this argument has the zext attribute on it in
|
|
|
|
/// its containing function.
|
|
|
|
bool Argument::hasZExtAttr() const {
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::ZExt);
|
2014-08-05 05:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// hasSExtAttr Return true if this argument has the sext attribute on it in its
|
|
|
|
/// containing function.
|
|
|
|
bool Argument::hasSExtAttr() const {
|
2016-03-14 01:37:29 +00:00
|
|
|
return hasAttribute(Attribute::SExt);
|
2014-08-05 05:43:41 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 00:29:58 +00:00
|
|
|
/// Return true if this argument has the readonly or readnone attribute on it
|
|
|
|
/// in its containing function.
|
|
|
|
bool Argument::onlyReadsMemory() const {
|
|
|
|
return getParent()->getAttributes().
|
|
|
|
hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
|
|
|
|
getParent()->getAttributes().
|
|
|
|
hasAttribute(getArgNo()+1, Attribute::ReadNone);
|
|
|
|
}
|
|
|
|
|
2013-01-23 06:14:59 +00:00
|
|
|
/// addAttr - Add attributes to an argument.
|
|
|
|
void Argument::addAttr(AttributeSet AS) {
|
2013-02-21 19:46:51 +00:00
|
|
|
assert(AS.getNumSlots() <= 1 &&
|
2013-02-20 23:04:11 +00:00
|
|
|
"Trying to add more than one attribute set to an argument!");
|
|
|
|
AttrBuilder B(AS, AS.getSlotIndex(0));
|
|
|
|
getParent()->addAttributes(getArgNo() + 1,
|
|
|
|
AttributeSet::get(Parent->getContext(),
|
|
|
|
getArgNo() + 1, B));
|
2008-04-28 17:37:06 +00:00
|
|
|
}
|
2008-07-08 09:41:30 +00:00
|
|
|
|
2013-01-23 06:14:59 +00:00
|
|
|
/// removeAttr - Remove attributes from an argument.
|
|
|
|
void Argument::removeAttr(AttributeSet AS) {
|
2013-02-21 19:46:51 +00:00
|
|
|
assert(AS.getNumSlots() <= 1 &&
|
2013-02-20 23:04:11 +00:00
|
|
|
"Trying to remove more than one attribute set from an argument!");
|
|
|
|
AttrBuilder B(AS, AS.getSlotIndex(0));
|
|
|
|
getParent()->removeAttributes(getArgNo() + 1,
|
|
|
|
AttributeSet::get(Parent->getContext(),
|
|
|
|
getArgNo() + 1, B));
|
2008-04-28 17:37:06 +00:00
|
|
|
}
|
2008-01-24 17:47:11 +00:00
|
|
|
|
2016-03-14 01:37:29 +00:00
|
|
|
/// hasAttribute - Checks if an argument has a given attribute.
|
|
|
|
bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
|
|
|
|
return getParent()->hasAttribute(getArgNo() + 1, Kind);
|
|
|
|
}
|
|
|
|
|
2007-04-09 15:01:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-02 23:42:30 +00:00
|
|
|
// Helper Methods in Function
|
2007-04-09 15:01:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-24 18:13:04 +00:00
|
|
|
bool Function::isMaterializable() const {
|
2016-05-31 23:01:54 +00:00
|
|
|
return getGlobalObjectSubClassData() & (1 << IsMaterializableBit);
|
2014-10-24 18:13:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 20:47:23 +00:00
|
|
|
void Function::setIsMaterializable(bool V) {
|
2016-05-31 23:01:54 +00:00
|
|
|
unsigned Mask = 1 << IsMaterializableBit;
|
|
|
|
setGlobalObjectSubClassData((~Mask & getGlobalObjectSubClassData()) |
|
|
|
|
(V ? Mask : 0u));
|
2015-04-24 20:47:23 +00:00
|
|
|
}
|
2014-10-24 18:13:04 +00:00
|
|
|
|
2009-07-22 00:24:57 +00:00
|
|
|
LLVMContext &Function::getContext() const {
|
|
|
|
return getType()->getContext();
|
2009-07-02 18:03:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 23:07:58 +00:00
|
|
|
FunctionType *Function::getFunctionType() const {
|
|
|
|
return cast<FunctionType>(getValueType());
|
|
|
|
}
|
2007-04-09 15:01:12 +00:00
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
bool Function::isVarArg() const {
|
|
|
|
return getFunctionType()->isVarArg();
|
2007-04-09 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
Type *Function::getReturnType() const {
|
2008-01-02 23:42:30 +00:00
|
|
|
return getFunctionType()->getReturnType();
|
2007-11-25 14:10:56 +00:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
void Function::removeFromParent() {
|
2015-10-08 23:49:46 +00:00
|
|
|
getParent()->getFunctionList().remove(getIterator());
|
2007-11-25 14:10:56 +00:00
|
|
|
}
|
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
void Function::eraseFromParent() {
|
2015-10-08 23:49:46 +00:00
|
|
|
getParent()->getFunctionList().erase(getIterator());
|
2007-04-09 15:01:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-10-23 15:20:05 +00:00
|
|
|
Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name,
|
|
|
|
Module *ParentModule)
|
2015-08-21 21:35:28 +00:00
|
|
|
: GlobalObject(Ty, Value::FunctionVal,
|
2016-01-15 23:07:58 +00:00
|
|
|
OperandTraits<Function>::op_begin(this), 0, Linkage, name) {
|
2009-01-05 07:58:59 +00:00
|
|
|
assert(FunctionType::isValidReturnType(getReturnType()) &&
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
"invalid return type");
|
2015-04-24 20:47:23 +00:00
|
|
|
setGlobalObjectSubClassData(0);
|
2007-02-05 20:47:22 +00:00
|
|
|
SymTab = new ValueSymbolTable();
|
2002-09-06 20:46:32 +00:00
|
|
|
|
2007-08-18 06:14:52 +00:00
|
|
|
// If the function has arguments, mark them as lazily built.
|
|
|
|
if (Ty->getNumParams())
|
2009-12-29 02:14:09 +00:00
|
|
|
setValueSubclassData(1); // Set the "has lazy arguments" bit.
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2002-09-06 20:46:32 +00:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getFunctionList().push_back(this);
|
2008-04-07 13:39:11 +00:00
|
|
|
|
|
|
|
// Ensure intrinsics have the right parameter attributes.
|
2015-05-19 00:24:26 +00:00
|
|
|
// Note, the IntID field will have been set in Value::setName if this function
|
|
|
|
// name is a valid intrinsic ID.
|
|
|
|
if (IntID)
|
|
|
|
setAttributes(Intrinsic::getAttributes(getContext(), IntID));
|
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
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
// Remove the function from the on-the-side GC table.
|
|
|
|
clearGC();
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FT = getFunctionType();
|
2007-08-18 06:14:52 +00:00
|
|
|
for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
|
2010-01-05 13:12:22 +00:00
|
|
|
assert(!FT->getParamType(i)->isVoidTy() &&
|
2007-08-18 06:14:52 +00:00
|
|
|
"Cannot have void typed arguments!");
|
|
|
|
ArgumentList.push_back(new Argument(FT->getParamType(i)));
|
|
|
|
}
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2007-08-18 06:14:52 +00:00
|
|
|
// Clear the lazy arguments bit.
|
2009-12-29 02:14:09 +00:00
|
|
|
unsigned SDC = getSubclassDataFromValue();
|
2014-12-03 02:08:38 +00:00
|
|
|
const_cast<Function*>(this)->setValueSubclassData(SDC &= ~(1<<0));
|
2007-08-18 06:14:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 06:38:15 +00:00
|
|
|
void Function::stealArgumentListFrom(Function &Src) {
|
|
|
|
assert(isDeclaration() && "Expected no references to current arguments");
|
|
|
|
|
|
|
|
// Drop the current arguments, if any, and set the lazy argument bit.
|
|
|
|
if (!hasLazyArguments()) {
|
|
|
|
assert(llvm::all_of(ArgumentList,
|
|
|
|
[](const Argument &A) { return A.use_empty(); }) &&
|
|
|
|
"Expected arguments to be unused in declaration");
|
|
|
|
ArgumentList.clear();
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | (1 << 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to steal if Src has lazy arguments.
|
|
|
|
if (Src.hasLazyArguments())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Steal arguments from Src, and fix the lazy argument bits.
|
|
|
|
ArgumentList.splice(ArgumentList.end(), Src.ArgumentList);
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() & ~(1 << 0));
|
|
|
|
Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
|
|
|
|
}
|
|
|
|
|
2007-08-18 06:14:52 +00:00
|
|
|
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) {
|
2001-06-06 20:29:01 +00:00
|
|
|
Parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2014-10-24 18:13:04 +00:00
|
|
|
setIsMaterializable(false);
|
|
|
|
|
2016-06-26 14:10:56 +00:00
|
|
|
for (BasicBlock &BB : *this)
|
|
|
|
BB.dropAllReferences();
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2010-12-07 19:56:51 +00:00
|
|
|
// Delete all basic blocks. They are now unused, except possibly by
|
|
|
|
// blockaddresses, but BasicBlock's destructor takes care of those.
|
|
|
|
while (!BasicBlocks.empty())
|
|
|
|
BasicBlocks.begin()->eraseFromParent();
|
2013-09-16 01:08:15 +00:00
|
|
|
|
2015-12-19 08:52:49 +00:00
|
|
|
// Drop uses of any optional data (real or placeholder).
|
|
|
|
if (getNumOperands()) {
|
|
|
|
User::dropAllReferences();
|
|
|
|
setNumHungOffUseOperands(0);
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() & ~0xe);
|
|
|
|
}
|
2015-04-24 21:51:02 +00:00
|
|
|
|
|
|
|
// Metadata is stored in a side-table.
|
|
|
|
clearMetadata();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2016-06-14 20:27:35 +00:00
|
|
|
void Function::addAttribute(unsigned i, Attribute::AttrKind Kind) {
|
2012-12-07 23:16:57 +00:00
|
|
|
AttributeSet PAL = getAttributes();
|
2016-06-14 20:27:35 +00:00
|
|
|
PAL = PAL.addAttribute(getContext(), i, Kind);
|
2013-01-23 00:20:53 +00:00
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2016-06-12 06:17:24 +00:00
|
|
|
void Function::addAttribute(unsigned i, Attribute Attr) {
|
|
|
|
AttributeSet PAL = getAttributes();
|
|
|
|
PAL = PAL.addAttribute(getContext(), i, Attr);
|
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:27:35 +00:00
|
|
|
void Function::addAttributes(unsigned i, AttributeSet Attrs) {
|
2013-01-23 00:20:53 +00:00
|
|
|
AttributeSet PAL = getAttributes();
|
2016-06-14 20:27:35 +00:00
|
|
|
PAL = PAL.addAttributes(getContext(), i, Attrs);
|
2008-09-25 21:00:45 +00:00
|
|
|
setAttributes(PAL);
|
2008-04-08 07:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 20:27:35 +00:00
|
|
|
void Function::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
|
2016-03-14 01:37:29 +00:00
|
|
|
AttributeSet PAL = getAttributes();
|
2016-06-14 20:27:35 +00:00
|
|
|
PAL = PAL.removeAttribute(getContext(), i, Kind);
|
2016-03-14 01:37:29 +00:00
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:50:39 +00:00
|
|
|
void Function::removeAttribute(unsigned i, StringRef Kind) {
|
|
|
|
AttributeSet PAL = getAttributes();
|
|
|
|
PAL = PAL.removeAttribute(getContext(), i, Kind);
|
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:27:35 +00:00
|
|
|
void Function::removeAttributes(unsigned i, AttributeSet Attrs) {
|
2012-12-07 23:16:57 +00:00
|
|
|
AttributeSet PAL = getAttributes();
|
2016-06-14 20:27:35 +00:00
|
|
|
PAL = PAL.removeAttributes(getContext(), i, Attrs);
|
2008-09-25 21:00:45 +00:00
|
|
|
setAttributes(PAL);
|
2008-05-16 20:39:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 19:37:54 +00:00
|
|
|
void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
|
|
|
|
AttributeSet PAL = getAttributes();
|
|
|
|
PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
|
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2015-04-16 20:29:50 +00:00
|
|
|
void Function::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
|
|
|
|
AttributeSet PAL = getAttributes();
|
|
|
|
PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
|
|
|
|
setAttributes(PAL);
|
|
|
|
}
|
|
|
|
|
2016-01-08 02:28:20 +00:00
|
|
|
const std::string &Function::getGC() const {
|
2008-08-17 18:44:35 +00:00
|
|
|
assert(hasGC() && "Function has no collector");
|
2016-01-08 02:28:20 +00:00
|
|
|
return getContext().getGC(*this);
|
2007-12-10 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 10:46:35 +00:00
|
|
|
void Function::setGC(std::string Str) {
|
2016-01-08 02:28:20 +00:00
|
|
|
setValueSubclassDataBit(14, !Str.empty());
|
|
|
|
getContext().setGC(*this, std::move(Str));
|
2007-12-10 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
2008-08-17 18:44:35 +00:00
|
|
|
void Function::clearGC() {
|
2016-01-08 02:28:20 +00:00
|
|
|
if (!hasGC())
|
|
|
|
return;
|
|
|
|
getContext().deleteGC(*this);
|
|
|
|
setValueSubclassDataBit(14, false);
|
2007-12-10 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 20:03:17 +00:00
|
|
|
/// Copy all additional attributes (those not needed to create a Function) from
|
|
|
|
/// the Function Src to this one.
|
2008-05-26 19:58:59 +00:00
|
|
|
void Function::copyAttributesFrom(const GlobalValue *Src) {
|
2014-05-13 18:45:48 +00:00
|
|
|
GlobalObject::copyAttributesFrom(Src);
|
2015-12-02 20:03:17 +00:00
|
|
|
const Function *SrcF = dyn_cast<Function>(Src);
|
|
|
|
if (!SrcF)
|
|
|
|
return;
|
|
|
|
|
2008-05-26 19:58:59 +00:00
|
|
|
setCallingConv(SrcF->getCallingConv());
|
2008-09-25 21:00:45 +00:00
|
|
|
setAttributes(SrcF->getAttributes());
|
2008-08-17 18:44:35 +00:00
|
|
|
if (SrcF->hasGC())
|
|
|
|
setGC(SrcF->getGC());
|
|
|
|
else
|
|
|
|
clearGC();
|
2015-12-19 08:52:49 +00:00
|
|
|
if (SrcF->hasPersonalityFn())
|
|
|
|
setPersonalityFn(SrcF->getPersonalityFn());
|
2013-09-16 01:08:15 +00:00
|
|
|
if (SrcF->hasPrefixData())
|
|
|
|
setPrefixData(SrcF->getPrefixData());
|
2014-12-03 02:08:38 +00:00
|
|
|
if (SrcF->hasPrologueData())
|
|
|
|
setPrologueData(SrcF->getPrologueData());
|
2008-05-26 19:58:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 02:06:41 +00:00
|
|
|
/// Table of string intrinsic names indexed by enum value.
|
|
|
|
static const char * const IntrinsicNameTable[] = {
|
|
|
|
"not_intrinsic",
|
|
|
|
#define GET_INTRINSIC_NAME_TABLE
|
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
|
|
|
#undef GET_INTRINSIC_NAME_TABLE
|
|
|
|
};
|
|
|
|
|
2016-07-15 16:31:37 +00:00
|
|
|
/// Table of per-target intrinsic name tables.
|
|
|
|
#define GET_INTRINSIC_TARGET_DATA
|
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
|
|
|
#undef GET_INTRINSIC_TARGET_DATA
|
|
|
|
|
|
|
|
/// Find the segment of \c IntrinsicNameTable for intrinsics with the same
|
|
|
|
/// target as \c Name, or the generic table if \c Name is not target specific.
|
|
|
|
///
|
|
|
|
/// Returns the relevant slice of \c IntrinsicNameTable
|
|
|
|
static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
|
|
|
|
assert(Name.startswith("llvm."));
|
|
|
|
|
|
|
|
ArrayRef<IntrinsicTargetInfo> Targets(TargetInfos);
|
|
|
|
// Drop "llvm." and take the first dotted component. That will be the target
|
|
|
|
// if this is target specific.
|
|
|
|
StringRef Target = Name.drop_front(5).split('.').first;
|
|
|
|
auto It = std::lower_bound(Targets.begin(), Targets.end(), Target,
|
|
|
|
[](const IntrinsicTargetInfo &TI,
|
|
|
|
StringRef Target) { return TI.Name < Target; });
|
|
|
|
// We've either found the target or just fall back to the generic set, which
|
|
|
|
// is always first.
|
|
|
|
const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
|
|
|
|
return makeArrayRef(&IntrinsicNameTable[1] + TI.Offset, TI.Count);
|
|
|
|
}
|
|
|
|
|
2015-05-19 00:02:25 +00:00
|
|
|
/// \brief This does the actual lookup of an intrinsic ID which
|
|
|
|
/// matches the given function name.
|
2016-07-29 20:32:59 +00:00
|
|
|
Intrinsic::ID Function::lookupIntrinsicID(StringRef Name) {
|
2016-07-15 16:31:37 +00:00
|
|
|
ArrayRef<const char *> NameTable = findTargetSubtable(Name);
|
2016-01-26 22:33:19 +00:00
|
|
|
int Idx = Intrinsic::lookupLLVMIntrinsicByName(NameTable, Name);
|
2016-07-15 16:31:37 +00:00
|
|
|
if (Idx == -1)
|
|
|
|
return Intrinsic::not_intrinsic;
|
|
|
|
|
|
|
|
// Intrinsic IDs correspond to the location in IntrinsicNameTable, but we have
|
|
|
|
// an index into a sub-table.
|
|
|
|
int Adjust = NameTable.data() - IntrinsicNameTable;
|
|
|
|
Intrinsic::ID ID = static_cast<Intrinsic::ID>(Idx + Adjust);
|
2015-05-19 00:02:25 +00:00
|
|
|
|
2016-01-26 02:06:41 +00:00
|
|
|
// If the intrinsic is not overloaded, require an exact match. If it is
|
|
|
|
// overloaded, require a prefix match.
|
|
|
|
bool IsPrefixMatch = Name.size() > strlen(NameTable[Idx]);
|
|
|
|
return IsPrefixMatch == isOverloaded(ID) ? ID : Intrinsic::not_intrinsic;
|
2015-05-19 00:02:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 00:24:26 +00:00
|
|
|
void Function::recalculateIntrinsicID() {
|
2016-07-28 23:58:15 +00:00
|
|
|
const ValueName *ValName = this->getValueName();
|
|
|
|
if (!ValName || !isIntrinsic()) {
|
2015-05-19 00:24:26 +00:00
|
|
|
IntID = Intrinsic::not_intrinsic;
|
|
|
|
return;
|
2013-03-01 18:48:54 +00:00
|
|
|
}
|
2016-07-29 20:32:59 +00:00
|
|
|
IntID = lookupIntrinsicID(ValName->getKey());
|
2013-03-01 18:48:54 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 00:21:51 +00:00
|
|
|
/// Returns a stable mangling for the type specified for use in the name
|
2014-11-24 23:24:24 +00:00
|
|
|
/// mangling scheme used by 'any' types in intrinsic signatures. The mangling
|
|
|
|
/// of named types is simply their name. Manglings for unnamed types consist
|
|
|
|
/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions)
|
|
|
|
/// combined with the mangling of their component types. A vararg function
|
|
|
|
/// type will have a suffix of 'vararg'. Since function types can contain
|
|
|
|
/// other function types, we close a function type mangling with suffix 'f'
|
|
|
|
/// which can't be confused with it's prefix. This ensures we don't have
|
|
|
|
/// collisions between two unrelated function types. Otherwise, you might
|
|
|
|
/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.)
|
2015-01-14 23:05:17 +00:00
|
|
|
/// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most
|
|
|
|
/// cases) fall back to the MVT codepath, where they could be mangled to
|
|
|
|
/// 'x86mmx', for example; matching on derived types is not sufficient to mangle
|
|
|
|
/// everything.
|
2014-11-12 00:21:51 +00:00
|
|
|
static std::string getMangledTypeStr(Type* Ty) {
|
|
|
|
std::string Result;
|
|
|
|
if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
|
|
|
|
Result += "p" + llvm::utostr(PTyp->getAddressSpace()) +
|
|
|
|
getMangledTypeStr(PTyp->getElementType());
|
|
|
|
} else if (ArrayType* ATyp = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Result += "a" + llvm::utostr(ATyp->getNumElements()) +
|
|
|
|
getMangledTypeStr(ATyp->getElementType());
|
|
|
|
} else if (StructType* STyp = dyn_cast<StructType>(Ty)) {
|
2015-05-08 18:52:28 +00:00
|
|
|
assert(!STyp->isLiteral() && "TODO: implement literal types");
|
|
|
|
Result += STyp->getName();
|
2014-11-12 00:21:51 +00:00
|
|
|
} else if (FunctionType* FT = dyn_cast<FunctionType>(Ty)) {
|
|
|
|
Result += "f_" + getMangledTypeStr(FT->getReturnType());
|
|
|
|
for (size_t i = 0; i < FT->getNumParams(); i++)
|
|
|
|
Result += getMangledTypeStr(FT->getParamType(i));
|
|
|
|
if (FT->isVarArg())
|
|
|
|
Result += "vararg";
|
2014-11-24 23:24:24 +00:00
|
|
|
// Ensure nested function types are distinguishable.
|
|
|
|
Result += "f";
|
2015-11-19 07:17:16 +00:00
|
|
|
} else if (isa<VectorType>(Ty))
|
|
|
|
Result += "v" + utostr(Ty->getVectorNumElements()) +
|
|
|
|
getMangledTypeStr(Ty->getVectorElementType());
|
|
|
|
else if (Ty)
|
2014-11-12 00:21:51 +00:00
|
|
|
Result += EVT::getEVT(Ty).getEVTString();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-08-18 18:30:54 +00:00
|
|
|
StringRef Intrinsic::getName(ID id) {
|
|
|
|
assert(id < num_intrinsics && "Invalid intrinsic ID!");
|
|
|
|
return IntrinsicNameTable[id];
|
|
|
|
}
|
|
|
|
|
2011-07-14 17:45:39 +00:00
|
|
|
std::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
|
2006-03-25 06:32:47 +00:00
|
|
|
assert(id < num_intrinsics && "Invalid intrinsic ID!");
|
2016-01-26 02:06:41 +00:00
|
|
|
std::string Result(IntrinsicNameTable[id]);
|
2016-06-26 14:10:56 +00:00
|
|
|
for (Type *Ty : Tys) {
|
|
|
|
Result += "." + getMangledTypeStr(Ty);
|
2008-07-30 04:36:53 +00:00
|
|
|
}
|
2007-04-01 07:25:33 +00:00
|
|
|
return Result;
|
2006-03-25 06:32:47 +00:00
|
|
|
}
|
|
|
|
|
2012-05-16 06:34:44 +00:00
|
|
|
|
2012-05-27 18:28:35 +00:00
|
|
|
/// IIT_Info - These are enumerators that describe the entries returned by the
|
|
|
|
/// getIntrinsicInfoTableEntries function.
|
|
|
|
///
|
|
|
|
/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
|
|
|
|
enum IIT_Info {
|
2014-10-20 19:25:05 +00:00
|
|
|
// Common values should be encoded with 0-15.
|
2012-05-27 18:28:35 +00:00
|
|
|
IIT_Done = 0,
|
|
|
|
IIT_I1 = 1,
|
|
|
|
IIT_I8 = 2,
|
|
|
|
IIT_I16 = 3,
|
|
|
|
IIT_I32 = 4,
|
|
|
|
IIT_I64 = 5,
|
2013-01-11 01:45:05 +00:00
|
|
|
IIT_F16 = 6,
|
|
|
|
IIT_F32 = 7,
|
|
|
|
IIT_F64 = 8,
|
|
|
|
IIT_V2 = 9,
|
|
|
|
IIT_V4 = 10,
|
|
|
|
IIT_V8 = 11,
|
|
|
|
IIT_V16 = 12,
|
|
|
|
IIT_V32 = 13,
|
2014-10-20 19:25:05 +00:00
|
|
|
IIT_PTR = 14,
|
|
|
|
IIT_ARG = 15,
|
2014-09-30 11:32:22 +00:00
|
|
|
|
2014-10-20 19:25:05 +00:00
|
|
|
// Values from 16+ are only encodable with the inefficient encoding.
|
|
|
|
IIT_V64 = 16,
|
2014-09-30 11:32:22 +00:00
|
|
|
IIT_MMX = 17,
|
2015-09-02 13:36:25 +00:00
|
|
|
IIT_TOKEN = 18,
|
|
|
|
IIT_METADATA = 19,
|
|
|
|
IIT_EMPTYSTRUCT = 20,
|
|
|
|
IIT_STRUCT2 = 21,
|
|
|
|
IIT_STRUCT3 = 22,
|
|
|
|
IIT_STRUCT4 = 23,
|
|
|
|
IIT_STRUCT5 = 24,
|
|
|
|
IIT_EXTEND_ARG = 25,
|
|
|
|
IIT_TRUNC_ARG = 26,
|
|
|
|
IIT_ANYPTR = 27,
|
|
|
|
IIT_V1 = 28,
|
|
|
|
IIT_VARARG = 29,
|
|
|
|
IIT_HALF_VEC_ARG = 30,
|
|
|
|
IIT_SAME_VEC_WIDTH_ARG = 31,
|
|
|
|
IIT_PTR_TO_ARG = 32,
|
|
|
|
IIT_VEC_OF_PTRS_TO_ELT = 33,
|
2015-11-24 16:28:14 +00:00
|
|
|
IIT_I128 = 34,
|
|
|
|
IIT_V512 = 35,
|
|
|
|
IIT_V1024 = 36
|
2012-05-27 18:28:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
|
|
|
|
SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
|
2012-05-17 05:13:57 +00:00
|
|
|
IIT_Info Info = IIT_Info(Infos[NextElt++]);
|
2012-05-17 05:03:24 +00:00
|
|
|
unsigned StructElts = 2;
|
2012-05-27 18:28:35 +00:00
|
|
|
using namespace Intrinsic;
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-17 05:13:57 +00:00
|
|
|
switch (Info) {
|
2012-05-27 18:28:35 +00:00
|
|
|
case IIT_Done:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
|
|
|
|
return;
|
2013-10-31 17:18:11 +00:00
|
|
|
case IIT_VARARG:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::VarArg, 0));
|
|
|
|
return;
|
2012-05-27 18:28:35 +00:00
|
|
|
case IIT_MMX:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
|
|
|
|
return;
|
2015-09-02 13:36:25 +00:00
|
|
|
case IIT_TOKEN:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Token, 0));
|
|
|
|
return;
|
2012-05-27 18:28:35 +00:00
|
|
|
case IIT_METADATA:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
|
|
|
|
return;
|
2013-01-11 01:45:05 +00:00
|
|
|
case IIT_F16:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
|
|
|
|
return;
|
2012-05-27 18:28:35 +00:00
|
|
|
case IIT_F32:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
|
|
|
|
return;
|
|
|
|
case IIT_F64:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
|
|
|
|
return;
|
|
|
|
case IIT_I1:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
|
|
|
|
return;
|
|
|
|
case IIT_I8:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
|
|
|
|
return;
|
|
|
|
case IIT_I16:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
|
|
|
|
return;
|
|
|
|
case IIT_I32:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
|
|
|
|
return;
|
|
|
|
case IIT_I64:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
|
|
|
|
return;
|
2015-05-25 15:49:26 +00:00
|
|
|
case IIT_I128:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 128));
|
|
|
|
return;
|
2013-09-24 02:47:27 +00:00
|
|
|
case IIT_V1:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 04:30:58 +00:00
|
|
|
case IIT_V2:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 04:30:58 +00:00
|
|
|
case IIT_V4:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 04:30:58 +00:00
|
|
|
case IIT_V8:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 04:30:58 +00:00
|
|
|
case IIT_V16:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 05:03:24 +00:00
|
|
|
case IIT_V32:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2014-09-30 11:32:22 +00:00
|
|
|
case IIT_V64:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 64));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2015-11-24 16:28:14 +00:00
|
|
|
case IIT_V512:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 512));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
|
|
|
case IIT_V1024:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 1024));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-23 05:19:18 +00:00
|
|
|
case IIT_PTR:
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-23 05:19:18 +00:00
|
|
|
case IIT_ANYPTR: { // [ANYPTR addrspace, subtype]
|
2012-12-17 20:37:55 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
|
2012-05-27 18:28:35 +00:00
|
|
|
Infos[NextElt++]));
|
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case IIT_ARG: {
|
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
|
|
|
|
return;
|
|
|
|
}
|
2014-03-28 12:31:39 +00:00
|
|
|
case IIT_EXTEND_ARG: {
|
2012-05-27 18:28:35 +00:00
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
2014-03-28 12:31:39 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendArgument,
|
2012-05-27 18:28:35 +00:00
|
|
|
ArgInfo));
|
|
|
|
return;
|
2012-05-21 23:21:28 +00:00
|
|
|
}
|
2014-03-28 12:31:39 +00:00
|
|
|
case IIT_TRUNC_ARG: {
|
2012-05-27 18:28:35 +00:00
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
2014-03-28 12:31:39 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncArgument,
|
2012-05-27 18:28:35 +00:00
|
|
|
ArgInfo));
|
|
|
|
return;
|
2012-05-17 05:03:24 +00:00
|
|
|
}
|
2014-03-29 07:04:54 +00:00
|
|
|
case IIT_HALF_VEC_ARG: {
|
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::HalfVecArgument,
|
|
|
|
ArgInfo));
|
|
|
|
return;
|
|
|
|
}
|
2014-12-04 09:40:44 +00:00
|
|
|
case IIT_SAME_VEC_WIDTH_ARG: {
|
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument,
|
|
|
|
ArgInfo));
|
|
|
|
return;
|
|
|
|
}
|
2014-12-25 07:49:20 +00:00
|
|
|
case IIT_PTR_TO_ARG: {
|
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument,
|
|
|
|
ArgInfo));
|
|
|
|
return;
|
|
|
|
}
|
Masked Gather and Scatter Intrinsics.
Gather and Scatter are new introduced intrinsics, comming after recently implemented masked load and store.
This is the first patch for Gather and Scatter intrinsics. It includes only the syntax, parsing and verification.
Gather and Scatter intrinsics allow to perform multiple memory accesses (read/write) in one vector instruction.
The intrinsics are not target specific and will have the following syntax:
Gather:
declare <16 x i32> @llvm.masked.gather.v16i32(<16 x i32*> <vector of ptrs>, i32 <alignment>, <16 x i1> <mask>, <16 x i32> <passthru>)
declare <8 x float> @llvm.masked.gather.v8f32(<8 x float*><vector of ptrs>, i32 <alignment>, <8 x i1> <mask>, <8 x float><passthru>)
Scatter:
declare void @llvm.masked.scatter.v8i32(<8 x i32><vector value to be stored> , <8 x i32*><vector of ptrs> , i32 <alignment>, <8 x i1> <mask>)
declare void @llvm.masked.scatter.v16i32(<16 x i32> <vector value to be stored> , <16 x i32*> <vector of ptrs>, i32 <alignment>, <16 x i1><mask> )
Vector of ptrs - a set of source/destination addresses, to load/store the value.
Mask - switches on/off vector lanes to prevent memory access for switched-off lanes
vector of ptrs, value and mask should have the same vector width.
These are code examples where gather / scatter should be used and will allow function vectorization
;void foo1(int * restrict A, int * restrict B, int * restrict C) {
; for (int i=0; i<SIZE; i++) {
; A[i] = B[C[i]];
; }
;}
;void foo3(int * restrict A, int * restrict B) {
; for (int i=0; i<SIZE; i++) {
; A[B[i]] = i+5;
; }
;}
Tests will come in the following patches, with CodeGen and Vectorizer.
http://reviews.llvm.org/D7433
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228521 91177308-0d34-0410-b5e6-96231b3b80d8
2015-02-08 08:27:19 +00:00
|
|
|
case IIT_VEC_OF_PTRS_TO_ELT: {
|
|
|
|
unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt,
|
|
|
|
ArgInfo));
|
|
|
|
return;
|
|
|
|
}
|
2012-05-27 18:28:35 +00:00
|
|
|
case IIT_EMPTYSTRUCT:
|
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
|
|
|
|
return;
|
2016-08-17 20:30:52 +00:00
|
|
|
case IIT_STRUCT5: ++StructElts; LLVM_FALLTHROUGH;
|
|
|
|
case IIT_STRUCT4: ++StructElts; LLVM_FALLTHROUGH;
|
|
|
|
case IIT_STRUCT3: ++StructElts; LLVM_FALLTHROUGH;
|
2012-05-17 05:03:24 +00:00
|
|
|
case IIT_STRUCT2: {
|
2012-05-27 18:28:35 +00:00
|
|
|
OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
|
|
|
|
|
2012-05-17 05:03:24 +00:00
|
|
|
for (unsigned i = 0; i != StructElts; ++i)
|
2012-05-27 18:28:35 +00:00
|
|
|
DecodeIITType(NextElt, Infos, OutputTable);
|
|
|
|
return;
|
2012-05-17 04:30:58 +00:00
|
|
|
}
|
2012-05-16 06:34:44 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled");
|
|
|
|
}
|
|
|
|
|
2012-05-27 18:28:35 +00:00
|
|
|
|
|
|
|
#define GET_INTRINSIC_GENERATOR_GLOBAL
|
2013-01-02 12:09:16 +00:00
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
2012-05-27 18:28:35 +00:00
|
|
|
#undef GET_INTRINSIC_GENERATOR_GLOBAL
|
|
|
|
|
2012-12-17 20:37:55 +00:00
|
|
|
void Intrinsic::getIntrinsicInfoTableEntries(ID id,
|
2012-05-27 18:28:35 +00:00
|
|
|
SmallVectorImpl<IITDescriptor> &T){
|
2012-05-16 06:34:44 +00:00
|
|
|
// Check to see if the intrinsic's type was expressible by the table.
|
|
|
|
unsigned TableVal = IIT_Table[id-1];
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-17 15:55:41 +00:00
|
|
|
// Decode the TableVal into an array of IITValues.
|
|
|
|
SmallVector<unsigned char, 8> IITValues;
|
|
|
|
ArrayRef<unsigned char> IITEntries;
|
|
|
|
unsigned NextElt = 0;
|
|
|
|
if ((TableVal >> 31) != 0) {
|
|
|
|
// This is an offset into the IIT_LongEncodingTable.
|
|
|
|
IITEntries = IIT_LongEncodingTable;
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-17 15:55:41 +00:00
|
|
|
// Strip sentinel bit.
|
|
|
|
NextElt = (TableVal << 1) >> 1;
|
|
|
|
} else {
|
2012-05-27 18:28:35 +00:00
|
|
|
// Decode the TableVal into an array of IITValues. If the entry was encoded
|
|
|
|
// into a single word in the table itself, decode it now.
|
2012-05-17 05:13:57 +00:00
|
|
|
do {
|
|
|
|
IITValues.push_back(TableVal & 0xF);
|
|
|
|
TableVal >>= 4;
|
|
|
|
} while (TableVal);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-17 15:55:41 +00:00
|
|
|
IITEntries = IITValues;
|
|
|
|
NextElt = 0;
|
2012-05-16 06:34:44 +00:00
|
|
|
}
|
2012-05-27 18:28:35 +00:00
|
|
|
|
|
|
|
// Okay, decode the table into the output vector of IITDescriptors.
|
|
|
|
DecodeIITType(NextElt, IITEntries, T);
|
|
|
|
while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
|
|
|
|
DecodeIITType(NextElt, IITEntries, T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
|
|
|
|
ArrayRef<Type*> Tys, LLVMContext &Context) {
|
|
|
|
using namespace Intrinsic;
|
|
|
|
IITDescriptor D = Infos.front();
|
|
|
|
Infos = Infos.slice(1);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-27 18:28:35 +00:00
|
|
|
switch (D.Kind) {
|
|
|
|
case IITDescriptor::Void: return Type::getVoidTy(Context);
|
2013-10-31 17:18:11 +00:00
|
|
|
case IITDescriptor::VarArg: return Type::getVoidTy(Context);
|
2012-05-27 18:28:35 +00:00
|
|
|
case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
|
2015-09-02 13:36:25 +00:00
|
|
|
case IITDescriptor::Token: return Type::getTokenTy(Context);
|
2012-05-27 18:28:35 +00:00
|
|
|
case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
|
2013-01-11 01:45:05 +00:00
|
|
|
case IITDescriptor::Half: return Type::getHalfTy(Context);
|
2012-05-27 18:28:35 +00:00
|
|
|
case IITDescriptor::Float: return Type::getFloatTy(Context);
|
|
|
|
case IITDescriptor::Double: return Type::getDoubleTy(Context);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-27 18:28:35 +00:00
|
|
|
case IITDescriptor::Integer:
|
|
|
|
return IntegerType::get(Context, D.Integer_Width);
|
|
|
|
case IITDescriptor::Vector:
|
|
|
|
return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
|
|
|
|
case IITDescriptor::Pointer:
|
|
|
|
return PointerType::get(DecodeFixedType(Infos, Tys, Context),
|
|
|
|
D.Pointer_AddressSpace);
|
|
|
|
case IITDescriptor::Struct: {
|
|
|
|
Type *Elts[5];
|
|
|
|
assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
|
|
|
|
for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
|
|
|
|
Elts[i] = DecodeFixedType(Infos, Tys, Context);
|
2014-08-27 05:25:25 +00:00
|
|
|
return StructType::get(Context, makeArrayRef(Elts,D.Struct_NumElements));
|
2012-05-27 18:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case IITDescriptor::Argument:
|
|
|
|
return Tys[D.getArgumentNumber()];
|
2014-03-28 12:31:39 +00:00
|
|
|
case IITDescriptor::ExtendArgument: {
|
|
|
|
Type *Ty = Tys[D.getArgumentNumber()];
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return VectorType::getExtendedElementVectorType(VTy);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2014-03-28 12:31:39 +00:00
|
|
|
return IntegerType::get(Context, 2 * cast<IntegerType>(Ty)->getBitWidth());
|
|
|
|
}
|
|
|
|
case IITDescriptor::TruncArgument: {
|
|
|
|
Type *Ty = Tys[D.getArgumentNumber()];
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return VectorType::getTruncatedElementVectorType(VTy);
|
|
|
|
|
|
|
|
IntegerType *ITy = cast<IntegerType>(Ty);
|
|
|
|
assert(ITy->getBitWidth() % 2 == 0);
|
|
|
|
return IntegerType::get(Context, ITy->getBitWidth() / 2);
|
|
|
|
}
|
2014-03-29 07:04:54 +00:00
|
|
|
case IITDescriptor::HalfVecArgument:
|
|
|
|
return VectorType::getHalfElementsVectorType(cast<VectorType>(
|
|
|
|
Tys[D.getArgumentNumber()]));
|
2014-12-25 07:49:20 +00:00
|
|
|
case IITDescriptor::SameVecWidthArgument: {
|
2014-12-04 09:40:44 +00:00
|
|
|
Type *EltTy = DecodeFixedType(Infos, Tys, Context);
|
|
|
|
Type *Ty = Tys[D.getArgumentNumber()];
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
|
|
|
|
return VectorType::get(EltTy, VTy->getNumElements());
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled");
|
2014-12-25 07:49:20 +00:00
|
|
|
}
|
|
|
|
case IITDescriptor::PtrToArgument: {
|
|
|
|
Type *Ty = Tys[D.getArgumentNumber()];
|
|
|
|
return PointerType::getUnqual(Ty);
|
|
|
|
}
|
Masked Gather and Scatter Intrinsics.
Gather and Scatter are new introduced intrinsics, comming after recently implemented masked load and store.
This is the first patch for Gather and Scatter intrinsics. It includes only the syntax, parsing and verification.
Gather and Scatter intrinsics allow to perform multiple memory accesses (read/write) in one vector instruction.
The intrinsics are not target specific and will have the following syntax:
Gather:
declare <16 x i32> @llvm.masked.gather.v16i32(<16 x i32*> <vector of ptrs>, i32 <alignment>, <16 x i1> <mask>, <16 x i32> <passthru>)
declare <8 x float> @llvm.masked.gather.v8f32(<8 x float*><vector of ptrs>, i32 <alignment>, <8 x i1> <mask>, <8 x float><passthru>)
Scatter:
declare void @llvm.masked.scatter.v8i32(<8 x i32><vector value to be stored> , <8 x i32*><vector of ptrs> , i32 <alignment>, <8 x i1> <mask>)
declare void @llvm.masked.scatter.v16i32(<16 x i32> <vector value to be stored> , <16 x i32*> <vector of ptrs>, i32 <alignment>, <16 x i1><mask> )
Vector of ptrs - a set of source/destination addresses, to load/store the value.
Mask - switches on/off vector lanes to prevent memory access for switched-off lanes
vector of ptrs, value and mask should have the same vector width.
These are code examples where gather / scatter should be used and will allow function vectorization
;void foo1(int * restrict A, int * restrict B, int * restrict C) {
; for (int i=0; i<SIZE; i++) {
; A[i] = B[C[i]];
; }
;}
;void foo3(int * restrict A, int * restrict B) {
; for (int i=0; i<SIZE; i++) {
; A[B[i]] = i+5;
; }
;}
Tests will come in the following patches, with CodeGen and Vectorizer.
http://reviews.llvm.org/D7433
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228521 91177308-0d34-0410-b5e6-96231b3b80d8
2015-02-08 08:27:19 +00:00
|
|
|
case IITDescriptor::VecOfPtrsToElt: {
|
|
|
|
Type *Ty = Tys[D.getArgumentNumber()];
|
|
|
|
VectorType *VTy = dyn_cast<VectorType>(Ty);
|
|
|
|
if (!VTy)
|
|
|
|
llvm_unreachable("Expected an argument of Vector Type");
|
|
|
|
Type *EltTy = VTy->getVectorElementType();
|
|
|
|
return VectorType::get(PointerType::getUnqual(EltTy),
|
|
|
|
VTy->getNumElements());
|
|
|
|
}
|
2014-12-04 09:40:44 +00:00
|
|
|
}
|
2012-05-27 18:28:35 +00:00
|
|
|
llvm_unreachable("unhandled");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FunctionType *Intrinsic::getType(LLVMContext &Context,
|
|
|
|
ID id, ArrayRef<Type*> Tys) {
|
|
|
|
SmallVector<IITDescriptor, 8> Table;
|
|
|
|
getIntrinsicInfoTableEntries(id, Table);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-27 18:28:35 +00:00
|
|
|
ArrayRef<IITDescriptor> TableRef = Table;
|
|
|
|
Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
|
2012-12-17 20:37:55 +00:00
|
|
|
|
2012-05-17 15:55:41 +00:00
|
|
|
SmallVector<Type*, 8> ArgTys;
|
2012-05-27 18:28:35 +00:00
|
|
|
while (!TableRef.empty())
|
|
|
|
ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
|
2007-02-07 20:38:26 +00:00
|
|
|
|
2014-10-20 15:47:24 +00:00
|
|
|
// DecodeFixedType returns Void for IITDescriptor::Void and IITDescriptor::VarArg
|
|
|
|
// If we see void type as the type of the last argument, it is vararg intrinsic
|
|
|
|
if (!ArgTys.empty() && ArgTys.back()->isVoidTy()) {
|
|
|
|
ArgTys.pop_back();
|
|
|
|
return FunctionType::get(ResultTy, ArgTys, true);
|
|
|
|
}
|
2012-12-17 20:37:55 +00:00
|
|
|
return FunctionType::get(ResultTy, ArgTys, false);
|
2007-02-07 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 23:17:49 +00:00
|
|
|
bool Intrinsic::isOverloaded(ID id) {
|
|
|
|
#define GET_INTRINSIC_OVERLOAD_TABLE
|
2013-01-02 12:09:16 +00:00
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
2009-02-24 23:17:49 +00:00
|
|
|
#undef GET_INTRINSIC_OVERLOAD_TABLE
|
|
|
|
}
|
|
|
|
|
2015-06-18 19:28:26 +00:00
|
|
|
bool Intrinsic::isLeaf(ID id) {
|
|
|
|
switch (id) {
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Intrinsic::experimental_gc_statepoint:
|
|
|
|
case Intrinsic::experimental_patchpoint_void:
|
|
|
|
case Intrinsic::experimental_patchpoint_i64:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-12 01:18:58 +00:00
|
|
|
/// This defines the "Intrinsic::getAttributes(ID id)" method.
|
2007-12-03 20:06:50 +00:00
|
|
|
#define GET_INTRINSIC_ATTRIBUTES
|
2013-01-02 12:09:16 +00:00
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
2007-12-03 20:06:50 +00:00
|
|
|
#undef GET_INTRINSIC_ATTRIBUTES
|
|
|
|
|
2011-07-14 17:45:39 +00:00
|
|
|
Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
|
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.
|
2008-04-07 13:39:11 +00:00
|
|
|
return
|
2011-07-14 17:45:39 +00:00
|
|
|
cast<Function>(M->getOrInsertFunction(getName(id, Tys),
|
|
|
|
getType(M->getContext(), id, Tys)));
|
2007-02-07 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 01:49:45 +00:00
|
|
|
// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
|
|
|
|
#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
|
2013-01-02 12:09:16 +00:00
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
2009-02-05 01:49:45 +00:00
|
|
|
#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
|
|
|
|
|
2014-07-04 18:42:25 +00:00
|
|
|
// This defines the "Intrinsic::getIntrinsicForMSBuiltin()" method.
|
|
|
|
#define GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
|
|
|
|
#include "llvm/IR/Intrinsics.gen"
|
|
|
|
#undef GET_LLVM_INTRINSIC_FOR_MS_BUILTIN
|
|
|
|
|
2016-06-22 14:56:33 +00:00
|
|
|
bool Intrinsic::matchIntrinsicType(Type *Ty, ArrayRef<Intrinsic::IITDescriptor> &Infos,
|
|
|
|
SmallVectorImpl<Type*> &ArgTys) {
|
|
|
|
using namespace Intrinsic;
|
|
|
|
|
|
|
|
// If we ran out of descriptors, there are too many arguments.
|
|
|
|
if (Infos.empty()) return true;
|
|
|
|
IITDescriptor D = Infos.front();
|
|
|
|
Infos = Infos.slice(1);
|
|
|
|
|
|
|
|
switch (D.Kind) {
|
|
|
|
case IITDescriptor::Void: return !Ty->isVoidTy();
|
|
|
|
case IITDescriptor::VarArg: return true;
|
|
|
|
case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
|
|
|
|
case IITDescriptor::Token: return !Ty->isTokenTy();
|
|
|
|
case IITDescriptor::Metadata: return !Ty->isMetadataTy();
|
|
|
|
case IITDescriptor::Half: return !Ty->isHalfTy();
|
|
|
|
case IITDescriptor::Float: return !Ty->isFloatTy();
|
|
|
|
case IITDescriptor::Double: return !Ty->isDoubleTy();
|
|
|
|
case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
|
|
|
|
case IITDescriptor::Vector: {
|
|
|
|
VectorType *VT = dyn_cast<VectorType>(Ty);
|
|
|
|
return !VT || VT->getNumElements() != D.Vector_Width ||
|
|
|
|
matchIntrinsicType(VT->getElementType(), Infos, ArgTys);
|
|
|
|
}
|
|
|
|
case IITDescriptor::Pointer: {
|
|
|
|
PointerType *PT = dyn_cast<PointerType>(Ty);
|
|
|
|
return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
|
|
|
|
matchIntrinsicType(PT->getElementType(), Infos, ArgTys);
|
|
|
|
}
|
|
|
|
|
|
|
|
case IITDescriptor::Struct: {
|
|
|
|
StructType *ST = dyn_cast<StructType>(Ty);
|
|
|
|
if (!ST || ST->getNumElements() != D.Struct_NumElements)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
|
|
|
|
if (matchIntrinsicType(ST->getElementType(i), Infos, ArgTys))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IITDescriptor::Argument:
|
|
|
|
// Two cases here - If this is the second occurrence of an argument, verify
|
|
|
|
// that the later instance matches the previous instance.
|
|
|
|
if (D.getArgumentNumber() < ArgTys.size())
|
|
|
|
return Ty != ArgTys[D.getArgumentNumber()];
|
|
|
|
|
|
|
|
// Otherwise, if this is the first instance of an argument, record it and
|
|
|
|
// verify the "Any" kind.
|
|
|
|
assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
|
|
|
|
ArgTys.push_back(Ty);
|
|
|
|
|
|
|
|
switch (D.getArgumentKind()) {
|
|
|
|
case IITDescriptor::AK_Any: return false; // Success
|
|
|
|
case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
|
|
|
|
case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
|
|
|
|
case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
|
|
|
|
case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
|
|
|
|
}
|
|
|
|
llvm_unreachable("all argument kinds not covered");
|
|
|
|
|
|
|
|
case IITDescriptor::ExtendArgument: {
|
|
|
|
// This may only be used when referring to a previous vector argument.
|
|
|
|
if (D.getArgumentNumber() >= ArgTys.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Type *NewTy = ArgTys[D.getArgumentNumber()];
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
|
|
|
|
NewTy = VectorType::getExtendedElementVectorType(VTy);
|
|
|
|
else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
|
|
|
|
NewTy = IntegerType::get(ITy->getContext(), 2 * ITy->getBitWidth());
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Ty != NewTy;
|
|
|
|
}
|
|
|
|
case IITDescriptor::TruncArgument: {
|
|
|
|
// This may only be used when referring to a previous vector argument.
|
|
|
|
if (D.getArgumentNumber() >= ArgTys.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Type *NewTy = ArgTys[D.getArgumentNumber()];
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(NewTy))
|
|
|
|
NewTy = VectorType::getTruncatedElementVectorType(VTy);
|
|
|
|
else if (IntegerType *ITy = dyn_cast<IntegerType>(NewTy))
|
|
|
|
NewTy = IntegerType::get(ITy->getContext(), ITy->getBitWidth() / 2);
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Ty != NewTy;
|
|
|
|
}
|
|
|
|
case IITDescriptor::HalfVecArgument:
|
|
|
|
// This may only be used when referring to a previous vector argument.
|
|
|
|
return D.getArgumentNumber() >= ArgTys.size() ||
|
|
|
|
!isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
|
|
|
|
VectorType::getHalfElementsVectorType(
|
|
|
|
cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
|
|
|
|
case IITDescriptor::SameVecWidthArgument: {
|
|
|
|
if (D.getArgumentNumber() >= ArgTys.size())
|
|
|
|
return true;
|
|
|
|
VectorType * ReferenceType =
|
|
|
|
dyn_cast<VectorType>(ArgTys[D.getArgumentNumber()]);
|
|
|
|
VectorType *ThisArgType = dyn_cast<VectorType>(Ty);
|
|
|
|
if (!ThisArgType || !ReferenceType ||
|
|
|
|
(ReferenceType->getVectorNumElements() !=
|
|
|
|
ThisArgType->getVectorNumElements()))
|
|
|
|
return true;
|
|
|
|
return matchIntrinsicType(ThisArgType->getVectorElementType(),
|
|
|
|
Infos, ArgTys);
|
|
|
|
}
|
|
|
|
case IITDescriptor::PtrToArgument: {
|
|
|
|
if (D.getArgumentNumber() >= ArgTys.size())
|
|
|
|
return true;
|
|
|
|
Type * ReferenceType = ArgTys[D.getArgumentNumber()];
|
|
|
|
PointerType *ThisArgType = dyn_cast<PointerType>(Ty);
|
|
|
|
return (!ThisArgType || ThisArgType->getElementType() != ReferenceType);
|
|
|
|
}
|
|
|
|
case IITDescriptor::VecOfPtrsToElt: {
|
|
|
|
if (D.getArgumentNumber() >= ArgTys.size())
|
|
|
|
return true;
|
|
|
|
VectorType * ReferenceType =
|
|
|
|
dyn_cast<VectorType> (ArgTys[D.getArgumentNumber()]);
|
|
|
|
VectorType *ThisArgVecTy = dyn_cast<VectorType>(Ty);
|
|
|
|
if (!ThisArgVecTy || !ReferenceType ||
|
|
|
|
(ReferenceType->getVectorNumElements() !=
|
|
|
|
ThisArgVecTy->getVectorNumElements()))
|
|
|
|
return true;
|
|
|
|
PointerType *ThisArgEltTy =
|
|
|
|
dyn_cast<PointerType>(ThisArgVecTy->getVectorElementType());
|
|
|
|
if (!ThisArgEltTy)
|
|
|
|
return true;
|
|
|
|
return ThisArgEltTy->getElementType() !=
|
|
|
|
ReferenceType->getVectorElementType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled");
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:47:27 +00:00
|
|
|
bool
|
|
|
|
Intrinsic::matchIntrinsicVarArg(bool isVarArg,
|
|
|
|
ArrayRef<Intrinsic::IITDescriptor> &Infos) {
|
|
|
|
// If there are no descriptors left, then it can't be a vararg.
|
|
|
|
if (Infos.empty())
|
|
|
|
return isVarArg;
|
|
|
|
|
|
|
|
// There should be only one descriptor remaining at this point.
|
|
|
|
if (Infos.size() != 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check and verify the descriptor.
|
|
|
|
IITDescriptor D = Infos.front();
|
|
|
|
Infos = Infos.slice(1);
|
|
|
|
if (D.Kind == IITDescriptor::VarArg)
|
|
|
|
return !isVarArg;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-24 15:10:29 +00:00
|
|
|
Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
|
|
|
|
Intrinsic::ID ID = F->getIntrinsicID();
|
|
|
|
if (!ID)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
FunctionType *FTy = F->getFunctionType();
|
|
|
|
// Accumulate an array of overloaded types for the given intrinsic
|
|
|
|
SmallVector<Type *, 4> ArgTys;
|
|
|
|
{
|
|
|
|
SmallVector<Intrinsic::IITDescriptor, 8> Table;
|
|
|
|
getIntrinsicInfoTableEntries(ID, Table);
|
|
|
|
ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
|
|
|
|
|
|
|
|
// If we encounter any problems matching the signature with the descriptor
|
|
|
|
// just give up remangling. It's up to verifier to report the discrepancy.
|
|
|
|
if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
|
|
|
|
return None;
|
|
|
|
for (auto Ty : FTy->params())
|
|
|
|
if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
|
|
|
|
return None;
|
|
|
|
if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Name = F->getName();
|
|
|
|
if (Name == Intrinsic::getName(ID, ArgTys))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
|
|
|
|
NewDecl->setCallingConv(F->getCallingConv());
|
|
|
|
assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
|
|
|
|
return NewDecl;
|
|
|
|
}
|
|
|
|
|
2010-03-23 14:40:20 +00:00
|
|
|
/// hasAddressTaken - returns true if there are any uses of this function
|
|
|
|
/// other than direct calls or invokes to it.
|
2010-03-24 13:21:49 +00:00
|
|
|
bool Function::hasAddressTaken(const User* *PutOffender) const {
|
2014-03-09 03:16:01 +00:00
|
|
|
for (const Use &U : uses()) {
|
|
|
|
const User *FU = U.getUser();
|
|
|
|
if (isa<BlockAddress>(FU))
|
2012-05-12 08:30:16 +00:00
|
|
|
continue;
|
2016-02-18 22:09:30 +00:00
|
|
|
if (!isa<CallInst>(FU) && !isa<InvokeInst>(FU)) {
|
|
|
|
if (PutOffender)
|
|
|
|
*PutOffender = FU;
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-09 03:16:01 +00:00
|
|
|
ImmutableCallSite CS(cast<Instruction>(FU));
|
2016-02-18 22:09:30 +00:00
|
|
|
if (!CS.isCallee(&U)) {
|
|
|
|
if (PutOffender)
|
|
|
|
*PutOffender = FU;
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-10 08:41:11 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-20 05:23:42 +00:00
|
|
|
bool Function::isDefTriviallyDead() const {
|
|
|
|
// Check the linkage
|
|
|
|
if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
|
|
|
|
!hasAvailableExternallyLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the function is used by anything other than a blockaddress.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (const User *U : users())
|
|
|
|
if (!isa<BlockAddress>(U))
|
2011-10-20 05:23:42 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:43:40 +00:00
|
|
|
/// callsFunctionThatReturnsTwice - Return true if the function has a call to
|
|
|
|
/// setjmp or other function that gcc recognizes as "returning twice".
|
|
|
|
bool Function::callsFunctionThatReturnsTwice() const {
|
|
|
|
for (const_inst_iterator
|
|
|
|
I = inst_begin(this), E = inst_end(this); I != E; ++I) {
|
2014-01-14 04:20:01 +00:00
|
|
|
ImmutableCallSite CS(&*I);
|
|
|
|
if (CS && CS.hasFnAttr(Attribute::ReturnsTwice))
|
2011-10-17 18:43:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-16 01:08:15 +00:00
|
|
|
|
2015-12-19 08:52:49 +00:00
|
|
|
Constant *Function::getPersonalityFn() const {
|
|
|
|
assert(hasPersonalityFn() && getNumOperands());
|
|
|
|
return cast<Constant>(Op<0>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Function::setPersonalityFn(Constant *Fn) {
|
2015-12-23 18:27:23 +00:00
|
|
|
setHungoffOperand<0>(Fn);
|
2015-12-19 08:52:49 +00:00
|
|
|
setValueSubclassDataBit(3, Fn != nullptr);
|
2015-10-06 20:31:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 01:08:15 +00:00
|
|
|
Constant *Function::getPrefixData() const {
|
2015-12-19 08:52:49 +00:00
|
|
|
assert(hasPrefixData() && getNumOperands());
|
|
|
|
return cast<Constant>(Op<1>());
|
2013-09-16 01:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Function::setPrefixData(Constant *PrefixData) {
|
2015-12-23 18:27:23 +00:00
|
|
|
setHungoffOperand<1>(PrefixData);
|
2015-12-19 08:52:49 +00:00
|
|
|
setValueSubclassDataBit(1, PrefixData != nullptr);
|
2013-09-16 01:08:15 +00:00
|
|
|
}
|
2014-12-03 02:08:38 +00:00
|
|
|
|
|
|
|
Constant *Function::getPrologueData() const {
|
2015-12-19 08:52:49 +00:00
|
|
|
assert(hasPrologueData() && getNumOperands());
|
|
|
|
return cast<Constant>(Op<2>());
|
2014-12-03 02:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Function::setPrologueData(Constant *PrologueData) {
|
2015-12-23 18:27:23 +00:00
|
|
|
setHungoffOperand<2>(PrologueData);
|
2015-12-19 08:52:49 +00:00
|
|
|
setValueSubclassDataBit(2, PrologueData != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Function::allocHungoffUselist() {
|
|
|
|
// If we've already allocated a uselist, stop here.
|
|
|
|
if (getNumOperands())
|
2014-12-03 02:08:38 +00:00
|
|
|
return;
|
|
|
|
|
2015-12-19 08:52:49 +00:00
|
|
|
allocHungoffUses(3, /*IsPhi=*/ false);
|
|
|
|
setNumHungOffUseOperands(3);
|
|
|
|
|
|
|
|
// Initialize the uselist with placeholder operands to allow traversal.
|
|
|
|
auto *CPN = ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0));
|
|
|
|
Op<0>().set(CPN);
|
|
|
|
Op<1>().set(CPN);
|
|
|
|
Op<2>().set(CPN);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int Idx>
|
|
|
|
void Function::setHungoffOperand(Constant *C) {
|
2015-12-23 18:27:23 +00:00
|
|
|
if (C) {
|
|
|
|
allocHungoffUselist();
|
|
|
|
Op<Idx>().set(C);
|
|
|
|
} else if (getNumOperands()) {
|
|
|
|
Op<Idx>().set(
|
|
|
|
ConstantPointerNull::get(Type::getInt1PtrTy(getContext(), 0)));
|
|
|
|
}
|
2015-12-19 08:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Function::setValueSubclassDataBit(unsigned Bit, bool On) {
|
|
|
|
assert(Bit < 16 && "SubclassData contains only 16 bits");
|
|
|
|
if (On)
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() | (1 << Bit));
|
|
|
|
else
|
|
|
|
setValueSubclassData(getSubclassDataFromValue() & ~(1 << Bit));
|
2014-12-03 02:08:38 +00:00
|
|
|
}
|
2015-05-06 23:54:14 +00:00
|
|
|
|
2015-05-13 15:13:45 +00:00
|
|
|
void Function::setEntryCount(uint64_t Count) {
|
|
|
|
MDBuilder MDB(getContext());
|
|
|
|
setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count));
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<uint64_t> Function::getEntryCount() const {
|
|
|
|
MDNode *MD = getMetadata(LLVMContext::MD_prof);
|
|
|
|
if (MD && MD->getOperand(0))
|
|
|
|
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
|
|
|
|
if (MDS->getString().equals("function_entry_count")) {
|
|
|
|
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
|
2016-08-05 18:38:19 +00:00
|
|
|
uint64_t Count = CI->getValue().getZExtValue();
|
|
|
|
if (Count == 0)
|
|
|
|
return None;
|
|
|
|
return Count;
|
2015-05-13 15:13:45 +00:00
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|