blob: e7a7041d778ffff6ceb3cbdc3d78b060b39e9e4f [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Function.cpp - Implement the Global object classes ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000010// This file implements the Function class for the VMCore library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +000015#include "llvm/DerivedTypes.h"
Chris Lattner89046ca2004-10-12 04:20:25 +000016#include "llvm/IntrinsicInst.h"
Chris Lattner3e13b8c2008-01-02 23:42:30 +000017#include "llvm/ParameterAttributes.h"
Dan Gohman3a071482007-08-20 19:23:34 +000018#include "llvm/CodeGen/ValueTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/Support/LeakDetector.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000020#include "llvm/Support/StringPool.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Duncan Sands38ef3a82007-12-03 20:06:50 +000022#include "llvm/ADT/BitVector.h"
Gordon Henriksen71183b62007-12-10 03:18:06 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattnerb392d302004-12-05 06:43:27 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattnerf6c93e32005-01-30 00:09:23 +000027BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000028 BasicBlock *Ret = new BasicBlock();
29 // This should not be garbage monitored.
30 LeakDetector::removeGarbageObject(Ret);
31 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000032}
33
Chris Lattner113f4f42002-06-25 16:13:24 +000034iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
35 return F->getBasicBlockList();
36}
37
Chris Lattnerf6c93e32005-01-30 00:09:23 +000038Argument *ilist_traits<Argument>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000039 Argument *Ret = new Argument(Type::Int32Ty);
Chris Lattner184b2982002-09-08 18:59:35 +000040 // This should not be garbage monitored.
41 LeakDetector::removeGarbageObject(Ret);
42 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000043}
44
45iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
46 return F->getArgumentList();
47}
48
49// Explicit instantiations of SymbolTableListTraits since some of the methods
50// are not in the public header file...
Chris Lattnerb47aa542007-04-17 03:26:42 +000051template class SymbolTableListTraits<Argument, Function>;
52template class SymbolTableListTraits<BasicBlock, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Chris Lattnerda975502001-09-10 07:58:01 +000054//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000055// Argument Implementation
56//===----------------------------------------------------------------------===//
57
Misha Brukmanb1c93172005-04-21 23:48:37 +000058Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner32ab6432007-02-12 05:18:08 +000059 : Value(Ty, Value::ArgumentVal) {
Chris Lattner9ed7aef2002-09-06 21:33:15 +000060 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000061
62 // Make sure that we get added to a function
63 LeakDetector::addGarbageObject(this);
64
Chris Lattner9ed7aef2002-09-06 21:33:15 +000065 if (Par)
66 Par->getArgumentList().push_back(this);
Chris Lattner32ab6432007-02-12 05:18:08 +000067 setName(Name);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000068}
69
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000071 if (getParent())
72 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000074 if (getParent())
75 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000076}
77
Chris Lattnere30f09d2008-01-24 17:47:11 +000078/// getArgNo - Return the index of this formal argument in its containing
79/// function. For example in "void foo(int a, float b)" a is 0 and b is 1.
80unsigned Argument::getArgNo() const {
81 const Function *F = getParent();
82 assert(F && "Argument is not in a function");
83
84 Function::const_arg_iterator AI = F->arg_begin();
85 unsigned ArgIdx = 0;
86 for (; &*AI != this; ++AI)
87 ++ArgIdx;
88
89 return ArgIdx;
90}
91
92/// hasByValAttr - Return true if this argument has the byval attribute on it
93/// in its containing function.
94bool Argument::hasByValAttr() const {
95 if (!isa<PointerType>(getType())) return false;
96 return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::ByVal);
97}
98
99/// hasNoAliasAttr - Return true if this argument has the noalias attribute on
100/// it in its containing function.
101bool Argument::hasNoAliasAttr() const {
102 if (!isa<PointerType>(getType())) return false;
103 return getParent()->paramHasAttr(getArgNo()+1, ParamAttr::NoAlias);
104}
105
Owen Andersonc64dfb42008-02-17 23:22:28 +0000106/// hasSRetAttr - Return true if this argument has the sret attribute on
107/// it in its containing function.
108bool Argument::hasStructRetAttr() const {
109 if (!isa<PointerType>(getType())) return false;
Owen Andersonc66655e2008-02-18 04:06:26 +0000110 if (getArgNo()) return false; // StructRet param must be first param
111 return getParent()->paramHasAttr(1, ParamAttr::StructRet);
Owen Andersonc64dfb42008-02-17 23:22:28 +0000112}
113
Chris Lattnere30f09d2008-01-24 17:47:11 +0000114
115
116
Chris Lattnerd255ae22002-04-09 19:39:35 +0000117//===----------------------------------------------------------------------===//
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000118// Helper Methods in Function
Reid Spencer019c8862007-04-09 15:01:12 +0000119//===----------------------------------------------------------------------===//
120
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000121const FunctionType *Function::getFunctionType() const {
122 return cast<FunctionType>(getType()->getElementType());
Reid Spencer019c8862007-04-09 15:01:12 +0000123}
124
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000125bool Function::isVarArg() const {
126 return getFunctionType()->isVarArg();
Reid Spencer019c8862007-04-09 15:01:12 +0000127}
128
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000129const Type *Function::getReturnType() const {
130 return getFunctionType()->getReturnType();
Duncan Sands185eeac2007-11-25 14:10:56 +0000131}
132
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000133void Function::removeFromParent() {
134 getParent()->getFunctionList().remove(this);
Duncan Sands185eeac2007-11-25 14:10:56 +0000135}
136
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000137void Function::eraseFromParent() {
138 getParent()->getFunctionList().erase(this);
Reid Spencer019c8862007-04-09 15:01:12 +0000139}
140
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000141/// @brief Determine whether the function has the given attribute.
142bool Function::paramHasAttr(uint16_t i, unsigned attr) const {
143 return ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr);
Reid Spencer019c8862007-04-09 15:01:12 +0000144}
145
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000146/// @brief Determine if the function cannot return.
147bool Function::doesNotReturn() const {
148 return paramHasAttr(0, ParamAttr::NoReturn);
Duncan Sandsb41f8722007-11-30 18:19:18 +0000149}
150
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000151/// @brief Determine if the function cannot unwind.
152bool Function::doesNotThrow() const {
153 return paramHasAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000154}
155
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000156/// @brief Determine if the function does not access memory.
157bool Function::doesNotAccessMemory() const {
158 return paramHasAttr(0, ParamAttr::ReadNone);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000159}
160
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000161/// @brief Determine if the function does not access or only reads memory.
162bool Function::onlyReadsMemory() const {
163 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
164}
165
166/// @brief Determine if the function returns a structure.
167bool Function::isStructReturn() const {
168 return paramHasAttr(1, ParamAttr::StructRet);
Reid Spencerc6a83842007-04-22 17:28:03 +0000169}
170
Reid Spencer019c8862007-04-09 15:01:12 +0000171//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +0000172// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +0000173//===----------------------------------------------------------------------===//
174
Chris Lattner379a8d22003-04-16 20:28:45 +0000175Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
Chris Lattner6213ae02002-09-06 20:46:32 +0000176 const std::string &name, Module *ParentModule)
Christopher Lambedf07882007-12-17 01:12:55 +0000177 : GlobalValue(PointerType::getUnqual(Ty),
178 Value::FunctionVal, 0, 0, Linkage, name),
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000179 ParamAttrs(0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000180 SymTab = new ValueSymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +0000181
Chris Lattner3ae303c2003-11-21 22:32:23 +0000182 assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
183 && "LLVM functions cannot return aggregate values!");
184
Chris Lattnere2de9082007-08-18 06:14:52 +0000185 // If the function has arguments, mark them as lazily built.
186 if (Ty->getNumParams())
187 SubclassData = 1; // Set the "has lazy arguments" bit.
188
Chris Lattner184b2982002-09-08 18:59:35 +0000189 // Make sure that we get added to a function
190 LeakDetector::addGarbageObject(this);
191
Chris Lattner6213ae02002-09-06 20:46:32 +0000192 if (ParentModule)
193 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194}
195
Gordon Henriksen14a55692007-12-10 02:14:30 +0000196Function::~Function() {
197 dropAllReferences(); // After this it is safe to delete instructions.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198
Chris Lattner2f7c9632001-06-06 20:29:01 +0000199 // Delete all of the method arguments and unlink from symbol table...
Gordon Henriksen14a55692007-12-10 02:14:30 +0000200 ArgumentList.clear();
201 delete SymTab;
Reid Spencerc6a83842007-04-22 17:28:03 +0000202
203 // Drop our reference to the parameter attributes, if any.
Gordon Henriksen14a55692007-12-10 02:14:30 +0000204 if (ParamAttrs)
205 ParamAttrs->dropRef();
Gordon Henriksen71183b62007-12-10 03:18:06 +0000206
207 // Remove the function from the on-the-side collector table.
208 clearCollector();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209}
210
Chris Lattnere2de9082007-08-18 06:14:52 +0000211void Function::BuildLazyArguments() const {
212 // Create the arguments vector, all arguments start out unnamed.
213 const FunctionType *FT = getFunctionType();
214 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
215 assert(FT->getParamType(i) != Type::VoidTy &&
216 "Cannot have void typed arguments!");
217 ArgumentList.push_back(new Argument(FT->getParamType(i)));
218 }
219
220 // Clear the lazy arguments bit.
221 const_cast<Function*>(this)->SubclassData &= ~1;
222}
223
224size_t Function::arg_size() const {
225 return getFunctionType()->getNumParams();
226}
227bool Function::arg_empty() const {
228 return getFunctionType()->getNumParams() == 0;
229}
230
Chris Lattner4e8c4872002-03-23 22:51:58 +0000231void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000232 if (getParent())
233 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000235 if (getParent())
236 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237}
238
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000239void Function::setParamAttrs(const ParamAttrsList *attrs) {
240 // Avoid deleting the ParamAttrsList if they are setting the
241 // attributes to the same list.
242 if (ParamAttrs == attrs)
243 return;
244
245 // Drop reference on the old ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000246 if (ParamAttrs)
247 ParamAttrs->dropRef();
248
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000249 // Add reference to the new ParamAttrsList
Reid Spencerc6a83842007-04-22 17:28:03 +0000250 if (attrs)
251 attrs->addRef();
252
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000253 // Set the new ParamAttrsList.
Reid Spencerc6a83842007-04-22 17:28:03 +0000254 ParamAttrs = attrs;
255}
256
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257// dropAllReferences() - This function causes all the subinstructions to "let
258// go" of all references that they are maintaining. This allows one to
259// 'delete' a whole class at a time, even though there may be circular
260// references... first all references are dropped, and all use counts go to
Misha Brukmanfa100532003-10-10 17:54:14 +0000261// zero. Then everything is deleted for real. Note that no operations are
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262// valid on an object that has "dropped all references", except operator
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263// delete.
264//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000265void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000266 for (iterator I = begin(), E = end(); I != E; ++I)
267 I->dropAllReferences();
Chris Lattnerc1b16512003-09-17 04:58:59 +0000268 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269}
Chris Lattnerda975502001-09-10 07:58:01 +0000270
Gordon Henriksen71183b62007-12-10 03:18:06 +0000271// Maintain the collector name for each function in an on-the-side table. This
272// saves allocating an additional word in Function for programs which do not use
273// GC (i.e., most programs) at the cost of increased overhead for clients which
274// do use GC.
275static DenseMap<const Function*,PooledStringPtr> *CollectorNames;
276static StringPool *CollectorNamePool;
277
278bool Function::hasCollector() const {
279 return CollectorNames && CollectorNames->count(this);
280}
281
282const char *Function::getCollector() const {
283 assert(hasCollector() && "Function has no collector");
284 return *(*CollectorNames)[this];
285}
286
287void Function::setCollector(const char *Str) {
288 if (!CollectorNamePool)
289 CollectorNamePool = new StringPool();
290 if (!CollectorNames)
291 CollectorNames = new DenseMap<const Function*,PooledStringPtr>();
292 (*CollectorNames)[this] = CollectorNamePool->intern(Str);
293}
294
295void Function::clearCollector() {
296 if (CollectorNames) {
297 CollectorNames->erase(this);
298 if (CollectorNames->empty()) {
299 delete CollectorNames;
300 CollectorNames = 0;
Gordon Henriksen4b904b92007-12-10 03:35:18 +0000301 if (CollectorNamePool->empty()) {
302 delete CollectorNamePool;
303 CollectorNamePool = 0;
304 }
Gordon Henriksen71183b62007-12-10 03:18:06 +0000305 }
306 }
307}
308
Chris Lattnerbb346d02003-05-08 03:47:33 +0000309/// getIntrinsicID - This method returns the ID number of the specified
Brian Gaeke960707c2003-11-11 22:41:34 +0000310/// function, or Intrinsic::not_intrinsic if the function is not an
Misha Brukmanfa100532003-10-10 17:54:14 +0000311/// intrinsic, or if the pointer is null. This value is always defined to be
Chris Lattnerbb346d02003-05-08 03:47:33 +0000312/// zero to allow easy checking for whether a function is intrinsic or not. The
313/// particular intrinsic functions which correspond to this value are defined in
314/// llvm/Intrinsics.h.
315///
Reid Spencer9c2eec32007-04-16 06:54:34 +0000316unsigned Function::getIntrinsicID(bool noAssert) const {
Chris Lattner1e92e062007-02-15 19:17:16 +0000317 const ValueName *ValName = this->getValueName();
Reid Spencerc5f397a2007-04-16 07:08:44 +0000318 if (!ValName)
319 return 0;
Chris Lattner1e92e062007-02-15 19:17:16 +0000320 unsigned Len = ValName->getKeyLength();
321 const char *Name = ValName->getKeyData();
322
Reid Spencer78d71f12007-04-16 16:56:54 +0000323 if (Len < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000324 || Name[2] != 'v' || Name[3] != 'm')
Chris Lattnerbb346d02003-05-08 03:47:33 +0000325 return 0; // All intrinsics start with 'llvm.'
Chris Lattner3284ed72003-09-19 19:31:41 +0000326
Reid Spencer9c2eec32007-04-16 06:54:34 +0000327 assert((Len != 5 || noAssert) && "'llvm.' is an invalid intrinsic name!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000328
Chris Lattnerff4d4ee2006-03-09 20:35:01 +0000329#define GET_FUNCTION_RECOGNIZER
330#include "llvm/Intrinsics.gen"
331#undef GET_FUNCTION_RECOGNIZER
Reid Spencer9c2eec32007-04-16 06:54:34 +0000332 assert(noAssert && "Invalid LLVM intrinsic name");
Chris Lattnerbb346d02003-05-08 03:47:33 +0000333 return 0;
334}
335
Reid Spencer2a2117c2007-04-01 07:25:33 +0000336std::string Intrinsic::getName(ID id, const Type **Tys, unsigned numTys) {
Chris Lattner71b8c982006-03-25 06:32:47 +0000337 assert(id < num_intrinsics && "Invalid intrinsic ID!");
338 const char * const Table[] = {
339 "not_intrinsic",
340#define GET_INTRINSIC_NAME_TABLE
341#include "llvm/Intrinsics.gen"
342#undef GET_INTRINSIC_NAME_TABLE
343 };
Reid Spencer2a2117c2007-04-01 07:25:33 +0000344 if (numTys == 0)
345 return Table[id];
346 std::string Result(Table[id]);
347 for (unsigned i = 0; i < numTys; ++i)
348 if (Tys[i])
Dan Gohman3a071482007-08-20 19:23:34 +0000349 Result += "." + MVT::getValueTypeString(MVT::getValueType(Tys[i]));
Reid Spencer2a2117c2007-04-01 07:25:33 +0000350 return Result;
Chris Lattner71b8c982006-03-25 06:32:47 +0000351}
352
Reid Spencer2a2117c2007-04-01 07:25:33 +0000353const FunctionType *Intrinsic::getType(ID id, const Type **Tys,
Chris Lattner31f82df2007-06-05 23:49:06 +0000354 unsigned numTys) {
Jim Laskey2682ea62007-02-07 20:38:26 +0000355 const Type *ResultTy = NULL;
356 std::vector<const Type*> ArgTys;
Jim Laskey2682ea62007-02-07 20:38:26 +0000357 bool IsVarArg = false;
358
359#define GET_INTRINSIC_GENERATOR
360#include "llvm/Intrinsics.gen"
361#undef GET_INTRINSIC_GENERATOR
362
Reid Spencer26d9ff62007-04-09 06:11:23 +0000363 return FunctionType::get(ResultTy, ArgTys, IsVarArg);
Jim Laskey2682ea62007-02-07 20:38:26 +0000364}
365
Duncan Sands38ef3a82007-12-03 20:06:50 +0000366const ParamAttrsList *Intrinsic::getParamAttrs(ID id) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000367 ParamAttrsVector Attrs;
368 uint16_t Attr = ParamAttr::None;
369
370#define GET_INTRINSIC_ATTRIBUTES
371#include "llvm/Intrinsics.gen"
372#undef GET_INTRINSIC_ATTRIBUTES
373
374 // Intrinsics cannot throw exceptions.
375 Attr |= ParamAttr::NoUnwind;
376
377 Attrs.push_back(ParamAttrsWithIndex::get(0, Attr));
Chris Lattner76719ba2008-01-03 01:20:12 +0000378 return ParamAttrsList::get(Attrs);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000379}
380
Reid Spencer2a2117c2007-04-01 07:25:33 +0000381Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys,
382 unsigned numTys) {
Duncan Sands38ef3a82007-12-03 20:06:50 +0000383 // There can never be multiple globals with the same name of different types,
384 // because intrinsics must be a specific type.
385 Function *F =
386 cast<Function>(M->getOrInsertFunction(getName(id, Tys, numTys),
387 getType(id, Tys, numTys)));
388 F->setParamAttrs(getParamAttrs(id));
389 return F;
Jim Laskey2682ea62007-02-07 20:38:26 +0000390}
391
Chris Lattner1ccab842004-10-12 04:32:37 +0000392Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000393 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000394 if (CE->getOpcode() == Instruction::BitCast) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000395 if (isa<PointerType>(CE->getOperand(0)->getType()))
396 return StripPointerCasts(CE->getOperand(0));
397 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
398 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
399 if (!CE->getOperand(i)->isNullValue())
400 return Ptr;
401 return StripPointerCasts(CE->getOperand(0));
402 }
403 return Ptr;
404 }
405
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000406 if (BitCastInst *CI = dyn_cast<BitCastInst>(Ptr)) {
Chris Lattner89046ca2004-10-12 04:20:25 +0000407 if (isa<PointerType>(CI->getOperand(0)->getType()))
408 return StripPointerCasts(CI->getOperand(0));
409 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
Chris Lattnerbbe9b8a2007-04-25 05:49:09 +0000410 if (GEP->hasAllZeroIndices())
411 return StripPointerCasts(GEP->getOperand(0));
Chris Lattner89046ca2004-10-12 04:20:25 +0000412 }
413 return Ptr;
414}
Chris Lattnerbb346d02003-05-08 03:47:33 +0000415
Reid Spencerc49dd8d2004-07-17 23:50:19 +0000416// vim: sw=2 ai