blob: 2a366c84cd62e5bb12d9b0d35c25f993af95678d [file] [log] [blame]
Chris Lattner5aa9e3e2002-05-24 20:42:13 +00001//===- FunctionResolution.cpp - Resolve declarations to implementations ---===//
John Criswell482202a2003-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 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"
25#include "llvm/iOther.h"
Chris Lattner013eca002002-10-09 21:10:06 +000026#include "llvm/Constants.h"
Chris Lattner4cd99ff2003-10-22 03:35:34 +000027#include "llvm/Target/TargetData.h"
Chris Lattnercbf08392003-08-13 22:15:04 +000028#include "llvm/Assembly/Writer.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000029#include "Support/Statistic.h"
Chris Lattner5aa9e3e2002-05-24 20:42:13 +000030#include <algorithm>
31
Brian Gaeke960707c2003-11-11 22:41:34 +000032namespace llvm {
33
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
38 struct FunctionResolvingPass : public Pass {
Chris Lattner4cd99ff2003-10-22 03:35:34 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetData>();
41 }
42
Chris Lattner113f4f42002-06-25 16:13:24 +000043 bool run(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
48Pass *createFunctionResolvingPass() {
49 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]);
58 const FunctionType *OldMT = Old->getFunctionType();
59 const FunctionType *ConcreteMT = Concrete->getFunctionType();
60
Chris Lattner9810b942003-04-28 01:23:29 +000061 if (OldMT->getParamTypes().size() > ConcreteMT->getParamTypes().size() &&
Chris Lattner50cbb902003-03-03 19:57:46 +000062 !ConcreteMT->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 }
Chris Lattner013eca002002-10-09 21:10:06 +000072
73 // Check to make sure that if there are specified types, that they
74 // match...
75 //
Chris Lattnerdbb05b02003-02-27 20:55:48 +000076 unsigned NumArguments = std::min(OldMT->getParamTypes().size(),
77 ConcreteMT->getParamTypes().size());
78
Chris Lattner50cbb902003-03-03 19:57:46 +000079 if (!Old->use_empty() && !Concrete->use_empty())
80 for (unsigned i = 0; i < NumArguments; ++i)
Chris Lattner6fc0ee92003-08-23 20:03:05 +000081 if (OldMT->getParamTypes()[i] != ConcreteMT->getParamTypes()[i])
Chris Lattner015d98e2003-08-20 23:50:38 +000082 if (OldMT->getParamTypes()[i]->getPrimitiveID() !=
Chris Lattner6fc0ee92003-08-23 20:03:05 +000083 ConcreteMT->getParamTypes()[i]->getPrimitiveID()) {
84 std::cerr << "WARNING: Function [" << Old->getName()
85 << "]: Parameter types conflict for: '" << OldMT
86 << "' and '" << ConcreteMT << "'\n";
Chris Lattner015d98e2003-08-20 23:50:38 +000087 return Changed;
Chris Lattner6fc0ee92003-08-23 20:03:05 +000088 }
Chris Lattner013eca002002-10-09 21:10:06 +000089
Chris Lattner9810b942003-04-28 01:23:29 +000090 // Attempt to convert all of the uses of the old function to the concrete
91 // form of the function. If there is a use of the fn that we don't
92 // understand here we punt to avoid making a bad transformation.
Chris Lattner013eca002002-10-09 21:10:06 +000093 //
Chris Lattner9810b942003-04-28 01:23:29 +000094 // At this point, we know that the return values are the same for our two
95 // functions and that the Old function has no varargs fns specified. In
96 // otherwords it's just <retty> (...)
Chris Lattner013eca002002-10-09 21:10:06 +000097 //
Chris Lattnere9340222003-07-23 22:03:18 +000098 if (!Old->use_empty()) { // Avoid making the CPR unless we really need it
99 Value *Replacement = Concrete;
100 if (Concrete->getType() != Old->getType())
101 Replacement = ConstantExpr::getCast(ConstantPointerRef::get(Concrete),
102 Old->getType());
103 NumResolved += Old->use_size();
104 Old->replaceAllUsesWith(Replacement);
105 }
Chris Lattner08043682003-05-31 21:08:45 +0000106
107 // Since there are no uses of Old anymore, remove it from the module.
108 M.getFunctionList().erase(Old);
Chris Lattner013eca002002-10-09 21:10:06 +0000109 }
110 return Changed;
111}
112
113
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000114static bool ResolveGlobalVariables(Module &M,
115 std::vector<GlobalValue*> &Globals,
Chris Lattner013eca002002-10-09 21:10:06 +0000116 GlobalVariable *Concrete) {
117 bool Changed = false;
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000118 Constant *CCPR = ConstantPointerRef::get(Concrete);
119
Chris Lattner013eca002002-10-09 21:10:06 +0000120 for (unsigned i = 0; i != Globals.size(); ++i)
121 if (Globals[i] != Concrete) {
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000122 Constant *Cast = ConstantExpr::getCast(CCPR, Globals[i]->getType());
123 Globals[i]->replaceAllUsesWith(Cast);
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000124
Chris Lattner013eca002002-10-09 21:10:06 +0000125 // Since there are no uses of Old anymore, remove it from the module.
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000126 M.getGlobalList().erase(cast<GlobalVariable>(Globals[i]));
Chris Lattner013eca002002-10-09 21:10:06 +0000127
128 ++NumGlobals;
129 Changed = true;
130 }
131 return Changed;
132}
133
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000134static bool ProcessGlobalsWithSameName(Module &M, TargetData &TD,
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000135 std::vector<GlobalValue*> &Globals) {
Chris Lattner013eca002002-10-09 21:10:06 +0000136 assert(!Globals.empty() && "Globals list shouldn't be empty here!");
137
138 bool isFunction = isa<Function>(Globals[0]); // Is this group all functions?
Chris Lattner013eca002002-10-09 21:10:06 +0000139 GlobalValue *Concrete = 0; // The most concrete implementation to resolve to
140
Chris Lattner013eca002002-10-09 21:10:06 +0000141 for (unsigned i = 0; i != Globals.size(); ) {
142 if (isa<Function>(Globals[i]) != isFunction) {
143 std::cerr << "WARNING: Found function and global variable with the "
144 << "same name: '" << Globals[i]->getName() << "'.\n";
145 return false; // Don't know how to handle this, bail out!
146 }
147
Chris Lattner52b8fc02002-11-10 03:36:55 +0000148 if (isFunction) {
Chris Lattner013eca002002-10-09 21:10:06 +0000149 // For functions, we look to merge functions definitions of "int (...)"
150 // to 'int (int)' or 'int ()' or whatever else is not completely generic.
151 //
152 Function *F = cast<Function>(Globals[i]);
Chris Lattner5997c3d2002-11-08 00:38:20 +0000153 if (!F->isExternal()) {
Chris Lattner52b8fc02002-11-10 03:36:55 +0000154 if (Concrete && !Concrete->isExternal())
Chris Lattner013eca002002-10-09 21:10:06 +0000155 return false; // Found two different functions types. Can't choose!
156
157 Concrete = Globals[i];
Chris Lattner52b8fc02002-11-10 03:36:55 +0000158 } else if (Concrete) {
159 if (Concrete->isExternal()) // If we have multiple external symbols...x
160 if (F->getFunctionType()->getNumParams() >
161 cast<Function>(Concrete)->getFunctionType()->getNumParams())
162 Concrete = F; // We are more concrete than "Concrete"!
163
164 } else {
165 Concrete = F;
Chris Lattner013eca002002-10-09 21:10:06 +0000166 }
Chris Lattner013eca002002-10-09 21:10:06 +0000167 } else {
Chris Lattner013eca002002-10-09 21:10:06 +0000168 GlobalVariable *GV = cast<GlobalVariable>(Globals[i]);
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000169 if (!GV->isExternal()) {
170 if (Concrete) {
171 std::cerr << "WARNING: Two global variables with external linkage"
172 << " exist with the same name: '" << GV->getName()
173 << "'!\n";
174 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000175 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000176 Concrete = GV;
Chris Lattner013eca002002-10-09 21:10:06 +0000177 }
Chris Lattner013eca002002-10-09 21:10:06 +0000178 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000179 ++i;
Chris Lattner013eca002002-10-09 21:10:06 +0000180 }
181
182 if (Globals.size() > 1) { // Found a multiply defined global...
Chris Lattner2b132962003-05-31 21:57:06 +0000183 // If there are no external declarations, and there is at most one
184 // externally visible instance of the global, then there is nothing to do.
185 //
186 bool HasExternal = false;
187 unsigned NumInstancesWithExternalLinkage = 0;
188
189 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
190 if (Globals[i]->isExternal())
191 HasExternal = true;
192 else if (!Globals[i]->hasInternalLinkage())
193 NumInstancesWithExternalLinkage++;
194 }
195
196 if (!HasExternal && NumInstancesWithExternalLinkage <= 1)
197 return false; // Nothing to do? Must have multiple internal definitions.
198
Chris Lattner9e8fe492003-10-22 23:03:38 +0000199 // There are a couple of special cases we don't want to print the warning
200 // for, check them now.
201 bool DontPrintWarning = false;
202 if (Concrete && Globals.size() == 2) {
203 GlobalValue *Other = Globals[Globals[0] == Concrete];
204 // If the non-concrete global is a function which takes (...) arguments,
205 // and the return values match, do not warn.
206 if (Function *ConcreteF = dyn_cast<Function>(Concrete))
207 if (Function *OtherF = dyn_cast<Function>(Other))
208 if (ConcreteF->getReturnType() == OtherF->getReturnType() &&
209 OtherF->getFunctionType()->isVarArg() &&
210 OtherF->getFunctionType()->getParamTypes().empty())
211 DontPrintWarning = true;
212
213 // Otherwise, if the non-concrete global is a global array variable with a
214 // size of 0, and the concrete global is an array with a real size, don't
215 // warn. This occurs due to declaring 'extern int A[];'.
216 if (GlobalVariable *ConcreteGV = dyn_cast<GlobalVariable>(Concrete))
217 if (GlobalVariable *OtherGV = dyn_cast<GlobalVariable>(Other))
218 if (const ArrayType *OtherAT =
219 dyn_cast<ArrayType>(OtherGV->getType()->getElementType()))
220 if (const ArrayType *ConcreteAT =
221 dyn_cast<ArrayType>(ConcreteGV->getType()->getElementType()))
222 if (OtherAT->getElementType() == ConcreteAT->getElementType() &&
223 OtherAT->getNumElements() == 0)
224 DontPrintWarning = true;
225 }
Chris Lattner2b132962003-05-31 21:57:06 +0000226
Chris Lattner9e8fe492003-10-22 23:03:38 +0000227 if (!DontPrintWarning) {
228 std::cerr << "WARNING: Found global types that are not compatible:\n";
229 for (unsigned i = 0; i < Globals.size(); ++i) {
230 std::cerr << "\t" << *Globals[i]->getType() << " %"
231 << Globals[i]->getName() << "\n";
232 }
Chris Lattner013eca002002-10-09 21:10:06 +0000233 }
234
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000235 if (!Concrete)
236 Concrete = Globals[0];
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000237 else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Concrete)) {
238 // Handle special case hack to change globals if it will make their types
239 // happier in the long run. The situation we do this is intentionally
240 // extremely limited.
241 if (GV->use_empty() && GV->hasInitializer() &&
242 GV->getInitializer()->isNullValue()) {
243 // Check to see if there is another (external) global with the same size
244 // and a non-empty use-list. If so, we will make IT be the real
245 // implementation.
246 unsigned TS = TD.getTypeSize(Concrete->getType()->getElementType());
247 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
248 if (Globals[i] != Concrete && !Globals[i]->use_empty() &&
249 isa<GlobalVariable>(Globals[i]) &&
250 TD.getTypeSize(Globals[i]->getType()->getElementType()) == TS) {
251 // At this point we want to replace Concrete with Globals[i]. Make
252 // concrete external, and Globals[i] have an initializer.
253 GlobalVariable *NGV = cast<GlobalVariable>(Globals[i]);
254 const Type *ElTy = NGV->getType()->getElementType();
255 NGV->setInitializer(Constant::getNullValue(ElTy));
256 cast<GlobalVariable>(Concrete)->setInitializer(0);
257 Concrete = NGV;
258 break;
259 }
260 }
261 }
Chris Lattneraf2c00b2003-10-21 23:17:56 +0000262
Chris Lattner013eca002002-10-09 21:10:06 +0000263 if (isFunction)
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000264 return ResolveFunctions(M, Globals, cast<Function>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000265 else
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000266 return ResolveGlobalVariables(M, Globals,
267 cast<GlobalVariable>(Concrete));
Chris Lattner013eca002002-10-09 21:10:06 +0000268 }
Chris Lattnerdefe5c72003-04-19 00:15:27 +0000269 return false;
Chris Lattner013eca002002-10-09 21:10:06 +0000270}
271
Chris Lattner113f4f42002-06-25 16:13:24 +0000272bool FunctionResolvingPass::run(Module &M) {
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000273 std::map<std::string, std::vector<GlobalValue*> > Globals;
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000274
Chris Lattner12b35932003-10-22 04:42:20 +0000275 // Loop over the globals, adding them to the Globals map. We use a two pass
276 // algorithm here to avoid problems with iterators getting invalidated if we
277 // did a one pass scheme.
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000278 //
Chris Lattner6d3425e2003-10-22 04:43:18 +0000279 bool Changed = false;
Chris Lattner12b35932003-10-22 04:42:20 +0000280 for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
281 Function *F = I++;
Chris Lattner6d3425e2003-10-22 04:43:18 +0000282 if (F->use_empty() && F->isExternal()) {
Chris Lattner12b35932003-10-22 04:42:20 +0000283 M.getFunctionList().erase(F);
Chris Lattner6d3425e2003-10-22 04:43:18 +0000284 Changed = true;
285 } else if (!F->hasInternalLinkage() && !F->getName().empty())
Chris Lattner12b35932003-10-22 04:42:20 +0000286 Globals[F->getName()].push_back(F);
287 }
288
289 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ) {
290 GlobalVariable *GV = I++;
Chris Lattner6d3425e2003-10-22 04:43:18 +0000291 if (GV->use_empty() && GV->isExternal()) {
Chris Lattner12b35932003-10-22 04:42:20 +0000292 M.getGlobalList().erase(GV);
Chris Lattner6d3425e2003-10-22 04:43:18 +0000293 Changed = true;
294 } else if (!GV->hasInternalLinkage() && !GV->getName().empty())
Chris Lattner12b35932003-10-22 04:42:20 +0000295 Globals[GV->getName()].push_back(GV);
296 }
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000297
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000298 TargetData &TD = getAnalysis<TargetData>();
299
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000300 // Now we have a list of all functions with a particular name. If there is
301 // more than one entry in a list, merge the functions together.
302 //
Chris Lattnerb2d9f7d2003-01-30 18:22:32 +0000303 for (std::map<std::string, std::vector<GlobalValue*> >::iterator
304 I = Globals.begin(), E = Globals.end(); I != E; ++I)
Chris Lattner4cd99ff2003-10-22 03:35:34 +0000305 Changed |= ProcessGlobalsWithSameName(M, TD, I->second);
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000306
Chris Lattner52b8fc02002-11-10 03:36:55 +0000307 // Now loop over all of the globals, checking to see if any are trivially
308 // dead. If so, remove them now.
309
310 for (Module::iterator I = M.begin(), E = M.end(); I != E; )
311 if (I->isExternal() && I->use_empty()) {
312 Function *F = I;
313 ++I;
314 M.getFunctionList().erase(F);
315 ++NumResolved;
316 Changed = true;
317 } else {
318 ++I;
319 }
320
321 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; )
322 if (I->isExternal() && I->use_empty()) {
323 GlobalVariable *GV = I;
324 ++I;
325 M.getGlobalList().erase(GV);
326 ++NumGlobals;
327 Changed = true;
328 } else {
329 ++I;
330 }
331
Chris Lattner5aa9e3e2002-05-24 20:42:13 +0000332 return Changed;
333}
Brian Gaeke960707c2003-11-11 22:41:34 +0000334
335} // End llvm namespace