blob: dba44a0dc15674318b86bf4db42d9b61f7de1e29 [file] [log] [blame]
Chris Lattner22ee3eb2002-05-24 20:42:13 +00001//===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner22ee3eb2002-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 Lattnerff1be262002-07-23 22:04:02 +000021#include "llvm/Transforms/IPO.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000022#include "llvm/Module.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/Pass.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000025#include "llvm/Instructions.h"
Chris Lattnera45ec542002-10-09 21:10:06 +000026#include "llvm/Constants.h"
Chris Lattner03fb8b22003-11-20 21:21:31 +000027#include "llvm/Support/CallSite.h"
Chris Lattnerefd47ba2003-10-22 03:35:34 +000028#include "llvm/Target/TargetData.h"
Chris Lattner250d91b2003-08-13 22:15:04 +000029#include "llvm/Assembly/Writer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/ADT/Statistic.h"
Chris Lattner22ee3eb2002-05-24 20:42:13 +000031#include <algorithm>
Chris Lattner03fb8b22003-11-20 21:21:31 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner22ee3eb2002-05-24 20:42:13 +000034namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000035 Statistic<>NumResolved("funcresolve", "Number of varargs functions resolved");
Chris Lattnera45ec542002-10-09 21:10:06 +000036 Statistic<> NumGlobals("funcresolve", "Number of global variables resolved");
Chris Lattner22ee3eb2002-05-24 20:42:13 +000037
Chris Lattnerb12914b2004-09-20 04:48:05 +000038 struct FunctionResolvingPass : public ModulePass {
Chris Lattnerefd47ba2003-10-22 03:35:34 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 }
42
Chris Lattnerb12914b2004-09-20 04:48:05 +000043 bool runOnModule(Module &M);
Chris Lattner22ee3eb2002-05-24 20:42:13 +000044 };
Chris Lattner1e435162002-07-26 21:12:44 +000045 RegisterOpt<FunctionResolvingPass> X("funcresolve", "Resolve Functions");
Chris Lattner22ee3eb2002-05-24 20:42:13 +000046}
47
Chris Lattnerb12914b2004-09-20 04:48:05 +000048ModulePass *llvm::createFunctionResolvingPass() {
Chris Lattner22ee3eb2002-05-24 20:42:13 +000049 return new FunctionResolvingPass();
50}
51
Chris Lattnerda902ba2003-01-30 18:22:32 +000052static bool ResolveFunctions(Module &M, std::vector<GlobalValue*> &Globals,
Chris Lattnera45ec542002-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]);
58 const FunctionType *OldMT = Old->getFunctionType();
59 const FunctionType *ConcreteMT = Concrete->getFunctionType();
60
Chris Lattnerd5d89962004-02-09 04:14:01 +000061 if (OldMT->getNumParams() > ConcreteMT->getNumParams() &&
Chris Lattnerdde601d2003-03-03 19:57:46 +000062 !ConcreteMT->isVarArg())
Chris Lattner4e2fd752003-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 }
Chris Lattnera45ec542002-10-09 21:10:06 +000072
73 // Check to make sure that if there are specified types, that they
74 // match...
75 //
Chris Lattnerd5d89962004-02-09 04:14:01 +000076 unsigned NumArguments = std::min(OldMT->getNumParams(),
77 ConcreteMT->getNumParams());
Chris Lattner4e2fd752003-02-27 20:55:48 +000078
Chris Lattnerdde601d2003-03-03 19:57:46 +000079 if (!Old->use_empty() && !Concrete->use_empty())
80 for (unsigned i = 0; i < NumArguments; ++i)
Chris Lattnerd5d89962004-02-09 04:14:01 +000081 if (OldMT->getParamType(i) != ConcreteMT->getParamType(i))
Chris Lattnerf70c22b2004-06-17 18:19:28 +000082 if (OldMT->getParamType(i)->getTypeID() !=
83 ConcreteMT->getParamType(i)->getTypeID()) {
Chris Lattnera0f85e52003-08-23 20:03:05 +000084 std::cerr << "WARNING: Function [" << Old->getName()
Chris Lattner143df9a2003-11-20 18:19:35 +000085 << "]: Parameter types conflict for: '";
86 WriteTypeSymbolic(std::cerr, OldMT, &M);
87 std::cerr << "' and '";
88 WriteTypeSymbolic(std::cerr, ConcreteMT, &M);
89 std::cerr << "'\n";
Chris Lattnerb29170f2003-08-20 23:50:38 +000090 return Changed;
Chris Lattnera0f85e52003-08-23 20:03:05 +000091 }
Chris Lattnera45ec542002-10-09 21:10:06 +000092
Chris Lattner1fd95af2003-04-28 01:23:29 +000093 // Attempt to convert all of the uses of the old function to the concrete
94 // form of the function. If there is a use of the fn that we don't
95 // understand here we punt to avoid making a bad transformation.
Chris Lattnera45ec542002-10-09 21:10:06 +000096 //
Chris Lattner1fd95af2003-04-28 01:23:29 +000097 // At this point, we know that the return values are the same for our two
98 // functions and that the Old function has no varargs fns specified. In
99 // otherwords it's just <retty> (...)
Chris Lattnera45ec542002-10-09 21:10:06 +0000100 //
Chris Lattnerd514d822005-02-01 01:23:31 +0000101 if (!Old->use_empty()) {
Chris Lattner1078d112003-07-23 22:03:18 +0000102 Value *Replacement = Concrete;
103 if (Concrete->getType() != Old->getType())
Chris Lattnerd514d822005-02-01 01:23:31 +0000104 Replacement = ConstantExpr::getCast(Concrete, Old->getType());
105 NumResolved += Old->getNumUses();
Chris Lattner1078d112003-07-23 22:03:18 +0000106 Old->replaceAllUsesWith(Replacement);
107 }
Chris Lattner12ce59d2003-05-31 21:08:45 +0000108
109 // Since there are no uses of Old anymore, remove it from the module.
110 M.getFunctionList().erase(Old);
Chris Lattnera45ec542002-10-09 21:10:06 +0000111 }
112 return Changed;
113}
114
115
Chris Lattnerda902ba2003-01-30 18:22:32 +0000116static bool ResolveGlobalVariables(Module &M,
117 std::vector<GlobalValue*> &Globals,
Chris Lattnera45ec542002-10-09 21:10:06 +0000118 GlobalVariable *Concrete) {
119 bool Changed = false;
Chris Lattnerea2294a2003-04-19 00:15:27 +0000120
Chris Lattnera45ec542002-10-09 21:10:06 +0000121 for (unsigned i = 0; i != Globals.size(); ++i)
122 if (Globals[i] != Concrete) {
Reid Spencer518310c2004-07-18 00:44:37 +0000123 Constant *Cast = ConstantExpr::getCast(Concrete, Globals[i]->getType());
Chris Lattner5858e1e2003-10-21 23:17:56 +0000124 Globals[i]->replaceAllUsesWith(Cast);
Chris Lattnerea2294a2003-04-19 00:15:27 +0000125
Chris Lattnera45ec542002-10-09 21:10:06 +0000126 // Since there are no uses of Old anymore, remove it from the module.
Chris Lattner5858e1e2003-10-21 23:17:56 +0000127 M.getGlobalList().erase(cast<GlobalVariable>(Globals[i]));
Chris Lattnera45ec542002-10-09 21:10:06 +0000128
129 ++NumGlobals;
130 Changed = true;
131 }
132 return Changed;
133}
134
Chris Lattner03fb8b22003-11-20 21:21:31 +0000135// Check to see if all of the callers of F ignore the return value.
136static bool CallersAllIgnoreReturnValue(Function &F) {
137 if (F.getReturnType() == Type::VoidTy) return true;
138 for (Value::use_iterator I = F.use_begin(), E = F.use_end(); I != E; ++I) {
Reid Spencer518310c2004-07-18 00:44:37 +0000139 if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
140 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
Chris Lattner03fb8b22003-11-20 21:21:31 +0000141 I != E; ++I) {
142 CallSite CS = CallSite::get(*I);
143 if (!CS.getInstruction() || !CS.getInstruction()->use_empty())
144 return false;
145 }
146 } else {
147 CallSite CS = CallSite::get(*I);
148 if (!CS.getInstruction() || !CS.getInstruction()->use_empty())
149 return false;
150 }
151 }
152 return true;
153}
154
Chris Lattnerefd47ba2003-10-22 03:35:34 +0000155static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
Chris Lattnerda902ba2003-01-30 18:22:32 +0000156 std::vector<GlobalValue*> &Globals) {
Chris Lattnera45ec542002-10-09 21:10:06 +0000157 assert(!Globals.empty() && "Globals list shouldn't be empty here!");
158
159 bool isFunction = isa<Function>(Globals[0]); // Is this group all functions?
Chris Lattnera45ec542002-10-09 21:10:06 +0000160 GlobalValue *Concrete = 0; // The most concrete implementation to resolve to
161
Chris Lattnera45ec542002-10-09 21:10:06 +0000162 for (unsigned i = 0; i != Globals.size(); ) {
163 if (isa<Function>(Globals[i]) != isFunction) {
164 std::cerr << "WARNING: Found function and global variable with the "
165 << "same name: '" << Globals[i]->getName() << "'.\n";
166 return false; // Don't know how to handle this, bail out!
167 }
168
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000169 if (isFunction) {
Chris Lattnera45ec542002-10-09 21:10:06 +0000170 // For functions, we look to merge functions definitions of "int (...)"
171 // to 'int (int)' or 'int ()' or whatever else is not completely generic.
172 //
173 Function *F = cast<Function>(Globals[i]);
Chris Lattner6f239632002-11-08 00:38:20 +0000174 if (!F->isExternal()) {
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000175 if (Concrete && !Concrete->isExternal())
Chris Lattnera45ec542002-10-09 21:10:06 +0000176 return false; // Found two different functions types. Can't choose!
177
178 Concrete = Globals[i];
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000179 } else if (Concrete) {
Chris Lattner03fb8b22003-11-20 21:21:31 +0000180 if (Concrete->isExternal()) // If we have multiple external symbols...
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000181 if (F->getFunctionType()->getNumParams() >
182 cast<Function>(Concrete)->getFunctionType()->getNumParams())
183 Concrete = F; // We are more concrete than "Concrete"!
184
185 } else {
186 Concrete = F;
Chris Lattnera45ec542002-10-09 21:10:06 +0000187 }
Chris Lattnera45ec542002-10-09 21:10:06 +0000188 } else {
Chris Lattnera45ec542002-10-09 21:10:06 +0000189 GlobalVariable *GV = cast<GlobalVariable>(Globals[i]);
Chris Lattner5858e1e2003-10-21 23:17:56 +0000190 if (!GV->isExternal()) {
191 if (Concrete) {
192 std::cerr << "WARNING: Two global variables with external linkage"
193 << " exist with the same name: '" << GV->getName()
194 << "'!\n";
195 return false;
Chris Lattnera45ec542002-10-09 21:10:06 +0000196 }
Chris Lattner5858e1e2003-10-21 23:17:56 +0000197 Concrete = GV;
Chris Lattnera45ec542002-10-09 21:10:06 +0000198 }
Chris Lattnera45ec542002-10-09 21:10:06 +0000199 }
Chris Lattnerea2294a2003-04-19 00:15:27 +0000200 ++i;
Chris Lattnera45ec542002-10-09 21:10:06 +0000201 }
202
203 if (Globals.size() > 1) { // Found a multiply defined global...
Chris Lattnerc16e6312003-05-31 21:57:06 +0000204 // If there are no external declarations, and there is at most one
205 // externally visible instance of the global, then there is nothing to do.
206 //
207 bool HasExternal = false;
208 unsigned NumInstancesWithExternalLinkage = 0;
209
210 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
211 if (Globals[i]->isExternal())
212 HasExternal = true;
213 else if (!Globals[i]->hasInternalLinkage())
214 NumInstancesWithExternalLinkage++;
215 }
216
217 if (!HasExternal && NumInstancesWithExternalLinkage <= 1)
218 return false; // Nothing to do? Must have multiple internal definitions.
219
Chris Lattnerce94319552003-10-22 23:03:38 +0000220 // There are a couple of special cases we don't want to print the warning
221 // for, check them now.
222 bool DontPrintWarning = false;
223 if (Concrete && Globals.size() == 2) {
224 GlobalValue *Other = Globals[Globals[0] == Concrete];
225 // If the non-concrete global is a function which takes (...) arguments,
Chris Lattner03fb8b22003-11-20 21:21:31 +0000226 // and the return values match (or was never used), do not warn.
Chris Lattnerce94319552003-10-22 23:03:38 +0000227 if (Function *ConcreteF = dyn_cast<Function>(Concrete))
228 if (Function *OtherF = dyn_cast<Function>(Other))
Chris Lattner03fb8b22003-11-20 21:21:31 +0000229 if ((ConcreteF->getReturnType() == OtherF->getReturnType() ||
230 CallersAllIgnoreReturnValue(*OtherF)) &&
Chris Lattnerce94319552003-10-22 23:03:38 +0000231 OtherF->getFunctionType()->isVarArg() &&
Chris Lattnerd5d89962004-02-09 04:14:01 +0000232 OtherF->getFunctionType()->getNumParams() == 0)
Chris Lattnerce94319552003-10-22 23:03:38 +0000233 DontPrintWarning = true;
234
235 // Otherwise, if the non-concrete global is a global array variable with a
236 // size of 0, and the concrete global is an array with a real size, don't
237 // warn. This occurs due to declaring 'extern int A[];'.
238 if (GlobalVariable *ConcreteGV = dyn_cast<GlobalVariable>(Concrete))
Chris Lattner4e4c4442004-08-20 00:30:39 +0000239 if (GlobalVariable *OtherGV = dyn_cast<GlobalVariable>(Other)) {
240 const Type *CTy = ConcreteGV->getType();
241 const Type *OTy = OtherGV->getType();
242
243 if (CTy->isSized())
244 if (!OTy->isSized() || !TD.getTypeSize(OTy) ||
245 TD.getTypeSize(OTy) == TD.getTypeSize(CTy))
246 DontPrintWarning = true;
247 }
Chris Lattnerce94319552003-10-22 23:03:38 +0000248 }
Chris Lattnerc16e6312003-05-31 21:57:06 +0000249
Chris Lattner23367a72004-09-30 00:12:29 +0000250 if (0 && !DontPrintWarning) {
Chris Lattnerce94319552003-10-22 23:03:38 +0000251 std::cerr << "WARNING: Found global types that are not compatible:\n";
252 for (unsigned i = 0; i < Globals.size(); ++i) {
Chris Lattner143df9a2003-11-20 18:19:35 +0000253 std::cerr << "\t";
254 WriteTypeSymbolic(std::cerr, Globals[i]->getType(), &M);
255 std::cerr << " %" << Globals[i]->getName() << "\n";
Chris Lattnerce94319552003-10-22 23:03:38 +0000256 }
Chris Lattnera45ec542002-10-09 21:10:06 +0000257 }
258
Chris Lattner5858e1e2003-10-21 23:17:56 +0000259 if (!Concrete)
260 Concrete = Globals[0];
Chris Lattnerefd47ba2003-10-22 03:35:34 +0000261 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Concrete)) {
262 // Handle special case hack to change globals if it will make their types
263 // happier in the long run. The situation we do this is intentionally
264 // extremely limited.
265 if (GV->use_empty() && GV->hasInitializer() &&
266 GV->getInitializer()->isNullValue()) {
267 // Check to see if there is another (external) global with the same size
268 // and a non-empty use-list. If so, we will make IT be the real
269 // implementation.
270 unsigned TS = TD.getTypeSize(Concrete->getType()->getElementType());
271 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
272 if (Globals[i] != Concrete && !Globals[i]->use_empty() &&
273 isa<GlobalVariable>(Globals[i]) &&
274 TD.getTypeSize(Globals[i]->getType()->getElementType()) == TS) {
275 // At this point we want to replace Concrete with Globals[i]. Make
276 // concrete external, and Globals[i] have an initializer.
277 GlobalVariable *NGV = cast<GlobalVariable>(Globals[i]);
278 const Type *ElTy = NGV->getType()->getElementType();
279 NGV->setInitializer(Constant::getNullValue(ElTy));
280 cast<GlobalVariable>(Concrete)->setInitializer(0);
281 Concrete = NGV;
282 break;
283 }
284 }
285 }
Chris Lattner5858e1e2003-10-21 23:17:56 +0000286
Chris Lattnera45ec542002-10-09 21:10:06 +0000287 if (isFunction)
Chris Lattnerea2294a2003-04-19 00:15:27 +0000288 return ResolveFunctions(M, Globals, cast<Function>(Concrete));
Chris Lattnera45ec542002-10-09 21:10:06 +0000289 else
Chris Lattnerea2294a2003-04-19 00:15:27 +0000290 return ResolveGlobalVariables(M, Globals,
291 cast<GlobalVariable>(Concrete));
Chris Lattnera45ec542002-10-09 21:10:06 +0000292 }
Chris Lattnerea2294a2003-04-19 00:15:27 +0000293 return false;
Chris Lattnera45ec542002-10-09 21:10:06 +0000294}
295
Chris Lattnerb12914b2004-09-20 04:48:05 +0000296bool FunctionResolvingPass::runOnModule(Module &M) {
Chris Lattnerda902ba2003-01-30 18:22:32 +0000297 std::map<std::string, std::vector<GlobalValue*> > Globals;
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000298
Chris Lattner4cb766a2003-10-22 04:42:20 +0000299 // Loop over the globals, adding them to the Globals map. We use a two pass
300 // algorithm here to avoid problems with iterators getting invalidated if we
301 // did a one pass scheme.
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000302 //
Chris Lattner6be5e562003-10-22 04:43:18 +0000303 bool Changed = false;
Chris Lattner4cb766a2003-10-22 04:42:20 +0000304 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
305 Function *F = I++;
Chris Lattner6be5e562003-10-22 04:43:18 +0000306 if (F->use_empty() && F->isExternal()) {
Chris Lattner4cb766a2003-10-22 04:42:20 +0000307 M.getFunctionList().erase(F);
Chris Lattner6be5e562003-10-22 04:43:18 +0000308 Changed = true;
Chris Lattnere5ad50b2004-06-18 05:50:48 +0000309 } else if (!F->hasInternalLinkage() && !F->getName().empty() &&
310 !F->getIntrinsicID())
Chris Lattner4cb766a2003-10-22 04:42:20 +0000311 Globals[F->getName()].push_back(F);
312 }
313
Chris Lattnere4d5c442005-03-15 04:54:21 +0000314 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ) {
Chris Lattner4cb766a2003-10-22 04:42:20 +0000315 GlobalVariable *GV = I++;
Chris Lattner6be5e562003-10-22 04:43:18 +0000316 if (GV->use_empty() && GV->isExternal()) {
Chris Lattner4cb766a2003-10-22 04:42:20 +0000317 M.getGlobalList().erase(GV);
Chris Lattner6be5e562003-10-22 04:43:18 +0000318 Changed = true;
319 } else if (!GV->hasInternalLinkage() && !GV->getName().empty())
Chris Lattner4cb766a2003-10-22 04:42:20 +0000320 Globals[GV->getName()].push_back(GV);
321 }
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000322
Chris Lattnerefd47ba2003-10-22 03:35:34 +0000323 TargetData &TD = getAnalysis<TargetData>();
324
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000325 // Now we have a list of all functions with a particular name. If there is
326 // more than one entry in a list, merge the functions together.
327 //
Chris Lattnerda902ba2003-01-30 18:22:32 +0000328 for (std::map<std::string, std::vector<GlobalValue*> >::iterator
329 I = Globals.begin(), E = Globals.end(); I != E; ++I)
Chris Lattnerefd47ba2003-10-22 03:35:34 +0000330 Changed |= ProcessGlobalsWithSameName(M, TD, I->second);
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000331
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000332 // Now loop over all of the globals, checking to see if any are trivially
333 // dead. If so, remove them now.
334
335 for (Module::iterator I = M.begin(), E = M.end(); I != E; )
336 if (I->isExternal() && I->use_empty()) {
337 Function *F = I;
338 ++I;
339 M.getFunctionList().erase(F);
340 ++NumResolved;
341 Changed = true;
342 } else {
343 ++I;
344 }
345
Chris Lattnere4d5c442005-03-15 04:54:21 +0000346 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; )
Chris Lattnera2b8d7b2002-11-10 03:36:55 +0000347 if (I->isExternal() && I->use_empty()) {
348 GlobalVariable *GV = I;
349 ++I;
350 M.getGlobalList().erase(GV);
351 ++NumGlobals;
352 Changed = true;
353 } else {
354 ++I;
355 }
356
Chris Lattner22ee3eb2002-05-24 20:42:13 +0000357 return Changed;
358}