1cf3056db0fee1db7921214b1f25cea04e959e105Chris Lattner//===-- Function.cpp - Implement the Global object classes ----------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
10c2c50cdcdc19a1bca993c06d13d8cdca87083ce4Chandler Carruth// This file implements the Function class for the IR library.
11009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
12009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===//
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
140b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
154c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman#include "LLVMContextImpl.h"
16d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "SymbolTableListTraitsImpl.h"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/DenseMap.h"
18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/STLExtras.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/StringExtras.h"
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/ValueTypes.h"
210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h"
230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
25c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif#include "llvm/Support/CallSite.h"
2611f1a8335e207971f66eb60609e16b8719be5450Rafael Espindola#include "llvm/Support/InstIterator.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/LeakDetector.h"
28b983d67465a939c43173e381f9063d9d9245f45bOwen Anderson#include "llvm/Support/ManagedStatic.h"
291f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/RWMutex.h"
30d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/StringPool.h"
311f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Threading.h"
3231f8499e83dc4dccbb57ea7e76d1fd49b7010d0cChris Lattnerusing namespace llvm;
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
347e70829632f82de15db187845666aaca6e04b792Chris Lattner// Explicit instantiations of SymbolTableListTraits since some of the methods
357e70829632f82de15db187845666aaca6e04b792Chris Lattner// are not in the public header file...
36c63ca0a71b299ee0b8fc7dc8405d7f3c856ecfa3John McCalltemplate class llvm::SymbolTableListTraits<Argument, Function>;
37c63ca0a71b299ee0b8fc7dc8405d7f3c856ecfa3John McCalltemplate class llvm::SymbolTableListTraits<BasicBlock, Function>;
38009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
3970cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner//===----------------------------------------------------------------------===//
4080dd50b4aaee0ae609d21f8dded9c3af1e801b9eChris Lattner// Argument Implementation
4180dd50b4aaee0ae609d21f8dded9c3af1e801b9eChris Lattner//===----------------------------------------------------------------------===//
4280dd50b4aaee0ae609d21f8dded9c3af1e801b9eChris Lattner
432d24e2a396a1d211baaeedf32148a3b657240170David Blaikievoid Argument::anchor() { }
442d24e2a396a1d211baaeedf32148a3b657240170David Blaikie
45db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerArgument::Argument(Type *Ty, const Twine &Name, Function *Par)
46dec628eead87b20773c98a00830580df211acc98Chris Lattner  : Value(Ty, Value::ArgumentVal) {
47bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner  Parent = 0;
48d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner
49d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  // Make sure that we get added to a function
50d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  LeakDetector::addGarbageObject(this);
51d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner
52bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner  if (Par)
53bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner    Par->getArgumentList().push_back(this);
54dec628eead87b20773c98a00830580df211acc98Chris Lattner  setName(Name);
55bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner}
56bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner
57bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattnervoid Argument::setParent(Function *parent) {
58d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  if (getParent())
59d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner    LeakDetector::addGarbageObject(this);
60bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner  Parent = parent;
61d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  if (getParent())
62d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner    LeakDetector::removeGarbageObject(this);
63bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner}
64bded132d00ed626a6541b67ad101ef0fd47d3491Chris Lattner
65de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner/// getArgNo - Return the index of this formal argument in its containing
660f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman/// function.  For example in "void foo(int a, float b)" a is 0 and b is 1.
67de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattnerunsigned Argument::getArgNo() const {
68de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  const Function *F = getParent();
69de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  assert(F && "Argument is not in a function");
700f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
71de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  Function::const_arg_iterator AI = F->arg_begin();
72de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  unsigned ArgIdx = 0;
73de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  for (; &*AI != this; ++AI)
74de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner    ++ArgIdx;
75de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner
76de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner  return ArgIdx;
77de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner}
78de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner
79de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner/// hasByValAttr - Return true if this argument has the byval attribute on it
80de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner/// in its containing function.
81de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattnerbool Argument::hasByValAttr() const {
821df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!getType()->isPointerTy()) return false;
8339cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling  return getParent()->getAttributes().
8439cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling    hasAttribute(getArgNo()+1, Attribute::ByVal);
85de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner}
86de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner
87ae441cc33cee83a598f74e3d0447fe02157b0389Chris Lattnerunsigned Argument::getParamAlignment() const {
88ae441cc33cee83a598f74e3d0447fe02157b0389Chris Lattner  assert(getType()->isPointerTy() && "Only pointers have alignments");
89ae441cc33cee83a598f74e3d0447fe02157b0389Chris Lattner  return getParent()->getParamAlignment(getArgNo()+1);
900f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
91ae441cc33cee83a598f74e3d0447fe02157b0389Chris Lattner}
92ae441cc33cee83a598f74e3d0447fe02157b0389Chris Lattner
93be1ce0a867a7e62094fc9a158c15bb9e48cb80faDuncan Sands/// hasNestAttr - Return true if this argument has the nest attribute on
94be1ce0a867a7e62094fc9a158c15bb9e48cb80faDuncan Sands/// it in its containing function.
95be1ce0a867a7e62094fc9a158c15bb9e48cb80faDuncan Sandsbool Argument::hasNestAttr() const {
961df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!getType()->isPointerTy()) return false;
9739cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling  return getParent()->getAttributes().
9839cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling    hasAttribute(getArgNo()+1, Attribute::Nest);
99be1ce0a867a7e62094fc9a158c15bb9e48cb80faDuncan Sands}
100be1ce0a867a7e62094fc9a158c15bb9e48cb80faDuncan Sands
101de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
102de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner/// it in its containing function.
103de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattnerbool Argument::hasNoAliasAttr() const {
1041df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!getType()->isPointerTy()) return false;
10539cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling  return getParent()->getAttributes().
10639cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling    hasAttribute(getArgNo()+1, Attribute::NoAlias);
107de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner}
108de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner
10917da06ffbd1f7269b6b9037f883a3d8a5c985f62Duncan Sands/// hasNoCaptureAttr - Return true if this argument has the nocapture attribute
11017da06ffbd1f7269b6b9037f883a3d8a5c985f62Duncan Sands/// on it in its containing function.
11117da06ffbd1f7269b6b9037f883a3d8a5c985f62Duncan Sandsbool Argument::hasNoCaptureAttr() const {
1121df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!getType()->isPointerTy()) return false;
11339cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling  return getParent()->getAttributes().
11439cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling    hasAttribute(getArgNo()+1, Attribute::NoCapture);
11517da06ffbd1f7269b6b9037f883a3d8a5c985f62Duncan Sands}
11617da06ffbd1f7269b6b9037f883a3d8a5c985f62Duncan Sands
1177d54254c9cc6e5a3be3a7d9d22241c0ae8a7fa03Owen Anderson/// hasSRetAttr - Return true if this argument has the sret attribute on
1187d54254c9cc6e5a3be3a7d9d22241c0ae8a7fa03Owen Anderson/// it in its containing function.
1197d54254c9cc6e5a3be3a7d9d22241c0ae8a7fa03Owen Andersonbool Argument::hasStructRetAttr() const {
1201df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!getType()->isPointerTy()) return false;
121b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif  if (this != getParent()->arg_begin())
122b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif    return false; // StructRet param must be first param
12339cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling  return getParent()->getAttributes().
12439cd0c8e477255a7296b2bd1dc67193f8d38c003Bill Wendling    hasAttribute(1, Attribute::StructRet);
1257d54254c9cc6e5a3be3a7d9d22241c0ae8a7fa03Owen Anderson}
1267d54254c9cc6e5a3be3a7d9d22241c0ae8a7fa03Owen Anderson
12728d65722d6f283b327b5815914382077fe9c0ab4Bill Wendling/// addAttr - Add attributes to an argument.
12828d65722d6f283b327b5815914382077fe9c0ab4Bill Wendlingvoid Argument::addAttr(AttributeSet AS) {
129fece442c697eb29f14b4718bdafa3a97d545b476Bill Wendling  assert(AS.getNumSlots() <= 1 &&
130b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling         "Trying to add more than one attribute set to an argument!");
131b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling  AttrBuilder B(AS, AS.getSlotIndex(0));
132b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling  getParent()->addAttributes(getArgNo() + 1,
133b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling                             AttributeSet::get(Parent->getContext(),
134b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling                                               getArgNo() + 1, B));
135e2435da8abe5ca62c7f08f29c242b6b98e0ec7afGordon Henriksen}
136cfa3c236b23aff80e48df11ef5833b7e63fab802Duncan Sands
13728d65722d6f283b327b5815914382077fe9c0ab4Bill Wendling/// removeAttr - Remove attributes from an argument.
13828d65722d6f283b327b5815914382077fe9c0ab4Bill Wendlingvoid Argument::removeAttr(AttributeSet AS) {
139fece442c697eb29f14b4718bdafa3a97d545b476Bill Wendling  assert(AS.getNumSlots() <= 1 &&
140b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling         "Trying to remove more than one attribute set from an argument!");
141b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling  AttrBuilder B(AS, AS.getSlotIndex(0));
142b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling  getParent()->removeAttributes(getArgNo() + 1,
143b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling                                AttributeSet::get(Parent->getContext(),
144b7a1dda9c91b3d1821f4235c35a0d62c62d18848Bill Wendling                                                  getArgNo() + 1, B));
145e2435da8abe5ca62c7f08f29c242b6b98e0ec7afGordon Henriksen}
146de6fa5f25f572b79b3dd7bbbe4dba78a54a4b2f8Chris Lattner
14780dd50b4aaee0ae609d21f8dded9c3af1e801b9eChris Lattner//===----------------------------------------------------------------------===//
14850ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner// Helper Methods in Function
1494746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer//===----------------------------------------------------------------------===//
1504746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer
151e922c0201916e0b980ab3cfe91e1413e68d55647Owen AndersonLLVMContext &Function::getContext() const {
152e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson  return getType()->getContext();
15362fabf5faba70c507c8fbe298260e9b3749fdd79Owen Anderson}
15462fabf5faba70c507c8fbe298260e9b3749fdd79Owen Anderson
1551afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris LattnerFunctionType *Function::getFunctionType() const {
15650ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner  return cast<FunctionType>(getType()->getElementType());
1574746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer}
1584746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer
15950ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattnerbool Function::isVarArg() const {
16050ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner  return getFunctionType()->isVarArg();
1614746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer}
1624746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer
1631afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris LattnerType *Function::getReturnType() const {
16450ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner  return getFunctionType()->getReturnType();
165827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands}
166827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands
16750ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattnervoid Function::removeFromParent() {
16850ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner  getParent()->getFunctionList().remove(this);
169827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands}
170827cde1c8319e51463007078a7ce3660ebc93036Duncan Sands
17150ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattnervoid Function::eraseFromParent() {
17250ee9ddc8f0633af6cb0a5693a2c706e98f944daChris Lattner  getParent()->getFunctionList().erase(this);
1734746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer}
1744746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer
1754746ecf16eeb5ff920672fdff1c0dd85594437edReid Spencer//===----------------------------------------------------------------------===//
17679df7c0aaa18129e55968c8783ef8346807bd4afChris Lattner// Function Implementation
17770cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner//===----------------------------------------------------------------------===//
17870cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner
179db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris LattnerFunction::Function(FunctionType *Ty, LinkageTypes Linkage,
1806e0d1cb30957a636c53158d3089e6fb88348a57aDaniel Dunbar                   const Twine &name, Module *ParentModule)
1810f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman  : GlobalValue(PointerType::getUnqual(Ty),
18258d74910c6b82e622ecbb57d6644d48fec5a5c0fChris Lattner                Value::FunctionVal, 0, 0, Linkage, name) {
183c8e222b9a48a84e98a5fc4ccd6b015197b09bed4Chris Lattner  assert(FunctionType::isValidReturnType(getReturnType()) &&
1841afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner         "invalid return type");
185ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer  SymTab = new ValueSymbolTable();
186482701563a2ff76815d2cd19856469319e7c2d88Chris Lattner
1870162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  // If the function has arguments, mark them as lazily built.
1880162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  if (Ty->getNumParams())
189cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner    setValueSubclassData(1);   // Set the "has lazy arguments" bit.
1900f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
191d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  // Make sure that we get added to a function
192d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  LeakDetector::addGarbageObject(this);
193d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner
194482701563a2ff76815d2cd19856469319e7c2d88Chris Lattner  if (ParentModule)
195482701563a2ff76815d2cd19856469319e7c2d88Chris Lattner    ParentModule->getFunctionList().push_back(this);
19679ab3e801480e6412c064a3fb15a79a8cab13efcDuncan Sands
19779ab3e801480e6412c064a3fb15a79a8cab13efcDuncan Sands  // Ensure intrinsics have the right parameter attributes.
19849de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen  if (unsigned IID = getIntrinsicID())
199cb3de0bc800d7920087b19bb12a545d4cc84114eBill Wendling    setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID)));
20081b2ab8a249c3652133a1dcc0209343cde9b499fDevang Patel
201009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
202009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
203afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon HenriksenFunction::~Function() {
204afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen  dropAllReferences();    // After this it is safe to delete instructions.
205009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
206009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  // Delete all of the method arguments and unlink from symbol table...
207afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen  ArgumentList.clear();
208afba8fe662d65b25b4baf46bb26cc18e1f9cc0a5Gordon Henriksen  delete SymTab;
209b90909e40536db17665727f5ca1c618e485464c3Reid Spencer
2105eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  // Remove the function from the on-the-side GC table.
2115eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  clearGC();
2124c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman
2134c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  // Remove the intrinsicID from the Cache.
2144c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  if(getValueName() && isIntrinsic())
2154c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman    getContext().pImpl->IntrinsicIDCache.erase(this);
216009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
217009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
2180162c1815e5a1b33750d313c49f707bc446ea946Chris Lattnervoid Function::BuildLazyArguments() const {
2190162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  // Create the arguments vector, all arguments start out unnamed.
220db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  FunctionType *FT = getFunctionType();
2210162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
222f012705c7e4ca8cf90b6b734ce1d5355daca5ba5Benjamin Kramer    assert(!FT->getParamType(i)->isVoidTy() &&
2230162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner           "Cannot have void typed arguments!");
2240162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner    ArgumentList.push_back(new Argument(FT->getParamType(i)));
2250162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  }
2260f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
2270162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  // Clear the lazy arguments bit.
228cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  unsigned SDC = getSubclassDataFromValue();
229cafe9bba32aeed16e8e3db28b4cd4ff13160438fChris Lattner  const_cast<Function*>(this)->setValueSubclassData(SDC &= ~1);
2300162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner}
2310162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner
2320162c1815e5a1b33750d313c49f707bc446ea946Chris Lattnersize_t Function::arg_size() const {
2330162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  return getFunctionType()->getNumParams();
2340162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner}
2350162c1815e5a1b33750d313c49f707bc446ea946Chris Lattnerbool Function::arg_empty() const {
2360162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner  return getFunctionType()->getNumParams() == 0;
2370162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner}
2380162c1815e5a1b33750d313c49f707bc446ea946Chris Lattner
239e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattnervoid Function::setParent(Module *parent) {
240d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  if (getParent())
241d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner    LeakDetector::addGarbageObject(this);
242009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner  Parent = parent;
243d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner  if (getParent())
244d1e693f2a3883dacf213aa2b477540c57b53b714Chris Lattner    LeakDetector::removeGarbageObject(this);
245009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
246009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner
247009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// dropAllReferences() - This function causes all the subinstructions to "let
248009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// go" of all references that they are maintaining.  This allows one to
249009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// 'delete' a whole class at a time, even though there may be circular
250009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// references... first all references are dropped, and all use counts go to
2516b63452c3ad26678b32f93dbca55902a313ee4e9Misha Brukman// zero.  Then everything is deleted for real.  Note that no operations are
252fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// valid on an object that has "dropped all references", except operator
253009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// delete.
254009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//
255e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattnervoid Function::dropAllReferences() {
2567e70829632f82de15db187845666aaca6e04b792Chris Lattner  for (iterator I = begin(), E = end(); I != E; ++I)
2577e70829632f82de15db187845666aaca6e04b792Chris Lattner    I->dropAllReferences();
2580f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
259a6399ae08967109b1427b34b5bde775a38bb4d75Dan Gohman  // Delete all basic blocks. They are now unused, except possibly by
260a6399ae08967109b1427b34b5bde775a38bb4d75Dan Gohman  // blockaddresses, but BasicBlock's destructor takes care of those.
261a6399ae08967109b1427b34b5bde775a38bb4d75Dan Gohman  while (!BasicBlocks.empty())
262a6399ae08967109b1427b34b5bde775a38bb4d75Dan Gohman    BasicBlocks.begin()->eraseFromParent();
263009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner}
26470cc3397f84c2e1fd69c059a0ef89e398e847b00Chris Lattner
26570d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendlingvoid Function::addAttribute(unsigned i, Attribute::AttrKind attr) {
26699faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  AttributeSet PAL = getAttributes();
26770d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling  PAL = PAL.addAttribute(getContext(), i, attr);
26870d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling  setAttributes(PAL);
26970d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling}
27070d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling
27170d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendlingvoid Function::addAttributes(unsigned i, AttributeSet attrs) {
27270d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling  AttributeSet PAL = getAttributes();
27370d2ca0725b05a2d372e4dc3336e8ea350093e98Bill Wendling  PAL = PAL.addAttributes(getContext(), i, attrs);
2740598866c052147c31b808391f58434ce3dbfb838Devang Patel  setAttributes(PAL);
27592432a8db1266f7932bce13a7e6354b0993838d3Duncan Sands}
27692432a8db1266f7932bce13a7e6354b0993838d3Duncan Sands
2778246df61f6de716acf1f8c64fac3c19970a2c174Bill Wendlingvoid Function::removeAttributes(unsigned i, AttributeSet attrs) {
27899faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  AttributeSet PAL = getAttributes();
2798246df61f6de716acf1f8c64fac3c19970a2c174Bill Wendling  PAL = PAL.removeAttributes(getContext(), i, attrs);
2800598866c052147c31b808391f58434ce3dbfb838Devang Patel  setAttributes(PAL);
2810bf7b414ae0bb6699cadc3a210d18cfec44e9354Eric Christopher}
2820bf7b414ae0bb6699cadc3a210d18cfec44e9354Eric Christopher
2835eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen// Maintain the GC name for each function in an on-the-side table. This saves
2845eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen// allocating an additional word in Function for programs which do not use GC
2855eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen// (i.e., most programs) at the cost of increased overhead for clients which do
2865eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen// use GC.
287bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Andersonstatic DenseMap<const Function*,PooledStringPtr> *GCNames;
288bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Andersonstatic StringPool *GCNamePool;
289b2c0fe4d93ba75b8343de60984b0ce3548cd22c9Owen Andersonstatic ManagedStatic<sys::SmartRWMutex<true> > GCLock;
29080a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen
2915eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksenbool Function::hasGC() const {
292a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedReader<true> Reader(*GCLock);
293b2c0fe4d93ba75b8343de60984b0ce3548cd22c9Owen Anderson  return GCNames && GCNames->count(this);
29480a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen}
29580a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen
2965eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksenconst char *Function::getGC() const {
2975eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  assert(hasGC() && "Function has no collector");
298a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedReader<true> Reader(*GCLock);
299b2c0fe4d93ba75b8343de60984b0ce3548cd22c9Owen Anderson  return *(*GCNames)[this];
30080a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen}
30180a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen
3025eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksenvoid Function::setGC(const char *Str) {
303a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedWriter<true> Writer(*GCLock);
304bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson  if (!GCNamePool)
305bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson    GCNamePool = new StringPool();
306bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson  if (!GCNames)
307bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson    GCNames = new DenseMap<const Function*,PooledStringPtr>();
308bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson  (*GCNames)[this] = GCNamePool->intern(Str);
30980a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen}
31080a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen
3115eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksenvoid Function::clearGC() {
312a9d1f2c559ef4b2549e29288fe6944e68913ba0fOwen Anderson  sys::SmartScopedWriter<true> Writer(*GCLock);
313bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson  if (GCNames) {
3145eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    GCNames->erase(this);
315bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson    if (GCNames->empty()) {
316bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson      delete GCNames;
317bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson      GCNames = 0;
318bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson      if (GCNamePool->empty()) {
319bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson        delete GCNamePool;
320bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson        GCNamePool = 0;
321bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson      }
322bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson    }
323bf5ec1b9db6f1f18b633908416b5798abd08981cOwen Anderson  }
32480a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen}
32580a75bfae980df96f969f1c05b0c4a80ce975240Gordon Henriksen
32628c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands/// copyAttributesFrom - copy all additional attributes (those not needed to
32728c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands/// create a Function) from the Function Src to this one.
32828c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sandsvoid Function::copyAttributesFrom(const GlobalValue *Src) {
32928c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands  assert(isa<Function>(Src) && "Expected a Function!");
33028c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands  GlobalValue::copyAttributesFrom(Src);
33128c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands  const Function *SrcF = cast<Function>(Src);
33228c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands  setCallingConv(SrcF->getCallingConv());
3330598866c052147c31b808391f58434ce3dbfb838Devang Patel  setAttributes(SrcF->getAttributes());
3345eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  if (SrcF->hasGC())
3355eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    setGC(SrcF->getGC());
3365eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen  else
3375eca075b74d62c621b160aa216b4cd50829a2cc7Gordon Henriksen    clearGC();
33828c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands}
33928c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands
340dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner/// getIntrinsicID - This method returns the ID number of the specified
341d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke/// function, or Intrinsic::not_intrinsic if the function is not an
3426b63452c3ad26678b32f93dbca55902a313ee4e9Misha Brukman/// intrinsic, or if the pointer is null.  This value is always defined to be
343dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner/// zero to allow easy checking for whether a function is intrinsic or not.  The
344dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner/// particular intrinsic functions which correspond to this value are defined in
3454c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman/// llvm/Intrinsics.h.  Results are cached in the LLVM context, subsequent
3464c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman/// requests for the same ID return results much faster from the cache.
347dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner///
34849de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesenunsigned Function::getIntrinsicID() const {
3493b515802f652b16b05c9a8f344d219a0739b36a3Chris Lattner  const ValueName *ValName = this->getValueName();
350f846f16c92590379e900d01812e828e83b759ceeMichael Ilseman  if (!ValName || !isIntrinsic())
351085659f040f8b943c931db8a695dec4bf22578a5Reid Spencer    return 0;
3524c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman
3534c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  LLVMContextImpl::IntrinsicIDCacheTy &IntrinsicIDCache =
3544c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman    getContext().pImpl->IntrinsicIDCache;
3554c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  if(!IntrinsicIDCache.count(this)) {
3564c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman    unsigned Id = lookupIntrinsicID();
3574c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman    IntrinsicIDCache[this]=Id;
3584c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman    return Id;
3594c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  }
3604c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  return IntrinsicIDCache[this];
3614c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman}
3624c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman
3634c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman/// This private method does the actual lookup of an intrinsic ID when the query
3644c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman/// could not be answered from the cache.
3654c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilsemanunsigned Function::lookupIntrinsicID() const {
3664c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman  const ValueName *ValName = this->getValueName();
3673b515802f652b16b05c9a8f344d219a0739b36a3Chris Lattner  unsigned Len = ValName->getKeyLength();
3683b515802f652b16b05c9a8f344d219a0739b36a3Chris Lattner  const char *Name = ValName->getKeyData();
3690f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
3709a016ff284f7301da20e74fb1304fb17e3d1214dChris Lattner#define GET_FUNCTION_RECOGNIZER
371351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
3729a016ff284f7301da20e74fb1304fb17e3d1214dChris Lattner#undef GET_FUNCTION_RECOGNIZER
3734c8e74f0b75cb10820c45c86399fbd02e4a8832aMichael Ilseman
374dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner  return 0;
375dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner}
376dd035d188a80f560ff4193391a9e8df64c993369Chris Lattner
377eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramerstd::string Intrinsic::getName(ID id, ArrayRef<Type*> Tys) {
378b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner  assert(id < num_intrinsics && "Invalid intrinsic ID!");
379607b8ebfb849e3257d5ab64fae3ae62229eadb6fChris Lattner  static const char * const Table[] = {
380b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner    "not_intrinsic",
381b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner#define GET_INTRINSIC_NAME_TABLE
382351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
383b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner#undef GET_INTRINSIC_NAME_TABLE
384b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner  };
385eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer  if (Tys.empty())
3861437a0976567810c2930218b1ba582820150b05cReid Spencer    return Table[id];
3871437a0976567810c2930218b1ba582820150b05cReid Spencer  std::string Result(Table[id]);
388eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer  for (unsigned i = 0; i < Tys.size(); ++i) {
389db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    if (PointerType* PTyp = dyn_cast<PointerType>(Tys[i])) {
3900f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman      Result += ".p" + llvm::utostr(PTyp->getAddressSpace()) +
391e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson                EVT::getEVT(PTyp->getElementType()).getEVTString();
392e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    }
393e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang    else if (Tys[i])
394e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      Result += "." + EVT::getEVT(Tys[i]).getEVTString();
395e3b3a7241c01f26613694e53b26b01abf764ddfcMon P Wang  }
3961437a0976567810c2930218b1ba582820150b05cReid Spencer  return Result;
397b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner}
398b847423fc6653bc1fd0d1dde4f968502985d2c6aChris Lattner
399a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner
400908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner/// IIT_Info - These are enumerators that describe the entries returned by the
401908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner/// getIntrinsicInfoTableEntries function.
402908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner///
403908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner/// NOTE: This must be kept in synch with the copy in TblGen/IntrinsicEmitter!
404908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattnerenum IIT_Info {
405908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  // Common values should be encoded with 0-15.
406908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_Done = 0,
407908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_I1   = 1,
408908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_I8   = 2,
409908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_I16  = 3,
410908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_I32  = 4,
411908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_I64  = 5,
4124d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_F16  = 6,
4134d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_F32  = 7,
4144d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_F64  = 8,
4154d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_V2   = 9,
4164d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_V4   = 10,
4174d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_V8   = 11,
4184d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_V16  = 12,
4194d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_V32  = 13,
420908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_PTR  = 14,
421908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IIT_ARG  = 15,
4220f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
423908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  // Values from 16+ are only encodable with the inefficient encoding.
4244d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_MMX  = 16,
4254d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_METADATA = 17,
4264d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_EMPTYSTRUCT = 18,
4274d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_STRUCT2 = 19,
4284d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_STRUCT3 = 20,
4294d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_STRUCT4 = 21,
4304d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_STRUCT5 = 22,
4314d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_EXTEND_VEC_ARG = 23,
4324d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_TRUNC_VEC_ARG = 24,
4334d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  IIT_ANYPTR = 25
434908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner};
435908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
436908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
437908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattnerstatic void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
438908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                      SmallVectorImpl<Intrinsic::IITDescriptor> &OutputTable) {
439626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner  IIT_Info Info = IIT_Info(Infos[NextElt++]);
440d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  unsigned StructElts = 2;
441908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  using namespace Intrinsic;
4420f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
443626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner  switch (Info) {
444908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_Done:
445908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Void, 0));
446908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
447908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_MMX:
448908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::MMX, 0));
449908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
450908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_METADATA:
451908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Metadata, 0));
452908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
4534d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  case IIT_F16:
4544d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Half, 0));
4554d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman    return;
456908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_F32:
457908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Float, 0));
458908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
459908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_F64:
460908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Double, 0));
461908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
462908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_I1:
463908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 1));
464908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
465908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_I8:
466908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 8));
467908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
468908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_I16:
469908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer,16));
470908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
471908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_I32:
472908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 32));
473908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
474908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_I64:
475908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Integer, 64));
476908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
47746aaf69e37287a9696b6cc5c49d10d0937ff052bChris Lattner  case IIT_V2:
478908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 2));
479908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
480908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
48146aaf69e37287a9696b6cc5c49d10d0937ff052bChris Lattner  case IIT_V4:
482908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 4));
483908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
484908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
48546aaf69e37287a9696b6cc5c49d10d0937ff052bChris Lattner  case IIT_V8:
486908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 8));
487908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
488908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
48946aaf69e37287a9696b6cc5c49d10d0937ff052bChris Lattner  case IIT_V16:
490908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 16));
491908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
492908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
493d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_V32:
494908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Vector, 32));
495908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
496908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
497a48289a672e20587e95bcdd84d5c71d28eb3d2b8Chris Lattner  case IIT_PTR:
498908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer, 0));
499908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
500908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
501a48289a672e20587e95bcdd84d5c71d28eb3d2b8Chris Lattner  case IIT_ANYPTR: {  // [ANYPTR addrspace, subtype]
5020f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Pointer,
503908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                             Infos[NextElt++]));
504908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, Infos, OutputTable);
505908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
506908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  }
507908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_ARG: {
508908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
509908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Argument, ArgInfo));
510908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
511908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  }
512908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_EXTEND_VEC_ARG: {
513908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
514908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::ExtendVecArgument,
515908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                             ArgInfo));
516908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
517b428511989eea383b4fa65678c12b055d5c72af5Pete Cooper  }
518d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_TRUNC_VEC_ARG: {
519908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]);
520908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::TruncVecArgument,
521908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                             ArgInfo));
522908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
523d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  }
524908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IIT_EMPTYSTRUCT:
525908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0));
526908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
527d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_STRUCT5: ++StructElts; // FALL THROUGH.
528d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_STRUCT4: ++StructElts; // FALL THROUGH.
529d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_STRUCT3: ++StructElts; // FALL THROUGH.
530d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner  case IIT_STRUCT2: {
531908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct,StructElts));
532908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
533d7cf5eb0219db7303875941ab62b3ef6e8fb0b31Chris Lattner    for (unsigned i = 0; i != StructElts; ++i)
534908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner      DecodeIITType(NextElt, Infos, OutputTable);
535908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return;
53646aaf69e37287a9696b6cc5c49d10d0937ff052bChris Lattner  }
537a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner  }
538a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner  llvm_unreachable("unhandled");
539a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner}
540a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner
541908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
542908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner#define GET_INTRINSIC_GENERATOR_GLOBAL
543351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
544908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner#undef GET_INTRINSIC_GENERATOR_GLOBAL
545908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
5460f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilsemanvoid Intrinsic::getIntrinsicInfoTableEntries(ID id,
547908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                             SmallVectorImpl<IITDescriptor> &T){
548a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner  // Check to see if the intrinsic's type was expressible by the table.
549a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner  unsigned TableVal = IIT_Table[id-1];
5500f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
551387c9dcddabe8551468647b249d3629953caab9eChris Lattner  // Decode the TableVal into an array of IITValues.
552387c9dcddabe8551468647b249d3629953caab9eChris Lattner  SmallVector<unsigned char, 8> IITValues;
553387c9dcddabe8551468647b249d3629953caab9eChris Lattner  ArrayRef<unsigned char> IITEntries;
554387c9dcddabe8551468647b249d3629953caab9eChris Lattner  unsigned NextElt = 0;
555387c9dcddabe8551468647b249d3629953caab9eChris Lattner  if ((TableVal >> 31) != 0) {
556387c9dcddabe8551468647b249d3629953caab9eChris Lattner    // This is an offset into the IIT_LongEncodingTable.
557387c9dcddabe8551468647b249d3629953caab9eChris Lattner    IITEntries = IIT_LongEncodingTable;
5580f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
559387c9dcddabe8551468647b249d3629953caab9eChris Lattner    // Strip sentinel bit.
560387c9dcddabe8551468647b249d3629953caab9eChris Lattner    NextElt = (TableVal << 1) >> 1;
561387c9dcddabe8551468647b249d3629953caab9eChris Lattner  } else {
562908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    // Decode the TableVal into an array of IITValues.  If the entry was encoded
563908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    // into a single word in the table itself, decode it now.
564626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner    do {
565626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner      IITValues.push_back(TableVal & 0xF);
566626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner      TableVal >>= 4;
567626b108619ec72267ff7b89d325ccf808fee88b6Chris Lattner    } while (TableVal);
5680f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
569387c9dcddabe8551468647b249d3629953caab9eChris Lattner    IITEntries = IITValues;
570387c9dcddabe8551468647b249d3629953caab9eChris Lattner    NextElt = 0;
571a98aa6ad1e8e53600b2e322e2ab869e03d436849Chris Lattner  }
572908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
573908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  // Okay, decode the table into the output vector of IITDescriptors.
574908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  DecodeIITType(NextElt, IITEntries, T);
575908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  while (NextElt != IITEntries.size() && IITEntries[NextElt] != 0)
576908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    DecodeIITType(NextElt, IITEntries, T);
577908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner}
578908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
579908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
580908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattnerstatic Type *DecodeFixedType(ArrayRef<Intrinsic::IITDescriptor> &Infos,
581908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                             ArrayRef<Type*> Tys, LLVMContext &Context) {
582908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  using namespace Intrinsic;
583908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  IITDescriptor D = Infos.front();
584908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  Infos = Infos.slice(1);
5850f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
586908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  switch (D.Kind) {
587908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Void: return Type::getVoidTy(Context);
588908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::MMX: return Type::getX86_MMXTy(Context);
589908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Metadata: return Type::getMetadataTy(Context);
5904d0b4a45dc724666d5de4f2f7c2d295487e4ca3eMichael Ilseman  case IITDescriptor::Half: return Type::getHalfTy(Context);
591908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Float: return Type::getFloatTy(Context);
592908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Double: return Type::getDoubleTy(Context);
5930f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
594908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Integer:
595908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return IntegerType::get(Context, D.Integer_Width);
596908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Vector:
597908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return VectorType::get(DecodeFixedType(Infos, Tys, Context),D.Vector_Width);
598908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Pointer:
599908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return PointerType::get(DecodeFixedType(Infos, Tys, Context),
600908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                            D.Pointer_AddressSpace);
601908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Struct: {
602908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    Type *Elts[5];
603908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    assert(D.Struct_NumElements <= 5 && "Can't handle this yet");
604908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
605908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner      Elts[i] = DecodeFixedType(Infos, Tys, Context);
606908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return StructType::get(Context, ArrayRef<Type*>(Elts,D.Struct_NumElements));
607908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  }
608908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
609908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::Argument:
610908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return Tys[D.getArgumentNumber()];
611908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::ExtendVecArgument:
612908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return VectorType::getExtendedElementVectorType(cast<VectorType>(
613908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                                  Tys[D.getArgumentNumber()]));
6140f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
615908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  case IITDescriptor::TruncVecArgument:
616908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    return VectorType::getTruncatedElementVectorType(cast<VectorType>(
617908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                                  Tys[D.getArgumentNumber()]));
618908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  }
619908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  llvm_unreachable("unhandled");
620908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner}
621908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
622908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
623908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner
624908a831a9a1fb043bc4758d6712d78255099ae51Chris LattnerFunctionType *Intrinsic::getType(LLVMContext &Context,
625908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner                                 ID id, ArrayRef<Type*> Tys) {
626908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  SmallVector<IITDescriptor, 8> Table;
627908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  getIntrinsicInfoTableEntries(id, Table);
6280f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
629908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  ArrayRef<IITDescriptor> TableRef = Table;
630908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  Type *ResultTy = DecodeFixedType(TableRef, Tys, Context);
6310f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman
632387c9dcddabe8551468647b249d3629953caab9eChris Lattner  SmallVector<Type*, 8> ArgTys;
633908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner  while (!TableRef.empty())
634908a831a9a1fb043bc4758d6712d78255099ae51Chris Lattner    ArgTys.push_back(DecodeFixedType(TableRef, Tys, Context));
63595af592a631f403e1458ec1155f89fc31011572cJim Laskey
6360f47e7e01928a4e16f773dc4db14a800d148bfc8Michael Ilseman  return FunctionType::get(ResultTy, ArgTys, false);
63795af592a631f403e1458ec1155f89fc31011572cJim Laskey}
63895af592a631f403e1458ec1155f89fc31011572cJim Laskey
6390d52ff1f7b993750a74a5d4432273092de9af069Mon P Wangbool Intrinsic::isOverloaded(ID id) {
6400d52ff1f7b993750a74a5d4432273092de9af069Mon P Wang#define GET_INTRINSIC_OVERLOAD_TABLE
641351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
6420d52ff1f7b993750a74a5d4432273092de9af069Mon P Wang#undef GET_INTRINSIC_OVERLOAD_TABLE
6430d52ff1f7b993750a74a5d4432273092de9af069Mon P Wang}
6440d52ff1f7b993750a74a5d4432273092de9af069Mon P Wang
645048ffb239c282277959463c61200b86e2380cb84Chris Lattner/// This defines the "Intrinsic::getAttributes(ID id)" method.
646a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands#define GET_INTRINSIC_ATTRIBUTES
647351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
648a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands#undef GET_INTRINSIC_ATTRIBUTES
649a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands
650eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin KramerFunction *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
651a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  // There can never be multiple globals with the same name of different types,
652a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands  // because intrinsics must be a specific type.
65379ab3e801480e6412c064a3fb15a79a8cab13efcDuncan Sands  return
654eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer    cast<Function>(M->getOrInsertFunction(getName(id, Tys),
655eb9a85f09e18b3fe88499710404b38d3a9128f62Benjamin Kramer                                          getType(M->getContext(), id, Tys)));
65695af592a631f403e1458ec1155f89fc31011572cJim Laskey}
65795af592a631f403e1458ec1155f89fc31011572cJim Laskey
65849de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen// This defines the "Intrinsic::getIntrinsicForGCCBuiltin()" method.
65949de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
660351ba145a7db32b457f118ecc4d873765ac2a16bChandler Carruth#include "llvm/IR/Intrinsics.gen"
66149de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
66249de98214b82fefeb8f16efbf8cdd8813a85469bDale Johannesen
6630054c7a867f85f52fdcc11279d696160de92c9f8Gabor Greif/// hasAddressTaken - returns true if there are any uses of this function
6640054c7a867f85f52fdcc11279d696160de92c9f8Gabor Greif/// other than direct calls or invokes to it.
665c9f7500d1752feac7cece26d20007a99d21f677cGabor Greifbool Function::hasAddressTaken(const User* *PutOffender) const {
66660ad781c61815ca5b8dc2a45a102e1c8af65992fGabor Greif  for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
667c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif    const User *U = *I;
668b7454fd9df0b477e3daf2fce6e1d5e1b241562dfJay Foad    if (isa<BlockAddress>(U))
669b7454fd9df0b477e3daf2fce6e1d5e1b241562dfJay Foad      continue;
670c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif    if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
671c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif      return PutOffender ? (*PutOffender = U, true) : true;
672c8b82ccbcf3b2e2384d2c0b5176e7b0b40b7f82fGabor Greif    ImmutableCallSite CS(cast<Instruction>(U));
673c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif    if (!CS.isCallee(I))
674c9f7500d1752feac7cece26d20007a99d21f677cGabor Greif      return PutOffender ? (*PutOffender = U, true) : true;
675757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad  }
676757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad  return false;
677757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad}
678757068f3bad425fb126fe16ab7b8a82a636e6bbdJay Foad
679c66330504c3f433430a28cd7f7f981e555c51bceEli Friedmanbool Function::isDefTriviallyDead() const {
680c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman  // Check the linkage
681c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman  if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
682c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman      !hasAvailableExternallyLinkage())
683c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman    return false;
684c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman
685c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman  // Check if the function is used by anything other than a blockaddress.
686c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman  for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
687c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman    if (!isa<BlockAddress>(*I))
688c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman      return false;
689c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman
690c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman  return true;
691c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman}
692c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman
6933c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling/// callsFunctionThatReturnsTwice - Return true if the function has a call to
6943c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling/// setjmp or other function that gcc recognizes as "returning twice".
6953c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendlingbool Function::callsFunctionThatReturnsTwice() const {
6963c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling  for (const_inst_iterator
6973c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling         I = inst_begin(this), E = inst_end(this); I != E; ++I) {
6983c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling    const CallInst* callInst = dyn_cast<CallInst>(&*I);
6993c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling    if (!callInst)
7003c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling      continue;
7013c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling    if (callInst->canReturnTwice())
7023c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling      return true;
7033c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling  }
7043c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling
7053c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling  return false;
7063c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling}
7073c5e60994f53eef2808a33b5ca6c3dffc2168054Bill Wendling
708