blob: ff1a30271204c395565b6f7e83c6954ca8383547 [file] [log] [blame]
Chris Lattner5aa9e3e2002-05-24 20:42:13 +00001//===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner5aa9e3e2002-05-24 20:42:13 +00009//
10// Loop over the functions that are in the module and look for functions that
11// have the same name. More often than not, there will be things like:
12//
13// declare void %foo(...)
14// void %foo(int, int) { ... }
15//
16// because of the way things are declared in C. If this is the case, patch
17// things up.
18//
19//===----------------------------------------------------------------------===//
20
Chris Lattner5afe2f22002-07-23 22:04:02 +000021#include "llvm/Transforms/IPO.h"
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000022#include "llvm/Module.h"
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/Pass.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000025#include "llvm/Instructions.h"
Chris Lattner013eca002002-10-09 21:10:06 +000026#include "llvm/Constants.h"
Chris Lattner8791e262003-11-20 21:21:31 +000027#include "llvm/Support/CallSite.h"
Chris Lattner4cd99ff2003-10-22 03:35:34 +000028#include "llvm/Target/TargetData.h"
Chris Lattnercbf08392003-08-13 22:15:04 +000029#include "llvm/Assembly/Writer.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/ADT/Statistic.h"
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000031#include <algorithm>
Chris Lattner8791e262003-11-20 21:21:31 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000034namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035 Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved");
Chris Lattner013eca002002-10-09 21:10:06 +000036 Statistic<> NumGlobals("funcresolve", "Number of global variables resolved");
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000037
Chris Lattner4f2cf032004-09-20 04:48:05 +000038 struct FunctionResolvingPass : public ModulePass {
Chris Lattner4cd99ff2003-10-22 03:35:34 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 }
42
Chris Lattner4f2cf032004-09-20 04:48:05 +000043 bool runOnModule(Module &M);
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000044 };
Chris Lattnera2c09852002-07-26 21:12:44 +000045 RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000046}
47
Chris Lattner4f2cf032004-09-20 04:48:05 +000048ModulePass *llvm::createFunctionResolvingPass() {
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000049 return new FunctionResolvingPass();
50}
51
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +000052static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals,
Chris Lattner013eca002002-10-09 21:10:06 +000053 Function *Concrete) {
54 bool Changed = false;
55 for (unsigned i = 0; i != Globals.size(); ++i)
56 if (Globals[i] != Concrete) {
57 Function *Old = cast<Function>(Globals[i]);
Reid Spencer175613a2005-12-13 19:56:51 +000058 const FunctionType *OldFT = Old->getFunctionType();
59 const FunctionType *ConcreteFT = Concrete->getFunctionType();
Misha Brukmanb1c93172005-04-21 23:48:37 +000060
Reid Spencer175613a2005-12-13 19:56:51 +000061 if (OldFT->getNumParams() > ConcreteFT->getNumParams() &&
62 !ConcreteFT->isVarArg())
Chris Lattnerdbb05b02003-02-27 20:55:48 +000063 if (!Old->use_empty()) {
64 std::cerr << "WARNING: Linking function '" << Old->getName()
65 << "' is causing arguments to be dropped.\n";
66 std::cerr << "WARNING: Prototype: ";
67 WriteAsOperand(std::cerr, Old);
68 std::cerr << " resolved to ";
69 WriteAsOperand(std::cerr, Concrete);
70 std::cerr << "\n";
71 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000072
Chris Lattner013eca002002-10-09 21:10:06 +000073 // Check to make sure that if there are specified types, that they
74 // match...
75 //
Reid Spencer175613a2005-12-13 19:56:51 +000076 unsigned NumArguments = std::min(OldFT->getNumParams(),
77 ConcreteFT->getNumParams());
Chris Lattnerdbb05b02003-02-27 20:55:48 +000078
Chris Lattner50cbb902003-03-03 19:57:46 +000079 if (!Old->use_empty() && !Concrete->use_empty())
80 for (unsigned i = 0; i < NumArguments; ++i)
Reid Spencer175613a2005-12-13 19:56:51 +000081 if (OldFT->getParamType(i) != ConcreteFT->getParamType(i))
82 if (OldFT->getParamType(i)->getTypeID() !=
83 ConcreteFT->getParamType(i)->getTypeID()) {
Chris Lattner6fc0ee92003-08-23 20:03:05 +000084 std::cerr << "WARNING: Function [" << Old->getName()
Chris Lattner18e5d522003-11-20 18:19:35 +000085 << "]: Parameter types conflict for: '";
Reid Spencer175613a2005-12-13 19:56:51 +000086 WriteTypeSymbolic(std::cerr, OldFT, &M);
87 std::cerr << "' (in "
88 << Old->getParent()->getModuleIdentifier() << ") and '";
89 WriteTypeSymbolic(std::cerr, ConcreteFT, &M);
90 std::cerr << "'(in "
91 << Concrete->getParent()->getModuleIdentifier() << ")\n";
Chris Lattner015d98e2003-08-20 23:50:38 +000092 return Changed;
Chris Lattner6fc0ee92003-08-23 20:03:05 +000093 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000094
Chris Lattner9810b942003-04-28 01:23:29 +000095 // Attempt to convert all of the uses of the old function to the concrete
96 // form of the function. If there is a use of the fn that we don't
97 // understand here we punt to avoid making a bad transformation.
Chris Lattner013eca002002-10-09 21:10:06 +000098 //
Chris Lattner9810b942003-04-28 01:23:29 +000099 // At this point, we know that the return values are the same for our two
100 // functions and that the Old function has no varargs fns specified. In
101 // otherwords it's just <retty> (...)
Chris Lattner013eca002002-10-09 21:10:06 +0000102 //
Chris Lattnerd6a44922005-02-01 01:23:31 +0000103 if (!Old->use_empty()) {
Chris Lattnere9340222003-07-23 22:03:18 +0000104 Value *Replacement = Concrete;
105 if (Concrete->getType() != Old->getType())
Chris Lattnerd6a44922005-02-01 01:23:31 +0000106 Replacement = ConstantExpr::getCast(Concrete, Old->getType());
107 NumResolved += Old->getNumUses();
Chris Lattnere9340222003-07-23 22:03:18 +0000108 Old->replaceAllUsesWith(Replacement);
109 }
Chris Lattner08043682003-05-31 21:08:45 +0000110
111 // Since there are no uses of Old anymore, remove it from the module.
112 M.getFunctionList().erase(Old);
Chris Lattner013eca002002-10-09 21:10:06 +0000113 }
114 return Changed;
115}
116
117
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000118static bool ResolveGlobalVariables(Module &M,
119 std::vector<GlobalValue*> &Globals,
Chris Lattner013eca002002-10-09 21:10:06 +0000120 GlobalVariable *Concrete) {
121 bool Changed = false;
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000122
Chris Lattner013eca002002-10-09 21:10:06 +0000123 for (unsigned i = 0; i != Globals.size(); ++i)
124 if (Globals[i] != Concrete) {
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000125 Constant *Cast = ConstantExpr::getCast(Concrete, Globals[i]->getType());
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000126 Globals[i]->replaceAllUsesWith(Cast);
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000127
Chris Lattner013eca002002-10-09 21:10:06 +0000128 // Since there are no uses of Old anymore, remove it from the module.
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000129 M.getGlobalList().erase(cast<GlobalVariable>(Globals[i]));
Chris Lattner013eca002002-10-09 21:10:06 +0000130
131 ++NumGlobals;
132 Changed = true;
133 }
134 return Changed;
135}
136
Chris Lattner8791e262003-11-20 21:21:31 +0000137// Check to see if all of the callers of F ignore the return value.
138static bool CallersAllIgnoreReturnValue(Function &F) {
139 if (F.getReturnType() == Type::VoidTy) return true;
140 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000141 if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
142 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
Chris Lattner8791e262003-11-20 21:21:31 +0000143 I != E; ++I) {
144 CallSite CS = CallSite::get(*I);
145 if (!CS.getInstruction() || !CS.getInstruction()->use_empty())
146 return false;
147 }
148 } else {
149 CallSite CS = CallSite::get(*I);
150 if (!CS.getInstruction() || !CS.getInstruction()->use_empty())
151 return false;
152 }
153 }
154 return true;
155}
156
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000157static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000158 std::vector<GlobalValue*> &Globals) {
Chris Lattner013eca002002-10-09 21:10:06 +0000159 assert(!Globals.empty() && "Globals list shouldn't be empty here!");
160
161 bool isFunction = isa<Function>(Globals[0]); // Is this group all functions?
Chris Lattner013eca002002-10-09 21:10:06 +0000162 GlobalValue *Concrete = 0; // The most concrete implementation to resolve to
163
Chris Lattner013eca002002-10-09 21:10:06 +0000164 for (unsigned i = 0; i != Globals.size(); ) {
165 if (isa<Function>(Globals[i]) != isFunction) {
166 std::cerr << "WARNING: Found function and global variable with the "
167 << "same name: '" << Globals[i]->getName() << "'.\n";
168 return false; // Don't know how to handle this, bail out!
169 }
170
Chris Lattner52b8fc02002-11-10 03:36:55 +0000171 if (isFunction) {
Chris Lattner013eca002002-10-09 21:10:06 +0000172 // For functions, we look to merge functions definitions of "int (...)"
173 // to 'int (int)' or 'int ()' or whatever else is not completely generic.
174 //
175 Function *F = cast<Function>(Globals[i]);
Chris Lattner5997c3d2002-11-08 00:38:20 +0000176 if (!F->isExternal()) {
Chris Lattner52b8fc02002-11-10 03:36:55 +0000177 if (Concrete && !Concrete->isExternal())
Chris Lattner013eca002002-10-09 21:10:06 +0000178 return false; // Found two different functions types. Can't choose!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179
Chris Lattner013eca002002-10-09 21:10:06 +0000180 Concrete = Globals[i];
Chris Lattner52b8fc02002-11-10 03:36:55 +0000181 } else if (Concrete) {
Chris Lattner8791e262003-11-20 21:21:31 +0000182 if (Concrete->isExternal()) // If we have multiple external symbols...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000183 if (F->getFunctionType()->getNumParams() >
Chris Lattner52b8fc02002-11-10 03:36:55 +0000184 cast<Function>(Concrete)->getFunctionType()->getNumParams())
185 Concrete = F; // We are more concrete than "Concrete"!
186
187 } else {
188 Concrete = F;
Chris Lattner013eca002002-10-09 21:10:06 +0000189 }
Chris Lattner013eca002002-10-09 21:10:06 +0000190 } else {
Chris Lattner013eca002002-10-09 21:10:06 +0000191 GlobalVariable *GV = cast<GlobalVariable>(Globals[i]);
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000192 if (!GV->isExternal()) {
193 if (Concrete) {
194 std::cerr << "WARNING: Two global variables with external linkage"
195 << " exist with the same name: '" << GV->getName()
196 << "'!\n";
197 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000198 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000199 Concrete = GV;
Chris Lattner013eca002002-10-09 21:10:06 +0000200 }
Chris Lattner013eca002002-10-09 21:10:06 +0000201 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000202 ++i;
Chris Lattner013eca002002-10-09 21:10:06 +0000203 }
204
205 if (Globals.size() > 1) { // Found a multiply defined global...
Chris Lattner2b132962003-05-31 21:57:06 +0000206 // If there are no external declarations, and there is at most one
207 // externally visible instance of the global, then there is nothing to do.
208 //
209 bool HasExternal = false;
210 unsigned NumInstancesWithExternalLinkage = 0;
211
212 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
213 if (Globals[i]->isExternal())
214 HasExternal = true;
215 else if (!Globals[i]->hasInternalLinkage())
216 NumInstancesWithExternalLinkage++;
217 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218
Chris Lattner2b132962003-05-31 21:57:06 +0000219 if (!HasExternal && NumInstancesWithExternalLinkage <= 1)
220 return false; // Nothing to do? Must have multiple internal definitions.
221
Chris Lattner9e8fe492003-10-22 23:03:38 +0000222 // There are a couple of special cases we don't want to print the warning
223 // for, check them now.
224 bool DontPrintWarning = false;
225 if (Concrete && Globals.size() == 2) {
226 GlobalValue *Other = Globals[Globals[0] == Concrete];
227 // If the non-concrete global is a function which takes (...) arguments,
Chris Lattner8791e262003-11-20 21:21:31 +0000228 // and the return values match (or was never used), do not warn.
Chris Lattner9e8fe492003-10-22 23:03:38 +0000229 if (Function *ConcreteF = dyn_cast<Function>(Concrete))
230 if (Function *OtherF = dyn_cast<Function>(Other))
Chris Lattner8791e262003-11-20 21:21:31 +0000231 if ((ConcreteF->getReturnType() == OtherF->getReturnType() ||
232 CallersAllIgnoreReturnValue(*OtherF)) &&
Chris Lattner9e8fe492003-10-22 23:03:38 +0000233 OtherF->getFunctionType()->isVarArg() &&
Chris Lattnerfa829be2004-02-09 04:14:01 +0000234 OtherF->getFunctionType()->getNumParams() == 0)
Chris Lattner9e8fe492003-10-22 23:03:38 +0000235 DontPrintWarning = true;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000236
Chris Lattner9e8fe492003-10-22 23:03:38 +0000237 // Otherwise, if the non-concrete global is a global array variable with a
238 // size of 0, and the concrete global is an array with a real size, don't
239 // warn. This occurs due to declaring 'extern int A[];'.
240 if (GlobalVariable *ConcreteGV = dyn_cast<GlobalVariable>(Concrete))
Chris Lattner14c198d2004-08-20 00:30:39 +0000241 if (GlobalVariable *OtherGV = dyn_cast<GlobalVariable>(Other)) {
242 const Type *CTy = ConcreteGV->getType();
243 const Type *OTy = OtherGV->getType();
244
245 if (CTy->isSized())
246 if (!OTy->isSized() || !TD.getTypeSize(OTy) ||
247 TD.getTypeSize(OTy) == TD.getTypeSize(CTy))
248 DontPrintWarning = true;
249 }
Chris Lattner9e8fe492003-10-22 23:03:38 +0000250 }
Chris Lattner2b132962003-05-31 21:57:06 +0000251
Chris Lattner9af8efd2004-09-30 00:12:29 +0000252 if (0 && !DontPrintWarning) {
Chris Lattner9e8fe492003-10-22 23:03:38 +0000253 std::cerr << "WARNING: Found global types that are not compatible:\n";
254 for (unsigned i = 0; i < Globals.size(); ++i) {
Chris Lattner18e5d522003-11-20 18:19:35 +0000255 std::cerr << "\t";
256 WriteTypeSymbolic(std::cerr, Globals[i]->getType(), &M);
257 std::cerr << " %" << Globals[i]->getName() << "\n";
Chris Lattner9e8fe492003-10-22 23:03:38 +0000258 }
Chris Lattner013eca002002-10-09 21:10:06 +0000259 }
260
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000261 if (!Concrete)
262 Concrete = Globals[0];
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000263 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Concrete)) {
264 // Handle special case hack to change globals if it will make their types
265 // happier in the long run. The situation we do this is intentionally
266 // extremely limited.
267 if (GV->use_empty() && GV->hasInitializer() &&
268 GV->getInitializer()->isNullValue()) {
269 // Check to see if there is another (external) global with the same size
270 // and a non-empty use-list. If so, we will make IT be the real
271 // implementation.
272 unsigned TS = TD.getTypeSize(Concrete->getType()->getElementType());
273 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
274 if (Globals[i] != Concrete && !Globals[i]->use_empty() &&
275 isa<GlobalVariable>(Globals[i]) &&
276 TD.getTypeSize(Globals[i]->getType()->getElementType()) == TS) {
277 // At this point we want to replace Concrete with Globals[i]. Make
278 // concrete external, and Globals[i] have an initializer.
279 GlobalVariable *NGV = cast<GlobalVariable>(Globals[i]);
280 const Type *ElTy = NGV->getType()->getElementType();
281 NGV->setInitializer(Constant::getNullValue(ElTy));
282 cast<GlobalVariable>(Concrete)->setInitializer(0);
283 Concrete = NGV;
284 break;
285 }
286 }
287 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000288
Chris Lattner013eca002002-10-09 21:10:06 +0000289 if (isFunction)
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000290 return ResolveFunctions(M, Globals, cast<Function>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000291 else
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000292 return ResolveGlobalVariables(M, Globals,
293 cast<GlobalVariable>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000294 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000295 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000296}
297
Chris Lattner4f2cf032004-09-20 04:48:05 +0000298bool FunctionResolvingPass::runOnModule(Module &M) {
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000299 std::map<std::string, std::vector<GlobalValue*> > Globals;
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000300
Chris Lattner12b35932003-10-22 04:42:20 +0000301 // Loop over the globals, adding them to the Globals map. We use a two pass
302 // algorithm here to avoid problems with iterators getting invalidated if we
303 // did a one pass scheme.
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000304 //
Chris Lattner6d3425e2003-10-22 04:43:18 +0000305 bool Changed = false;
Chris Lattner12b35932003-10-22 04:42:20 +0000306 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
307 Function *F = I++;
Chris Lattner6d3425e2003-10-22 04:43:18 +0000308 if (F->use_empty() && F->isExternal()) {
Chris Lattner12b35932003-10-22 04:42:20 +0000309 M.getFunctionList().erase(F);
Chris Lattner6d3425e2003-10-22 04:43:18 +0000310 Changed = true;
Chris Lattner1e1abdd2004-06-18 05:50:48 +0000311 } else if (!F->hasInternalLinkage() && !F->getName().empty() &&
312 !F->getIntrinsicID())
Chris Lattner12b35932003-10-22 04:42:20 +0000313 Globals[F->getName()].push_back(F);
314 }
315
Chris Lattner531f9e92005-03-15 04:54:21 +0000316 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ) {
Chris Lattner12b35932003-10-22 04:42:20 +0000317 GlobalVariable *GV = I++;
Chris Lattner6d3425e2003-10-22 04:43:18 +0000318 if (GV->use_empty() && GV->isExternal()) {
Chris Lattner12b35932003-10-22 04:42:20 +0000319 M.getGlobalList().erase(GV);
Chris Lattner6d3425e2003-10-22 04:43:18 +0000320 Changed = true;
321 } else if (!GV->hasInternalLinkage() && !GV->getName().empty())
Chris Lattner12b35932003-10-22 04:42:20 +0000322 Globals[GV->getName()].push_back(GV);
323 }
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000324
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000325 TargetData &TD = getAnalysis<TargetData>();
326
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000327 // Now we have a list of all functions with a particular name. If there is
328 // more than one entry in a list, merge the functions together.
329 //
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000330 for (std::map<std::string, std::vector<GlobalValue*> >::iterator
331 I = Globals.begin(), E = Globals.end(); I != E; ++I)
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000332 Changed |= ProcessGlobalsWithSameName(M, TD, I->second);
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000333
Chris Lattner52b8fc02002-11-10 03:36:55 +0000334 // Now loop over all of the globals, checking to see if any are trivially
335 // dead. If so, remove them now.
336
337 for (Module::iterator I = M.begin(), E = M.end(); I != E; )
338 if (I->isExternal() && I->use_empty()) {
339 Function *F = I;
340 ++I;
341 M.getFunctionList().erase(F);
342 ++NumResolved;
343 Changed = true;
344 } else {
345 ++I;
346 }
347
Chris Lattner531f9e92005-03-15 04:54:21 +0000348 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; )
Chris Lattner52b8fc02002-11-10 03:36:55 +0000349 if (I->isExternal() && I->use_empty()) {
350 GlobalVariable *GV = I;
351 ++I;
352 M.getGlobalList().erase(GV);
353 ++NumGlobals;
354 Changed = true;
355 } else {
356 ++I;
357 }
358
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000359 return Changed;
360}